Skip to content

Commit

Permalink
Enable commissioner for tv-app (#9408)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdecenzo authored Sep 4, 2021
1 parent d716f27 commit 92309bc
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 27 deletions.
127 changes: 126 additions & 1 deletion examples/platform/linux/ControllerShellCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>
#include <platform/CHIPDeviceLayer.h>
#include <protocols/secure_channel/RendezvousParameters.h>
#include <protocols/user_directed_commissioning/UserDirectedCommissioning.h>

namespace chip {
Expand Down Expand Up @@ -116,6 +117,106 @@ static CHIP_ERROR display(bool printHeader)

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();
};

NodeId gRemoteId = kTestDeviceNodeId;
uint64_t gFabricId = 0;
PairingCommand gPairingCommand;

CHIP_ERROR PairingCommand::UpdateNetworkAddress()
{
ChipLogProgress(chipTool, "Mdns: Updating NodeId: %" PRIx64 " FabricId: %" PRIx64 " ...", gRemoteId, gFabricId);
return gCommissioner->UpdateDevice(gRemoteId);
}

void PairingCommand::OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR err)
{
ChipLogProgress(chipTool, "OnAddressUpdateComplete: %s", ErrorStr(err));
}

void PairingCommand::OnStatusUpdate(DevicePairingDelegate::Status status)
{
switch (status)
{
case DevicePairingDelegate::Status::SecurePairingSuccess:
ChipLogProgress(chipTool, "Secure Pairing Success");
break;
case DevicePairingDelegate::Status::SecurePairingFailed:
ChipLogError(chipTool, "Secure Pairing Failed");
break;
}
}

void PairingCommand::OnPairingComplete(CHIP_ERROR err)
{
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(chipTool, "Pairing Success");
UpdateNetworkAddress();
}
else
{
ChipLogProgress(chipTool, "Pairing Failure: %s", ErrorStr(err));
}
}

void PairingCommand::OnPairingDeleted(CHIP_ERROR err)
{
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(chipTool, "Pairing Deleted Success");
}
else
{
ChipLogProgress(chipTool, "Pairing Deleted Failure: %s", ErrorStr(err));
}
}

void PairingCommand::OnCommissioningComplete(NodeId nodeId, CHIP_ERROR err)
{
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(chipTool, "Device commissioning completed with success");
}
else
{
ChipLogProgress(chipTool, "Device commissioning Failure: %s", ErrorStr(err));
}
}

