Init repo
This commit is contained in:
105
src/improv.rs
Normal file
105
src/improv.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
pub const IMPROV_HEADER: [u8; 6] = [
|
||||
'I' as u8,
|
||||
'M' as u8,
|
||||
'P' as u8,
|
||||
'R' as u8,
|
||||
'O' as u8,
|
||||
'V' as u8,
|
||||
];
|
||||
|
||||
pub const IMPROV_VERSION: u8 = 0x01;
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
#[repr(u8)]
|
||||
pub enum PacketType {
|
||||
CurrentState = 0x01,
|
||||
ErrorState = 0x02,
|
||||
RPCCommand = 0x03,
|
||||
RPCResult = 0x04,
|
||||
}
|
||||
|
||||
impl TryFrom<&u8> for PacketType {
|
||||
type Error= &'static str;
|
||||
|
||||
fn try_from(b: &u8) -> Result<Self, Self::Error> {
|
||||
match b {
|
||||
0x01 => Ok(Self::CurrentState),
|
||||
0x02 => Ok(Self::ErrorState),
|
||||
0x03 => Ok(Self::RPCCommand),
|
||||
0x04 => Ok(Self::RPCResult),
|
||||
_ => Err("Cannot convert to packet type"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for PacketType {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::CurrentState => write!(f, "Current State"),
|
||||
Self::ErrorState => write!(f, "Error State"),
|
||||
Self::RPCCommand => write!(f, "RPC Command"),
|
||||
Self::RPCResult => write!(f, "RPC Result"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[repr(u8)]
|
||||
pub enum CurrentState {
|
||||
Ready = 0x02,
|
||||
Provisioning = 0x03,
|
||||
Provisioned = 0x04,
|
||||
}
|
||||
|
||||
|
||||
impl TryFrom<&u8> for CurrentState {
|
||||
type Error= &'static str;
|
||||
|
||||
fn try_from(b: &u8) -> Result<Self, Self::Error> {
|
||||
match b {
|
||||
0x02 => Ok(Self::Ready),
|
||||
0x03 => Ok(Self::Provisioning),
|
||||
0x04 => Ok(Self::Provisioned),
|
||||
_ => Err("Cannot convert to current state"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for CurrentState {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Ready => write!(f, "Ready"),
|
||||
Self::Provisioning => write!(f, "Provisioning"),
|
||||
Self::Provisioned => write!(f, "Provisioned"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[repr(u8)]
|
||||
pub enum ErrorState {
|
||||
NoError = 0x00,
|
||||
InvalidRPCPacket = 0x01,
|
||||
UnknownRPCCommand = 0x02,
|
||||
UnableToConnect = 0x03,
|
||||
UnknownError = 0xFF,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[repr(u8)]
|
||||
pub enum RPCCommand {
|
||||
SendWifiSettings = 0x01,
|
||||
RequestCurrentState = 0x02,
|
||||
}
|
||||
|
||||
pub fn calculate_checksum(data: &Vec<u8>) -> u8 {
|
||||
// Pass data as full packet, with header, but without checksum byte
|
||||
|
||||
let mut checksum: u8 = 0x00;
|
||||
|
||||
for e in data {
|
||||
checksum = checksum.wrapping_add(e.clone());
|
||||
}
|
||||
|
||||
return checksum;
|
||||
}
|
Reference in New Issue
Block a user