Skip to content

Commit

Permalink
Merge pull request #627 from meshtastic/protobuf-ble-logging
Browse files Browse the repository at this point in the history
New LogRecord protobuf BLE logging
  • Loading branch information
ianmcorvidae authored Jul 2, 2024
2 parents 0e6a0eb + 32b4575 commit 22bf34e
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions meshtastic/ble_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@
import time
from threading import Thread
from typing import List, Optional

import print_color # type: ignore[import-untyped]

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,
)
SERVICE_UUID = "6ba1b218-15a8-461f-9fa8-5dcae273eafd"
TORADIO_UUID = "f75c76d2-129e-4dad-a1dd-7866124401e7"
FROMRADIO_UUID = "2c55e69e-4993-11ed-b878-0242ac120002"
FROMNUM_UUID = "ed9da18c-a800-4f66-a670-aa7547e34453"
LOGRADIO_UUID = "6c6fd238-78fa-436b-aacf-15c5be1ef2e2"
LEGACY_LOGRADIO_UUID = "6c6fd238-78fa-436b-aacf-15c5be1ef2e2"
LOGRADIO_UUID = "5a3d6e49-06e6-4423-9944-e9de8cdf9547"


class BLEInterface(MeshInterface):
Expand Down Expand Up @@ -56,6 +62,9 @@ def __init__(
self.close()
raise e

if self.client.has_characteristic(LEGACY_LOGRADIO_UUID):
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)

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

async def log_radio_handler(self, _, b): # pylint: disable=C0116
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

if log_record.DEBUG:
print_color.print(message, color="cyan", end=None)
elif log_record.INFO:
print_color.print(message, color="white", end=None)
elif log_record.WARNING:
print_color.print(message, color="yellow", end=None)
elif log_record.ERROR:
print_color.print(message, color="red", end=None)
else:
print_color.print(message, end=None)

async def legacy_log_radio_handler(self, _, b): # pylint: disable=C0116
log_radio = b.decode("utf-8").replace("\n", "")
if log_radio.startswith("DEBUG"):
print_color.print(log_radio, color="cyan", end=None)
Expand Down

0 comments on commit 22bf34e

Please sign in to comment.