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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use std::fmt::format;
use bitfield_struct::bitfield;
use crate::Conversion;

/// Bitfield representing an 8-byte data field.
///
/// ### Repr `u64`
///
/// | Field            | Size (bits) |
/// |------------------|-------------|
/// | byte 0           | 8           |
/// | byte 1           | 8           |
/// | byte 2           | 8           |
/// | byte 3           | 8           |
/// | byte 4           | 8           |
/// | byte 5           | 8           |
/// | byte 6           | 8           |
/// | byte 7           | 8           |
#[bitfield(u64, order = Msb)]
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct DataField {
    #[bits(8)]
    byte_0_bits: u8,
    #[bits(8)]
    byte_1_bits: u8,
    #[bits(8)]
    byte_2_bits: u8,
    #[bits(8)]
    byte_3_bits: u8,
    #[bits(8)]
    byte_4_bits: u8,
    #[bits(8)]
    byte_5_bits: u8,
    #[bits(8)]
    byte_6_bits: u8,
    #[bits(8)]
    byte_7_bits: u8,
}

impl Conversion for DataField {
    type Type = u64;

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

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

        Self(bits)
    }

    /// Creates a new [`DataField`] bitfield from a 64-bit integer.
    #[inline]
    fn try_from_bits(bits: u64) -> Option<Self> {
        Some(Self(bits))
    }

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

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

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

macro_rules! field_x {
    ($($num:tt),*) => {
        paste::paste! {
            $(
                #[inline]
                pub const fn [<byte_ $num>](&self) -> u8 {
                    self.[<byte_ $num _bits>]()
                }
            )*
        }
    };
}

impl DataField {
    field_x!(0, 1, 2, 3, 4, 5, 6, 7);

    /// Return the 64-bit [`DataField`] bitfield as little-endian bytes.
    #[must_use]
    pub const fn to_le_bytes(&self) -> [u8; 8] {
        self.into_bits().to_le_bytes()
    }

    /// Return the 64-bit [`DataField`] bitfield as big-endian bytes.
    #[must_use]
    pub const fn to_be_bytes(&self) -> [u8; 8] {
        self.into_bits().to_be_bytes()
    }

    /// Return the 64-bit [`DataField`] bitfield as native-endian bytes.
    #[must_use]
    pub const fn to_ne_bytes(&self) -> [u8; 8] {
        self.into_bits().to_ne_bytes()
    }

    /// Convert the [`DataField`] bitfield to little-endian byte format.
    #[must_use]
    pub const fn to_le(&self) -> Self {
        Self(self.into_bits().to_le())
    }

    /// Convert the [`DataField`] bitfield to big-endian byte format.
    #[must_use]
    pub const fn to_be(&self) -> Self {
        Self(self.into_bits().to_be())
    }
}

/// Represents a Name in the SAE J1939 protocol.
///
/// The Name structure is used in the SAE J1939 protocol to represent the identity of a device or
/// component within a vehicle's network.
///
/// ### Repr: `u64`
/// | Field                             | Size (bits) |
/// |-----------------------------------|-------------|
/// | Arbitrary address bits            | 1           |
/// | Industry group bits               | 3           |
/// | Vehicle system instance bits      | 4           |
/// | Vehicle system bits               | 7           |
/// | Reserved bits                     | 1           |
/// | Function bits                     | 8           |
/// | Function instance bits            | 5           |
/// | ECU instance bits                 | 3           |
/// | Manufacturer code bits            | 11          |
/// | Identity number bits              | 21          |
#[bitfield(u64, order = Msb)]
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct NameField {
    #[bits(1)]
    arbitrary_address_bits: bool,
    #[bits(3)]
    industry_group_bits: u8,
    #[bits(4)]
    vehicle_system_instance_bits: u8,
    #[bits(7)]
    vehicle_system_bits: u8,
    #[bits(1)]
    reserved_bits: bool,
    #[bits(8)]
    function_bits: u8,
    #[bits(5)]
    function_instance_bits: u8,
    #[bits(3)]
    ecu_instance_bits: u8,
    #[bits(11)]
    manufacturer_code_bits: u16,
    #[bits(21)]
    identity_number_bits: u32,
}

impl Conversion for NameField {
    type Type = u64;

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

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

        Self(bits)
    }

    /// Creates a new [`NameField`] bitfield from a 64-bit integer.
    #[inline]
    fn try_from_bits(bits: u64) -> Option<Self> {
        Some(Self(bits))
    }

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

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

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

