garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

cartopolis-agent · 2026-11-30T15:13:21Z

feat(xtask): the paired Pinnacle in the air moves its address in the Bluetooth demo

The air's devices have identities now. After the bonded device is opened
again, Pinnacle Test moves to a new resolvable private address, as a phone
does, and the demo shows it still paired and nearby, then opens it: the link
is private again with no number to compare. The old address no longer
answers, so the connection went to the new one. cargo xtask bluetooth-demo
runs the Bluetooth part alone.
README.md         |  1 +
 xtask/src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/README.md b/README.md
index c430806..11daf5e 100644
--- a/README.md
+++ b/README.md
@@ -73,6 +73,7 @@ cargo xtask contacts-demo              # the address book: write, keep, export,
 cargo xtask files-demo                 # Files: notes found in Notes' folder, one opened and moved, one deleted
 cargo xtask notices-demo               # past notifications: two texts, their list, one opened from it, cleared
 cargo xtask network-demo               # Settings' network page: online, switched off, on again
+cargo xtask bluetooth-demo             # the devices around, one paired, then found again after it moved its address
 cargo xtask access-demo                # five scripts, Settings in Dutch, larger text, high contrast, the screen reader
 cargo xtask sound-demo                 # the volume kept at a restart, Settings' slider moved, its tone as loud as it says
 cargo xtask updates-demo               # an app's new version: told, then installed by itself
diff --git a/xtask/src/main.rs b/xtask/src/main.rs
index 44b3c6f..3b8b80a 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -104,6 +104,7 @@ fn main() -> ExitCode {
         ["sms-demo"] => sms_demo(),
         ["notices-demo"] => notices_demo(),
         ["network-demo"] => network_demo(),
+        ["bluetooth-demo"] => bluetooth_only_demo(),
         ["access-demo"] => access_demo(),
         ["sound-demo"] => sound_demo(),
         ["updates-demo"] => updates_demo(),
@@ -6403,6 +6404,11 @@ fn expect_tone(path: &Path, hz: f64, seconds: f64) -> Result {
     }
 }
 
+/// The identity resolving key of the air's `n`th device (from 1).
+fn air_irk(n: u8) -> [u8; 16] {
+    [0x1d ^ n; 16]
+}
+
 /// The air a guest's Bluetooth controller is in: a simulated controller
 /// behind the guest's serial port (a Unix socket QEMU serves), and devices
 /// of our own beside it, each a host of its own: "Pinnacle Test" with its
@@ -6436,6 +6442,7 @@ impl SimAir {
             let i = air.add(pinnacle_bt::Address([n, 0x5e, 0x1d, 0x00, 0x4e, 0xd0]));
             let mut host = pinnacle_bt::Host::new(name, services);
             host.enable_pairing([n; 32], io);
+            host.set_identity(air_irk(n));
             host.advertise(true);
             devices.push((i, host));
         };
@@ -6518,6 +6525,21 @@ impl SimAir {
         SimAir { inner }
     }
 
+    /// The `n`th device of our own (from 0, in the order added) moves to a
+    /// new private address made from its identity key, as a phone does every
+    /// few minutes; the old one no longer answers. Returns the new address.
+    fn move_device(&self, n: usize) -> Result<pinnacle_bt::Address> {
+        let mut inner = self.inner.lock().map_err(|e| e.to_string())?;
+        let i = inner.devices.get(n).ok_or("no such device")?.0;
+        let prand = (inner.start.elapsed().as_nanos() as u32).to_le_bytes();
+        let address = pinnacle_bt::Address(pinnacle_bt::smp::private_address(
+            &air_irk(n as u8 + 1),
+            [prand[0], prand[1], prand[2]],
+        ));
+        inner.air.controllers[i].move_to(address);
+        Ok(address)
+    }
+
     /// The socket path for a new guest; QEMU serves it, this connects.
     fn socket_for_guest(&self) -> PathBuf {
         let n = SOCKETS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
@@ -6650,10 +6672,33 @@ fn bluetooth_demo(s: &mut Session, dir: &Path) -> Result {
     std::thread::sleep(Duration::from_millis(800));
     s.shot(&dir.join("bt-8-private-again.png"))?;
     s.tap_named(B, "disconnect")?;
+    std::thread::sleep(Duration::from_millis(800));
+    // It moves to a new private address, as a phone does every few
+    // minutes: still the paired one nearby, and private again when opened,
+    // with no number to compare. The old address no longer answers, so a
+    // connection at all went to the new one.
+    let moved = air().move_device(0)?;
+    println!("xtask: Pinnacle Test moved to {}", moved.text());
+    std::thread::sleep(Duration::from_millis(1200));
+    s.shot(&dir.join("bt-9-moved.png"))?;
+    s.tap_named(B, "device-0")?;
+    s.wait_for("bt: connected to")?;
+    s.wait_for("bt: the link to")?;
+    std::thread::sleep(Duration::from_millis(800));
+    s.shot(&dir.join("bt-10-moved-private.png"))?;
+    s.tap_named(B, "disconnect")?;
     std::thread::sleep(Duration::from_millis(400));
     s.home()
 }
 
+/// The Bluetooth app alone, as `demo` drives it.
+fn bluetooth_only_demo() -> Result {
+    let dir = demo_dir("demo-bluetooth")?;
+    let mut s = Session::start()?;
+    s.wait_for(&home_apps(0))?;
+    bluetooth_demo(&mut s, &dir)
+}
+
 /// The one air every guest of this run is in.
 fn air() -> &'static SimAir {
     static AIR: std::sync::OnceLock<SimAir> = std::sync::OnceLock::new();