Skip to content

Commit 30f950f

Browse files
authored
Fix onError on write response
1 parent 1314205 commit 30f950f

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

platformio.ini

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ build_flags =
66
-Wextra
77
-Wpedantic
88
-std=c++11
9-
-pthread
109

1110
[env:native]
1211
platform = native
@@ -18,3 +17,10 @@ build_flags =
1817
-D VW_START_PAYLOAD_LENGTH=10
1918
extra_scripts = test_coverage.py
2019
build_type = debug
20+
test_testing_command =
21+
valgrind
22+
--leak-check=full
23+
--show-leak-kinds=all
24+
--track-origins=yes
25+
--error-exitcode=1
26+
${platformio.build_dir}/${this.__env__}/program

src/VS2/PacketVS2.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ bool PacketVS2::createPacket(PacketType pt, FunctionCode fc, uint8_t id, uint16_
4545

4646
// check arguments
4747
if (len == 0) {
48-
vw_log_i("Length error: %u", len);
48+
vw_log_w("Length error: %u", len);
4949
return false;
5050
}
5151
if (fc == FunctionCode::WRITE && !data) {
52-
vw_log_i("Function code - data mismatch");
52+
vw_log_w("Function code - data mismatch");
5353
return false;
5454
}
5555
if (id > 7) {
@@ -61,7 +61,7 @@ bool PacketVS2::createPacket(PacketType pt, FunctionCode fc, uint8_t id, uint16_
6161
if (toAllocate > _allocatedLength) {
6262
uint8_t* newBuffer = reinterpret_cast<uint8_t*>(realloc(_buffer, toAllocate));
6363
if (!newBuffer) {
64-
vw_log_i("buffer not available");
64+
vw_log_e("buffer not available");
6565
return false;
6666
}
6767
_buffer = newBuffer;
@@ -129,6 +129,7 @@ uint8_t PacketVS2::dataLength() const {
129129
}
130130

131131
const uint8_t* PacketVS2::data() const {
132+
if (functionCode() == FunctionCode::WRITE) return nullptr;
132133
return &_buffer[6];
133134
}
134135

src/VS2/ParserVS2.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,20 @@ ParserResult ParserVS2::parse(const uint8_t b) {
7777

7878
case ParserStep::PAYLOADLENGTH:
7979
_packet[5] = b;
80-
if (_packet.functionCode() == VitoWiFi::FunctionCode::WRITE ||
81-
_packet.packetType() == VitoWiFi::PacketType::RESPONSE) {
80+
if ((_packet.functionCode() == VitoWiFi::FunctionCode::READ &&
81+
_packet.packetType() == VitoWiFi::PacketType::REQUEST) ||
82+
(_packet.functionCode() == VitoWiFi::FunctionCode::WRITE &&
83+
_packet.packetType() == VitoWiFi::PacketType::RESPONSE)) {
84+
// read requests and write responses don't have a data payload
85+
_step = ParserStep::CHECKSUM;
86+
} else {
8287
if (b != _packet.length() - 6U) {
8388
vw_log_w("Invalid payload length: %u (expected %u)", b, _packet.length() - 6U);
8489
_step = ParserStep::STARTBYTE;
8590
return ParserResult::ERROR;
8691
}
8792
_payloadLength = b;
8893
_step = ParserStep::PAYLOAD;
89-
} else {
90-
_step = ParserStep::CHECKSUM;
9194
}
9295
break;
9396

test/test_ParserVS2/test_ParserVS2.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void test_ok_request() {
5454
TEST_ASSERT_EQUAL_UINT8(0x02, parser.packet().dataLength());
5555
}
5656

57-
void test_ok_response() {
57+
void test_ok_readresponse() {
5858
const uint8_t stream[] = {
5959
0x41, // start byte
6060
0x07, // length
@@ -81,7 +81,7 @@ void test_ok_response() {
8181
}
8282
}
8383

84-
TEST_ASSERT_EQUAL(ParserResult::COMPLETE, result);
84+
TEST_ASSERT_EQUAL(ParserResult::COMPLETE, result);
8585
TEST_ASSERT_EQUAL_UINT(length, bytesRead);
8686
TEST_ASSERT_EQUAL_UINT8(packetLength, parser.packet().length());
8787
TEST_ASSERT_EQUAL_UINT8(PacketType::RESPONSE, parser.packet().packetType());
@@ -92,6 +92,41 @@ void test_ok_response() {
9292
TEST_ASSERT_EQUAL_UINT8_ARRAY(data, parser.packet().data(), 2);
9393
}
9494

95+
void test_ok_writeresponse() {
96+
const uint8_t stream[] = {
97+
0x41, // start byte
98+
0x05, // length
99+
0x01, // packet type (response)
100+
0x02, // flags: id + function code (0 + write)
101+
0x23, // address 1
102+
0x23, // address 2
103+
0x01, // payload length
104+
0x4F // cs
105+
};
106+
const std::size_t length = 8;
107+
const std::size_t packetLength = 6;
108+
109+
std::size_t bytesRead = 0;
110+
ParserResult result = ParserResult::ERROR;
111+
112+
while (bytesRead < length) {
113+
result = parser.parse(stream[bytesRead++]);
114+
if (result != ParserResult::CONTINUE) {
115+
break;
116+
}
117+
}
118+
119+
TEST_ASSERT_EQUAL(ParserResult::COMPLETE, result);
120+
TEST_ASSERT_EQUAL_UINT(length, bytesRead);
121+
TEST_ASSERT_EQUAL_UINT8(packetLength, parser.packet().length());
122+
TEST_ASSERT_EQUAL_UINT8(PacketType::RESPONSE, parser.packet().packetType());
123+
TEST_ASSERT_EQUAL_UINT8(0x00, parser.packet().id());
124+
TEST_ASSERT_EQUAL_UINT8(FunctionCode::WRITE, parser.packet().functionCode());
125+
TEST_ASSERT_EQUAL_UINT16(0x2323, parser.packet().address());
126+
TEST_ASSERT_EQUAL_UINT8(0x01, parser.packet().dataLength());
127+
TEST_ASSERT_NULL(parser.packet().data());
128+
}
129+
95130
void test_spuriousbytes() {
96131
const uint8_t stream[] = {
97132
0x05,
@@ -251,7 +286,8 @@ void test_invalidChecksum() {
251286
int main() {
252287
UNITY_BEGIN();
253288
RUN_TEST(test_ok_request);
254-
RUN_TEST(test_ok_response);
289+
RUN_TEST(test_ok_readresponse);
290+
RUN_TEST(test_ok_writeresponse);
255291
RUN_TEST(test_spuriousbytes);
256292
RUN_TEST(test_invalidLength);
257293
RUN_TEST(test_invalidPacketType);

0 commit comments

Comments
 (0)