frenz-proto 0.3.0

Wire-protocol decoders and command encoders for the FRENZ brainband (no I/O, no_std)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//! Decoders for notification payloads.
//!
//! Device times are the band's whole-second clock: seconds since power-on until the host sends
//! [`Command::TimeSync`](crate::Command::TimeSync), Unix seconds afterwards.

use crate::command::Response;
use crate::error::ParseError;
use crate::gatt::Characteristic;
use crate::{be_u16, be_u32};

/// Any decoded notification.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Packet {
    /// EEG frame (9 samples).
    Eeg(EegPacket),
    /// Accelerometer frame (25 samples).
    Accelerometer(MotionPacket),
    /// Gyroscope frame (25 samples).
    Gyroscope(MotionPacket),
    /// PPG frame (12–13 samples).
    Ppg(PpgPacket),
    /// Heart-rate reading.
    HeartRate(VitalPacket),
    /// SpO2 reading.
    Spo2(VitalPacket),
    /// Battery percentage.
    Battery(BatteryPacket),
    /// Electrode contact state.
    Contact(ContactPacket),
    /// Message on the status channel.
    Status(StatusMessage),
    /// Command response.
    Response(Response),
}

impl Packet {
    /// Decodes a notification received on `characteristic`.
    pub fn decode(characteristic: Characteristic, payload: &[u8]) -> Result<Self, ParseError> {
        Ok(match characteristic {
            Characteristic::Eeg => Self::Eeg(EegPacket::decode(payload)?),
            Characteristic::Accelerometer => Self::Accelerometer(MotionPacket::decode(characteristic, payload)?),
            Characteristic::Gyroscope => Self::Gyroscope(MotionPacket::decode(characteristic, payload)?),
            Characteristic::Ppg => Self::Ppg(PpgPacket::decode(payload)?),
            Characteristic::HeartRate => Self::HeartRate(VitalPacket::decode(characteristic, payload)?),
            Characteristic::Spo2 => Self::Spo2(VitalPacket::decode(characteristic, payload)?),
            Characteristic::Battery => Self::Battery(BatteryPacket::decode(payload)?),
            Characteristic::Contact => Self::Contact(ContactPacket::decode(payload)?),
            Characteristic::Status => Self::Status(StatusMessage::decode(payload)?),
            Characteristic::CommandResponse => Self::Response(Response::decode(payload)?),
            Characteristic::CommandRx => return Err(ParseError::UnknownCharacteristic),
        })
    }

    /// The device time carried by the packet, if it has one.
    pub fn device_time(&self) -> Option<u32> {
        match self {
            Self::Eeg(p) => Some(p.device_time),
            Self::Accelerometer(p) | Self::Gyroscope(p) => Some(p.device_time),
            Self::Ppg(p) => Some(p.device_time),
            Self::HeartRate(p) | Self::Spo2(p) => Some(p.device_time),
            Self::Battery(p) => Some(p.device_time),
            Self::Contact(p) => Some(p.device_time),
            Self::Status(_) | Self::Response(_) => None,
        }
    }
}

/// Sign-extends a 24-bit big-endian integer.
fn i24(b: [u8; 3]) -> i32 {
    i32::from_be_bytes([b[0], b[1], b[2], 0]) >> 8
}

fn read3(bytes: &[u8], at: usize) -> Option<[u8; 3]> {
    Some([*bytes.get(at)?, *bytes.get(at + 1)?, *bytes.get(at + 2)?])
}

/// EEG frame: 9 samples × 6 channel slots of signed 24-bit ADC counts.
///
/// Only slots 0, 1, 3 and 4 carry signal on the verified hardware ([`EegPacket::ACTIVE_CHANNELS`]);
/// slots 2 and 5 are always zero. Units are raw ADC counts; the microvolt scale is not yet known.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EegPacket {
    /// 8-bit frame counter; consecutive frames differ by 1 (mod 256).
    pub sequence: u8,
    /// Device time (whole seconds).
    pub device_time: u32,
    /// `samples[i][slot]`, raw signed 24-bit counts.
    pub samples: [[i32; 6]; 9],
    /// Per-sample flag byte (meaning unknown; 1 on ~4 % of samples).
    pub flags: [u8; 9],
    /// Final byte of the frame (meaning unknown; observed 0–16).
    pub trailer: u8,
}

