Skip to content

Commit

Permalink
Convert mSuccess and mFailure
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Jan 28, 2022
1 parent 7d8b464 commit 7c1c6f7
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 47 deletions.
119 changes: 81 additions & 38 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,7 @@ ControllerDeviceInitParams DeviceController::GetControllerDeviceInitParams()
}

DeviceCommissioner::DeviceCommissioner() :
mSuccess(BasicSuccess, this), mFailure(BasicFailure, this), mOnDeviceConnectedCallback(OnDeviceConnectedFn, this),
mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this),
mOnDeviceConnectedCallback(OnDeviceConnectedFn, this), mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this),
mDeviceAttestationInformationVerificationCallback(OnDeviceAttestationInformationVerification, this),
mDeviceNOCChainCallback(OnDeviceNOCChainGeneration, this), mSetUpCodePairer(this), mAutoCommissioner(this)
{
Expand Down Expand Up @@ -1457,18 +1456,11 @@ void DeviceCommissioner::OnNodeDiscoveryComplete(const chip::Dnssd::DiscoveredNo
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_DNSSD

void BasicSuccess(void * context, uint16_t val)
void OnBasicFailure(void * context, CHIP_ERROR error)
{
ChipLogProgress(Controller, "Received success response 0x%x\n", val);
ChipLogProgress(Controller, "Received failure response %s\n", chip::ErrorStr(error));
DeviceCommissioner * commissioner = static_cast<DeviceCommissioner *>(context);
commissioner->CommissioningStageComplete(CHIP_NO_ERROR);
}

void BasicFailure(void * context, uint8_t status)
{
ChipLogProgress(Controller, "Received failure response %d\n", (int) status);
DeviceCommissioner * commissioner = static_cast<DeviceCommissioner *>(context);
commissioner->CommissioningStageComplete(static_cast<CHIP_ERROR>(status));
commissioner->CommissioningStageComplete(error);
}

void DeviceCommissioner::CommissioningStageComplete(CHIP_ERROR err, CommissioningDelegate::CommissioningReport report)
Expand Down Expand Up @@ -1592,6 +1584,51 @@ void AttributeReadFailure(void * context, CHIP_ERROR status)
commissioner->CommissioningStageComplete(status);
}

void DeviceCommissioner::OnArmFailSafe(void * context,
const GeneralCommissioning::Commands::ArmFailSafeResponse::DecodableType & data)
{
// TODO: Use errorCode
ChipLogProgress(Controller, "Received ArmFailSafe response");
DeviceCommissioner * commissioner = static_cast<DeviceCommissioner *>(context);
commissioner->CommissioningStageComplete(CHIP_NO_ERROR);
}

void DeviceCommissioner::OnSetRegulatoryConfigResponse(
void * context, const GeneralCommissioning::Commands::SetRegulatoryConfigResponse::DecodableType & data)
{
// TODO: Use errorCode
ChipLogProgress(Controller, "Received SetRegulatoryConfig response");
DeviceCommissioner * commissioner = static_cast<DeviceCommissioner *>(context);
commissioner->CommissioningStageComplete(CHIP_NO_ERROR);
}

void DeviceCommissioner::OnNetworkConfigResponse(void * context,
const NetworkCommissioning::Commands::NetworkConfigResponse::DecodableType & data)
{
// TODO: Use networkingStatus
ChipLogProgress(Controller, "Received NetworkConfig response");
DeviceCommissioner * commissioner = static_cast<DeviceCommissioner *>(context);
commissioner->CommissioningStageComplete(CHIP_NO_ERROR);
}

void DeviceCommissioner::OnConnectNetworkResponse(
void * context, const NetworkCommissioning::Commands::ConnectNetworkResponse::DecodableType & data)
{
// TODO: Use networkingStatus
ChipLogProgress(Controller, "Received ConnectNetwork response");
DeviceCommissioner * commissioner = static_cast<DeviceCommissioner *>(context);
commissioner->CommissioningStageComplete(CHIP_NO_ERROR);
}

void DeviceCommissioner::OnCommissioningCompleteResponse(
void * context, const GeneralCommissioning::Commands::CommissioningCompleteResponse::DecodableType & data)
{
// TODO: Use errorCode
ChipLogProgress(Controller, "Received CommissioningComplete response");
DeviceCommissioner * commissioner = static_cast<DeviceCommissioner *>(context);
commissioner->CommissioningStageComplete(CHIP_NO_ERROR);
}

void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, CommissioningStage step, CommissioningParameters & params,
CommissioningDelegate * delegate, EndpointId endpoint,
Optional<System::Clock::Timeout> timeout)
Expand All @@ -1612,11 +1649,12 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio
{
case CommissioningStage::kArmFailsafe: {
ChipLogProgress(Controller, "Arming failsafe");
// TODO(cecille): Find a way to enumerate the clusters here.
GeneralCommissioningCluster genCom;
// TODO: should get the endpoint information from the descriptor cluster.
SetupCluster(genCom, proxy, endpoint, timeout);
genCom.ArmFailSafe(mSuccess.Cancel(), mFailure.Cancel(), params.GetFailsafeTimerSeconds(), breadcrumb, kCommandTimeoutMs);
GeneralCommissioning::Commands::ArmFailSafe::Type request;
request.expiryLengthSeconds = params.GetFailsafeTimerSeconds();
request.breadcrumb = breadcrumb;
request.timeoutMs = kCommandTimeoutMs;
SendCommand<GeneralCommissioningCluster>(proxy, request, OnArmFailSafe, OnBasicFailure, timeout);
}
break;
case CommissioningStage::kGetNetworkTechnology: {
Expand Down Expand Up @@ -1664,10 +1702,12 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio
}
chip::CharSpan countryCode(countryCodeStr, actualCountryCodeSize);

GeneralCommissioningCluster genCom;
SetupCluster(genCom, proxy, endpoint, timeout);
genCom.SetRegulatoryConfig(mSuccess.Cancel(), mFailure.Cancel(), regulatoryLocation, countryCode, breadcrumb,
kCommandTimeoutMs);
GeneralCommissioning::Commands::SetRegulatoryConfig::Type request;
request.location = static_cast<GeneralCommissioning::RegulatoryLocationType>(regulatoryLocation);
request.countryCode = countryCode;
request.breadcrumb = breadcrumb;
request.timeoutMs = kCommandTimeoutMs;
SendCommand<GeneralCommissioningCluster>(proxy, request, OnSetRegulatoryConfigResponse, OnBasicFailure, timeout);
}
break;
case CommissioningStage::kSendPAICertificateRequest:
Expand Down Expand Up @@ -1786,11 +1826,13 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio
CommissioningStageComplete(CHIP_ERROR_INVALID_ARGUMENT);
return;
}

