Skip to content

Commit

Permalink
[python] Implement PyChipError for passing detailed error infomation …
Browse files Browse the repository at this point in the history
…to Python API (#22224)

* [python] Use PyChipError for more detailed error message

* Add pychip_FormatError

* Add some getters for PyChipError

* Avoid return PyChipError to end user

* Fix

* Add raise_on_error to existing calls and cleanup
  • Loading branch information
erjiaqing authored and pull[bot] committed Jun 25, 2023
1 parent eb08873 commit 2641458
Show file tree
Hide file tree
Showing 30 changed files with 710 additions and 552 deletions.
1 change: 1 addition & 0 deletions src/controller/python/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

#include <controller/CHIPCommissionableNodeController.h>
#include <controller/python/chip/native/PyChipError.h>
#include <inet/IPAddress.h>
#include <lib/support/BytesToHex.h>
#include <lib/support/logging/CHIPLogging.h>
Expand All @@ -33,43 +34,41 @@
using namespace chip;
using namespace chip::Controller;

static_assert(std::is_same<uint32_t, ChipError::StorageType>::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)
{
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(
Expand Down
11 changes: 6 additions & 5 deletions src/controller/python/ChipDeviceController-Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

#include <controller/CHIPDeviceController.h>
#include <controller/python/chip/native/PyChipError.h>
#include <json/json.h>
#include <lib/core/CHIPError.h>
#include <lib/core/CHIPTLV.h>
Expand All @@ -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<Dnssd::DiscoveryFilterType>(filterType));
switch (static_cast<Dnssd::DiscoveryFilterType>(filterType))
Expand All @@ -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<uint64_t>(numericalArg);
break;
Expand All @@ -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,
Expand Down
29 changes: 13 additions & 16 deletions src/controller/python/ChipDeviceController-IssueNocChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>

#include <controller/CHIPDeviceController.h>
#include <controller/python/chip/native/PyChipError.h>
#include <lib/core/CHIPError.h>
#include <lib/support/Span.h>

Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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<Controller::OnNOCChainGeneration>(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<Controller::OnNOCChainGeneration>(pychip_DeviceController_IssueNOCChainCallback, pythonContext)));
}
Loading

0 comments on commit 2641458

Please sign in to comment.