use crate::command::Response;
use crate::error::ParseError;
use crate::gatt::Characteristic;
use crate::{be_u16, be_u32};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Packet {
Eeg(EegPacket),
Accelerometer(MotionPacket),
Gyroscope(MotionPacket),
Ppg(PpgPacket),
HeartRate(VitalPacket),
Spo2(VitalPacket),
Battery(BatteryPacket),
Contact(ContactPacket),
Status(StatusMessage),
Response(Response),
}
impl Packet {
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),
})
}
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,
}
}
}
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)?])
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EegPacket {
pub sequence: u8,
pub device_time: u32,
pub samples: [[i32; 6]; 9],
pub flags: [u8; 9],
pub trailer: u8,
}
impl EegPacket {
pub const LEN: usize = 178;
pub const FRAME_TYPE: u8 = 6;
pub const SAMPLES: usize = 9;
pub const RATE_HZ: f64 = 125.0;
pub const ACTIVE_CHANNELS: [(usize, &'static str); 4] = [
(0, "forehead_right"),
(1, "comb_left"),
(3, "forehead_left"),
(4, "comb_right"),
];
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)?,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MotionPacket {
pub device_time: u32,
pub samples: [[i16; 3]; 25],
pub tail: [u8; 4],
}
impl MotionPacket {
pub const LEN: usize = 158;
pub const SAMPLES: usize = 25;
pub const RATE_HZ: f64 = 50.0;
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)?,
],
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PpgPacket {
pub device_time: u32,
samples: [[u32; 3]; Self::MAX_SAMPLES],
len: u8,
pub trailer: [u8; 2],
}
impl PpgPacket {
pub const MAX_SAMPLES: usize = 16;
pub const RATE_HZ: f64 = 25.0;
pub fn samples(&self) -> &[[u32; 3]] {
self.samples.get(..self.len as usize).unwrap_or(&[])
}
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)?],
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VitalPacket {
pub device_time: u32,
pub value: u8,
pub check: u8,
}
impl VitalPacket {
pub const LEN: usize = 6;
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)?))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BatteryPacket {
pub device_time: u32,
pub percent: u8,
}
impl BatteryPacket {
pub fn decode(p: &[u8]) -> Result<Self, ParseError> {
let (device_time, percent) = time_and_byte(Characteristic::Battery, p)?;
Ok(Self { device_time, percent })
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ContactPacket {
pub device_time: u32,
pub state: u8,
}
impl ContactPacket {
pub fn decode(p: &[u8]) -> Result<Self, ParseError> {
let (device_time, state) = time_and_byte(Characteristic::Contact, p)?;
Ok(Self { device_time, state })
}
pub fn off_head(&self) -> bool {
self.state == 0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatusMessage {
bytes: [u8; Self::MAX_LEN],
len: u8,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusKind {
Heartbeat,
PowerOffWarning,
Periodic,
Other,
}
impl StatusMessage {
pub const MAX_LEN: usize = 32;
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,
})
}
pub fn bytes(&self) -> &[u8] {
self.bytes.get(..self.len as usize).unwrap_or(&[])
}
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; 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());
}
}