static CHIP_ERROR pairOnNetwork(bool printHeader, uint64_t fabric, uint32_t pincode, uint16_t disc,
chip::Transport::PeerAddress address)
{
streamer_t * sout = streamer_get();

if (printHeader)
{
streamer_printf(sout, "onnetwork \r\n");
}

RendezvousParameters params = RendezvousParameters().SetSetupPINCode(pincode).SetDiscriminator(disc).SetPeerAddress(address);

gCommissioner->RegisterDeviceAddressUpdateDelegate(&gPairingCommand);
gCommissioner->RegisterPairingDelegate(&gPairingCommand);
gCommissioner->PairDevice(gRemoteId, params);

streamer_printf(sout, "done\r\n");

return CHIP_NO_ERROR;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY

static CHIP_ERROR PrintAllCommands()
Expand All @@ -125,14 +226,19 @@ static CHIP_ERROR PrintAllCommands()
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
streamer_printf(
sout, " resetudc Clear all pending UDC sessions from this UDC server. Usage: commission resetudc\r\n");
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
streamer_printf(sout, " commissionable Discover all commissionable nodes. Usage: commission discover\r\n");
streamer_printf(
sout,
" commissionable-instance <name> Discover all commissionable node with given instance name. Usage: commission "
"commissionable-instance DC514873944A5CFF\r\n");
streamer_printf(sout,
" display Display all discovered commissionable nodes. Usage: commission display\r\n");
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
streamer_printf(sout,
" onnetwork <fabric> <pincode> <disc> <IP> <port> Pair given device. Usage: commission onnetwork 2222 "
"20202021 3840 127.0.0.1 5540\r\n");
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
streamer_printf(sout, "\r\n");

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -165,6 +271,25 @@ static CHIP_ERROR DiscoverHandler(int argc, char ** argv)
{
return error = display(true);
}
else if (strcmp(argv[0], "onnetwork") == 0)
{
// onnetwork fabric pincode disc IP port
if (argc < 6)
{
return PrintAllCommands();
}
char * eptr;
gFabricId = (uint64_t) strtol(argv[1], &eptr, 10);
uint32_t pincode = (uint32_t) strtol(argv[2], &eptr, 10);
uint16_t disc = (uint16_t) strtol(argv[3], &eptr, 10);

chip::Inet::IPAddress address;
chip::Inet::IPAddress::FromString(argv[4], address);

uint16_t port = (uint16_t) strtol(argv[5], &eptr, 10);

return error = pairOnNetwork(true, gFabricId, pincode, disc, chip::Transport::PeerAddress::UDP(address, port));
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
else
{
Expand Down
26 changes: 0 additions & 26 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1197,12 +1197,10 @@ CHIP_ERROR DeviceCommissioner::SendOperationalCertificateSigningRequestCommand(D
chip::Controller::OperationalCredentialsCluster cluster;
cluster.Associate(device, 0);

#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
Callback::Cancelable * successCallback = mOpCSRResponseCallback.Cancel();
Callback::Cancelable * failureCallback = mOnCSRFailureCallback.Cancel();

ReturnErrorOnFailure(cluster.OpCSRRequest(successCallback, failureCallback, device->GetCSRNonce()));
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE

ChipLogDetail(Controller, "Sent OpCSR request, waiting for the CSR");
return CHIP_NO_ERROR;
Expand Down Expand Up @@ -1305,13 +1303,11 @@ CHIP_ERROR DeviceCommissioner::SendOperationalCertificate(Device * device, const
chip::Controller::OperationalCredentialsCluster cluster;
cluster.Associate(device, 0);

#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
Callback::Cancelable * successCallback = mNOCResponseCallback.Cancel();
Callback::Cancelable * failureCallback = mOnCertFailureCallback.Cancel();

ReturnErrorOnFailure(
cluster.AddNOC(successCallback, failureCallback, opCertBuf, ByteSpan(nullptr, 0), mLocalId.GetNodeId(), mVendorId));
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE

ChipLogProgress(Controller, "Sent operational certificate to the device");

Expand Down Expand Up @@ -1395,12 +1391,10 @@ CHIP_ERROR DeviceCommissioner::SendTrustedRootCertificate(Device * device, const
chip::Controller::OperationalCredentialsCluster cluster;
cluster.Associate(device, 0);

#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
Callback::Cancelable * successCallback = mRootCertResponseCallback.Cancel();
Callback::Cancelable * failureCallback = mOnRootCertFailureCallback.Cancel();

ReturnErrorOnFailure(cluster.AddTrustedRootCertificate(successCallback, failureCallback, rcac));
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE

ChipLogProgress(Controller, "Sent root certificate to the device");

Expand Down Expand Up @@ -1632,18 +1626,14 @@ void DeviceControllerInteractionModelDelegate::OnReportData(const app::ReadClien
TLV::TLVReader * apData,
Protocols::InteractionModel::ProtocolCode status)
{
#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
IMReadReportAttributesResponseCallback(apReadClient, aPath, apData, status);
#endif // !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
}

CHIP_ERROR DeviceControllerInteractionModelDelegate::ReadError(const app::ReadClient * apReadClient, CHIP_ERROR aError)
{
app::ClusterInfo path;
path.mNodeId = apReadClient->GetExchangeContext()->GetSecureSession().GetPeerNodeId();
#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
IMReadReportAttributesResponseCallback(apReadClient, path, nullptr, Protocols::InteractionModel::ProtocolCode::Failure);
#endif // !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
return CHIP_NO_ERROR;
}

Expand All @@ -1652,28 +1642,22 @@ CHIP_ERROR DeviceControllerInteractionModelDelegate::WriteResponseStatus(
const uint32_t aProtocolId, const uint16_t aProtocolCode, app::AttributePathParams & aAttributePathParams,
uint8_t aCommandIndex)
{
#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
IMWriteResponseCallback(apWriteClient, chip::app::ToEmberAfStatus(Protocols::InteractionModel::ProtocolCode(aProtocolCode)));
#endif
return CHIP_NO_ERROR;
}

CHIP_ERROR DeviceControllerInteractionModelDelegate::WriteResponseProtocolError(const app::WriteClient * apWriteClient,
uint8_t aAttributeIndex)
{
#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
// When WriteResponseProtocolError occurred, it means server returned an invalid packet.
IMWriteResponseCallback(apWriteClient, EMBER_ZCL_STATUS_FAILURE);
#endif
return CHIP_NO_ERROR;
}

CHIP_ERROR DeviceControllerInteractionModelDelegate::WriteResponseError(const app::WriteClient * apWriteClient, CHIP_ERROR aError)
{
#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
// When WriteResponseError occurred, it means we failed to receive the response from server.
IMWriteResponseCallback(apWriteClient, EMBER_ZCL_STATUS_FAILURE);
#endif
return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -1802,13 +1786,11 @@ void DeviceCommissioner::AdvanceCommissioningStage(CHIP_ERROR err)

device = &mActiveDevices[mDeviceBeingPaired];

#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
// TODO(cecille): We probably want something better than this for breadcrumbs.
uint64_t breadcrumb = static_cast<uint64_t>(nextStage);

// TODO(cecille): This should be customized per command.
constexpr uint32_t kCommandTimeoutMs = 3000;
#endif // !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE

switch (nextStage)
{
Expand All @@ -1820,10 +1802,8 @@ void DeviceCommissioner::AdvanceCommissioningStage(CHIP_ERROR err)
GeneralCommissioningCluster genCom;
// TODO: should get the endpoint information from the descriptor cluster.
genCom.Associate(device, 0);
#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
uint16_t commissioningExpirySeconds = 5;
genCom.ArmFailSafe(mSuccess.Cancel(), mFailure.Cancel(), commissioningExpirySeconds, breadcrumb, kCommandTimeoutMs);
#endif // !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
}
break;
case CommissioningStage::kConfigRegulatory: {
Expand Down Expand Up @@ -1864,10 +1844,8 @@ void DeviceCommissioner::AdvanceCommissioningStage(CHIP_ERROR err)

GeneralCommissioningCluster genCom;
genCom.Associate(device, 0);
#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
genCom.SetRegulatoryConfig(mSuccess.Cancel(), mFailure.Cancel(), static_cast<uint8_t>(regulatoryLocation), countryCode,
breadcrumb, kCommandTimeoutMs);
#endif // !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
}
break;
case CommissioningStage::kCheckCertificates: {
Expand Down Expand Up @@ -1901,12 +1879,10 @@ void DeviceCommissioner::AdvanceCommissioningStage(CHIP_ERROR err)
// TODO: Once network credential sending is implemented, attempting to set wifi credential on an ethernet only device
// will cause an error to be sent back. At that point, we should scan and we shoud see the proper ethernet network ID
// returned in the scan results. For now, we use magic.
#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
char magicNetworkEnableCode[] = "ETH0";
netCom.EnableNetwork(mSuccess.Cancel(), mFailure.Cancel(),
ByteSpan(reinterpret_cast<uint8_t *>(&magicNetworkEnableCode), sizeof(magicNetworkEnableCode)),
breadcrumb, kCommandTimeoutMs);
#endif // !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
}
break;
case CommissioningStage::kFindOperational: {
Expand All @@ -1923,9 +1899,7 @@ void DeviceCommissioner::AdvanceCommissioningStage(CHIP_ERROR err)
ChipLogProgress(Controller, "Calling commissioning complete");
GeneralCommissioningCluster genCom;
genCom.Associate(device, 0);
#if !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE // temporary - until example app clusters are updated (Issue 8347)
genCom.CommissioningComplete(mSuccess.Cancel(), mFailure.Cancel());
#endif // !CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
}
break;
case CommissioningStage::kCleanup:
Expand Down

0 comments on commit 92309bc

Please sign in to comment.