pub mod message;
pub mod read;
pub mod write;
use crate::fsemul::sdio::errors::SdioProtocolError;
pub const SDIO_BLOCK_SIZE: usize = 0x200_usize;
pub const SDIO_BLOCK_SIZE_AS_U32: u32 = 0x200_u32;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum SdioControlPacketType {
TelnetMessage,
Read,
Write,
StartBlockChannel,
StartControlListeningChannel,
}
impl From<SdioControlPacketType> for u16 {
fn from(value: SdioControlPacketType) -> Self {
match value {
SdioControlPacketType::TelnetMessage => 8,
SdioControlPacketType::Read => 0,
SdioControlPacketType::Write => 1,
SdioControlPacketType::StartBlockChannel => 0xA,
SdioControlPacketType::StartControlListeningChannel => 0xB,
}
}
}
impl TryFrom<u16> for SdioControlPacketType {
type Error = SdioProtocolError;
fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::Read),
1 => Ok(Self::Write),
8 => Ok(Self::TelnetMessage),
0xA => Ok(Self::StartBlockChannel),
0xB => Ok(Self::StartControlListeningChannel),
_ => Err(SdioProtocolError::UnknownPrintfPacketType(value)),
}
}
}
#[cfg(test)]
mod unit_tests {
use super::*;
#[test]
pub fn roundtrip_control_packet_type() {
for packet_ty in vec![
SdioControlPacketType::TelnetMessage,
SdioControlPacketType::Read,
SdioControlPacketType::Write,
] {
assert_eq!(
Ok(packet_ty),
SdioControlPacketType::try_from(u16::from(packet_ty)),
"Round-tripped control packet type was not the same?"
);
}
}
}