From 6275173bab6a35171b20c18605a9bb663ce95a10 Mon Sep 17 00:00:00 2001 From: Simon Lin Date: Wed, 11 Dec 2019 00:56:52 +0800 Subject: [PATCH] [thci] fix wrong call to .lstrip('0x') (#4383) Wpanctl THCI might return the wrong RLOC address when RLOC16 == 0 due to incorrect call to .lstrip('0x'). We use .lstrip('0x') to strip '0x' at the beginning of a hex string, however this is not correct when the hex string is '0x0', because '0x0'.lstrip('0x') == '' This commit fixes all incorrect calls to .lstrip('0x'). There are 3 different kinds of fix: - s.lstrip('0x') replaced by self.__lstrip0x(s) (lstrip_0x is a correct implementation of stripping '0x' at the beginning of a string) - hex(v).rstrip('L').lstrip('0x') replaced by '%x' % v - hex(v).lstrip('0x').zfill(4) replaced by '%04x' % v --- tools/harness-thci/OpenThread.py | 29 +++++++++++---- tools/harness-thci/OpenThread_WpanCtl.py | 47 +++++++++++++----------- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/tools/harness-thci/OpenThread.py b/tools/harness-thci/OpenThread.py index 0fd56d756..f364e8483 100644 --- a/tools/harness-thci/OpenThread.py +++ b/tools/harness-thci/OpenThread.py @@ -503,7 +503,7 @@ class OpenThread(IThci): IPv6 address dotted-quad format """ prefix1 = strIp6Prefix.rstrip('L') - prefix2 = prefix1.lstrip('0x') + prefix2 = self.__lstrip0x(prefix1) hexPrefix = str(prefix2).ljust(16, '0') hexIter = iter(hexPrefix) finalMac = ':'.join( @@ -1432,7 +1432,7 @@ class OpenThread(IThci): euiStr = euiStr.rstrip('L') address64 = '' if '0x' in euiStr: - address64 = euiStr.lstrip('0x') + address64 = self.__lstrip0x(euiStr) # prepend 0 at the beginning if len(address64) < 16: address64 = address64.zfill(16) @@ -2410,7 +2410,7 @@ class OpenThread(IThci): cmd += Addr if len(TLVs) != 0: - tlvs = ''.join(hex(tlv).lstrip('0x').zfill(2) for tlv in TLVs) + tlvs = ''.join('%02x' % tlv for tlv in TLVs) cmd += ' binary ' cmd += tlvs @@ -2509,7 +2509,7 @@ class OpenThread(IThci): ModuleHelper.Default_XpanId, ModuleHelper.Default_NwkName, ) - pskc = hex(stretchedPskc).rstrip('L').lstrip('0x') + pskc = '%x' % stretchedPskc if len(pskc) < 32: pskc = pskc.zfill(32) @@ -2605,7 +2605,7 @@ class OpenThread(IThci): cmd += Addr if len(TLVs) != 0: - tlvs = ''.join(hex(tlv).lstrip('0x').zfill(2) for tlv in TLVs) + tlvs = ''.join('%02x' % tlv for tlv in TLVs) cmd += ' binary ' cmd += tlvs @@ -2707,7 +2707,7 @@ class OpenThread(IThci): cmd = 'commissioner mgmtget' if len(TLVs) != 0: - tlvs = ''.join(hex(tlv).lstrip('0x').zfill(2) for tlv in TLVs) + tlvs = ''.join('%02x' % tlv for tlv in TLVs) cmd += ' binary ' cmd += tlvs @@ -2761,7 +2761,7 @@ class OpenThread(IThci): if xChannelTlv is not None: cmd += ' binary ' - cmd += '000300' + hex(xChannelTlv).lstrip('0x').zfill(4) + cmd += '000300' + '%04x' % xChannelTlv print(cmd) @@ -2869,3 +2869,18 @@ class OpenThread(IThci): return True else: return False + + @staticmethod + def __lstrip0x(s): + """strip 0x at the beginning of a hex string if it exists + + Args: + s: hex string + + Returns: + hex string with leading 0x stripped + """ + if s.startswith('0x'): + s = s[2:] + + return s diff --git a/tools/harness-thci/OpenThread_WpanCtl.py b/tools/harness-thci/OpenThread_WpanCtl.py index fa1e65546..09d67b6b3 100644 --- a/tools/harness-thci/OpenThread_WpanCtl.py +++ b/tools/harness-thci/OpenThread_WpanCtl.py @@ -663,7 +663,7 @@ class OpenThread_WpanCtl(IThci): IPv6 address dotted-quad format """ prefix1 = strIp6Prefix.rstrip('L') - prefix2 = prefix1.lstrip('0x') + prefix2 = self.__lstrip0x(prefix1) hexPrefix = str(prefix2).ljust(16, '0') hexIter = iter(hexPrefix) finalMac = ':'.join( @@ -1104,9 +1104,7 @@ class OpenThread_WpanCtl(IThci): )[0] ) mlprefix = prefix.split('/')[0] - rloc16 = self.__sendCommand(WPANCTL_CMD + 'getprop -v Thread:RLOC16')[ - 0 - ].lstrip('0x') + rloc16 = self.__lstrip0x(self.__sendCommand(WPANCTL_CMD + 'getprop -v Thread:RLOC16')[0]) print('prefix: %s' % prefix) print('mlprefix: %s ' % mlprefix) print('rloc16: %s' % rloc16) @@ -2617,7 +2615,7 @@ class OpenThread_WpanCtl(IThci): cmd = WPANCTL_CMD + 'dataset mgmt-get-active' if len(TLVs) != 0: - tlvs = ''.join(hex(tlv).lstrip('0x').zfill(2) for tlv in TLVs) + tlvs = ''.join('%02x' % tlv for tlv in TLVs) setTLVCmd = WPANCTL_CMD + 'setprop Dataset:RawTlvs ' + tlvs if self.__sendCommand(setTLVCmd)[0] == 'Fail': return False @@ -2678,9 +2676,7 @@ class OpenThread_WpanCtl(IThci): return False if listActiveTimestamp is not None: - sActiveTimestamp = str(hex(listActiveTimestamp[0])) - if len(sActiveTimestamp) < 18: - sActiveTimestamp = sActiveTimestamp.lstrip('0x').zfill(16) + sActiveTimestamp = '%016x' % listActiveTimestamp[0] setActiveTimeCmd = ( WPANCTL_CMD + 'setprop Dataset:ActiveTimestamp ' @@ -2767,7 +2763,7 @@ class OpenThread_WpanCtl(IThci): ModuleHelper.Default_XpanId, ModuleHelper.Default_NwkName, ) - pskc = hex(stretchedPskc).rstrip('L').lstrip('0x') + pskc = '%x' % stretchedPskc if len(pskc) < 32: pskc = pskc.zfill(32) @@ -2863,7 +2859,7 @@ class OpenThread_WpanCtl(IThci): cmd = WPANCTL_CMD + 'dataset mgmt-get-pending' if len(TLVs) != 0: - tlvs = ''.join(hex(tlv).lstrip('0x').zfill(2) for tlv in TLVs) + tlvs = ''.join('%02x' % tlv for tlv in TLVs) setTLVCmd = WPANCTL_CMD + 'setprop Dataset:RawTlvs ' + tlvs if self.__sendCommand(setTLVCmd)[0] == 'Fail': return False @@ -2916,9 +2912,7 @@ class OpenThread_WpanCtl(IThci): return False if listPendingTimestamp is not None: - sActiveTimestamp = str(hex(listPendingTimestamp[0])) - if len(sActiveTimestamp) < 18: - sActiveTimestamp = sActiveTimestamp.lstrip('0x').zfill(16) + sActiveTimestamp = '%016x' % listPendingTimestamp[0] setPendingTimeCmd = ( WPANCTL_CMD + 'setprop Dataset:PendingTimestamp ' @@ -2928,9 +2922,7 @@ class OpenThread_WpanCtl(IThci): return False if listActiveTimestamp is not None: - sActiveTimestamp = str(hex(listActiveTimestamp[0])) - if len(sActiveTimestamp) < 18: - sActiveTimestamp = sActiveTimestamp.lstrip('0x').zfill(16) + sActiveTimestamp = '%016x' % listActiveTimestamp[0] setActiveTimeCmd = ( WPANCTL_CMD + 'setprop Dataset:ActiveTimestamp ' @@ -3012,7 +3004,7 @@ class OpenThread_WpanCtl(IThci): print(TLVs) if len(TLVs) != 0: - tlvs = ''.join(hex(tlv).lstrip('0x').zfill(2) for tlv in TLVs) + tlvs = ''.join('%02x' % tlv for tlv in TLVs) cmd += tlvs print(cmd) @@ -3057,9 +3049,7 @@ class OpenThread_WpanCtl(IThci): elif xCommissionerSessionID is None: # use original session id if self.isActiveCommissioner is True: - cmd += '0b02' + self.__getCommissionerSessionId().lstrip( - '0x' - ) + cmd += '0b02' + self.__lstrip0x(self.__getCommissionerSessionId()) else: pass @@ -3074,7 +3064,7 @@ class OpenThread_WpanCtl(IThci): cmd += '0902' + str(hex(xBorderRouterLocator)) if xChannelTlv is not None: - cmd += '000300' + hex(xChannelTlv).lstrip('0x').zfill(4) + cmd += '000300' + '%04x' % xChannelTlv print(cmd) @@ -3177,3 +3167,18 @@ class OpenThread_WpanCtl(IThci): return True else: return False + + @staticmethod + def __lstrip0x(s): + """strip 0x at the beginning of a hex string if it exists + + Args: + s: hex string + + Returns: + hex string with leading 0x stripped + """ + if s.startswith('0x'): + s = s[2:] + + return s