Skip to content

Commit

Permalink
[Python] Add python commissioning flow
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing committed Jan 9, 2023
1 parent 7f668cd commit a656dc4
Show file tree
Hide file tree
Showing 11 changed files with 1,135 additions and 167 deletions.
1 change: 1 addition & 0 deletions scripts/tests/cirque_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ CIRQUE_TESTS=(
"SplitCommissioningTest"
"CommissioningFailureTest"
"CommissioningFailureOnReportTest"
"CustomCommissioningTest"
)

BOLD_GREEN_TEXT="\033[1;32m"
Expand Down
4 changes: 4 additions & 0 deletions src/controller/python/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ chip_python_wheel_action("chip-core") {
"chip/clusters/Attribute.py",
"chip/clusters/Command.py",
"chip/clusters/__init__.py",
"chip/commissioning/__init__.py",
"chip/commissioning/commissioning_flow_blocks.py",
"chip/commissioning/pase.py",
"chip/configuration/__init__.py",
"chip/discovery/__init__.py",
"chip/discovery/library_handle.py",
Expand Down Expand Up @@ -270,6 +273,7 @@ chip_python_wheel_action("chip-core") {
"chip.ble",
"chip.ble.commissioning",
"chip.configuration",
"chip.commissioning",
"chip.clusters",
"chip.utils",
"chip.discovery",
Expand Down
65 changes: 65 additions & 0 deletions src/controller/python/OpCredsBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <lib/support/TestGroupData.h>
#include <lib/support/logging/CHIPLogging.h>

#include <controller/python/chip/commissioning/DummyOperationalCredentialsIssuer.h>
#include <controller/python/chip/native/PyChipError.h>
#include <credentials/GroupDataProviderImpl.h>
#include <credentials/attestation_verifier/DefaultDeviceAttestationVerifier.h>
Expand All @@ -57,6 +58,8 @@ const chip::Credentials::AttestationTrustStore * GetTestFileAttestationTrustStor

return &attestationTrustStore;
}

chip::Python::DummyOperationalCredentialsIssuer sDummyOperationalCredentialsIssuer;
} // namespace

namespace chip {
Expand Down Expand Up @@ -319,6 +322,68 @@ void pychip_OnCommissioningStatusUpdate(chip::PeerId peerId, chip::Controller::C
return sTestCommissioner.OnCommissioningStatusUpdate(peerId, stageCompleted, err);
}

/**
* By using pychip_OpCreds_AllocateControllerForCustomCommissioningFlow, we will not using the commissioning flow from the
* Controller class.
*
*/
PyChipError pychip_OpCreds_AllocateControllerForCustomCommissioningFlow(chip::Controller::DeviceCommissioner ** outDevCtrl,
uint8_t * serializedEphemeralKey,
uint32_t serializedEphemeralKeyLen, uint8_t * noc,
uint32_t nocLen, uint8_t * icac, uint32_t icacLen,
uint8_t * rcac, uint32_t rcacLen,
chip::VendorId adminVendorId, bool enableServerInteractions)
{
ChipLogDetail(Controller, "Creating New Device Controller");

auto devCtrl = std::make_unique<chip::Controller::DeviceCommissioner>();
VerifyOrReturnError(devCtrl != nullptr, ToPyChipError(CHIP_ERROR_NO_MEMORY));

chip::Crypto::P256Keypair ephemeralKey;
chip::Crypto::P256SerializedKeypair keyPair;
memcpy(keyPair.Bytes(), serializedEphemeralKey, serializedEphemeralKeyLen);
CHIP_ERROR err = ephemeralKey.Deserialize(keyPair);
VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err));

ReturnErrorCodeIf(nocLen > Controller::kMaxCHIPDERCertLength, ToPyChipError(CHIP_ERROR_NO_MEMORY));
ReturnErrorCodeIf(icacLen > Controller::kMaxCHIPDERCertLength, ToPyChipError(CHIP_ERROR_NO_MEMORY));
ReturnErrorCodeIf(rcacLen > Controller::kMaxCHIPDERCertLength, ToPyChipError(CHIP_ERROR_NO_MEMORY));

Controller::SetupParams initParams;
initParams.pairingDelegate = &sPairingDelegate;
initParams.operationalCredentialsDelegate = &sDummyOperationalCredentialsIssuer;
initParams.operationalKeypair = &ephemeralKey;
initParams.controllerRCAC = ByteSpan(rcac, rcacLen);
initParams.controllerICAC = ByteSpan(icac, icacLen);
initParams.controllerNOC = ByteSpan(noc, nocLen);
initParams.enableServerInteractions = enableServerInteractions;
initParams.controllerVendorId = adminVendorId;
initParams.permitMultiControllerFabrics = true;

err = Controller::DeviceControllerFactory::GetInstance().SetupCommissioner(initParams, *devCtrl);
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, ToPyChipError(err));

ChipLogProgress(Support, "Setting up group data for Fabric Index %u with Compressed Fabric ID:",
static_cast<unsigned>(devCtrl->GetFabricIndex()));
ChipLogByteSpan(Support, compressedFabricIdSpan);

chip::ByteSpan defaultIpk = chip::GroupTesting::DefaultIpkValue::GetDefaultIpk();
err =
chip::Credentials::SetSingleIpkEpochKey(&sGroupDataProvider, devCtrl->GetFabricIndex(), defaultIpk, compressedFabricIdSpan);
VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err));

*outDevCtrl = devCtrl.release();

return ToPyChipError(CHIP_NO_ERROR);
}

PyChipError pychip_OpCreds_AllocateController(OpCredsContext * context, chip::Controller::DeviceCommissioner ** outDevCtrl,
FabricId fabricId, chip::NodeId nodeId, chip::VendorId adminVendorId,
const char * paaTrustStorePath, bool useTestCommissioner,
Expand Down
Loading

0 comments on commit a656dc4

Please sign in to comment.