WriteAttributeCommand.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. inline constexpr const char * kWriteCommandKey = "write";
  23. inline constexpr const char * kWriteByIdCommandKey = "write-by-id";
  24. inline constexpr const char * kForceWriteCommandKey = "force-write";
  25. enum class WriteCommandType
  26. {
  27. kWrite, // regular, writable attributes
  28. kForceWrite, // forced writes, send a write command on something expected to fail
  29. };
  30. template <class T = std::vector<CustomArgument *>>
  31. class WriteAttribute : public InteractionModelWriter, public ModelCommand, public chip::app::WriteClient::Callback
  32. {
  33. public:
  34. WriteAttribute(CredentialIssuerCommands * credsIssuerConfig) :
  35. InteractionModelWriter(this), ModelCommand(kWriteByIdCommandKey, credsIssuerConfig)
  36. {
  37. AddArgumentClusterIds();
  38. AddArgumentAttributeIds();
  39. AddArgumentAttributeValues();
  40. AddArguments();
  41. }
  42. WriteAttribute(chip::ClusterId clusterId, CredentialIssuerCommands * credsIssuerConfig) :
  43. InteractionModelWriter(this), ModelCommand(kWriteByIdCommandKey, credsIssuerConfig), mClusterIds(1, clusterId)
  44. {
  45. AddArgumentAttributeIds();
  46. AddArgumentAttributeValues();
  47. AddArguments();
  48. }
  49. template <typename minType, typename maxType>
  50. WriteAttribute(chip::ClusterId clusterId, const char * attributeName, minType minValue, maxType maxValue,
  51. chip::AttributeId attributeId, WriteCommandType commandType, CredentialIssuerCommands * credsIssuerConfig) :
  52. WriteAttribute(clusterId, attributeId, commandType, credsIssuerConfig)
  53. {
  54. AddArgumentAttributeName(attributeName);
  55. AddArgumentAttributeValues(static_cast<int64_t>(minValue), static_cast<uint64_t>(maxValue));
  56. AddArguments();
  57. }
  58. WriteAttribute(chip::ClusterId clusterId, const char * attributeName, float minValue, float maxValue,
  59. chip::AttributeId attributeId, WriteCommandType commandType, CredentialIssuerCommands * credsIssuerConfig) :
  60. WriteAttribute(clusterId, attributeId, commandType, credsIssuerConfig)
  61. {
  62. AddArgumentAttributeName(attributeName);
  63. AddArgumentAttributeValues(minValue, maxValue);
  64. AddArguments();
  65. }
  66. WriteAttribute(chip::ClusterId clusterId, const char * attributeName, double minValue, double maxValue,
  67. chip::AttributeId attributeId, WriteCommandType commandType, CredentialIssuerCommands * credsIssuerConfig) :
  68. WriteAttribute(clusterId, attributeId, commandType, credsIssuerConfig)
  69. {
  70. AddArgumentAttributeName(attributeName);
  71. AddArgumentAttributeValues(minValue, maxValue);
  72. AddArguments();
  73. }
  74. WriteAttribute(chip::ClusterId clusterId, const char * attributeName, chip::AttributeId attributeId,
  75. WriteCommandType commandType, CredentialIssuerCommands * credsIssuerConfig) :
  76. WriteAttribute(clusterId, attributeId, commandType, credsIssuerConfig)
  77. {
  78. AddArgumentAttributeName(attributeName);
  79. AddArgumentAttributeValues();
  80. AddArguments();
  81. }
  82. WriteAttribute(chip::ClusterId clusterId, const char * attributeName, chip::AttributeId attributeId,
  83. TypedComplexArgument<T> & attributeParser, WriteCommandType commandType,
  84. CredentialIssuerCommands * credsIssuerConfig) :
  85. WriteAttribute(clusterId, attributeId, commandType, credsIssuerConfig)
  86. {
  87. AddArgumentAttributeName(attributeName);
  88. AddArgumentAttributeValues(attributeParser);
  89. AddArguments();
  90. }
  91. ~WriteAttribute() {}
  92. CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector<chip::EndpointId> endpointIds) override
  93. {
  94. return WriteAttribute::SendCommand(device, endpointIds, mClusterIds, mAttributeIds, mAttributeValues);
  95. }
  96. CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex) override
  97. {
  98. return WriteAttribute::SendGroupCommand(groupId, fabricIndex, mClusterIds, mAttributeIds, mAttributeValues);
  99. }
  100. /////////// WriteClient Callback Interface /////////
  101. void OnResponse(const chip::app::WriteClient * client, const chip::app::ConcreteDataAttributePath & path,
  102. chip::app::StatusIB status) override
  103. {
  104. CHIP_ERROR error = status.ToChipError();
  105. if (CHIP_NO_ERROR != error)
  106. {
  107. LogErrorOnFailure(RemoteDataModelLogger::LogErrorAsJSON(path, status));
  108. ChipLogError(chipTool, "Response Failure: %s", chip::ErrorStr(error));
  109. mError = error;
  110. }
  111. }
  112. void OnError(const chip::app::WriteClient * client, CHIP_ERROR error) override
  113. {
  114. LogErrorOnFailure(RemoteDataModelLogger::LogErrorAsJSON(error));
  115. ChipLogProgress(chipTool, "Error: %s", chip::ErrorStr(error));
  116. mError = error;
  117. }
  118. void OnDone(chip::app::WriteClient * client) override
  119. {
  120. InteractionModelWriter::Shutdown();
  121. SetCommandExitStatus(mError);
  122. }
  123. CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector<chip::EndpointId> endpointIds,
  124. std::vector<chip::ClusterId> clusterIds, std::vector<chip::AttributeId> attributeIds, const T & values)
  125. {
  126. return InteractionModelWriter::WriteAttribute(device, endpointIds, clusterIds, attributeIds, values);
  127. }
  128. CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex, std::vector<chip::ClusterId> clusterIds,
  129. std::vector<chip::AttributeId> attributeIds, const T & value)
  130. {
  131. ChipLogDetail(chipTool, "Sending Write Attribute to Group %u, on Fabric %x, for cluster %u with attributeId %u", groupId,
  132. fabricIndex, clusterIds.at(0), attributeIds.at(0));
  133. chip::Optional<chip::DataVersion> dataVersion = chip::NullOptional;
  134. if (mDataVersions.HasValue())
  135. {
  136. dataVersion.SetValue(mDataVersions.Value().at(0));
  137. }
  138. return InteractionModelWriter::WriteGroupAttribute(groupId, fabricIndex, clusterIds.at(0), attributeIds.at(0), value,
  139. dataVersion);
  140. }
  141. void Shutdown() override
  142. {
  143. mError = CHIP_NO_ERROR;
  144. ModelCommand::Shutdown();
  145. }
  146. protected:
  147. WriteAttribute(const char * attributeName, CredentialIssuerCommands * credsIssuerConfig) :
  148. InteractionModelWriter(this), ModelCommand(kWriteCommandKey, credsIssuerConfig)
  149. {
  150. // Subclasses are responsible for calling AddArguments.
  151. }
  152. void AddArgumentClusterIds()
  153. {
  154. AddArgument("cluster-ids", 0, UINT32_MAX, &mClusterIds,
  155. "Comma-separated list of cluster ids to write to (e.g. \"6\" or \"6,0x201\").");
  156. }
  157. void AddArgumentAttributeIds()
  158. {
  159. AddArgument("attribute-ids", 0, UINT32_MAX, &mAttributeIds,
  160. "Comma-separated list of attribute ids to write (e.g. \"16385\" or \"16385,0x4002\").");
  161. }
  162. void AddArgumentAttributeName(const char * attributeName)
  163. {
  164. AddArgument("attribute-name", attributeName, "The attribute name to write.");
  165. }
  166. template <typename U = T, std::enable_if_t<std::is_same<U, std::vector<CustomArgument *>>::value, int> = 0>
  167. static const char * GetAttributeValuesDescription()
  168. {
  169. return "Comma-separated list of attribute values to write. Each value is represented as follows, depending on the type:\n"
  170. " * struct: a JSON-encoded object, with field ids as keys.\n"
  171. " * list: a JSON-encoded array of values.\n"
  172. " * null: A literal null.\n"
  173. " * boolean: A literal true or false.\n"
  174. " * unsigned integer: One of:\n"
  175. " a) The number directly, as decimal.\n"
  176. " b) The number directly, as 0x followed by hex digits. (Only for the toplevel value, not inside structs or "
  177. "lists.)\n"
  178. " c) A string starting with \"u:\" followed by decimal digits\n"
  179. " * signed integer: One of:\n"
  180. " a) The number directly, if it's negative.\n"
  181. " c) A string starting with \"s:\" followed by decimal digits\n"
  182. " * single-precision float: A string starting with \"f:\" followed by the number.\n"
  183. " * double-precision float: One of:\n"
  184. " a) The number directly, if it's not an integer.\n"
  185. " b) A string starting with \"d:\" followed by the number.\n"
  186. " * octet string: A string starting with \"hex:\" followed by the hex encoding of the bytes.\n"
  187. " * string: A string with the characters.";
  188. }
  189. static const char * GetTypedAttributeValuesDescription() { return "Comma-separated list of attribute values to write."; }
  190. template <typename U = T, std::enable_if_t<!std::is_same<U, std::vector<CustomArgument *>>::value, int> = 0>
  191. static const char * GetAttributeValuesDescription()
  192. {
  193. return GetTypedAttributeValuesDescription();
  194. }
  195. template <typename minType, typename maxType>
  196. void AddArgumentAttributeValues(minType minValue, maxType maxValue)
  197. {
  198. AddArgument("attribute-values", minValue, maxValue, &mAttributeValues, GetTypedAttributeValuesDescription());
  199. }
  200. void AddArgumentAttributeValues() { AddArgument("attribute-values", &mAttributeValues, GetAttributeValuesDescription()); }
  201. void AddArgumentAttributeValues(TypedComplexArgument<T> & attributeParser)
  202. {
  203. attributeParser.SetArgument(&mAttributeValues);
  204. AddArgument("attribute-values", &attributeParser, GetTypedAttributeValuesDescription());
  205. }
  206. void AddArguments()
  207. {
  208. AddArgument("timedInteractionTimeoutMs", 0, UINT16_MAX, &mTimedInteractionTimeoutMs,
  209. "If provided, do a timed write with the given timed interaction timeout. See \"7.6.10. Timed Interaction\" in "
  210. "the Matter specification.");
  211. AddArgument("busyWaitForMs", 0, UINT16_MAX, &mBusyWaitForMs,
  212. "If provided, block the main thread processing for the given time right after sending a command.");
  213. AddArgument("data-version", 0, UINT32_MAX, &mDataVersions,
  214. "Comma-separated list of data versions for the clusters being written.");
  215. AddArgument("suppressResponse", 0, 1, &mSuppressResponse);
  216. AddArgument("repeat-count", 1, UINT16_MAX, &mRepeatCount);
  217. AddArgument("repeat-delay-ms", 0, UINT16_MAX, &mRepeatDelayInMs);
  218. ModelCommand::AddArguments();
  219. }
  220. private:
  221. // This constructor is private as it is not intended to be used from outside the class.
  222. WriteAttribute(chip::ClusterId clusterId, chip::AttributeId attributeId, WriteCommandType commandType,
  223. CredentialIssuerCommands * credsIssuerConfig) :
  224. InteractionModelWriter(this),
  225. ModelCommand(commandType == WriteCommandType::kWrite ? kWriteCommandKey : kForceWriteCommandKey, credsIssuerConfig),
  226. mClusterIds(1, clusterId), mAttributeIds(1, attributeId)
  227. {}
  228. std::vector<chip::ClusterId> mClusterIds;
  229. std::vector<chip::AttributeId> mAttributeIds;
  230. CHIP_ERROR mError = CHIP_NO_ERROR;
  231. T mAttributeValues;
  232. };
  233. template <class T>
  234. class WriteAttributeAsComplex : public WriteAttribute<T>
  235. {
  236. public:
  237. WriteAttributeAsComplex(chip::ClusterId clusterId, const char * attributeName, chip::AttributeId attributeId,
  238. WriteCommandType commandType, CredentialIssuerCommands * credsIssuerConfig) :
  239. WriteAttribute<T>(clusterId, attributeName, attributeId, mAttributeParser, commandType, credsIssuerConfig)
  240. {}
  241. private:
  242. TypedComplexArgument<T> mAttributeParser;
  243. };