Skip to content

Commit e5ecba7

Browse files
authored
Merge pull request #202 from mkinney/format_mac_address
if mac address is in nodes, format it like a valid mac address
2 parents 9c66447 + a1809f5 commit e5ecba7

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

meshtastic/mesh_interface.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import meshtastic.node
2020
from . import portnums_pb2, mesh_pb2
21-
from .util import stripnl, Timeout, our_exit, remove_keys_from_dict
21+
from .util import stripnl, Timeout, our_exit, remove_keys_from_dict, convert_mac_addr
2222
from .__init__ import LOCAL_ADDR, BROADCAST_NUM, BROADCAST_ADDR, ResponseHandler, publishingThread, OUR_APP_VERSION, protocols
2323

2424
class MeshInterface:
@@ -87,6 +87,14 @@ def showInfo(self, file=sys.stdout):
8787
# when the TBeam is first booted, it sometimes shows the 'raw' data
8888
# so, we will just remove any raw keys
8989
n2 = remove_keys_from_dict('raw', n)
90+
91+
# if we have 'macaddr', re-format it
92+
if 'macaddr' in n2['user']:
93+
val = n2['user']['macaddr']
94+
# decode the base64 value
95+
addr = convert_mac_addr(val)
96+
n2['user']['macaddr'] = addr
97+
9098
nodes = nodes + f" {stripnl(n2)}"
9199
infos = owner + myinfo + mesh + nodes
92100
print(infos)

meshtastic/tests/test_util.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
support_info, genPSK256, fromStr, fromPSK,
1111
quoteBooleans, catchAndIgnore,
1212
remove_keys_from_dict, Timeout, hexstr,
13-
ipstr, readnet_u16, findPorts)
13+
ipstr, readnet_u16, findPorts, convert_mac_addr)
1414

1515

1616
@pytest.mark.unit
@@ -225,3 +225,10 @@ def test_readnet_u16():
225225
def test_findPorts_when_none_found(patch_comports):
226226
"""Test findPorts()"""
227227
assert not findPorts()
228+
229+
230+
@pytest.mark.unit
231+
def test_convert_mac_addr():
232+
"""Test convert_mac_addr()"""
233+
assert convert_mac_addr('/c0gFyhb') == 'fd:cd:20:17:28:5b'
234+
assert convert_mac_addr('') == ''

meshtastic/util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from queue import Queue
55
import os
66
import sys
7+
import base64
78
import time
89
import platform
910
import logging
@@ -224,3 +225,12 @@ def ipstr(barray):
224225
def readnet_u16(p, offset):
225226
"""Read big endian u16 (network byte order)"""
226227
return p[offset] * 256 + p[offset + 1]
228+
229+
230+
def convert_mac_addr(val):
231+
"""Convert the base 64 encoded value to a mac address
232+
val - base64 encoded value (ex: '/c0gFyhb'))
233+
returns: a string formatted like a mac address (ex: 'fd:cd:20:17:28:5b')
234+
"""
235+
val_as_bytes = base64.b64decode(val)
236+
return hexstr(val_as_bytes)

0 commit comments

Comments
 (0)