1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use std::fmt::format;
use bitfield_struct::bitfield;
use crate::Conversion;
use crate::j1939::{DestinationAddress, J1939, J1939Id};

/// Represents the assignment typeof a Protocol Data Unit (PDU).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PduAssignment {
    /// Society of Automotive Engineers (SAE) assigned PDU.
    /// Contains the PDU value.
    Sae(u32),
    /// Manufacturer/proprietary assigned PDU.
    /// Contains the PDU value.
    Manufacturer(u32),

    /// Unknown or unrecognized PDU assignment.
    /// Contains the PDU value.
    Unknown(u32),
}

/// Represents the format of a Protocol Data Unit (PDU).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PduFormat {
    /// PDU format 1.
    /// Contains PDU format value.
    Pdu1(u8),
    /// PDU format 2.
    /// Contains PDU format value.
    Pdu2(u8),
}

/// Represents the communication mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommunicationMode {
    /// Point-to-Point communication mode.
    /// This PDU communication variant may contain a destination address.
    P2P,
    /// Broadcast communication mode.
    Broadcast,
}

/// Represents the group extension.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GroupExtension {
    /// No group extension.
    None,
    /// Group extension with a specific value.
    Some(u8),
}

/// Bitfield representation of 18-bit Parameter Group Number (PGN).
///
/// ### Repr: `u32`
///
/// | Field                  | Size (bits) |
/// |------------------------|-------------|
/// | Padding bits (private) | 14          |
/// | Reserved bits          | 1           |
/// | Data page bits         | 1           |
/// | PDU format bits        | 8           |
/// | PDU specific bits      | 8           |
#[bitfield(u32, order = Msb)]
#[derive(PartialEq, Eq)]
pub struct Pgn {
    #[bits(14)]
    _padding_bits: u16,
    #[bits(1)]
    reserved_bits: bool,
    #[bits(1)]
    data_page_bits: bool,
    #[bits(8)]
    pdu_format_bits: u8,
    #[bits(8)]
    pdu_specific_bits: u8,
}

impl Conversion for Pgn {
    type Type = u32;

    /// Creates a new [`Pgn`] bitfield from a 32-bit integer.
    #[inline]
    fn from_bits(bits: u32) -> Self {
        Self(bits)
    }

    /// Creates a new [`Pgn`] bitfield from a base-16 (hex) string slice.
    #[inline]
    fn from_hex(hex_str: &str) -> Self {
        let bits = u32::from_str_radix(hex_str, 16).unwrap_or_default();

        Self(bits)
    }

    /// Creates a new [`Pgn`] bitfield from a 32-bit integer.
    #[inline]
    fn try_from_bits(bits: u32) -> Option<Self> {
        match bits {
            0..=0x3FFFF => Some(Self(bits)),
            _ => None,
        }
    }

    /// Creates a new [`Pgn`] bitfield from a base-16 (hex) string slice.
    #[inline]
    fn try_from_hex(hex_str: &str) -> Option<Self> {
        match u32::from_str_radix(hex_str, 16) {
            Ok(v) => Self::try_from_bits(v),
            Err(_) => None,
        }
    }

    /// Creates a new 32-bit integer from the [`Pgn`] bitfield.
    #[inline]
    fn into_bits(self) -> u32 {
        self.into_bits()
    }

    /// Creates a new base-16 (hex) `String` from the [`Pgn`] bitfield.
    #[inline]
    fn into_hex(self) -> String {
        format(format_args!("{:05X}", self.into_bits()))
    }
}

impl Pgn {
    /// Returns the PDU format based on the parsed bits.
    ///
    /// # Returns
    /// - `PduFormat::Pdu1(bits)` if the PDU format value is less than 240.
    /// - `PduFormat::Pdu2(bits)` otherwise.
    #[must_use]
    pub const fn pdu_format(&self) -> PduFormat {
        match (self.pdu_format_bits() < 240, self.pdu_format_bits()) {
            (true, a) => PduFormat::Pdu1(a),
            (false, b) => PduFormat::Pdu2(b),
        }
    }

