From 6a2a9d20935983be9bfb955c312e3411d9a38675 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Wed, 12 Jan 2022 17:10:51 -0800 Subject: [PATCH] suggested fix from MitchConner912 for not converting mac address more than once --- meshtastic/tests/test_util.py | 1 + meshtastic/util.py | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/meshtastic/tests/test_util.py b/meshtastic/tests/test_util.py index b933143..03cf4c9 100644 --- a/meshtastic/tests/test_util.py +++ b/meshtastic/tests/test_util.py @@ -249,4 +249,5 @@ def test_findPorts_when_none_found(patch_comports): def test_convert_mac_addr(): """Test convert_mac_addr()""" assert convert_mac_addr('/c0gFyhb') == 'fd:cd:20:17:28:5b' + assert convert_mac_addr('fd:cd:20:17:28:5b') == 'fd:cd:20:17:28:5b' assert convert_mac_addr('') == '' diff --git a/meshtastic/util.py b/meshtastic/util.py index 0d6a837..8abbea9 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -3,6 +3,7 @@ import traceback from queue import Queue import os +import re import sys import base64 import time @@ -238,5 +239,7 @@ def convert_mac_addr(val): 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) + if not re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", val): + val_as_bytes = base64.b64decode(val) + return hexstr(val_as_bytes) + return val