Skip to content

Commit

Permalink
Add android hooks and callbacks for network scan
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdecenzo committed Jul 18, 2022
1 parent 63f0db7 commit 1540921
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/controller/AutoCommissioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ CommissioningStage AutoCommissioner::GetNextCommissioningStageInternal(Commissio
ChipLogProgress(Controller, "Not a BLE connection, skipping ScanNetworks");
}
// fall through if no network scan is called for
FALLTHROUGH;
case CommissioningStage::kScanNetworks:
return CommissioningStage::kConfigRegulatory;
case CommissioningStage::kConfigRegulatory:
Expand Down
59 changes: 56 additions & 3 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,41 @@ void DeviceCommissioner::SendCommissioningCompleteCallbacks(NodeId nodeId, const
}
}

void DeviceCommissioner::PauseCommissioning()
{
VerifyOrReturn(mDeviceBeingCommissioned != nullptr);
mCommissioningPaused = true;
}

void DeviceCommissioner::ResumeCommissioning()
{
VerifyOrReturn(mCommissioningPaused);
VerifyOrReturn(mDeviceBeingCommissioned != nullptr);

NodeId nodeId = mDeviceBeingCommissioned->GetDeviceId();
DeviceProxy * proxy = mDeviceBeingCommissioned;
mDeviceBeingCommissioned = nullptr;
CommissioningDelegate::CommissioningReport report;

if (mCommissioningDelegate == nullptr)
{
return;
}
report.stageCompleted = mCommissioningStage;
CHIP_ERROR status = mCommissioningDelegate->CommissioningStepFinished(mCommissioningPausedErr, report);
if (status != CHIP_NO_ERROR)
{
// Commissioning delegate will only return error if it failed to perform the appropriate commissioning step.
// In this case, we should complete the commissioning for it.
CompletionStatus completionStatus;
completionStatus.err = status;
completionStatus.failedStage = MakeOptional(report.stageCompleted);
mCommissioningStage = CommissioningStage::kCleanup;
mDeviceBeingCommissioned = proxy;
CleanupCommissioning(proxy, nodeId, completionStatus);
}
}

void DeviceCommissioner::CommissioningStageComplete(CHIP_ERROR err, CommissioningDelegate::CommissioningReport report)
{
// Once this stage is complete, reset mDeviceBeingCommissioned - this will be reset when the delegate calls the next step.
Expand All @@ -1569,6 +1604,13 @@ void DeviceCommissioner::CommissioningStageComplete(CHIP_ERROR err, Commissionin
{
mPairingDelegate->OnCommissioningStatusUpdate(PeerId(GetCompressedFabricId(), nodeId), mCommissioningStage, err);
}

if (mCommissioningPaused)
{
mDeviceBeingCommissioned = proxy;
mCommissioningPausedErr = err;
return;
}
if (mCommissioningDelegate == nullptr)
{
return;
Expand Down Expand Up @@ -1828,11 +1870,16 @@ void DeviceCommissioner::OnSetRegulatoryConfigResponse(
commissioner->CommissioningStageComplete(err, report);
}

void OnScanNetworksFailure(void * context, CHIP_ERROR error)
void DeviceCommissioner::OnScanNetworksFailure(void * context, CHIP_ERROR error)
{
ChipLogProgress(Controller, "Received ScanNetworks failure response %s\n", chip::ErrorStr(error));
// need to advance to next step

DeviceCommissioner * commissioner = static_cast<DeviceCommissioner *>(context);
if (commissioner->GetPairingDelegate() != nullptr)
{
commissioner->GetPairingDelegate()->OnScanNetworksFailure(error);
}
// need to advance to next step
// clear error so that we don't abort the commissioning when ScanNetworks fails
commissioner->CommissioningStageComplete(CHIP_NO_ERROR);
}
Expand Down Expand Up @@ -1880,8 +1927,14 @@ void DeviceCommissioner::OnScanNetworksResponse(void * context,
ChipLogProgress(Controller, "ScanNetwork response, no Thread results");
}
}

DeviceCommissioner * commissioner = static_cast<DeviceCommissioner *>(context);
// clear error so that we don't abort the commissioning when ScanNetworks fails

if (commissioner->GetPairingDelegate() != nullptr)
{
commissioner->GetPairingDelegate()->OnScanNetworksSuccess(data);
}
// need to advance to next step
commissioner->CommissioningStageComplete(CHIP_NO_ERROR);
}

Expand Down
16 changes: 15 additions & 1 deletion src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ constexpr uint16_t kNumMaxActiveDevices = CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_DEVI

// Raw functions for cluster callbacks
void OnBasicFailure(void * context, CHIP_ERROR err);
void OnScanNetworksFailure(void * context, CHIP_ERROR err);

struct ControllerInitParams
{
Expand Down Expand Up @@ -561,6 +560,18 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
*/
CHIP_ERROR ValidateAttestationInfo(const Credentials::DeviceAttestationVerifier::AttestationInfo & info);

/**
* @brief
* This function puts the commissioner in a paused state to prevent advancing to the next stage.
* It is expected that a DevicePairingDelegate may call this method when processing the
* OnCommissioningStatusUpdate, for example, in order to obtain network credentials from the user based
* upon the results of the NetworkScan.
* Use ResumeCommissioning to continue the commissioning process.
*
*/
void PauseCommissioning();
void ResumeCommissioning();

/**
* @brief
* Sends CommissioningStepComplete report to the commissioning delegate. Function will fill in current step.
Expand Down Expand Up @@ -767,6 +778,7 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
static void
OnScanNetworksResponse(void * context,
const chip::app::Clusters::NetworkCommissioning::Commands::ScanNetworksResponse::DecodableType & data);
static void OnScanNetworksFailure(void * context, CHIP_ERROR err);
static void
OnNetworkConfigResponse(void * context,
const chip::app::Clusters::NetworkCommissioning::Commands::NetworkConfigResponse::DecodableType & data);
Expand Down Expand Up @@ -875,6 +887,8 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
Platform::UniquePtr<app::ReadClient> mReadClient;
Credentials::AttestationVerificationResult mAttestationResult;
Credentials::DeviceAttestationVerifier * mDeviceAttestationVerifier = nullptr;
bool mCommissioningPaused = false;
CHIP_ERROR mCommissioningPausedErr = CHIP_NO_ERROR;
};

} // namespace Controller
Expand Down
14 changes: 14 additions & 0 deletions src/controller/DevicePairingDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ class DLL_EXPORT DevicePairingDelegate
{}

virtual void OnCommissioningStatusUpdate(PeerId peerId, CommissioningStage stageCompleted, CHIP_ERROR error) {}

/**
* @brief
* Called with the NetworkScanResponse returned from the target
*/
virtual void OnScanNetworksSuccess(
const chip::app::Clusters::NetworkCommissioning::Commands::ScanNetworksResponse::DecodableType & dataResponse)
{}

/**
* @brief
* Called when the NetworkScan request fails.
*/
virtual void OnScanNetworksFailure(CHIP_ERROR error) {}
};

} // namespace Controller
Expand Down
Loading

0 comments on commit 1540921

Please sign in to comment.