@@ -19,10 +19,14 @@ use std::fs;
1919use std:: fs:: OpenOptions ;
2020use std:: path:: PathBuf ;
2121use std:: process:: Command ;
22+ use std:: sync:: atomic:: AtomicBool ;
23+ use std:: sync:: atomic:: Ordering ;
2224use std:: sync:: Arc ;
2325use std:: thread;
2426use std:: time:: Duration ;
2527use tokio:: runtime:: Builder ;
28+ use tokio:: sync:: broadcast;
29+ use tokio:: sync:: broadcast:: Sender as BroadcastSender ;
2630use tokio:: sync:: mpsc:: Sender ;
2731use tokio:: sync:: Mutex ;
2832use tokio:: sync:: Notify ;
@@ -179,12 +183,13 @@ async fn action_handler(config: &mut SharedConfig) {
179183async fn tokio_main (
180184 config : SharedConfig ,
181185 config_json : SharedConfigJson ,
182- need_restart : Arc < Notify > ,
186+ restart_tx : BroadcastSender < ( ) > ,
183187 tcp_start : Arc < Notify > ,
184188 config_file : PathBuf ,
185189 tx : Arc < Mutex < Option < Sender < Packet > > > > ,
186190 sensor_channel : Arc < Mutex < Option < u8 > > > ,
187191 led_support : bool ,
192+ profile_connected : Arc < AtomicBool > ,
188193) -> Result < ( ) > {
189194 let accessory_started = Arc :: new ( Notify :: new ( ) ) ;
190195 let accessory_started_cloned = accessory_started. clone ( ) ;
@@ -275,6 +280,7 @@ async fn tokio_main(
275280
276281 // main connection loop
277282 let change_usb_order = cfg. change_usb_order ;
283+ let mut need_restart = restart_tx. subscribe ( ) ;
278284 loop {
279285 if let Some ( ref mut leds) = led_manager {
280286 leds. set_led ( LedColor :: Green , LedMode :: Heartbeat ) . await ;
@@ -289,21 +295,27 @@ async fn tokio_main(
289295 enable_usb_if_present ( & mut usb, accessory_started. clone ( ) ) . await ;
290296 }
291297
292- // bluetooth handshake
293- if let Err ( e) = bluetooth
294- . aa_handshake (
295- cfg. dongle_mode ,
296- cfg. connect . clone ( ) ,
297- wifi_conf. clone ( ) . unwrap ( ) ,
298- tcp_start. clone ( ) ,
299- Duration :: from_secs ( cfg. bt_timeout_secs . into ( ) ) ,
300- cfg. action_requested == Some ( Action :: Stop ) ,
301- )
302- . await
303- {
304- error ! ( "{} bluetooth AA handshake error: {}" , NAME , e) ;
305- tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) . await ;
306- continue ;
298+ // run only if not handling this in handshake task
299+ if !( cfg. quick_reconnect && profile_connected. load ( Ordering :: Relaxed ) ) {
300+ // bluetooth handshake
301+ if let Err ( e) = bluetooth
302+ . aa_handshake (
303+ cfg. dongle_mode ,
304+ cfg. connect . clone ( ) ,
305+ wifi_conf. clone ( ) . unwrap ( ) ,
306+ tcp_start. clone ( ) ,
307+ Duration :: from_secs ( cfg. bt_timeout_secs . into ( ) ) ,
308+ cfg. action_requested == Some ( Action :: Stop ) ,
309+ cfg. quick_reconnect ,
310+ restart_tx. subscribe ( ) ,
311+ profile_connected. clone ( ) ,
312+ )
313+ . await
314+ {
315+ error ! ( "{} bluetooth AA handshake error: {}" , NAME , e) ;
316+ tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) . await ;
317+ continue ;
318+ }
307319 }
308320
309321 if !change_usb_order {
@@ -315,14 +327,21 @@ async fn tokio_main(
315327 leds. set_led ( LedColor :: Blue , LedMode :: On ) . await ;
316328 }
317329 // wait for restart notification
318- need_restart. notified ( ) . await ;
330+ let _ = need_restart. recv ( ) . await ;
331+ if !( cfg. quick_reconnect && profile_connected. load ( Ordering :: Relaxed ) ) {
332+ info ! (
333+ "{} 📵 TCP/USB connection closed or not started, trying again..." ,
334+ NAME
335+ ) ;
336+ tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) . await ;
337+ } else {
338+ info ! (
339+ "{} 📵 TCP/USB connection closed or not started, quick restart..." ,
340+ NAME
341+ ) ;
342+ }
319343
320344 // TODO: make proper main loop with cancelation
321- info ! (
322- "{} 📵 TCP/USB connection closed or not started, trying again..." ,
323- NAME
324- ) ;
325- tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) . await ;
326345 // re-read config
327346 cfg = config. read ( ) . await . clone ( ) ;
328347 }
@@ -525,8 +544,7 @@ fn main() -> Result<()> {
525544 }
526545
527546 // notify for syncing threads
528- let need_restart = Arc :: new ( Notify :: new ( ) ) ;
529- let need_restart_cloned = need_restart. clone ( ) ;
547+ let ( restart_tx, _) = broadcast:: channel ( 1 ) ;
530548 let tcp_start = Arc :: new ( Notify :: new ( ) ) ;
531549 let tcp_start_cloned = tcp_start. clone ( ) ;
532550 let config = Arc :: new ( RwLock :: new ( config) ) ;
@@ -536,30 +554,35 @@ fn main() -> Result<()> {
536554 let tx_cloned = tx. clone ( ) ;
537555 let sensor_channel = Arc :: new ( Mutex :: new ( None ) ) ;
538556 let sensor_channel_cloned = sensor_channel. clone ( ) ;
557+ let profile_connected = Arc :: new ( AtomicBool :: new ( false ) ) ;
539558
540559 // build and spawn main tokio runtime
541560 let runtime = Builder :: new_multi_thread ( ) . enable_all ( ) . build ( ) . unwrap ( ) ;
561+ let restart_tx_cloned = restart_tx. clone ( ) ;
562+ let profile_connected_cloned = profile_connected. clone ( ) ;
542563 runtime. spawn ( async move {
543564 tokio_main (
544565 config_cloned,
545566 config_json. clone ( ) ,
546- need_restart ,
567+ restart_tx_cloned ,
547568 tcp_start,
548569 args. config . clone ( ) ,
549570 tx_cloned,
550571 sensor_channel_cloned,
551572 led_support,
573+ profile_connected_cloned,
552574 )
553575 . await
554576 } ) ;
555577
556578 // start tokio_uring runtime simultaneously
557579 let _ = tokio_uring:: start ( io_loop (
558- need_restart_cloned ,
580+ restart_tx ,
559581 tcp_start_cloned,
560582 config,
561583 tx,
562584 sensor_channel,
585+ profile_connected,
563586 ) ) ;
564587
565588 info ! (
0 commit comments