Skip to content

Commit

Permalink
Adds tests to ext ID & fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JannesBrands committed Feb 21, 2024
1 parent 073356f commit 0c8d683
Showing 1 changed file with 98 additions and 6 deletions.
104 changes: 98 additions & 6 deletions src/j1939/extended_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ pub struct ExtendedId {
}

impl ExtendedId {
/// The number of bits in the extended identifier
pub const BIT_LENGTH: u8 = 29;
const PRIORITY_START: usize = 0;
const PRIORITY_END: usize = 3;
const PGN_START: usize = 3;
const PGN_END: usize = 21;
const SOURCE_ADDRESS_START: usize = 21;
const SOURCE_ADDRESS_END: usize = 29;

pub fn new(standard_id: StandardId, pgn: Pgn) -> Self {
Self { standard_id, pgn }
}

/// Get the raw value of the extended identifier
/// Raw value of the extended identifier
pub fn raw(&self) -> u32 {
let mut raw_id: BitVec<u32> = BitVec::new();
raw_id.extend(self.standard_id.priority().raw_bits());
Expand All @@ -34,7 +41,21 @@ impl ExtendedId {
raw_id.load::<u32>()
}

/// Get the PGN of the identifier
/// Raw bits of the extended identifier
pub fn raw_bits(&self) -> [bool; 29] {
let mut raw_id: BitVec<u32> = BitVec::new();
raw_id.extend(self.standard_id.priority().raw_bits());
raw_id.extend(self.pgn.raw_bits());
raw_id.extend(self.standard_id.source_address().raw_bits());
[
raw_id[0], raw_id[1], raw_id[2], raw_id[3], raw_id[4], raw_id[5], raw_id[6], raw_id[7],
raw_id[8], raw_id[9], raw_id[10], raw_id[11], raw_id[12], raw_id[13], raw_id[14],
raw_id[15], raw_id[16], raw_id[17], raw_id[18], raw_id[19], raw_id[20], raw_id[21],
raw_id[22], raw_id[23], raw_id[24], raw_id[25], raw_id[26], raw_id[27], raw_id[28],
]
}

/// PGN of the identifier
#[inline]
pub fn pgn(&self) -> Pgn {
self.pgn
Expand All @@ -52,14 +73,28 @@ impl TryFrom<u32> for ExtendedId {

fn try_from(raw_id: u32) -> Result<Self, Self::Error> {
let bit_data = raw_id.view_bits::<Msb0>().to_bitvec();
let priority = Priority::try_from(bit_data.load::<u8>());
let pgn = Pgn::try_from(bit_data.load::<u32>());
let source_address = Address::new(bit_data.load::<u8>());
let mut priority_bits =
bit_data[ExtendedId::PRIORITY_START..ExtendedId::PRIORITY_END].to_bitvec();
let mut pgn_bits = bit_data[ExtendedId::PGN_START..ExtendedId::PGN_END].to_bitvec();
let mut source_address_bits =
bit_data[ExtendedId::SOURCE_ADDRESS_START..ExtendedId::SOURCE_ADDRESS_END].to_bitvec();

priority_bits.reverse();
pgn_bits.reverse();
source_address_bits.reverse();

let priority = Priority::try_from(priority_bits.load::<u8>());
let pgn = Pgn::try_from(pgn_bits.load::<u32>());
let source_address = Address::new(source_address_bits.load::<u8>());

if priority.is_err() || pgn.is_err() {
if priority.is_err() {
return Err(ParseIdError::Priority);
}

if pgn.is_err() {
return Err(ParseIdError::Pgn);
}

Ok(ExtendedId::new(
StandardId::new(priority.unwrap(), source_address),
pgn.unwrap(),
Expand All @@ -84,5 +119,62 @@ mod tests {
Pgn::new(false, true, PduFormat::new(0x8A), PduSpecific::new(0x0F)),
);
assert_eq!(id.raw(), 0x18A0F25);

let id = ExtendedId::new(
StandardId::new(Priority::Seven, Address::new(0xAF)),
Pgn::new(false, true, PduFormat::new(0x8A), PduSpecific::new(0x2F)),
);
assert_eq!(id.raw(), 0x1D8A2FAF);

let id = ExtendedId::new(
StandardId::new(Priority::Zero, Address::new(0x00)),
Pgn::new(true, false, PduFormat::new(0x4C), PduSpecific::new(0x12)),
);
assert_eq!(id.raw(), 0x24C1200);
}

#[test]
fn test_raw_bits() {
let id = ExtendedId::new(
StandardId::new(Priority::Zero, Address::new(0x25)),
Pgn::new(false, true, PduFormat::new(0x8A), PduSpecific::new(0x0F)),
);
assert_eq!(
id.raw_bits(),
[
false, false, false, false, true, true, false, false, false, true, false, true,
false, false, false, false, false, true, true, true, true, false, false, true,
false, false, true, false, true
]
);

let id = ExtendedId::new(
StandardId::new(Priority::Seven, Address::new(0xAF)),
Pgn::new(false, true, PduFormat::new(0x8A), PduSpecific::new(0x2F)),
);

assert_eq!(
id.raw_bits(),
[
true, true, true, false, true, true, false, false, false, true, false, true, false,
false, false, true, false, true, true, true, true, true, false, true, false, true,
true, true, true
]
);
}

#[test]
fn test_from_extended_id_for_embedded_id() {
let id = ExtendedId::new(
StandardId::new(Priority::Zero, Address::new(0x25)),
Pgn::new(false, true, PduFormat::new(0x8A), PduSpecific::new(0x0F)),
);
let embedded_id: embedded_can::Id = id.into();
assert_eq!(
embedded_id,
embedded_can::Id::Extended(embedded_can::ExtendedId::new(0x18A0F25).unwrap())
);
}

}
}

Check failure on line 180 in src/j1939/extended_id.rs

View workflow job for this annotation

GitHub Actions / Test and coverage

unexpected closing delimiter: `}`

Check failure on line 180 in src/j1939/extended_id.rs

View workflow job for this annotation

GitHub Actions / Format

unexpected closing delimiter: `}`

0 comments on commit 0c8d683

Please sign in to comment.