Skip to content

Commit 7842b75

Browse files
committed
fix stop_on_disconnect together with quick_reconnect
1 parent 157f20c commit 7842b75

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/bluetooth.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::btle;
2+
use crate::config::Action;
23
use crate::config::WifiConfig;
34
use crate::config::IDENTITY_NAME;
45
use crate::config_types::BluetoothAddressList;
@@ -567,7 +568,7 @@ impl Bluetooth {
567568
bt_timeout: Duration,
568569
stopped: bool,
569570
quick_reconnect: bool,
570-
mut need_restart: BroadcastReceiver<()>,
571+
mut need_restart: BroadcastReceiver<Option<Action>>,
571572
profile_connected: Arc<AtomicBool>,
572573
) -> Result<()> {
573574
// Use the provided session and adapter instead of creating new ones
@@ -580,11 +581,22 @@ impl Bluetooth {
580581
if quick_reconnect {
581582
// keep the bluetooth profile connection alive
582583
// and use it in a loop to restart handshake when necessary
584+
let adapter_cloned = self.adapter.clone();
583585
let _ = Some(tokio::spawn(async move {
584586
profile_connected.store(true, Ordering::Relaxed);
585587
loop {
586588
// wait for restart notification from main loop (eg when HU disconnected)
587-
let _ = need_restart.recv().await;
589+
let action = need_restart.recv().await;
590+
if let Ok(Some(action)) = action {
591+
// check if we need to stop now
592+
if action == Action::Stop {
593+
// disconnect and break
594+
if let Ok(device) = adapter_cloned.device(bluer::Address(*address)) {
595+
let _ = device.disconnect().await;
596+
}
597+
break;
598+
}
599+
}
588600

589601
// now restart handshake with the same params
590602
match Self::send_params(wifi_config.clone(), &mut stream).await {

src/io_uring.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ async fn tcp_wait_for_connection(listener: &mut TcpListener) -> Result<TcpStream
285285
}
286286

287287
pub async fn io_loop(
288-
need_restart: BroadcastSender<()>,
288+
need_restart: BroadcastSender<Option<Action>>,
289289
tcp_start: Arc<Notify>,
290290
config: SharedConfig,
291291
tx: Arc<Mutex<Option<Sender<Packet>>>>,
@@ -334,7 +334,7 @@ pub async fn io_loop(
334334
error!("{} 🔴 Enabling Android Auto: {}", NAME, e);
335335
// notify main loop to restart
336336
if !profile_connected.load(Ordering::Relaxed) {
337-
let _ = need_restart.send(());
337+
let _ = need_restart.send(None);
338338
}
339339
continue;
340340
}
@@ -355,7 +355,7 @@ pub async fn io_loop(
355355
} else {
356356
// notify main loop to restart
357357
if !profile_connected.load(Ordering::Relaxed) {
358-
let _ = need_restart.send(());
358+
let _ = need_restart.send(None);
359359
}
360360
continue;
361361
}
@@ -371,7 +371,7 @@ pub async fn io_loop(
371371
} else {
372372
// notify main loop to restart
373373
if !profile_connected.load(Ordering::Relaxed) {
374-
let _ = need_restart.send(());
374+
let _ = need_restart.send(None);
375375
}
376376
continue;
377377
}
@@ -392,7 +392,7 @@ pub async fn io_loop(
392392
error!("{} 🔴 Error opening USB accessory: {}", NAME, e);
393393
// notify main loop to restart
394394
if !profile_connected.load(Ordering::Relaxed) {
395-
let _ = need_restart.send(());
395+
let _ = need_restart.send(None);
396396
}
397397
continue;
398398
}
@@ -552,8 +552,10 @@ pub async fn io_loop(
552552
NAME,
553553
format_duration(started.elapsed()).to_string()
554554
);
555+
// obtain action for passing it to broadcast sender
556+
let action = shared_config.read().await.action_requested.clone();
555557
// stream(s) closed, notify main loop to restart
556-
let _ = need_restart.send(());
558+
let _ = need_restart.send(action);
557559
}
558560

559561
#[allow(unreachable_code)]

src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ async fn action_handler(config: &mut SharedConfig) {
183183
async fn tokio_main(
184184
config: SharedConfig,
185185
config_json: SharedConfigJson,
186-
restart_tx: BroadcastSender<()>,
186+
restart_tx: BroadcastSender<Option<Action>>,
187187
tcp_start: Arc<Notify>,
188188
config_file: PathBuf,
189189
tx: Arc<Mutex<Option<Sender<Packet>>>>,
@@ -296,7 +296,9 @@ async fn tokio_main(
296296
}
297297

298298
// run only if not handling this in handshake task
299-
if !(cfg.quick_reconnect && profile_connected.load(Ordering::Relaxed)) {
299+
if !(cfg.quick_reconnect && profile_connected.load(Ordering::Relaxed))
300+
|| cfg.action_requested == Some(Action::Stop)
301+
{
300302
// bluetooth handshake
301303
if let Err(e) = bluetooth
302304
.aa_handshake(

0 commit comments

Comments
 (0)