From 25e97f3865c4eeb646ec3b71f5c1a668acdfe57f Mon Sep 17 00:00:00 2001 From: Jannes Brands Date: Thu, 22 Feb 2024 22:00:59 +0100 Subject: [PATCH] add important addresses & addr ranges, documentation --- src/j1939/address.rs | 57 +++++++++++++++++++++++++++++++++++---- src/j1939/byte_field.rs | 7 +++++ src/j1939/extended_id.rs | 56 +++++++++++++++++++++++++++++++++----- src/j1939/frame.rs | 8 +++++- src/j1939/id.rs | 6 +++++ src/j1939/page.rs | 6 +++++ src/j1939/pdu_format.rs | 6 +++++ src/j1939/pdu_specific.rs | 6 +++++ src/j1939/pgn.rs | 8 +++++- src/j1939/priority.rs | 6 +++++ src/j1939/standard_id.rs | 6 +++++ 11 files changed, 159 insertions(+), 13 deletions(-) diff --git a/src/j1939/address.rs b/src/j1939/address.rs index a8a60e4..2131fff 100644 --- a/src/j1939/address.rs +++ b/src/j1939/address.rs @@ -1,8 +1,13 @@ -// Copyright 2023 Raven Industries inc. +/* +Copyright 2023 Raven Industries inc. + +@author Jannes Brands +@date 2024-02-22 +*/ use crate::j1939::byte_field::ByteField; -/// J1939 address (8-bits) used to identify ECUs on the network +/// J1939 address (8-bits) used to identify control applications on the network #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[repr(transparent)] pub struct Address(u8); @@ -10,11 +15,53 @@ pub struct Address(u8); impl Address { /// The number of bits in the address pub const BIT_LENGTH: u8 = 8; - /// Address representing broadcasts for destination specific PGNs + + /// Global Preferred Addresses + /// + /// only to be used by control applications that handles the given function and + /// function instance, if applicable, that is assigned to that address by SAE J1939. + /// + /// For more information see SAE J1939 4.6.1 + pub const GLOBAL_PREFERRED_ADDRESSES: (std::ops::Range, std::ops::Range) = + (0x00..0x7F, 0xF8..0xFD); + + /// Dynamic addresses + /// + /// any control application executing any system function can claim and use it. + /// The supplier of a control application can employ any strategy + /// to select the initial address within the range of 128 to 247. + /// + /// For more information see SAE J1939 4.6.2 + pub const DYNAMIC_ADDRESSES: std::ops::Range = 0x80..0xF7; + + /// Global Address + /// + /// The SAE J1939 source address 255 serves as the global destination address. + /// This global destination address is exclusively utilized as the destination + /// address in a D_PDU1 data frame to signify that the SAE J1939 data frame is + /// intended for all Control Applications (CAs) on the network. + /// + /// For more information see SAE J1939 4.6.3 pub const GLOBAL: Address = Self::BROADCAST; - /// Alias for the global address + /// Alias for the [Address::GLOBAL] pub const BROADCAST: Address = Address(0xFF); - /// The null address is used by ECUs without an address such as during address claiming + + /// Null Address + /// + /// The SAE J1939 source address 254 is designated as the Null address. + /// This Null address is specifically employed as the source (transmitter) address + /// within a D_PDU1 or D_PDU2 data frame. + /// + /// There are only two approved applications for the Null address: + /// + /// 1. The Null address can be utilized with an Address Claimed Parameter Group (PG) + /// when a Control Application (CA) reports its inability to claim an SAE J1939 Address. + /// + /// 2. the Null address can be employed with a Request PG soliciting + /// the Address Claimed PG when the Request PG is transmitted by a CA + /// prior to claiming a source address. + /// + /// For more information see SAE J1939 4.6.4 pub const NULL: Address = Address(0xFE); /// Create a new address diff --git a/src/j1939/byte_field.rs b/src/j1939/byte_field.rs index a6217ae..5caac1e 100644 --- a/src/j1939/byte_field.rs +++ b/src/j1939/byte_field.rs @@ -1,3 +1,10 @@ +/* +Copyright 2023 Raven Industries inc. + +@author Jannes Brands +@date 2024-02-22 +*/ + use bitvec::order::Msb0; use bitvec::view::BitView; diff --git a/src/j1939/extended_id.rs b/src/j1939/extended_id.rs index 8b8ab44..ab57518 100644 --- a/src/j1939/extended_id.rs +++ b/src/j1939/extended_id.rs @@ -1,4 +1,9 @@ -// Copyright 2023 Raven Industries inc. +/* +Copyright 2023 Raven Industries inc. + +@author Jannes Brands +@date 2024-02-22 +*/ use crate::j1939::byte_field::ByteField; use crate::j1939::id::{Id, ParseIdError}; use crate::j1939::priority::Priority; @@ -68,11 +73,12 @@ impl From for EmbeddedId { } } -impl TryFrom for ExtendedId { +impl TryFrom<[bool; 29]> for ExtendedId { type Error = ParseIdError; - fn try_from(raw_id: u32) -> Result { - let bit_data = raw_id.view_bits::().to_bitvec(); + fn try_from(raw_bits: [bool; 29]) -> Result { + let mut bit_data: BitVec = BitVec::new(); + bit_data.extend(raw_bits.iter()); 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(); @@ -102,6 +108,45 @@ impl TryFrom for ExtendedId { } } +impl TryFrom for ExtendedId { + type Error = ParseIdError; + + fn try_from(raw_id: u32) -> Result { + let raw_id_bits = raw_id.view_bits::().to_bitvec(); + Self::try_from([ + raw_id_bits[3], + raw_id_bits[4], + raw_id_bits[5], + raw_id_bits[6], + raw_id_bits[7], + raw_id_bits[8], + raw_id_bits[9], + raw_id_bits[10], + raw_id_bits[11], + raw_id_bits[12], + raw_id_bits[13], + raw_id_bits[14], + raw_id_bits[15], + raw_id_bits[16], + raw_id_bits[17], + raw_id_bits[18], + raw_id_bits[19], + raw_id_bits[20], + raw_id_bits[21], + raw_id_bits[22], + raw_id_bits[23], + raw_id_bits[24], + raw_id_bits[25], + raw_id_bits[26], + raw_id_bits[27], + raw_id_bits[28], + raw_id_bits[29], + raw_id_bits[30], + raw_id_bits[31], + ]) + } +} + impl From for Id { fn from(id: ExtendedId) -> Self { Id::Extended(id) @@ -176,7 +221,6 @@ mod tests { ); } - /* not finished yet TODO! #[test] fn test_try_from_u32_for_extended_id() { let id = ExtendedId::try_from(0x18A0F25).unwrap(); @@ -205,5 +249,5 @@ mod tests { Pgn::new(true, false, PduFormat::new(0x4C), PduSpecific::new(0x12)), ) ); - }*/ + } } diff --git a/src/j1939/frame.rs b/src/j1939/frame.rs index 3f80741..d7b6658 100644 --- a/src/j1939/frame.rs +++ b/src/j1939/frame.rs @@ -1,4 +1,10 @@ -// Copyright 2023 Raven Industries inc. +/* +Copyright 2023 Raven Industries inc. + +@author Jannes Brands +@date 2024-02-22 +*/ + use crate::j1939::id::Id; use embedded_can::{Frame as EmbeddedFrame, Id as EmbeddedId}; diff --git a/src/j1939/id.rs b/src/j1939/id.rs index 57257f2..c69bd51 100644 --- a/src/j1939/id.rs +++ b/src/j1939/id.rs @@ -1,3 +1,9 @@ +/* +Copyright 2023 Raven Industries inc. + +@author Jannes Brands +@date 2024-02-22 +*/ use crate::j1939::standard_id::StandardId; use crate::j1939::{Address, ExtendedId, Pgn, Priority}; use bitvec::field::BitField; diff --git a/src/j1939/page.rs b/src/j1939/page.rs index 1a369fa..4731adf 100644 --- a/src/j1939/page.rs +++ b/src/j1939/page.rs @@ -1,3 +1,9 @@ +/* +Copyright 2023 Raven Industries inc. + +@author Jannes Brands +@date 2024-02-22 +*/ use bitvec::field::BitField; use bitvec::order::Msb0; use bitvec::prelude::BitVec; diff --git a/src/j1939/pdu_format.rs b/src/j1939/pdu_format.rs index 278b6cd..134c7dc 100644 --- a/src/j1939/pdu_format.rs +++ b/src/j1939/pdu_format.rs @@ -1,3 +1,9 @@ +/* +Copyright 2023 Raven Industries inc. + +@author Jannes Brands +@date 2024-02-22 +*/ use crate::j1939::byte_field::ByteField; /// PDU format field defined in the PGN diff --git a/src/j1939/pdu_specific.rs b/src/j1939/pdu_specific.rs index 1c7e0ed..c0e771b 100644 --- a/src/j1939/pdu_specific.rs +++ b/src/j1939/pdu_specific.rs @@ -1,3 +1,9 @@ +/* +Copyright 2023 Raven Industries inc. + +@author Jannes Brands +@date 2024-02-22 +*/ use crate::j1939::byte_field::ByteField; /// PDU specific field defined in the PGN diff --git a/src/j1939/pgn.rs b/src/j1939/pgn.rs index 2bc647f..b996d49 100644 --- a/src/j1939/pgn.rs +++ b/src/j1939/pgn.rs @@ -1,4 +1,10 @@ -// Copyright 2023 Raven Industries inc. +/* +Copyright 2023 Raven Industries inc. + +@author Jannes Brands +@date 2024-02-22 +*/ + use crate::j1939::byte_field::ByteField; use crate::j1939::page::Page; use crate::j1939::pdu_format::PduFormat; diff --git a/src/j1939/priority.rs b/src/j1939/priority.rs index a2a3874..0f7c42c 100644 --- a/src/j1939/priority.rs +++ b/src/j1939/priority.rs @@ -1,3 +1,9 @@ +/* +Copyright 2023 Raven Industries inc. + +@author Jannes Brands +@date 2024-02-22 +*/ use bitvec::field::BitField; use bitvec::order::Lsb0; use bitvec::vec::BitVec; diff --git a/src/j1939/standard_id.rs b/src/j1939/standard_id.rs index 3622ffb..f0d1325 100644 --- a/src/j1939/standard_id.rs +++ b/src/j1939/standard_id.rs @@ -1,3 +1,9 @@ +/* +Copyright 2023 Raven Industries inc. + +@author Jannes Brands +@date 2024-02-22 +*/ use crate::j1939::byte_field::ByteField; use crate::j1939::id::Id; use crate::j1939::{Address, Priority};