Skip to content

Commit 22bf34e

Browse files
authored
Merge pull request #627 from meshtastic/protobuf-ble-logging
New LogRecord protobuf BLE logging
2 parents 0e6a0eb + 32b4575 commit 22bf34e

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

meshtastic/ble_interface.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@
77
import time
88
from threading import Thread
99
from typing import List, Optional
10-
1110
import print_color # type: ignore[import-untyped]
11+
1212
from bleak import BleakClient, BleakScanner, BLEDevice
1313
from bleak.exc import BleakDBusError, BleakError
1414

15+
import google.protobuf
16+
1517
from meshtastic.mesh_interface import MeshInterface
1618

19+
from .protobuf import (
20+
mesh_pb2,
21+
)
1722
SERVICE_UUID = "6ba1b218-15a8-461f-9fa8-5dcae273eafd"
1823
TORADIO_UUID = "f75c76d2-129e-4dad-a1dd-7866124401e7"
1924
FROMRADIO_UUID = "2c55e69e-4993-11ed-b878-0242ac120002"
2025
FROMNUM_UUID = "ed9da18c-a800-4f66-a670-aa7547e34453"
21-
LOGRADIO_UUID = "6c6fd238-78fa-436b-aacf-15c5be1ef2e2"
26+
LEGACY_LOGRADIO_UUID = "6c6fd238-78fa-436b-aacf-15c5be1ef2e2"
27+
LOGRADIO_UUID = "5a3d6e49-06e6-4423-9944-e9de8cdf9547"
2228

2329

2430
class BLEInterface(MeshInterface):
@@ -56,6 +62,9 @@ def __init__(
5662
self.close()
5763
raise e
5864

65+
if self.client.has_characteristic(LEGACY_LOGRADIO_UUID):
66+
self.client.start_notify(LEGACY_LOGRADIO_UUID, self.legacy_log_radio_handler)
67+
5968
if self.client.has_characteristic(LOGRADIO_UUID):
6069
self.client.start_notify(LOGRADIO_UUID, self.log_radio_handler)
6170

@@ -82,6 +91,26 @@ def from_num_handler(self, _, b): # pylint: disable=C0116
8291
self.should_read = True
8392

8493
async def log_radio_handler(self, _, b): # pylint: disable=C0116
94+
log_record = mesh_pb2.LogRecord()
95+
try:
96+
log_record.ParseFromString(bytes(b))
97+
except google.protobuf.message.DecodeError:
98+
return
99+
100+
message = f'[{log_record.source}] {log_record.message}' if log_record.source else log_record.message
101+
102+
if log_record.DEBUG:
103+
print_color.print(message, color="cyan", end=None)
104+
elif log_record.INFO:
105+
print_color.print(message, color="white", end=None)
106+
elif log_record.WARNING:
107+
print_color.print(message, color="yellow", end=None)
108+
elif log_record.ERROR:
109+
print_color.print(message, color="red", end=None)
110+
else:
111+
print_color.print(message, end=None)
112+
113+
async def legacy_log_radio_handler(self, _, b): # pylint: disable=C0116
85114
log_radio = b.decode("utf-8").replace("\n", "")
86115
if log_radio.startswith("DEBUG"):
87116
print_color.print(log_radio, color="cyan", end=None)

0 commit comments

Comments
 (0)