Skip to content

Commit 03c3b81

Browse files
committed
use SharedConfig instead of single variables where applicable
1 parent b109195 commit 03c3b81

File tree

3 files changed

+53
-100
lines changed

3 files changed

+53
-100
lines changed

src/io_uring.rs

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use humantime::format_duration;
33
use simplelog::*;
44
use std::cell::RefCell;
55
use std::marker::PhantomData;
6-
use std::path::PathBuf;
76
use std::process::Command;
87
use std::rc::Rc;
98
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -33,8 +32,7 @@ const USB_ACCESSORY_PATH: &str = "/dev/usb_accessory";
3332
pub const BUFFER_LEN: usize = 16 * 1024;
3433
const TCP_CLIENT_TIMEOUT: Duration = Duration::new(30, 0);
3534

36-
use crate::config::HexdumpLevel;
37-
use crate::config::UsbId;
35+
use crate::config::SharedConfig;
3836
use crate::ev::{rest_server, RestContext};
3937
use crate::mitm::endpoint_reader;
4038
use crate::mitm::proxy;
@@ -197,31 +195,18 @@ pub async fn io_loop(
197195
need_restart: Arc<Notify>,
198196
tcp_start: Arc<Notify>,
199197
read_timeout: Duration,
200-
mitm: bool,
201-
dpi: u16,
202-
developer_mode: bool,
203-
disable_media_sink: bool,
204-
disable_tts_sink: bool,
205-
remove_tap_restriction: bool,
206-
video_in_motion: bool,
207-
hex_requested: HexdumpLevel,
208-
wired: Option<UsbId>,
209-
dhu: bool,
210-
ev: bool,
211-
ev_battery_logger: Option<PathBuf>,
212-
ev_battery_capacity: u64,
213-
ev_factor: f32,
198+
config: SharedConfig,
214199
) -> Result<()> {
215200
// prepare/bind needed TCP listeners
216201
let mut dhu_listener = None;
217202
let mut md_listener = None;
218-
if !wired.is_some() {
203+
if !config.read().await.wired.is_some() {
219204
info!("{} 🛰️ Starting TCP server for MD...", NAME);
220205
let bind_addr = format!("0.0.0.0:{}", TCP_SERVER_PORT).parse().unwrap();
221206
md_listener = Some(TcpListener::bind(bind_addr).unwrap());
222207
info!("{} 🛰️ MD TCP server bound to: <u>{}</u>", NAME, bind_addr);
223208
}
224-
if dhu {
209+
if config.read().await.dhu {
225210
info!("{} 🛰️ Starting TCP server for DHU...", NAME);
226211
let bind_addr = format!("0.0.0.0:{}", TCP_DHU_PORT).parse().unwrap();
227212
dhu_listener = Some(TcpListener::bind(bind_addr).unwrap());
@@ -233,12 +218,12 @@ pub async fn io_loop(
233218
let mut md_usb = None;
234219
let mut hu_tcp = None;
235220
let mut hu_usb = None;
236-
if wired.is_some() {
221+
if config.read().await.wired.is_some() {
237222
info!(
238223
"{} 💤 trying to enable Android Auto mode on USB port...",
239224
NAME
240225
);
241-
match usb_stream::new(wired.clone()).await {
226+
match usb_stream::new(config.read().await.wired.clone()).await {
242227
Err(e) => {
243228
error!("{} 🔴 Enabling Android Auto: {}", NAME, e);
244229
// notify main loop to restart
@@ -266,7 +251,7 @@ pub async fn io_loop(
266251
}
267252
}
268253

269-
if dhu {
254+
if config.read().await.dhu {
270255
info!(
271256
"{} 🛰️ DHU TCP server: listening for `Desktop Head Unit` connection...",
272257
NAME
@@ -355,11 +340,11 @@ pub async fn io_loop(
355340
// handling battery in JSON
356341
let mut rest_server_handle = None;
357342
let mut rest_ctx = None;
358-
if mitm && ev {
343+
if config.read().await.mitm && config.read().await.ev {
359344
let ctx = RestContext {
360345
sensor_channel: None,
361-
ev_battery_capacity,
362-
ev_factor,
346+
ev_battery_capacity: config.read().await.ev_battery_capacity,
347+
ev_factor: config.read().await.ev_factor,
363348
};
364349
let ctx = Arc::new(Mutex::new(ctx));
365350

@@ -379,17 +364,8 @@ pub async fn io_loop(
379364
tx_hu.clone(),
380365
rx_hu,
381366
rxr_md,
382-
dpi,
383-
developer_mode,
384-
disable_media_sink,
385-
disable_tts_sink,
386-
remove_tap_restriction,
387-
video_in_motion,
388-
!mitm,
389-
hex_requested,
390-
ev,
367+
config.clone(),
391368
rest_ctx.clone(),
392-
ev_battery_logger.clone(),
393369
));
394370
from_stream = tokio_uring::spawn(proxy(
395371
ProxyType::MobileDevice,
@@ -398,17 +374,8 @@ pub async fn io_loop(
398374
tx_md,
399375
rx_md,
400376
rxr_hu,
401-
dpi,
402-
developer_mode,
403-
disable_media_sink,
404-
disable_tts_sink,
405-
remove_tap_restriction,
406-
video_in_motion,
407-
!mitm,
408-
hex_requested,
409-
ev,
377+
config.clone(),
410378
rest_ctx.clone(),
411-
ev_battery_logger.clone(),
412379
));
413380

414381
// Thread for monitoring transfer
@@ -446,7 +413,7 @@ pub async fn io_loop(
446413
handle.abort();
447414
}
448415
// stop EV battery logger if neded
449-
if let Some(ref path) = ev_battery_logger {
416+
if let Some(ref path) = config.read().await.ev_battery_logger {
450417
let _ = Command::new(path).arg("stop").spawn();
451418
}
452419

src/main.rs

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod usb_stream;
99
mod web;
1010

1111
use crate::config::AppConfig;
12+
use crate::config::SharedConfig;
1213
use bluetooth::bluetooth_setup_connection;
1314
use bluetooth::bluetooth_stop;
1415
use clap::Parser;
@@ -157,18 +158,18 @@ fn logging_init(debug: bool, disable_console_debug: bool, log_path: &PathBuf) {
157158
}
158159

159160
async fn tokio_main(
160-
config: AppConfig,
161+
config: SharedConfig,
161162
need_restart: Arc<Notify>,
162163
tcp_start: Arc<Notify>,
163164
config_file: PathBuf,
164165
) {
165166
let accessory_started = Arc::new(Notify::new());
166167
let accessory_started_cloned = accessory_started.clone();
167168

168-
if let Some(ref bindaddr) = config.webserver {
169+
if let Some(ref bindaddr) = config.read().await.webserver {
169170
// preparing AppState and starting webserver
170171
let state = web::AppState {
171-
config: Arc::new(RwLock::new(config.clone())),
172+
config: config.clone(),
172173
config_file: config_file.into(),
173174
};
174175
let app = web::app(state.into());
@@ -193,19 +194,25 @@ async fn tokio_main(
193194
}
194195

195196
let wifi_conf = {
196-
if !config.wired.is_some() {
197-
Some(init_wifi_config(&config.iface, config.hostapd_conf))
197+
if !config.read().await.wired.is_some() {
198+
Some(init_wifi_config(
199+
&config.read().await.iface,
200+
config.read().await.hostapd_conf.clone(),
201+
))
198202
} else {
199203
None
200204
}
201205
};
202206
let mut usb = None;
203-
if !config.dhu {
204-
if config.legacy {
207+
if !config.read().await.dhu {
208+
if config.read().await.legacy {
205209
// start uevent listener in own task
206210
std::thread::spawn(|| uevent_listener(accessory_started_cloned));
207211
}
208-
usb = Some(UsbGadgetState::new(config.legacy, config.udc));
212+
usb = Some(UsbGadgetState::new(
213+
config.read().await.legacy,
214+
config.read().await.udc.clone(),
215+
));
209216
}
210217
loop {
211218
if let Some(ref mut usb) = usb {
@@ -218,13 +225,13 @@ async fn tokio_main(
218225
if let Some(ref wifi_conf) = wifi_conf {
219226
loop {
220227
match bluetooth_setup_connection(
221-
config.advertise,
222-
config.btalias.clone(),
223-
config.connect,
228+
config.read().await.advertise,
229+
config.read().await.btalias.clone(),
230+
config.read().await.connect,
224231
wifi_conf.clone(),
225232
tcp_start.clone(),
226-
config.keepalive,
227-
Duration::from_secs(config.bt_timeout_secs.into()),
233+
config.read().await.keepalive,
234+
Duration::from_secs(config.read().await.bt_timeout_secs.into()),
228235
)
229236
.await
230237
{
@@ -331,47 +338,22 @@ fn main() {
331338
let need_restart_cloned = need_restart.clone();
332339
let tcp_start = Arc::new(Notify::new());
333340
let tcp_start_cloned = tcp_start.clone();
334-
let mitm = config.mitm;
335-
let dpi = config.dpi;
336-
let developer_mode = config.developer_mode;
337-
let disable_media_sink = config.disable_media_sink;
338-
let disable_tts_sink = config.disable_tts_sink;
339-
let remove_tap_restriction = config.remove_tap_restriction;
340-
let video_in_motion = config.video_in_motion;
341-
let hex_requested = config.hexdump_level;
342-
let wired = config.wired.clone();
343-
let dhu = config.dhu;
344-
let ev = config.ev;
345-
let ev_battery_logger = config.ev_battery_logger.clone();
346-
let ev_battery_capacity = config.ev_battery_capacity.clone();
347-
let ev_factor = config.ev_factor.clone();
341+
let config = Arc::new(RwLock::new(config));
342+
let config_cloned = config.clone();
348343

349344
// build and spawn main tokio runtime
350345
let runtime = Builder::new_multi_thread().enable_all().build().unwrap();
351-
runtime.spawn(
352-
async move { tokio_main(config, need_restart, tcp_start, args.config.clone()).await },
353-
);
346+
runtime.spawn(async move {
347+
tokio_main(config_cloned, need_restart, tcp_start, args.config.clone()).await
348+
});
354349

355350
// start tokio_uring runtime simultaneously
356351
let _ = tokio_uring::start(io_loop(
357352
stats_interval,
358353
need_restart_cloned,
359354
tcp_start_cloned,
360355
read_timeout,
361-
mitm,
362-
dpi,
363-
developer_mode,
364-
disable_media_sink,
365-
disable_tts_sink,
366-
remove_tap_restriction,
367-
video_in_motion,
368-
hex_requested,
369-
wired,
370-
dhu,
371-
ev,
372-
ev_battery_logger,
373-
ev_battery_capacity,
374-
ev_factor,
356+
config,
375357
));
376358

377359
info!(

src/mitm.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use protobuf::{Enum, Message, MessageDyn};
2828
use protos::ControlMessageType::{self, *};
2929

3030
use crate::config::HexdumpLevel;
31+
use crate::config::SharedConfig;
3132
use crate::ev::RestContext;
3233
use crate::io_uring::Endpoint;
3334
use crate::io_uring::IoDevice;
@@ -650,18 +651,21 @@ pub async fn proxy<A: Endpoint<A> + 'static>(
650651
tx: Sender<Packet>,
651652
mut rx: Receiver<Packet>,
652653
mut rxr: Receiver<Packet>,
653-
dpi: u16,
654-
developer_mode: bool,
655-
disable_media_sink: bool,
656-
disable_tts_sink: bool,
657-
remove_tap_restriction: bool,
658-
video_in_motion: bool,
659-
passthrough: bool,
660-
hex_requested: HexdumpLevel,
661-
ev: bool,
654+
config: SharedConfig,
662655
rest_ctx: Option<Arc<tokio::sync::Mutex<RestContext>>>,
663-
ev_battery_logger: Option<PathBuf>,
664656
) -> Result<()> {
657+
// load needed config options to local variables
658+
let passthrough = !config.read().await.mitm;
659+
let hex_requested = config.read().await.hexdump_level;
660+
let dpi = config.read().await.dpi;
661+
let developer_mode = config.read().await.developer_mode;
662+
let disable_media_sink = config.read().await.disable_media_sink;
663+
let disable_tts_sink = config.read().await.disable_tts_sink;
664+
let remove_tap_restriction = config.read().await.remove_tap_restriction;
665+
let video_in_motion = config.read().await.video_in_motion;
666+
let ev = config.read().await.ev;
667+
let ev_battery_logger = &config.read().await.ev_battery_logger;
668+
665669
// in full_frames/passthrough mode we only directly pass packets from one endpoint to the other
666670
if passthrough {
667671
loop {

0 commit comments

Comments
 (0)