From a1809f5b840e4e310e069c0055e89454768c04d8 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Fri, 31 Dec 2021 20:01:14 -0800 Subject: [PATCH] if mac address is in nodes, format it like a valid mac address --- meshtastic/mesh_interface.py | 10 +++++++++- meshtastic/tests/test_util.py | 9 ++++++++- meshtastic/util.py | 10 ++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/meshtastic/mesh_interface.py b/meshtastic/mesh_interface.py index cb50fd3..e7e9ca1 100644 --- a/meshtastic/mesh_interface.py +++ b/meshtastic/mesh_interface.py @@ -18,7 +18,7 @@ from google.protobuf.json_format import MessageToJson import meshtastic.node from . import portnums_pb2, mesh_pb2 -from .util import stripnl, Timeout, our_exit, remove_keys_from_dict +from .util import stripnl, Timeout, our_exit, remove_keys_from_dict, convert_mac_addr from .__init__ import LOCAL_ADDR, BROADCAST_NUM, BROADCAST_ADDR, ResponseHandler, publishingThread, OUR_APP_VERSION, protocols class MeshInterface: @@ -87,6 +87,14 @@ class MeshInterface: # when the TBeam is first booted, it sometimes shows the 'raw' data # so, we will just remove any raw keys n2 = remove_keys_from_dict('raw', n) + + # if we have 'macaddr', re-format it + if 'macaddr' in n2['user']: + val = n2['user']['macaddr'] + # decode the base64 value + addr = convert_mac_addr(val) + n2['user']['macaddr'] = addr + nodes = nodes + f" {stripnl(n2)}" infos = owner + myinfo + mesh + nodes print(infos) diff --git a/meshtastic/tests/test_util.py b/meshtastic/tests/test_util.py index 8ca96cc..d05d8e0 100644 --- a/meshtastic/tests/test_util.py +++ b/meshtastic/tests/test_util.py @@ -10,7 +10,7 @@ from meshtastic.util import (fixme, stripnl, pskToString, our_exit, support_info, genPSK256, fromStr, fromPSK, quoteBooleans, catchAndIgnore, remove_keys_from_dict, Timeout, hexstr, - ipstr, readnet_u16, findPorts) + ipstr, readnet_u16, findPorts, convert_mac_addr) @pytest.mark.unit @@ -225,3 +225,10 @@ def test_readnet_u16(): def test_findPorts_when_none_found(patch_comports): """Test findPorts()""" assert not findPorts() + + +@pytest.mark.unit +def test_convert_mac_addr(): + """Test convert_mac_addr()""" + assert convert_mac_addr('/c0gFyhb') == 'fd:cd:20:17:28:5b' + assert convert_mac_addr('') == '' diff --git a/meshtastic/util.py b/meshtastic/util.py index 0a3fca5..e87f725 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -4,6 +4,7 @@ import traceback from queue import Queue import os import sys +import base64 import time import platform import logging @@ -224,3 +225,12 @@ def ipstr(barray): def readnet_u16(p, offset): """Read big endian u16 (network byte order)""" return p[offset] * 256 + p[offset + 1] + + +def convert_mac_addr(val): + """Convert the base 64 encoded value to a mac address + val - base64 encoded value (ex: '/c0gFyhb')) + returns: a string formatted like a mac address (ex: 'fd:cd:20:17:28:5b') + """ + val_as_bytes = base64.b64decode(val) + return hexstr(val_as_bytes)