diff --git a/examples/chip-tool/templates/tests/tests.js b/examples/chip-tool/templates/tests/tests.js index c1e16776591d1b..0487a94e93cadd 100644 --- a/examples/chip-tool/templates/tests/tests.js +++ b/examples/chip-tool/templates/tests/tests.js @@ -549,6 +549,7 @@ function getTests() 'TestSystemCommands', 'TestBinding', 'TestUserLabelCluster', + 'TestArmFailSafe', ]; const MultiAdmin = [ diff --git a/src/app/tests/suites/TestArmFailSafe.yaml b/src/app/tests/suites/TestArmFailSafe.yaml new file mode 100644 index 00000000000000..9cd001595462e7 --- /dev/null +++ b/src/app/tests/suites/TestArmFailSafe.yaml @@ -0,0 +1,72 @@ +# Copyright (c) 2022 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: ArmFailSafe Tests + +config: + nodeId: 0x12344321 + endpoint: 0 + discriminator: + type: INT16U + defaultValue: 3840 + payload: + type: CHAR_STRING + defaultValue: "MT:-24J0AFN00KA0648G00" # This value needs to be generated automatically + +tests: + - label: "Reboot target device" + cluster: "SystemCommands" + command: "Reboot" + arguments: + values: + - name: "discriminator" + value: discriminator + + - label: "Wait for the alpha device to be retrieved " + cluster: "DelayCommands" + command: "WaitForCommissionee" + arguments: + values: + - name: "nodeId" + value: nodeId + + - label: "Query fabrics list" + command: "readAttribute" + cluster: "Operational Credentials" + attribute: "Fabrics" + response: + value: [{ Label: "", nodeId: nodeId }] + constraints: + type: list + + - label: "ArmFailSafe on target device with timeout 0" + cluster: "General Commissioning" + command: "ArmFailSafe" + arguments: + values: + - name: "expiryLengthSeconds" + value: 0 + - name: "breadcrumb" + value: 0 + response: + values: + - name: "errorCode" + value: 0 + + - label: "Reads NodeLabel mandatory attribute of target device" + command: "readAttribute" + cluster: "Basic" + attribute: "NodeLabel" + response: + value: "" diff --git a/src/platform/Ameba/ConnectivityManagerImpl.cpp b/src/platform/Ameba/ConnectivityManagerImpl.cpp index 0f7878639add42..1427fa939f96a5 100644 --- a/src/platform/Ameba/ConnectivityManagerImpl.cpp +++ b/src/platform/Ameba/ConnectivityManagerImpl.cpp @@ -72,7 +72,7 @@ CHIP_ERROR ConnectivityManagerImpl::_Init() // Set callback functions from chip_porting chip_connmgr_set_callback_func((chip_connmgr_callback)(conn_callback_dispatcher), this); - + // Register WiFi event handlers wifi_reg_event_handler(WIFI_EVENT_CONNECT, ConnectivityManagerImpl::RtkWiFiStationConnectedHandler, NULL); wifi_reg_event_handler(WIFI_EVENT_DISCONNECT, ConnectivityManagerImpl::RtkWiFiStationDisconnectedHandler, NULL); diff --git a/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp b/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp index 57bc627697b12e..142d2b2daa7e5b 100644 --- a/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp +++ b/src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp @@ -38,10 +38,10 @@ constexpr char kWiFiCredentialsKeyName[] = "wifi-pass"; CHIP_ERROR AmebaWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChangeCallback) { CHIP_ERROR err; - size_t ssidLen = 0; - size_t credentialsLen = 0; - mpScanCallback = nullptr; - mpConnectCallback = nullptr; + size_t ssidLen = 0; + size_t credentialsLen = 0; + mpScanCallback = nullptr; + mpConnectCallback = nullptr; mpStatusChangeCallback = networkStatusChangeCallback; err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiCredentialsKeyName, mSavedNetwork.credentials, @@ -59,7 +59,7 @@ CHIP_ERROR AmebaWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChan mSavedNetwork.credentialsLen = credentialsLen; mSavedNetwork.ssidLen = ssidLen; - mStagingNetwork = mSavedNetwork; + mStagingNetwork = mSavedNetwork; return err; } @@ -268,7 +268,7 @@ void AmebaWiFiDriver::OnNetworkStatusChange() Network configuredNetwork; rtw_wifi_setting_t mWiFiSetting; CHIP_GetWiFiConfig(&mWiFiSetting); - bool staEnable = (mWiFiSetting.mode == RTW_MODE_STA || mWiFiSetting.mode == RTW_MODE_STA_AP); + bool staEnable = (mWiFiSetting.mode == RTW_MODE_STA || mWiFiSetting.mode == RTW_MODE_STA_AP); bool staConnected = (mWiFiSetting.ssid[0] != 0); CHIP_ERROR err = GetConfiguredNetwork(configuredNetwork); diff --git a/zzz_generated/chip-tool/zap-generated/test/Commands.h b/zzz_generated/chip-tool/zap-generated/test/Commands.h index 326e09a15b094b..d10ac67c62a2d5 100644 --- a/zzz_generated/chip-tool/zap-generated/test/Commands.h +++ b/zzz_generated/chip-tool/zap-generated/test/Commands.h @@ -209,6 +209,7 @@ class TestList : public Command printf("TestSystemCommands\n"); printf("TestBinding\n"); printf("TestUserLabelCluster\n"); + printf("TestArmFailSafe\n"); printf("TestMultiAdmin\n"); printf("Test_TC_SWDIAG_1_1\n"); printf("Test_TC_SWDIAG_2_1\n"); @@ -100557,6 +100558,242 @@ class TestUserLabelClusterSuite : public TestCommand } }; +class TestArmFailSafeSuite : public TestCommand +{ +public: + TestArmFailSafeSuite(CredentialIssuerCommands * credsIssuerConfig) : + TestCommand("TestArmFailSafe", credsIssuerConfig), mTestIndex(0) + { + AddArgument("nodeId", 0, UINT64_MAX, &mNodeId); + AddArgument("endpoint", 0, UINT16_MAX, &mEndpoint); + AddArgument("discriminator", 0, UINT16_MAX, &mDiscriminator); + AddArgument("payload", &mPayload); + AddArgument("timeout", 0, UINT16_MAX, &mTimeout); + } + + ~TestArmFailSafeSuite() {} + + /////////// TestCommand Interface ///////// + void NextTest() override + { + CHIP_ERROR err = CHIP_NO_ERROR; + + if (0 == mTestIndex) + { + ChipLogProgress(chipTool, " **** Test Start: TestArmFailSafe\n"); + } + + if (mTestCount == mTestIndex) + { + ChipLogProgress(chipTool, " **** Test Complete: TestArmFailSafe\n"); + SetCommandExitStatus(CHIP_NO_ERROR); + return; + } + + Wait(); + + // Ensure we increment mTestIndex before we start running the relevant + // command. That way if we lose the timeslice after we send the message + // but before our function call returns, we won't end up with an + // incorrect mTestIndex value observed when we get the response. + switch (mTestIndex++) + { + case 0: + ChipLogProgress(chipTool, " ***** Test Step 0 : Reboot target device\n"); + err = TestRebootTargetDevice_0(); + break; + case 1: + ChipLogProgress(chipTool, " ***** Test Step 1 : Wait for the alpha device to be retrieved \n"); + err = TestWaitForTheAlphaDeviceToBeRetrieved_1(); + break; + case 2: + ChipLogProgress(chipTool, " ***** Test Step 2 : Query fabrics list\n"); + err = TestQueryFabricsList_2(); + break; + case 3: + ChipLogProgress(chipTool, " ***** Test Step 3 : ArmFailSafe on target device with timeout 0\n"); + err = TestArmFailSafeOnTargetDeviceWithTimeout0_3(); + break; + case 4: + ChipLogProgress(chipTool, " ***** Test Step 4 : Reads NodeLabel mandatory attribute of target device\n"); + err = TestReadsNodeLabelMandatoryAttributeOfTargetDevice_4(); + break; + } + + if (CHIP_NO_ERROR != err) + { + ChipLogError(chipTool, " ***** Test Failure: %s\n", chip::ErrorStr(err)); + SetCommandExitStatus(err); + } + } + + chip::System::Clock::Timeout GetWaitDuration() const override + { + return chip::System::Clock::Seconds16(mTimeout.ValueOr(kTimeoutInSeconds)); + } + +private: + std::atomic_uint16_t mTestIndex; + const uint16_t mTestCount = 5; + + chip::Optional mNodeId; + chip::Optional mEndpoint; + chip::Optional mDiscriminator; + chip::Optional mPayload; + chip::Optional mTimeout; + + void OnResponse(const chip::app::StatusIB & status, chip::TLV::TLVReader * data) override + { + bool shouldContinue = false; + + switch (mTestIndex - 1) + { + case 0: + VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), 0)); + shouldContinue = true; + break; + case 1: + VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), 0)); + shouldContinue = true; + break; + default: + LogErrorOnFailure(ContinueOnChipMainThread(CHIP_ERROR_INVALID_ARGUMENT)); + } + + if (shouldContinue) + { + ContinueOnChipMainThread(CHIP_NO_ERROR); + } + } + + static void OnFailureCallback_2(void * context, CHIP_ERROR error) + { + (static_cast(context))->OnFailureResponse_2(error); + } + + static void + OnSuccessCallback_2(void * context, + const chip::app::DataModel::DecodableList< + chip::app::Clusters::OperationalCredentials::Structs::FabricDescriptor::DecodableType> & fabrics) + { + (static_cast(context))->OnSuccessResponse_2(fabrics); + } + + static void OnFailureCallback_4(void * context, CHIP_ERROR error) + { + (static_cast(context))->OnFailureResponse_4(error); + } + + static void OnSuccessCallback_4(void * context, chip::CharSpan nodeLabel) + { + (static_cast(context))->OnSuccessResponse_4(nodeLabel); + } + + // + // Tests methods + // + + CHIP_ERROR TestRebootTargetDevice_0() + { + SetIdentity(kIdentityAlpha); + return Reboot(mDiscriminator.HasValue() ? mDiscriminator.Value() : 3840U); + } + + CHIP_ERROR TestWaitForTheAlphaDeviceToBeRetrieved_1() + { + SetIdentity(kIdentityAlpha); + return WaitForCommissionee(mNodeId.HasValue() ? mNodeId.Value() : 305414945ULL); + } + + CHIP_ERROR TestQueryFabricsList_2() + { + const chip::EndpointId endpoint = mEndpoint.HasValue() ? mEndpoint.Value() : 0; + chip::Controller::OperationalCredentialsClusterTest cluster; + cluster.Associate(mDevices[kIdentityAlpha], endpoint); + + ReturnErrorOnFailure(cluster.ReadAttribute( + this, OnSuccessCallback_2, OnFailureCallback_2, true)); + return CHIP_NO_ERROR; + } + + void OnFailureResponse_2(CHIP_ERROR error) + { + chip::app::StatusIB status(error); + ThrowFailureResponse(error); + } + + void OnSuccessResponse_2(const chip::app::DataModel::DecodableList< + chip::app::Clusters::OperationalCredentials::Structs::FabricDescriptor::DecodableType> & fabrics) + { + { + auto iter_0 = fabrics.begin(); + VerifyOrReturn(CheckNextListItemDecodes("fabrics", iter_0, 0)); + VerifyOrReturn(CheckValueAsString("fabrics[0].label", iter_0.GetValue().label, chip::CharSpan("", 0))); + VerifyOrReturn(CheckNoMoreListItems("fabrics", iter_0, 1)); + } + VerifyOrReturn(CheckConstraintType("fabrics", "", "list")); + NextTest(); + } + + CHIP_ERROR TestArmFailSafeOnTargetDeviceWithTimeout0_3() + { + const chip::EndpointId endpoint = mEndpoint.HasValue() ? mEndpoint.Value() : 0; + using RequestType = chip::app::Clusters::GeneralCommissioning::Commands::ArmFailSafe::Type; + + RequestType request; + request.expiryLengthSeconds = 0U; + request.breadcrumb = 0ULL; + + auto success = [](void * context, const typename RequestType::ResponseType & data) { + (static_cast(context))->OnSuccessResponse_3(data.errorCode, data.debugText); + }; + + auto failure = [](void * context, CHIP_ERROR error) { + (static_cast(context))->OnFailureResponse_3(error); + }; + + ReturnErrorOnFailure(chip::Controller::InvokeCommand(mDevices[kIdentityAlpha], this, success, failure, endpoint, request)); + return CHIP_NO_ERROR; + } + + void OnFailureResponse_3(CHIP_ERROR error) + { + chip::app::StatusIB status(error); + ThrowFailureResponse(error); + } + + void OnSuccessResponse_3(chip::app::Clusters::GeneralCommissioning::CommissioningError errorCode, chip::CharSpan debugText) + { + VerifyOrReturn(CheckValue("errorCode", errorCode, 0)); + + NextTest(); + } + + CHIP_ERROR TestReadsNodeLabelMandatoryAttributeOfTargetDevice_4() + { + const chip::EndpointId endpoint = mEndpoint.HasValue() ? mEndpoint.Value() : 0; + chip::Controller::BasicClusterTest cluster; + cluster.Associate(mDevices[kIdentityAlpha], endpoint); + + ReturnErrorOnFailure(cluster.ReadAttribute( + this, OnSuccessCallback_4, OnFailureCallback_4, true)); + return CHIP_NO_ERROR; + } + + void OnFailureResponse_4(CHIP_ERROR error) + { + chip::app::StatusIB status(error); + ThrowFailureResponse(error); + } + + void OnSuccessResponse_4(chip::CharSpan nodeLabel) + { + VerifyOrReturn(CheckValueAsString("nodeLabel", nodeLabel, chip::CharSpan("", 0))); + + NextTest(); + } +}; + class TestMultiAdminSuite : public TestCommand { public: @@ -127655,6 +127892,7 @@ void registerCommandsTests(Commands & commands, CredentialIssuerCommands * creds make_unique(credsIssuerConfig), make_unique(credsIssuerConfig), make_unique(credsIssuerConfig), + make_unique(credsIssuerConfig), make_unique(credsIssuerConfig), make_unique(credsIssuerConfig), make_unique(credsIssuerConfig),