garage44.eu/jeroen/pinnacle

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

.. /bt/src/hogp.rs

raw

//! 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],
            ),
        ],
    )
}