diff --git a/src/controller/python/BUILD.gn b/src/controller/python/BUILD.gn index 60988c5239d974..7031bdf08aa361 100644 --- a/src/controller/python/BUILD.gn +++ b/src/controller/python/BUILD.gn @@ -70,6 +70,7 @@ shared_library("ChipDeviceCtrl") { "chip/internal/ChipThreadWork.h", "chip/internal/CommissionerImpl.cpp", "chip/logging/LoggingRedirect.cpp", + "chip/native/PyChipError.cpp", "chip/utils/DeviceProxyUtils.cpp", ] } else { diff --git a/src/controller/python/ChipCommissionableNodeController-ScriptBinding.cpp b/src/controller/python/ChipCommissionableNodeController-ScriptBinding.cpp index 0fd3df5de89bec..12277183e52b6d 100644 --- a/src/controller/python/ChipCommissionableNodeController-ScriptBinding.cpp +++ b/src/controller/python/ChipCommissionableNodeController-ScriptBinding.cpp @@ -24,6 +24,7 @@ */ #include +#include #include #include #include @@ -33,29 +34,27 @@ using namespace chip; using namespace chip::Controller; -static_assert(std::is_same::value, "python assumes CHIP_ERROR maps to c_uint32"); - extern "C" { -ChipError::StorageType +PyChipError pychip_CommissionableNodeController_NewController(chip::Controller::CommissionableNodeController ** outCommissionableNodeCtrl); -ChipError::StorageType +PyChipError pychip_CommissionableNodeController_DeleteController(chip::Controller::CommissionableNodeController * commissionableNodeCtrl); -ChipError::StorageType +PyChipError pychip_CommissionableNodeController_DiscoverCommissioners(chip::Controller::CommissionableNodeController * commissionableNodeCtrl); void pychip_CommissionableNodeController_PrintDiscoveredCommissioners( chip::Controller::CommissionableNodeController * commissionableNodeCtrl); } -ChipError::StorageType +PyChipError pychip_CommissionableNodeController_NewController(chip::Controller::CommissionableNodeController ** outCommissionableNodeCtrl) { *outCommissionableNodeCtrl = new (std::nothrow) chip::Controller::CommissionableNodeController(); - VerifyOrReturnError(*outCommissionableNodeCtrl != nullptr, CHIP_ERROR_NO_MEMORY.AsInteger()); - return CHIP_NO_ERROR.AsInteger(); + VerifyOrReturnError(*outCommissionableNodeCtrl != nullptr, ToPyChipError(CHIP_ERROR_NO_MEMORY)); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType +PyChipError pychip_CommissionableNodeController_DeleteController(chip::Controller::CommissionableNodeController * commissionableNodeCtrl) { if (commissionableNodeCtrl != nullptr) @@ -63,13 +62,13 @@ pychip_CommissionableNodeController_DeleteController(chip::Controller::Commissio delete commissionableNodeCtrl; } - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType +PyChipError pychip_CommissionableNodeController_DiscoverCommissioners(chip::Controller::CommissionableNodeController * commissionableNodeCtrl) { - return commissionableNodeCtrl->DiscoverCommissioners().AsInteger(); + return ToPyChipError(commissionableNodeCtrl->DiscoverCommissioners()); } void pychip_CommissionableNodeController_PrintDiscoveredCommissioners( diff --git a/src/controller/python/ChipDeviceController-Discovery.cpp b/src/controller/python/ChipDeviceController-Discovery.cpp index f4aaaed81bdf49..36da3a8eedb0d1 100644 --- a/src/controller/python/ChipDeviceController-Discovery.cpp +++ b/src/controller/python/ChipDeviceController-Discovery.cpp @@ -24,6 +24,7 @@ */ #include +#include #include #include #include @@ -49,8 +50,8 @@ bool pychip_DeviceController_HasDiscoveredCommissionableNode(Controller::DeviceC return false; } -ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodes(Controller::DeviceCommissioner * devCtrl, - const uint8_t filterType, const char * filterParam) +PyChipError pychip_DeviceController_DiscoverCommissionableNodes(Controller::DeviceCommissioner * devCtrl, const uint8_t filterType, + const char * filterParam) { Dnssd::DiscoveryFilter filter(static_cast(filterType)); switch (static_cast(filterType)) @@ -67,7 +68,7 @@ ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodes(Contr unsigned long long int numericalArg = strtoull(filterParam, nullptr, 0); if ((numericalArg == ULLONG_MAX) && (errno == ERANGE)) { - return CHIP_ERROR_INVALID_ARGUMENT.AsInteger(); + return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); } filter.code = static_cast(numericalArg); break; @@ -82,10 +83,10 @@ ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodes(Contr filter.instanceName = filterParam; break; default: - return CHIP_ERROR_INVALID_ARGUMENT.AsInteger(); + return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); } - return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); + return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); } void pychip_DeviceController_IterateDiscoveredCommissionableNodes(Controller::DeviceCommissioner * devCtrl, diff --git a/src/controller/python/ChipDeviceController-IssueNocChain.cpp b/src/controller/python/ChipDeviceController-IssueNocChain.cpp index 39a3952e0fb0f7..fe5b8ecc3919b0 100644 --- a/src/controller/python/ChipDeviceController-IssueNocChain.cpp +++ b/src/controller/python/ChipDeviceController-IssueNocChain.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -33,7 +34,7 @@ using namespace chip; extern "C" { typedef void (*pychip_DeviceController_IssueNOCChainCallbackPythonCallback)( - PyObject * context, ChipError::StorageType status, const uint8_t * noc, size_t nocLen, const uint8_t * icac, size_t icacLen, + PyObject * context, PyChipError status, const uint8_t * noc, size_t nocLen, const uint8_t * icac, size_t icacLen, const uint8_t * rcac, size_t rcacLen, const uint8_t * ipk, size_t ipkLen, NodeId adminSubject); static pychip_DeviceController_IssueNOCChainCallbackPythonCallback pychip_DeviceController_IssueNOCChainCallbackPythonCallbackFunct; @@ -44,9 +45,8 @@ void pychip_DeviceController_SetIssueNOCChainCallbackPythonCallback( pychip_DeviceController_IssueNOCChainCallbackPythonCallbackFunct = callback; } -ChipError::StorageType pychip_DeviceController_IssueNOCChain(chip::Controller::DeviceCommissioner * devCtrl, - PyObject * pythonContext, uint8_t * NOCSRElements, - size_t NOCSRElementsLen, NodeId nodeId); +PyChipError pychip_DeviceController_IssueNOCChain(chip::Controller::DeviceCommissioner * devCtrl, PyObject * pythonContext, + uint8_t * NOCSRElements, size_t NOCSRElementsLen, NodeId nodeId); } void pychip_DeviceController_IssueNOCChainCallback(void * context, CHIP_ERROR status, const ByteSpan & noc, const ByteSpan & icac, @@ -90,25 +90,22 @@ void pychip_DeviceController_IssueNOCChainCallback(void * context, CHIP_ERROR st if (err == CHIP_NO_ERROR) { pychip_DeviceController_IssueNOCChainCallbackPythonCallbackFunct( - context, err.AsInteger(), chipNocSpan.data(), chipNocSpan.size(), chipIcacSpan.data(), chipIcacSpan.size(), + context, ToPyChipError(err), chipNocSpan.data(), chipNocSpan.size(), chipIcacSpan.data(), chipIcacSpan.size(), chipRcacSpan.data(), chipRcacSpan.size(), ipkData.data(), ipk.HasValue() ? ipkData.size() : 0, adminSubject.ValueOr(kUndefinedNodeId)); } else { - pychip_DeviceController_IssueNOCChainCallbackPythonCallbackFunct(context, err.AsInteger(), nullptr, 0, nullptr, 0, nullptr, - 0, nullptr, 0, 0); + pychip_DeviceController_IssueNOCChainCallbackPythonCallbackFunct(context, ToPyChipError(err), nullptr, 0, nullptr, 0, + nullptr, 0, nullptr, 0, 0); } } -ChipError::StorageType pychip_DeviceController_IssueNOCChain(chip::Controller::DeviceCommissioner * devCtrl, - PyObject * pythonContext, uint8_t * NOCSRElements, - size_t NOCSRElementsLen, NodeId nodeId) +PyChipError pychip_DeviceController_IssueNOCChain(chip::Controller::DeviceCommissioner * devCtrl, PyObject * pythonContext, + uint8_t * NOCSRElements, size_t NOCSRElementsLen, NodeId nodeId) { - return devCtrl - ->IssueNOCChain( - ByteSpan(NOCSRElements, NOCSRElementsLen), nodeId, - /* Note: Memory leak here. This is a quick and a bit dirty PoC */ - new Callback::Callback(pychip_DeviceController_IssueNOCChainCallback, pythonContext)) - .AsInteger(); + return ToPyChipError(devCtrl->IssueNOCChain( + ByteSpan(NOCSRElements, NOCSRElementsLen), nodeId, + /* Note: Memory leak here. This is a quick and a bit dirty PoC */ + new Callback::Callback(pychip_DeviceController_IssueNOCChainCallback, pythonContext))); } diff --git a/src/controller/python/ChipDeviceController-ScriptBinding.cpp b/src/controller/python/ChipDeviceController-ScriptBinding.cpp index 05e0abf8b04585..a7659e60d21f4b 100644 --- a/src/controller/python/ChipDeviceController-ScriptBinding.cpp +++ b/src/controller/python/ChipDeviceController-ScriptBinding.cpp @@ -54,6 +54,7 @@ #include #include #include +#include #include #include @@ -82,12 +83,10 @@ using namespace chip::Controller; using namespace chip::Credentials; using namespace chip::DeviceLayer; -static_assert(std::is_same::value, "python assumes CHIP_ERROR maps to c_uint32"); - extern "C" { typedef void (*ConstructBytesArrayFunct)(const uint8_t * dataBuf, uint32_t dataLen); typedef void (*LogMessageFunct)(uint64_t time, uint64_t timeUS, const char * moduleName, uint8_t category, const char * msg); -typedef void (*DeviceAvailableFunc)(DeviceProxy * device, ChipError::StorageType err); +typedef void (*DeviceAvailableFunc)(DeviceProxy * device, PyChipError err); typedef void (*ChipThreadTaskRunnerFunct)(intptr_t context); } @@ -111,79 +110,71 @@ chip::NodeId kDefaultLocalDeviceId = chip::kTestControllerNodeId; chip::NodeId kRemoteDeviceId = chip::kTestDeviceNodeId; extern "C" { -ChipError::StorageType pychip_DeviceController_StackInit(Controller::Python::StorageAdapter * storageAdapter, - bool enableServerInteractions); -ChipError::StorageType pychip_DeviceController_StackShutdown(); - -ChipError::StorageType pychip_DeviceController_NewDeviceController(chip::Controller::DeviceCommissioner ** outDevCtrl, - chip::NodeId localDeviceId, bool useTestCommissioner); -ChipError::StorageType pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl); -ChipError::StorageType pychip_DeviceController_GetAddressAndPort(chip::Controller::DeviceCommissioner * devCtrl, - chip::NodeId nodeId, char * outAddress, uint64_t maxAddressLen, - uint16_t * outPort); -ChipError::StorageType pychip_DeviceController_GetCompressedFabricId(chip::Controller::DeviceCommissioner * devCtrl, - uint64_t * outFabricId); -ChipError::StorageType pychip_DeviceController_GetFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId); -ChipError::StorageType pychip_DeviceController_GetNodeId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outNodeId); +PyChipError pychip_DeviceController_StackInit(Controller::Python::StorageAdapter * storageAdapter, bool enableServerInteractions); +PyChipError pychip_DeviceController_StackShutdown(); + +PyChipError pychip_DeviceController_NewDeviceController(chip::Controller::DeviceCommissioner ** outDevCtrl, + chip::NodeId localDeviceId, bool useTestCommissioner); +PyChipError pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl); +PyChipError pychip_DeviceController_GetAddressAndPort(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, + char * outAddress, uint64_t maxAddressLen, uint16_t * outPort); +PyChipError pychip_DeviceController_GetCompressedFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId); +PyChipError pychip_DeviceController_GetFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId); +PyChipError pychip_DeviceController_GetNodeId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outNodeId); // Rendezvous -ChipError::StorageType pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator, - uint32_t setupPINCode, chip::NodeId nodeid); -ChipError::StorageType pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr, - uint32_t setupPINCode, chip::NodeId nodeid); -ChipError::StorageType pychip_DeviceController_ConnectWithCode(chip::Controller::DeviceCommissioner * devCtrl, - const char * onboardingPayload, chip::NodeId nodeid); -ChipError::StorageType pychip_DeviceController_SetThreadOperationalDataset(const char * threadOperationalDataset, uint32_t size); -ChipError::StorageType pychip_DeviceController_SetWiFiCredentials(const char * ssid, const char * credentials); -ChipError::StorageType pychip_DeviceController_CloseSession(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid); -ChipError::StorageType pychip_DeviceController_EstablishPASESessionIP(chip::Controller::DeviceCommissioner * devCtrl, - const char * peerAddrStr, uint32_t setupPINCode, - chip::NodeId nodeid); -ChipError::StorageType pychip_DeviceController_Commission(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid); - -ChipError::StorageType -pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, - uint16_t long_discriminator); -ChipError::StorageType pychip_DeviceController_DiscoverAllCommissionableNodes(chip::Controller::DeviceCommissioner * devCtrl); - -ChipError::StorageType -pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, - uint16_t short_discriminator); -ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodesVendor(chip::Controller::DeviceCommissioner * devCtrl, - uint16_t vendor); -ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodesDeviceType(chip::Controller::DeviceCommissioner * devCtrl, - uint16_t device_type); -ChipError::StorageType -pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled(chip::Controller::DeviceCommissioner * devCtrl); - -ChipError::StorageType pychip_DeviceController_OnNetworkCommission(chip::Controller::DeviceCommissioner * devCtrl, uint64_t nodeId, - uint32_t setupPasscode, const uint8_t filterType, - const char * filterParam); - -ChipError::StorageType pychip_DeviceController_PostTaskOnChipThread(ChipThreadTaskRunnerFunct callback, void * pythonContext); - -ChipError::StorageType pychip_DeviceController_OpenCommissioningWindow(chip::Controller::DeviceCommissioner * devCtrl, - chip::NodeId nodeid, uint16_t timeout, uint32_t iteration, - uint16_t discriminator, uint8_t optionInt); +PyChipError pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator, + uint32_t setupPINCode, chip::NodeId nodeid); +PyChipError pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr, + uint32_t setupPINCode, chip::NodeId nodeid); +PyChipError pychip_DeviceController_ConnectWithCode(chip::Controller::DeviceCommissioner * devCtrl, const char * onboardingPayload, + chip::NodeId nodeid); +PyChipError pychip_DeviceController_SetThreadOperationalDataset(const char * threadOperationalDataset, uint32_t size); +PyChipError pychip_DeviceController_SetWiFiCredentials(const char * ssid, const char * credentials); +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_Commission(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid); + +PyChipError pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, + uint16_t long_discriminator); +PyChipError pychip_DeviceController_DiscoverAllCommissionableNodes(chip::Controller::DeviceCommissioner * devCtrl); + +PyChipError pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, + uint16_t short_discriminator); +PyChipError pychip_DeviceController_DiscoverCommissionableNodesVendor(chip::Controller::DeviceCommissioner * devCtrl, + uint16_t vendor); +PyChipError pychip_DeviceController_DiscoverCommissionableNodesDeviceType(chip::Controller::DeviceCommissioner * devCtrl, + uint16_t device_type); +PyChipError pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled(chip::Controller::DeviceCommissioner * devCtrl); + +PyChipError pychip_DeviceController_OnNetworkCommission(chip::Controller::DeviceCommissioner * devCtrl, uint64_t nodeId, + uint32_t setupPasscode, const uint8_t filterType, const char * filterParam); + +PyChipError pychip_DeviceController_PostTaskOnChipThread(ChipThreadTaskRunnerFunct callback, void * pythonContext); + +PyChipError pychip_DeviceController_OpenCommissioningWindow(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid, + uint16_t timeout, uint32_t iteration, uint16_t discriminator, + uint8_t optionInt); void pychip_DeviceController_PrintDiscoveredDevices(chip::Controller::DeviceCommissioner * devCtrl); bool pychip_DeviceController_GetIPForDiscoveredDevice(chip::Controller::DeviceCommissioner * devCtrl, int idx, char * addrStr, uint32_t len); // Pairing Delegate -ChipError::StorageType +PyChipError pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback(chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnPairingCompleteFunct callback); -ChipError::StorageType pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback( +PyChipError pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback( chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnCommissioningCompleteFunct callback); -ChipError::StorageType pychip_ScriptDevicePairingDelegate_SetCommissioningStatusUpdateCallback( +PyChipError pychip_ScriptDevicePairingDelegate_SetCommissioningStatusUpdateCallback( chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnCommissioningStatusUpdateFunct callback); // BLE -ChipError::StorageType pychip_DeviceCommissioner_CloseBleConnection(chip::Controller::DeviceCommissioner * devCtrl); +PyChipError pychip_DeviceCommissioner_CloseBleConnection(chip::Controller::DeviceCommissioner * devCtrl); uint8_t pychip_DeviceController_GetLogFilter(); void pychip_DeviceController_SetLogFilter(uint8_t category); @@ -192,18 +183,18 @@ const char * pychip_Stack_ErrorToString(ChipError::StorageType err); const char * pychip_Stack_StatusReportToString(uint32_t profileId, uint16_t statusCode); void pychip_Stack_SetLogFunct(LogMessageFunct logFunct); -ChipError::StorageType pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, - DeviceAvailableFunc callback); -ChipError::StorageType pychip_FreeOperationalDeviceProxy(chip::OperationalDeviceProxy * deviceProxy); -ChipError::StorageType pychip_GetLocalSessionId(chip::OperationalDeviceProxy * deviceProxy, uint16_t * localSessionId); -ChipError::StorageType pychip_GetNumSessionsToPeer(chip::OperationalDeviceProxy * deviceProxy, uint32_t * numSessions); -ChipError::StorageType pychip_GetDeviceBeingCommissioned(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, - CommissioneeDeviceProxy ** proxy); -ChipError::StorageType pychip_ExpireSessions(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId); +PyChipError pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, + DeviceAvailableFunc callback); +PyChipError pychip_FreeOperationalDeviceProxy(chip::OperationalDeviceProxy * deviceProxy); +PyChipError pychip_GetLocalSessionId(chip::OperationalDeviceProxy * deviceProxy, uint16_t * localSessionId); +PyChipError pychip_GetNumSessionsToPeer(chip::OperationalDeviceProxy * deviceProxy, uint32_t * numSessions); +PyChipError pychip_GetDeviceBeingCommissioned(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, + CommissioneeDeviceProxy ** proxy); +PyChipError pychip_ExpireSessions(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId); uint64_t pychip_GetCommandSenderHandle(chip::DeviceProxy * device); -chip::ChipError::StorageType pychip_InteractionModel_ShutdownSubscription(SubscriptionId subscriptionId); +PyChipError pychip_InteractionModel_ShutdownSubscription(SubscriptionId subscriptionId); // // Storage @@ -229,8 +220,7 @@ void pychip_Storage_ShutdownAdapter(chip::Controller::Python::StorageAdapter * s delete storageAdapter; } -ChipError::StorageType pychip_DeviceController_StackInit(Controller::Python::StorageAdapter * storageAdapter, - bool enableServerInteractions) +PyChipError pychip_DeviceController_StackInit(Controller::Python::StorageAdapter * storageAdapter, bool enableServerInteractions) { VerifyOrDie(storageAdapter != nullptr); @@ -239,10 +229,10 @@ ChipError::StorageType pychip_DeviceController_StackInit(Controller::Python::Sto factoryParams.fabricIndependentStorage = storageAdapter; sGroupDataProvider.SetStorageDelegate(storageAdapter); - ReturnErrorOnFailure(sGroupDataProvider.Init().AsInteger()); + PyReturnErrorOnFailure(ToPyChipError(sGroupDataProvider.Init())); factoryParams.groupDataProvider = &sGroupDataProvider; - ReturnErrorOnFailure(sPersistentStorageOpCertStore.Init(storageAdapter).AsInteger()); + PyReturnErrorOnFailure(ToPyChipError(sPersistentStorageOpCertStore.Init(storageAdapter))); factoryParams.opCertStore = &sPersistentStorageOpCertStore; factoryParams.enableServerInteractions = enableServerInteractions; @@ -253,7 +243,7 @@ ChipError::StorageType pychip_DeviceController_StackInit(Controller::Python::Sto static chip::DeviceLayer::TestOnlyCommissionableDataProvider TestOnlyCommissionableDataProvider; chip::DeviceLayer::SetCommissionableDataProvider(&TestOnlyCommissionableDataProvider); - ReturnErrorOnFailure(DeviceControllerFactory::GetInstance().Init(factoryParams).AsInteger()); + PyReturnErrorOnFailure(ToPyChipError(DeviceControllerFactory::GetInstance().Init(factoryParams))); // // In situations where all the controller instances get shutdown, the entire stack is then also @@ -270,19 +260,19 @@ ChipError::StorageType pychip_DeviceController_StackInit(Controller::Python::Sto // Finally, start up the main Matter thread. Any further interactions with the stack // will now need to happen on the Matter thread, OR protected with the stack lock. // - ReturnErrorOnFailure(chip::DeviceLayer::PlatformMgr().StartEventLoopTask().AsInteger()); + PyReturnErrorOnFailure(ToPyChipError(chip::DeviceLayer::PlatformMgr().StartEventLoopTask())); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_DeviceController_StackShutdown() +PyChipError pychip_DeviceController_StackShutdown() { ChipLogError(Controller, "Shutting down the stack..."); // // Let's stop the Matter thread, and wait till the event loop has stopped. // - ReturnErrorOnFailure(chip::DeviceLayer::PlatformMgr().StopEventLoopTask().AsInteger()); + PyReturnErrorOnFailure(ToPyChipError(chip::DeviceLayer::PlatformMgr().StopEventLoopTask())); // // There is the symmetric call to match the Retain called at stack initialization @@ -292,38 +282,36 @@ ChipError::StorageType pychip_DeviceController_StackShutdown() DeviceControllerFactory::GetInstance().Shutdown(); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_DeviceController_GetAddressAndPort(chip::Controller::DeviceCommissioner * devCtrl, - chip::NodeId nodeId, char * outAddress, uint64_t maxAddressLen, - uint16_t * outPort) +PyChipError pychip_DeviceController_GetAddressAndPort(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, + char * outAddress, uint64_t maxAddressLen, uint16_t * outPort) { Inet::IPAddress address; - ReturnErrorOnFailure(devCtrl->GetPeerAddressAndPort(nodeId, address, *outPort).AsInteger()); + PyReturnErrorOnFailure(ToPyChipError(devCtrl->GetPeerAddressAndPort(nodeId, address, *outPort))); VerifyOrReturnError(address.ToString(outAddress, static_cast(maxAddressLen)), - CHIP_ERROR_BUFFER_TOO_SMALL.AsInteger()); + ToPyChipError(CHIP_ERROR_BUFFER_TOO_SMALL)); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_DeviceController_GetCompressedFabricId(chip::Controller::DeviceCommissioner * devCtrl, - uint64_t * outFabricId) +PyChipError pychip_DeviceController_GetCompressedFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId) { *outFabricId = devCtrl->GetCompressedFabricId(); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_DeviceController_GetFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId) +PyChipError pychip_DeviceController_GetFabricId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outFabricId) { *outFabricId = devCtrl->GetFabricId(); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_DeviceController_GetNodeId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outNodeId) +PyChipError pychip_DeviceController_GetNodeId(chip::Controller::DeviceCommissioner * devCtrl, uint64_t * outNodeId) { *outNodeId = devCtrl->GetNodeId(); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } const char * pychip_DeviceController_ErrorToString(ChipError::StorageType err) @@ -353,44 +341,41 @@ void pychip_DeviceController_SetLogFilter(uint8_t category) #endif } -ChipError::StorageType pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator, - uint32_t setupPINCode, chip::NodeId nodeid) +PyChipError pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator, + uint32_t setupPINCode, chip::NodeId nodeid) { - return devCtrl - ->PairDevice(nodeid, - chip::RendezvousParameters() - .SetPeerAddress(Transport::PeerAddress(Transport::Type::kBle)) - .SetSetupPINCode(setupPINCode) - .SetDiscriminator(discriminator), - sCommissioningParameters) - .AsInteger(); + return ToPyChipError(devCtrl->PairDevice(nodeid, + chip::RendezvousParameters() + .SetPeerAddress(Transport::PeerAddress(Transport::Type::kBle)) + .SetSetupPINCode(setupPINCode) + .SetDiscriminator(discriminator), + sCommissioningParameters)); } -ChipError::StorageType pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr, - uint32_t setupPINCode, chip::NodeId nodeid) +PyChipError pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr, + uint32_t setupPINCode, chip::NodeId nodeid) { chip::Inet::IPAddress peerAddr; chip::Transport::PeerAddress addr; chip::RendezvousParameters params = chip::RendezvousParameters().SetSetupPINCode(setupPINCode); - VerifyOrReturnError(chip::Inet::IPAddress::FromString(peerAddrStr, peerAddr), CHIP_ERROR_INVALID_ARGUMENT.AsInteger()); + VerifyOrReturnError(chip::Inet::IPAddress::FromString(peerAddrStr, peerAddr), ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); // TODO: IP rendezvous should use TCP connection. addr.SetTransportType(chip::Transport::Type::kUdp).SetIPAddress(peerAddr); params.SetPeerAddress(addr).SetDiscriminator(0); - return devCtrl->PairDevice(nodeid, params, sCommissioningParameters).AsInteger(); + return ToPyChipError(devCtrl->PairDevice(nodeid, params, sCommissioningParameters)); } -ChipError::StorageType pychip_DeviceController_ConnectWithCode(chip::Controller::DeviceCommissioner * devCtrl, - const char * onboardingPayload, chip::NodeId nodeid) +PyChipError pychip_DeviceController_ConnectWithCode(chip::Controller::DeviceCommissioner * devCtrl, const char * onboardingPayload, + chip::NodeId nodeid) { - return devCtrl->PairDevice(nodeid, onboardingPayload, sCommissioningParameters).AsInteger(); + return ToPyChipError(devCtrl->PairDevice(nodeid, onboardingPayload, sCommissioningParameters)); } -ChipError::StorageType pychip_DeviceController_OnNetworkCommission(chip::Controller::DeviceCommissioner * devCtrl, uint64_t nodeId, - uint32_t setupPasscode, const uint8_t filterType, - const char * filterParam) +PyChipError pychip_DeviceController_OnNetworkCommission(chip::Controller::DeviceCommissioner * devCtrl, uint64_t nodeId, + uint32_t setupPasscode, const uint8_t filterType, const char * filterParam) { Dnssd::DiscoveryFilter filter(static_cast(filterType)); switch (static_cast(filterType)) @@ -407,7 +392,7 @@ ChipError::StorageType pychip_DeviceController_OnNetworkCommission(chip::Control unsigned long long int numericalArg = strtoull(filterParam, nullptr, 0); if ((numericalArg == ULLONG_MAX) && (errno == ERANGE)) { - return CHIP_ERROR_INVALID_ARGUMENT.AsInteger(); + return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); } filter.code = static_cast(numericalArg); break; @@ -422,37 +407,37 @@ ChipError::StorageType pychip_DeviceController_OnNetworkCommission(chip::Control filter.instanceName = filterParam; break; default: - return CHIP_ERROR_INVALID_ARGUMENT.AsInteger(); + return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); } sPairingDeviceDiscoveryDelegate.Init(nodeId, setupPasscode, sCommissioningParameters, &sPairingDelegate, devCtrl); devCtrl->RegisterDeviceDiscoveryDelegate(&sPairingDeviceDiscoveryDelegate); - return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); + return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); } -ChipError::StorageType pychip_DeviceController_SetThreadOperationalDataset(const char * threadOperationalDataset, uint32_t size) +PyChipError pychip_DeviceController_SetThreadOperationalDataset(const char * threadOperationalDataset, uint32_t size) { - ReturnErrorCodeIf(!sThreadBuf.Alloc(size), CHIP_ERROR_NO_MEMORY.AsInteger()); + ReturnErrorCodeIf(!sThreadBuf.Alloc(size), ToPyChipError(CHIP_ERROR_NO_MEMORY)); memcpy(sThreadBuf.Get(), threadOperationalDataset, size); sCommissioningParameters.SetThreadOperationalDataset(ByteSpan(sThreadBuf.Get(), size)); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_DeviceController_SetWiFiCredentials(const char * ssid, const char * credentials) +PyChipError pychip_DeviceController_SetWiFiCredentials(const char * ssid, const char * credentials) { size_t ssidSize = strlen(ssid); - ReturnErrorCodeIf(!sSsidBuf.Alloc(ssidSize), CHIP_ERROR_NO_MEMORY.AsInteger()); + ReturnErrorCodeIf(!sSsidBuf.Alloc(ssidSize), ToPyChipError(CHIP_ERROR_NO_MEMORY)); memcpy(sSsidBuf.Get(), ssid, ssidSize); size_t credsSize = strlen(credentials); - ReturnErrorCodeIf(!sCredsBuf.Alloc(credsSize), CHIP_ERROR_NO_MEMORY.AsInteger()); + ReturnErrorCodeIf(!sCredsBuf.Alloc(credsSize), ToPyChipError(CHIP_ERROR_NO_MEMORY)); memcpy(sCredsBuf.Get(), credentials, credsSize); sCommissioningParameters.SetWiFiCredentials( chip::Controller::WiFiCredentials(ByteSpan(sSsidBuf.Get(), ssidSize), ByteSpan(sCredsBuf.Get(), credsSize))); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_DeviceController_CloseSession(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid) +PyChipError pychip_DeviceController_CloseSession(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid) { // // Since we permit multiple controllers per fabric and each is associated with a unique fabric index, closing a session @@ -466,115 +451,108 @@ ChipError::StorageType pychip_DeviceController_CloseSession(chip::Controller::De } }); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_DeviceController_EstablishPASESessionIP(chip::Controller::DeviceCommissioner * devCtrl, - const char * peerAddrStr, uint32_t setupPINCode, - chip::NodeId nodeid) +PyChipError pychip_DeviceController_EstablishPASESessionIP(chip::Controller::DeviceCommissioner * devCtrl, const char * peerAddrStr, + uint32_t setupPINCode, chip::NodeId nodeid) { chip::Inet::IPAddress peerAddr; chip::Transport::PeerAddress addr; RendezvousParameters params = chip::RendezvousParameters().SetSetupPINCode(setupPINCode); - VerifyOrReturnError(chip::Inet::IPAddress::FromString(peerAddrStr, peerAddr), CHIP_ERROR_INVALID_ARGUMENT.AsInteger()); + VerifyOrReturnError(chip::Inet::IPAddress::FromString(peerAddrStr, peerAddr), ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); addr.SetTransportType(chip::Transport::Type::kUdp).SetIPAddress(peerAddr); params.SetPeerAddress(addr).SetDiscriminator(0); - return devCtrl->EstablishPASEConnection(nodeid, params).AsInteger(); + return ToPyChipError(devCtrl->EstablishPASEConnection(nodeid, params)); } -ChipError::StorageType pychip_DeviceController_Commission(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid) +PyChipError pychip_DeviceController_Commission(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid) { CommissioningParameters params; - return devCtrl->Commission(nodeid, params).AsInteger(); + return ToPyChipError(devCtrl->Commission(nodeid, params)); } -ChipError::StorageType pychip_DeviceController_DiscoverAllCommissionableNodes(chip::Controller::DeviceCommissioner * devCtrl) +PyChipError pychip_DeviceController_DiscoverAllCommissionableNodes(chip::Controller::DeviceCommissioner * devCtrl) { Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kNone, static_cast(0)); - return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); + return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); } -ChipError::StorageType -pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, - uint16_t long_discriminator) +PyChipError pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, + uint16_t long_discriminator) { Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kLongDiscriminator, long_discriminator); - return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); + return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); } -ChipError::StorageType -pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, - uint16_t short_discriminator) +PyChipError pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator(chip::Controller::DeviceCommissioner * devCtrl, + uint16_t short_discriminator) { Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kShortDiscriminator, short_discriminator); - return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); + return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); } -ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodesVendor(chip::Controller::DeviceCommissioner * devCtrl, - uint16_t vendor) +PyChipError pychip_DeviceController_DiscoverCommissionableNodesVendor(chip::Controller::DeviceCommissioner * devCtrl, + uint16_t vendor) { Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kVendorId, vendor); - return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); + return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); } -ChipError::StorageType pychip_DeviceController_DiscoverCommissionableNodesDeviceType(chip::Controller::DeviceCommissioner * devCtrl, - uint16_t device_type) +PyChipError pychip_DeviceController_DiscoverCommissionableNodesDeviceType(chip::Controller::DeviceCommissioner * devCtrl, + uint16_t device_type) { Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kDeviceType, device_type); - return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); + return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); } -ChipError::StorageType -pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled(chip::Controller::DeviceCommissioner * devCtrl) +PyChipError pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled(chip::Controller::DeviceCommissioner * devCtrl) { Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kCommissioningMode); - return devCtrl->DiscoverCommissionableNodes(filter).AsInteger(); + return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); } -ChipError::StorageType pychip_DeviceController_OpenCommissioningWindow(chip::Controller::DeviceCommissioner * devCtrl, - chip::NodeId nodeid, uint16_t timeout, uint32_t iteration, - uint16_t discriminator, uint8_t optionInt) +PyChipError pychip_DeviceController_OpenCommissioningWindow(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeid, + uint16_t timeout, uint32_t iteration, uint16_t discriminator, + uint8_t optionInt) { const auto option = static_cast(optionInt); if (option == Controller::CommissioningWindowOpener::CommissioningWindowOption::kOriginalSetupCode) { - return Controller::AutoCommissioningWindowOpener::OpenBasicCommissioningWindow(devCtrl, nodeid, - System::Clock::Seconds16(timeout)) - .AsInteger(); + return ToPyChipError(Controller::AutoCommissioningWindowOpener::OpenBasicCommissioningWindow( + devCtrl, nodeid, System::Clock::Seconds16(timeout))); } if (option == Controller::CommissioningWindowOpener::CommissioningWindowOption::kTokenWithRandomPIN) { SetupPayload payload; - return Controller::AutoCommissioningWindowOpener::OpenCommissioningWindow( - devCtrl, nodeid, System::Clock::Seconds16(timeout), iteration, discriminator, NullOptional, NullOptional, - payload) - .AsInteger(); + return ToPyChipError(Controller::AutoCommissioningWindowOpener::OpenCommissioningWindow( + devCtrl, nodeid, System::Clock::Seconds16(timeout), iteration, discriminator, NullOptional, NullOptional, payload)); } - return CHIP_ERROR_INVALID_ARGUMENT.AsInteger(); + return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); } -ChipError::StorageType +PyChipError pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback(chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnPairingCompleteFunct callback) { sPairingDelegate.SetKeyExchangeCallback(callback); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback( +PyChipError pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback( chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnCommissioningCompleteFunct callback) { sPairingDelegate.SetCommissioningCompleteCallback(callback); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_ScriptDevicePairingDelegate_SetCommissioningStatusUpdateCallback( +PyChipError pychip_ScriptDevicePairingDelegate_SetCommissioningStatusUpdateCallback( chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnCommissioningStatusUpdateFunct callback) { sPairingDelegate.SetCommissioningStatusUpdateCallback(callback); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } const char * pychip_Stack_ErrorToString(ChipError::StorageType err) @@ -600,14 +578,14 @@ struct GetDeviceCallbacks { auto * self = static_cast(context); auto * operationalDeviceProxy = new OperationalDeviceProxy(&exchangeMgr, sessionHandle); - self->mCallback(operationalDeviceProxy, CHIP_NO_ERROR.AsInteger()); + self->mCallback(operationalDeviceProxy, ToPyChipError(CHIP_NO_ERROR)); delete self; } static void OnConnectionFailureFn(void * context, const ScopedNodeId & peerId, CHIP_ERROR error) { auto * self = static_cast(context); - self->mCallback(nullptr, error.AsInteger()); + self->mCallback(nullptr, ToPyChipError(error)); delete self; } @@ -617,70 +595,70 @@ struct GetDeviceCallbacks }; } // anonymous namespace -ChipError::StorageType pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, - DeviceAvailableFunc callback) +PyChipError pychip_GetConnectedDeviceByNodeId(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, + DeviceAvailableFunc callback) { - VerifyOrReturnError(devCtrl != nullptr, CHIP_ERROR_INVALID_ARGUMENT.AsInteger()); + VerifyOrReturnError(devCtrl != nullptr, ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); auto * callbacks = new GetDeviceCallbacks(callback); - return devCtrl->GetConnectedDevice(nodeId, &callbacks->mOnSuccess, &callbacks->mOnFailure).AsInteger(); + return ToPyChipError(devCtrl->GetConnectedDevice(nodeId, &callbacks->mOnSuccess, &callbacks->mOnFailure)); } -ChipError::StorageType pychip_FreeOperationalDeviceProxy(chip::OperationalDeviceProxy * deviceProxy) +PyChipError pychip_FreeOperationalDeviceProxy(chip::OperationalDeviceProxy * deviceProxy) { if (deviceProxy != nullptr) { delete deviceProxy; } - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_GetLocalSessionId(chip::OperationalDeviceProxy * deviceProxy, uint16_t * localSessionId) +PyChipError pychip_GetLocalSessionId(chip::OperationalDeviceProxy * deviceProxy, uint16_t * localSessionId) { - VerifyOrReturnError(deviceProxy->GetSecureSession().HasValue(), CHIP_ERROR_MISSING_SECURE_SESSION.AsInteger()); - VerifyOrReturnError(localSessionId != nullptr, CHIP_ERROR_INVALID_ARGUMENT.AsInteger()); + VerifyOrReturnError(deviceProxy->GetSecureSession().HasValue(), ToPyChipError(CHIP_ERROR_MISSING_SECURE_SESSION)); + VerifyOrReturnError(localSessionId != nullptr, ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); *localSessionId = deviceProxy->GetSecureSession().Value()->AsSecureSession()->GetLocalSessionId(); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_GetNumSessionsToPeer(chip::OperationalDeviceProxy * deviceProxy, uint32_t * numSessions) +PyChipError pychip_GetNumSessionsToPeer(chip::OperationalDeviceProxy * deviceProxy, uint32_t * numSessions) { - VerifyOrReturnError(deviceProxy->GetSecureSession().HasValue(), CHIP_ERROR_MISSING_SECURE_SESSION.AsInteger()); - VerifyOrReturnError(numSessions != nullptr, CHIP_ERROR_INVALID_ARGUMENT.AsInteger()); + VerifyOrReturnError(deviceProxy->GetSecureSession().HasValue(), ToPyChipError(CHIP_ERROR_MISSING_SECURE_SESSION)); + VerifyOrReturnError(numSessions != nullptr, ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); *numSessions = 0; deviceProxy->GetExchangeManager()->GetSessionManager()->ForEachMatchingSession( deviceProxy->GetPeerScopedNodeId(), [numSessions](auto * session) { (*numSessions)++; }); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_GetDeviceBeingCommissioned(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, - CommissioneeDeviceProxy ** proxy) +PyChipError pychip_GetDeviceBeingCommissioned(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, + CommissioneeDeviceProxy ** proxy) { - return devCtrl->GetDeviceBeingCommissioned(nodeId, proxy).AsInteger(); + return ToPyChipError(devCtrl->GetDeviceBeingCommissioned(nodeId, proxy)); } // This is a method called VERY seldom, just for RemoveFabric/UpdateNOC -ChipError::StorageType pychip_ExpireSessions(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId) +PyChipError pychip_ExpireSessions(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId) { - VerifyOrReturnError((devCtrl != nullptr) && (devCtrl->SessionMgr() != nullptr), CHIP_ERROR_INVALID_ARGUMENT.AsInteger()); + VerifyOrReturnError((devCtrl != nullptr) && (devCtrl->SessionMgr() != nullptr), ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); // // 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. // devCtrl->SessionMgr()->ExpireAllSessionsOnLogicalFabric(ScopedNodeId(nodeId, devCtrl->GetFabricIndex())); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_DeviceCommissioner_CloseBleConnection(chip::Controller::DeviceCommissioner * devCtrl) +PyChipError pychip_DeviceCommissioner_CloseBleConnection(chip::Controller::DeviceCommissioner * devCtrl) { #if CONFIG_NETWORK_LAYER_BLE devCtrl->CloseBleConnection(); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); #else - return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE.AsInteger(); + return ToPyChipError(CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE); #endif } @@ -700,12 +678,12 @@ void pychip_Stack_SetLogFunct(LogMessageFunct logFunct) // like using the log module. } -ChipError::StorageType pychip_DeviceController_PostTaskOnChipThread(ChipThreadTaskRunnerFunct callback, void * pythonContext) +PyChipError pychip_DeviceController_PostTaskOnChipThread(ChipThreadTaskRunnerFunct callback, void * pythonContext) { if (callback == nullptr || pythonContext == nullptr) { - return CHIP_ERROR_INVALID_ARGUMENT.AsInteger(); + return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); } PlatformMgr().ScheduleWork(callback, reinterpret_cast(pythonContext)); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } diff --git a/src/controller/python/ChipDeviceController-ScriptDevicePairingDelegate.cpp b/src/controller/python/ChipDeviceController-ScriptDevicePairingDelegate.cpp index e98b68ace56446..67cc6abdb01702 100644 --- a/src/controller/python/ChipDeviceController-ScriptDevicePairingDelegate.cpp +++ b/src/controller/python/ChipDeviceController-ScriptDevicePairingDelegate.cpp @@ -19,6 +19,7 @@ #include "ChipDeviceController-ScriptDevicePairingDelegate.h" #include "lib/support/TypeTraits.h" +#include namespace chip { namespace Controller { @@ -53,7 +54,7 @@ void ScriptDevicePairingDelegate::OnPairingComplete(CHIP_ERROR error) { if (mOnPairingCompleteCallback != nullptr) { - mOnPairingCompleteCallback(error.AsInteger()); + mOnPairingCompleteCallback(ToPyChipError(error)); } } @@ -61,7 +62,7 @@ void ScriptDevicePairingDelegate::OnCommissioningComplete(NodeId nodeId, CHIP_ER { if (mOnCommissioningCompleteCallback != nullptr) { - mOnCommissioningCompleteCallback(nodeId, error.AsInteger()); + mOnCommissioningCompleteCallback(nodeId, ToPyChipError(error)); } } diff --git a/src/controller/python/ChipDeviceController-ScriptDevicePairingDelegate.h b/src/controller/python/ChipDeviceController-ScriptDevicePairingDelegate.h index 24991ffa5f73ca..cc41b30d3eb4b1 100644 --- a/src/controller/python/ChipDeviceController-ScriptDevicePairingDelegate.h +++ b/src/controller/python/ChipDeviceController-ScriptDevicePairingDelegate.h @@ -26,13 +26,14 @@ #pragma once #include +#include namespace chip { namespace Controller { extern "C" { -typedef void (*DevicePairingDelegate_OnPairingCompleteFunct)(ChipError::StorageType err); -typedef void (*DevicePairingDelegate_OnCommissioningCompleteFunct)(NodeId nodeId, ChipError::StorageType err); +typedef void (*DevicePairingDelegate_OnPairingCompleteFunct)(PyChipError err); +typedef void (*DevicePairingDelegate_OnCommissioningCompleteFunct)(NodeId nodeId, PyChipError err); // Used for testing by OpCredsBinding typedef void (*DevicePairingDelegate_OnCommissioningSuccessFunct)(PeerId peerId); diff --git a/src/controller/python/OpCredsBinding.cpp b/src/controller/python/OpCredsBinding.cpp index 2787b096307e59..ef92b2c163b575 100644 --- a/src/controller/python/OpCredsBinding.cpp +++ b/src/controller/python/OpCredsBinding.cpp @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -43,8 +44,6 @@ using namespace chip; -static_assert(std::is_same::value, "python assumes CHIP_ERROR maps to c_uint32"); - using Py_GenerateNOCChainFunc = void (*)(void * pyContext, const char * csrElements, const char * attestationSignature, const char * dac, const char * pai, const char * paa, Controller::OnNOCChainGeneration onNocChainGenerationFunc); @@ -320,19 +319,17 @@ void pychip_OnCommissioningStatusUpdate(chip::PeerId peerId, chip::Controller::C return sTestCommissioner.OnCommissioningStatusUpdate(peerId, stageCompleted, err); } -ChipError::StorageType pychip_OpCreds_AllocateController(OpCredsContext * context, - chip::Controller::DeviceCommissioner ** outDevCtrl, FabricId fabricId, - chip::NodeId nodeId, chip::VendorId adminVendorId, - const char * paaTrustStorePath, bool useTestCommissioner, - bool enableServerInteractions, CASEAuthTag * caseAuthTags, - uint32_t caseAuthTagLen) +PyChipError pychip_OpCreds_AllocateController(OpCredsContext * context, chip::Controller::DeviceCommissioner ** outDevCtrl, + FabricId fabricId, chip::NodeId nodeId, chip::VendorId adminVendorId, + const char * paaTrustStorePath, bool useTestCommissioner, + bool enableServerInteractions, CASEAuthTag * caseAuthTags, uint32_t caseAuthTagLen) { ChipLogDetail(Controller, "Creating New Device Controller"); - VerifyOrReturnError(context != nullptr, CHIP_ERROR_INVALID_ARGUMENT.AsInteger()); + VerifyOrReturnError(context != nullptr, ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); auto devCtrl = std::make_unique(); - VerifyOrReturnError(devCtrl != nullptr, CHIP_ERROR_NO_MEMORY.AsInteger()); + VerifyOrReturnError(devCtrl != nullptr, ToPyChipError(CHIP_ERROR_NO_MEMORY)); if (paaTrustStorePath == nullptr) { @@ -347,18 +344,18 @@ ChipError::StorageType pychip_OpCreds_AllocateController(OpCredsContext * contex chip::Crypto::P256Keypair ephemeralKey; CHIP_ERROR err = ephemeralKey.Initialize(); - VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); + VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err)); chip::Platform::ScopedMemoryBuffer noc; - ReturnErrorCodeIf(!noc.Alloc(Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY.AsInteger()); + ReturnErrorCodeIf(!noc.Alloc(Controller::kMaxCHIPDERCertLength), ToPyChipError(CHIP_ERROR_NO_MEMORY)); MutableByteSpan nocSpan(noc.Get(), Controller::kMaxCHIPDERCertLength); chip::Platform::ScopedMemoryBuffer icac; - ReturnErrorCodeIf(!icac.Alloc(Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY.AsInteger()); + ReturnErrorCodeIf(!icac.Alloc(Controller::kMaxCHIPDERCertLength), ToPyChipError(CHIP_ERROR_NO_MEMORY)); MutableByteSpan icacSpan(icac.Get(), Controller::kMaxCHIPDERCertLength); chip::Platform::ScopedMemoryBuffer rcac; - ReturnErrorCodeIf(!rcac.Alloc(Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY.AsInteger()); + ReturnErrorCodeIf(!rcac.Alloc(Controller::kMaxCHIPDERCertLength), ToPyChipError(CHIP_ERROR_NO_MEMORY)); MutableByteSpan rcacSpan(rcac.Get(), Controller::kMaxCHIPDERCertLength); CATValues catValues; @@ -367,13 +364,13 @@ ChipError::StorageType pychip_OpCreds_AllocateController(OpCredsContext * contex { ChipLogError(Controller, "Too many of CASE Tags (%u) exceeds kMaxSubjectCATAttributeCount", static_cast(caseAuthTagLen)); - return CHIP_ERROR_INVALID_ARGUMENT.AsInteger(); + return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); } memcpy(catValues.values.data(), caseAuthTags, caseAuthTagLen * sizeof(CASEAuthTag)); err = context->mAdapter->GenerateNOCChain(nodeId, fabricId, catValues, ephemeralKey.Pubkey(), rcacSpan, icacSpan, nocSpan); - VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); + VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err)); Controller::SetupParams initParams; initParams.pairingDelegate = &sPairingDelegate; @@ -395,14 +392,14 @@ ChipError::StorageType pychip_OpCreds_AllocateController(OpCredsContext * contex } err = Controller::DeviceControllerFactory::GetInstance().SetupCommissioner(initParams, *devCtrl); - VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); + VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err)); // Setup IPK in Group Data Provider for controller after Commissioner init which sets-up the fabric table entry uint8_t compressedFabricId[sizeof(uint64_t)] = { 0 }; chip::MutableByteSpan compressedFabricIdSpan(compressedFabricId); err = devCtrl->GetCompressedFabricIdBytes(compressedFabricIdSpan); - VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); + VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err)); ChipLogProgress(Support, "Setting up group data for Fabric Index %u with Compressed Fabric ID:", static_cast(devCtrl->GetFabricIndex())); @@ -411,20 +408,20 @@ ChipError::StorageType pychip_OpCreds_AllocateController(OpCredsContext * contex chip::ByteSpan defaultIpk = chip::GroupTesting::DefaultIpkValue::GetDefaultIpk(); err = chip::Credentials::SetSingleIpkEpochKey(&sGroupDataProvider, devCtrl->GetFabricIndex(), defaultIpk, compressedFabricIdSpan); - VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); + VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err)); *outDevCtrl = devCtrl.release(); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -ChipError::StorageType pychip_OpCreds_SetMaximallyLargeCertsUsed(OpCredsContext * context, bool enabled) +PyChipError pychip_OpCreds_SetMaximallyLargeCertsUsed(OpCredsContext * context, bool enabled) { - VerifyOrReturnError(context != nullptr && context->mAdapter != nullptr, CHIP_ERROR_INCORRECT_STATE.AsInteger()); + VerifyOrReturnError(context != nullptr && context->mAdapter != nullptr, ToPyChipError(CHIP_ERROR_INCORRECT_STATE)); context->mAdapter->SetMaximallyLargeCertsUsed(enabled); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } void pychip_OpCreds_FreeDelegate(OpCredsContext * context) @@ -432,7 +429,7 @@ void pychip_OpCreds_FreeDelegate(OpCredsContext * context) Platform::Delete(context); } -ChipError::StorageType pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl) +PyChipError pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl) { if (devCtrl != nullptr) { @@ -440,7 +437,7 @@ ChipError::StorageType pychip_DeviceController_DeleteDeviceController(chip::Cont delete devCtrl; } - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } bool pychip_TestCommissionerUsed() diff --git a/src/controller/python/chip/CertificateAuthority.py b/src/controller/python/chip/CertificateAuthority.py index 7f40f0cd016100..b6f283129a1a31 100644 --- a/src/controller/python/chip/CertificateAuthority.py +++ b/src/controller/python/chip/CertificateAuthority.py @@ -22,6 +22,7 @@ from dataclasses import dataclass, field from typing import * from ctypes import * +from chip.native import PyChipError from rich.pretty import pprint import json import logging @@ -74,7 +75,7 @@ def __init__(self, chipStack: ChipStack.ChipStack, caIndex: int, persistentStora self._Handle().pychip_OpCreds_InitializeDelegate.restype = c_void_p self._Handle().pychip_OpCreds_InitializeDelegate.argtypes = [ctypes.py_object, ctypes.c_uint32, ctypes.c_void_p] - self._Handle().pychip_OpCreds_SetMaximallyLargeCertsUsed.restype = c_uint32 + self._Handle().pychip_OpCreds_SetMaximallyLargeCertsUsed.restype = PyChipError self._Handle().pychip_OpCreds_SetMaximallyLargeCertsUsed.argtypes = [ctypes.c_void_p, ctypes.c_bool] if (persistentStorage is None): @@ -191,12 +192,9 @@ def maximizeCertChains(self) -> bool: @maximizeCertChains.setter def maximizeCertChains(self, enabled: bool): - res = self._chipStack.Call( + self._chipStack.Call( lambda: self._Handle().pychip_OpCreds_SetMaximallyLargeCertsUsed(ctypes.c_void_p(self._closure), ctypes.c_bool(enabled)) - ) - - if res != 0: - raise self._chipStack.ErrorToException(res) + ).raise_on_error() self._maximizeCertChains = enabled diff --git a/src/controller/python/chip/ChipCommissionableNodeCtrl.py b/src/controller/python/chip/ChipCommissionableNodeCtrl.py index c1d47dd64b91c0..9a3bcc2c6f40a0 100644 --- a/src/controller/python/chip/ChipCommissionableNodeCtrl.py +++ b/src/controller/python/chip/ChipCommissionableNodeCtrl.py @@ -28,6 +28,7 @@ from ctypes import * from .ChipStack import * from .exceptions import * +from .native import PyChipError __all__ = ["ChipCommissionableNodeController"] @@ -53,10 +54,8 @@ def __init__(self, chipStack: ChipStack): self._InitLib() commissionableNodeCtrl = c_void_p(None) - res = self._dmLib.pychip_CommissionableNodeController_NewController( - pointer(commissionableNodeCtrl)) - if res != 0: - raise self._ChipStack.ErrorToException(res) + self._dmLib.pychip_CommissionableNodeController_NewController( + pointer(commissionableNodeCtrl)).raise_on_error() self.commissionableNodeCtrl = commissionableNodeCtrl self._ChipStack.commissionableNodeCtrl = commissionableNodeCtrl @@ -68,16 +67,16 @@ def __del__(self): self.commissionableNodeCtrl = None def PrintDiscoveredCommissioners(self): - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_CommissionableNodeController_PrintDiscoveredCommissioners( self.commissionableNodeCtrl) ) def DiscoverCommissioners(self): - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_CommissionableNodeController_DiscoverCommissioners( self.commissionableNodeCtrl) - ) + ).raise_on_error() # ----- Private Members ----- def _InitLib(self): @@ -86,15 +85,15 @@ def _InitLib(self): self._dmLib.pychip_CommissionableNodeController_NewController.argtypes = [ POINTER(c_void_p)] - self._dmLib.pychip_CommissionableNodeController_NewController.restype = c_uint32 + self._dmLib.pychip_CommissionableNodeController_NewController.restype = PyChipError self._dmLib.pychip_CommissionableNodeController_DeleteController.argtypes = [ c_void_p] - self._dmLib.pychip_CommissionableNodeController_DeleteController.restype = c_uint32 + self._dmLib.pychip_CommissionableNodeController_DeleteController.restype = PyChipError self._dmLib.pychip_CommissionableNodeController_DiscoverCommissioners.argtypes = [ c_void_p] - self._dmLib.pychip_CommissionableNodeController_DiscoverCommissioners.restype = c_uint32 + self._dmLib.pychip_CommissionableNodeController_DiscoverCommissioners.restype = PyChipError self._dmLib.pychip_CommissionableNodeController_PrintDiscoveredCommissioners.argtypes = [ c_void_p] diff --git a/src/controller/python/chip/ChipDeviceCtrl.py b/src/controller/python/chip/ChipDeviceCtrl.py index 4547567680f404..9c07108acc45cb 100644 --- a/src/controller/python/chip/ChipDeviceCtrl.py +++ b/src/controller/python/chip/ChipDeviceCtrl.py @@ -42,6 +42,7 @@ from . import clusters as Clusters from .FabricAdmin import FabricAdmin from . import discovery +from .native import PyChipError import enum import threading import typing @@ -54,19 +55,19 @@ __all__ = ["ChipDeviceController"] -_DevicePairingDelegate_OnPairingCompleteFunct = CFUNCTYPE(None, c_uint32) +_DevicePairingDelegate_OnPairingCompleteFunct = CFUNCTYPE(None, PyChipError) _DevicePairingDelegate_OnCommissioningCompleteFunct = CFUNCTYPE( - None, c_uint64, c_uint32) + None, c_uint64, PyChipError) _DevicePairingDelegate_OnCommissioningStatusUpdateFunct = CFUNCTYPE( - None, c_uint64, c_uint8, c_uint32) + None, c_uint64, c_uint8, PyChipError) # void (*)(Device *, CHIP_ERROR). # # CHIP_ERROR is actually signed, so using c_uint32 is weird, but everything # else seems to do it. -_DeviceAvailableFunct = CFUNCTYPE(None, c_void_p, c_uint32) +_DeviceAvailableFunct = CFUNCTYPE(None, c_void_p, PyChipError) _IssueNOCChainCallbackPythonCallbackFunct = CFUNCTYPE( - None, py_object, c_uint32, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_uint64) + None, py_object, PyChipError, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_uint64) _ChipDeviceController_IterateDiscoveredCommissionableNodesFunct = CFUNCTYPE(None, c_char_p, c_size_t) @@ -81,9 +82,9 @@ class NOCChain: @_IssueNOCChainCallbackPythonCallbackFunct -def _IssueNOCChainCallbackPythonCallback(devCtrl, status: int, noc: c_void_p, nocLen: int, icac: c_void_p, icacLen: int, rcac: c_void_p, rcacLen: int, ipk: c_void_p, ipkLen: int, adminSubject: int): +def _IssueNOCChainCallbackPythonCallback(devCtrl, status: PyChipError, noc: c_void_p, nocLen: int, icac: c_void_p, icacLen: int, rcac: c_void_p, rcacLen: int, ipk: c_void_p, ipkLen: int, adminSubject: int): nocChain = NOCChain(None, None, None, None, 0) - if status == 0: + if status.is_success: nocBytes = None if nocLen > 0: nocBytes = string_at(noc, nocLen)[:] @@ -170,26 +171,26 @@ def deviceProxy(self) -> ctypes.c_void_p: @property def localSessionId(self) -> int: self._dmLib.pychip_GetLocalSessionId.argtypes = [ctypes.c_void_p, POINTER(ctypes.c_uint16)] - self._dmLib.pychip_GetLocalSessionId.restype = ctypes.c_uint32 + self._dmLib.pychip_GetLocalSessionId.restype = PyChipError localSessionId = ctypes.c_uint16(0) builtins.chipStack.Call( lambda: self._dmLib.pychip_GetLocalSessionId(self._deviceProxy, pointer(localSessionId)) - ) + ).raise_on_error() return localSessionId.value @property def numTotalSessions(self) -> int: self._dmLib.pychip_GetNumSessionsToPeer.argtypes = [ctypes.c_void_p, POINTER(ctypes.c_uint32)] - self._dmLib.pychip_GetNumSessionsToPeer.restype = ctypes.c_uint32 + self._dmLib.pychip_GetNumSessionsToPeer.restype = PyChipError numSessions = ctypes.c_uint32(0) builtins.chipStack.Call( lambda: self._dmLib.pychip_GetNumSessionsToPeer(self._deviceProxy, pointer(numSessions)) - ) + ).raise_on_error() return numSessions.value @@ -219,16 +220,13 @@ def __init__(self, opCredsContext: ctypes.c_void_p, fabricId: int, nodeId: int, self._dmLib.pychip_OpCreds_AllocateController.argtypes = [c_void_p, POINTER( c_void_p), c_uint64, c_uint64, c_uint16, c_char_p, c_bool, c_bool, POINTER(c_uint32), c_uint32] - self._dmLib.pychip_OpCreds_AllocateController.restype = c_uint32 + self._dmLib.pychip_OpCreds_AllocateController.restype = PyChipError # TODO(erjiaqing@): Figure out how to control enableServerInteractions for a single device controller (node) - res = self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_OpCreds_AllocateController(c_void_p( opCredsContext), pointer(devCtrl), fabricId, nodeId, adminVendorId, c_char_p(None if len(paaTrustStorePath) == 0 else str.encode(paaTrustStorePath)), useTestCommissioner, self._ChipStack.enableServerInteractions, c_catTags, len(catTags)) - ) - - if res != 0: - raise self._ChipStack.ErrorToException(res) + ).raise_on_error() self.devCtrl = devCtrl self._fabricAdmin = fabricAdmin @@ -245,21 +243,21 @@ def __init__(self, opCredsContext: ctypes.c_void_p, fabricId: int, nodeId: int, self._Cluster.InitLib(self._dmLib) def HandleCommissioningComplete(nodeid, err): - if err != 0: - print("Failed to commission: {}".format(err)) - else: + if err.is_success: print("Commissioning complete") + else: + print("Failed to commission: {}".format(err)) + self.state = DCState.IDLE self._ChipStack.callbackRes = err self._ChipStack.commissioningEventRes = err self._ChipStack.commissioningCompleteEvent.set() self._ChipStack.completeEvent.set() - def HandlePASEEstablishmentComplete(err): - if err != 0: + def HandlePASEEstablishmentComplete(err: PyChipError): + if err.is_success: print("Failed to establish secure session to device: {}".format(err)) - self._ChipStack.callbackRes = self._ChipStack.ErrorToException( - err) + self._ChipStack.callbackRes = err.to_exception() else: print("Established secure session with Device") @@ -272,7 +270,7 @@ def HandlePASEEstablishmentComplete(err): else: # When commissioning, getting an error during key exhange # needs to unblock the entire commissioning flow. - if err != 0: + if not err.is_success: HandleCommissioningComplete(0, err) self.cbHandlePASEEstablishmentCompleteFunct = _DevicePairingDelegate_OnPairingCompleteFunct( @@ -333,7 +331,7 @@ def Shutdown(self): self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_DeleteDeviceController( self.devCtrl) - ) + ).raise_on_error() self.devCtrl = None ChipDeviceController.activeList.remove(self) @@ -374,18 +372,6 @@ def IsConnected(self): self.devCtrl) ) - def ConnectBle(self, bleConnection): - self.CheckIsActive() - - self._ChipStack.CallAsync( - lambda: self._dmLib.pychip_DeviceController_ValidateBTP( - self.devCtrl, - bleConnection, - self._ChipStack.cbHandleComplete, - self._ChipStack.cbHandleError, - ) - ) - def ConnectBLE(self, discriminator, setupPinCode, nodeid): self.CheckIsActive() @@ -395,19 +381,19 @@ def ConnectBLE(self, discriminator, setupPinCode, nodeid): self._ChipStack.CallAsync( lambda: self._dmLib.pychip_DeviceController_ConnectBLE( self.devCtrl, discriminator, setupPinCode, nodeid) - ) + ).raise_on_error() if not self._ChipStack.commissioningCompleteEvent.isSet(): # Error 50 is a timeout return False - return self._ChipStack.commissioningEventRes == 0 + return self._ChipStack.commissioningEventRes.is_success def CloseBLEConnection(self): self.CheckIsActive() - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceCommissioner_CloseBleConnection( self.devCtrl) - ) + ).raise_on_error() def ExpireSessions(self, nodeid): """Close all sessions with `nodeid` (if any existed) so that sessions get re-established. @@ -419,18 +405,16 @@ def ExpireSessions(self, nodeid): """ self.CheckIsActive() - res = self._ChipStack.Call(lambda: self._dmLib.pychip_ExpireSessions(self.devCtrl, nodeid)) - if res != 0: - raise self._ChipStack.ErrorToException(res) + self._ChipStack.Call(lambda: self._dmLib.pychip_ExpireSessions(self.devCtrl, nodeid)).raise_on_error() # TODO: This needs to be called MarkSessionDefunct def CloseSession(self, nodeid): self.CheckIsActive() - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_CloseSession( self.devCtrl, nodeid) - ) + ).raise_on_error() def EstablishPASESessionIP(self, ipaddr: str, setupPinCode: int, nodeid: int): self.CheckIsActive() @@ -576,18 +560,18 @@ def CommissionWiFi(self, discriminator, setupPinCode, nodeId, ssid: str, credent def SetWiFiCredentials(self, ssid: str, credentials: str): self.CheckIsActive() - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_SetWiFiCredentials( ssid.encode("utf-8"), credentials.encode("utf-8")) - ) + ).raise_on_error() def SetThreadOperationalDataset(self, threadOperationalDataset): self.CheckIsActive() - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_SetThreadOperationalDataset( threadOperationalDataset, len(threadOperationalDataset)) - ) + ).raise_on_error() def ResolveNode(self, nodeid): self.CheckIsActive() @@ -600,6 +584,7 @@ def GetAddressAndPort(self, nodeid): address = create_string_buffer(64) port = c_uint16(0) + # Intentially return None instead of raising exceptions on error error = self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_GetAddressAndPort( self.devCtrl, nodeid, address, 64, pointer(port)) @@ -628,12 +613,9 @@ def DiscoverCommissionableNodes(self, filterType: discovery.FilterType = discove if isinstance(filter, int): filter = str(filter) - res = self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_DiscoverCommissionableNodes( - self.devCtrl, int(filterType), str(filter).encode("utf-8") + b"\x00")) - - if res != 0: - raise self._ChipStack.ErrorToException(res) + self.devCtrl, int(filterType), str(filter).encode("utf-8") + b"\x00")).raise_on_error() if timeoutSecond != 0: if stopOnFirst: @@ -652,57 +634,57 @@ def DiscoverCommissionableNodesLongDiscriminator(self, long_discriminator): ''' self.CheckIsActive() - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator( self.devCtrl, long_discriminator) - ) + ).raise_on_error() def DiscoverCommissionableNodesShortDiscriminator(self, short_discriminator): ''' Deprecated, use DiscoverCommissionableNodes ''' self.CheckIsActive() - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator( self.devCtrl, short_discriminator) - ) + ).raise_on_error() def DiscoverCommissionableNodesVendor(self, vendor): ''' Deprecated, use DiscoverCommissionableNodes ''' self.CheckIsActive() - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesVendor( self.devCtrl, vendor) - ) + ).raise_on_error() def DiscoverCommissionableNodesDeviceType(self, device_type): ''' Deprecated, use DiscoverCommissionableNodes ''' self.CheckIsActive() - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesDeviceType( self.devCtrl, device_type) - ) + ).raise_on_error() def DiscoverCommissionableNodesCommissioningEnabled(self): ''' Deprecated, use DiscoverCommissionableNodes ''' self.CheckIsActive() - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled( self.devCtrl) - ) + ).raise_on_error() def PrintDiscoveredDevices(self): ''' Deprecated, use GetCommissionableNodes ''' self.CheckIsActive() - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_PrintDiscoveredDevices( self.devCtrl) ) @@ -723,15 +705,6 @@ def HandleDevice(deviceJson, deviceJsonLen): return self._ChipStack.Call(lambda: GetDevices(self)) - def ParseQRCode(self, qrCode, output): - self.CheckIsActive() - - print(output) - return self._ChipStack.Call( - lambda: self._dmLib.pychip_DeviceController_ParseQRCode( - qrCode, output) - ) - def GetIPForDiscoveredDevice(self, idx, addrStr, length): self.CheckIsActive() @@ -745,36 +718,30 @@ def DiscoverAllCommissioning(self): ''' self.CheckIsActive() - return self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_DiscoverAllCommissionableNodes( self.devCtrl) - ) + ).raise_on_error() def OpenCommissioningWindow(self, nodeid, timeout, iteration, discriminator, option): self.CheckIsActive() - res = self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_OpenCommissioningWindow( self.devCtrl, nodeid, timeout, iteration, discriminator, option) - ) - - if res != 0: - raise self._ChipStack.ErrorToException(res) + ).raise_on_error() def GetCompressedFabricId(self): self.CheckIsActive() fabricid = c_uint64(0) - res = self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_GetCompressedFabricId( self.devCtrl, pointer(fabricid)) - ) + ).raise_on_error() - if res == 0: - return fabricid.value - else: - raise self._ChipStack.ErrorToException(res) + return fabricid.value def GetFabricIdInternal(self): """Get the fabric ID from the object. Only used to validate cached value from property.""" @@ -782,15 +749,12 @@ def GetFabricIdInternal(self): fabricid = c_uint64(0) - res = self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_GetFabricId( self.devCtrl, pointer(fabricid)) - ) + ).raise_on_error() - if res == 0: - return fabricid.value - else: - raise self._ChipStack.ErrorToException(res) + return fabricid.value def GetNodeIdInternal(self) -> int: """Get the node ID from the object. Only used to validate cached value from property.""" @@ -798,15 +762,12 @@ def GetNodeIdInternal(self) -> int: nodeid = c_uint64(0) - res = self._ChipStack.Call( + self._ChipStack.Call( lambda: self._dmLib.pychip_DeviceController_GetNodeId( self.devCtrl, pointer(nodeid)) - ) + ).raise_on_error() - if res == 0: - return nodeid.value - else: - raise self._ChipStack.ErrorToException(res) + return nodeid.value def GetClusterHandler(self): self.CheckIsActive() @@ -834,14 +795,12 @@ def DeviceAvailableCallback(device, err): if allowPASE: res = self._ChipStack.Call(lambda: self._dmLib.pychip_GetDeviceBeingCommissioned( self.devCtrl, nodeid, byref(returnDevice)), timeoutMs) - if res == 0: + if res.is_success: print('Using PASE connection') return DeviceProxyWrapper(returnDevice) - res = self._ChipStack.Call(lambda: self._dmLib.pychip_GetConnectedDeviceByNodeId( - self.devCtrl, nodeid, DeviceAvailableCallback), timeoutMs) - if res != 0: - raise self._ChipStack.ErrorToException(res) + self._ChipStack.Call(lambda: self._dmLib.pychip_GetConnectedDeviceByNodeId( + self.devCtrl, nodeid, DeviceAvailableCallback), timeoutMs).raise_on_error() # The callback might have been received synchronously (during self._ChipStack.Call()). # Check if the device is already set before waiting for the callback. @@ -856,7 +815,7 @@ def DeviceAvailableCallback(device, err): raise TimeoutError("Timed out waiting for DNS-SD resolution") if returnDevice.value is None: - raise self._ChipStack.ErrorToException(returnErr) + returnErr.raise_on_error() return DeviceProxyWrapper(returnDevice, self._dmLib) @@ -889,14 +848,12 @@ async def SendCommand(self, nodeid: int, endpoint: int, payload: ClusterObjects. future = eventLoop.create_future() device = self.GetConnectedDeviceSync(nodeid, timeoutMs=interactionTimeoutMs) - res = ClusterCommand.SendCommand( + ClusterCommand.SendCommand( future, eventLoop, responseType, device.deviceProxy, ClusterCommand.CommandPath( EndpointId=endpoint, ClusterId=payload.cluster_id, CommandId=payload.command_id, - ), payload, timedRequestTimeoutMs=timedRequestTimeoutMs, interactionTimeoutMs=interactionTimeoutMs) - if res != 0: - future.set_exception(self._ChipStack.ErrorToException(res)) + ), payload, timedRequestTimeoutMs=timedRequestTimeoutMs, interactionTimeoutMs=interactionTimeoutMs).raise_on_error() return await future async def WriteAttribute(self, nodeid: int, attributes: typing.List[typing.Tuple[int, ClusterObjects.ClusterAttributeDescriptor, int]], timedRequestTimeoutMs: int = None, interactionTimeoutMs: int = None): @@ -928,10 +885,8 @@ async def WriteAttribute(self, nodeid: int, attributes: typing.List[typing.Tuple attrs.append(ClusterAttribute.AttributeWriteRequest( v[0], v[1], v[2], 1, v[1].value)) - res = ClusterAttribute.WriteAttributes( - future, eventLoop, device.deviceProxy, attrs, timedRequestTimeoutMs=timedRequestTimeoutMs, interactionTimeoutMs=interactionTimeoutMs) - if res != 0: - raise self._ChipStack.ErrorToException(res) + ClusterAttribute.WriteAttributes( + future, eventLoop, device.deviceProxy, attrs, timedRequestTimeoutMs=timedRequestTimeoutMs, interactionTimeoutMs=interactionTimeoutMs).raise_on_error() return await future def _parseAttributePathTuple(self, pathTuple: typing.Union[ @@ -1110,10 +1065,8 @@ async def Read(self, nodeid: int, attributes: typing.List[typing.Union[ eventPaths = [self._parseEventPathTuple( v) for v in events] if events else None - res = ClusterAttribute.Read(future=future, eventLoop=eventLoop, device=device.deviceProxy, devCtrl=self, attributes=attributePaths, dataVersionFilters=clusterDataVersionFilters, events=eventPaths, returnClusterObject=returnClusterObject, - subscriptionParameters=ClusterAttribute.SubscriptionParameters(reportInterval[0], reportInterval[1]) if reportInterval else None, fabricFiltered=fabricFiltered, keepSubscriptions=keepSubscriptions) - if res != 0: - raise self._ChipStack.ErrorToException(res) + ClusterAttribute.Read(future=future, eventLoop=eventLoop, device=device.deviceProxy, devCtrl=self, attributes=attributePaths, dataVersionFilters=clusterDataVersionFilters, events=eventPaths, returnClusterObject=returnClusterObject, + subscriptionParameters=ClusterAttribute.SubscriptionParameters(reportInterval[0], reportInterval[1]) if reportInterval else None, fabricFiltered=fabricFiltered, keepSubscriptions=keepSubscriptions).raise_on_error() return await future async def ReadAttribute(self, nodeid: int, attributes: typing.List[typing.Union[ @@ -1302,60 +1255,61 @@ def _InitLib(self): self._dmLib.pychip_DeviceController_DeleteDeviceController.argtypes = [ c_void_p] - self._dmLib.pychip_DeviceController_DeleteDeviceController.restype = c_uint32 + self._dmLib.pychip_DeviceController_DeleteDeviceController.restype = PyChipError self._dmLib.pychip_DeviceController_ConnectBLE.argtypes = [ c_void_p, c_uint16, c_uint32, c_uint64] - self._dmLib.pychip_DeviceController_ConnectBLE.restype = c_uint32 + self._dmLib.pychip_DeviceController_ConnectBLE.restype = PyChipError self._dmLib.pychip_DeviceController_ConnectIP.argtypes = [ c_void_p, c_char_p, c_uint32, c_uint64] self._dmLib.pychip_DeviceController_SetThreadOperationalDataset.argtypes = [ c_char_p, c_uint32] - self._dmLib.pychip_DeviceController_SetThreadOperationalDataset.restype = c_uint32 + self._dmLib.pychip_DeviceController_SetThreadOperationalDataset.restype = PyChipError self._dmLib.pychip_DeviceController_SetWiFiCredentials.argtypes = [ c_char_p, c_char_p] - self._dmLib.pychip_DeviceController_SetWiFiCredentials.restype = c_uint32 + self._dmLib.pychip_DeviceController_SetWiFiCredentials.restype = PyChipError self._dmLib.pychip_DeviceController_Commission.argtypes = [ c_void_p, c_uint64] - self._dmLib.pychip_DeviceController_Commission.restype = c_uint32 + self._dmLib.pychip_DeviceController_Commission.restype = PyChipError self._dmLib.pychip_DeviceController_OnNetworkCommission.argtypes = [c_void_p, c_uint64, c_uint32, c_uint8, c_char_p] - self._dmLib.pychip_DeviceController_OnNetworkCommission.restype = c_uint32 + self._dmLib.pychip_DeviceController_OnNetworkCommission.restype = PyChipError - self._dmLib.pychip_DeviceController_DiscoverAllCommissionableNodes.argtypes = [ + self._dmLib.pychip_DeviceController_DiscoverCommissionableNodes.argtypes = [ c_void_p, c_uint8, c_char_p] - self._dmLib.pychip_DeviceController_DiscoverAllCommissionableNodes.restype = c_uint32 + self._dmLib.pychip_DeviceController_DiscoverCommissionableNodes.restype = PyChipError self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator.argtypes = [ c_void_p, c_uint16] - self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator.restype = c_uint32 + self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesLongDiscriminator.restype = PyChipError self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator.argtypes = [ c_void_p, c_uint16] - self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator.restype = c_uint32 + self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesShortDiscriminator.restype = PyChipError self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesVendor.argtypes = [ c_void_p, c_uint16] - self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesVendor.restype = c_uint32 + self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesVendor.restype = PyChipError self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesDeviceType.argtypes = [ c_void_p, c_uint16] - self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesDeviceType.restype = c_uint32 + self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesDeviceType.restype = PyChipError self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled.argtypes = [ c_void_p] - self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled.restype = c_uint32 + self._dmLib.pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled.restype = PyChipError self._dmLib.pychip_DeviceController_EstablishPASESessionIP.argtypes = [ c_void_p, c_char_p, c_uint32, c_uint64] - self._dmLib.pychip_DeviceController_EstablishPASESessionIP.restype = c_uint32 + self._dmLib.pychip_DeviceController_EstablishPASESessionIP.restype = PyChipError self._dmLib.pychip_DeviceController_DiscoverAllCommissionableNodes.argtypes = [ c_void_p] + self._dmLib.pychip_DeviceController_DiscoverAllCommissionableNodes.restype = PyChipError self._dmLib.pychip_DeviceController_PrintDiscoveredDevices.argtypes = [ c_void_p] self._dmLib.pychip_DeviceController_PrintDiscoveredDevices.argtypes = [ @@ -1369,65 +1323,67 @@ def _InitLib(self): self._dmLib.pychip_DeviceController_ConnectIP.argtypes = [ c_void_p, c_char_p, c_uint32, c_uint64] - self._dmLib.pychip_DeviceController_ConnectIP.restype = c_uint32 + self._dmLib.pychip_DeviceController_ConnectIP.restype = PyChipError self._dmLib.pychip_DeviceController_ConnectWithCode.argtypes = [ c_void_p, c_char_p, c_uint64] - self._dmLib.pychip_DeviceController_ConnectWithCode.restype = c_uint32 + self._dmLib.pychip_DeviceController_ConnectWithCode.restype = PyChipError self._dmLib.pychip_DeviceController_CloseSession.argtypes = [ c_void_p, c_uint64] - self._dmLib.pychip_DeviceController_CloseSession.restype = c_uint32 + self._dmLib.pychip_DeviceController_CloseSession.restype = PyChipError self._dmLib.pychip_DeviceController_GetAddressAndPort.argtypes = [ c_void_p, c_uint64, c_char_p, c_uint64, POINTER(c_uint16)] - self._dmLib.pychip_DeviceController_GetAddressAndPort.restype = c_uint32 + self._dmLib.pychip_DeviceController_GetAddressAndPort.restype = PyChipError self._dmLib.pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback.argtypes = [ c_void_p, _DevicePairingDelegate_OnPairingCompleteFunct] - self._dmLib.pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback.restype = c_uint32 + self._dmLib.pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback.restype = PyChipError self._dmLib.pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback.argtypes = [ c_void_p, _DevicePairingDelegate_OnCommissioningCompleteFunct] - self._dmLib.pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback.restype = c_uint32 + self._dmLib.pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback.restype = PyChipError self._dmLib.pychip_ScriptDevicePairingDelegate_SetCommissioningStatusUpdateCallback.argtypes = [ c_void_p, _DevicePairingDelegate_OnCommissioningStatusUpdateFunct] - self._dmLib.pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback.restype = c_uint32 + self._dmLib.pychip_ScriptDevicePairingDelegate_SetCommissioningCompleteCallback.restype = PyChipError self._dmLib.pychip_GetConnectedDeviceByNodeId.argtypes = [ c_void_p, c_uint64, _DeviceAvailableFunct] - self._dmLib.pychip_GetConnectedDeviceByNodeId.restype = c_uint32 + self._dmLib.pychip_GetConnectedDeviceByNodeId.restype = PyChipError self._dmLib.pychip_FreeOperationalDeviceProxy.argtypes = [ c_void_p] - self._dmLib.pychip_FreeOperationalDeviceProxy.restype = c_uint32 + self._dmLib.pychip_FreeOperationalDeviceProxy.restype = PyChipError self._dmLib.pychip_GetDeviceBeingCommissioned.argtypes = [ c_void_p, c_uint64, c_void_p] - self._dmLib.pychip_GetDeviceBeingCommissioned.restype = c_uint32 + self._dmLib.pychip_GetDeviceBeingCommissioned.restype = PyChipError self._dmLib.pychip_ExpireSessions.argtypes = [c_void_p, c_uint64] - self._dmLib.pychip_ExpireSessions.restype = c_uint32 + self._dmLib.pychip_ExpireSessions.restype = PyChipError self._dmLib.pychip_DeviceCommissioner_CloseBleConnection.argtypes = [ c_void_p] - self._dmLib.pychip_DeviceCommissioner_CloseBleConnection.restype = c_uint32 + self._dmLib.pychip_DeviceCommissioner_CloseBleConnection.restype = PyChipError self._dmLib.pychip_GetCommandSenderHandle.argtypes = [c_void_p] self._dmLib.pychip_GetCommandSenderHandle.restype = c_uint64 self._dmLib.pychip_DeviceController_GetCompressedFabricId.argtypes = [ c_void_p, POINTER(c_uint64)] - self._dmLib.pychip_DeviceController_GetCompressedFabricId.restype = c_uint32 + self._dmLib.pychip_DeviceController_GetCompressedFabricId.restype = PyChipError self._dmLib.pychip_DeviceController_OpenCommissioningWindow.argtypes = [ c_void_p, c_uint64, c_uint16, c_uint32, c_uint16, c_uint8] - self._dmLib.pychip_DeviceController_OpenCommissioningWindow.restype = c_uint32 + self._dmLib.pychip_DeviceController_OpenCommissioningWindow.restype = PyChipError self._dmLib.pychip_TestCommissionerUsed.argtypes = [] self._dmLib.pychip_TestCommissionerUsed.restype = c_bool self._dmLib.pychip_TestCommissioningCallbacks.argtypes = [] + self._dmLib.pychip_TestCommissioningCallbacks.restype = c_bool + self._dmLib.pychip_ResetCommissioningTests.argtypes = [] self._dmLib.pychip_TestPaseConnection.argtypes = [c_uint64] @@ -1441,8 +1397,17 @@ def _InitLib(self): self._dmLib.pychip_DeviceController_IssueNOCChain.argtypes = [ c_void_p, py_object, c_char_p, c_size_t, c_uint64 ] - self._dmLib.pychip_DeviceController_IssueNOCChain.restype = c_uint32 + self._dmLib.pychip_DeviceController_IssueNOCChain.restype = PyChipError self._dmLib.pychip_DeviceController_SetIssueNOCChainCallbackPythonCallback.argtypes = [ _IssueNOCChainCallbackPythonCallbackFunct] self._dmLib.pychip_DeviceController_SetIssueNOCChainCallbackPythonCallback.restype = None + + self._dmLib.pychip_DeviceController_GetNodeId.argtypes = [c_void_p, POINTER(c_uint64)] + self._dmLib.pychip_DeviceController_GetNodeId.restype = PyChipError + + self._dmLib.pychip_DeviceController_GetFabricId.argtypes = [c_void_p, POINTER(c_uint64)] + self._dmLib.pychip_DeviceController_GetFabricId.restype = PyChipError + + self._dmLib.pychip_DeviceController_GetLogFilter = [None] + self._dmLib.pychip_DeviceController_GetLogFilter = c_uint8 diff --git a/src/controller/python/chip/ChipStack.py b/src/controller/python/chip/ChipStack.py index 77d36b39d511c0..3ca313be08214d 100644 --- a/src/controller/python/chip/ChipStack.py +++ b/src/controller/python/chip/ChipStack.py @@ -47,6 +47,7 @@ from .clusters.CHIPClusters import * import chip.native +from chip.native import PyChipError __all__ = [ "DeviceStatusStruct", @@ -269,8 +270,7 @@ def HandleChipThreadRun(callback): # Initialize the chip stack. res = self._ChipStackLib.pychip_DeviceController_StackInit( self._persistentStorage.GetSdkStorageObject(), enableServerInteractions) - if res != 0: - raise self.ErrorToException(res) + res.raise_on_error() im.InitIMDelegate() ClusterAttribute.Init() @@ -328,7 +328,7 @@ def setLogFunct(self, logFunct): self._ChipStackLib.pychip_Stack_SetLogFunct(logFunct) def Shutdown(self): - self.Call(lambda: self._ChipStackLib.pychip_DeviceController_StackShutdown()) + self.Call(lambda: self._ChipStackLib.pychip_DeviceController_StackShutdown()).raise_on_error() # # We only shutdown the persistent storage layer AFTER we've shut down the stack, @@ -377,9 +377,9 @@ def CallAsync(self, callFunct): with self.networkLock: res = self.PostTaskOnChipThread(callFunct).Wait() - if res != 0: + if not res.is_success: self.completeEvent.set() - raise self.ErrorToException(res) + raise res.to_exception() while not self.completeEvent.isSet(): if self.blockingCB: self.blockingCB() @@ -398,9 +398,9 @@ def PostTaskOnChipThread(self, callFunct) -> AsyncCallableHandle: pythonapi.Py_IncRef(py_object(callObj)) res = self._ChipStackLib.pychip_DeviceController_PostTaskOnChipThread( self.cbHandleChipThreadRun, py_object(callObj)) - if res != 0: + if not res.is_success: pythonapi.Py_DecRef(py_object(callObj)) - raise self.ErrorToException(res) + raise res.to_exception() return callObj def ErrorToException(self, err, devStatusPtr=None): @@ -450,9 +450,9 @@ def _loadLib(self): self._chipDLLPath = chip.native.FindNativeLibraryPath() self._ChipStackLib.pychip_DeviceController_StackInit.argtypes = [c_void_p, c_bool] - self._ChipStackLib.pychip_DeviceController_StackInit.restype = c_uint32 + self._ChipStackLib.pychip_DeviceController_StackInit.restype = PyChipError self._ChipStackLib.pychip_DeviceController_StackShutdown.argtypes = [] - self._ChipStackLib.pychip_DeviceController_StackShutdown.restype = c_uint32 + self._ChipStackLib.pychip_DeviceController_StackShutdown.restype = PyChipError self._ChipStackLib.pychip_Stack_StatusReportToString.argtypes = [ c_uint32, c_uint16, @@ -462,8 +462,8 @@ def _loadLib(self): self._ChipStackLib.pychip_Stack_ErrorToString.restype = c_char_p self._ChipStackLib.pychip_Stack_SetLogFunct.argtypes = [ _LogMessageFunct] - self._ChipStackLib.pychip_Stack_SetLogFunct.restype = c_uint32 + self._ChipStackLib.pychip_Stack_SetLogFunct.restype = PyChipError self._ChipStackLib.pychip_DeviceController_PostTaskOnChipThread.argtypes = [ _ChipThreadTaskRunnerFunct, py_object] - self._ChipStackLib.pychip_DeviceController_PostTaskOnChipThread.restype = c_uint32 + self._ChipStackLib.pychip_DeviceController_PostTaskOnChipThread.restype = PyChipError diff --git a/src/controller/python/chip/clusters/Attribute.py b/src/controller/python/chip/clusters/Attribute.py index 0fa3530d36ab41..d7608f8d3c53a9 100644 --- a/src/controller/python/chip/clusters/Attribute.py +++ b/src/controller/python/chip/clusters/Attribute.py @@ -31,6 +31,7 @@ import chip.exceptions import chip.interaction_model import chip.tlv +from chip.native import PyChipError from enum import Enum, unique import inspect import sys @@ -708,8 +709,8 @@ def handleEventData(self, header: EventHeader, path: EventPath, data: bytes, sta except Exception as ex: logging.exception(ex) - def handleError(self, chipError: int): - self._resultError = chipError + def handleError(self, chipError: PyChipError): + self._resultError = chipError.code def _handleSubscriptionEstablished(self, subscriptionId): if not self._future.done(): @@ -729,13 +730,13 @@ def handleSubscriptionEstablished(self, subscriptionId): self._event_loop.call_soon_threadsafe( self._handleSubscriptionEstablished, subscriptionId) - def handleResubscriptionAttempted(self, terminationCause: int, nextResubscribeIntervalMsec: int): + def handleResubscriptionAttempted(self, terminationCause: PyChipError, nextResubscribeIntervalMsec: int): if (self._subscription_handler._onResubscriptionAttemptedCb_isAsync): self._event_loop.create_task(self._subscription_handler._onResubscriptionAttemptedCb( - self._subscription_handler, terminationCause, nextResubscribeIntervalMsec)) + self._subscription_handler, terminationCause.code, nextResubscribeIntervalMsec)) else: self._event_loop.call_soon_threadsafe( - self._subscription_handler._onResubscriptionAttemptedCb, self._subscription_handler, terminationCause, nextResubscribeIntervalMsec) + self._subscription_handler._onResubscriptionAttemptedCb, self._subscription_handler, terminationCause.code, nextResubscribeIntervalMsec) def _handleReportBegin(self): pass @@ -800,7 +801,7 @@ def handleResponse(self, path: AttributePath, status: int): except: self._resultData.append(AttributeWriteResult(Path=path, Status=status)) - def handleError(self, chipError: int): + def handleError(self, chipError: PyChipError): self._resultError = chipError def _handleDone(self): @@ -810,7 +811,7 @@ def _handleDone(self): # move on, possibly invalidating the provided _event_loop. # if self._resultError is not None: - self._future.set_exception(chip.exceptions.ChipStackError(self._resultError)) + self._future.set_exception(self._resultError.to_exception()) else: self._future.set_result(self._resultData) @@ -828,11 +829,11 @@ def handleDone(self): _OnReadAttributeDataCallbackFunct = CFUNCTYPE( None, py_object, c_uint32, c_uint16, c_uint32, c_uint32, c_uint8, c_void_p, c_size_t) _OnSubscriptionEstablishedCallbackFunct = CFUNCTYPE(None, py_object, c_uint32) -_OnResubscriptionAttemptedCallbackFunct = CFUNCTYPE(None, py_object, c_uint32, c_uint32) +_OnResubscriptionAttemptedCallbackFunct = CFUNCTYPE(None, py_object, PyChipError, c_uint32) _OnReadEventDataCallbackFunct = CFUNCTYPE( None, py_object, c_uint16, c_uint32, c_uint32, c_uint64, c_uint8, c_uint64, c_uint8, c_void_p, c_size_t, c_uint8) _OnReadErrorCallbackFunct = CFUNCTYPE( - None, py_object, c_uint32) + None, py_object, PyChipError) _OnReadDoneCallbackFunct = CFUNCTYPE( None, py_object) _OnReportBeginCallbackFunct = CFUNCTYPE( @@ -862,12 +863,12 @@ def _OnSubscriptionEstablishedCallback(closure, subscriptionId): @_OnResubscriptionAttemptedCallbackFunct -def _OnResubscriptionAttemptedCallback(closure, terminationCause: int, nextResubscribeIntervalMsec: int): +def _OnResubscriptionAttemptedCallback(closure, terminationCause: PyChipError, nextResubscribeIntervalMsec: int): closure.handleResubscriptionAttempted(terminationCause, nextResubscribeIntervalMsec) @_OnReadErrorCallbackFunct -def _OnReadErrorCallback(closure, chiperror: int): +def _OnReadErrorCallback(closure, chiperror: PyChipError): closure.handleError(chiperror) @@ -889,7 +890,7 @@ def _OnReadDoneCallback(closure): _OnWriteResponseCallbackFunct = CFUNCTYPE( None, py_object, c_uint16, c_uint32, c_uint32, c_uint16) _OnWriteErrorCallbackFunct = CFUNCTYPE( - None, py_object, c_uint32) + None, py_object, PyChipError) _OnWriteDoneCallbackFunct = CFUNCTYPE( None, py_object) @@ -901,7 +902,7 @@ def _OnWriteResponseCallback(closure, endpoint: int, cluster: int, attribute: in @_OnWriteErrorCallbackFunct -def _OnWriteErrorCallback(closure, chiperror: int): +def _OnWriteErrorCallback(closure, chiperror: PyChipError): closure.handleError(chiperror) @@ -910,7 +911,7 @@ def _OnWriteDoneCallback(closure): closure.handleDone() -def WriteAttributes(future: Future, eventLoop, device, attributes: List[AttributeWriteRequest], timedRequestTimeoutMs: int = None, interactionTimeoutMs: int = None) -> int: +def WriteAttributes(future: Future, eventLoop, device, attributes: List[AttributeWriteRequest], timedRequestTimeoutMs: int = None, interactionTimeoutMs: int = None) -> PyChipError: handle = chip.native.GetLibraryHandle() writeargs = [] @@ -936,7 +937,7 @@ def WriteAttributes(future: Future, eventLoop, device, attributes: List[Attribut res = builtins.chipStack.Call( lambda: handle.pychip_WriteClient_WriteAttributes( ctypes.py_object(transaction), device, ctypes.c_uint16(0 if timedRequestTimeoutMs is None else timedRequestTimeoutMs), ctypes.c_uint16(0 if interactionTimeoutMs is None else interactionTimeoutMs), ctypes.c_size_t(len(attributes)), *writeargs)) - if res != 0: + if not res.is_success: ctypes.pythonapi.Py_DecRef(ctypes.py_object(transaction)) return res @@ -951,7 +952,7 @@ def WriteAttributes(future: Future, eventLoop, device, attributes: List[Attribut ) -def Read(future: Future, eventLoop, device, devCtrl, attributes: List[AttributePath] = None, dataVersionFilters: List[DataVersionFilter] = None, events: List[EventPath] = None, returnClusterObject: bool = True, subscriptionParameters: SubscriptionParameters = None, fabricFiltered: bool = True, keepSubscriptions: bool = False) -> int: +def Read(future: Future, eventLoop, device, devCtrl, attributes: List[AttributePath] = None, dataVersionFilters: List[DataVersionFilter] = None, events: List[EventPath] = None, returnClusterObject: bool = True, subscriptionParameters: SubscriptionParameters = None, fabricFiltered: bool = True, keepSubscriptions: bool = False) -> PyChipError: if (not attributes) and dataVersionFilters: raise ValueError( "Must provide valid attribute list when data version filters is not null") @@ -1047,7 +1048,7 @@ def Read(future: Future, eventLoop, device, devCtrl, attributes: List[AttributeP transaction.SetClientObjPointers(readClientObj, readCallbackObj) - if res != 0: + if not res.is_success: ctypes.pythonapi.Py_DecRef(ctypes.py_object(transaction)) return res @@ -1068,10 +1069,10 @@ def Init(): if not handle.pychip_WriteClient_InitCallbacks.argtypes: setter = chip.native.NativeLibraryHandleMethodArguments(handle) - handle.pychip_WriteClient_WriteAttributes.restype = c_uint32 + handle.pychip_WriteClient_WriteAttributes.restype = PyChipError setter.Set('pychip_WriteClient_InitCallbacks', None, [ _OnWriteResponseCallbackFunct, _OnWriteErrorCallbackFunct, _OnWriteDoneCallbackFunct]) - handle.pychip_ReadClient_Read.restype = c_uint32 + handle.pychip_ReadClient_Read.restype = PyChipError setter.Set('pychip_ReadClient_InitCallbacks', None, [ _OnReadAttributeDataCallbackFunct, _OnReadEventDataCallbackFunct, _OnSubscriptionEstablishedCallbackFunct, _OnResubscriptionAttemptedCallbackFunct, _OnReadErrorCallbackFunct, _OnReadDoneCallbackFunct, _OnReportBeginCallbackFunct, _OnReportEndCallbackFunct]) diff --git a/src/controller/python/chip/clusters/Command.py b/src/controller/python/chip/clusters/Command.py index 74101d4edb47cf..83026866e1b0e7 100644 --- a/src/controller/python/chip/clusters/Command.py +++ b/src/controller/python/chip/clusters/Command.py @@ -26,6 +26,7 @@ from .ClusterObjects import ClusterCommand import chip.exceptions import chip.interaction_model +from chip.native import PyChipError import inspect import sys @@ -99,12 +100,11 @@ def handleResponse(self, path: CommandPath, status: Status, response: bytes): self._event_loop.call_soon_threadsafe( self._handleResponse, path, status, response) - def _handleError(self, imError: Status, chipError: int, exception: Exception): + def _handleError(self, imError: Status, chipError: PyChipError, exception: Exception): if exception: self._future.set_exception(exception) elif chipError != 0: - self._future.set_exception( - chip.exceptions.ChipStackError(chipError)) + self._future.set_exception(chipError.to_exception()) else: try: self._future.set_exception( @@ -114,7 +114,7 @@ def _handleError(self, imError: Status, chipError: int, exception: Exception): self._future.set_exception(chip.interaction_model.InteractionModelError( chip.interaction_model.Status.Failure)) - def handleError(self, status: Status, chipError: int): + def handleError(self, status: Status, chipError: PyChipError): self._event_loop.call_soon_threadsafe( self._handleError, status, chipError, None ) @@ -123,7 +123,7 @@ def handleError(self, status: Status, chipError: int): _OnCommandSenderResponseCallbackFunct = CFUNCTYPE( None, py_object, c_uint16, c_uint32, c_uint32, c_uint16, c_uint8, c_void_p, c_uint32) _OnCommandSenderErrorCallbackFunct = CFUNCTYPE( - None, py_object, c_uint16, c_uint8, c_uint32) + None, py_object, c_uint16, c_uint8, PyChipError) _OnCommandSenderDoneCallbackFunct = CFUNCTYPE( None, py_object) @@ -136,7 +136,7 @@ def _OnCommandSenderResponseCallback(closure, endpoint: int, cluster: int, comma @_OnCommandSenderErrorCallbackFunct -def _OnCommandSenderErrorCallback(closure, imStatus: int, clusterStatus: int, chiperror: int): +def _OnCommandSenderErrorCallback(closure, imStatus: int, clusterStatus: int, chiperror: PyChipError): closure.handleError(Status(imStatus, clusterStatus), chiperror) @@ -145,7 +145,7 @@ def _OnCommandSenderDoneCallback(closure): ctypes.pythonapi.Py_DecRef(ctypes.py_object(closure)) -def SendCommand(future: Future, eventLoop, responseType: Type, device, commandPath: CommandPath, payload: ClusterCommand, timedRequestTimeoutMs: int = None, interactionTimeoutMs: int = None) -> int: +def SendCommand(future: Future, eventLoop, responseType: Type, device, commandPath: CommandPath, payload: ClusterCommand, timedRequestTimeoutMs: int = None, interactionTimeoutMs: int = None) -> PyChipError: ''' Send a cluster-object encapsulated command to a device and does the following: - On receipt of a successful data response, returns the cluster-object equivalent through the provided future. - None (on a successful response containing no data) @@ -183,7 +183,7 @@ def Init(): setter = chip.native.NativeLibraryHandleMethodArguments(handle) setter.Set('pychip_CommandSender_SendCommand', - c_uint32, [py_object, c_void_p, c_uint16, c_uint32, c_uint32, c_char_p, c_size_t, c_uint16]) + PyChipError, [py_object, c_void_p, c_uint16, c_uint32, c_uint32, c_char_p, c_size_t, c_uint16]) setter.Set('pychip_CommandSender_InitCallbacks', None, [ _OnCommandSenderResponseCallbackFunct, _OnCommandSenderErrorCallbackFunct, _OnCommandSenderDoneCallbackFunct]) diff --git a/src/controller/python/chip/clusters/attribute.cpp b/src/controller/python/chip/clusters/attribute.cpp index 0c90495ae4bf00..b847a200df315b 100644 --- a/src/controller/python/chip/clusters/attribute.cpp +++ b/src/controller/python/chip/clusters/attribute.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -74,9 +75,9 @@ using OnReadEventDataCallback = void (*)(PyObject * appContext, chip:: uint8_t timestampType, uint8_t * data, uint32_t dataLen, std::underlying_type_t imstatus); using OnSubscriptionEstablishedCallback = void (*)(PyObject * appContext, SubscriptionId subscriptionId); -using OnResubscriptionAttemptedCallback = void (*)(PyObject * appContext, uint32_t aTerminationCause, +using OnResubscriptionAttemptedCallback = void (*)(PyObject * appContext, PyChipError aTerminationCause, uint32_t aNextResubscribeIntervalMsec); -using OnReadErrorCallback = void (*)(PyObject * appContext, uint32_t chiperror); +using OnReadErrorCallback = void (*)(PyObject * appContext, PyChipError chiperror); using OnReadDoneCallback = void (*)(PyObject * appContext); using OnReportBeginCallback = void (*)(PyObject * appContext); using OnReportEndCallback = void (*)(PyObject * appContext); @@ -148,7 +149,7 @@ class ReadClientCallback : public ReadClient::Callback CHIP_ERROR OnResubscriptionNeeded(ReadClient * apReadClient, CHIP_ERROR aTerminationCause) override { ReturnErrorOnFailure(ReadClient::Callback::OnResubscriptionNeeded(apReadClient, aTerminationCause)); - gOnResubscriptionAttemptedCallback(mAppContext, aTerminationCause.AsInteger(), + gOnResubscriptionAttemptedCallback(mAppContext, ToPyChipError(aTerminationCause), apReadClient->ComputeTimeTillNextSubscription()); return CHIP_NO_ERROR; } @@ -192,7 +193,7 @@ class ReadClientCallback : public ReadClient::Callback to_underlying(apStatus == nullptr ? Protocols::InteractionModel::Status::Success : apStatus->mStatus)); } - void OnError(CHIP_ERROR aError) override { gOnReadErrorCallback(mAppContext, aError.AsInteger()); } + void OnError(CHIP_ERROR aError) override { gOnReadErrorCallback(mAppContext, ToPyChipError(aError)); } void OnReportBegin() override { gOnReportBeginCallback(mAppContext); } void OnDeallocatePaths(chip::app::ReadPrepareParams && aReadPrepareParams) override @@ -244,18 +245,16 @@ struct __attribute__((packed)) PyReadAttributeParams }; // Encodes n attribute write requests, follows 3 * n arguments, in the (AttributeWritePath*=void *, uint8_t*, size_t) order. -chip::ChipError::StorageType pychip_WriteClient_WriteAttributes(void * appContext, DeviceProxy * device, - uint16_t timedWriteTimeoutMs, uint16_t interactionTimeoutMs, - size_t n, ...); -chip::ChipError::StorageType pychip_ReadClient_ReadAttributes(void * appContext, ReadClient ** pReadClient, - ReadClientCallback ** pCallback, DeviceProxy * device, - uint8_t * readParamsBuf, size_t n, size_t total, ...); +PyChipError pychip_WriteClient_WriteAttributes(void * appContext, DeviceProxy * device, uint16_t timedWriteTimeoutMs, + uint16_t interactionTimeoutMs, size_t n, ...); +PyChipError pychip_ReadClient_ReadAttributes(void * appContext, ReadClient ** pReadClient, ReadClientCallback ** pCallback, + DeviceProxy * device, uint8_t * readParamsBuf, size_t n, size_t total, ...); } using OnWriteResponseCallback = void (*)(PyObject * appContext, chip::EndpointId endpointId, chip::ClusterId clusterId, chip::AttributeId attributeId, std::underlying_type_t imstatus); -using OnWriteErrorCallback = void (*)(PyObject * appContext, uint32_t chiperror); +using OnWriteErrorCallback = void (*)(PyObject * appContext, PyChipError chiperror); using OnWriteDoneCallback = void (*)(PyObject * appContext); OnWriteResponseCallback gOnWriteResponseCallback = nullptr; @@ -277,7 +276,7 @@ class WriteClientCallback : public WriteClient::Callback void OnError(const WriteClient * apWriteClient, CHIP_ERROR aProtocolError) override { - gOnWriteErrorCallback(mAppContext, aProtocolError.AsInteger()); + gOnWriteErrorCallback(mAppContext, ToPyChipError(aProtocolError)); } void OnDone(WriteClient * apWriteClient) override @@ -323,9 +322,8 @@ void pychip_ReadClient_InitCallbacks(OnReadAttributeDataCallback onReadAttribute gOnReportEndCallback = onReportEndCallback; } -chip::ChipError::StorageType pychip_WriteClient_WriteAttributes(void * appContext, DeviceProxy * device, - uint16_t timedWriteTimeoutMs, uint16_t interactionTimeoutMs, - size_t n, ...) +PyChipError pychip_WriteClient_WriteAttributes(void * appContext, DeviceProxy * device, uint16_t timedWriteTimeoutMs, + uint16_t interactionTimeoutMs, size_t n, ...) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -374,7 +372,7 @@ chip::ChipError::StorageType pychip_WriteClient_WriteAttributes(void * appContex exit: va_end(args); - return err.AsInteger(); + return ToPyChipError(err); } void pychip_ReadClient_Abort(ReadClient * apReadClient, ReadClientCallback * apCallback) @@ -391,9 +389,9 @@ void pychip_ReadClient_OverrideLivenessTimeout(ReadClient * pReadClient, uint32_ pReadClient->OverrideLivenessTimeout(System::Clock::Milliseconds32(livenessTimeoutMs)); } -chip::ChipError::StorageType pychip_ReadClient_Read(void * appContext, ReadClient ** pReadClient, ReadClientCallback ** pCallback, - DeviceProxy * device, uint8_t * readParamsBuf, size_t numAttributePaths, - size_t numDataversionFilters, size_t numEventPaths, ...) +PyChipError pychip_ReadClient_Read(void * appContext, ReadClient ** pReadClient, ReadClientCallback ** pCallback, + DeviceProxy * device, uint8_t * readParamsBuf, size_t numAttributePaths, + size_t numDataversionFilters, size_t numEventPaths, ...) { CHIP_ERROR err = CHIP_NO_ERROR; PyReadAttributeParams pyParams = {}; @@ -496,6 +494,6 @@ chip::ChipError::StorageType pychip_ReadClient_Read(void * appContext, ReadClien exit: va_end(args); - return err.AsInteger(); + return ToPyChipError(err); } } diff --git a/src/controller/python/chip/clusters/command.cpp b/src/controller/python/chip/clusters/command.cpp index 342e624698ed18..9c4686b9000317 100644 --- a/src/controller/python/chip/clusters/command.cpp +++ b/src/controller/python/chip/clusters/command.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -32,11 +33,9 @@ using namespace chip::app; using PyObject = void *; extern "C" { -chip::ChipError::StorageType pychip_CommandSender_SendCommand(void * appContext, DeviceProxy * device, - uint16_t timedRequestTimeoutMs, chip::EndpointId endpointId, - chip::ClusterId clusterId, chip::CommandId commandId, - const uint8_t * payload, size_t length, - uint16_t interactionTimeoutMs); +PyChipError pychip_CommandSender_SendCommand(void * appContext, DeviceProxy * device, uint16_t timedRequestTimeoutMs, + chip::EndpointId endpointId, chip::ClusterId clusterId, chip::CommandId commandId, + const uint8_t * payload, size_t length, uint16_t interactionTimeoutMs); } namespace chip { @@ -48,7 +47,7 @@ using OnCommandSenderResponseCallback = void (*)(PyObject appContext, chip::Endp chip::ClusterStatus clusterStatus, const uint8_t * payload, uint32_t length); using OnCommandSenderErrorCallback = void (*)(PyObject appContext, std::underlying_type_t status, - chip::ClusterStatus clusterStatus, uint32_t chiperror); + chip::ClusterStatus clusterStatus, PyChipError chiperror); using OnCommandSenderDoneCallback = void (*)(PyObject appContext); OnCommandSenderResponseCallback gOnCommandSenderResponseCallback = nullptr; @@ -96,7 +95,7 @@ class CommandSenderCallback : public CommandSender::Callback // for the error code, because otherwise // the callee will think we have a stack // exception. - aProtocolError.IsIMStatus() ? 0 : aProtocolError.AsInteger()); + aProtocolError.IsIMStatus() ? ToPyChipError(CHIP_NO_ERROR) : ToPyChipError(aProtocolError)); } void OnDone(CommandSender * apCommandSender) override @@ -125,14 +124,13 @@ void pychip_CommandSender_InitCallbacks(OnCommandSenderResponseCallback onComman gOnCommandSenderDoneCallback = onCommandSenderDoneCallback; } -chip::ChipError::StorageType pychip_CommandSender_SendCommand(void * appContext, DeviceProxy * device, - uint16_t timedRequestTimeoutMs, chip::EndpointId endpointId, - chip::ClusterId clusterId, chip::CommandId commandId, - const uint8_t * payload, size_t length, uint16_t interactionTimeoutMs) +PyChipError pychip_CommandSender_SendCommand(void * appContext, DeviceProxy * device, uint16_t timedRequestTimeoutMs, + chip::EndpointId endpointId, chip::ClusterId clusterId, chip::CommandId commandId, + const uint8_t * payload, size_t length, uint16_t interactionTimeoutMs) { CHIP_ERROR err = CHIP_NO_ERROR; - VerifyOrReturnError(device->GetSecureSession().HasValue(), CHIP_ERROR_MISSING_SECURE_SESSION.AsInteger()); + VerifyOrReturnError(device->GetSecureSession().HasValue(), ToPyChipError(CHIP_ERROR_MISSING_SECURE_SESSION)); std::unique_ptr callback = std::make_unique(appContext); std::unique_ptr sender = std::make_unique(callback.get(), device->GetExchangeManager(), @@ -164,6 +162,6 @@ chip::ChipError::StorageType pychip_CommandSender_SendCommand(void * appContext, callback.release(); exit: - return err.AsInteger(); + return ToPyChipError(err); } } diff --git a/src/controller/python/chip/discovery/NodeResolution.cpp b/src/controller/python/chip/discovery/NodeResolution.cpp index 2bf44a2a01ccab..8f92477f1c1ef0 100644 --- a/src/controller/python/chip/discovery/NodeResolution.cpp +++ b/src/controller/python/chip/discovery/NodeResolution.cpp @@ -16,6 +16,7 @@ */ #include +#include #include #include #include @@ -25,13 +26,11 @@ using namespace chip; using namespace chip::Dnssd; -static_assert(std::is_same::value, "python assumes CHIP_ERROR maps to c_uint32"); - namespace { // callback types shared with python code (see ptyhon code in chip.discovery.types) using DiscoverSuccessCallback = void (*)(uint64_t fabricId, uint64_t nodeId, uint32_t interfaceId, const char * ip, uint16_t port); -using DiscoverFailureCallback = void (*)(uint64_t fabricId, uint64_t nodeId, ChipError::StorageType error_code); +using DiscoverFailureCallback = void (*)(uint64_t fabricId, uint64_t nodeId, PyChipError error_code); class PythonResolverDelegate : public OperationalResolveDelegate { @@ -62,7 +61,7 @@ class PythonResolverDelegate : public OperationalResolveDelegate { if (mFailureCallback != nullptr) { - mFailureCallback(peerId.GetCompressedFabricId(), peerId.GetNodeId(), error.AsInteger()); + mFailureCallback(peerId.GetCompressedFabricId(), peerId.GetNodeId(), ToPyChipError(error)); } else { @@ -88,7 +87,7 @@ extern "C" void pychip_discovery_set_callbacks(DiscoverSuccessCallback success, gPythonResolverDelegate.SetFailureCallback(failure); } -extern "C" ChipError::StorageType pychip_discovery_resolve(uint64_t fabricId, uint64_t nodeId) +extern "C" PyChipError pychip_discovery_resolve(uint64_t fabricId, uint64_t nodeId) { CHIP_ERROR result = CHIP_NO_ERROR; @@ -101,5 +100,5 @@ extern "C" ChipError::StorageType pychip_discovery_resolve(uint64_t fabricId, ui chip::Inet::IPAddressType::kAny); }); - return result.AsInteger(); + return ToPyChipError(result); } diff --git a/src/controller/python/chip/discovery/__init__.py b/src/controller/python/chip/discovery/__init__.py index a6897949c26813..d41325f5ff5a8d 100644 --- a/src/controller/python/chip/discovery/__init__.py +++ b/src/controller/python/chip/discovery/__init__.py @@ -24,6 +24,7 @@ from chip.discovery.library_handle import _GetDiscoveryLibraryHandle from chip.discovery.types import DiscoverSuccessCallback_t, DiscoverFailureCallback_t +from chip.native import PyChipError class FilterType(enum.IntEnum): @@ -201,10 +202,10 @@ def _DiscoverSuccess(fabric: int, node: int, interface: int, ip: str, port: int @DiscoverFailureCallback_t -def _DiscoverFailure(fabric: int, node: int, errorCode: int): +def _DiscoverFailure(fabric: int, node: int, errorCode: PyChipError): # Many discovery errors currently do not include a useful node/fabric id # hence we just log and rely on discovery timeouts to return 'no data' - logging.error("Discovery failure, error %d", errorCode) + logging.error("Discovery failure, error %d", errorCode.code) def FindAddressAsync(fabricid: int, nodeid: int, callback, timeout_ms=1000): diff --git a/src/controller/python/chip/discovery/library_handle.py b/src/controller/python/chip/discovery/library_handle.py index edf31631e3ac78..19ad5e8954181c 100644 --- a/src/controller/python/chip/discovery/library_handle.py +++ b/src/controller/python/chip/discovery/library_handle.py @@ -17,6 +17,7 @@ import chip.native import ctypes from chip.discovery.types import DiscoverSuccessCallback_t, DiscoverFailureCallback_t +from chip.native import PyChipError def _GetDiscoveryLibraryHandle() -> ctypes.CDLL: @@ -33,7 +34,7 @@ def _GetDiscoveryLibraryHandle() -> ctypes.CDLL: if not handle.pychip_discovery_resolve.argtypes: setter = chip.native.NativeLibraryHandleMethodArguments(handle) - setter.Set('pychip_discovery_resolve', ctypes.c_uint32, + setter.Set('pychip_discovery_resolve', PyChipError, [ctypes.c_uint64, ctypes.c_uint64]) setter.Set('pychip_discovery_set_callbacks', None, [ DiscoverSuccessCallback_t, DiscoverFailureCallback_t]) diff --git a/src/controller/python/chip/discovery/types.py b/src/controller/python/chip/discovery/types.py index 775c2420559bbf..34868b804f1bfc 100644 --- a/src/controller/python/chip/discovery/types.py +++ b/src/controller/python/chip/discovery/types.py @@ -15,6 +15,7 @@ # from ctypes import CFUNCTYPE, c_char_p, c_uint16, c_uint32, c_uint64 +from chip.native import PyChipError DiscoverSuccessCallback_t = CFUNCTYPE( @@ -30,5 +31,5 @@ None, # void return c_uint64, # fabric id c_uint64, # node id - c_uint32, # CHIP_ERROR error code + PyChipError, # CHIP_ERROR error code ) diff --git a/src/controller/python/chip/interaction_model/Delegate.cpp b/src/controller/python/chip/interaction_model/Delegate.cpp index 20ce1d2747b678..827d753b68cfc2 100644 --- a/src/controller/python/chip/interaction_model/Delegate.cpp +++ b/src/controller/python/chip/interaction_model/Delegate.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -49,15 +50,13 @@ void pychip_InteractionModelDelegate_SetOnWriteResponseStatusCallback(PythonInte extern "C" { -static_assert(std::is_same::value, "python assumes CHIP_ERROR maps to c_uint32"); - -chip::ChipError::StorageType pychip_InteractionModel_GetCommandSenderHandle(uint64_t * commandSender) +PyChipError pychip_InteractionModel_GetCommandSenderHandle(uint64_t * commandSender) { chip::app::CommandSender * commandSenderObj = nullptr; - VerifyOrReturnError(commandSender != nullptr, CHIP_ERROR_INVALID_ARGUMENT.AsInteger()); + VerifyOrReturnError(commandSender != nullptr, ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT)); commandSenderObj = new (std::nothrow) chip::app::CommandSender(nullptr, nullptr); - VerifyOrReturnError(commandSenderObj != nullptr, (CHIP_ERROR_NO_MEMORY).AsInteger()); + VerifyOrReturnError(commandSenderObj != nullptr, ToPyChipError((CHIP_ERROR_NO_MEMORY))); *commandSender = reinterpret_cast(commandSenderObj); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } } diff --git a/src/controller/python/chip/internal/CommissionerImpl.cpp b/src/controller/python/chip/internal/CommissionerImpl.cpp index 260a9c026dd641..96ab90f44b8ae2 100644 --- a/src/controller/python/chip/internal/CommissionerImpl.cpp +++ b/src/controller/python/chip/internal/CommissionerImpl.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -203,22 +204,20 @@ extern "C" chip::Controller::DeviceCommissioner * pychip_internal_Commissioner_N return result.release(); } -static_assert(std::is_same::value, "python assumes CHIP_ERROR maps to c_uint32"); - /// Returns CHIP_ERROR corresponding to an UnpairDevice call -extern "C" chip::ChipError::StorageType pychip_internal_Commissioner_Unpair(chip::Controller::DeviceCommissioner * commissioner, - uint64_t remoteDeviceId) +extern "C" PyChipError pychip_internal_Commissioner_Unpair(chip::Controller::DeviceCommissioner * commissioner, + uint64_t remoteDeviceId) { CHIP_ERROR err; chip::python::ChipMainThreadScheduleAndWait([&]() { err = commissioner->UnpairDevice(remoteDeviceId); }); - return err.AsInteger(); + return ToPyChipError(err); } -extern "C" chip::ChipError::StorageType -pychip_internal_Commissioner_BleConnectForPairing(chip::Controller::DeviceCommissioner * commissioner, uint64_t remoteNodeId, - uint32_t pinCode, uint16_t discriminator) +extern "C" PyChipError pychip_internal_Commissioner_BleConnectForPairing(chip::Controller::DeviceCommissioner * commissioner, + uint64_t remoteNodeId, uint32_t pinCode, + uint16_t discriminator) { CHIP_ERROR err; @@ -234,5 +233,5 @@ pychip_internal_Commissioner_BleConnectForPairing(chip::Controller::DeviceCommis err = commissioner->PairDevice(remoteNodeId, params); }); - return err.AsInteger(); + return ToPyChipError(err); } diff --git a/src/controller/python/chip/native/CommonStackInit.cpp b/src/controller/python/chip/native/CommonStackInit.cpp index f01bcf8799fbfb..8a149412a73944 100644 --- a/src/controller/python/chip/native/CommonStackInit.cpp +++ b/src/controller/python/chip/native/CommonStackInit.cpp @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -40,8 +41,6 @@ using namespace chip; -static_assert(std::is_same::value, "python assumes CHIP_ERROR maps to c_uint32"); - extern "C" { struct __attribute__((packed)) PyCommonStackInitParams @@ -60,19 +59,19 @@ void pychip_CauseCrash() *ptr = 0; } -ChipError::StorageType pychip_CommonStackInit(const PyCommonStackInitParams * aParams) +PyChipError pychip_CommonStackInit(const PyCommonStackInitParams * aParams) { - ReturnErrorOnFailure(Platform::MemoryInit().AsInteger()); + PyReturnErrorOnFailure(ToPyChipError(Platform::MemoryInit())); #if CHIP_DEVICE_LAYER_TARGET_LINUX && CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE // By default, Linux device is configured as a BLE peripheral while the controller needs a BLE central. - ReturnErrorOnFailure( - DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(aParams->mBluetoothAdapterId, /* BLE central */ true).AsInteger()); + PyReturnErrorOnFailure( + ToPyChipError(DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(aParams->mBluetoothAdapterId, /* BLE central */ true))); #endif - ReturnErrorOnFailure(DeviceLayer::PlatformMgr().InitChipStack().AsInteger()); + PyReturnErrorOnFailure(ToPyChipError(DeviceLayer::PlatformMgr().InitChipStack())); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } void pychip_CommonStackShutdown() diff --git a/src/controller/python/chip/native/PyChipError.cpp b/src/controller/python/chip/native/PyChipError.cpp new file mode 100644 index 00000000000000..58309c1a061fa7 --- /dev/null +++ b/src/controller/python/chip/native/PyChipError.cpp @@ -0,0 +1,46 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "PyChipError.h" + +#include + +#include + +using namespace chip; +using namespace chip::DeviceLayer; + +void pychip_FormatError(PyChipError * apError, char * apBuf, uint32_t aBufSize) +{ + // Note: ChipError::AsString is not thread safe since it accesses a global (and mutable) variable for holding the formatted + // string. + // TODO: We should consider providing an API for putting the string into a user provided buffer to avoid potential race + // conditions. +#if CHIP_STACK_LOCK_TRACKING_ENABLED + if (!PlatformMgr().IsChipStackLockedByCurrentThread()) + { + PlatformMgr().LockChipStack(); + snprintf(apBuf, aBufSize, "%s", CHIP_ERROR(apError->mCode, apError->mFile, apError->mLine).AsString()); + PlatformMgr().UnlockChipStack(); + } + else +#endif + { + snprintf(apBuf, aBufSize, "%s", CHIP_ERROR(apError->mCode, apError->mFile, apError->mLine).AsString()); + } +} diff --git a/src/controller/python/chip/native/PyChipError.h b/src/controller/python/chip/native/PyChipError.h new file mode 100644 index 00000000000000..ed36c4c3e10fb2 --- /dev/null +++ b/src/controller/python/chip/native/PyChipError.h @@ -0,0 +1,89 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +// Note: We can use a constructor here to avoid "ToPyChipError", but it will result in compilers warning for incompatible with C +// linkage. +struct PyChipError +{ + uint32_t mCode; + uint32_t mLine; + const char * mFile; +}; + +// To avoid the error message "user-defined type 'PyChipError' which is incompatible with C" + +inline bool operator!(const PyChipError & aError) +{ + return aError.mCode != 0; +} + +inline bool operator==(const PyChipError & aError, const CHIP_ERROR & aOther) +{ + return aError.mCode == aOther.AsInteger(); +} + +inline bool operator==(const CHIP_ERROR & aOther, const PyChipError & aError) +{ + return aError.mCode == aOther.AsInteger(); +} + +inline bool operator!=(const PyChipError & aError, const CHIP_ERROR & aOther) +{ + return !(aError.mCode == aOther.AsInteger()); +} + +inline bool operator!=(const CHIP_ERROR & aOther, const PyChipError & aError) +{ + return !(aError.mCode == aOther.AsInteger()); +} + +inline PyChipError ToPyChipError(const CHIP_ERROR & aError) +{ + return PyChipError + { + .mCode = aError.AsInteger(), +#if CHIP_CONFIG_ERROR_SOURCE + .mLine = aError.GetLine(), .mFile = aError.GetFile(), +#else + .mLine = 0, .mFile = nullptr, +#endif + }; +} + +// A version of ReturnErrorOnFailure which can use PyChipError as expr +// Note: we should consider implement operator == for CHIP_ERROR then we can overload it easily. +#define PyReturnErrorOnFailure(expr) \ + do \ + { \ + auto __err = (expr); \ + if (__err != CHIP_NO_ERROR) \ + { \ + return __err; \ + } \ + } while (false) + +static_assert(std::is_same::value, + "python assumes CHIP_ERROR maps to c_uint32"); + +extern "C" { +void pychip_FormatError(PyChipError * apError, char * apBuf, uint32_t aBufSize); +} diff --git a/src/controller/python/chip/native/__init__.py b/src/controller/python/chip/native/__init__.py index f0c175237e7b8a..1810c18c62f69d 100644 --- a/src/controller/python/chip/native/__init__.py +++ b/src/controller/python/chip/native/__init__.py @@ -3,6 +3,9 @@ import os import platform import construct +import chip.exceptions +import typing +import enum NATIVE_LIBRARY_BASE_NAME = "_ChipDeviceCtrl.so" @@ -18,6 +21,100 @@ def _AllDirsToRoot(dir): dir = parent +class ErrorRange(enum.IntEnum): + ''' The enum of chip::ChipError::Range + ''' + SDK = 0x0 + OS = 0x1 + POSIX = 0x2 + LWIP = 0x3 + OPENTHREAD = 0x4 + PLATFROM = 0x5 + + +class ErrorSDKPart(enum.IntEnum): + ''' The enum of chip::ChipError::SDKPart + ''' + CORE = 0 + INET = 1 + DEVICE = 2 + ASN1 = 3 + BLE = 4 + IM_GLOBAL_STATUS = 5 + IM_CLUSTER_STATUS = 6 + APPLICATION = 7 + + +class PyChipError(ctypes.Structure): + ''' The ChipError for Python library. + + We are using the following struct for passing the infomations of CHIP_ERROR between C++ and Python: + + ```c + struct PyChipError + { + uint32_t mCode; + uint32_t mLine; + const char * mFile; + }; + ``` + ''' + _fields_ = [('code', ctypes.c_uint32), ('line', ctypes.c_uint32), ('file', ctypes.c_void_p)] + + def raise_on_error(self) -> None: + if self.code != 0: + raise self.to_exception() + + @property + def is_success(self) -> bool: + return self.code == 0 + + @property + def is_sdk_error(self) -> bool: + return self.range == ErrorRange.SDK + + @property + def range(self) -> ErrorRange: + return ErrorRange((self.code >> 24) & 0xFF) + + @property + def value(self) -> int: + return (self.code) & 0xFFFFFF + + @property + def sdk_part(self) -> ErrorSDKPart: + if not self.is_sdk_error: + return None + return ErrorSDKPart((self.code >> 8) & 0x07) + + @property + def sdk_code(self) -> int: + if not self.is_sdk_error: + return None + return self.code & 0xFF + + def to_exception(self) -> typing.Union[None, chip.exceptions.ChipStackError]: + if not self.is_success: + return chip.exceptions.ChipStackError(self.code, str(self)) + + def __str__(self): + buf = ctypes.create_string_buffer(256) + GetLibraryHandle().pychip_FormatError(ctypes.pointer(self), buf, 256) + return buf.value.decode() + + def __eq__(self, other): + if isinstance(other, int): + return self.code == other + if isinstance(other, PyChipError): + return self.code == other.code + if isinstance(other, chip.exceptions.ChipStackError): + return self.code == other.err + raise ValueError(f"Cannot compare PyChipError with {type(other)}") + + def __ne__(self, other): + return not self == other + + def FindNativeLibraryPath() -> str: """Find the native CHIP dll/so path.""" @@ -79,7 +176,8 @@ def _GetLibraryHandle(shouldInit: bool) -> ctypes.CDLL: raise Exception("Common stack has not been initialized!") _nativeLibraryHandle = ctypes.CDLL(FindNativeLibraryPath()) setter = NativeLibraryHandleMethodArguments(_nativeLibraryHandle) - setter.Set("pychip_CommonStackInit", ctypes.c_uint32, [ctypes.c_char_p]) + setter.Set("pychip_CommonStackInit", PyChipError, [ctypes.c_char_p]) + setter.Set("pychip_FormatError", None, [ctypes.POINTER(PyChipError), ctypes.c_char_p, ctypes.c_uint32]) return _nativeLibraryHandle diff --git a/src/controller/python/chip/setup_payload/Generator.cpp b/src/controller/python/chip/setup_payload/Generator.cpp index 4211fc3248dfb6..4481a404462394 100644 --- a/src/controller/python/chip/setup_payload/Generator.cpp +++ b/src/controller/python/chip/setup_payload/Generator.cpp @@ -15,6 +15,7 @@ * limitations under the License. */ +#include #include #include #include @@ -24,11 +25,9 @@ using namespace chip; -static_assert(std::is_same::value, "python assumes CHIP_ERROR maps to c_uint32"); - -extern "C" ChipError::StorageType pychip_SetupPayload_PrintOnboardingCodes(uint32_t passcode, uint16_t vendorId, uint16_t productId, - uint16_t discriminator, uint8_t customFlow, - uint8_t capabilities, uint8_t version) +extern "C" PyChipError pychip_SetupPayload_PrintOnboardingCodes(uint32_t passcode, uint16_t vendorId, uint16_t productId, + uint16_t discriminator, uint8_t customFlow, uint8_t capabilities, + uint8_t version) { std::string QRCode; std::string manualPairingCode; @@ -55,16 +54,16 @@ extern "C" ChipError::StorageType pychip_SetupPayload_PrintOnboardingCodes(uint3 break; default: ChipLogError(SetupPayload, "Invalid Custom Flow"); - return CHIP_ERROR_INVALID_ARGUMENT.AsInteger(); + return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); } CHIP_ERROR err = ManualSetupPayloadGenerator(payload).payloadDecimalStringRepresentation(manualPairingCode); - VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); + VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err)); ChipLogProgress(SetupPayload, "Manual pairing code: [%s]", manualPairingCode.c_str()); err = QRCodeSetupPayloadGenerator(payload).payloadBase38Representation(QRCode); - VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); + VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err)); ChipLogProgress(SetupPayload, "SetupQRCode: [%s]", QRCode.c_str()); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } diff --git a/src/controller/python/chip/setup_payload/Parser.cpp b/src/controller/python/chip/setup_payload/Parser.cpp index 83266f1fc4c469..c48dc3f3722c13 100644 --- a/src/controller/python/chip/setup_payload/Parser.cpp +++ b/src/controller/python/chip/setup_payload/Parser.cpp @@ -15,6 +15,7 @@ * limitations under the License. */ +#include #include #include #include @@ -24,8 +25,6 @@ using namespace chip; -static_assert(std::is_same::value, "python assumes CHIP_ERROR maps to c_uint32"); - namespace { using AttributeVisitor = void (*)(const char * attrName, const char * attrValue); @@ -69,25 +68,24 @@ void YieldSetupPayloadAttributes(const SetupPayload & payload, AttributeVisitor } // namespace -extern "C" ChipError::StorageType pychip_SetupPayload_ParseQrCode(const char * qrCode, AttributeVisitor attrVisitor, - VendorAttributeVisitor vendorAttrVisitor) +extern "C" PyChipError pychip_SetupPayload_ParseQrCode(const char * qrCode, AttributeVisitor attrVisitor, + VendorAttributeVisitor vendorAttrVisitor) { SetupPayload payload; CHIP_ERROR err = QRCodeSetupPayloadParser(qrCode).populatePayload(payload); - VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); + VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err)); YieldSetupPayloadAttributes(payload, attrVisitor, vendorAttrVisitor); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } -extern "C" ChipError::StorageType pychip_SetupPayload_ParseManualPairingCode(const char * manualPairingCode, - AttributeVisitor attrVisitor, - VendorAttributeVisitor vendorAttrVisitor) +extern "C" PyChipError pychip_SetupPayload_ParseManualPairingCode(const char * manualPairingCode, AttributeVisitor attrVisitor, + VendorAttributeVisitor vendorAttrVisitor) { SetupPayload payload; CHIP_ERROR err = ManualSetupPayloadParser(manualPairingCode).populatePayload(payload); - VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger()); + VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err)); YieldSetupPayloadAttributes(payload, attrVisitor, vendorAttrVisitor); - return CHIP_NO_ERROR.AsInteger(); + return ToPyChipError(CHIP_NO_ERROR); } diff --git a/src/controller/python/chip/utils/DeviceProxyUtils.cpp b/src/controller/python/chip/utils/DeviceProxyUtils.cpp index ea6add7a039609..d3bb2c6e65e204 100644 --- a/src/controller/python/chip/utils/DeviceProxyUtils.cpp +++ b/src/controller/python/chip/utils/DeviceProxyUtils.cpp @@ -27,13 +27,12 @@ #include "system/SystemClock.h" #include +#include #include #include using namespace chip; -static_assert(std::is_same::value, "python assumes CHIP_ERROR maps to c_uint32"); - extern "C" { /** diff --git a/src/controller/python/test/test_scripts/base.py b/src/controller/python/test/test_scripts/base.py index cab2f665ffe65d..ca4c30a4a5a93b 100644 --- a/src/controller/python/test/test_scripts/base.py +++ b/src/controller/python/test/test_scripts/base.py @@ -931,11 +931,7 @@ async def OnResubscriptionSucceeded(transaction): def TestCloseSession(self, nodeid: int): self.logger.info(f"Closing sessions with device {nodeid}") try: - err = self.devCtrl.CloseSession(nodeid) - if err != 0: - self.logger.exception( - f"Failed to close sessions with device {nodeid}: {err}") - return False + self.devCtrl.CloseSession(nodeid) return True except Exception as ex: self.logger.exception(