TestCommand.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/pics/PICSBooleanExpressionParser.h>
  23. #include <app/tests/suites/pics/PICSBooleanReader.h>
  24. #include <app-common/zap-generated/ids/Attributes.h>
  25. #include <app-common/zap-generated/ids/Clusters.h>
  26. #include <app-common/zap-generated/ids/Commands.h>
  27. class TestCommand
  28. {
  29. public:
  30. TestCommand(const char * commandName) : mCommandPath(0, 0, 0), mAttributePath(0, 0, 0) {}
  31. virtual ~TestCommand() {}
  32. virtual void NextTest() = 0;
  33. void Wait() {}
  34. void SetCommandExitStatus(CHIP_ERROR status)
  35. {
  36. chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
  37. exit(CHIP_NO_ERROR == status ? EXIT_SUCCESS : EXIT_FAILURE);
  38. }
  39. CHIP_ERROR Log(const char * message)
  40. {
  41. ChipLogProgress(chipTool, "%s", message);
  42. NextTest();
  43. return CHIP_NO_ERROR;
  44. }
  45. CHIP_ERROR UserPrompt(const char * message)
  46. {
  47. ChipLogProgress(chipTool, "USER_PROMPT: %s", message);
  48. NextTest();
  49. return CHIP_NO_ERROR;
  50. }
  51. CHIP_ERROR WaitForCommissioning()
  52. {
  53. isRunning = false;
  54. return chip::DeviceLayer::PlatformMgr().AddEventHandler(OnPlatformEvent, reinterpret_cast<intptr_t>(this));
  55. }
  56. static void OnPlatformEvent(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
  57. {
  58. switch (event->Type)
  59. {
  60. case chip::DeviceLayer::DeviceEventType::kCommissioningComplete:
  61. ChipLogProgress(chipTool, "Commissioning complete");
  62. chip::DeviceLayer::PlatformMgr().RemoveEventHandler(OnPlatformEvent, arg);
  63. TestCommand * command = reinterpret_cast<TestCommand *>(arg);
  64. command->isRunning = true;
  65. command->NextTest();
  66. break;
  67. }
  68. }
  69. void CheckCommandPath(const chip::app::ConcreteCommandPath & commandPath)
  70. {
  71. if (commandPath == mCommandPath)
  72. {
  73. NextTest();
  74. return;
  75. }
  76. ChipLogError(chipTool, "CommandPath does not match");
  77. SetCommandExitStatus(CHIP_ERROR_INTERNAL);
  78. }
  79. void CheckAttributePath(const chip::app::ConcreteAttributePath & attributePath)
  80. {
  81. if (attributePath == mAttributePath)
  82. {
  83. NextTest();
  84. return;
  85. }
  86. ChipLogError(chipTool, "AttributePath does not match");
  87. return SetCommandExitStatus(CHIP_ERROR_INTERNAL);
  88. }
  89. void ClearAttributeAndCommandPaths()
  90. {
  91. mCommandPath = chip::app::ConcreteCommandPath(0, 0, 0);
  92. mAttributePath = chip::app::ConcreteAttributePath(0, 0, 0);
  93. }
  94. bool ShouldSkip(const char * expression)
  95. {
  96. // If there is no PICS configuration file, considers that nothing should be skipped.
  97. if (!PICS.HasValue())
  98. {
  99. return false;
  100. }
  101. std::map<std::string, bool> pics(PICS.Value());
  102. bool shouldSkip = !PICSBooleanExpressionParser::Eval(expression, pics);
  103. if (shouldSkip)
  104. {
  105. ChipLogProgress(chipTool, " **** Skipping: %s == false\n", expression);
  106. NextTest();
  107. }
  108. return shouldSkip;
  109. }
  110. chip::Optional<std::map<std::string, bool>> PICS;
  111. std::atomic_bool isRunning{ true };
  112. protected:
  113. chip::app::ConcreteCommandPath mCommandPath;
  114. chip::app::ConcreteAttributePath mAttributePath;
  115. chip::Optional<chip::EndpointId> mEndpointId;
  116. };