Skip to content

Commit

Permalink
extend address helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannes Brands committed Jan 27, 2024
1 parent bf52d30 commit 4b15403
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/j1939/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,34 @@

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Address(pub u8);
pub struct Address(u8);

impl Address {
/// Address representing broadcasts for destination specific PGNs
pub const GLOBAL: Address = Address(0xFF);
pub const GLOBAL: Address = Self::BROADCAST;
/// Alias for the global address
pub const BROADCAST: Address = Address(0xFF);
/// The null address is used by ECUs without an address such as during address claiming
pub const NULL: Address = Address(0xFE);

pub fn new(raw_address: u8) -> Self {
Self { 0: raw_address }
}

#[inline]
pub fn is_global(&self) -> bool {
self.is_broadcast()
}

#[inline]
pub fn is_broadcast(&self) -> bool {
self == Self::BROADCAST
}

#[inline]
pub fn is_null(&self) -> bool {
self == Self::NULL
}
}

// TODO: custom Debug impl and helpers

0 comments on commit 4b15403

Please sign in to comment.