//! GATT: services made of characteristics, as an attribute table a server //! answers from and a client discovers. use alloc::string::String; use alloc::vec::Vec; /// A UUID: the Bluetooth SIG's 16-bit ones, or a full 128-bit one of our /// own, both as the ATT protocol sends them (least significant byte first). #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] pub enum Uuid { Short(u16), Long([u8; 16]), } impl Uuid { pub fn bytes(&self) -> Vec { match self { Uuid::Short(u) => u.to_le_bytes().to_vec(), Uuid::Long(b) => b.to_vec(), } } pub fn parse(bytes: &[u8]) -> Option { match bytes.len() { 2 => Some(Uuid::Short(u16::from_le_bytes([bytes[0], bytes[1]]))), 16 => Some(Uuid::Long(bytes.try_into().ok()?)), _ => None, } } /// "180f" or the 128-bit form "12345678-1234-5678-1234-56789abcdef0". pub fn text(&self) -> String { match self { Uuid::Short(u) => alloc::format!("{u:04x}"), Uuid::Long(b) => { let h = |i: usize| alloc::format!("{:02x}", b[15 - i]); let part = |range: core::ops::Range| range.map(h).collect::(); alloc::format!( "{}-{}-{}-{}-{}", part(0..4), part(4..6), part(6..8), part(8..10), part(10..16) ) } } } /// What the SIG's assigned numbers call it, for the ones Pinnacle shows. pub fn name(&self) -> Option<&'static str> { Some(match self { Uuid::Short(GENERIC_ACCESS) => "Generic Access", Uuid::Short(GENERIC_ATTRIBUTE) => "Generic Attribute", Uuid::Short(DEVICE_INFORMATION) => "Device Information", Uuid::Short(BATTERY) => "Battery", Uuid::Short(HEART_RATE) => "Heart Rate", Uuid::Short(HUMAN_INTERFACE_DEVICE) => "Human Interface Device", Uuid::Short(DEVICE_NAME) => "Device Name", Uuid::Short(APPEARANCE) => "Appearance", Uuid::Short(MANUFACTURER_NAME) => "Manufacturer", 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, }) } } // Services. pub const GENERIC_ACCESS: u16 = 0x1800; pub const GENERIC_ATTRIBUTE: u16 = 0x1801; pub const DEVICE_INFORMATION: u16 = 0x180a; pub const BATTERY: u16 = 0x180f; pub const HEART_RATE: u16 = 0x180d; pub const HUMAN_INTERFACE_DEVICE: u16 = 0x1812; // Characteristics. pub const DEVICE_NAME: u16 = 0x2a00; pub const APPEARANCE: u16 = 0x2a01; 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; pub const CLIENT_CONFIGURATION: u16 = 0x2902; /// Pinnacle's own service: this device's signed contact card, so two /// people side by side become contacts with no network at all. pub const PINNACLE_SERVICE: [u8; 16] = [ 0x1e, 0x70, 0x6b, 0x8c, 0x32, 0x11, 0x4d, 0x9e, 0xb7, 0x4a, 0x5f, 0x0c, 0x00, 0x00, 0x6e, 0x50, ]; pub const PINNACLE_CARD: [u8; 16] = [ 0x1e, 0x70, 0x6b, 0x8c, 0x32, 0x11, 0x4d, 0x9e, 0xb7, 0x4a, 0x5f, 0x0c, 0x01, 0x00, 0x6e, 0x50, ]; /// Characteristic properties, as the declaration carries them. pub const PROP_READ: u8 = 0x02; pub const PROP_WRITE_NO_RESPONSE: u8 = 0x04; pub const PROP_WRITE: u8 = 0x08; pub const PROP_NOTIFY: u8 = 0x10; /// A characteristic, as a server offers it or a client found it. #[derive(Clone, PartialEq, Eq, Debug)] pub struct Characteristic { pub uuid: Uuid, pub properties: u8, /// Its value's handle (on a server: assigned when the table is built). pub handle: u16, /// Its client configuration descriptor's handle, if it notifies. pub configuration: Option, /// The value, on a server. pub value: Vec, } impl Characteristic { pub fn new(uuid: Uuid, properties: u8, value: &[u8]) -> Characteristic { Characteristic { uuid, properties, handle: 0, configuration: None, value: value.to_vec(), } } } #[derive(Clone, PartialEq, Eq, Debug)] pub struct Service { pub uuid: Uuid, /// Its declaration's handle, and the last handle in it. pub handle: u16, pub end: u16, pub characteristics: Vec, } impl Service { pub fn new(uuid: Uuid, characteristics: Vec) -> Service { Service { uuid, handle: 0, end: 0, characteristics, } } } /// One row of a server's attribute table. #[derive(Clone, Debug)] pub struct Attribute { pub handle: u16, pub kind: Uuid, pub value: Vec, pub readable: bool, pub writable: bool, /// The value handle this client configuration belongs to. pub configures: Option, } /// A server's attribute table: the declarations and values of its /// services, handles from 1 up, as a client reads them. #[derive(Clone, Debug, Default)] pub struct Table { pub attributes: Vec, pub services: Vec, } impl Table { pub fn new(mut services: Vec) -> Table { let mut attributes = Vec::new(); let mut handle = 1u16; for service in &mut services { service.handle = handle; attributes.push(Attribute { handle, kind: Uuid::Short(PRIMARY_SERVICE), value: service.uuid.bytes(), readable: true, writable: false, configures: None, }); handle += 1; for ch in &mut service.characteristics { ch.handle = handle + 1; let mut declaration = alloc::vec![ch.properties]; declaration.extend_from_slice(&ch.handle.to_le_bytes()); declaration.extend_from_slice(&ch.uuid.bytes()); attributes.push(Attribute { handle, kind: Uuid::Short(CHARACTERISTIC), value: declaration, readable: true, writable: false, configures: None, }); attributes.push(Attribute { handle: ch.handle, kind: ch.uuid, value: ch.value.clone(), readable: ch.properties & PROP_READ != 0, writable: ch.properties & (PROP_WRITE | PROP_WRITE_NO_RESPONSE) != 0, configures: None, }); handle += 2; if ch.properties & PROP_NOTIFY != 0 { ch.configuration = Some(handle); attributes.push(Attribute { handle, kind: Uuid::Short(CLIENT_CONFIGURATION), value: alloc::vec![0, 0], readable: true, writable: true, configures: Some(ch.handle), }); handle += 1; } } service.end = handle - 1; } Table { attributes, services } } pub fn get(&self, handle: u16) -> Option<&Attribute> { self.attributes.iter().find(|a| a.handle == handle) } pub fn get_mut(&mut self, handle: u16) -> Option<&mut Attribute> { self.attributes.iter_mut().find(|a| a.handle == handle) } /// The handle of the characteristic `uuid` in service `service`. pub fn value_handle(&self, service: Uuid, uuid: Uuid) -> Option { self.services .iter() .find(|s| s.uuid == service)? .characteristics .iter() .find(|c| c.uuid == uuid) .map(|c| c.handle) } }