From 2421225fe8200e90538dc6290f4a4cbcf4b00a9f Mon Sep 17 00:00:00 2001 From: Song GUO Date: Sat, 20 Nov 2021 00:25:36 +0800 Subject: [PATCH] [NetworkCommissioning] Send Response Command instead of status code for network commissioning cluster (#12010) --- .../network-commissioning-ember.cpp | 18 ++++------- .../network-commissioning.cpp | 32 +++++++++++-------- .../network-commissioning.h | 14 ++++---- .../python/test/test_scripts/base.py | 10 +++++- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/app/clusters/network-commissioning/network-commissioning-ember.cpp b/src/app/clusters/network-commissioning/network-commissioning-ember.cpp index f4991e02411458..e26f11e95a7d44 100644 --- a/src/app/clusters/network-commissioning/network-commissioning-ember.cpp +++ b/src/app/clusters/network-commissioning/network-commissioning-ember.cpp @@ -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; } @@ -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; } @@ -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; } diff --git a/src/app/clusters/network-commissioning/network-commissioning.cpp b/src/app/clusters/network-commissioning/network-commissioning.cpp index c11b57203b6a86..000be12a1615ee 100644 --- a/src/app/clusters/network-commissioning/network-commissioning.cpp +++ b/src/app/clusters/network-commissioning/network-commissioning.cpp @@ -30,6 +30,8 @@ #include #include +#include + #if CHIP_DEVICE_CONFIG_ENABLE_THREAD #include #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD @@ -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; @@ -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; @@ -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 { @@ -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; @@ -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 diff --git a/src/app/clusters/network-commissioning/network-commissioning.h b/src/app/clusters/network-commissioning/network-commissioning.h index 7db189d71e3225..bcef0338289c8b 100644 --- a/src/app/clusters/network-commissioning/network-commissioning.h +++ b/src/app/clusters/network-commissioning/network-commissioning.h @@ -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 diff --git a/src/controller/python/test/test_scripts/base.py b/src/controller/python/test/test_scripts/base.py index d469c1d2b9d3fc..f00d5c6a96a7f7 100644 --- a/src/controller/python/test/test_scripts/base.py +++ b/src/controller/python/test/test_scripts/base.py @@ -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 @@ -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