diff --git a/scripts/tests/chiptest/accessories.py b/scripts/tests/chiptest/accessories.py index a91af7bdaf6211..cc6b095ed531b3 100644 --- a/scripts/tests/chiptest/accessories.py +++ b/scripts/tests/chiptest/accessories.py @@ -81,6 +81,16 @@ def reboot(self, name, discriminator): return accessory.stop() and accessory.start(discriminator) return False + def factoryResetAll(self): + for accessory in self.__accessories.values(): + accessory.factoryReset() + + def factoryReset(self, name): + accessory = self.__accessories[name] + if accessory: + return accessory.factoryReset() + return False + def ping(self): return True @@ -90,6 +100,7 @@ def __startXMLRPCServer(self): self.server.register_function(self.start, 'start') self.server.register_function(self.stop, 'stop') self.server.register_function(self.reboot, 'reboot') + self.server.register_function(self.factoryReset, 'factoryReset') self.server.register_function(self.ping, 'ping') self.server_thread = threading.Thread(target=self.__handle_request) diff --git a/scripts/tests/chiptest/test_definition.py b/scripts/tests/chiptest/test_definition.py index 6d0791a25130fb..3266bc9f0eb7c4 100644 --- a/scripts/tests/chiptest/test_definition.py +++ b/scripts/tests/chiptest/test_definition.py @@ -64,6 +64,16 @@ def reboot(self, discriminator): return True return False + def factoryReset(self): + storage = '/tmp/chip_kvs' + if platform.system() == 'Darwin': + storage = str(Path.home()) + '/Documents/chip.store' + + if os.path.exists(storage): + os.unlink(storage) + + return True + def poll(self): # When the server is manually stopped, process polling is overriden so the other # processes that depends on the accessory beeing alive does not stop. @@ -192,16 +202,8 @@ def Run(self, runner, apps_register, paths: ApplicationPaths): if os.path.exists(f): os.unlink(f) - # Remove server all_clusters_app or tv_app storage, so it will be commissionable again - if platform.system() == 'Linux': - if os.path.exists('/tmp/chip_kvs'): - os.unlink('/tmp/chip_kvs') - - if platform.system() == "Darwin": - if os.path.exists(str(Path.home()) + '/Documents/chip.store'): - os.unlink(str(Path.home()) + '/Documents/chip.store') - app = App(runner, app_cmd) + app.factoryReset() # Remove server application storage, so it will be commissionable again app.start(str(randrange(1, 4096))) apps_register.add("default", app) @@ -216,4 +218,5 @@ def Run(self, runner, apps_register, paths: ApplicationPaths): raise finally: apps_register.killAll() + apps_register.factoryResetAll() apps_register.removeAll() diff --git a/src/app/tests/suites/TestSystemCommands.yaml b/src/app/tests/suites/TestSystemCommands.yaml index 4a84cd273e3716..b22b2b5c6cfb0b 100644 --- a/src/app/tests/suites/TestSystemCommands.yaml +++ b/src/app/tests/suites/TestSystemCommands.yaml @@ -39,3 +39,6 @@ tests: values: - name: "discriminator" value: 2222 + + - label: "Factory Reset the accessory" + command: "FactoryReset" diff --git a/src/app/tests/suites/commands/system/SystemCommands.cpp b/src/app/tests/suites/commands/system/SystemCommands.cpp index 7fb1eb23fcd6cb..739d171808933e 100644 --- a/src/app/tests/suites/commands/system/SystemCommands.cpp +++ b/src/app/tests/suites/commands/system/SystemCommands.cpp @@ -34,8 +34,7 @@ CHIP_ERROR SystemCommands::Start(uint16_t discriminator) char command[128]; VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s %u", scriptDir, scriptName, discriminator) >= 0, CHIP_ERROR_INTERNAL); - VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL); - return ContinueOnChipMainThread(); + return RunInternal(command); } CHIP_ERROR SystemCommands::Stop() @@ -45,9 +44,7 @@ CHIP_ERROR SystemCommands::Stop() char command[128]; VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s", scriptDir, scriptName) >= 0, CHIP_ERROR_INTERNAL); - - VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL); - return ContinueOnChipMainThread(); + return RunInternal(command); } CHIP_ERROR SystemCommands::Reboot(uint16_t discriminator) @@ -58,7 +55,21 @@ CHIP_ERROR SystemCommands::Reboot(uint16_t discriminator) char command[128]; VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s %u", scriptDir, scriptName, discriminator) >= 0, CHIP_ERROR_INTERNAL); + return RunInternal(command); +} + +CHIP_ERROR SystemCommands::FactoryReset() +{ + const char * scriptDir = getScriptsFolder(); + constexpr const char * scriptName = "FactoryReset.py"; + char command[128]; + VerifyOrReturnError(snprintf(command, sizeof(command), "%s%s", scriptDir, scriptName) >= 0, CHIP_ERROR_INTERNAL); + return RunInternal(command); +} + +CHIP_ERROR SystemCommands::RunInternal(const char * command) +{ VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL); return ContinueOnChipMainThread(); } diff --git a/src/app/tests/suites/commands/system/SystemCommands.h b/src/app/tests/suites/commands/system/SystemCommands.h index 3f5e249572dc8b..feedde867f436d 100644 --- a/src/app/tests/suites/commands/system/SystemCommands.h +++ b/src/app/tests/suites/commands/system/SystemCommands.h @@ -31,4 +31,8 @@ class SystemCommands CHIP_ERROR Start(uint16_t discriminator); CHIP_ERROR Stop(); CHIP_ERROR Reboot(uint16_t discriminator); + CHIP_ERROR FactoryReset(); + +private: + CHIP_ERROR RunInternal(const char * command); }; diff --git a/src/app/tests/suites/commands/system/scripts/FactoryReset.py b/src/app/tests/suites/commands/system/scripts/FactoryReset.py new file mode 100755 index 00000000000000..b435e220dfc604 --- /dev/null +++ b/src/app/tests/suites/commands/system/scripts/FactoryReset.py @@ -0,0 +1,27 @@ +#!/usr/bin/env -S python3 -B + +# 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. + +import sys +import xmlrpc.client + +IP = '127.0.0.1' +PORT = 9000 + +if sys.platform == 'linux': + IP = '10.10.10.5' + +with xmlrpc.client.ServerProxy('http://' + IP + ':' + str(PORT) + '/', allow_none=True) as proxy: + proxy.factoryReset('default') diff --git a/src/app/zap-templates/common/simulated-clusters/clusters/SystemCommands.js b/src/app/zap-templates/common/simulated-clusters/clusters/SystemCommands.js index 246b8285643345..70a283532665a2 100644 --- a/src/app/zap-templates/common/simulated-clusters/clusters/SystemCommands.js +++ b/src/app/zap-templates/common/simulated-clusters/clusters/SystemCommands.js @@ -42,9 +42,15 @@ const Reboot = { response : { arguments : [] } }; +const FactoryReset = { + name : 'FactoryReset', + arguments : [], + response : { arguments : [] } +}; + const SystemCommands = { name : 'SystemCommands', - commands : [ Start, Stop, Reboot ], + commands : [ Start, Stop, Reboot, FactoryReset ], }; // diff --git a/zzz_generated/chip-tool/zap-generated/test/Commands.h b/zzz_generated/chip-tool/zap-generated/test/Commands.h index 107c8eb95e9789..1c2ed38c68523c 100644 --- a/zzz_generated/chip-tool/zap-generated/test/Commands.h +++ b/zzz_generated/chip-tool/zap-generated/test/Commands.h @@ -73301,6 +73301,10 @@ class TestSystemCommands : public TestCommand ChipLogProgress(chipTool, " ***** Test Step 3 : Reboot the accessory with an other given discriminator\n"); err = TestRebootTheAccessoryWithAnOtherGivenDiscriminator_3(); break; + case 4: + ChipLogProgress(chipTool, " ***** Test Step 4 : Factory Reset the accessory\n"); + err = TestFactoryResetTheAccessory_4(); + break; } if (CHIP_NO_ERROR != err) @@ -73312,7 +73316,7 @@ class TestSystemCommands : public TestCommand private: std::atomic_uint16_t mTestIndex; - const uint16_t mTestCount = 4; + const uint16_t mTestCount = 5; chip::Optional mCluster; chip::Optional mEndpoint; @@ -73344,6 +73348,12 @@ class TestSystemCommands : public TestCommand SetIdentity(kIdentityAlpha); return Reboot(2222); } + + CHIP_ERROR TestFactoryResetTheAccessory_4() + { + SetIdentity(kIdentityAlpha); + return FactoryReset(); + } }; class Test_TC_SWDIAG_1_1 : public TestCommand