use crate::error::CanError;
pub const STANDARD_ID_MAX: u32 = 0x7FF;
pub const EXTENDED_ID_MAX: u32 = 0x1FFF_FFFF;
pub(crate) const CAN_EFF_FLAG: u32 = 0x8000_0000;
pub(crate) const CAN_RTR_FLAG: u32 = 0x4000_0000;
pub(crate) const CAN_ERR_FLAG: u32 = 0x2000_0000;
pub(crate) const CAN_EFF_MASK: u32 = 0x1FFF_FFFF;
pub(crate) const CAN_SFF_MASK: u32 = 0x7FF;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CanId {
Standard(u16),
Extended(u32),
}
impl CanId {
pub fn standard(raw: u16) -> Result<Self, CanError> {
if u32::from(raw) > STANDARD_ID_MAX {
return Err(CanError::InvalidIdLength {
max: STANDARD_ID_MAX,
got: u32::from(raw),
});
}
Ok(Self::Standard(raw))
}
pub fn extended(raw: u32) -> Result<Self, CanError> {
if raw > EXTENDED_ID_MAX {
return Err(CanError::InvalidIdLength {
max: EXTENDED_ID_MAX,
got: raw,
});
}
Ok(Self::Extended(raw))
}
pub fn from_raw_socketcan(word: u32) -> Result<Self, CanError> {
if word & CAN_ERR_FLAG != 0 {
return Err(CanError::InvalidIdLength {
max: CAN_EFF_MASK,
got: word,
});
}
if word & CAN_EFF_FLAG != 0 {
Self::extended(word & CAN_EFF_MASK)
} else {
let id = word & CAN_EFF_MASK;
if id > CAN_SFF_MASK {
return Err(CanError::InvalidIdLength {
max: CAN_SFF_MASK,
got: id,
});
}
#[allow(clippy::cast_possible_truncation)]
Ok(Self::Standard(id as u16))
}
}
#[must_use]
pub const fn is_standard(&self) -> bool {
matches!(self, Self::Standard(_))
}
#[must_use]
pub const fn is_extended(&self) -> bool {
matches!(self, Self::Extended(_))
}
#[must_use]
pub const fn width(&self) -> u8 {
match self {
Self::Standard(_) => 11,
Self::Extended(_) => 29,
}
}
#[must_use]
pub const fn raw(&self) -> u32 {
match *self {
Self::Standard(v) => v as u32,
Self::Extended(v) => v,
}
}
#[must_use]
pub const fn to_raw_socketcan(&self) -> u32 {
match *self {
Self::Standard(v) => v as u32,
Self::Extended(v) => v | CAN_EFF_FLAG,
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]
use super::*;
#[test]
fn standard_boundary() {
assert!(CanId::standard(0).is_ok());
assert!(CanId::standard(0x7FF).is_ok());
assert_eq!(
CanId::standard(0x800),
Err(CanError::InvalidIdLength {
max: STANDARD_ID_MAX,
got: 0x800
})
);
}
#[test]
fn extended_boundary() {
assert!(CanId::extended(0).is_ok());
assert!(CanId::extended(EXTENDED_ID_MAX).is_ok());
assert_eq!(
CanId::extended(0x2000_0000),
Err(CanError::InvalidIdLength {
max: EXTENDED_ID_MAX,
got: 0x2000_0000
})
);
}
#[test]
fn raw_socketcan_roundtrip() {
assert_eq!(CanId::from_raw_socketcan(0x123), Ok(CanId::Standard(0x123)));
assert_eq!(
CanId::from_raw_socketcan(0x123 | CAN_EFF_FLAG),
Ok(CanId::Extended(0x123))
);
assert_eq!(
CanId::Extended(0x1AB).to_raw_socketcan(),
0x1AB | CAN_EFF_FLAG
);
assert_eq!(CanId::Standard(0x1AB).to_raw_socketcan(), 0x1AB);
assert!(CanId::from_raw_socketcan(0x800).is_err());
assert!(CanId::from_raw_socketcan(CAN_ERR_FLAG | 0x123).is_err());
assert!(CanId::from_raw_socketcan(0x0000_1000).is_err());
}
#[test]
fn widths() {
assert_eq!(CanId::Standard(0).width(), 11);
assert_eq!(CanId::Extended(0).width(), 29);
}
}