Skip to content
Snippets Groups Projects
Commit 938b1f7a authored by Charles Javerliat's avatar Charles Javerliat
Browse files

Added escape character

parent 09dd6989
No related branches found
No related tags found
No related merge requests found
......@@ -30,7 +30,14 @@ def frame_data(data: bytearray, dst: bytearray):
dst.append(SOF_FLAG)
dst.append(len(data) >> 8 & 0xFF)
dst.append(len(data) & 0xFF)
dst.extend(data)
for d in data:
if d == EOF_FLAG or d == SOF_FLAG or d == ESC_FLAG:
dst.append(ESC_FLAG)
dst.append(d ^ 0x20)
else:
dst.append(d)
dst.append(crc >> 8 & 0xFF)
dst.append(crc & 0xFF)
dst.append(EOF_FLAG)
......@@ -53,7 +60,14 @@ def unframe_data(framed_data: bytearray, dst: bytearray):
return ERR_MISSING_SOF_DELIMITER
size = int.from_bytes(framed_data[1:3], byteorder="big")
dst.extend(framed_data[3:3 + size])
for i in range(3, 3 + size):
if framed_data[i] == ESC_FLAG:
dst.append(framed_data[i + 1] ^ 0x20)
i += 1
else:
dst.append(framed_data[i])
crc = framed_data[3 + size:3 + size + 2]
if crc != crc16(dst):
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment