Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nwprov] Implement breadcrumb support #18303

Merged
merged 4 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions src/app/clusters/network-commissioning/network-commissioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "network-commissioning.h"

#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app/CommandHandlerInterface.h>
#include <app/InteractionModelEngine.h>
Expand Down Expand Up @@ -267,12 +268,14 @@ void Instance::HandleScanNetworks(HandlerContext & ctx, const Commands::ScanNetw
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Protocols::InteractionModel::Status::InvalidCommand);
return;
}
mAsyncCommandHandle = CommandHandler::Handle(&ctx.mCommandHandler);
mLastOperationBreadcrumb = req.breadcrumb;
mAsyncCommandHandle = CommandHandler::Handle(&ctx.mCommandHandler);
mpDriver.Get<WiFiDriver *>()->ScanNetworks(ssid, this);
}
else if (mFeatureFlags.Has(NetworkCommissioningFeature::kThreadNetworkInterface))
{
mAsyncCommandHandle = CommandHandler::Handle(&ctx.mCommandHandler);
mLastOperationBreadcrumb = req.breadcrumb;
mAsyncCommandHandle = CommandHandler::Handle(&ctx.mCommandHandler);
mpDriver.Get<ThreadDriver *>()->ScanNetworks(this);
}
else
Expand Down Expand Up @@ -362,6 +365,10 @@ void Instance::HandleAddOrUpdateWiFiNetwork(HandlerContext & ctx, const Commands
mpDriver.Get<WiFiDriver *>()->AddOrUpdateNetwork(req.ssid, req.credentials, debugText, outNetworkIndex);
FillDebugTextAndNetworkIndex(response, debugText, outNetworkIndex);
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
if (response.networkingStatus == NetworkCommissioningStatus::kSuccess)
{
KickBreadcrumbChange(req.breadcrumb);
}
}

void Instance::HandleAddOrUpdateThreadNetwork(HandlerContext & ctx, const Commands::AddOrUpdateThreadNetwork::DecodableType & req)
Expand All @@ -381,6 +388,22 @@ void Instance::HandleAddOrUpdateThreadNetwork(HandlerContext & ctx, const Comman
mpDriver.Get<ThreadDriver *>()->AddOrUpdateNetwork(req.operationalDataset, debugText, outNetworkIndex);
FillDebugTextAndNetworkIndex(response, debugText, outNetworkIndex);
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
if (response.networkingStatus == NetworkCommissioningStatus::kSuccess)
{
KickBreadcrumbChange(req.breadcrumb);
}
}

void Instance::KickBreadcrumbChange(const Optional<uint64_t> & breadcrumb)
erjiaqing marked this conversation as resolved.
Show resolved Hide resolved
{
VerifyOrReturn(breadcrumb.HasValue());
GeneralCommissioning::Attributes::Breadcrumb::Set(0, breadcrumb.Value());
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
}

void Instance::KickBreadcrumbChange()
{
KickBreadcrumbChange(mLastOperationBreadcrumb);
mLastOperationBreadcrumb.ClearValue();
}

void Instance::HandleRemoveNetwork(HandlerContext & ctx, const Commands::RemoveNetwork::DecodableType & req)
Expand All @@ -399,6 +422,10 @@ void Instance::HandleRemoveNetwork(HandlerContext & ctx, const Commands::RemoveN
response.networkingStatus = mpWirelessDriver->RemoveNetwork(req.networkID, debugText, outNetworkIndex);
FillDebugTextAndNetworkIndex(response, debugText, outNetworkIndex);
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
if (response.networkingStatus == NetworkCommissioningStatus::kSuccess)
{
KickBreadcrumbChange(req.breadcrumb);
}
}

