@@ -92,6 +92,43 @@ void test_ok_readresponse() {
92
92
TEST_ASSERT_EQUAL_UINT8_ARRAY (data, parser.packet ().data (), 2 );
93
93
}
94
94
95
+ void test_nok_readresponse () {
96
+ const uint8_t stream[] = {
97
+ 0x41 , // start byte
98
+ 0x06 , // length
99
+ 0x03 , // packet type (error)
100
+ 0x01 , // flags: id + function code (0 + read)
101
+ 0x77 , // address 1
102
+ 0x60 , // address 2
103
+ 0x01 , // payload length
104
+ 0x01 , // payload
105
+ 0xe3 // cs
106
+ };
107
+ const std::size_t length = 9 ;
108
+ const std::size_t packetLength = 7 ;
109
+ const uint8_t data[1 ] = {0x01 };
110
+
111
+ std::size_t bytesRead = 0 ;
112
+ ParserResult result = ParserResult::ERROR;
113
+
114
+ while (bytesRead < length) {
115
+ result = parser.parse (stream[bytesRead++]);
116
+ if (result != ParserResult::CONTINUE) {
117
+ break ;
118
+ }
119
+ }
120
+
121
+ TEST_ASSERT_EQUAL (ParserResult::COMPLETE, result);
122
+ TEST_ASSERT_EQUAL_UINT (length, bytesRead);
123
+ TEST_ASSERT_EQUAL_UINT8 (packetLength, parser.packet ().length ());
124
+ TEST_ASSERT_EQUAL_UINT8 (PacketType::ERROR, parser.packet ().packetType ());
125
+ TEST_ASSERT_EQUAL_UINT8 (0x00 , parser.packet ().id ());
126
+ TEST_ASSERT_EQUAL_UINT8 (FunctionCode::READ, parser.packet ().functionCode ());
127
+ TEST_ASSERT_EQUAL_UINT16 (0x7760 , parser.packet ().address ());
128
+ TEST_ASSERT_EQUAL_UINT8 (0x01 , parser.packet ().dataLength ());
129
+ TEST_ASSERT_EQUAL_UINT8_ARRAY (data, parser.packet ().data (), 1 );
130
+ }
131
+
95
132
void test_ok_writeresponse () {
96
133
const uint8_t stream[] = {
97
134
0x41 , // start byte
@@ -287,6 +324,7 @@ int main() {
287
324
UNITY_BEGIN ();
288
325
RUN_TEST (test_ok_request);
289
326
RUN_TEST (test_ok_readresponse);
327
+ RUN_TEST (test_nok_readresponse);
290
328
RUN_TEST (test_ok_writeresponse);
291
329
RUN_TEST (test_spuriousbytes);
292
330
RUN_TEST (test_invalidLength);
0 commit comments