impl EegPacket {
    /// Verified frame length.
    pub const LEN: usize = 178;
    /// Frame type byte of EEG frames (equals the channel-slot count).
    pub const FRAME_TYPE: u8 = 6;
    /// Samples per frame.
    pub const SAMPLES: usize = 9;
    /// Nominal sample rate.
    pub const RATE_HZ: f64 = 125.0;
    /// Channel slots that carry signal, and the electrode each corresponds to (press test).
    pub const ACTIVE_CHANNELS: [(usize, &'static str); 4] = [
        (0, "forehead_right"),
        (1, "comb_left"),
        (3, "forehead_left"),
        (4, "comb_right"),
    ];

    /// Decodes a frame.
    pub fn decode(p: &[u8]) -> Result<Self, ParseError> {
        let frame_type = *p.get(1).ok_or(ParseError::Length {
            characteristic: Characteristic::Eeg,
            actual: p.len(),
        })?;
        if frame_type != Self::FRAME_TYPE {
            if p.len() == Self::LEN && p.first() == Some(&Self::FRAME_TYPE) {
                return Err(ParseError::EegUnsequenced);
            }
            return Err(ParseError::EegFrameType(frame_type));
        }
        if p.len() != Self::LEN {
            return Err(ParseError::Length {
                characteristic: Characteristic::Eeg,
                actual: p.len(),
            });
        }
        let bad = ParseError::Length {
            characteristic: Characteristic::Eeg,
            actual: p.len(),
        };
        let mut samples = [[0i32; 6]; 9];
        let mut flags = [0u8; 9];
        for (i, (row, flag)) in samples.iter_mut().zip(flags.iter_mut()).enumerate() {
            let base = 6 + 19 * i;
            for (j, v) in row.iter_mut().enumerate() {
                *v = i24(read3(p, base + 3 * j).ok_or(bad)?);
            }
            *flag = *p.get(base + 18).ok_or(bad)?;
        }
        Ok(Self {
            sequence: *p.first().ok_or(bad)?,
            device_time: be_u32(p, 2).ok_or(bad)?,
            samples,
            flags,
            trailer: *p.get(Self::LEN - 1).ok_or(bad)?,
        })
    }
}

/// Accelerometer or gyroscope frame: 25 samples of x, y, z signed 16-bit counts.
///
/// Sensor frame (verified): +x down (toward the chin when worn), +y toward the wearer's left,
/// +z forward. Accelerometer ≈ 16 384 counts/g (±2 g); gyroscope scale unknown.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MotionPacket {
    /// Device time (whole seconds).
    pub device_time: u32,
    /// `samples[i] = [x, y, z]` raw counts.
    pub samples: [[i16; 3]; 25],
    /// Last four bytes (meaning unknown; mostly zero).
    pub tail: [u8; 4],
}

impl MotionPacket {
    /// Verified frame length.
    pub const LEN: usize = 158;
    /// Samples per frame.
    pub const SAMPLES: usize = 25;
    /// Nominal sample rate.
    pub const RATE_HZ: f64 = 50.0;

    /// Decodes a frame received on `characteristic` (used only for error reporting).
    pub fn decode(characteristic: Characteristic, p: &[u8]) -> Result<Self, ParseError> {
        let bad = ParseError::Length {
            characteristic,
            actual: p.len(),
        };
        if p.len() != Self::LEN {
            return Err(bad);
        }
        let mut samples = [[0i16; 3]; 25];
        for (i, s) in samples.iter_mut().enumerate() {
            for (a, v) in s.iter_mut().enumerate() {
                *v = be_u16(p, 4 + 6 * i + 2 * a).ok_or(bad)? as i16;
            }
        }
        let t = p.get(154..158).ok_or(bad)?;
        Ok(Self {
            device_time: be_u32(p, 0).ok_or(bad)?,
            samples,
            tail: [
                *t.first().ok_or(bad)?,
                *t.get(1).ok_or(bad)?,
                *t.get(2).ok_or(bad)?,
                *t.get(3).ok_or(bad)?,
            ],
        })
    }
}

