garage44.eu/jeroen/pinnacle

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

cartopolis-agent · 2026-11-30T16:44:52Z

feat(bt): read a paired keyboard over HID over GATT

bt::hogp finds a keyboard's boot input among its services and starts it:
the boot protocol asked for (Protocol Mode, written without a response:
Host::write_command), then its input report's notifications on. Its
keyboard_service is the HID service a boot keyboard offers, which the
simulated keyboard serves. A test pairs one by passkey and has its keys
notified over the encrypted link, heard by no listener.
bt/src/gatt.rs  | 13 ++++++++
 bt/src/hogp.rs  | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 bt/src/host.rs  |  6 ++++
 bt/src/lib.rs   |  1 +
 bt/src/tests.rs | 72 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 186 insertions(+)

diff --git a/bt/src/gatt.rs b/bt/src/gatt.rs
index 975e579..9fb9deb 100644
--- a/bt/src/gatt.rs
+++ b/bt/src/gatt.rs
@@ -62,6 +62,12 @@ impl Uuid {
             Uuid::Short(MODEL_NUMBER) => "Model",
             Uuid::Short(BATTERY_LEVEL) => "Battery Level",
             Uuid::Short(HEART_RATE_MEASUREMENT) => "Heart Rate Measurement",
+            Uuid::Short(HID_INFORMATION) => "HID Information",
+            Uuid::Short(REPORT_MAP) => "Report Map",
+            Uuid::Short(HID_CONTROL_POINT) => "HID Control Point",
+            Uuid::Short(REPORT) => "Report",
+            Uuid::Short(PROTOCOL_MODE) => "Protocol Mode",
+            Uuid::Short(BOOT_KEYBOARD_INPUT) => "Boot Keyboard Input Report",
             Uuid::Long(PINNACLE_SERVICE) => "Pinnacle",
             Uuid::Long(PINNACLE_CARD) => "Contact Card",
             _ => return None,
@@ -83,6 +89,13 @@ pub const MANUFACTURER_NAME: u16 = 0x2a29;
 pub const MODEL_NUMBER: u16 = 0x2a24;
 pub const BATTERY_LEVEL: u16 = 0x2a19;
 pub const HEART_RATE_MEASUREMENT: u16 = 0x2a37;
+// HID over GATT's (HIDS 1.0).
+pub const BOOT_KEYBOARD_INPUT: u16 = 0x2a22;
+pub const HID_INFORMATION: u16 = 0x2a4a;
+pub const REPORT_MAP: u16 = 0x2a4b;
+pub const HID_CONTROL_POINT: u16 = 0x2a4c;
+pub const REPORT: u16 = 0x2a4d;
+pub const PROTOCOL_MODE: u16 = 0x2a4e;
 // Declarations.
 pub const PRIMARY_SERVICE: u16 = 0x2800;
 pub const CHARACTERISTIC: u16 = 0x2803;
diff --git a/bt/src/hogp.rs b/bt/src/hogp.rs
new file mode 100644
index 0000000..8be29b9
--- /dev/null
+++ b/bt/src/hogp.rs
@@ -0,0 +1,94 @@
+//! HID over GATT (HOGP 1.0), as the host a keyboard types into: of the
+//! services a keyboard offers, the handles its keys are read by. Pinnacle
+//! asks for the boot protocol, whose input report is the eight bytes every
+//! keyboard can send (`pinnacle_hid::Keyboard` reads them), so no report
+//! map need be read; a keyboard's own report protocol, its media keys and
+//! its lights are not used.
+//!
+//! The host reads keys only from a keyboard it paired with, over the
+//! encrypted link: a device nearby that was never paired cannot type.
+
+use crate::Host;
+use crate::gatt::{self, Service, Uuid};
+
+/// What the boot protocol is asked with: Protocol Mode's value.
+pub const BOOT_PROTOCOL: u8 = 0x00;
+
+/// A keyboard's boot input, as found in its services.
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub struct BootKeyboard {
+    /// The Protocol Mode characteristic's value handle, if it has one (a
+    /// keyboard in the boot protocol only may not).
+    pub protocol_mode: Option<u16>,
+    /// The Boot Keyboard Input Report's value handle, and its client
+    /// configuration's, which turns its notifications on.
+    pub input: u16,
+    pub configuration: u16,
+}
+
+/// Has a connected keyboard send its keys: the boot protocol asked for
+/// (written without a response, as HOGP has it), then its input report's
+/// notifications on; each report then comes as [`Event::Notified`] of
+/// [`BootKeyboard::input`].
+///
+/// [`Event::Notified`]: crate::Event::Notified
+pub fn start(host: &mut Host, connection: u16, keyboard: &BootKeyboard) {
+    if let Some(mode) = keyboard.protocol_mode {
+        host.write_command(connection, mode, &[BOOT_PROTOCOL]);
+    }
+    host.subscribe(connection, keyboard.configuration, true);
+}
+
+/// The boot keyboard among a device's services, if it is one.
+pub fn boot_keyboard(services: &[Service]) -> Option<BootKeyboard> {
+    let hid = services
+        .iter()
+        .find(|s| s.uuid == Uuid::Short(gatt::HUMAN_INTERFACE_DEVICE))?;
+    let find = |uuid: u16| hid.characteristics.iter().find(|c| c.uuid == Uuid::Short(uuid));
+    let input = find(gatt::BOOT_KEYBOARD_INPUT)?;
+    Some(BootKeyboard {
+        protocol_mode: find(gatt::PROTOCOL_MODE).map(|c| c.handle),
+        input: input.handle,
+        configuration: input.configuration?,
+    })
+}
+
+/// The report descriptor of a keyboard with only the boot report (HID
+/// 1.11, appendix E.6): its Report Map.
+const BOOT_REPORT_MAP: [u8; 63] = [
+    0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25, 0x01, //
+    0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x01, 0x95, 0x05, 0x75, 0x01, //
+    0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91, 0x01, 0x95, 0x06, //
+    0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x05, 0x07, 0x19, 0x00, 0x29, 0x65, 0x81, 0x00, 0xc0,
+];
+
+/// The HID service of a keyboard that speaks the boot protocol, as a
+/// keyboard offers it: what the simulated keyboard serves. Its input
+/// report is set ([`Host::set_value`]) to the eight bytes held.
+pub fn keyboard_service() -> Service {
+    use gatt::{Characteristic, PROP_NOTIFY, PROP_READ, PROP_WRITE_NO_RESPONSE};
+    Service::new(
+        Uuid::Short(gatt::HUMAN_INTERFACE_DEVICE),
+        alloc::vec![
+            // Version 1.11, no country, may wake the host, normally connectable.
+            Characteristic::new(
+                Uuid::Short(gatt::HID_INFORMATION),
+                PROP_READ,
+                &[0x11, 0x01, 0x00, 0x02]
+            ),
+            Characteristic::new(Uuid::Short(gatt::REPORT_MAP), PROP_READ, &BOOT_REPORT_MAP),
+            Characteristic::new(Uuid::Short(gatt::HID_CONTROL_POINT), PROP_WRITE_NO_RESPONSE, &[0]),
+            // The report protocol until told otherwise.
+            Characteristic::new(
+                Uuid::Short(gatt::PROTOCOL_MODE),
+                PROP_READ | PROP_WRITE_NO_RESPONSE,
+                &[0x01],
+            ),
+            Characteristic::new(
+                Uuid::Short(gatt::BOOT_KEYBOARD_INPUT),
+                PROP_READ | PROP_NOTIFY,
+                &[0; 8],
+            ),
+        ],
+    )
+}
diff --git a/bt/src/host.rs b/bt/src/host.rs
index 909e179..1aca05c 100644
--- a/bt/src/host.rs
+++ b/bt/src/host.rs
@@ -800,6 +800,12 @@ impl Host {
         );
     }
 