    /// Returns the group extension based on the parsed bits.
    ///
    /// # Returns
    /// - `GroupExtension::None` if the PDU format is `Pdu1`.
    /// - `GroupExtension::Some(bits)` if the PDU format is `Pdu2`.
    #[must_use]
    pub const fn group_extension(&self) -> GroupExtension {
        match self.pdu_format() {
            PduFormat::Pdu1(_) => GroupExtension::None,
            PduFormat::Pdu2(_) => GroupExtension::Some(self.pdu_specific_bits()),
        }
    }

    /// Returns the destination address based on the parsed PDU format.
    ///
    /// # Returns
    /// - `DestinationAddress::Some(bits)` if the PDU format is `Pdu1`.
    /// - `DestinationAddress::None` if the PDU format is `Pdu2`.
    #[must_use]
    pub const fn destination_address(&self) -> DestinationAddress {
        match self.pdu_format() {
            PduFormat::Pdu1(_) => DestinationAddress::Some(self.pdu_specific_bits()),
            PduFormat::Pdu2(_) => DestinationAddress::None,
        }
    }

    /// Returns the communication mode based on the parsed PDU format.
    ///
    /// # Returns
    /// - `CommunicationMode::P2P` if the PDU format is `Pdu1`.
    /// - `CommunicationMode::Broadcast` if the PDU format is `Pdu2`.
    #[must_use]
    pub const fn communication_mode(&self) -> CommunicationMode {
        match self.pdu_format() {
            PduFormat::Pdu1(_) => CommunicationMode::P2P,
            PduFormat::Pdu2(_) => CommunicationMode::Broadcast,
        }
    }

    /// Checks if the communication mode is point-to-point (P2P).
    ///
    /// # Returns
    /// - `true` if the communication mode is `P2P`.
    /// - `false` if the communication mode is `Broadcast`.
    #[must_use]
    pub const fn is_p2p(&self) -> bool {
        match self.communication_mode() {
            CommunicationMode::P2P => true,
            CommunicationMode::Broadcast => false,
        }
    }

    /// Checks if the communication mode is broadcast.
    ///
    /// # Returns
    /// - `true` if the communication mode is `Broadcast`.
    /// - `false` if the communication mode is `P2P`.
    #[must_use]
    pub const fn is_broadcast(&self) -> bool {
        match self.communication_mode() {
            CommunicationMode::P2P => false,
            CommunicationMode::Broadcast => true,
        }
    }

    /// Determines the PDU assignment based on the parsed bits.
    ///
    /// # Returns
    /// - `PduAssignment::Sae(bits)` for known SAE-defined PDU assignments.
    /// - `PduAssignment::Manufacturer(bits)` for manufacturer-defined PDU assignments.
    /// - `PduAssignment::Unknown(bits)` for unrecognized PDU assignments.
    #[must_use]
    pub fn pdu_assignment(&self) -> PduAssignment {
        match self.into_bits() {
            0x0000_0000..=0x0000_EE00
            | 0x0000_F000..=0x0000_FEFF
            | 0x0001_0000..=0x0001_EE00
            | 0x0001_F000..=0x0001_FEFF => PduAssignment::Sae(self.into_bits()),

            0x0000_EF00 | 0x0000_FF00..=0x0000_FFFF | 0x0001_EF00 | 0x0001_FF00..=0x0001_FFFF => {
                PduAssignment::Manufacturer(self.into_bits())
            }
            p => PduAssignment::Unknown(p),
        }
    }
}

impl J1939Id {
    /// Computes the PGN bitfield value based on the 29-bit identifier fields.
    ///
    /// # Returns
    /// The combined PGN bitfield value.
    #[must_use]
    pub fn pgn_bits(&self) -> u32 {
        let pgn_bitfield = Pgn::new()
            .with_data_page_bits(self.data_page())
            .with_pdu_format_bits(self.pdu_format())
            .with_pdu_specific_bits(self.pdu_specific());

        pgn_bitfield.into_bits()
    }

    /// Constructs and returns a [`Pgn`] struct based on the 29-bit identifier fields.
    ///
    /// # Returns
    /// A [`Pgn`] bitfield initialized with the 29-bit identifier fields.
    #[must_use]
    pub fn pgn(&self) -> Pgn {
        Pgn::new()
            .with_data_page_bits(self.data_page())
            .with_pdu_format_bits(self.pdu_format())
            .with_pdu_specific_bits(self.pdu_specific())
    }
}