//! Siri Remote (2nd gen, Apple 0x004C:0x0315) wire format, reverse engineered //! from btmon captures (see docs/siri-remote.md). //! //! All data arrives as HID Report (0x2A4D) GATT notifications, told apart by length: //! - 2 bytes: button bitmask (u16 little-endian) //! - 11 bytes: touchpad frame (~66 Hz while touched) //! - 99 bytes: Siri microphone audio (not decoded) /// Button bits (u16 little-endian of the 2-byte report). pub mod button { pub const HOME: u16 = 0x0001; // TV / Home pub const VOL_UP: u16 = 0x0002; pub const VOL_DOWN: u16 = 0x0004; pub const SELECT: u16 = 0x0008; // center click pub const POWER: u16 = 0x0010; pub const SIRI: u16 = 0x0020; pub const BACK: u16 = 0x0040; pub const MUTE: u16 = 0x0080; pub const PLAY_PAUSE: u16 = 0x0100; pub const UP: u16 = 0x0200; pub const RIGHT: u16 = 0x0400; pub const DOWN: u16 = 0x0800; pub const LEFT: u16 = 0x1000; pub const ALL: [(u16, &str); 13] = [ (HOME, "home"), (VOL_UP, "vol_up"), (VOL_DOWN, "vol_down"), (SELECT, "select"), (POWER, "power"), (SIRI, "siri"), (BACK, "back"), (MUTE, "mute"), (PLAY_PAUSE, "play_pause"), (UP, "up"), (RIGHT, "right"), (DOWN, "down"), (LEFT, "left"), ]; } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Report { Buttons(u16), Touch(TouchFrame), Audio, Unknown(usize), } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct TouchFrame { /// Device timestamp (wraps), roughly 0.47 ms per unit. pub stamp: u16, /// Signed coordinates: +x right, +y up. Roughly -1100..+400 / -1100..+250. pub x: i16, pub y: i16, /// Contact size / pressure-like values. pub size: [u8; 3], /// Last frame of a touch (finger lifted). pub released: bool, } fn s12(v: u16) -> i16 { if v >= 2048 { v as i16 - 4096 } else { v as i16 } } pub fn parse(data: &[u8]) -> Report { match data.len() { 2 => Report::Buttons(u16::from_le_bytes([data[0], data[1]])), 11 => { let x = s12(data[4] as u16 | ((data[5] as u16 & 0x0f) << 8)); let y = s12((data[5] as u16 >> 4) | ((data[6] as u16) << 4)); let size = [data[7], data[8], data[9]]; Report::Touch(TouchFrame { stamp: u16::from_le_bytes([data[1], data[2]]), x, y, size, released: size == [0, 0, 0], }) } 99 => Report::Audio, n => Report::Unknown(n), } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Swipe { Up, Down, Left, Right, } /// Turns touch movement into discrete steps, like tvOS: every `step` units of /// travel along the dominant axis emits one swipe. Touches that end in a /// button click (ring/center press) are ignored. pub struct Gestures { pub step: i32, origin: Option<(i32, i32)>, suppressed: bool, } impl Default for Gestures { fn default() -> Self { Self { step: 300, origin: None, suppressed: false } } } impl Gestures { pub fn new(step: i32) -> Self { Self { step, ..Default::default() } } /// A button press during a touch means the user is clicking, not swiping. pub fn on_button(&mut self) { if self.origin.is_some() { self.suppressed = true; } } pub fn on_touch(&mut self, f: &TouchFrame) -> Option { if f.released { self.origin = None; self.suppressed = false; return None; } let p = (f.x as i32, f.y as i32); let Some(o) = self.origin else { self.origin = Some(p); return None; }; if self.suppressed { return None; } let (dx, dy) = (p.0 - o.0, p.1 - o.1); let swipe = if dx.abs() >= self.step && dx.abs() * 2 > dy.abs() * 3 { Some(if dx > 0 { Swipe::Right } else { Swipe::Left }) } else if dy.abs() >= self.step && dy.abs() * 2 > dx.abs() * 3 { Some(if dy > 0 { Swipe::Up } else { Swipe::Down }) } else { None }; if swipe.is_some() { self.origin = Some(p); } swipe } } #[cfg(test)] mod tests { use super::*; fn hex(s: &str) -> Vec { s.split_whitespace().map(|b| u8::from_str_radix(b, 16).unwrap()).collect() } #[test] fn buttons_from_capture() { assert_eq!(parse(&hex("00 02")), Report::Buttons(button::UP)); assert_eq!(parse(&hex("08 00")), Report::Buttons(button::SELECT)); assert_eq!(parse(&hex("00 01")), Report::Buttons(button::PLAY_PAUSE)); assert_eq!(parse(&hex("40 00")), Report::Buttons(button::BACK)); assert_eq!(parse(&hex("00 00")), Report::Buttons(0)); } #[test] fn touch_from_capture() { // Swipe left -> right crossing x = 0 (captured 448.406 / 448.452). let Report::Touch(a) = parse(&hex("32 0c e9 00 ea 9f e8 83 6f 14 84")) else { panic!() }; let Report::Touch(b) = parse(&hex("32 68 e9 00 0b 50 e9 84 6c 13 84")) else { panic!() }; assert_eq!((a.x, b.x), (-22, 11)); assert!(b.y > a.y); let Report::Touch(end) = parse(&hex("32 00 00 00 28 61 f4 00 00 00 67")) else { panic!() }; assert!(end.released); } #[test] fn swipe_steps() { let mut g = Gestures::default(); let frame = |x, y| TouchFrame { stamp: 0, x, y, size: [100, 100, 10], released: false }; assert_eq!(g.on_touch(&frame(-900, -500)), None); assert_eq!(g.on_touch(&frame(-700, -480)), None); assert_eq!(g.on_touch(&frame(-590, -470)), Some(Swipe::Right)); assert_eq!(g.on_touch(&frame(-300, -460)), None); assert_eq!(g.on_touch(&frame(-280, -460)), Some(Swipe::Right)); // a button press without a finger on the pad must not swallow the next swipe g.on_touch(&TouchFrame { released: true, ..frame(0, 0) }); g.on_button(); g.on_touch(&frame(0, 0)); assert_eq!(g.on_touch(&frame(400, 0)), Some(Swipe::Right)); // click during a touch suppresses g.on_touch(&TouchFrame { released: true, ..frame(0, 0) }); g.on_touch(&frame(0, 0)); g.on_button(); assert_eq!(g.on_touch(&frame(0, 600)), None); } }