From 126f6b2915fc26ee17342e1dc80f96f031938a6c Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Wed, 6 Jul 2022 15:04:20 +0200 Subject: [PATCH] [python] Move string encoding to ChipDeviceCtrl.py (#20306) * Encode strings in ChipDeviceCtrl.py * Fix Python tests --- src/controller/python/chip-device-ctrl.py | 8 +++----- src/controller/python/chip/ChipDeviceCtrl.py | 14 +++++++------- src/controller/python/test/test_scripts/base.py | 8 ++++---- .../python/test/test_scripts/mobile-device-test.py | 2 -- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/controller/python/chip-device-ctrl.py b/src/controller/python/chip-device-ctrl.py index 4d85016de24498..c94514b719fe36 100755 --- a/src/controller/python/chip-device-ctrl.py +++ b/src/controller/python/chip-device-ctrl.py @@ -552,8 +552,7 @@ def do_paseonly(self, line): print("Device is assigned with nodeid = {}".format(nodeid)) if args[0] == "-ip" and len(args) >= 3: - self.devCtrl.EstablishPASESessionIP(args[1].encode( - "utf-8"), int(args[2]), nodeid) + self.devCtrl.EstablishPASESessionIP(args[1], int(args[2]), nodeid) else: print("Usage:") self.do_help("paseonly") @@ -614,8 +613,7 @@ def do_connect(self, line): print("Device is assigned with nodeid = {}".format(nodeid)) if args[0] == "-ip" and len(args) >= 3: - self.devCtrl.CommissionIP(args[1].encode( - "utf-8"), int(args[2]), nodeid) + self.devCtrl.CommissionIP(args[1], int(args[2]), nodeid) elif args[0] == "-ble" and len(args) >= 3: self.devCtrl.ConnectBLE(int(args[1]), int(args[2]), nodeid) elif args[0] in ['-qr', '-code'] and len(args) >= 2: @@ -948,7 +946,7 @@ def do_setpairingwificredential(self, line): self.do_help("set-pairing-wifi-credential") return self.devCtrl.SetWiFiCredentials( - args[0].encode("utf-8"), args[1].encode("utf-8")) + args[0], args[1]) except Exception as ex: print(str(ex)) return diff --git a/src/controller/python/chip/ChipDeviceCtrl.py b/src/controller/python/chip/ChipDeviceCtrl.py index 8b2f546418ada6..70aa7cada7b5f3 100644 --- a/src/controller/python/chip/ChipDeviceCtrl.py +++ b/src/controller/python/chip/ChipDeviceCtrl.py @@ -278,13 +278,13 @@ def CloseSession(self, nodeid): self.devCtrl, nodeid) ) - def EstablishPASESessionIP(self, ipaddr, setupPinCode, nodeid): + def EstablishPASESessionIP(self, ipaddr: str, setupPinCode: int, nodeid: int): self.CheckIsActive() self.state = DCState.RENDEZVOUS_ONGOING return self._ChipStack.CallAsync( lambda: self._dmLib.pychip_DeviceController_EstablishPASESessionIP( - self.devCtrl, ipaddr, setupPinCode, nodeid) + self.devCtrl, ipaddr.encode("utf-8"), setupPinCode, nodeid) ) def Commission(self, nodeid): @@ -345,7 +345,7 @@ def CommissionWithCode(self, setupPayload: str, nodeid: int): return False return self._ChipStack.commissioningEventRes == 0 - def CommissionIP(self, ipaddr, setupPinCode, nodeid): + def CommissionIP(self, ipaddr: str, setupPinCode: int, nodeid: int): self.CheckIsActive() # IP connection will run through full commissioning, so we need to wait @@ -356,7 +356,7 @@ def CommissionIP(self, ipaddr, setupPinCode, nodeid): self._ChipStack.CallAsync( lambda: self._dmLib.pychip_DeviceController_ConnectIP( - self.devCtrl, ipaddr, setupPinCode, nodeid) + self.devCtrl, ipaddr.encode("utf-8"), setupPinCode, nodeid) ) if not self._ChipStack.commissioningCompleteEvent.isSet(): # Error 50 is a timeout @@ -374,18 +374,18 @@ def CommissionThread(self, discriminator, setupPinCode, nodeId, threadOperationa self.SetThreadOperationalDataset(threadOperationalDataset) return self.ConnectBLE(discriminator, setupPinCode, nodeId) - def CommissionWiFi(self, discriminator, setupPinCode, nodeId, ssid, credentials): + def CommissionWiFi(self, discriminator, setupPinCode, nodeId, ssid: str, credentials: str): ''' Commissions a WiFi device over BLE ''' self.SetWiFiCredentials(ssid, credentials) return self.ConnectBLE(discriminator, setupPinCode, nodeId) - def SetWiFiCredentials(self, ssid, credentials): + def SetWiFiCredentials(self, ssid: str, credentials: str): self.CheckIsActive() return self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_SetWiFiCredentials( - ssid, credentials) + ssid.encode("utf-8"), credentials.encode("utf-8")) ) def SetThreadOperationalDataset(self, threadOperationalDataset): diff --git a/src/controller/python/test/test_scripts/base.py b/src/controller/python/test/test_scripts/base.py index 020d7744d44724..84bc98328122b9 100644 --- a/src/controller/python/test/test_scripts/base.py +++ b/src/controller/python/test/test_scripts/base.py @@ -194,7 +194,7 @@ def _WaitForOneDiscoveredDevice(self, timeoutSeconds: int = 2): time.sleep(0.2) if time.time() > timeout: return None - return ctypes.string_at(addrStrStorage) + return ctypes.string_at(addrStrStorage).decode("utf-8") def TestDiscovery(self, discriminator: int): self.logger.info( @@ -213,7 +213,7 @@ def TestPaseOnly(self, ip: str, setuppin: int, nodeid: int): self.logger.info( "Attempting to establish PASE session with device id: {} addr: {}".format(str(nodeid), ip)) if self.devCtrl.EstablishPASESessionIP( - ip.encode("utf-8"), setuppin, nodeid) is not None: + ip, setuppin, nodeid) is not None: self.logger.info( "Failed to establish PASE session with device id: {} addr: {}".format(str(nodeid), ip)) return False @@ -267,7 +267,7 @@ def TestCommissionFailureOnReport(self, nodeid: int, failAfter: int): def TestCommissioning(self, ip: str, setuppin: int, nodeid: int): self.logger.info("Commissioning device {}".format(ip)) - if not self.devCtrl.CommissionIP(ip.encode("utf-8"), setuppin, nodeid): + if not self.devCtrl.CommissionIP(ip, setuppin, nodeid): self.logger.info( "Failed to finish commissioning device {}".format(ip)) return False @@ -597,7 +597,7 @@ async def TestMultiFabric(self, ip: str, setuppin: int, nodeid: int): self.devCtrl2 = self.fabricAdmin2.NewController( self.controllerNodeId, self.paaTrustStorePath) - if not self.devCtrl2.CommissionIP(ip.encode("utf-8"), setuppin, nodeid): + if not self.devCtrl2.CommissionIP(ip, setuppin, nodeid): self.logger.info( "Failed to finish key exchange with device {}".format(ip)) return False diff --git a/src/controller/python/test/test_scripts/mobile-device-test.py b/src/controller/python/test/test_scripts/mobile-device-test.py index a2f470763738e5..f02a93dc388953 100755 --- a/src/controller/python/test/test_scripts/mobile-device-test.py +++ b/src/controller/python/test/test_scripts/mobile-device-test.py @@ -67,8 +67,6 @@ def ethernet_commissioning(test: BaseTestHelper, discriminator: int, setup_pin: if address_override: address = address_override - else: - address = address.decode("utf-8") logger.info("Testing commissioning") FailIfNot(test.TestCommissioning(ip=address,