From 595b61820b645001190241262bc3afaa58c6b0a4 Mon Sep 17 00:00:00 2001 From: Jerry Johns Date: Sun, 11 Dec 2022 19:32:16 -0800 Subject: [PATCH] Add ability to establish PASE over BLE to Python DeviceController (#22985) * Add ability to establish PASE over BLE to Python DeviceController This adds the ability to just establish PASE over BLE to the Python device controller. This then makes it possible to write custom commissioning code in Python if needed. * Restyled by clang-format * fix compile error * Fix return error for EstablishPaseSessionBLE API Co-authored-by: yunhanw-google Co-authored-by: Restyled.io --- .../ChipDeviceController-ScriptBinding.cpp | 18 ++++++++++++++++++ src/controller/python/chip/ChipDeviceCtrl.py | 13 +++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/controller/python/ChipDeviceController-ScriptBinding.cpp b/src/controller/python/ChipDeviceController-ScriptBinding.cpp index a7659e60d21f4b..8115266991c2cd 100644 --- a/src/controller/python/ChipDeviceController-ScriptBinding.cpp +++ b/src/controller/python/ChipDeviceController-ScriptBinding.cpp @@ -134,6 +134,8 @@ PyChipError pychip_DeviceController_SetWiFiCredentials(const char * ssid, const PyChipError pychip_DeviceController_CloseSession(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid); PyChipError pychip_DeviceController_EstablishPASESessionIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr, uint32_t setupPINCode, chip::NodeId nodeid); +PyChipError pychip_DeviceController_EstablishPASESessionBLE(chip::Controller::DeviceCommissioner * devCtrl, uint32_t setupPINCode, + uint16_t discriminator, chip::NodeId nodeid); PyChipError pychip_DeviceController_Commission(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid); PyChipError pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, @@ -465,6 +467,17 @@ PyChipError pychip_DeviceController_EstablishPASESessionIP(chip::Controller::Dev params.SetPeerAddress(addr).SetDiscriminator(0); return ToPyChipError(devCtrl->EstablishPASEConnection(nodeid, params)); } + +PyChipError pychip_DeviceController_EstablishPASESessionBLE(chip::Controller::DeviceCommissioner * devCtrl, uint32_t setupPINCode, + uint16_t discriminator, chip::NodeId nodeid) +{ + chip::Transport::PeerAddress addr; + RendezvousParameters params = chip::RendezvousParameters().SetSetupPINCode(setupPINCode); + addr.SetTransportType(chip::Transport::Type::kBle); + params.SetPeerAddress(addr).SetDiscriminator(discriminator); + return ToPyChipError(devCtrl->EstablishPASEConnection(nodeid, params)); +} + PyChipError pychip_DeviceController_Commission(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid) { CommissioningParameters params; @@ -644,6 +657,11 @@ PyChipError pychip_ExpireSessions(chip::Controller::DeviceCommissioner * devCtrl { VerifyOrReturnError((devCtrl != nullptr) && (devCtrl->SessionMgr() != nullptr), ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); + // + // Stop any active pairing sessions to this node. + // + devCtrl->StopPairing(nodeId); + // // Since we permit multiple controllers on the same fabric each associated with a different fabric index, expiring a session // needs to correctly expire sessions on other controllers on matching fabrics as well. diff --git a/src/controller/python/chip/ChipDeviceCtrl.py b/src/controller/python/chip/ChipDeviceCtrl.py index 9a492969feb84a..8178480742540a 100644 --- a/src/controller/python/chip/ChipDeviceCtrl.py +++ b/src/controller/python/chip/ChipDeviceCtrl.py @@ -416,6 +416,15 @@ def CloseSession(self, nodeid): self.devCtrl, nodeid) ).raise_on_error() + def EstablishPASESessionBLE(self, setupPinCode: int, discriminator: int, nodeid: int): + self.CheckIsActive() + + self.state = DCState.RENDEZVOUS_ONGOING + return self._ChipStack.CallAsync( + lambda: self._dmLib.pychip_DeviceController_EstablishPASESessionBLE( + self.devCtrl, setupPinCode, discriminator, nodeid) + ) + def EstablishPASESessionIP(self, ipaddr: str, setupPinCode: int, nodeid: int): self.CheckIsActive() @@ -1310,6 +1319,10 @@ def _InitLib(self): c_void_p, c_char_p, c_uint32, c_uint64] self._dmLib.pychip_DeviceController_EstablishPASESessionIP.restype = PyChipError + self._dmLib.pychip_DeviceController_EstablishPASESessionBLE.argtypes = [ + c_void_p, c_uint32, c_uint16, c_uint64] + self._dmLib.pychip_DeviceController_EstablishPASESessionBLE.restype = PyChipError + self._dmLib.pychip_DeviceController_DiscoverAllCommissionableNodes.argtypes = [ c_void_p] self._dmLib.pychip_DeviceController_DiscoverAllCommissionableNodes.restype = PyChipError