void Instance::HandleConnectNetwork(HandlerContext & ctx, const Commands::ConnectNetwork::DecodableType & req)
Expand All @@ -414,8 +441,8 @@ void Instance::HandleConnectNetwork(HandlerContext & ctx, const Commands::Connec

mConnectingNetworkIDLen = static_cast<uint8_t>(req.networkID.size());
memcpy(mConnectingNetworkID, req.networkID.data(), mConnectingNetworkIDLen);

mAsyncCommandHandle = CommandHandler::Handle(&ctx.mCommandHandler);
mAsyncCommandHandle = CommandHandler::Handle(&ctx.mCommandHandler);
mLastOperationBreadcrumb = req.breadcrumb;
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
mpWirelessDriver->ConnectNetwork(req.networkID, this);
}

Expand All @@ -431,6 +458,10 @@ void Instance::HandleReorderNetwork(HandlerContext & ctx, const Commands::Reorde
response.networkingStatus = mpWirelessDriver->ReorderNetwork(req.networkID, req.networkIndex, debugText);
FillDebugTextAndNetworkIndex(response, debugText, req.networkIndex);
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
if (response.networkingStatus == NetworkCommissioningStatus::kSuccess)
{
KickBreadcrumbChange(req.breadcrumb);
}
}

void Instance::OnResult(Status commissioningError, CharSpan debugText, int32_t interfaceStatus)
Expand Down Expand Up @@ -467,6 +498,10 @@ void Instance::OnResult(Status commissioningError, CharSpan debugText, int32_t i
mLastNetworkingStatusValue.SetNonNull(commissioningError);

commandHandle->AddResponse(mPath, response);
if (commissioningError == NetworkCommissioningStatus::kSuccess)
{
KickBreadcrumbChange();
}
}

void Instance::OnFinished(Status status, CharSpan debugText, ThreadScanResponseIterator * networks)
Expand Down Expand Up @@ -529,6 +564,10 @@ void Instance::OnFinished(Status status, CharSpan debugText, ThreadScanResponseI
{
ChipLogError(Zcl, "Failed to encode response: %s", err.AsString());
}
if (status == NetworkCommissioningStatus::kSuccess)
{
KickBreadcrumbChange();
}
networks->Release();
}

