Skip to content

Commit 41039dc

Browse files
committed
tokio_main: move reboot detection to separate background task
1 parent 2880366 commit 41039dc

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/main.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ async fn enable_usb_if_present(usb: &mut Option<UsbGadgetState>, accessory_start
167167
}
168168
}
169169

170-
async fn action_handler(config: &mut SharedConfig) -> Result<()> {
170+
async fn action_handler(config: &mut SharedConfig) {
171171
// check pending action
172172
let action = config.read().await.action_requested.clone();
173173
if let Some(action) = action {
@@ -179,12 +179,10 @@ async fn action_handler(config: &mut SharedConfig) -> Result<()> {
179179
tokio::time::sleep(std::time::Duration::from_secs(60)).await;
180180
}
181181
}
182-
183-
Ok(())
184182
}
185183

186184
async fn tokio_main(
187-
mut config: SharedConfig,
185+
config: SharedConfig,
188186
config_json: SharedConfigJson,
189187
need_restart: Arc<Notify>,
190188
tcp_start: Arc<Notify>,
@@ -242,11 +240,17 @@ async fn tokio_main(
242240
usb = Some(UsbGadgetState::new(cfg.legacy, cfg.udc.clone()));
243241
}
244242

243+
// spawn a background task for reboot detection
244+
let mut config_cloned = config.clone();
245+
let _ = tokio::spawn(async move {
246+
loop {
247+
action_handler(&mut config_cloned).await;
248+
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
249+
}
250+
});
251+
245252
let change_usb_order = cfg.change_usb_order;
246253
loop {
247-
// check if we need to reboot
248-
action_handler(&mut config).await?;
249-
250254
if let Some(ref mut usb) = usb {
251255
if let Err(e) = usb.init() {
252256
error!("{} 🔌 USB init error: {}", NAME, e);
@@ -261,9 +265,6 @@ async fn tokio_main(
261265

262266
if let Some(ref wifi_conf) = wifi_conf {
263267
loop {
264-
// check if we need to reboot
265-
action_handler(&mut config).await?;
266-
267268
// read and clone the effective config in advance to avoid holding the lock
268269
let cfg = config.read().await.clone();
269270

@@ -305,9 +306,6 @@ async fn tokio_main(
305306
// wait for restart
306307
need_restart.notified().await;
307308

308-
// check if we need to reboot
309-
action_handler(&mut config).await?;
310-
311309
// TODO: make proper main loop with cancelation
312310
info!(
313311
"{} 📵 TCP/USB connection closed or not started, trying again...",

0 commit comments

Comments
 (0)