Skip to content

Commit d95b7bf

Browse files
committed
using BytesIO to decode packets
1 parent 2599b97 commit d95b7bf

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/meshcore_cli/meshcore_cli.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
import asyncio
7-
import os, sys
7+
import os, sys, io
88
import time, datetime
99
import getopt, json, shlex, re
1010
import logging
@@ -208,22 +208,19 @@ async def handle_log_rx(event):
208208
return
209209

210210
pkt = bytes().fromhex(event.payload["payload"])
211+
pbuf = io.BytesIO(pkt)
212+
header = pbuf.read(1)[0]
213+
214+
if header & ~1 == 0x14: # flood msg / channel
215+
if handle_log_rx.channel_echoes:
216+
if header & 1 == 0: # has transport code
217+
pbuf.read(4) # discard transport code
218+
path_len = pbuf.read(1)[0]
219+
path = pbuf.read(path_len).hex()
220+
chan_hash = pbuf.read(1).hex()
221+
cipher_mac = pbuf.read(2)
222+
msg = pbuf.read() # until the end of buffer
211223

212-
if handle_log_rx.channel_echoes:
213-
if pkt[0] & ~1 == 0x14:
214-
chan_name = ""
215-
if pkt[0] & 1: #no transport code
216-
path_len = pkt[1]
217-
path = pkt[2:path_len+2].hex()
218-
path_end = path_len+2
219-
else:
220-
path_len = pkt[5]
221-
path = pkt[6:path_len+6].hex()
222-
path_end = path_len+6
223-
224-
chan_hash = pkt[path_end:path_end+1].hex()
225-
cipher_mac = pkt[path_end+1:path_end+3]
226-
msg = pkt[path_end+3:]
227224
channel = None
228225
for c in await get_channels(mc):
229226
if c["channel_hash"] == chan_hash : # validate against MAC
@@ -233,6 +230,8 @@ async def handle_log_rx(event):
233230
channel = c
234231
break
235232

233+
chan_name = ""
234+
236235
if channel is None :
237236
if handle_log_rx.echo_unk_chans:
238237
chan_name = chan_hash

0 commit comments

Comments
 (0)