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
/// A J1939 CAN message containing header information and payload data.
///
/// This structure represents a complete J1939 message with all necessary
/// fields for communication on a J1939 network. J1939 is a vehicle bus
/// standard used in commercial vehicles and heavy-duty equipment.
///
/// # Fields
///
/// * `priority` - Message priority (0-7, where 0 is highest priority)
/// * `pgn` - Parameter Group Number identifying the message type
/// * `source_address` - Address of the sending device (0-253)
/// * `data` - Message payload data (up to 223 bytes for multi-packet messages)
/// * `length` - Actual number of bytes used in the data field (0-223)
///
/// # Examples
///
/// ```
/// use j1939_core::message::J1939Message;
///
/// // Create a new message using default
/// let mut msg = J1939Message::default();
/// msg.priority = 3;
/// msg.pgn = 0xF004; // Engine temperature
/// msg.source_address = 0x80; // Engine ECU
/// msg.data[0] = 75; // Temperature value
/// msg.length = 1;
///
/// // Create a message with specific data
/// let mut msg = J1939Message::default();
/// msg.priority = 6;
/// msg.pgn = 0xFECA;
/// msg.source_address = 0x17;
/// msg.data[0] = 0x12;
/// msg.data[1] = 0x34;
/// msg.data[2] = 0x56;
/// msg.data[3] = 0x78;
/// msg.length = 4;
/// ```
/// Trait for encoding structured data into J1939 message format.
///
/// Implement this trait to convert your data structures into J1939 messages
/// that can be transmitted over the network. The trait provides a standard
/// interface for serializing data into the message payload.
///
/// # Examples
///
/// ```
/// use j1939_core::message::{J1939Message, Marshall};
///
/// struct EngineData {
/// temperature: u8,
/// rpm: u16,
/// }
///
/// impl Marshall for EngineData {
/// fn marshall(&self, msg: &mut J1939Message) -> j1939_core::Result<()> {
/// msg.pgn = 0xF004; // Engine temperature PGN
/// msg.data[0] = self.temperature;
/// msg.data[1] = (self.rpm & 0xFF) as u8;
/// msg.data[2] = ((self.rpm >> 8) & 0xFF) as u8;
/// msg.length = 3;
/// Ok(())
/// }
/// }
/// ```
/// Trait for decoding J1939 messages into structured data.
///
/// Implement this trait to convert received J1939 messages back into your
/// data structures. The trait provides a standard interface for deserializing
/// message payloads into typed data.
///
/// # Examples
///
/// ```
/// use j1939_core::message::{J1939Message, Unmarshall};
///
/// struct EngineData {
/// temperature: u8,
/// rpm: u16,
/// }
///
/// impl Unmarshall for EngineData {
/// fn unmarshall(msg: &J1939Message) -> j1939_core::Result<Self> {
/// if msg.length < 3 {
/// return Err(j1939_core::Error::InvalidLength);
/// }
///
/// Ok(EngineData {
/// temperature: msg.data[0],
/// rpm: msg.data[1] as u16 | ((msg.data[2] as u16) << 8),
/// })
/// }
/// }
/// ```