ClusterCommand.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2022 Project CHIP Authors
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #pragma once
  19. #include <app/tests/suites/commands/interaction_model/InteractionModel.h>
  20. #include "DataModelLogger.h"
  21. #include "ModelCommand.h"
  22. class ClusterCommand : public InteractionModelCommands, public ModelCommand, public chip::app::CommandSender::Callback
  23. {
  24. public:
  25. ClusterCommand(CredentialIssuerCommands * credsIssuerConfig) :
  26. InteractionModelCommands(this), ModelCommand("command-by-id", credsIssuerConfig)
  27. {
  28. AddArgument("cluster-id", 0, UINT32_MAX, &mClusterId);
  29. AddByIdArguments();
  30. AddArguments();
  31. }
  32. ClusterCommand(chip::ClusterId clusterId, CredentialIssuerCommands * credsIssuerConfig) :
  33. InteractionModelCommands(this), ModelCommand("command-by-id", credsIssuerConfig), mClusterId(clusterId)
  34. {
  35. AddByIdArguments();
  36. AddArguments();
  37. }
  38. ~ClusterCommand() {}
  39. CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector<chip::EndpointId> endpointIds) override
  40. {
  41. return InteractionModelCommands::SendCommand(device, endpointIds.at(0), mClusterId, mCommandId, mPayload);
  42. }
  43. template <class T>
  44. CHIP_ERROR SendCommand(chip::DeviceProxy * device, chip::EndpointId endpointId, chip::ClusterId clusterId,
  45. chip::CommandId commandId, const T & value)
  46. {
  47. return InteractionModelCommands::SendCommand(device, endpointId, clusterId, commandId, value);
  48. }
  49. CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex) override
  50. {
  51. return InteractionModelCommands::SendGroupCommand(groupId, fabricIndex, mClusterId, mCommandId, mPayload);
  52. }
  53. template <class T>
  54. CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex, chip::ClusterId clusterId,
  55. chip::CommandId commandId, const T & value)
  56. {
  57. return InteractionModelCommands::SendGroupCommand(groupId, fabricIndex, clusterId, commandId, value);
  58. }
  59. /////////// CommandSender Callback Interface /////////
  60. virtual void OnResponse(chip::app::CommandSender * client, const chip::app::ConcreteCommandPath & path,
  61. const chip::app::StatusIB & status, chip::TLV::TLVReader * data) override
  62. {
  63. CHIP_ERROR error = status.ToChipError();
  64. if (CHIP_NO_ERROR != error)
  65. {
  66. LogErrorOnFailure(RemoteDataModelLogger::LogErrorAsJSON(path, status));
  67. ChipLogError(chipTool, "Response Failure: %s", chip::ErrorStr(error));
  68. mError = error;
  69. return;
  70. }
  71. if (data != nullptr)
  72. {
  73. LogErrorOnFailure(RemoteDataModelLogger::LogCommandAsJSON(path, data));
  74. error = DataModelLogger::LogCommand(path, data);
  75. if (CHIP_NO_ERROR != error)
  76. {
  77. ChipLogError(chipTool, "Response Failure: Can not decode Data");
  78. mError = error;
  79. return;
  80. }
  81. }
  82. }
  83. virtual void OnError(const chip::app::CommandSender * client, CHIP_ERROR error) override
  84. {
  85. LogErrorOnFailure(RemoteDataModelLogger::LogErrorAsJSON(error));
  86. ChipLogProgress(chipTool, "Error: %s", chip::ErrorStr(error));
  87. mError = error;
  88. }
  89. virtual void OnDone(chip::app::CommandSender * client) override
  90. {
  91. if (mCommandSender.size())
  92. {
  93. mCommandSender.front().reset();
  94. mCommandSender.erase(mCommandSender.begin());
  95. }
  96. // If the command is repeated N times, wait for all the responses to comes in
  97. // before exiting.
  98. bool shouldStop = true;
  99. if (mRepeatCount.HasValue())
  100. {
  101. mRepeatCount.SetValue(static_cast<uint16_t>(mRepeatCount.Value() - 1));
  102. shouldStop = mRepeatCount.Value() == 0;
  103. }
  104. if (shouldStop)
  105. {
  106. SetCommandExitStatus(mError);
  107. }
  108. }
  109. void Shutdown() override
  110. {
  111. mError = CHIP_NO_ERROR;
  112. ModelCommand::Shutdown();
  113. }
  114. protected:
  115. ClusterCommand(const char * commandName, CredentialIssuerCommands * credsIssuerConfig) :
  116. InteractionModelCommands(this), ModelCommand(commandName, credsIssuerConfig)
  117. {
  118. // Subclasses are responsible for calling AddArguments.
  119. }
  120. void AddByIdArguments()
  121. {
  122. AddArgument("command-id", 0, UINT32_MAX, &mCommandId);
  123. AddArgument("payload", &mPayload,
  124. "The command payload. This should be a JSON-encoded object, with string representations of field ids as keys. "
  125. " The values for the keys are represented as follows, depending on the type:\n"
  126. " * struct: a JSON-encoded object, with field ids as keys.\n"
  127. " * list: a JSON-encoded array of values.\n"
  128. " * null: A literal null.\n"
  129. " * boolean: A literal true or false.\n"
  130. " * unsigned integer: One of:\n"
  131. " a) The number directly, as decimal.\n"
  132. " b) A string starting with \"u:\" followed by decimal digits\n"
  133. " * signed integer: One of:\n"
  134. " a) The number directly, if it's negative.\n"
  135. " b) A string starting with \"s:\" followed by decimal digits\n"
  136. " * single-precision float: A string starting with \"f:\" followed by the number.\n"
  137. " * double-precision float: One of:\n"
  138. " a) The number directly, if it's not an integer.\n"
  139. " b) A string starting with \"d:\" followed by the number.\n"
  140. " * octet string: A string starting with \"hex:\" followed by the hex encoding of the bytes.\n"
  141. " * string: A string with the characters.\n"
  142. "\n"
  143. " An example payload may look like this: '{ \"0x0\": { \"0\": null, \"1\": false }, \"1\": [17, \"u:17\"], "
  144. "\"0x2\": [ -17, \"s:17\", \"s:-17\" ], \"0x3\": \"f:2\", \"0x4\": [ \"d:3\", 4.5 ], \"0x5\": \"hex:ab12\", "
  145. "\"0x6\": \"ab12\" }' and represents:\n"
  146. " Field 0: a struct with two fields, one with value null and one with value false.\n"
  147. " Field 1: A list of unsigned integers.\n"
  148. " Field 2: A list of signed integers.\n"
  149. " Field 3: A single-precision float.\n"
  150. " Field 4: A list of double-precision floats.\n"
  151. " Field 5: A 2-byte octet string.\n"
  152. " Field 6: A 4-char character string.");
  153. }
  154. void AddArguments()
  155. {
  156. AddArgument("timedInteractionTimeoutMs", 0, UINT16_MAX, &mTimedInteractionTimeoutMs,
  157. "If provided, do a timed invoke with the given timed interaction timeout. See \"7.6.10. Timed Interaction\" in "
  158. "the Matter specification.");
  159. AddArgument("busyWaitForMs", 0, UINT16_MAX, &mBusyWaitForMs,
  160. "If provided, block the main thread processing for the given time right after sending a command.");
  161. AddArgument("suppressResponse", 0, 1, &mSuppressResponse);
  162. AddArgument("repeat-count", 1, UINT16_MAX, &mRepeatCount);
  163. AddArgument("repeat-delay-ms", 0, UINT16_MAX, &mRepeatDelayInMs);
  164. ModelCommand::AddArguments();
  165. }
  166. private:
  167. chip::ClusterId mClusterId;
  168. chip::CommandId mCommandId;
  169. CHIP_ERROR mError = CHIP_NO_ERROR;
  170. CustomArgument mPayload;
  171. };