Expand Down Expand Up @@ -588,6 +627,10 @@ void Instance::OnFinished(Status status, CharSpan debugText, WiFiScanResponseIte
{
ChipLogError(Zcl, "Failed to encode response: %s", err.AsString());
}
if (status == NetworkCommissioningStatus::kSuccess)
{
KickBreadcrumbChange();
}
if (networks != nullptr)
{
networks->Release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ class Instance : public CommandHandlerInterface,
uint8_t mLastNetworkID[DeviceLayer::NetworkCommissioning::kMaxNetworkIDLen];
uint8_t mLastNetworkIDLen = 0;

Optional<uint64_t> mLastOperationBreadcrumb;
void KickBreadcrumbChange(const Optional<uint64_t> & breadcrumbValue);
void KickBreadcrumbChange();

// Actual handlers of the commands
void HandleScanNetworks(HandlerContext & ctx, const Commands::ScanNetworks::DecodableType & req);
void HandleAddOrUpdateWiFiNetwork(HandlerContext & ctx, const Commands::AddOrUpdateWiFiNetwork::DecodableType & req);
Expand Down
37 changes: 29 additions & 8 deletions src/controller/python/test/test_scripts/network_commissioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from chip.clusters.Types import NullValue
import chip.interaction_model
import asyncio
import random

import base

Expand Down Expand Up @@ -55,6 +56,18 @@ class NetworkCommissioningTests:
def __init__(self, devCtrl, nodeid):
self._devCtrl = devCtrl
self._nodeid = nodeid
self._last_breadcrumb = random.randint(1, 1 << 48)

async def must_verify_breadcrumb(self):
res = await self._devCtrl.ReadAttribute(nodeid=self._nodeid, attributes=[(0, Clusters.GeneralCommissioning.Attributes.Breadcrumb)], returnClusterObject=True)
if self._last_breadcrumb is not None:
if self._last_breadcrumb != res[0][Clusters.GeneralCommissioning].breadcrumb:
raise AssertionError(
f"Breadcrumb attribute mismatch! Expect {self._last_breadcrumb} got {res[0][Clusters.GeneralCommissioning].breadcrumb}")

def with_breadcrumb(self) -> int:
self._last_breadcrumb += 1
return self._last_breadcrumb

def log_interface_basic_info(self, values):
logger.info(f"The interface supports {values.maxNetworks} networks.")
Expand Down Expand Up @@ -129,11 +142,12 @@ async def test_wifi(self, endpointId):
# Scan networks
logger.info(f"Scan networks")
req = Clusters.NetworkCommissioning.Commands.ScanNetworks(
ssid=b'', breadcrumb=0)
ssid=b'', breadcrumb=self.with_breadcrumb())
res = await self._devCtrl.SendCommand(nodeid=self._nodeid, endpoint=endpointId, payload=req)
logger.info(f"Received response: {res}")
if res.networkingStatus != Clusters.NetworkCommissioning.Enums.NetworkCommissioningStatus.kSuccess:
raise AssertionError(f"Unexpected result: {res.networkingStatus}")
await self.must_verify_breadcrumb()

# Arm the failsafe before making network config changes
logger.info(f"Arming the failsafe")
Expand All @@ -149,24 +163,26 @@ async def test_wifi(self, endpointId):
if len(networkList) != 0:
logger.info(f"Removing existing network")
req = Clusters.NetworkCommissioning.Commands.RemoveNetwork(
networkID=networkList[0].networkID, breadcrumb=0)
networkID=networkList[0].networkID, breadcrumb=self.with_breadcrumb())
res = await self._devCtrl.SendCommand(nodeid=self._nodeid, endpoint=endpointId, payload=req)
logger.info(f"Received response: {res}")
if res.networkingStatus != Clusters.NetworkCommissioning.Enums.NetworkCommissioningStatus.kSuccess:
raise AssertionError(
f"Unexpected result: {res.networkingStatus}")
await self.must_verify_breadcrumb()

# Add first network
logger.info(f"Adding first test network")
req = Clusters.NetworkCommissioning.Commands.AddOrUpdateWiFiNetwork(
ssid=TEST_WIFI_SSID.encode(), credentials=TEST_WIFI_PASS.encode(), breadcrumb=0)
ssid=TEST_WIFI_SSID.encode(), credentials=TEST_WIFI_PASS.encode(), breadcrumb=self.with_breadcrumb())
res = await self._devCtrl.SendCommand(nodeid=self._nodeid, endpoint=endpointId, payload=req)
logger.info(f"Received response: {res}")
if res.networkingStatus != Clusters.NetworkCommissioning.Enums.NetworkCommissioningStatus.kSuccess:
raise AssertionError(f"Unexpected result: {res.networkingStatus}")
if res.networkIndex != 0:
raise AssertionError(
f"Unexpected result: {res.networkIndex} (should be 0)")
await self.must_verify_breadcrumb()

logger.info(f"Check network list")
res = await self._devCtrl.ReadAttribute(nodeid=self._nodeid, attributes=[(endpointId, Clusters.NetworkCommissioning.Attributes.Networks)], returnClusterObject=True)
Expand All @@ -181,12 +197,13 @@ async def test_wifi(self, endpointId):

logger.info(f"Connect to a network")
req = Clusters.NetworkCommissioning.Commands.ConnectNetwork(
networkID=TEST_WIFI_SSID.encode(), breadcrumb=0)
networkID=TEST_WIFI_SSID.encode(), breadcrumb=self.with_breadcrumb())
res = await self._devCtrl.SendCommand(nodeid=self._nodeid, endpoint=endpointId, payload=req)
logger.info(f"Got response: {res}")
if res.networkingStatus != Clusters.NetworkCommissioning.Enums.NetworkCommissioningStatus.kSuccess:
raise AssertionError(f"Unexpected result: {res.networkingStatus}")
logger.info(f"Device connected to a network.")
await self.must_verify_breadcrumb()

