use byteorder::{ByteOrder, LittleEndian};
use proc_bitfield::bitfield;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ExtendedControlMessageType {
EprGetSourceCap,
EprGetSinkCap,
EprKeepAlive,
EprKeepAliveAck,
}
impl From<ExtendedControlMessageType> for u8 {
fn from(value: ExtendedControlMessageType) -> Self {
match value {
ExtendedControlMessageType::EprGetSourceCap => 1,
ExtendedControlMessageType::EprGetSinkCap => 2,
ExtendedControlMessageType::EprKeepAlive => 3,
ExtendedControlMessageType::EprKeepAliveAck => 4,
}
}
}
impl From<u8> for ExtendedControlMessageType {
fn from(value: u8) -> Self {
match value {
1 => ExtendedControlMessageType::EprGetSourceCap,
2 => ExtendedControlMessageType::EprGetSinkCap,
3 => ExtendedControlMessageType::EprKeepAlive,
4 => ExtendedControlMessageType::EprKeepAliveAck,
_ => panic!("Cannot convert {} to ExtendedControlMessageType", value), }
}
}
bitfield!(
#[derive(Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ExtendedControl(pub u16): Debug, FromStorage, IntoStorage {
pub data: u8 @ 8..=15,
pub message_type: u8 [ExtendedControlMessageType] @ 0..=7,
}
);
impl ExtendedControl {
pub fn to_bytes(self, buf: &mut [u8]) -> usize {
LittleEndian::write_u16(buf, self.0);
2
}
pub fn from_bytes(buf: &[u8]) -> Self {
assert!(buf.len() >= 2);
Self(LittleEndian::read_u16(buf))
}
}
impl Default for ExtendedControl {
fn default() -> Self {
Self(0).with_data(0)
}
}