Skip to content

Commit 869d4ef

Browse files
committed
readSerialNumber using optional, added Test
1 parent e11ed53 commit 869d4ef

File tree

3 files changed

+87
-41
lines changed

3 files changed

+87
-41
lines changed

src/LinNodeConfig.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ bool LinNodeConfig::readProductId(uint8_t &NAD, uint16_t &supplierId, uint16_t &
133133

134134
/// @brief get Serial Number from specific node (optinal Function of Node)
135135
/// @details see LIN Spec 2.2A 4.2.1 LIN PRODUCT IDENTIFICATION
136-
/// @param NAD
137-
/// @param supplierId
138-
/// @param functionId
139-
/// @param serialNumber
140-
/// @return
141-
bool LinNodeConfig::readSerialNumber(uint8_t &NAD, uint16_t supplierId, uint16_t functionId, uint32_t &serialNumber)
136+
/// @param NAD
137+
/// @param supplierId
138+
/// @param functionId
139+
/// @param serialNumber
140+
/// @return Serial number or fail
141+
std::optional<uint32_t> LinNodeConfig::readSerialNumber(uint8_t &NAD, uint16_t supplierId, uint16_t functionId)
142142
{
143143
uint8_t SID = static_cast<uint8_t>(ServiceIdentifier::READ_BY_ID);
144144
std::vector<uint8_t> payload = {
@@ -153,7 +153,7 @@ bool LinNodeConfig::readSerialNumber(uint8_t &NAD, uint16_t supplierId, uint16_t
153153

154154
if (!checkPayload_isValid(SID, raw))
155155
{
156-
return false;
156+
return {};
157157
}
158158

159159
struct responseSid0ProductId
@@ -172,9 +172,9 @@ bool LinNodeConfig::readSerialNumber(uint8_t &NAD, uint16_t supplierId, uint16_t
172172
return false;
173173
*/
174174

175-
serialNumber = re.serialNumber_MSB << 24 | re.serialNumber_LSB3 << 16 | re.serialNumber_LSB2 << 8 | re.serialNumber_LSB;
176-
177-
return true;
175+
uint32_t serialNumber = re.serialNumber_MSB << 24 | re.serialNumber_LSB3 << 16 | re.serialNumber_LSB2 << 8 | re.serialNumber_LSB;
176+
177+
return serialNumber;
178178
}
179179

180180

src/LinNodeConfig.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ class LinNodeConfig : protected LinTransportLayer{
2929

3030
std::optional<std::vector<uint8_t>> readById(uint8_t &NAD, uint16_t supplierId, uint16_t functionId, uint8_t id);
3131
bool readProductId(uint8_t &NAD, uint16_t &supplierId, uint16_t &functionId, uint8_t &variantId);
32-
bool readSerialNumber(uint8_t &NAD, uint16_t supplierId, uint16_t functionId, uint32_t &serialNumber);
32+
std::optional<uint32_t> readSerialNumber(uint8_t &NAD, uint16_t supplierId, uint16_t functionId);
3333

3434
bool assignNAD(uint8_t &NAD, uint16_t supplierId, uint16_t functionId, uint8_t newNAD);
3535
bool conditionalChangeNAD(uint8_t &NAD, uint8_t id, uint8_t byte, uint8_t invert, uint8_t mask, uint8_t newNAD);
3636

3737
// not implemented:
38-
// std::vector<uint8_t> LinNodeConfig::dataDump(uint8_t* NAD, std::vector<uint8_t>)
38+
// std::optional<std::vector<uint8_t>> LinNodeConfig::dataDump(uint8_t* NAD, std::vector<uint8_t>)
3939

4040
bool saveConfig(uint8_t &NAD);
4141

test/native/test_LinNodeConfig/test_LinNodeConfig.cpp

+75-29
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void test_lin_sleep() {
6464
TEST_ASSERT_EQUAL_MEMORY(bus_transmitted.data(), linDriver->txBuffer.data(), bus_transmitted.size());
6565
}
6666

67+
/// @brief Node Identification: Read by identifier (Spec LIN 2.2A, Page 78, Chap. 4.2.6.1)
6768
void test_lin_getID() {
6869
std::cout << "\n\n\nRunning test: " << __FUNCTION__ << std::endl;
6970

@@ -72,15 +73,16 @@ void test_lin_getID() {
7273
constexpr uint16_t request_FunctionId = 0x3FFF; // Wildcard
7374

7475
std::vector<uint8_t> bus_transmitted = {
75-
0x00, 0x55, 0x3C, // Master Request
76+
// Master
77+
0x00, 0x55, 0x3C, // Frame: Master Request
7678
request_NAD, // NAD
77-
0x06, // 6 Byte
78-
0xB2, // SID
79-
0x00, // Cmd Identifier
79+
0x06, // PCI: Single frame, 6 Byte
80+
0xB2, // SID: Read by ID
81+
0x00, // Identifier: Lin Product Identification
8082
lowerByte(request_SupplierId), upperByte(request_SupplierId), // Supplier ID LSB, MSB
8183
lowerByte(request_FunctionId), upperByte(request_FunctionId), // Function ID LSB, MSB
82-
0x09, // chksum
83-
84+
0x09, // Frame: Checksum
85+
// Slave
8486
0x00, 0x55, 0x7d // Request for Slave Response
8587
};
8688

@@ -89,32 +91,25 @@ void test_lin_getID() {
8991
constexpr uint16_t response_supplierId = 0x2E06;
9092
constexpr uint16_t response_functionId = 0x1080;
9193
constexpr uint8_t response_variant = 0x56;
92-
struct Response {
93-
std::vector<uint8_t> head {
94-
response_NAD, // NAD
95-
0x06 // Single Frame, 6 Bytes
96-
};
97-
std::vector<uint8_t> payload {
98-
0xF2, // RSID
99-
lowerByte(response_supplierId), upperByte(response_supplierId), // supplier ID, LSB MSB
100-
lowerByte(response_functionId), upperByte(response_functionId), // function ID, LSB MSB
101-
response_variant // variant
102-
};
103-
uint8_t checksum {
104-
0xe1 // Frame Checksum
105-
};
106-
} response;
107-
linDriver->mock_Input(response.head);
108-
linDriver->mock_Input(response.payload);
109-
// no fillbytes required
110-
linDriver->mock_Input(response.checksum);
94+
std::vector<uint8_t> response {
95+
response_NAD, // NAD
96+
0x06, // PCI: Single Frame, 6 Bytes
97+
0xF2, // RSID
98+
lowerByte(response_supplierId), upperByte(response_supplierId), // supplier ID, LSB MSB
99+
lowerByte(response_functionId), upperByte(response_functionId), // function ID, LSB MSB
100+
response_variant, // variant
101+
0xE1 // Frame Checksum
102+
};
103+
linDriver->mock_Input(response);
111104

112105
uint8_t NAD = request_NAD;
113106
uint16_t supplierId = request_SupplierId;
114107
uint16_t functionId = request_FunctionId;
115108
uint8_t variant = 0;
116109
bool result = linNodeConfig->readProductId(NAD, supplierId, functionId, variant);
117110

111+
TEST_ASSERT_TRUE(result);
112+
118113
TEST_ASSERT_EQUAL(response_NAD, NAD);
119114
TEST_ASSERT_EQUAL(response_supplierId, supplierId);
120115
TEST_ASSERT_EQUAL(response_functionId, functionId);
@@ -124,6 +119,56 @@ void test_lin_getID() {
124119
TEST_ASSERT_EQUAL_MEMORY(bus_transmitted.data(), linDriver->txBuffer.data(), bus_transmitted.size());
125120
}
126121

122+
/// @brief Node Identification: Read by identifier (Spec LIN 2.2A, Page 78, Chap. 4.2.6.1)
123+
void test_lin_serialNumber()
124+
{
125+
std::cout << "\n\n\nRunning test: " << __FUNCTION__ << std::endl;
126+
127+
constexpr uint8_t request_NAD = 0x7F; // wildcard
128+
constexpr uint16_t request_SupplierId = 0x7FFF; // wildcard
129+
constexpr uint16_t request_FunctionId = 0x3FFF; // wildcard
130+
131+
std::vector<uint8_t> bus_transmitted = {
132+
// Master
133+
0x00, 0x55, 0x3C, // Frame Head: Master Request
134+
request_NAD, // NAD Wildcard
135+
0x06, // PID: Single Frame, 6 Byte
136+
0xB2, // SID: Read by identifier
137+
0x00, // Identifier: Serial number
138+
lowerByte(request_SupplierId), upperByte(request_SupplierId), // Supplier ID LSB, MSB
139+
lowerByte(request_FunctionId), upperByte(request_FunctionId), // Function ID LSB, MSB
140+
0x09, // Frame: Checksum
141+
142+
0x00, 0x55, 0x7D // Frame Head: Slave Response
143+
};
144+
145+
// Slave Response
146+
constexpr uint8_t response_NAD = 0x0A;
147+
constexpr uint32_t response_SN = 0x76543210;
148+
std::vector<uint8_t> response {
149+
response_NAD, // <-- according to spec initial NAD will be used
150+
0x05, // PCI: Single Frame, 5 Byte
151+
0xF2, // RSID
152+
0x10, 0x32, 0x54, 0x76, // Srial Number LSB...MSB
153+
0xFF, // unused
154+
0xF0 // Frame: Checksum
155+
};
156+
linDriver->mock_Input(response);
157+
158+
uint8_t NAD = request_NAD;
159+
uint16_t supplierId = request_SupplierId;
160+
uint16_t functionId = request_FunctionId;
161+
auto result = linNodeConfig->readSerialNumber(NAD, supplierId, functionId);
162+
163+
TEST_ASSERT_EQUAL(response_NAD, NAD); // <-- answer will follow on old NAD
164+
165+
TEST_ASSERT_TRUE(result);
166+
TEST_ASSERT_EQUAL_UINT32(response_SN, result.value());
167+
168+
TEST_ASSERT_EQUAL(bus_transmitted.size(), linDriver->txBuffer.size());
169+
TEST_ASSERT_EQUAL_MEMORY(bus_transmitted.data(), linDriver->txBuffer.data(), bus_transmitted.size());
170+
}
171+
127172
/// @brief Node Configuration Service: Assign NAD (Spec LIN 2.2A, Page 74, Chap. 4.2.5.1)
128173
void test_lin_assignNAD_ok()
129174
{
@@ -151,7 +196,7 @@ void test_lin_assignNAD_ok()
151196
// Slave Response
152197
std::vector<uint8_t> response {
153198
request_NAD, // initial NAD <-- according to spec initial NAD will be used
154-
0x01, // PCI: Single Frame, 1 Bytes
199+
0x01, // PCI: Single Frame, 1 Byte
155200
0xF0, // RSID
156201
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 5x unused
157202
0x8E // Frame Checksum
@@ -200,7 +245,7 @@ void test_lin_conditionalChangeNAD_ok()
200245
// Slave Response
201246
std::vector<uint8_t> response {
202247
request_NAD_new, // NAD <-- according to spec: new NAD will be used
203-
0x01, // PCI: Single Frame, 1 Bytes
248+
0x01, // PCI: Single Frame, 1 Byte
204249
0xF3, // RSID
205250
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 5x unused
206251
0xEF // Frame Checksum
@@ -240,7 +285,7 @@ void test_lin_saveConfig_ok()
240285
// Slave Response
241286
std::vector<uint8_t> response {
242287
request_NAD, // NAD
243-
0x01, // Single Frame, 1 Bytes
288+
0x01, // Single Frame, 1 Byte
244289
0xF6, // RSID = Save Config
245290
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 5x unused
246291
0xA1 // Frame Checksum
@@ -289,7 +334,7 @@ void test_lin_AssignFrameIdRange_ok()
289334
// Slave Response
290335
std::vector<uint8_t> response {
291336
request_NAD, // NAD
292-
0x01, // PID: Single Frame, 1 Bytes
337+
0x01, // PID: Single Frame, 1 Byte
293338
0xF7, // RSID: Assign Frame ID Range
294339
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 5x unused
295340
0xA0 // Frame Checksum
@@ -313,6 +358,7 @@ int main() {
313358
RUN_TEST(test_lin_wakeup);
314359
RUN_TEST(test_lin_sleep);
315360
RUN_TEST(test_lin_getID);
361+
RUN_TEST(test_lin_serialNumber);
316362
RUN_TEST(test_lin_assignNAD_ok);
317363
RUN_TEST(test_lin_conditionalChangeNAD_ok);
318364
RUN_TEST(test_lin_saveConfig_ok);

0 commit comments

Comments
 (0)