mod parse;
mod reassembly;
mod result;
pub use parse::{ParseError, parse_packet};
pub use reassembly::{Reassembler, Yielded};
pub use result::encode_response;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Command {
SendWifiSettings { ssid: String, password: String },
Identify,
DeviceInfo,
Scan,
GetHostname,
SetHostname(String),
GetDeviceName,
SetDeviceName(String),
}
impl Command {
pub fn id(&self) -> u8 {
match self {
Self::SendWifiSettings { .. } => 0x01,
Self::Identify => 0x02,
Self::DeviceInfo => 0x03,
Self::Scan => 0x04,
Self::GetHostname | Self::SetHostname(_) => 0x05,
Self::GetDeviceName | Self::SetDeviceName(_) => 0x06,
}
}
}
pub fn checksum(bytes: &[u8]) -> u8 {
bytes.iter().fold(0u8, |acc, &b| acc.wrapping_add(b))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn checksum_lsb_wraps() {
assert_eq!(checksum(&[]), 0);
assert_eq!(checksum(&[0x01, 0x02, 0x03]), 0x06);
assert_eq!(checksum(&[0xFF, 0x01]), 0x00);
assert_eq!(checksum(&[1u8; 256]), 0);
}
}