/// PPG frame: up to [`PpgPacket::MAX_SAMPLES`] samples of three unsigned 19-bit optical channels.
///
/// Channel `k` holds the word tagged `k + 1`. Channel 0 (tag 1) is always zero on the verified
/// hardware; which LED each tag drives is not yet known.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PpgPacket {
    /// Device time (whole seconds).
    pub device_time: u32,
    samples: [[u32; 3]; Self::MAX_SAMPLES],
    len: u8,
    /// Final two bytes (always zero so far).
    pub trailer: [u8; 2],
}

impl PpgPacket {
    /// Largest sample count accepted (12 and 13 observed).
    pub const MAX_SAMPLES: usize = 16;
    /// Nominal sample rate.
    pub const RATE_HZ: f64 = 25.0;

    /// Decoded samples, `[channel0, channel1, channel2]` each.
    pub fn samples(&self) -> &[[u32; 3]] {
        self.samples.get(..self.len as usize).unwrap_or(&[])
    }

    /// Decodes a frame.
    pub fn decode(p: &[u8]) -> Result<Self, ParseError> {
        let bad = ParseError::Length {
            characteristic: Characteristic::Ppg,
            actual: p.len(),
        };
        let body = p.len().checked_sub(6).ok_or(bad)?;
        let n = body / 9;
        if body % 9 != 0 || n == 0 || n > Self::MAX_SAMPLES {
            return Err(bad);
        }
        let mut samples = [[0u32; 3]; Self::MAX_SAMPLES];
        for (i, s) in samples.iter_mut().take(n).enumerate() {
            let mut seen = [false; 3];
            for k in 0..3 {
                let w = read3(p, 4 + 9 * i + 3 * k).ok_or(bad)?;
                let tag = usize::from(w[0] >> 3);
                let value = (u32::from(w[0] & 0x07) << 16) | (u32::from(w[1]) << 8) | u32::from(w[2]);
                let col = tag
                    .checked_sub(1)
                    .filter(|c| *c < 3)
                    .ok_or(ParseError::PpgTags { sample: i })?;
                let slot = seen.get_mut(col).ok_or(ParseError::PpgTags { sample: i })?;
                if *slot {
                    return Err(ParseError::PpgTags { sample: i });
                }
                *slot = true;
                *s.get_mut(col).ok_or(ParseError::PpgTags { sample: i })? = value;
            }
        }
        Ok(Self {
            device_time: be_u32(p, 0).ok_or(bad)?,
            samples,
            len: n as u8,
            trailer: [*p.get(p.len() - 2).ok_or(bad)?, *p.last().ok_or(bad)?],
        })
    }
}

/// Heart-rate or SpO2 reading.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VitalPacket {
    /// Device time (whole seconds).
    pub device_time: u32,
    /// Reading (beats per minute or percent saturation); 0 when the band has no reading.
    pub value: u8,
    /// 1 when the band has no reading, otherwise 0.
    pub check: u8,
}

impl VitalPacket {
    /// Verified length.
    pub const LEN: usize = 6;

    /// Decodes a reading received on `characteristic` (used only for error reporting).
    pub fn decode(characteristic: Characteristic, p: &[u8]) -> Result<Self, ParseError> {
        let bad = ParseError::Length {
            characteristic,
            actual: p.len(),
        };
        if p.len() != Self::LEN {
            return Err(bad);
        }
        Ok(Self {
            device_time: be_u32(p, 0).ok_or(bad)?,
            value: *p.get(4).ok_or(bad)?,
            check: *p.get(5).ok_or(bad)?,
        })
    }
}

fn time_and_byte(characteristic: Characteristic, p: &[u8]) -> Result<(u32, u8), ParseError> {
    let bad = ParseError::Length {
        characteristic,
        actual: p.len(),
    };
    if p.len() != 5 {
        return Err(bad);
    }
    Ok((be_u32(p, 0).ok_or(bad)?, *p.get(4).ok_or(bad)?))
}

/// Battery level.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BatteryPacket {
    /// Device time (whole seconds).
    pub device_time: u32,
    /// Charge in percent (matches the standard GATT battery level).
    pub percent: u8,
}

impl BatteryPacket {
    /// Decodes a battery notification.
    pub fn decode(p: &[u8]) -> Result<Self, ParseError> {
        let (device_time, percent) = time_and_byte(Characteristic::Battery, p)?;
        Ok(Self { device_time, percent })
    }
}

