From a33c8977e62ea6fd21f47f5b102a86f11fbd42b9 Mon Sep 17 00:00:00 2001 From: chrisdecenzo Date: Tue, 7 Dec 2021 10:19:27 -0800 Subject: [PATCH] Fix 12692 --- examples/platform/linux/AppMain.cpp | 128 ++++++++++++++++++++++++++-- 1 file changed, 123 insertions(+), 5 deletions(-) diff --git a/examples/platform/linux/AppMain.cpp b/examples/platform/linux/AppMain.cpp index b3583fd6b7c57c..9030459ed7d7b9 100644 --- a/examples/platform/linux/AppMain.cpp +++ b/examples/platform/linux/AppMain.cpp @@ -29,8 +29,8 @@ #include #include +#include #include -#include #include #include @@ -222,8 +222,8 @@ CHIP_ERROR InitCommissioner() ReturnErrorOnFailure(gFabricStorage.Initialize(&gServerStorage)); factoryParams.fabricStorage = &gFabricStorage; - // use a different listen port for the commissioner. - factoryParams.listenPort = LinuxDeviceOptions::GetInstance().securedCommissionerPort; + // use a different listen port for the commissioner than the default used by chip-tool. + factoryParams.listenPort = LinuxDeviceOptions::GetInstance().securedCommissionerPort + 10; params.storageDelegate = &gServerStorage; params.deviceAddressUpdateDelegate = nullptr; params.operationalCredentialsDelegate = &gOpCredsIssuer; @@ -234,7 +234,9 @@ CHIP_ERROR InitCommissioner() ReturnErrorOnFailure(gCommissioner.SetUdcListenPort(LinuxDeviceOptions::GetInstance().unsecuredCommissionerPort)); // Initialize device attestation verifier - SetDeviceAttestationVerifier(Examples::GetExampleDACVerifier()); + // TODO: Replace testingRootStore with a AttestationTrustStore that has the necessary official PAA roots available + const chip::Credentials::AttestationTrustStore * testingRootStore = chip::Credentials::GetTestAttestationTrustStore(); + SetDeviceAttestationVerifier(GetDefaultDACVerifier(testingRootStore)); chip::Platform::ScopedMemoryBuffer noc; VerifyOrReturnError(noc.Alloc(chip::Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY); @@ -272,6 +274,122 @@ CHIP_ERROR ShutdownCommissioner() return CHIP_NO_ERROR; } +class PairingCommand : public chip::Controller::DevicePairingDelegate, public chip::Controller::DeviceAddressUpdateDelegate +{ + /////////// DevicePairingDelegate Interface ///////// + void OnStatusUpdate(chip::Controller::DevicePairingDelegate::Status status) override; + void OnPairingComplete(CHIP_ERROR error) override; + void OnPairingDeleted(CHIP_ERROR error) override; + void OnCommissioningComplete(NodeId deviceId, CHIP_ERROR error) override; + + /////////// DeviceAddressUpdateDelegate Interface ///////// + void OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR error) override; + + CHIP_ERROR UpdateNetworkAddress(); +}; + +PairingCommand gPairingCommand; +NodeId gRemoteId = kTestDeviceNodeId; + +CHIP_ERROR PairingCommand::UpdateNetworkAddress() +{ + ChipLogProgress(AppServer, "Mdns: Updating NodeId: %" PRIx64 " ...", gRemoteId); + return gCommissioner.UpdateDevice(gRemoteId); +} + +void PairingCommand::OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR err) +{ + ChipLogProgress(AppServer, "OnAddressUpdateComplete: %s", ErrorStr(err)); +} + +void PairingCommand::OnStatusUpdate(DevicePairingDelegate::Status status) +{ + switch (status) + { + case DevicePairingDelegate::Status::SecurePairingSuccess: + ChipLogProgress(AppServer, "Secure Pairing Success"); + break; + case DevicePairingDelegate::Status::SecurePairingFailed: + ChipLogError(AppServer, "Secure Pairing Failed"); + break; + } +} + +void PairingCommand::OnPairingComplete(CHIP_ERROR err) +{ + if (err == CHIP_NO_ERROR) + { + ChipLogProgress(AppServer, "Pairing Success"); + } + else + { + ChipLogProgress(AppServer, "Pairing Failure: %s", ErrorStr(err)); + // For some devices, it may take more time to appear on the network and become discoverable + // over DNS-SD, so don't give up on failure and restart the address update. Note that this + // will not be repeated endlessly as each chip-tool command has a timeout (in the case of + // the `pairing` command it equals 120s). + // UpdateNetworkAddress(); + } +} + +void PairingCommand::OnPairingDeleted(CHIP_ERROR err) +{ + if (err == CHIP_NO_ERROR) + { + ChipLogProgress(AppServer, "Pairing Deleted Success"); + } + else + { + ChipLogProgress(AppServer, "Pairing Deleted Failure: %s", ErrorStr(err)); + } +} + +void PairingCommand::OnCommissioningComplete(NodeId nodeId, CHIP_ERROR err) +{ + if (err == CHIP_NO_ERROR) + { + ChipLogProgress(AppServer, "Device commissioning completed with success"); + } + else + { + ChipLogProgress(AppServer, "Device commissioning Failure: %s", ErrorStr(err)); + } +} + +CHIP_ERROR CommissionerPairOnNetwork(uint32_t pincode, uint16_t disc, chip::Transport::PeerAddress address) +{ + RendezvousParameters params = RendezvousParameters().SetSetupPINCode(pincode).SetDiscriminator(disc).SetPeerAddress(address); + + gCommissioner.RegisterDeviceAddressUpdateDelegate(&gPairingCommand); + gCommissioner.RegisterPairingDelegate(&gPairingCommand); + gCommissioner.PairDevice(gRemoteId, params); + + return CHIP_NO_ERROR; +} + +CHIP_ERROR CommissionerPairUDC(uint32_t pincode, size_t index) +{ + UDCClientState * state = gCommissioner.GetUserDirectedCommissioningServer()->GetUDCClients().GetUDCClientState(index); + if (state == nullptr) + { + ChipLogProgress(AppServer, "udc client[%ld] null \r\n", index); + return CHIP_ERROR_KEY_NOT_FOUND; + } + else + { + Transport::PeerAddress peerAddress = state->GetPeerAddress(); + + state->SetUDCClientProcessingState(UDCClientProcessingState::kCommissioningNode); + + return CommissionerPairOnNetwork(pincode, state->GetLongDiscriminator(), peerAddress); + } +} + +DeviceCommissioner * GetDeviceCommissioner() +{ + return &gCommissioner; +} + #endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE void ChipLinuxAppMainLoop() @@ -305,7 +423,7 @@ void ChipLinuxAppMainLoop() #if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE InitCommissioner(); #if defined(ENABLE_CHIP_SHELL) - chip::Shell::RegisterControllerCommands(&gCommissioner); + chip::Shell::RegisterControllerCommands(); #endif // defined(ENABLE_CHIP_SHELL) #endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE