const fn frenz_uuid(short: u32) -> u128 {
((short as u128) << 96) | 0x0000_0000_0000_ffff_ff45_4152_4142_4c45
}
pub const COMMAND_SERVICE: u128 = frenz_uuid(0x4542_0100);
pub const STREAM_SERVICE: u128 = frenz_uuid(0x4542_0200);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Characteristic {
CommandRx,
CommandResponse,
Status,
Eeg,
Accelerometer,
Ppg,
HeartRate,
Spo2,
Battery,
Gyroscope,
Contact,
}
impl Characteristic {
pub const ALL: [Self; 11] = [
Self::CommandRx,
Self::CommandResponse,
Self::Status,
Self::Eeg,
Self::Accelerometer,
Self::Ppg,
Self::HeartRate,
Self::Spo2,
Self::Battery,
Self::Gyroscope,
Self::Contact,
];
pub const NOTIFY: [Self; 10] = [
Self::CommandResponse,
Self::Status,
Self::Eeg,
Self::Accelerometer,
Self::Ppg,
Self::HeartRate,
Self::Spo2,
Self::Battery,
Self::Gyroscope,
Self::Contact,
];
pub const fn uuid(self) -> u128 {
frenz_uuid(match self {
Self::CommandRx => 0x4542_0101,
Self::CommandResponse => 0x4542_0102,
Self::Status => 0x4542_0103,
Self::Eeg => 0x4542_0201,
Self::Accelerometer => 0x4542_0202,
Self::Ppg => 0x4542_0203,
Self::HeartRate => 0x4542_0204,
Self::Spo2 => 0x4542_0205,
Self::Battery => 0x4542_0206,
Self::Gyroscope => 0x4542_0207,
Self::Contact => 0x4542_0208,
})
}
pub fn from_uuid(uuid: u128) -> Option<Self> {
Self::ALL.into_iter().find(|c| c.uuid() == uuid)
}
pub const fn name(self) -> &'static str {
match self {
Self::CommandRx => "command_rx",
Self::CommandResponse => "command_response",
Self::Status => "status",
Self::Eeg => "eeg",
Self::Accelerometer => "accelerometer",
Self::Ppg => "ppg",
Self::HeartRate => "heart_rate",
Self::Spo2 => "spo2",
Self::Battery => "battery",
Self::Gyroscope => "gyroscope",
Self::Contact => "contact",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn uuids_match_the_wire() {
assert_eq!(Characteristic::Eeg.uuid(), 0x4542_0201_0000_ffff_ff45_4152_4142_4c45);
assert_eq!(COMMAND_SERVICE, 0x4542_0100_0000_ffff_ff45_4152_4142_4c45);
for c in Characteristic::ALL {
assert_eq!(Characteristic::from_uuid(c.uuid()), Some(c));
}
assert_eq!(Characteristic::from_uuid(0), None);
}
}