/// Electrode contact state, sent every 2 s.
///
/// Observed: 3 while worn, 2 intermittently while worn, 1 shortly after putting the band on,
/// 0 once removed, and 0, 0, 1, 1, 3 over ~10 s after every stream start. Probably a 0–3 contact-quality
/// level (unverified).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ContactPacket {
    /// Device time (whole seconds).
    pub device_time: u32,
    /// Raw state byte.
    pub state: u8,
}

impl ContactPacket {
    /// Decodes a contact notification.
    pub fn decode(p: &[u8]) -> Result<Self, ParseError> {
        let (device_time, state) = time_and_byte(Characteristic::Contact, p)?;
        Ok(Self { device_time, state })
    }

    /// True when the band reports no contact at all (it powers off ~33 s later).
    pub fn off_head(&self) -> bool {
        self.state == 0
    }
}

/// A message on the status channel, kept verbatim.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatusMessage {
    bytes: [u8; Self::MAX_LEN],
    len: u8,
}

/// Interpretation of a status message. Only [`StatusKind::Heartbeat`] is a structural certainty;
/// the others are inferred from timing and may be renamed as they are verified.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusKind {
    /// `0e 00 00 00 00`, about once a second.
    Heartbeat,
    /// `07 db` or `07 d0`: seen seconds before the band powered itself off when off-head.
    PowerOffWarning,
    /// `07 11`, `07 12 …`: emitted as a block every 60 s.
    Periodic,
    /// Anything else.
    Other,
}

impl StatusMessage {
    /// Longest message accepted (6 bytes observed).
    pub const MAX_LEN: usize = 32;

    /// Keeps a status message verbatim.
    pub fn decode(p: &[u8]) -> Result<Self, ParseError> {
        if p.is_empty() || p.len() > Self::MAX_LEN {
            return Err(ParseError::Length {
                characteristic: Characteristic::Status,
                actual: p.len(),
            });
        }
        let mut bytes = [0u8; Self::MAX_LEN];
        bytes
            .get_mut(..p.len())
            .ok_or(ParseError::EmptyResponse)?
            .copy_from_slice(p);
        Ok(Self {
            bytes,
            len: p.len() as u8,
        })
    }

    /// The raw message.
    pub fn bytes(&self) -> &[u8] {
        self.bytes.get(..self.len as usize).unwrap_or(&[])
    }

