#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[deprecated(
note = "The `auto_uds` crate has been renamed to `automotive_diag`. Update your Cargo.toml, and use this enum from the `uds` namespace."
)]
pub enum CommunicationType {
NormalCommunication,
NetworkManagement,
All,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[deprecated(
note = "The `auto_uds` crate has been renamed to `automotive_diag`. Update your Cargo.toml, and use this enum from the `uds` namespace."
)]
pub enum Subnet {
All,
Custom(u8),
RxOnly,
}
pub fn decode_communication_type(value: u8) -> Result<(CommunicationType, Subnet), u8> {
let typ = match value & 0x0F {
0x01 => CommunicationType::NormalCommunication,
0x02 => CommunicationType::NetworkManagement,
0x03 => CommunicationType::All,
value => return Err(value),
};
let subnet = match value >> 4 {
0x00 => Subnet::All,
0x0F => Subnet::RxOnly,
x => Subnet::Custom(x),
};
Ok((typ, subnet))
}
#[must_use]
pub fn encode_communication_type(typ: CommunicationType, subnet: Subnet) -> u8 {
let typ = match typ {
CommunicationType::NormalCommunication => 0x01,
CommunicationType::NetworkManagement => 0x02,
CommunicationType::All => 0x03,
};
let subnet = match subnet {
Subnet::All => 0x00,
Subnet::Custom(x) => x,
Subnet::RxOnly => 0x0F,
};
typ | (subnet << 4)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encode_decode_enum() {
for value in 0_u8..=0xFF {
match decode_communication_type(value) {
Ok((typ, sub)) => {
let enc = encode_communication_type(typ, sub);
assert_eq!(value, enc, "{value:#02X} → ({typ:?}, {sub:?}) → {enc:#02X}");
}
Err(err) => {
assert_eq!(value & 0x0F, err);
}
}
}
}
}