#![allow(dead_code)]
use num_enum::{TryFromPrimitive, TryFromPrimitiveError};
use crate::{Error, Result};
#[derive(Debug)]
pub struct NameVersion {
pub name0: String,
pub name1: String,
pub version: u32,
}
pub struct TkeyFrame {
pub(crate) header: TKeyFrameHeader,
pub(crate) code: u8,
pub(crate) data: Vec<u8>,
}
#[derive(Debug)]
pub struct TKeyFrameHeader {
pub(crate) id: u8,
pub(crate) endpoint: Endpoint,
pub(crate) cmd_len: CmdLen,
}
#[derive(Debug, TryFromPrimitive)]
#[repr(u8)]
pub enum Endpoint {
DestFW = 2,
DestApp = 3,
}
#[derive(Debug, TryFromPrimitive)]
#[repr(u8)]
pub enum CmdLen {
CmdLen1 = 0,
CmdLen4 = 1,
CmdLen32 = 2,
CmdLen128 = 3,
}
impl CmdLen {
pub fn byte_len(&self) -> u8 {
match self {
CmdLen::CmdLen1 => 1,
CmdLen::CmdLen4 => 4,
CmdLen::CmdLen32 => 32,
CmdLen::CmdLen128 => 128,
}
}
}
impl TKeyFrameHeader {
pub fn parse(b: u8) -> Result<Self> {
if (b & 0b1000_0000) != 0 {
return Err(Error::TkeySerialFrameHeaderParsingFailed("".to_string()));
}
if (b & 0b0000_0100) != 0 {
return Err(Error::TkeySerialFrameHeaderParsingFailed("".to_string()));
}
Ok(Self {
id: (b & 0b0110_0000) >> 5,
endpoint: ((b & 0b0001_1000) >> 3).try_into().map_err(
|e: TryFromPrimitiveError<Endpoint>| {
Error::TkeySerialFrameHeaderParsingFailed(e.to_string())
},
)?,
cmd_len: (b & 0b0000_0011)
.try_into()
.map_err(|e: TryFromPrimitiveError<CmdLen>| {
Error::TkeySerialFrameHeaderParsingFailed(e.to_string())
})?,
})
}
}