TestCommand.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #pragma once
  19. #include <atomic>
  20. #include <app/ConcreteAttributePath.h>
  21. #include <app/ConcreteCommandPath.h>
  22. #include <app/tests/suites/commands/delay/DelayCommands.h>
  23. #include <app/tests/suites/commands/discovery/DiscoveryCommands.h>
  24. #include <app/tests/suites/commands/log/LogCommands.h>
  25. #include <app/tests/suites/include/ConstraintsChecker.h>
  26. #include <app/tests/suites/include/PICSChecker.h>
  27. #include <app/tests/suites/include/TestRunner.h>
  28. #include <app/tests/suites/include/ValueChecker.h>
  29. #include <app-common/zap-generated/ids/Attributes.h>
  30. #include <app-common/zap-generated/ids/Clusters.h>
  31. #include <app-common/zap-generated/ids/Commands.h>
  32. inline constexpr const char kIdentityAlpha[] = "";
  33. inline constexpr const char kIdentityBeta[] = "";
  34. inline constexpr const char kIdentityGamma[] = "";
  35. class TestCommand : public TestRunner,
  36. public PICSChecker,
  37. public LogCommands,
  38. public DiscoveryCommands,
  39. public DelayCommands,
  40. public ValueChecker,
  41. public ConstraintsChecker
  42. {
  43. public:
  44. TestCommand(const char * commandName, uint16_t testsCount) :
  45. TestRunner(commandName, testsCount), mCommandPath(0, 0, 0), mAttributePath(0, 0, 0)
  46. {}
  47. virtual ~TestCommand() {}
  48. void SetCommandExitStatus(CHIP_ERROR status)
  49. {
  50. chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
  51. mExitCode = (CHIP_NO_ERROR == status ? EXIT_SUCCESS : EXIT_FAILURE);
  52. }
  53. int GetCommandExitCode() { return mExitCode; }
  54. template <typename T>
  55. size_t AddArgument(const char * name, chip::Optional<T> * value)
  56. {
  57. return 0;
  58. }
  59. template <typename T>
  60. size_t AddArgument(const char * name, int64_t min, uint64_t max, chip::Optional<T> * value)
  61. {
  62. return 0;
  63. }
  64. CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) override
  65. {
  66. if (CHIP_NO_ERROR == err)
  67. {
  68. NextTest();
  69. }
  70. else
  71. {
  72. Exit(chip::ErrorStr(err), err);
  73. }
  74. return CHIP_NO_ERROR;
  75. }
  76. void Exit(std::string message, CHIP_ERROR err) override
  77. {
  78. LogEnd(message, err);
  79. if (CHIP_NO_ERROR == err)
  80. {
  81. chip::DeviceLayer::PlatformMgr().ScheduleWork(AsyncExit, reinterpret_cast<intptr_t>(this));
  82. }
  83. else
  84. {
  85. SetCommandExitStatus(err);
  86. }
  87. }
  88. static void AsyncExit(intptr_t context)
  89. {
  90. TestCommand * command = reinterpret_cast<TestCommand *>(context);
  91. command->SetCommandExitStatus(CHIP_NO_ERROR);
  92. }
  93. static void ScheduleNextTest(intptr_t context)
  94. {
  95. TestCommand * command = reinterpret_cast<TestCommand *>(context);
  96. command->isRunning = true;
  97. command->NextTest();
  98. }
  99. static void OnPlatformEvent(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
  100. {
  101. switch (event->Type)
  102. {
  103. case chip::DeviceLayer::DeviceEventType::kCommissioningComplete:
  104. ChipLogProgress(chipTool, "Commissioning complete");
  105. chip::DeviceLayer::PlatformMgr().ScheduleWork(ScheduleNextTest, arg);
  106. chip::DeviceLayer::PlatformMgr().RemoveEventHandler(OnPlatformEvent, arg);
  107. break;
  108. }
  109. }
  110. void CheckCommandPath(const chip::app::ConcreteCommandPath & commandPath)
  111. {
  112. if (commandPath == mCommandPath)
  113. {
  114. chip::DeviceLayer::PlatformMgr().ScheduleWork(ScheduleNextTest, reinterpret_cast<intptr_t>(this));
  115. return;
  116. }
  117. ChipLogError(chipTool, "CommandPath does not match");
  118. SetCommandExitStatus(CHIP_ERROR_INTERNAL);
  119. }
  120. void CheckAttributePath(const chip::app::ConcreteAttributePath & attributePath)
  121. {
  122. if (attributePath == mAttributePath)
  123. {
  124. chip::DeviceLayer::PlatformMgr().ScheduleWork(ScheduleNextTest, reinterpret_cast<intptr_t>(this));
  125. return;
  126. }
  127. ChipLogError(chipTool, "AttributePath does not match");
  128. return SetCommandExitStatus(CHIP_ERROR_INTERNAL);
  129. }
  130. void ClearAttributeAndCommandPaths()
  131. {
  132. mCommandPath = chip::app::ConcreteCommandPath(0, 0, 0);
  133. mAttributePath = chip::app::ConcreteAttributePath(0, 0, 0);
  134. }
  135. std::atomic_bool isRunning{ true };
  136. CHIP_ERROR WaitAttribute(chip::EndpointId endpointId, chip::ClusterId clusterId, chip::AttributeId attributeId)
  137. {
  138. ClearAttributeAndCommandPaths();
  139. ChipLogError(chipTool, "[Endpoint: 0x%08x Cluster: %d, Attribute: %d]", endpointId, clusterId, attributeId);
  140. mAttributePath = chip::app::ConcreteAttributePath(endpointId, clusterId, attributeId);
  141. return CHIP_NO_ERROR;
  142. }
  143. CHIP_ERROR WaitCommand(chip::EndpointId endpointId, chip::ClusterId clusterId, chip::CommandId commandId)
  144. {
  145. ClearAttributeAndCommandPaths();
  146. ChipLogError(chipTool, "[Endpoint: 0x%08x Cluster: %d, Command: %d]", endpointId, clusterId, commandId);
  147. mCommandPath = chip::app::ConcreteCommandPath(endpointId, clusterId, commandId);
  148. return CHIP_NO_ERROR;
  149. }
  150. protected:
  151. chip::app::ConcreteCommandPath mCommandPath;
  152. chip::app::ConcreteAttributePath mAttributePath;
  153. chip::Optional<chip::NodeId> mCommissionerNodeId;
  154. chip::Optional<chip::EndpointId> mEndpointId;
  155. int mExitCode = EXIT_SUCCESS;
  156. void SetIdentity(const char * name){};
  157. /////////// DelayCommands Interface /////////
  158. void OnWaitForMs() override { NextTest(); }
  159. CHIP_ERROR WaitForCommissioning(const char * identity,
  160. const chip::app::Clusters::DelayCommands::Commands::WaitForCommissioning::Type & value) override
  161. {
  162. isRunning = false;
  163. return chip::DeviceLayer::PlatformMgr().AddEventHandler(OnPlatformEvent, reinterpret_cast<intptr_t>(this));
  164. }
  165. };