Skip to content

Commit 74579b0

Browse files
committed
Disable old wrap REST code and pass tx and sensor_channel to new context
1 parent 6acb347 commit 74579b0

File tree

5 files changed

+50
-100
lines changed

5 files changed

+50
-100
lines changed

src/ev.rs

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use simplelog::*;
22
use std::path::PathBuf;
3-
use std::sync::Arc;
43
use tokio::fs;
54
use tokio::sync::mpsc::Sender;
6-
use tokio::sync::Mutex;
75

86
// protobuf stuff:
97
include!(concat!(env!("OUT_DIR"), "/protos/mod.rs"));
@@ -14,7 +12,6 @@ use crate::mitm::{ENCRYPTED, FRAME_TYPE_FIRST, FRAME_TYPE_LAST};
1412
use protobuf::Message;
1513

1614
use serde::Deserialize;
17-
use warp::Filter;
1815

1916
pub static FORD_EV_MODEL: &[u8] = include_bytes!("protos/ford_ev_model.bin");
2017
pub const EV_MODEL_FILE: &str = "/etc/aa-proxy-rs/ev_model.bin";
@@ -31,68 +28,6 @@ pub struct BatteryData {
3128
pub battery_level: f32,
3229
}
3330

34-
// reset server context
35-
#[derive(Clone)]
36-
pub struct RestContext {
37-
pub sensor_channel: Option<u8>,
38-
pub ev_battery_capacity: u64,
39-
pub ev_factor: f32,
40-
}
41-
42-
pub async fn rest_server(tx: Sender<Packet>, ctx: Arc<Mutex<RestContext>>) -> Result<()> {
43-
let battery_route = warp::post()
44-
.and(warp::path("battery"))
45-
.and(warp::body::json())
46-
.and(warp::any().map({
47-
let ctx = ctx.clone();
48-
move || ctx.clone()
49-
}))
50-
.and_then(move |data: BatteryData, ctx: Arc<Mutex<RestContext>>| {
51-
let tx = tx.clone();
52-
async move {
53-
if data.battery_level < 0.0 || data.battery_level > 100.0 {
54-
let msg = format!(
55-
"battery_level out of range: {} (expected 0.0–100.0)",
56-
data.battery_level
57-
);
58-
return Ok::<_, warp::Rejection>(warp::reply::with_status(
59-
msg,
60-
warp::http::StatusCode::BAD_REQUEST,
61-
));
62-
}
63-
64-
info!("{} Received battery level: {}", NAME, data.battery_level);
65-
let rest_ctx = ctx.lock().await;
66-
if let Some(ch) = rest_ctx.sensor_channel {
67-
if let Err(e) = send_ev_data(
68-
tx,
69-
data.battery_level,
70-
ch,
71-
rest_ctx.ev_battery_capacity,
72-
rest_ctx.ev_factor,
73-
)
74-
.await
75-
{
76-
error!("{} EV model error: {}", NAME, e);
77-
}
78-
} else {
79-
warn!("{} Not sending packet because no sensor channel yet", NAME);
80-
}
81-
82-
Ok(warp::reply::with_status(
83-
"OK".into(),
84-
warp::http::StatusCode::OK,
85-
))
86-
}
87-
});
88-
89-
info!("{} Server running on http://127.0.0.1:3030", NAME);
90-
91-
warp::serve(battery_route).run(([127, 0, 0, 1], 3030)).await;
92-
93-
Ok(())
94-
}
95-
9631
fn scale_percent_to_value(percent: f32, max_value: u64) -> u64 {
9732
let scaled = (percent as f64 / 100.0) * max_value as f64;
9833
scaled.round() as u64

src/io_uring.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ pub const BUFFER_LEN: usize = 16 * 1024;
3333
const TCP_CLIENT_TIMEOUT: Duration = Duration::new(30, 0);
3434

3535
use crate::config::SharedConfig;
36-
use crate::ev::{rest_server, RestContext};
3736
use crate::mitm::endpoint_reader;
3837
use crate::mitm::proxy;
3938
use crate::mitm::Packet;
@@ -210,6 +209,8 @@ pub async fn io_loop(
210209
need_restart: Arc<Notify>,
211210
tcp_start: Arc<Notify>,
212211
config: SharedConfig,
212+
tx: Arc<Mutex<Option<Sender<Packet>>>>,
213+
sensor_channel: Arc<Mutex<Option<u8>>>,
213214
) -> Result<()> {
214215
// prepare/bind needed TCP listeners
215216
let mut dhu_listener = None;
@@ -367,19 +368,9 @@ pub async fn io_loop(
367368
}
368369

369370
// handling battery in JSON
370-
let mut rest_server_handle = None;
371-
let mut rest_ctx = None;
372371
if config.mitm && config.ev {
373-
let ctx = RestContext {
374-
sensor_channel: None,
375-
ev_battery_capacity: config.ev_battery_capacity,
376-
ev_factor: config.ev_factor,
377-
};
378-
let ctx = Arc::new(Mutex::new(ctx));
379-
380-
let tx = tx_hu.clone();
381-
rest_server_handle = Some(tokio::spawn(rest_server(tx, ctx.clone())));
382-
rest_ctx = Some(ctx);
372+
let mut tx_lock = tx.lock().await;
373+
*tx_lock = Some(tx_hu.clone());
383374
}
384375

385376
// dedicated reading threads:
@@ -394,7 +385,7 @@ pub async fn io_loop(
394385
rx_hu,
395386
rxr_md,
396387
config.clone(),
397-
rest_ctx.clone(),
388+
sensor_channel.clone(),
398389
));
399390
from_stream = tokio_uring::spawn(proxy(
400391
ProxyType::MobileDevice,
@@ -404,7 +395,7 @@ pub async fn io_loop(
404395
rx_md,
405396
rxr_hu,
406397
config.clone(),
407-
rest_ctx.clone(),
398+
sensor_channel.clone(),
408399
));
409400

410401
// Thread for monitoring transfer
@@ -439,9 +430,12 @@ pub async fn io_loop(
439430
from_file.abort();
440431
from_stream.abort();
441432
monitor.abort();
442-
if let Some(handle) = rest_server_handle {
443-
handle.abort();
444-
}
433+
434+
// set webserver context EV stuff to None
435+
let mut tx_lock = tx.lock().await;
436+
*tx_lock = None;
437+
let mut sc_lock = sensor_channel.lock().await;
438+
*sc_lock = None;
445439
// stop EV battery logger if neded
446440
if let Some(ref path) = config.ev_battery_logger {
447441
let _ = Command::new(path).arg("stop").spawn();

src/main.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod web;
1010

1111
use crate::config::AppConfig;
1212
use crate::config::SharedConfig;
13+
use crate::mitm::Packet;
1314
use bluetooth::bluetooth_setup_connection;
1415
use bluetooth::bluetooth_stop;
1516
use clap::Parser;
@@ -25,6 +26,8 @@ use std::path::PathBuf;
2526
use std::sync::Arc;
2627
use std::time::Duration;
2728
use tokio::runtime::Builder;
29+
use tokio::sync::mpsc::Sender;
30+
use tokio::sync::Mutex;
2831
use tokio::sync::Notify;
2932
use tokio::time::Instant;
3033

@@ -162,6 +165,8 @@ async fn tokio_main(
162165
need_restart: Arc<Notify>,
163166
tcp_start: Arc<Notify>,
164167
config_file: PathBuf,
168+
tx: Arc<Mutex<Option<Sender<Packet>>>>,
169+
sensor_channel: Arc<Mutex<Option<u8>>>,
165170
) {
166171
let accessory_started = Arc::new(Notify::new());
167172
let accessory_started_cloned = accessory_started.clone();
@@ -171,8 +176,8 @@ async fn tokio_main(
171176
let state = web::AppState {
172177
config: config.clone(),
173178
config_file: config_file.into(),
174-
tx: None,
175-
sensor_channel: None.into(),
179+
tx,
180+
sensor_channel,
176181
};
177182
let app = web::app(state.into());
178183

@@ -326,15 +331,33 @@ fn main() {
326331
let tcp_start_cloned = tcp_start.clone();
327332
let config = Arc::new(RwLock::new(config));
328333
let config_cloned = config.clone();
334+
let tx = Arc::new(Mutex::new(None));
335+
let tx_cloned = tx.clone();
336+
let sensor_channel = Arc::new(Mutex::new(None));
337+
let sensor_channel_cloned = sensor_channel.clone();
329338

330339
// build and spawn main tokio runtime
331340
let runtime = Builder::new_multi_thread().enable_all().build().unwrap();
332341
runtime.spawn(async move {
333-
tokio_main(config_cloned, need_restart, tcp_start, args.config.clone()).await
342+
tokio_main(
343+
config_cloned,
344+
need_restart,
345+
tcp_start,
346+
args.config.clone(),
347+
tx_cloned,
348+
sensor_channel_cloned,
349+
)
350+
.await
334351
});
335352

336353
// start tokio_uring runtime simultaneously
337-
let _ = tokio_uring::start(io_loop(need_restart_cloned, tcp_start_cloned, config));
354+
let _ = tokio_uring::start(io_loop(
355+
need_restart_cloned,
356+
tcp_start_cloned,
357+
config,
358+
tx,
359+
sensor_channel,
360+
));
338361

339362
info!(
340363
"🚩 aa-proxy-rs terminated, running time: {}",

src/mitm.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use protos::ControlMessageType::{self, *};
2929

3030
use crate::config::AppConfig;
3131
use crate::config::HexdumpLevel;
32-
use crate::ev::RestContext;
3332
use crate::io_uring::Endpoint;
3433
use crate::io_uring::IoDevice;
3534
use crate::io_uring::BUFFER_LEN;
@@ -271,7 +270,7 @@ pub async fn pkt_modify_hook(
271270
video_in_motion: bool,
272271
ev: bool,
273272
ctx: &mut ModifyContext,
274-
rest_ctx: Option<Arc<tokio::sync::Mutex<RestContext>>>,
273+
sensor_channel: Arc<tokio::sync::Mutex<Option<u8>>>,
275274
ev_battery_logger: Option<PathBuf>,
276275
) -> Result<bool> {
277276
// if for some reason we have too small packet, bail out
@@ -413,10 +412,8 @@ pub async fn pkt_modify_hook(
413412
// set in local context
414413
ctx.sensor_channel = Some(svc.id() as u8);
415414
// set in REST server context for remote EV requests
416-
if let Some(ctx) = rest_ctx {
417-
let mut rest_ctx = ctx.lock().await;
418-
rest_ctx.sensor_channel = Some(svc.id() as u8);
419-
}
415+
let mut sc_lock = sensor_channel.lock().await;
416+
*sc_lock = Some(svc.id() as u8);
420417
}
421418
}
422419

@@ -652,7 +649,7 @@ pub async fn proxy<A: Endpoint<A> + 'static>(
652649
mut rx: Receiver<Packet>,
653650
mut rxr: Receiver<Packet>,
654651
config: AppConfig,
655-
rest_ctx: Option<Arc<tokio::sync::Mutex<RestContext>>>,
652+
sensor_channel: Arc<tokio::sync::Mutex<Option<u8>>>,
656653
) -> Result<()> {
657654
let passthrough = !config.mitm;
658655
let hex_requested = config.hexdump_level;
@@ -798,7 +795,7 @@ pub async fn proxy<A: Endpoint<A> + 'static>(
798795
config.video_in_motion,
799796
config.ev,
800797
&mut ctx,
801-
rest_ctx.clone(),
798+
sensor_channel.clone(),
802799
config.ev_battery_logger.clone(),
803800
)
804801
.await?;
@@ -843,7 +840,7 @@ pub async fn proxy<A: Endpoint<A> + 'static>(
843840
config.video_in_motion,
844841
config.ev,
845842
&mut ctx,
846-
rest_ctx.clone(),
843+
sensor_channel.clone(),
847844
config.ev_battery_logger.clone(),
848845
)
849846
.await?;

src/web.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::sync::Arc;
2121
use tokio::fs;
2222
use tokio::io::AsyncWriteExt;
2323
use tokio::sync::mpsc::Sender;
24+
use tokio::sync::Mutex;
2425

2526
const TEMPLATE: &str = include_str!("../static/index.html");
2627
const PICO_CSS: &str = include_str!("../static/pico.min.css");
@@ -32,8 +33,8 @@ const NAME: &str = "<i><bright-black> web: </>";
3233
pub struct AppState {
3334
pub config: SharedConfig,
3435
pub config_file: Arc<PathBuf>,
35-
pub tx: Option<Sender<Packet>>,
36-
pub sensor_channel: Arc<Option<u8>>,
36+
pub tx: Arc<Mutex<Option<Sender<Packet>>>>,
37+
pub sensor_channel: Arc<Mutex<Option<u8>>>,
3738
}
3839

3940
pub fn app(state: Arc<AppState>) -> Router {
@@ -70,8 +71,8 @@ pub async fn battery_handler(
7071

7172
info!("{} Received battery level: {}", NAME, data.battery_level);
7273

73-
if let Some(ch) = *state.sensor_channel {
74-
if let Some(tx) = &state.tx {
74+
if let Some(ch) = *state.sensor_channel.lock().await {
75+
if let Some(tx) = state.tx.lock().await.clone() {
7576
if let Err(e) = send_ev_data(tx.clone(), data.battery_level, ch, 0, 0.0).await {
7677
error!("{} EV model error: {}", NAME, e);
7778
}

0 commit comments

Comments
 (0)