Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python] Implement PyChipError for passing detailed error infomation to Python API #22224

Merged
merged 8 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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