| 1 | //! realme Buds T310 commands, tables and pure decoders. No I/O here, so every function can be |
| 2 | //! tested with bytes from the spec. Each constant cites docs/protocol/realme_t310.md. |
| 3 | |
| 4 | use crate::error::{Error, Result}; |
| 5 | use crate::reader::Reader; |
| 6 | use crate::status::{Battery, Charge, Gesture, NoiseControl, NoiseMode, Switch}; |
| 7 | |
| 8 | /// Spec: realme_t310.md#bitmap |
| 9 | pub(super) const HANDSHAKE: u16 = 0x0100; |
| 10 | /// Spec: realme_t310.md#identity |
| 11 | pub(super) const PRODUCT_ID: u16 = 0x0103; |
| 12 | /// Spec: realme_t310.md#firmware |
| 13 | pub(super) const FIRMWARE: u16 = 0x0105; |
| 14 | /// Spec: realme_t310.md#battery |
| 15 | pub(super) const BATTERY: u16 = 0x0106; |
| 16 | /// Spec: realme_t310.md#gestures |
| 17 | pub(super) const GESTURES: u16 = 0x0108; |
| 18 | /// Spec: realme_t310.md#anc-read |
| 19 | pub(super) const NOISE_CONTROL: u16 = 0x010C; |
| 20 | /// Spec: realme_t310.md#switches |
| 21 | pub(super) const SWITCHES: u16 = 0x010D; |
| 22 | /// Spec: realme_t310.md#anc-write |
| 23 | pub(super) const NOISE_CONTROL_WRITE: u16 = 0x0404; |
| 24 | /// Spec: realme_t310.md#eq-read |
| 25 | pub(super) const EQ_PRESET: u16 = 0x010F; |
| 26 | /// Spec: realme_t310.md#codec |
| 27 | pub(super) const CODEC: u16 = 0x0114; |
| 28 | /// Not sent: the T310 does not answer it (HW1). Kept for W6. Spec: realme_t310.md#bass-read |
| 29 | #[allow(dead_code)] |
| 30 | pub(super) const BASS_LEVEL: u16 = 0x0124; |
| 31 | /// Spec: realme_t310.md#events-offered |
| 32 | pub(super) const EVENTS_OFFERED: u16 = 0x0200; |
| 33 | /// Spec: realme_t310.md#subscribe |
| 34 | pub(super) const SUBSCRIBE: u16 = 0x0205; |
| 35 | /// Spec: realme_t310.md#fit-test |
| 36 | pub(super) const FIT_TEST: u16 = 0x0405; |
| 37 | |
| 38 | /// Spec: realme_t310.md#identity |
| 39 | pub(super) const T310_PRODUCT_ID: u32 = 0x06_3C_12; |
| 40 | |
| 41 | /// Payload asking for the current noise-control mode. Spec: realme_t310.md#anc-read |
| 42 | pub(super) const NOISE_MODE_QUERY: [u8; 2] = [0x01, 0x01]; |
| 43 | /// Payload asking for the hold's noise-control cycle (one setting for both buds). |
| 44 | /// Spec: realme_t310.md#hold-cycle |
| 45 | pub(super) const HOLD_CYCLE_QUERY: [u8; 2] = [0x02, 0x01]; |
| 46 | /// Count, then the T310's switch ids: `05 06 11 1A 1B 1D`. Spec: realme_t310.md#switches |
| 47 | pub(super) const SWITCH_QUERY: [u8; 7] = [6, 0x05, 0x06, 0x11, 0x1A, 0x1B, 0x1D]; |
| 48 | |
| 49 | /// Event codes to subscribe to; the optional ones only when their command is allowed. |
| 50 | /// Spec: realme_t310.md#subscribe |
| 51 | const EVENTS: [(u8, Option<u16>); 4] = [(0x01, None), (0x02, None), (0x03, None), (0x04, Some(FIT_TEST))]; |
| 52 | |
| 53 | /// Spec: realme_t310.md#switches |
| 54 | const SWITCH_NAMES: [(u8, &str, &str); 5] = [ |
| 55 | (0x06, "game", "Game mode"), |
| 56 | (0x11, "dual", "Dual connection"), |
| 57 | (0x1A, "wind", "Wind-noise reduction"), |
| 58 | (0x1B, "spatial", "3D sound"), |
| 59 | (0x1D, "bass-boost", "Bass boost"), |
| 60 | ]; |
| 61 | |
| 62 | /// Noise-control masks: mode and level. Spec: realme_t310.md#anc-read |
| 63 | const NOISE_MODES: [(u32, &str, Option<&str>); 5] = [ |
| 64 | (0x01, "off", None), |
| 65 | (0x02, "transparency", None), |
| 66 | (0x04, "anc", Some("light")), |
| 67 | (0x08, "anc", Some("deep")), |
| 68 | (0x10, "anc", Some("medium")), |
| 69 | ]; |
| 70 | |
| 71 | /// Bits of the hold cycle. Spec: realme_t310.md#hold-cycle |
| 72 | const CYCLE_MODES: [(u32, &str); 3] = [(0x01, "off"), (0x02, "transparency"), (0x08, "anc")]; |
| 73 | |
| 74 | /// Built-in EQ presets by id. Spec: realme_t310.md#eq-read |
| 75 | const EQ_PRESETS: [(u8, &str); 4] = [ |
| 76 | (0x00, "pure-raw"), |
| 77 | (0x01, "deep-bass"), |
| 78 | (0x02, "pure-vocals"), |
| 79 | (0x03, "pure-bass"), |
| 80 | ]; |
| 81 | |
| 82 | /// Spec: realme_t310.md#codec |
| 83 | const CODECS: [(u8, &str); 8] = [ |
| 84 | (0x01, "SBC"), |
| 85 | (0x02, "AAC"), |
| 86 | (0x03, "LDAC"), |
| 87 | (0x04, "aptX"), |
| 88 | (0x05, "aptX HD"), |
| 89 | (0x06, "aptX Adaptive"), |
| 90 | (0x07, "LHDC"), |
| 91 | (0x08, "LHDC V5"), |
| 92 | ]; |
| 93 | |
| 94 | /// Gesture-table names. Spec: realme_t310.md#gestures |
| 95 | const SIDES: [(u8, &str); 3] = [(0x01, "left"), (0x02, "right"), (0x04, "both")]; |
| 96 | /// Spec: realme_t310.md#gestures |
| 97 | const MAIN_GROUP: u8 = 0x01; |
| 98 | /// Spec: realme_t310.md#gestures |
| 99 | const ACTS: [(u8, &str); 6] = [ |
| 100 | (0x01, "single-tap"), |
| 101 | (0x02, "double-tap"), |
| 102 | (0x03, "triple-tap"), |
| 103 | (0x04, "hold"), |
| 104 | (0x05, "slide"), |
| 105 | (0x06, "long-hold"), |
| 106 | ]; |
| 107 | /// Spec: realme_t310.md#gestures |
| 108 | const FUNCTIONS: [(u8, &str); 15] = [ |
| 109 | (0x00, "none"), |
| 110 | (0x01, "play-pause"), |
| 111 | (0x03, "voice-assistant"), |
| 112 | (0x05, "previous"), |
| 113 | (0x06, "next"), |
| 114 | (0x07, "volume"), |
| 115 | (0x08, "noise-cycle"), |
| 116 | (0x09, "favourite-music"), |
| 117 | (0x0A, "switch-track"), |
| 118 | (0x0B, "volume-up"), |
| 119 | (0x0C, "volume-down"), |
| 120 | (0x0D, "switch-devices"), |
| 121 | (0x11, "game-mode"), |
| 122 | (0x1C, "decline-call"), |
| 123 | (0x1D, "answer-call"), |
| 124 | ]; |
| 125 | |
| 126 | fn name_of<K: PartialEq + Copy>(table: &[(K, &'static str)], key: K) -> Option<&'static str> { |
| 127 | table.iter().find(|(k, _)| *k == key).map(|(_, name)| *name) |
| 128 | } |
| 129 | |
| 130 | /// When a command may be sent. Spec: realme_t310.md#bitmap |
| 131 | enum Gate { |
| 132 | Always, |
| 133 | Bit(u8), |
| 134 | } |
| 135 | |
| 136 | /// The gate of every command budctl uses. An unlisted command is never sent. |
| 137 | /// Spec: realme_t310.md#bitmap |
| 138 | fn gate(cmd: u16) -> Option<Gate> { |
| 139 | Some(match cmd { |
| 140 | 0x0100..=0x0104 | 0x0106 | 0x010B | 0x010D | 0x0200 => Gate::Always, |
| 141 | 0x0105 => Gate::Bit(0), |
| 142 | 0x0108 | 0x0401 => Gate::Bit(3), |
| 143 | 0x0109 => Gate::Bit(4), |
| 144 | 0x0400 => Gate::Bit(5), |
| 145 | 0x0403 => Gate::Bit(7), |
| 146 | 0x010C | 0x0404 => Gate::Bit(8), |
| 147 | 0x0405 => Gate::Bit(9), |
| 148 | 0x010F | 0x0406 => Gate::Bit(10), |
| 149 | 0x0114 => Gate::Bit(17), |
| 150 | 0x0205 => Gate::Bit(22), |
| 151 | 0x0122 | 0x0418 => Gate::Bit(34), |
| 152 | 0x0124 | 0x041B => Gate::Bit(38), |
| 153 | _ => return None, |
| 154 | }) |
| 155 | } |
| 156 | |
| 157 | /// The commands these buds accept, from the handshake reply. Spec: realme_t310.md#bitmap |
| 158 | #[derive(Debug)] |
| 159 | pub(super) struct Commands { |
| 160 | bitmap: Vec<u8>, |
| 161 | } |
| 162 | |
| 163 | impl Commands { |
| 164 | pub(super) fn parse(reply: &[u8]) -> Result<Self> { |
| 165 | let mut r = ok_reply(reply, "the handshake")?; |
| 166 | Ok(Self { |
| 167 | bitmap: r.rest().to_vec(), |
| 168 | }) |
| 169 | } |
| 170 | |
| 171 | pub(super) fn allows(&self, cmd: u16) -> bool { |
| 172 | match gate(cmd) { |
| 173 | Some(Gate::Always) => true, |
| 174 | Some(Gate::Bit(n)) => self |
| 175 | .bitmap |
| 176 | .get(usize::from(n / 8)) |
| 177 | .is_some_and(|byte| byte & (1 << (n % 8)) != 0), |
| 178 | None => false, |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /// A reply whose status byte is `00`, positioned after it. Spec: realme_t310.md#seq |
| 184 | fn ok_reply<'a>(reply: &'a [u8], what: &'static str) -> Result<Reader<'a>> { |
| 185 | let mut r = Reader::new(reply, what); |
| 186 | match r.u8()? { |
| 187 | 0x00 => Ok(r), |
| 188 | status => Err(Error::Rejected { |
| 189 | what: what.to_owned(), |
| 190 | status, |
| 191 | }), |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /// Spec: realme_t310.md#identity |
| 196 | pub(super) fn decode_product_id(reply: &[u8]) -> Result<u32> { |
| 197 | let mut r = ok_reply(reply, "the product id query")?; |
| 198 | let [low, middle, high] = [r.u8()?, r.u8()?, r.u8()?]; |
| 199 | Ok(u32::from_le_bytes([low, middle, high, 0])) |
| 200 | } |
| 201 | |
| 202 | /// Spec: realme_t310.md#events-offered |
| 203 | pub(super) fn decode_events_offered(reply: &[u8]) -> Result<Vec<u8>> { |
| 204 | let mut r = ok_reply(reply, "the event list")?; |
| 205 | let count = r.u8()?; |
| 206 | Ok(r.take(usize::from(count))?.to_vec()) |
| 207 | } |
| 208 | |
| 209 | /// The subscribe payload: count, then the wanted codes the buds offer. |
| 210 | /// Spec: realme_t310.md#subscribe |
| 211 | pub(super) fn subscribe_payload(offered: &[u8], commands: &Commands) -> Vec<u8> { |
| 212 | let codes: Vec<u8> = EVENTS |
| 213 | .iter() |
| 214 | .filter(|(code, needs)| offered.contains(code) && needs.is_none_or(|cmd| commands.allows(cmd))) |
| 215 | .map(|(code, _)| *code) |
| 216 | .collect(); |
| 217 | let mut payload = vec![u8::try_from(codes.len()).unwrap_or_default()]; |
| 218 | payload.extend(codes); |
| 219 | payload |
| 220 | } |
| 221 | |
| 222 | /// Spec: realme_t310.md#battery |
| 223 | pub(super) fn decode_battery(reply: &[u8]) -> Result<Battery> { |
| 224 | let mut r = ok_reply(reply, "the battery reply")?; |
| 225 | let mut battery = Battery::default(); |
| 226 | for _ in 0..r.u8()? { |
| 227 | let part = r.u8()?; |
| 228 | let raw = r.u8()?; |
| 229 | let level = raw & 0x7F; |
| 230 | if level == 0 { |
| 231 | continue; // not known |
| 232 | } |
| 233 | let charge = Some(Charge { |
| 234 | level, |
| 235 | charging: raw & 0x80 != 0, |
| 236 | }); |
| 237 | match part { |
| 238 | 0x01 => battery.left = charge, |
| 239 | 0x02 => battery.right = charge, |
| 240 | 0x03 => battery.case = charge, |
| 241 | _ => {} |
| 242 | } |
| 243 | } |
| 244 | Ok(battery) |
| 245 | } |
| 246 | |
| 247 | /// The versions of kind 2: plain numbers are joined with dots; full dotted versions (the T310's) |
| 248 | /// are shown once each, joined with ` / `. The raw text when there are none. |
| 249 | /// Spec: realme_t310.md#firmware |
| 250 | pub(super) fn decode_firmware(reply: &[u8]) -> Result<String> { |
| 251 | let mut r = ok_reply(reply, "the firmware reply")?; |
| 252 | let _count = r.u8()?; |
| 253 | let text = String::from_utf8_lossy(r.rest()).into_owned(); |
| 254 | let fields: Vec<&str> = text.split(',').map(str::trim).collect(); |
| 255 | let mut versions: Vec<&str> = fields |
| 256 | .chunks(3) |
| 257 | .filter_map(|triple| match triple { |
| 258 | [_device, "2", version] => Some(*version), |
| 259 | _ => None, |
| 260 | }) |
| 261 | .collect(); |
| 262 | if versions.is_empty() { |
| 263 | return Ok(text); |
| 264 | } |
| 265 | if !versions.iter().any(|v| v.contains('.')) { |
| 266 | return Ok(versions.join(".")); |
| 267 | } |
| 268 | let mut seen = Vec::new(); |
| 269 | versions.retain(|v| { |
| 270 | let new = !seen.contains(v); |
| 271 | seen.push(*v); |
| 272 | new |
| 273 | }); |
| 274 | Ok(versions.join(" / ")) |
| 275 | } |
| 276 | |
| 277 | /// Spec: realme_t310.md#codec |
| 278 | pub(super) fn decode_codec(reply: &[u8]) -> Result<String> { |
| 279 | let codec = ok_reply(reply, "the codec reply")?.u8()?; |
| 280 | Ok(name_of(&CODECS, codec).map_or_else(|| format!("unknown (0x{codec:02X})"), str::to_owned)) |
| 281 | } |
| 282 | |
| 283 | /// Spec: realme_t310.md#anc-read |
| 284 | pub(super) fn decode_noise_mode(reply: &[u8]) -> Result<NoiseControl> { |
| 285 | let mask = noise_mask(reply, NOISE_MODE_QUERY, "the noise-control reply")?; |
| 286 | Ok(match NOISE_MODES.iter().find(|(m, _, _)| *m == mask) { |
| 287 | Some((_, mode, level)) => NoiseControl { |
| 288 | mode: (*mode).to_owned(), |
| 289 | level: level.map(str::to_owned), |
| 290 | }, |
| 291 | None => NoiseControl { |
| 292 | mode: format!("unknown (0x{mask:02X})"), |
| 293 | level: None, |
| 294 | }, |
| 295 | }) |
| 296 | } |
| 297 | |
| 298 | /// The mask that selects `mode`, as written and as read back. Spec: realme_t310.md#anc-write |
| 299 | pub(super) fn noise_write_mask(mode: NoiseMode) -> u32 { |
| 300 | match mode { |
| 301 | NoiseMode::Off => 0x01, |
| 302 | NoiseMode::Transparency => 0x02, |
| 303 | // Plain "noise cancelling on" is the deep level. |
| 304 | NoiseMode::Anc => 0x08, |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /// `01 01 <mask>`: the noise-mode kind, then the mask in the fewest bytes (all fit in one). |
| 309 | /// Spec: realme_t310.md#anc-write |
| 310 | pub(super) fn noise_write_payload(mode: NoiseMode) -> Vec<u8> { |
| 311 | let mut payload = NOISE_MODE_QUERY.to_vec(); |
| 312 | let mask = noise_write_mask(mode).to_le_bytes(); |
| 313 | let used = mask.iter().rposition(|&b| b != 0).map_or(1, |last| last + 1); |
| 314 | payload.extend(mask.iter().take(used)); |
| 315 | payload |
| 316 | } |
| 317 | |
| 318 | /// The mask in a noise-control read reply. Spec: realme_t310.md#anc-read |
| 319 | pub(super) fn decode_noise_mask(reply: &[u8]) -> Result<u32> { |
| 320 | noise_mask(reply, NOISE_MODE_QUERY, "the noise-control reply") |
| 321 | } |
| 322 | |
| 323 | /// A write reply: `00` means accepted. Spec: realme_t310.md#writes |
| 324 | pub(super) fn check_write_reply(reply: &[u8], what: &'static str) -> Result<()> { |
| 325 | ok_reply(reply, what).map(|_| ()) |
| 326 | } |
| 327 | |
| 328 | /// Spec: realme_t310.md#hold-cycle |
| 329 | pub(super) fn decode_hold_cycle(reply: &[u8]) -> Result<Vec<String>> { |
| 330 | let mask = noise_mask(reply, HOLD_CYCLE_QUERY, "the hold-cycle reply")?; |
| 331 | let mut modes: Vec<String> = CYCLE_MODES |
| 332 | .iter() |
| 333 | .filter(|(bit, _)| mask & bit != 0) |
| 334 | .map(|(_, name)| (*name).to_owned()) |
| 335 | .collect(); |
| 336 | let known = CYCLE_MODES.iter().fold(0, |all, (bit, _)| all | bit); |
| 337 | if mask & !known != 0 { |
| 338 | modes.push(format!("unknown (0x{:02X})", mask & !known)); |
| 339 | } |
| 340 | Ok(modes) |
| 341 | } |
| 342 | |
| 343 | /// `00 <echo of the query> <mask, 1-4 bytes LE>`. Spec: realme_t310.md#anc-read |
| 344 | fn noise_mask(reply: &[u8], query: [u8; 2], what: &'static str) -> Result<u32> { |
| 345 | let mut r = ok_reply(reply, what)?; |
| 346 | if [r.u8()?, r.u8()?] != query { |
| 347 | return Err(Error::Protocol(format!("{what} answers a different question"))); |
| 348 | } |
| 349 | let bytes = r.rest(); |
| 350 | if bytes.is_empty() || bytes.len() > 4 { |
| 351 | return Err(Error::Protocol(format!("{what} has a {} byte value", bytes.len()))); |
| 352 | } |
| 353 | Ok(bytes.iter().rev().fold(0, |mask, &b| (mask << 8) | u32::from(b))) |
| 354 | } |
| 355 | |
| 356 | /// Spec: realme_t310.md#switches |
| 357 | pub(super) fn decode_switches(reply: &[u8]) -> Result<Vec<Switch>> { |
| 358 | let mut r = ok_reply(reply, "the switch reply")?; |
| 359 | let mut switches = Vec::new(); |
| 360 | for _ in 0..r.u8()? { |
| 361 | let id = r.u8()?; |
| 362 | let value = r.u8()?; |
| 363 | if let Some((_, key, label)) = SWITCH_NAMES.iter().find(|(known, _, _)| *known == id) { |
| 364 | switches.push(Switch { |
| 365 | key, |
| 366 | label, |
| 367 | on: value == 0x01, |
| 368 | }); |
| 369 | } |
| 370 | } |
| 371 | Ok(switches) |
| 372 | } |
| 373 | |
| 374 | /// Spec: realme_t310.md#eq-read |
| 375 | pub(super) fn decode_eq(reply: &[u8]) -> Result<String> { |
| 376 | let id = ok_reply(reply, "the EQ reply")?.u8()?; |
| 377 | Ok(name_of(&EQ_PRESETS, id).map_or_else(|| format!("custom-{id}"), str::to_owned)) |
| 378 | } |
| 379 | |
| 380 | /// `00 <min> <max> <level>`, all signed. Not used until W6. Spec: realme_t310.md#bass-read |
| 381 | #[allow(dead_code)] |
| 382 | pub(super) fn decode_bass_level(reply: &[u8]) -> Result<i8> { |
| 383 | let mut r = ok_reply(reply, "the bass-level reply")?; |
| 384 | let (min, max, level) = (r.i8()?, r.i8()?, r.i8()?); |
| 385 | if !(min..=max).contains(&level) { |
| 386 | return Err(Error::Protocol(format!("bass level {level} outside {min}..={max}"))); |
| 387 | } |
| 388 | Ok(level) |
| 389 | } |
| 390 | |
| 391 | /// Spec: realme_t310.md#gestures |
| 392 | pub(super) fn decode_gestures(reply: &[u8]) -> Result<Vec<Gesture>> { |
| 393 | let mut r = ok_reply(reply, "the gesture table")?; |
| 394 | let mut gestures = Vec::new(); |
| 395 | for _ in 0..r.u8()? { |
| 396 | let [side, group, act, function] = [r.u8()?, r.u8()?, r.u8()?, r.u8()?]; |
| 397 | gestures.push(Gesture { |
| 398 | side: name_of(&SIDES, side).map_or_else(|| format!("device-0x{side:02X}"), str::to_owned), |
| 399 | gesture: name_of(&ACTS, act).map_or_else(|| format!("act-0x{act:02X}"), str::to_owned), |
| 400 | action: name_of(&FUNCTIONS, function).map_or_else(|| format!("fn-0x{function:02X}"), str::to_owned), |
| 401 | group: (group != MAIN_GROUP).then_some(group), |
| 402 | }); |
| 403 | } |
| 404 | Ok(gestures) |
| 405 | } |
| 406 | |
| 407 | /// The handshake reply of the owner's T310 (HW1). Spec: realme_t310.md#bitmap-t310 |
| 408 | #[cfg(test)] |
| 409 | pub(super) const T310_BITMAP_REPLY: &str = "00 BF 1F 5A 20 45 04 40 00"; |
| 410 | |
| 411 | #[cfg(test)] |
| 412 | mod tests { |
| 413 | use super::*; |
| 414 | use crate::transport::mock::hex; |
| 415 | |
| 416 | #[test] |
| 417 | fn t310_bitmap_allows_its_commands_and_nothing_else() { |
| 418 | let commands = Commands::parse(&hex(T310_BITMAP_REPLY)).unwrap(); |
| 419 | // Every gated command in the T310's logged command set (realme_t310.md#bitmap-t310). |
| 420 | for cmd in [ |
| 421 | 0x0100, 0x0103, 0x0105, 0x0106, 0x0108, 0x0109, 0x010C, 0x010D, 0x010F, 0x0114, 0x0122, 0x0124, 0x0205, |
| 422 | 0x0400, 0x0401, 0x0403, 0x0404, 0x0405, 0x0406, 0x0418, 0x041B, 0x0200, |
| 423 | ] { |
| 424 | assert!(commands.allows(cmd), "0x{cmd:04X} should be allowed"); |
| 425 | } |
| 426 | // Commands the T310 lacks, and commands budctl has no gate for. |
| 427 | for cmd in [0x012F, 0x041E, 0x0413, 0x0130, 0x0427, 0x0423, 0x012A, 0x0422, 0x9999] { |
| 428 | assert!(!commands.allows(cmd), "0x{cmd:04X} should be refused"); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | #[test] |
| 433 | fn a_short_bitmap_allows_only_the_always_commands() { |
| 434 | let commands = Commands::parse(&hex("00")).unwrap(); |
| 435 | assert!(commands.allows(BATTERY) && commands.allows(SWITCHES) && commands.allows(HANDSHAKE)); |
| 436 | assert!(!commands.allows(FIRMWARE) && !commands.allows(NOISE_CONTROL)); |
| 437 | assert!(matches!( |
| 438 | Commands::parse(&hex("01 FF")), |
| 439 | Err(Error::Rejected { status: 1, .. }) |
| 440 | )); |
| 441 | } |
| 442 | |
| 443 | #[test] |
| 444 | fn product_id_is_three_bytes_little_endian() { |
| 445 | assert_eq!(decode_product_id(&hex("00 12 3C 06")).unwrap(), T310_PRODUCT_ID); |
| 446 | assert_eq!(decode_product_id(&hex("00 14 54 06")).unwrap(), 0x06_54_14); |
| 447 | assert!(decode_product_id(&hex("00 12 3C")).is_err()); |
| 448 | } |
| 449 | |
| 450 | #[test] |
| 451 | fn subscribes_to_offered_events_only() { |
| 452 | let commands = Commands::parse(&hex(T310_BITMAP_REPLY)).unwrap(); |
| 453 | let buds4_offer = hex("00 09 01 02 03 04 08 0B F1 F2 F3"); |
| 454 | let offered = decode_events_offered(&buds4_offer).unwrap(); |
| 455 | assert_eq!(subscribe_payload(&offered, &commands), hex("04 01 02 03 04")); |
| 456 | assert_eq!(subscribe_payload(&[0x02, 0x01], &commands), hex("02 01 02")); |
| 457 | let no_fit_test = Commands::parse(&hex("00 BD 0D")).unwrap(); |
| 458 | assert_eq!(subscribe_payload(&offered, &no_fit_test), hex("03 01 02 03")); |
| 459 | } |
| 460 | |
| 461 | #[test] |
| 462 | fn battery_levels_charging_and_unknown_parts() { |
| 463 | let battery = decode_battery(&hex("00 03 01 64 02 E4 03 00")).unwrap(); |
| 464 | assert_eq!( |
| 465 | battery.left, |
| 466 | Some(Charge { |
| 467 | level: 100, |
| 468 | charging: false |
| 469 | }) |
| 470 | ); |
| 471 | assert_eq!( |
| 472 | battery.right, |
| 473 | Some(Charge { |
| 474 | level: 100, |
| 475 | charging: true |
| 476 | }) |
| 477 | ); |
| 478 | assert_eq!(battery.case, None); |
| 479 | assert_eq!(decode_battery(&hex("00 00")).unwrap(), Battery::default()); |
| 480 | assert!(decode_battery(&hex("00 03 01 64")).is_err()); |
| 481 | } |
| 482 | |
| 483 | #[test] |
| 484 | fn firmware_shows_kind_two_versions() { |
| 485 | let mut reply = hex("00 04"); |
| 486 | reply.extend(b"1,2,138,2,2,138,3,1,01,3,2,105"); |
| 487 | assert_eq!(decode_firmware(&reply).unwrap(), "138.138.105"); |
| 488 | // The owner's T310 (HW1): both buds report the same full version. |
| 489 | let mut t310 = hex("00 02"); |
| 490 | t310.extend(b"1,2,1.1.0.43,2,2,1.1.0.43"); |
| 491 | assert_eq!(decode_firmware(&t310).unwrap(), "1.1.0.43"); |
| 492 | let mut differ = hex("00 02"); |
| 493 | differ.extend(b"1,2,1.1.0.43,2,2,1.1.0.44"); |
| 494 | assert_eq!(decode_firmware(&differ).unwrap(), "1.1.0.43 / 1.1.0.44"); |
| 495 | let mut odd = hex("00 01"); |
| 496 | odd.extend(b"V1.0.2"); |
| 497 | assert_eq!(decode_firmware(&odd).unwrap(), "V1.0.2"); |
| 498 | } |
| 499 | |
| 500 | #[test] |
| 501 | fn noise_control_modes() { |
| 502 | let mode = |reply: &str| decode_noise_mode(&hex(reply)).unwrap(); |
| 503 | assert_eq!(mode("00 01 01 01").mode, "off"); |
| 504 | assert_eq!(mode("00 01 01 02 00").mode, "transparency"); |
| 505 | let deep = mode("00 01 01 08"); |
| 506 | assert_eq!((deep.mode.as_str(), deep.level.as_deref()), ("anc", Some("deep"))); |
| 507 | assert_eq!(mode("00 01 01 10").level.as_deref(), Some("medium")); |
| 508 | assert_eq!(mode("00 01 01 00 08").mode, "unknown (0x800)"); |
| 509 | assert!(decode_noise_mode(&hex("00 02 01 08")).is_err()); |
| 510 | assert!(decode_noise_mode(&hex("00 01 01")).is_err()); |
| 511 | assert!(matches!( |
| 512 | decode_noise_mode(&hex("0E 01 01 08")), |
| 513 | Err(Error::Rejected { status: 0x0E, .. }) |
| 514 | )); |
| 515 | } |
| 516 | |
| 517 | #[test] |
| 518 | fn noise_control_write_payloads() { |
| 519 | assert_eq!(noise_write_payload(NoiseMode::Off), hex("01 01 01")); |
| 520 | assert_eq!(noise_write_payload(NoiseMode::Transparency), hex("01 01 02")); |
| 521 | assert_eq!(noise_write_payload(NoiseMode::Anc), hex("01 01 08")); |
| 522 | // The family's documented frame for "off" (realme_t310.md#frame-whole). |
| 523 | let frame = |
| 524 | crate::devices::realme_t310::frame::encode(NOISE_CONTROL_WRITE, 0x06, &noise_write_payload(NoiseMode::Off)) |
| 525 | .unwrap(); |
| 526 | assert_eq!(frame, hex("AA 0A 00 00 04 04 06 03 00 01 01 01")); |
| 527 | } |
| 528 | |
| 529 | #[test] |
| 530 | fn write_replies() { |
| 531 | assert!(check_write_reply(&hex("00"), "the write").is_ok()); |
| 532 | assert!(matches!( |
| 533 | check_write_reply(&hex("0E"), "the write"), |
| 534 | Err(Error::Rejected { status: 0x0E, .. }) |
| 535 | )); |
| 536 | assert!(check_write_reply(&[], "the write").is_err()); |
| 537 | } |
| 538 | |
| 539 | #[test] |
| 540 | fn hold_cycle_lists_modes() { |
| 541 | assert_eq!(decode_hold_cycle(&hex("00 02 01 0A")).unwrap(), ["transparency", "anc"]); |
| 542 | assert_eq!( |
| 543 | decode_hold_cycle(&hex("00 02 01 0B 00")).unwrap(), |
| 544 | ["off", "transparency", "anc"] |
| 545 | ); |
| 546 | assert_eq!(decode_hold_cycle(&hex("00 02 01 14")).unwrap(), ["unknown (0x14)"]); |
| 547 | } |
| 548 | |
| 549 | #[test] |
| 550 | fn switches_from_report_22_values() { |
| 551 | let switches = decode_switches(&hex("00 06 05 00 06 00 11 01 1A 00 1B 01 1D 01")).unwrap(); |
| 552 | let on: Vec<(&str, bool)> = switches.iter().map(|s| (s.key, s.on)).collect(); |
| 553 | assert_eq!( |
| 554 | on, |
| 555 | [ |
| 556 | ("game", false), |
| 557 | ("dual", true), |
| 558 | ("wind", false), |
| 559 | ("spatial", true), |
| 560 | ("bass-boost", true) |
| 561 | ] |
| 562 | ); |
| 563 | assert!(decode_switches(&hex("00 02 06 01")).is_err()); |
| 564 | } |
| 565 | |
| 566 | #[test] |
| 567 | fn eq_codec_and_bass_level() { |
| 568 | assert_eq!(decode_eq(&hex("00 00")).unwrap(), "pure-raw"); |
| 569 | assert_eq!(decode_eq(&hex("00 03")).unwrap(), "pure-bass"); |
| 570 | assert_eq!(decode_eq(&hex("00 05")).unwrap(), "custom-5"); |
| 571 | assert_eq!(decode_codec(&hex("00 02")).unwrap(), "AAC"); |
| 572 | assert_eq!(decode_codec(&hex("00 09")).unwrap(), "unknown (0x09)"); |
| 573 | assert_eq!(decode_bass_level(&hex("00 FB 05 FE")).unwrap(), -2); |
| 574 | assert!(decode_bass_level(&hex("00 FB 05 07")).is_err()); |
| 575 | } |
| 576 | |
| 577 | #[test] |
| 578 | fn gesture_table_names_known_values() { |
| 579 | let gestures = decode_gestures(&hex("00 03 01 01 02 01 02 01 04 08 04 06 02 1D")).unwrap(); |
| 580 | let rows: Vec<(&str, &str, &str, Option<u8>)> = gestures |
| 581 | .iter() |
| 582 | .map(|g| (g.side.as_str(), g.gesture.as_str(), g.action.as_str(), g.group)) |
| 583 | .collect(); |
| 584 | assert_eq!( |
| 585 | rows, |
| 586 | [ |
| 587 | ("left", "double-tap", "play-pause", None), |
| 588 | ("right", "hold", "noise-cycle", None), |
| 589 | ("both", "double-tap", "answer-call", Some(6)), |
| 590 | ] |
| 591 | ); |
| 592 | let odd = decode_gestures(&hex("00 01 09 01 07 42")).unwrap(); |
| 593 | assert_eq!(odd.first().map(|g| g.action.as_str()), Some("fn-0x42")); |
| 594 | assert!(decode_gestures(&hex("00 02 01 01 02 01")).is_err()); |
| 595 | } |
| 596 | } |