+    /// Writes a characteristic's value without a response (a command):
+    /// nothing follows, and it does not wait behind other requests.
+    pub fn write_command(&mut self, connection: u16, handle: u16, value: &[u8]) {
+        self.send_att(connection, &att::write_command(handle, value));
+    }
+
     /// Asks for notifications of a characteristic, through its client
     /// configuration descriptor (`configuration` of a found one).
     pub fn subscribe(&mut self, connection: u16, configuration: u16, on: bool) {
diff --git a/bt/src/lib.rs b/bt/src/lib.rs
index 4b9505d..3c9787d 100644
--- a/bt/src/lib.rs
+++ b/bt/src/lib.rs
@@ -27,6 +27,7 @@ extern crate alloc;
 pub mod att;
 pub mod gatt;
 pub mod hci;
+pub mod hogp;
 pub mod host;
 pub mod sim;
 pub mod smp;
diff --git a/bt/src/tests.rs b/bt/src/tests.rs
index 0e8de78..2e76542 100644
--- a/bt/src/tests.rs
+++ b/bt/src/tests.rs
@@ -876,6 +876,78 @@ fn passkey_entry_either_way_round() {
     }
 }
 
+/// A keyboard over HID over GATT: found by its services, paired by the
+/// passkey typed on it, then in the boot protocol, its keys notified over
+/// the encrypted link and heard by no one else.
+#[test]
+fn a_paired_keyboard_sends_its_keys_privately() {
+    use crate::hogp;
+    let mut w = World::new(alloc::vec![
+        Host::new("Phone", Vec::new()),
+        Host::new("Keyboard", alloc::vec![hogp::keyboard_service()]),
+    ]);
+    w.hosts[0].enable_pairing([1; 32], smp::DISPLAY_YES_NO);
+    w.hosts[1].enable_pairing([2; 32], smp::KEYBOARD_ONLY);
+    w.hosts[1].advertise(true);
+    w.settle();
+    let (ca, cb) = connect(&mut w);
+    w.hosts[0].discover(ca);
+    w.settle();
+    let services = w
+        .events(0)
+        .into_iter()
+        .find_map(|e| match e {
+            Event::Discovered { services, .. } => Some(services),
+            _ => None,
+        })
+        .expect("services");
+    let keyboard = hogp::boot_keyboard(&services).expect("a keyboard");
+    assert!(keyboard.protocol_mode.is_some());
+    w.hosts[0].pair(ca);
+    w.settle();
+    let Some(Some(shown)) = passkey(&w.events(0)) else {
+        panic!("no passkey shown");
+    };
+    w.events(1);
+    w.hosts[1].enter_passkey(cb, shown);
+    w.settle();
+    assert!(w.hosts[0].encrypted(ca));
+    w.events(0);
+    w.events(1);
+
+    hogp::start(&mut w.hosts[0], ca, &keyboard);
+    w.settle();
+    let mode = w.hosts[1]
+        .table()
+        .value_handle(
+            Uuid::Short(gatt::HUMAN_INTERFACE_DEVICE),
+            Uuid::Short(gatt::PROTOCOL_MODE),
+        )
+        .unwrap();
+    assert_eq!(
+        w.hosts[1].table().get(mode).map(|a| a.value.clone()),
+        Some(alloc::vec![hogp::BOOT_PROTOCOL]),
+        "the boot protocol asked for"
+    );
+    let input = w.hosts[1]
+        .table()
+        .value_handle(
+            Uuid::Short(gatt::HUMAN_INTERFACE_DEVICE),
+            Uuid::Short(gatt::BOOT_KEYBOARD_INPUT),
+        )
+        .unwrap();
+    assert_eq!(input, keyboard.input);
+    // "q" held: usage 0x14, with Shift, an unlikely pattern on the air.
+    let report = [0x02, 0, 0x14, 0x14, 0x14, 0x14, 0, 0];
+    w.hosts[1].set_value(input, &report);
+    w.settle();
+    assert!(has(
+        &w.events(0),
+        |e| matches!(e, Event::Notified { handle, value, .. } if *handle == keyboard.input && value == &report)
+    ));
+    assert!(!heard(&w, &report));
+}
+
 #[test]
 fn numbers_not_confirmed_pair_nothing() {
     let mut w = pairing_world([smp::DISPLAY_YES_NO; 2]);