impl NameField {

    /// Indicates whether the ECU/CA can negotiate an address (true = yes; false = no).
    #[must_use]
    pub const fn arbitrary_address(&self) -> bool {
        self.arbitrary_address_bits()
    }

    /// These codes are associated with particular industries such as on-highway equipment,
    /// agricultural equipment, and more.
    #[must_use]
    pub const fn industry_group(&self) -> u8 {
        self.industry_group_bits()
    }

    /// Assigns a number to each instance on the Vehicle System (in case you connect several
    /// networks – e.g. connecting cars on a train).
    #[must_use]
    pub const fn vehicle_system_instance(&self) -> u8 {
        self.vehicle_system_instance_bits()
    }

    /// Vehicle systems are associated with the Industry Group and they can be, for instance,
    /// “tractor” in the “Common” industry or “trailer” in the “On-Highway” industry group.
    #[must_use]
    pub const fn vehicle_system(&self) -> u8 {
        self.vehicle_system_bits()
    }

    /// Always zero(false).
    #[must_use]
    pub const fn reserved(&self) -> bool {
        self.reserved_bits()
    }

    /// This code, in a range between 128 and 255, is assigned according to the Industry Group. A
    /// value between 0 and 127 is not associated with any other parameter.
    #[must_use]
    pub const fn function(&self) -> u8 {
        self.function_bits()
    }

    /// Returns the function instance.
    #[must_use]
    pub const fn function_instance(&self) -> u8 {
        self.function_instance_bits()
    }

    /// A J1939 network may accommodate several ECUs of the same kind (i.e. same functionality).
    /// The Instance code separates them.
    #[must_use]
    pub const fn ecu_instance(&self) -> u8 {
        self.ecu_instance_bits()
    }

    /// The 11-Bit Manufacturer Code is assigned by the SAE.
    #[must_use]
    pub const fn manufacturer_code(&self) -> u16 {
        self.manufacturer_code_bits()
    }

    /// This field is assigned by the manufacturer, similar to a serial number, i.e. the code must
    /// be uniquely assigned to the unit.
    #[must_use]
    pub const fn identity_number(&self) -> u32 {
        self.identity_number_bits()
    }
}

/// Represents a Protocol Data Unit (PDU) in the context of Controller Area Network (CAN).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Pdu {
    NameField(NameField),
    DataFiled(DataField),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum PduType {
    Name,
    Data,
}

#[cfg(test)]
mod data_tests {
    use super::*;

    #[test]
    fn test_data_bitfield() -> Result<(), anyhow::Error> {
        let data_a = DataField::from_hex("FFFF82DF1AFFFFFF");
        let be_bytes_a: [u8; 8] = [0xFF, 0xFF, 0x82, 0xDF, 0x1A, 0xFF, 0xFF, 0xFF];
        let le_bytes_a: [u8; 8] = [0xFF, 0xFF, 0xFF, 0x1A, 0xDF, 0x82, 0xFF, 0xFF];

        assert_eq!(data_a.byte_2(), 0x82);

        assert_eq!(be_bytes_a, data_a.to_be_bytes());
        assert_eq!(le_bytes_a, data_a.to_le_bytes());

        assert_eq!(18446606493475143679, data_a.into_bits());

        assert_eq!(
            DataField(18446743089616977919),
            data_a.to_be()
        );
        assert_eq!(
            DataField(18446606493475143679),
            data_a.to_le()
        );

        Ok(())
    }

    #[test]
    fn test_name_bitfield() {
        let name_a = NameField::new()
            .with_arbitrary_address_bits(true)
            .with_industry_group_bits(0)
            .with_vehicle_system_instance_bits(0x5)
            .with_vehicle_system_bits(0x6)
            .with_reserved_bits(false)
            .with_function_bits(0x5)
            .with_function_instance_bits(0x2)
            .with_ecu_instance_bits(0x1)
            .with_manufacturer_code_bits(0x122)
            .with_identity_number_bits(0xB0309);

        let bytes_a: [u8; 8] = [0x09, 0x03, 0x4B, 0x24, 0x11, 0x05, 0x0C, 0x85];
        let name_a_bytes = name_a.into_bits().to_le_bytes();

        assert_eq!(bytes_a, name_a_bytes);
    }
}