Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/CMRI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,9 @@ void CMRI::transmit()
_serial.write(GET);
for (int i = 0; i < _tx_length; i++)
{
if (_tx_buffer[i] == ETX)
_serial.write(ESC); // escape because this looks like an STX bit (very basic protocol)
if (_tx_buffer[i] == ESC)
_serial.write(ESC); // escape because this looks like an escape bit (very basic protocol)
// DLE-escape STX, ETX and DLE characters
if (_tx_buffer[i] == STX || _tx_buffer[i] == ETX || _tx_buffer[i] == ESC)
_serial.write(ESC);
_serial.write(_tx_buffer[i]);
}
_serial.write(ETX);
Expand Down
10 changes: 6 additions & 4 deletions test/test_cmri/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ void test_set_bit_reflected_in_transmit(void)
cmri.transmit();

// Frame: FF FF STX 'A' GET <3 data bytes> ETX
TEST_ASSERT_EQUAL_UINT8(0x01, s.tx[5]); // byte 0, bit 0
TEST_ASSERT_EQUAL_UINT8(0x02, s.tx[6]); // byte 1, bit 1
TEST_ASSERT_EQUAL_UINT8(0x00, s.tx[7]);
// byte 1 = 0x02 (STX), so it gets DLE-escaped to ESC 0x02
TEST_ASSERT_EQUAL_UINT8(0x01, s.tx[5]); // byte 0, bit 0
TEST_ASSERT_EQUAL_UINT8(CMRI::ESC, s.tx[6]); // DLE escape for byte 1 (STX value)
TEST_ASSERT_EQUAL_UINT8(0x02, s.tx[7]); // byte 1, bit 1
TEST_ASSERT_EQUAL_UINT8(0x00, s.tx[8]); // byte 2
}

// Regression for the set_bit() bounds bug: the old (pos + 7) / 8 check wrongly
Expand Down Expand Up @@ -154,7 +156,7 @@ void test_address_filtering(void)
TEST_ASSERT_EQUAL_UINT8(0, cmri.get_byte(0));
}

// Data bytes that collide with ETX/ESC are escaped in the transmitted frame.
// Data bytes that collide with STX, ETX or ESC are escaped in the transmitted frame.
void test_transmit_escapes_control_bytes(void)
{
Stream s;
Expand Down