# Disarm the failsafe
logger.info(f"Disarming the failsafe")
Expand Down Expand Up @@ -259,11 +276,12 @@ async def test_thread(self, endpointId):
# Scan networks
logger.info(f"Scan networks")
req = Clusters.NetworkCommissioning.Commands.ScanNetworks(
ssid=b'', breadcrumb=0)
ssid=b'', breadcrumb=self.with_breadcrumb())
res = await self._devCtrl.SendCommand(nodeid=self._nodeid, endpoint=endpointId, payload=req)
logger.info(f"Received response: {res}")
if res.networkingStatus != Clusters.NetworkCommissioning.Enums.NetworkCommissioningStatus.kSuccess:
raise AssertionError(f"Unexpected result: {res.networkingStatus}")
await self.must_verify_breadcrumb()

# Arm the failsafe before making network config changes
logger.info(f"Arming the failsafe")
Expand All @@ -279,24 +297,26 @@ async def test_thread(self, endpointId):
if len(networkList) != 0:
logger.info(f"Removing existing network")
req = Clusters.NetworkCommissioning.Commands.RemoveNetwork(
networkID=networkList[0].networkID, breadcrumb=0)
networkID=networkList[0].networkID, breadcrumb=self.with_breadcrumb())
res = await self._devCtrl.SendCommand(nodeid=self._nodeid, endpoint=endpointId, payload=req)
logger.info(f"Received response: {res}")
if res.networkingStatus != Clusters.NetworkCommissioning.Enums.NetworkCommissioningStatus.kSuccess:
raise AssertionError(
f"Unexpected result: {res.networkingStatus}")
await self.must_verify_breadcrumb()

# Add first network
logger.info(f"Adding first test network")
req = Clusters.NetworkCommissioning.Commands.AddOrUpdateThreadNetwork(
operationalDataset=TEST_THREAD_NETWORK_DATASET_TLVS[0], breadcrumb=0)
operationalDataset=TEST_THREAD_NETWORK_DATASET_TLVS[0], breadcrumb=self.with_breadcrumb())
res = await self._devCtrl.SendCommand(nodeid=self._nodeid, endpoint=endpointId, payload=req)
logger.info(f"Received response: {res}")
if res.networkingStatus != Clusters.NetworkCommissioning.Enums.NetworkCommissioningStatus.kSuccess:
raise AssertionError(f"Unexpected result: {res.networkingStatus}")
if res.networkIndex != 0:
raise AssertionError(
f"Unexpected result: {res.networkIndex} (should be 0)")
await self.must_verify_breadcrumb()

logger.info(f"Check network list")
res = await self._devCtrl.ReadAttribute(nodeid=self._nodeid, attributes=[(endpointId, Clusters.NetworkCommissioning.Attributes.Networks)], returnClusterObject=True)
Expand All @@ -311,12 +331,13 @@ async def test_thread(self, endpointId):

logger.info(f"Connect to a network")
req = Clusters.NetworkCommissioning.Commands.ConnectNetwork(
networkID=TEST_THREAD_NETWORK_IDS[0], breadcrumb=0)
networkID=TEST_THREAD_NETWORK_IDS[0], breadcrumb=self.with_breadcrumb())
res = await self._devCtrl.SendCommand(nodeid=self._nodeid, endpoint=endpointId, payload=req)
logger.info(f"Got response: {res}")
if res.networkingStatus != Clusters.NetworkCommissioning.Enums.NetworkCommissioningStatus.kSuccess:
raise AssertionError(f"Unexpected result: {res.networkingStatus}")
logger.info(f"Device connected to a network.")
await self.must_verify_breadcrumb()

# Disarm the failsafe
logger.info(f"Disarming the failsafe")
Expand Down