-
Notifications
You must be signed in to change notification settings - Fork 0
/
CANSAEJ1939.h
233 lines (209 loc) · 7.04 KB
/
CANSAEJ1939.h
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
/**
* @file CANSAEJ1939.h
* @author Ryan Johnson ([email protected])
* @brief Implements basic features of the SAEJ1939 Can2.0B Spec
* @version 0.1
* @date 2020-05-26
*
* @copyright Copyright (c) 2020
*
*/
#ifndef CAN_SAEJ1939_Layer_H
#define CAN_SAEJ1939_Layer_H
#include "CANInterface.h"
namespace CAN {
/**
* @brief The J1939ID Class builds and parses 29-bit can IDs.
*
* A SAEJ1939 CAN ID uses the extended identifiers in CAN2.0
* This is formatted as follows:
* MSB: 3 Bits - Priority
* 18 Bits - PGN (Parameter Group Number)
* LSB: 8 Bits - Source Address
*/
class J1939ID {
public:
/**
* @brief Construct a new empty J1939ID object
*/
J1939ID() : canID(0), priority(0), sourceAddress(0), PGN(0){};
/**
* @brief Construct a new J1939ID object from Priority, PGN, Source Address
*
* @param priority the CAN message priority
* @param sourceAddress the source address of the packet
* @param PGN the Parameter Group Number of the message
*/
J1939ID(byte priority, byte sourceAddress, unsigned long PGN)
: canID(((unsigned long)priority << 26) + (PGN << 8) +
(unsigned long)sourceAddress),
priority(priority), sourceAddress(sourceAddress), PGN(PGN){};
/**
* @brief Construct a new J1939ID object by parsing an existing ID
*
* @param ID the existing 29-bit can ID in a 32 bit container
*/
J1939ID(unsigned long ID)
: canID(ID), PGN((ID & (long)0xFFFF00) >> 8),
priority((ID >> 26) & 0b111), sourceAddress(ID & 0xFF){};
/**
* @brief Get the CAN ID represented by this object
*
* @return unsigned long the 29-but CAN ID represented by this object, with
* zero padding in front.
*/
unsigned long getID() { return canID; };
/**
* @brief Get the Priority
*
* @return byte priority byte (only valid to 3 bits)
*/
byte getPriority() { return priority; };
/**
* @brief Get the Source Address
*
* @return byte the source address
*/
byte getSourceAddress() { return sourceAddress; };
/**
* @brief Get the PGN
*
* @return unsigned long the Parameter Group Number of this ID
*/
unsigned long getPGN() { return PGN; };
private:
unsigned long canID;
byte priority;
byte sourceAddress;
unsigned long PGN;
};
/**
* @brief This structure represents a SAEJ1939 Message comprised of a J1939ID
* Object and 8 bytes of data
*
*/
struct J1939Message {
//! The CanID of this packet
J1939ID CanID;
//! 8 bytes of data, zero-filled initially
byte data[8] = {0, 0, 0, 0, 0, 0, 0, 0};
/**
* @brief Construct a new J1939Message object representing a command message
* with one byte of data in the payload
*
* The first byte of this message is actually the desination address. The
* following byte is data 1
*
* @param source source CAN address of the message
* @param dest destination CAN address of the message
* @param PGN parameter group number of the message
* @param data1 the one byte of data (leftmost byte) to send, filling the
* rest of the bytes with zeros
*/
J1939Message(byte source, byte dest, unsigned long PGN, byte data1)
{
data[0] = dest;
data[1] = data1;
CanID = J1939ID(6, source, PGN);
}
/**
* @brief Construct a new J1939Message object representing a command message
* with two bytes of data in the payload
*
* NOTE: The first byte of this message is actually the desination address.
* The following byte is data 1
*
* @param source source CAN address of the message
* @param dest destination CAN address of the message
* @param PGN parameter group number of the message
* @param data1 the first byte of data (leftmost byte) to send, filling the
* rest of the bytes with zeros
* @param data2 the second byte of data (second-to-leftmost byte) to send,
* filling the rest of the bytes with zeros
*/
J1939Message(byte source, byte dest, unsigned long PGN, byte data1,
byte data2)
{
data[0] = dest;
data[1] = data1;
data[2] = data2;
CanID = J1939ID(6, source, PGN);
}
/**
* @brief Construct a new J1939Message object representing a command message
* with three bytes of data in the payload
*
* @param source source CAN address of the message
* @param dest destination CAN address of the message
* @param PGN parameter group number of the message
* @param data1 the first byte of data (leftmost byte) to send, filling the
* rest of the bytes with zeros
* @param data2 the second byte of data (second-to-leftmost byte) to send,
* filling the rest of the bytes with zeros
* @param data3 the second byte of data (third-to-leftmost byte) to send,
* filling the rest of the bytes with zeros
*/
J1939Message(byte source, byte dest, unsigned long PGN, byte data1,
byte data2, byte data3)
{
data[0] = data1;
data[1] = data2;
data[2] = data3;
CanID = J1939ID(6, source, PGN);
}
/**
* @brief Construct a new J1939Message object by parsing a CAN2.0B extended
* packet
*
* @param p the packet to parse
*/
J1939Message(ExtendedCanDataPacket p)
{
unsigned long id = p.id;
memcpy(&data, &p.data, 8);
CanID = J1939ID(id);
}
}; // namespace CAN
/**
* @brief This interface allows writing to the Serial Can Module using the
* SAEJ1939 CAN 2.0 Protocol stack
*
*/
class J1939Interface : public RawInterface {
public:
/**
* @brief Construct a new J1939Interface object.
*
* @param serialInterface the HardwareSerial device to which the CAN Bus
* module is connected
*/
J1939Interface(HardwareSerial &serialInterface)
: RawInterface(serialInterface){};
/**
* @brief Write a J1939 Message to the CAN Bus.
*
* @param p the J1939 Message to send
* @param dest the address to send the packet to. The destination address is
* used to determine if the packet is peer-to-peer.
*/
void write(J1939Message p, byte dest);
/**
* @brief Read a J1939 Message from the CAN Bus
*
* @return J1939Message that was read. If no message was read, the message
* will be initialized empty (i.e. CAN ID is 0, data is 0)
*/
J1939Message read();
/**
* @see CAN::RawInterface::write(ExtendedCanDataPacket p)
*/
void writeRaw(ExtendedCanDataPacket &p) { return RawInterface::write(p); };
/**
* @see CAN::RawInterface::read(ExtendedCanDataPacket p)
*/
bool readRaw(ExtendedCanDataPacket &p) { return RawInterface::read(p); };
private:
bool j1939PeerToPeer(unsigned long lPGN);
};
} // namespace CAN
#endif