ChipLogProgress(Controller, "Adding wifi network");
NetworkCommissioningCluster netCom;
SetupCluster(netCom, proxy, endpoint, timeout);
netCom.AddOrUpdateWiFiNetwork(mSuccess.Cancel(), mFailure.Cancel(), params.GetWiFiCredentials().Value().ssid,
params.GetWiFiCredentials().Value().credentials, breadcrumb);
NetworkCommissioning::Commands::AddOrUpdateWiFiNetwork::Type request;
request.ssid = params.GetWiFiCredentials().Value().ssid;
request.credentials = params.GetWiFiCredentials().Value().credentials;
request.breadcrumb = breadcrumb;
SendCommand<NetworkCommissioningCluster>(proxy, request, OnNetworkConfigResponse, OnBasicFailure);
}
break;
case CommissioningStage::kThreadNetworkSetup: {
Expand All @@ -1801,10 +1843,10 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio
return;
}
ChipLogProgress(Controller, "Adding thread network");
NetworkCommissioningCluster netCom;
SetupCluster(netCom, proxy, endpoint, timeout);
netCom.AddOrUpdateThreadNetwork(mSuccess.Cancel(), mFailure.Cancel(), params.GetThreadOperationalDataset().Value(),
breadcrumb);
NetworkCommissioning::Commands::AddOrUpdateThreadNetwork::Type request;
request.operationalDataset = params.GetThreadOperationalDataset().Value();
request.breadcrumb = breadcrumb;
SendCommand<NetworkCommissioningCluster>(proxy, request, OnNetworkConfigResponse, OnBasicFailure);
}
break;
case CommissioningStage::kWiFiNetworkEnable: {
Expand All @@ -1815,9 +1857,10 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio
return;
}
ChipLogProgress(Controller, "Enabling wifi network");
NetworkCommissioningCluster netCom;
SetupCluster(netCom, proxy, endpoint, timeout);
netCom.ConnectNetwork(mSuccess.Cancel(), mFailure.Cancel(), params.GetWiFiCredentials().Value().ssid, breadcrumb);
NetworkCommissioning::Commands::ConnectNetwork::Type request;
request.networkID = params.GetWiFiCredentials().Value().ssid;
request.breadcrumb = breadcrumb;
SendCommand<NetworkCommissioningCluster>(proxy, request, OnConnectNetworkResponse, OnBasicFailure);
}
break;
case CommissioningStage::kThreadNetworkEnable: {
Expand All @@ -1832,9 +1875,10 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio
return;
}
ChipLogProgress(Controller, "Enabling thread network");
NetworkCommissioningCluster netCom;
SetupCluster(netCom, proxy, endpoint, timeout);
netCom.ConnectNetwork(mSuccess.Cancel(), mFailure.Cancel(), extendedPanId, breadcrumb);
NetworkCommissioning::Commands::ConnectNetwork::Type request;
request.networkID = extendedPanId;
request.breadcrumb = breadcrumb;
SendCommand<NetworkCommissioningCluster>(proxy, request, OnConnectNetworkResponse, OnBasicFailure);
}
break;
case CommissioningStage::kFindOperational: {
Expand All @@ -1849,9 +1893,8 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio
break;
case CommissioningStage::kSendComplete: {
ChipLogProgress(Controller, "Calling commissioning complete");
GeneralCommissioningCluster genCom;
SetupCluster(genCom, proxy, endpoint, timeout);
genCom.CommissioningComplete(mSuccess.Cancel(), mFailure.Cancel());
GeneralCommissioning::Commands::CommissioningComplete::Type request;
SendCommand<NetworkCommissioningCluster>(proxy, request, OnCommissioningCompleteResponse, OnBasicFailure, timeout);
}
break;
case CommissioningStage::kCleanup:
Expand All @@ -1869,7 +1912,7 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio
case CommissioningStage::kSecurePairing:
break;
}
}
} // namespace Controller

} // namespace Controller
} // namespace chip
42 changes: 33 additions & 9 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include <lib/core/CHIPTLV.h>
#include <lib/support/DLLUtil.h>
#include <lib/support/Pool.h>
#include <lib/support/SafeInt.h>
#include <lib/support/SerializableIntegerSet.h>
#include <lib/support/Span.h>
#include <lib/support/ThreadOperationalDataset.h>
Expand Down Expand Up @@ -88,10 +89,7 @@ constexpr uint16_t kNumMaxActiveDevices = CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_DEVI
constexpr uint16_t kNumMaxPairedDevices = 128;

