git clone https://garage44.eu/jeroen/pinnacle.gitcartopolis-agent · 2026-11-30T19:14:22Z
refactor(bt): one name lookup and one notification for both pairing questions The passkey landing (s940) copied the numeric-comparison branch's lookup of a device's advertised name and its lazily connected notification to the display server; Paired had a third copy of the lookup, and the KEYBOARD body was built by hand in three places. Now found_name, notify and keyboard_bytes, each once. Behaviour unchanged.
programs/src/bin/bt.rs | 75 ++++++++++++++++++++++++--------------------------
1 file changed, 36 insertions(+), 39 deletions(-)
diff --git a/programs/src/bin/bt.rs b/programs/src/bin/bt.rs
index 572a2c8..18d12a9 100644
--- a/programs/src/bin/bt.rs
+++ b/programs/src/bin/bt.rs
@@ -168,6 +168,34 @@ fn connection_bytes(connection: u16, address: &pinnacle_bt::Address) -> Vec<u8>
b
}
+/// KEYBOARD's body: the keyboard on `connection` types into this device,
+/// or (`on` false) stopped.
+fn keyboard_bytes(connection: u16, address: &pinnacle_bt::Address, on: bool) -> Vec<u8> {
+ let mut b = connection_bytes(connection, address);
+ b.push(u8::from(on));
+ b
+}
+
+/// The name a device nearby gave in its advertising, if it gave one.
+fn found_name(host: &Host, address: Address) -> Option<String> {
+ host.found()
+ .iter()
+ .find(|f| f.address == address)
+ .and_then(|f| f.name.clone())
+}
+
+/// A pairing asked by a device nearby with no app open: a notification,
+/// whose tap opens Bluetooth. The display server is connected to the
+/// first time.
+fn notify(display: &mut Option<Channel>, who: &str, text: &str) {
+ if display.is_none() {
+ *display = pinnacle_rt::connect("display");
+ }
+ if let Some(display) = display {
+ pinnacle_ui::notify(display, "bluetooth", who, text);
+ }
+}
+
/// A keyboard typing into this device: its connection, who, the handle
/// its reports come by, and its keys held.
struct Typing {
@@ -213,8 +241,7 @@ impl Keyboards {
keys: Keyboard::default(),
});
println!("bt: keys from {} go to the app in front", peer.text());
- let mut b = connection_bytes(connection, &peer);
- b.push(1);
+ let b = keyboard_bytes(connection, &peer, true);
said(clients, protocol::KEYBOARD, &b);
}
@@ -238,8 +265,7 @@ impl Keyboards {
if let Some(i) = self.typing.iter().position(|t| t.connection == connection) {
let mut t = self.typing.remove(i);
send_keys(out, t.keys.release_all());
- let mut b = connection_bytes(connection, &t.peer);
- b.push(0);
+ let b = keyboard_bytes(connection, &t.peer, false);
said(clients, protocol::KEYBOARD, &b);
}
}
@@ -360,8 +386,7 @@ fn main(argument: u64) -> u64 {
client.send(&protocol::message(protocol::ENCRYPTED, &b), Vec::new());
}
if keyboards.typing(c) {
- let mut b = connection_bytes(c, &address);
- b.push(1);
+ let b = keyboard_bytes(c, &address, true);
client.send(&protocol::message(protocol::KEYBOARD, &b), Vec::new());
}
}
@@ -559,24 +584,12 @@ fn main(argument: u64) -> u64 {
println!("bt: pairing with {} asks the person", peer.text());
said(&clients, protocol::ASK, &b);
if clients.is_empty() {
- // Asked by a device nearby with no app open: a
- // notification, whose tap opens Bluetooth.
- let who = host
- .found()
- .iter()
- .find(|f| f.address == peer)
- .and_then(|f| f.name.clone())
- .unwrap_or_else(|| peer.text());
+ let who = found_name(&host, peer).unwrap_or_else(|| peer.text());
let text = match code {
Some(code) => alloc::format!("Pair if it shows {code:06}"),
None => String::from("Wants to pair"),
};
- if display.is_none() {
- display = pinnacle_rt::connect("display");
- }
- if let Some(display) = &display {
- pinnacle_ui::notify(display, "bluetooth", &who, &text);
- }
+ notify(&mut display, &who, &text);
}
asking = Some((protocol::ASK, b));
}
@@ -585,25 +598,14 @@ fn main(argument: u64) -> u64 {
peer,
passkey: Some(passkey),
} => {
- let who = host
- .found()
- .iter()
- .find(|f| f.address == peer)
- .and_then(|f| f.name.clone())
- .unwrap_or_else(|| peer.text());
+ let who = found_name(&host, peer).unwrap_or_else(|| peer.text());
println!("bt: pairing with {who}: type {passkey:06} on it");
let mut b = connection_bytes(connection, &peer);
b.extend_from_slice(&passkey.to_le_bytes());
said(&clients, protocol::PASSKEY, &b);
if clients.is_empty() {
- // A keyboard asked to pair with no app open.
let text = alloc::format!("Type {passkey:06} on it, then Enter");
- if display.is_none() {
- display = pinnacle_rt::connect("display");
- }
- if let Some(display) = &display {
- pinnacle_ui::notify(display, "bluetooth", &who, &text);
- }
+ notify(&mut display, &who, &text);
}
asking = Some((protocol::PASSKEY, b));
}
@@ -612,12 +614,7 @@ fn main(argument: u64) -> u64 {
Event::Passkey { connection, .. } => host.confirm(connection, false),
Event::Paired { connection, bond } => {
asking = None;
- let name = host
- .found()
- .iter()
- .find(|f| f.address == bond.address)
- .and_then(|f| f.name.clone())
- .unwrap_or_default();
+ let name = found_name(&host, bond.address).unwrap_or_default();
println!(
"bt: paired with {}",
if name.is_empty() {