Skip to content

Commit

Permalink
[NetworkCommissioning] Send Response Command instead of status code f…
Browse files Browse the repository at this point in the history
…or network commissioning cluster (#12010)
  • Loading branch information
erjiaqing authored Nov 19, 2021
1 parent d9256a2 commit dcca930
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ bool emberAfNetworkCommissioningClusterAddThreadNetworkCallback(app::CommandHand
auto & breadcrumb = commandData.breadcrumb;
auto & timeoutMs = commandData.timeoutMs;

EmberAfNetworkCommissioningError err = app::Clusters::NetworkCommissioning::OnAddThreadNetworkCommandCallbackInternal(
nullptr, emberAfCurrentEndpoint(), operationalDataset, breadcrumb, timeoutMs);
emberAfSendImmediateDefaultResponse(err == EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_SUCCESS ? EMBER_ZCL_STATUS_SUCCESS
: EMBER_ZCL_STATUS_FAILURE);
app::Clusters::NetworkCommissioning::OnAddThreadNetworkCommandCallbackInternal(commandObj, commandPath, operationalDataset,
breadcrumb, timeoutMs);
return true;
}

Expand All @@ -59,10 +57,8 @@ bool emberAfNetworkCommissioningClusterAddWiFiNetworkCallback(app::CommandHandle
auto & breadcrumb = commandData.breadcrumb;
auto & timeoutMs = commandData.timeoutMs;

EmberAfNetworkCommissioningError err = app::Clusters::NetworkCommissioning::OnAddWiFiNetworkCommandCallbackInternal(
nullptr, emberAfCurrentEndpoint(), ssid, credentials, breadcrumb, timeoutMs);
emberAfSendImmediateDefaultResponse(err == EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_SUCCESS ? EMBER_ZCL_STATUS_SUCCESS
: EMBER_ZCL_STATUS_FAILURE);
app::Clusters::NetworkCommissioning::OnAddWiFiNetworkCommandCallbackInternal(commandObj, commandPath, ssid, credentials,
breadcrumb, timeoutMs);
return true;
}

Expand All @@ -74,10 +70,8 @@ bool emberAfNetworkCommissioningClusterEnableNetworkCallback(app::CommandHandler
auto & breadcrumb = commandData.breadcrumb;
auto & timeoutMs = commandData.timeoutMs;

EmberAfNetworkCommissioningError err = app::Clusters::NetworkCommissioning::OnEnableNetworkCommandCallbackInternal(
nullptr, emberAfCurrentEndpoint(), networkID, breadcrumb, timeoutMs);
emberAfSendImmediateDefaultResponse(err == EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_SUCCESS ? EMBER_ZCL_STATUS_SUCCESS
: EMBER_ZCL_STATUS_FAILURE);
app::Clusters::NetworkCommissioning::OnEnableNetworkCommandCallbackInternal(commandObj, commandPath, networkID, breadcrumb,
timeoutMs);
return true;
}

Expand Down
32 changes: 19 additions & 13 deletions src/app/clusters/network-commissioning/network-commissioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <platform/ConnectivityManager.h>
#include <platform/internal/DeviceControlServer.h>

#include <app-common/zap-generated/cluster-objects.h>

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <platform/ThreadStackManager.h>
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
Expand Down Expand Up @@ -104,10 +106,10 @@ namespace {
NetworkInfo sNetworks[kMaxNetworks];
} // namespace

EmberAfNetworkCommissioningError OnAddThreadNetworkCommandCallbackInternal(app::CommandHandler *, EndpointId,
ByteSpan operationalDataset, uint64_t breadcrumb,
uint32_t timeoutMs)
void OnAddThreadNetworkCommandCallbackInternal(app::CommandHandler * apCommandHandler, const app::ConcreteCommandPath & commandPath,
ByteSpan operationalDataset, uint64_t breadcrumb, uint32_t timeoutMs)
{
Commands::AddThreadNetworkResponse::Type response;
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
EmberAfNetworkCommissioningError err = EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_BOUNDS_EXCEEDED;

Expand Down Expand Up @@ -145,18 +147,19 @@ EmberAfNetworkCommissioningError OnAddThreadNetworkCommandCallbackInternal(app::
// TODO: We should encode response command here.

ChipLogDetail(Zcl, "AddThreadNetwork: %" PRIu8, err);
return err;
response.errorCode = err;
#else
// The target does not supports ThreadNetwork. We should not add AddThreadNetwork command in that case then the upper layer will
// return "Command not found" error.
return EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_UNKNOWN_ERROR;
response.errorCode = EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_UNKNOWN_ERROR;
#endif
apCommandHandler->AddResponseData(commandPath, response);
}

EmberAfNetworkCommissioningError OnAddWiFiNetworkCommandCallbackInternal(app::CommandHandler *, EndpointId, ByteSpan ssid,
ByteSpan credentials, uint64_t breadcrumb,
uint32_t timeoutMs)
void OnAddWiFiNetworkCommandCallbackInternal(app::CommandHandler * apCommandHandler, const app::ConcreteCommandPath & commandPath,
ByteSpan ssid, ByteSpan credentials, uint64_t breadcrumb, uint32_t timeoutMs)
{
Commands::AddWiFiNetworkResponse::Type response;
#if defined(CHIP_DEVICE_LAYER_TARGET)
EmberAfNetworkCommissioningError err = EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_BOUNDS_EXCEEDED;

Expand Down Expand Up @@ -202,12 +205,13 @@ EmberAfNetworkCommissioningError OnAddWiFiNetworkCommandCallbackInternal(app::Co
// TODO: We should encode response command here.

ChipLogDetail(Zcl, "AddWiFiNetwork: %" PRIu8, err);
return err;
response.errorCode = err;
#else
// The target does not supports WiFiNetwork.
// return "Command not found" error.
return EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_UNKNOWN_ERROR;
response.errorCode = EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_UNKNOWN_ERROR;
#endif
apCommandHandler->AddResponseData(commandPath, response);
}

namespace {
Expand Down Expand Up @@ -252,9 +256,10 @@ CHIP_ERROR DoEnableNetwork(NetworkInfo * network)
}
} // namespace

EmberAfNetworkCommissioningError OnEnableNetworkCommandCallbackInternal(app::CommandHandler *, EndpointId, ByteSpan networkID,
uint64_t breadcrumb, uint32_t timeoutMs)
void OnEnableNetworkCommandCallbackInternal(app::CommandHandler * apCommandHandler, const app::ConcreteCommandPath & commandPath,
ByteSpan networkID, uint64_t breadcrumb, uint32_t timeoutMs)
{
Commands::EnableNetworkResponse::Type response;
size_t networkSeq;
EmberAfNetworkCommissioningError err = EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_NETWORK_ID_NOT_FOUND;

Expand All @@ -277,7 +282,8 @@ EmberAfNetworkCommissioningError OnEnableNetworkCommandCallbackInternal(app::Com
{
DeviceLayer::Internal::DeviceControlServer::DeviceControlSvr().EnableNetworkForOperational(networkID);
}
return err;
response.errorCode = err;
apCommandHandler->AddResponseData(commandPath, response);
}

} // namespace NetworkCommissioning
Expand Down
14 changes: 6 additions & 8 deletions src/app/clusters/network-commissioning/network-commissioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ namespace chip {
namespace app {
namespace Clusters {
namespace NetworkCommissioning {
EmberAfNetworkCommissioningError OnAddThreadNetworkCommandCallbackInternal(app::CommandHandler *, EndpointId,
ByteSpan operationalDataset, uint64_t breadcrumb,
uint32_t timeoutMs);
EmberAfNetworkCommissioningError OnAddWiFiNetworkCommandCallbackInternal(app::CommandHandler *, EndpointId, ByteSpan ssid,
ByteSpan credentials, uint64_t breadcrumb,
uint32_t timeoutMs);
EmberAfNetworkCommissioningError OnEnableNetworkCommandCallbackInternal(app::CommandHandler *, EndpointId, ByteSpan networkID,
uint64_t breadcrumb, uint32_t timeoutMs);
void OnAddThreadNetworkCommandCallbackInternal(app::CommandHandler *, const app::ConcreteCommandPath & commandPath,
ByteSpan operationalDataset, uint64_t breadcrumb, uint32_t timeoutMs);
void OnAddWiFiNetworkCommandCallbackInternal(app::CommandHandler *, const app::ConcreteCommandPath & commandPath, ByteSpan ssid,
ByteSpan credentials, uint64_t breadcrumb, uint32_t timeoutMs);
void OnEnableNetworkCommandCallbackInternal(app::CommandHandler *, const app::ConcreteCommandPath & commandPath, ByteSpan networkID,
uint64_t breadcrumb, uint32_t timeoutMs);
} // namespace NetworkCommissioning

} // namespace Clusters
Expand Down
10 changes: 9 additions & 1 deletion src/controller/python/test/test_scripts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,14 @@ def TestCloseSession(self, nodeid: int):
def TestNetworkCommissioning(self, nodeid: int, endpoint: int, group: int, dataset: str, network_id: str):
self.logger.info("Commissioning network to device {}".format(nodeid))
try:
self.devCtrl.ZCLSend("NetworkCommissioning", "AddThreadNetwork", nodeid, endpoint, group, {
(err, resp) = self.devCtrl.ZCLSend("NetworkCommissioning", "AddThreadNetwork", nodeid, endpoint, group, {
"operationalDataset": bytes.fromhex(dataset),
"breadcrumb": 0,
"timeoutMs": 1000}, blocking=True)
self.logger.info(f"Received response: {resp}")
if resp.errorCode != 0:
self.logger.exception("Failed to add Thread network.")
return False
except Exception as ex:
self.logger.exception("Failed to send AddThreadNetwork command")
return False
Expand All @@ -165,6 +169,10 @@ def TestNetworkCommissioning(self, nodeid: int, endpoint: int, group: int, datas
"networkID": bytes.fromhex(network_id),
"breadcrumb": 0,
"timeoutMs": 1000}, blocking=True)
self.logger.info(f"Received response: {resp}")
if resp.errorCode != 0:
self.logger.exception("Failed to enable Thread network.")
return False
except Exception as ex:
self.logger.exception("Failed to send EnableNetwork command")
return False
Expand Down

0 comments on commit dcca930

Please sign in to comment.