Skip to content

Commit 72e0f2a

Browse files
committed
Don't silently ingnore malformed protobufs (the \0 in the device side
was at fault)
1 parent ecbda74 commit 72e0f2a

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

meshtastic/ble_interface.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
from threading import Thread
99
from typing import List, Optional
1010

11+
import google.protobuf
1112
from bleak import BleakClient, BleakScanner, BLEDevice
1213
from bleak.exc import BleakDBusError, BleakError
1314

14-
import google.protobuf
15-
1615
from meshtastic.mesh_interface import MeshInterface
1716

18-
from .protobuf import (
19-
mesh_pb2,
20-
)
17+
from .protobuf import mesh_pb2
18+
2119
SERVICE_UUID = "6ba1b218-15a8-461f-9fa8-5dcae273eafd"
2220
TORADIO_UUID = "f75c76d2-129e-4dad-a1dd-7866124401e7"
2321
FROMRADIO_UUID = "2c55e69e-4993-11ed-b878-0242ac120002"
@@ -63,7 +61,9 @@ def __init__(
6361
raise e
6462

6563
if self.client.has_characteristic(LEGACY_LOGRADIO_UUID):
66-
self.client.start_notify(LEGACY_LOGRADIO_UUID, self.legacy_log_radio_handler)
64+
self.client.start_notify(
65+
LEGACY_LOGRADIO_UUID, self.legacy_log_radio_handler
66+
)
6767

6868
if self.client.has_characteristic(LOGRADIO_UUID):
6969
self.client.start_notify(LOGRADIO_UUID, self.log_radio_handler)
@@ -94,11 +94,15 @@ async def log_radio_handler(self, _, b): # pylint: disable=C0116
9494
log_record = mesh_pb2.LogRecord()
9595
try:
9696
log_record.ParseFromString(bytes(b))
97-
except google.protobuf.message.DecodeError:
98-
return
9997

100-
message = f'[{log_record.source}] {log_record.message}' if log_record.source else log_record.message
101-
self._handleLogLine(message)
98+
message = (
99+
f"[{log_record.source}] {log_record.message}"
100+
if log_record.source
101+
else log_record.message
102+
)
103+
self._handleLogLine(message)
104+
except google.protobuf.message.DecodeError:
105+
logging.warning("Malformed LogRecord received. Skipping.")
102106

103107
async def legacy_log_radio_handler(self, _, b): # pylint: disable=C0116
104108
log_radio = b.decode("utf-8").replace("\n", "")
@@ -215,7 +219,9 @@ def close(self):
215219

216220
if self._want_receive:
217221
self.want_receive = False # Tell the thread we want it to stop
218-
self._receiveThread.join(timeout=2) # If bleak is hung, don't wait for the thread to exit (it is critical we disconnect)
222+
self._receiveThread.join(
223+
timeout=2
224+
) # If bleak is hung, don't wait for the thread to exit (it is critical we disconnect)
219225
self._receiveThread = None
220226

221227
if self.client:

0 commit comments

Comments
 (0)