    /// Best-known interpretation.
    pub fn kind(&self) -> StatusKind {
        match self.bytes() {
            [0x0e, 0, 0, 0, 0] => StatusKind::Heartbeat,
            [0x07, 0xdb] | [0x07, 0xd0] => StatusKind::PowerOffWarning,
            [0x07, 0x11] | [0x07, 0x12, ..] => StatusKind::Periodic,
            _ => StatusKind::Other,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn eeg_frame(seq: u8, secs: u32, value: impl Fn(usize, usize) -> i32) -> [u8; 178] {
        let mut p = [0u8; 178];
        p[0] = seq;
        p[1] = 6;
        p[2..6].copy_from_slice(&secs.to_be_bytes());
        for i in 0..9 {
            for j in 0..6 {
                let v = value(i, j).to_be_bytes();
                p[6 + 19 * i + 3 * j..6 + 19 * i + 3 * j + 3].copy_from_slice(&v[1..4]);
            }
            p[6 + 19 * i + 18] = (i % 2) as u8;
        }
        p[177] = 9;
        p
    }

    #[test]
    fn eeg_decodes_signed_24_bit_samples() {
        let p = eeg_frame(200, 1_795_201_441, |i, j| {
            (i as i32 * 100 + j as i32) * if j % 2 == 0 { -1 } else { 1 }
        });
        let e = EegPacket::decode(&p).unwrap();
        assert_eq!((e.sequence, e.device_time, e.trailer), (200, 1_795_201_441, 9));
        assert_eq!(e.samples[3], [-300, 301, -302, 303, -304, 305]);
        assert_eq!(e.flags[..3], [0, 1, 0]);
        let extremes = eeg_frame(0, 0, |_, j| if j == 0 { -(1 << 23) } else { (1 << 23) - 1 });
        assert_eq!(
            EegPacket::decode(&extremes).unwrap().samples[0][..2],
            [-(1 << 23), (1 << 23) - 1]
        );
    }

    #[test]
    fn eeg_rejects_other_frame_types_and_lengths() {
        let mut p = eeg_frame(0, 0, |_, _| 0);
        p[1] = 106;
        assert_eq!(EegPacket::decode(&p), Err(ParseError::EegFrameType(106)));
        p[0] = 6; // unsequenced variant: type byte first, Unix time follows
        assert_eq!(EegPacket::decode(&p), Err(ParseError::EegUnsequenced));
        p[1] = 6;
        assert!(matches!(
            EegPacket::decode(&p[..177]),
            Err(ParseError::Length { actual: 177, .. })
        ));
        assert!(EegPacket::decode(&[]).is_err());
    }

    #[test]
    fn motion_decodes_big_endian_i16() {
        let mut p = [0u8; 158];
        p[0..4].copy_from_slice(&383u32.to_be_bytes());
        p[4..6].copy_from_slice(&(-14799i16).to_be_bytes());
        p[152..154].copy_from_slice(&16384i16.to_be_bytes());
        p[154..158].copy_from_slice(&[0x01, 0xff, 0xe0, 0x00]);
        let m = MotionPacket::decode(Characteristic::Accelerometer, &p).unwrap();
        assert_eq!(
            (m.device_time, m.samples[0][0], m.samples[24][2], m.tail),
            (383, -14799, 16384, [1, 0xff, 0xe0, 0])
        );
        assert!(MotionPacket::decode(Characteristic::Accelerometer, &p[..154]).is_err());
    }

    fn ppg_word(tag: u8, value: u32) -> [u8; 3] {
        let w = (u32::from(tag) << 19) | (value & 0x7ffff);
        [(w >> 16) as u8, (w >> 8) as u8, w as u8]
    }

    #[test]
    fn ppg_decodes_by_tag_in_any_order() {
        let mut p = vec![0x6b, 0x00, 0x99, 0xa7];
        for i in 0..13u32 {
            for (tag, v) in [(1, 0), (3, 135_000 + i), (2, 0x7ffff - i)] {
                p.extend_from_slice(&ppg_word(tag, v));
            }
        }
        p.extend_from_slice(&[0, 0]);
        assert_eq!(p.len(), 123);
        let d = PpgPacket::decode(&p).unwrap();
        assert_eq!(d.samples().len(), 13);
        assert_eq!(d.samples()[12], [0, 0x7ffff - 12, 135_012]);
    }

    #[test]
    fn ppg_rejects_duplicate_or_unknown_tags() {
        let mut p = vec![0, 0, 0, 0];
        for tag in [1, 1, 2] {
            p.extend_from_slice(&ppg_word(tag, 5));
        }
        p.extend_from_slice(&[0, 0]);
        assert_eq!(PpgPacket::decode(&p), Err(ParseError::PpgTags { sample: 0 }));
        p[4] = 5 << 3;
        assert_eq!(PpgPacket::decode(&p), Err(ParseError::PpgTags { sample: 0 }));
        assert!(PpgPacket::decode(&p[..10]).is_err());
    }

    #[test]
    fn small_packets() {
        let v = VitalPacket::decode(Characteristic::HeartRate, &[0x6b, 0, 0x92, 0xf6, 72, 0]).unwrap();
        assert_eq!((v.device_time, v.value, v.check), (1_795_199_734, 72, 0));
        assert_eq!(BatteryPacket::decode(&[0, 0, 1, 0x81, 0x5a]).unwrap().percent, 90);
        assert!(ContactPacket::decode(&[0, 0, 1, 0x80, 0]).unwrap().off_head());
        assert_eq!(
            StatusMessage::decode(&[0x0e, 0, 0, 0, 0]).unwrap().kind(),
            StatusKind::Heartbeat
        );
        assert_eq!(
            StatusMessage::decode(&[0x07, 0xdb]).unwrap().kind(),
            StatusKind::PowerOffWarning
        );
        assert_eq!(
            StatusMessage::decode(&[0x07, 0x12, 0xaa, 0xaa, 0, 0x25])
                .unwrap()
                .kind(),
            StatusKind::Periodic
        );
        assert!(StatusMessage::decode(&[]).is_err());
    }
}