// Raw functions for cluster callbacks
typedef void (*BasicSuccessCallback)(void * context, uint16_t val);
typedef void (*BasicFailureCallback)(void * context, uint8_t status);
void BasicSuccess(void * context, uint16_t val);
void BasicFailure(void * context, uint8_t status);
void OnBasicFailure(void * context, CHIP_ERROR err);

struct ControllerInitParams
{
Expand Down Expand Up @@ -790,6 +788,19 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,

static void OnDeviceNOCChainGeneration(void * context, CHIP_ERROR status, const ByteSpan & noc, const ByteSpan & icac,
const ByteSpan & rcac, Optional<AesCcm128KeySpan> ipk, Optional<NodeId> adminSubject);
static void OnArmFailSafe(void * context,
const chip::app::Clusters::GeneralCommissioning::Commands::ArmFailSafeResponse::DecodableType & data);
static void OnSetRegulatoryConfigResponse(
void * context,
const chip::app::Clusters::GeneralCommissioning::Commands::SetRegulatoryConfigResponse::DecodableType & data);
static void
OnNetworkConfigResponse(void * context,
const chip::app::Clusters::NetworkCommissioning::Commands::NetworkConfigResponse::DecodableType & data);
static void OnConnectNetworkResponse(
void * context, const chip::app::Clusters::NetworkCommissioning::Commands::ConnectNetworkResponse::DecodableType & data);
static void OnCommissioningCompleteResponse(
void * context,
const chip::app::Clusters::GeneralCommissioning::Commands::CommissioningCompleteResponse::DecodableType & data);

/**
* @brief
Expand Down Expand Up @@ -825,13 +836,26 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
ClusterObjectT cluster;
cluster.Associate(device, 0);

ReturnErrorOnFailure(cluster.InvokeCommand(request, this, successCb, failureCb));
return CHIP_NO_ERROR;
return cluster.InvokeCommand(request, this, successCb, failureCb);
}

// Cluster callbacks for advancing commissioning flows
Callback::Callback<BasicSuccessCallback> mSuccess;
Callback::Callback<BasicFailureCallback> mFailure;
template <typename ClusterObjectT, typename RequestObjectT>
CHIP_ERROR SendCommand(DeviceProxy * device, RequestObjectT request,
CommandResponseSuccessCallback<typename RequestObjectT::ResponseType> successCb,
CommandResponseFailureCallback failureCb, chip::Optional<chip::System::Clock::Timeout> timeout)
{
ClusterObjectT cluster;
cluster.Associate(device, 0);

if (timeout.HasValue())
{
VerifyOrReturnError(chip::CanCastTo<uint16_t>(timeout.Value().count()), CHIP_ERROR_INVALID_ARGUMENT);
chip::Optional<uint16_t> timedInvokeRequestTimeoutInMs(static_cast<uint16_t>(timeout.Value().count()));
return cluster.InvokeCommand(request, this, successCb, failureCb, timedInvokeRequestTimeoutInMs);
}

return cluster.InvokeCommand(request, this, successCb, failureCb);
}

static CHIP_ERROR ConvertFromNodeOperationalCertStatus(uint8_t err);

Expand Down

0 comments on commit 7c1c6f7

Please sign in to comment.