Skip to content

Commit 8a010a8

Browse files
committed
chore: make ble signalling cleaner
1 parent fb17c69 commit 8a010a8

File tree

3 files changed

+61
-23
lines changed

3 files changed

+61
-23
lines changed

src/bluetooth.rs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::{state::GlobalState, structs::BleDisplayDevice};
1+
use crate::{
2+
state::{BleAction, GlobalState, MenuScene},
3+
structs::BleDisplayDevice,
4+
};
25
use alloc::string::ToString;
36
use core::cell::RefCell;
47
use embassy_futures::select::{Either, select, select3, select4};
@@ -18,6 +21,26 @@ pub async fn bluetooth_timer_task(
1821
state: GlobalState,
1922
) {
2023
loop {
24+
let (mut has_bond_info, bond_info) =
25+
if let Some(bond_info) = load_bonding_info(&state.nvs).await {
26+
log::info!("Bond stored.");
27+
(true, Some(bond_info))
28+
} else {
29+
log::info!("No bond stored.");
30+
31+
let current_menu_scene = state.state.lock().await.menu_scene.clone();
32+
if current_menu_scene != Some(MenuScene::BtDisplay) {
33+
loop {
34+
let sig = state.ble_sig.wait().await;
35+
if let BleAction::StartScan = sig {
36+
break;
37+
}
38+
}
39+
}
40+
41+
(false, None)
42+
};
43+
2144
let Ok(connector) = BleConnector::new(
2245
init,
2346
unsafe { bt.clone_unchecked() },
@@ -42,16 +65,6 @@ pub async fn bluetooth_timer_task(
4265
..
4366
} = stack.build();
4467

45-
let (mut has_bond_info, bond_info) =
46-
if let Some(bond_info) = load_bonding_info(&state.nvs).await {
47-
log::info!("Bond stored. Adding to stack.");
48-
stack.add_bond_information(bond_info.clone()).unwrap();
49-
(true, Some(bond_info))
50-
} else {
51-
log::info!("No bond stored.");
52-
(false, None)
53-
};
54-
5568
let mut central = if !has_bond_info {
5669
let discovery_channel: Channel<NoopRawMutex, BleDisplayDevice, 10> =
5770
embassy_sync::channel::Channel::new();
@@ -98,7 +111,7 @@ pub async fn bluetooth_timer_task(
98111
async {
99112
loop {
100113
Timer::after_millis(200).await;
101-
if state.ble_connect_sig.signaled() {
114+
if state.ble_sig.signaled() {
102115
break;
103116
}
104117
}
@@ -108,12 +121,21 @@ pub async fn bluetooth_timer_task(
108121

109122
scanner.into_inner()
110123
} else {
124+
stack
125+
.add_bond_information(bond_info.clone().unwrap())
126+
.unwrap();
127+
111128
central
112129
};
113130

114131
let display_addr = match bond_info {
115132
Some(ref bond_info) => bond_info.identity.bd_addr.into_inner(),
116-
None => state.ble_connect_sig.wait().await.addr,
133+
None => loop {
134+
let sig = state.ble_sig.wait().await;
135+
if let BleAction::Connect(d) = sig {
136+
break d.addr;
137+
}
138+
},
117139
};
118140
let target: Address = Address::random(display_addr);
119141

@@ -129,7 +151,12 @@ pub async fn bluetooth_timer_task(
129151
let _ = select3(
130152
runner.run(),
131153
async {
132-
state.ble_unpair_sig.wait().await;
154+
loop {
155+
let sig = state.ble_sig.wait().await;
156+
if let BleAction::Unpair = sig {
157+
break;
158+
}
159+
}
133160
},
134161
async {
135162
'outer: loop {

src/buttons.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::{
22
stackmat::CURRENT_TIME,
3-
state::{GlobalState, MenuScene, Scene, current_epoch, deeper_sleep_state, sleep_state},
3+
state::{
4+
BleAction, GlobalState, MenuScene, Scene, current_epoch, deeper_sleep_state, sleep_state,
5+
},
46
structs::DelegateResponsePacket,
57
utils::buttons::{Button, ButtonTrigger, ButtonsHandler},
68
};
@@ -183,16 +185,19 @@ async fn submit_up(
183185
);
184186

185187
_ = state.nvs.invalidate_key(b"BONDING_KEY").await;
186-
state.ble_connect_sig.signal(
187-
state_val.discovered_bluetooth_devices[state_val.selected_bluetooth_item]
188-
.clone(),
188+
state.ble_sig.signal(
189+
BleAction::Connect(
190+
state_val.discovered_bluetooth_devices[state_val.selected_bluetooth_item]
191+
.clone(),
192+
)
193+
.clone(),
189194
);
190195
} else if state_val.selected_bluetooth_item
191196
== state_val.discovered_bluetooth_devices.len()
192197
{
193198
log::debug!("[BtD] Unpair current device");
194199
_ = state.nvs.invalidate_key(b"BONDING_KEY").await;
195-
state.ble_unpair_sig.signal(());
200+
state.ble_sig.signal(BleAction::Unpair);
196201
} else if state_val.selected_bluetooth_item
197202
== state_val.discovered_bluetooth_devices.len() + 1
198203
{
@@ -223,6 +228,7 @@ async fn submit_up(
223228
}
224229
1 => {
225230
state_val.menu_scene = Some(MenuScene::BtDisplay);
231+
state.ble_sig.signal(BleAction::StartScan);
226232
}
227233
2 => {
228234
state_val.menu_scene = Some(MenuScene::Signing);

src/state.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ impl End2End {
135135
}
136136
}
137137

138+
#[derive(Debug, Clone, PartialEq)]
139+
pub enum BleAction {
140+
Connect(BleDisplayDevice),
141+
StartScan,
142+
Unpair,
143+
}
144+
138145
pub type GlobalState = Rc<GlobalStateInner>;
139146
pub struct GlobalStateInner {
140147
pub state: SignaledMutex<CriticalSectionRawMutex, SignaledGlobalStateInner>,
@@ -143,8 +150,7 @@ pub struct GlobalStateInner {
143150
pub show_battery: Signal<CriticalSectionRawMutex, u8>,
144151
pub update_progress: Signal<CriticalSectionRawMutex, u8>,
145152
pub sign_unsign_progress: Signal<CriticalSectionRawMutex, bool>,
146-
pub ble_connect_sig: Signal<CriticalSectionRawMutex, BleDisplayDevice>,
147-
pub ble_unpair_sig: Signal<CriticalSectionRawMutex, ()>,
153+
pub ble_sig: Signal<CriticalSectionRawMutex, BleAction>,
148154

149155
pub nvs: Nvs,
150156
pub aes: Mutex<NoopRawMutex, Aes<'static>>,
@@ -162,8 +168,7 @@ impl GlobalStateInner {
162168
show_battery: Signal::new(),
163169
update_progress: Signal::new(),
164170
sign_unsign_progress: Signal::new(),
165-
ble_connect_sig: Signal::new(),
166-
ble_unpair_sig: Signal::new(),
171+
ble_sig: Signal::new(),
167172

168173
nvs: nvs.clone(),
169174
aes: Mutex::new(Aes::new(aes)),

0 commit comments

Comments
 (0)