Skip to main content

frenz_proto/
lib.rs

1//! Wire protocol of the FRENZ brainband: characteristic identifiers, stream decoders, command
2//! encoders and command-response decoders.
3//!
4//! This crate performs no I/O and allocates nothing. Every decoder is strict: a payload whose
5//! length or structure differs from the verified layout returns a [`ParseError`] instead of being
6//! guessed at, and no input can cause a panic. Fields whose meaning is not yet known are kept raw.
7//!
8//! The layouts and their verification status are documented in `docs/PROTOCOL.md`.
9#![cfg_attr(not(feature = "std"), no_std)]
10
11pub mod command;
12pub mod error;
13pub mod gatt;
14pub mod stream;
15
16pub use command::{Command, Response, ResponseBody};
17pub use error::ParseError;
18pub use gatt::Characteristic;
19pub use stream::{
20    BatteryPacket, ContactPacket, EegPacket, MotionPacket, Packet, PpgPacket, StatusMessage, VitalPacket,
21};
22
23/// Reads a big-endian `u32` at `offset`, or `None` if out of range.
24pub(crate) fn be_u32(bytes: &[u8], offset: usize) -> Option<u32> {
25    let b = bytes.get(offset..offset.checked_add(4)?)?;
26    Some(u32::from_be_bytes([*b.first()?, *b.get(1)?, *b.get(2)?, *b.get(3)?]))
27}
28
29/// Reads a big-endian `u16` at `offset`, or `None` if out of range.
30pub(crate) fn be_u16(bytes: &[u8], offset: usize) -> Option<u16> {
31    Some(u16::from_be_bytes([
32        *bytes.get(offset)?,
33        *bytes.get(offset.checked_add(1)?)?,
34    ]))
35}