Skip to content

Commit

Permalink
[python] Move string encoding to ChipDeviceCtrl.py (project-chip#20306)
Browse files Browse the repository at this point in the history
* Encode strings in ChipDeviceCtrl.py

* Fix Python tests
  • Loading branch information
agners authored Jul 6, 2022
1 parent 342ba68 commit 126f6b2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
8 changes: 3 additions & 5 deletions src/controller/python/chip-device-ctrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/controller/python/chip/ChipDeviceCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions src/controller/python/test/test_scripts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/controller/python/test/test_scripts/mobile-device-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 126f6b2

Please sign in to comment.