7 #include "ModelCommand.h"
8 #include "gen/CHIPClientCallbacks.h"
9 #include <controller/CHIPClusters.h>
10 #include <lib/core/CHIPSafeCasts.h>
12 static void OnDefaultSuccessResponse(void * context)
14 ChipLogProgress(chipTool, "Default Success Response");
16 ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
17 command->SetCommandExitStatus(true);
20 static void OnDefaultFailureResponse(void * context, uint8_t status)
22 ChipLogProgress(chipTool, "Default Failure Response: 0x%02x", status);
24 ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
25 command->SetCommandExitStatus(false);
28 static void OnBooleanAttributeResponse(void * context, bool value)
30 ChipLogProgress(chipTool, "Boolean attribute Response: %d", value);
32 ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
33 command->SetCommandExitStatus(true);
36 static void OnInt8uAttributeResponse(void * context, uint8_t value)
38 ChipLogProgress(chipTool, "Int8u attribute Response: %" PRIu8, value);
40 ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
41 command->SetCommandExitStatus(true);
44 static void OnInt16uAttributeResponse(void * context, uint16_t value)
46 ChipLogProgress(chipTool, "Int16u attribute Response: %" PRIu16, value);
48 ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
49 command->SetCommandExitStatus(true);
52 static void OnInt32uAttributeResponse(void * context, uint32_t value)
54 ChipLogProgress(chipTool, "Int32u attribute Response: %" PRIu32, value);
56 ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
57 command->SetCommandExitStatus(true);
60 static void OnInt64uAttributeResponse(void * context, uint64_t value)
62 ChipLogProgress(chipTool, "Int64u attribute Response: %" PRIu64, value);
64 ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
65 command->SetCommandExitStatus(true);
68 static void OnInt16sAttributeResponse(void * context, int16_t value)
70 ChipLogProgress(chipTool, "Int16s attribute Response: %" PRId16, value);
72 ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
73 command->SetCommandExitStatus(true);
76 static void OnStringAttributeResponse(void * context, const chip::ByteSpan value)
78 char * str = (char *)malloc(value.size() * sizeof(char));
79 memmove(str, value.data(), value.size());
80 str[value.size()] = '\0';
83 ChipLogProgress(chipTool, "String attribute Response: %s (%" PRIu16 ")", str, strlen(str));
85 ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
86 command->SetCommandExitStatus(true);
89 {{#all_user_clusters}}
90 {{#if (isClient side) }}
91 {{#if (user_cluster_has_enabled_command name side)}}
92 {{#all_user_cluster_commands}}
93 {{#if (isStrEqual clusterName parent.name)}}
94 {{#if (isCommandAvailable parent.side incoming outgoing commandSource name)}}
95 {{#if (isStrEndsWith name "Response")}}
96 static void On{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}(void * context{{#zcl_command_arguments}}{{#unless (isStrEqual label "status")}}, {{asUnderlyingZclType type}} {{asSymbol label}}{{/unless}}{{/zcl_command_arguments}})
98 ChipLogProgress(chipTool, "{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}");
100 ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
101 command->SetCommandExitStatus(true);
107 {{/all_user_cluster_commands}}
110 {{/all_user_clusters}}
112 {{> clusters_header}}
115 constexpr chip::ClusterId k{{asCamelCased name false}}ClusterId = {{asHex code 4}};
121 {{#chip_server_cluster_commands}}
123 * Command {{asCamelCased name false}}
125 class {{asCamelCased clusterName false}}{{asCamelCased name false}}: public ModelCommand
128 {{asCamelCased clusterName false}}{{asCamelCased name false}}(): ModelCommand("{{asDelimitedCommand name}}")
130 {{#chip_server_cluster_command_arguments}}
131 {{#if (isString type)}}
132 AddArgument("{{asCamelCased label}}", &m{{asCamelCased label false}});
134 AddArgument("{{asCamelCased label}}", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, &m{{asCamelCased label false}});
136 {{/chip_server_cluster_command_arguments}}
137 ModelCommand::AddArguments();
140 CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
142 ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command ({{asHex code 2}}) on endpoint %" PRIu16, endpointId);
144 chip::Controller::{{asCamelCased parent.name false}}Cluster cluster;
145 cluster.Associate(device, endpointId);
146 return cluster.{{asCamelCased name false}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(){{#chip_server_cluster_command_arguments}}, {{#if (isString type)}} chip::ByteSpan(chip::Uint8::from_char(m{{asCamelCased label false}}), strlen(m{{asCamelCased label false}})){{else}}m{{asCamelCased label false}}{{/if}}{{/chip_server_cluster_command_arguments}});
150 {{#if (hasSpecificResponse name)}}
151 chip::Callback::Callback<{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}ResponseCallback> * onSuccessCallback = new chip::Callback::Callback<{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}ResponseCallback>(On{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}Response, this);
153 chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
155 chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
156 {{#chip_server_cluster_command_arguments}}
157 {{#if (isString type)}}
158 char * m{{asCamelCased label false}};
160 {{chipType}} m{{asCamelCased label false}};
162 {{/chip_server_cluster_command_arguments}}
165 {{/chip_server_cluster_commands}}
168 * Discover Attributes
170 class Discover{{asCamelCased name false}}Attributes: public ModelCommand
173 Discover{{asCamelCased name false}}Attributes(): ModelCommand("discover")
175 ModelCommand::AddArguments();
178 CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
180 ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x0C) on endpoint %" PRIu16, endpointId);
182 chip::Controller::{{asCamelCased name false}}Cluster cluster;
183 cluster.Associate(device, endpointId);
184 return cluster.DiscoverAttributes(onSuccessCallback->Cancel(), onFailureCallback->Cancel());
188 chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
189 chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
192 {{#chip_server_cluster_attributes}}
194 * Attribute {{asCamelCased name false}}
196 class Read{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
199 Read{{asCamelCased parent.name false}}{{asCamelCased name false}}(): ModelCommand("read")
201 AddArgument("attr-name", "{{asDelimitedCommand (asCamelCased name)}}");
202 ModelCommand::AddArguments();
205 CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
207 ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x00) on endpoint %" PRIu16, endpointId);
209 chip::Controller::{{asCamelCased parent.name false}}Cluster cluster;
210 cluster.Associate(device, endpointId);
211 return cluster.ReadAttribute{{asCamelCased name false}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel());
215 chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback> * onSuccessCallback = new chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback>(On{{asCallbackAttributeType atomicTypeId}}AttributeResponse, this);
216 chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
219 {{#if (isWritableAttribute)}}
220 class Write{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
223 Write{{asCamelCased parent.name false}}{{asCamelCased name false}}(): ModelCommand("write")
225 AddArgument("attr-name", "{{asDelimitedCommand (asCamelCased name)}}");
226 {{#if (isString type)}}
227 AddArgument("attr-value", &mValue);
229 AddArgument("attr-value", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, &mValue);
231 ModelCommand::AddArguments();
234 CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
236 ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x01) on endpoint %" PRIu16, endpointId);
238 chip::Controller::{{asCamelCased parent.name false}}Cluster cluster;
239 cluster.Associate(device, endpointId);
240 return cluster.WriteAttribute{{asCamelCased name false}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), {{#if (isString type)}} chip::ByteSpan(chip::Uint8::from_char(mValue), strlen(mValue)){{else}}mValue{{/if}});
244 chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
245 chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
246 {{#if (isString type)}}
254 {{#if (isReportableAttribute)}}
255 class Report{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
258 Report{{asCamelCased parent.name false}}{{asCamelCased name false}}(): ModelCommand("report")
260 AddArgument("attr-name", "{{asDelimitedCommand (asCamelCased name)}}");
261 AddArgument("min-interval", 0, UINT16_MAX, &mMinInterval);
262 AddArgument("max-interval", 0, UINT16_MAX, &mMaxInterval);
263 {{#unless (isDiscreteType)}}
264 AddArgument("change", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, &mChange);
266 ModelCommand::AddArguments();
269 CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
271 ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x06) on endpoint %" PRIu16, endpointId);
273 chip::Controller::{{asCamelCased parent.name false}}Cluster cluster;
274 cluster.Associate(device, endpointId);
276 CHIP_ERROR err = cluster.ReportAttribute{{asCamelCased name false}}(onReportCallback->Cancel());
277 if (err != CHIP_NO_ERROR)
282 return cluster.ConfigureAttribute{{asCamelCased name false}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mMinInterval, mMaxInterval{{#unless (isDiscreteType)}}, mChange{{/unless}});
286 chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
287 chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
288 chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback> * onReportCallback = new chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback>(On{{asCallbackAttributeType atomicTypeId}}AttributeResponse, this);
289 uint16_t mMinInterval;
290 uint16_t mMaxInterval;
291 {{#unless (isDiscreteType)}}
292 {{chipType}} mChange;
297 {{/chip_server_cluster_attributes}}
300 /*----------------------------------------------------------------------------*\
301 | Register all Clusters commands |
302 \*----------------------------------------------------------------------------*/
304 void registerCluster{{asCamelCased name false}}(Commands & commands)
306 const char * clusterName = "{{asCamelCased name false}}";
308 commands_list clusterCommands = {
309 {{#chip_server_cluster_commands}}
310 make_unique<{{asCamelCased clusterName false}}{{asCamelCased name false}}>(),
311 {{/chip_server_cluster_commands}}
312 make_unique<Discover{{asCamelCased name false}}Attributes>(),
313 {{#chip_server_cluster_attributes}}
314 make_unique<Read{{asCamelCased parent.name false}}{{asCamelCased name false}}>(),
315 {{#if (isWritableAttribute)}}
316 make_unique<Write{{asCamelCased parent.name false}}{{asCamelCased name false}}>(),
318 {{#if (isReportableAttribute)}}
319 make_unique<Report{{asCamelCased parent.name false}}{{asCamelCased name false}}>(),
321 {{/chip_server_cluster_attributes}}
324 commands.Register(clusterName, clusterCommands);
328 void registerClusters(Commands & commands)
331 registerCluster{{asCamelCased name false}}(commands);