From 72e0f2a92b947e56adc21420ef2e857aec83f299 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Sun, 7 Jul 2024 13:47:02 -0700 Subject: [PATCH] Don't silently ingnore malformed protobufs (the \0 in the device side was at fault) --- meshtastic/ble_interface.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/meshtastic/ble_interface.py b/meshtastic/ble_interface.py index 7c8aee6..8d7f47e 100644 --- a/meshtastic/ble_interface.py +++ b/meshtastic/ble_interface.py @@ -8,16 +8,14 @@ import time from threading import Thread from typing import List, Optional +import google.protobuf from bleak import BleakClient, BleakScanner, BLEDevice from bleak.exc import BleakDBusError, BleakError -import google.protobuf - from meshtastic.mesh_interface import MeshInterface -from .protobuf import ( - mesh_pb2, -) +from .protobuf import mesh_pb2 + SERVICE_UUID = "6ba1b218-15a8-461f-9fa8-5dcae273eafd" TORADIO_UUID = "f75c76d2-129e-4dad-a1dd-7866124401e7" FROMRADIO_UUID = "2c55e69e-4993-11ed-b878-0242ac120002" @@ -63,7 +61,9 @@ class BLEInterface(MeshInterface): raise e if self.client.has_characteristic(LEGACY_LOGRADIO_UUID): - self.client.start_notify(LEGACY_LOGRADIO_UUID, self.legacy_log_radio_handler) + self.client.start_notify( + LEGACY_LOGRADIO_UUID, self.legacy_log_radio_handler + ) if self.client.has_characteristic(LOGRADIO_UUID): self.client.start_notify(LOGRADIO_UUID, self.log_radio_handler) @@ -94,11 +94,15 @@ class BLEInterface(MeshInterface): log_record = mesh_pb2.LogRecord() try: log_record.ParseFromString(bytes(b)) - except google.protobuf.message.DecodeError: - return - message = f'[{log_record.source}] {log_record.message}' if log_record.source else log_record.message - self._handleLogLine(message) + message = ( + f"[{log_record.source}] {log_record.message}" + if log_record.source + else log_record.message + ) + self._handleLogLine(message) + except google.protobuf.message.DecodeError: + logging.warning("Malformed LogRecord received. Skipping.") async def legacy_log_radio_handler(self, _, b): # pylint: disable=C0116 log_radio = b.decode("utf-8").replace("\n", "") @@ -215,7 +219,9 @@ class BLEInterface(MeshInterface): if self._want_receive: self.want_receive = False # Tell the thread we want it to stop - self._receiveThread.join(timeout=2) # If bleak is hung, don't wait for the thread to exit (it is critical we disconnect) + self._receiveThread.join( + timeout=2 + ) # If bleak is hung, don't wait for the thread to exit (it is critical we disconnect) self._receiveThread = None if self.client: