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();
139 ~{{asCamelCased clusterName false}}{{asCamelCased name false}}()
141 delete onSuccessCallback;
142 delete onFailureCallback;
145 CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
147 ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command ({{asHex code 2}}) on endpoint %" PRIu16, endpointId);
149 chip::Controller::{{asCamelCased parent.name false}}Cluster cluster;
150 cluster.Associate(device, endpointId);
151 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}});
155 {{#if (hasSpecificResponse name)}}
156 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);
158 chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
160 chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
161 {{#chip_server_cluster_command_arguments}}
162 {{#if (isString type)}}
163 char * m{{asCamelCased label false}};
165 {{chipType}} m{{asCamelCased label false}};
167 {{/chip_server_cluster_command_arguments}}
170 {{/chip_server_cluster_commands}}
173 * Discover Attributes
175 class Discover{{asCamelCased name false}}Attributes: public ModelCommand
178 Discover{{asCamelCased name false}}Attributes(): ModelCommand("discover")
180 ModelCommand::AddArguments();
183 ~Discover{{asCamelCased name false}}Attributes()
185 delete onSuccessCallback;
186 delete onFailureCallback;
189 CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
191 ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x0C) on endpoint %" PRIu16, endpointId);
193 chip::Controller::{{asCamelCased name false}}Cluster cluster;
194 cluster.Associate(device, endpointId);
195 return cluster.DiscoverAttributes(onSuccessCallback->Cancel(), onFailureCallback->Cancel());
199 chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
200 chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
203 {{#chip_server_cluster_attributes}}
205 * Attribute {{asCamelCased name false}}
207 class Read{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
210 Read{{asCamelCased parent.name false}}{{asCamelCased name false}}(): ModelCommand("read")
212 AddArgument("attr-name", "{{asDelimitedCommand (asCamelCased name)}}");
213 ModelCommand::AddArguments();
216 ~Read{{asCamelCased parent.name false}}{{asCamelCased name false}}()
218 delete onSuccessCallback;
219 delete onFailureCallback;
222 CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
224 ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x00) on endpoint %" PRIu16, endpointId);
226 chip::Controller::{{asCamelCased parent.name false}}Cluster cluster;
227 cluster.Associate(device, endpointId);
228 return cluster.ReadAttribute{{asCamelCased name false}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel());
232 chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback> * onSuccessCallback = new chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback>(On{{asCallbackAttributeType atomicTypeId}}AttributeResponse, this);
233 chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
236 {{#if (isWritableAttribute)}}
237 class Write{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
240 Write{{asCamelCased parent.name false}}{{asCamelCased name false}}(): ModelCommand("write")
242 AddArgument("attr-name", "{{asDelimitedCommand (asCamelCased name)}}");
243 {{#if (isString type)}}
244 AddArgument("attr-value", &mValue);
246 AddArgument("attr-value", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, &mValue);
248 ModelCommand::AddArguments();
251 ~Write{{asCamelCased parent.name false}}{{asCamelCased name false}}()
253 delete onSuccessCallback;
254 delete onFailureCallback;
257 CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
259 ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x01) on endpoint %" PRIu16, endpointId);
261 chip::Controller::{{asCamelCased parent.name false}}Cluster cluster;
262 cluster.Associate(device, endpointId);
263 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}});
267 chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
268 chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
269 {{#if (isString type)}}
277 {{#if (isReportableAttribute)}}
278 class Report{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
281 Report{{asCamelCased parent.name false}}{{asCamelCased name false}}(): ModelCommand("report")
283 AddArgument("attr-name", "{{asDelimitedCommand (asCamelCased name)}}");
284 AddArgument("min-interval", 0, UINT16_MAX, &mMinInterval);
285 AddArgument("max-interval", 0, UINT16_MAX, &mMaxInterval);
286 {{#unless (isDiscreteType)}}
287 AddArgument("change", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, &mChange);
289 ModelCommand::AddArguments();
292 ~Report{{asCamelCased parent.name false}}{{asCamelCased name false}}()
294 delete onSuccessCallback;
295 delete onFailureCallback;
296 delete onReportCallback;
299 CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
301 ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x06) on endpoint %" PRIu16, endpointId);
303 chip::Controller::{{asCamelCased parent.name false}}Cluster cluster;
304 cluster.Associate(device, endpointId);
306 CHIP_ERROR err = cluster.ReportAttribute{{asCamelCased name false}}(onReportCallback->Cancel());
307 if (err != CHIP_NO_ERROR)
312 return cluster.ConfigureAttribute{{asCamelCased name false}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mMinInterval, mMaxInterval{{#unless (isDiscreteType)}}, mChange{{/unless}});
316 chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
317 chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
318 chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback> * onReportCallback = new chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback>(On{{asCallbackAttributeType atomicTypeId}}AttributeResponse, this);
319 uint16_t mMinInterval;
320 uint16_t mMaxInterval;
321 {{#unless (isDiscreteType)}}
322 {{chipType}} mChange;
327 {{/chip_server_cluster_attributes}}
330 /*----------------------------------------------------------------------------*\
331 | Register all Clusters commands |
332 \*----------------------------------------------------------------------------*/
334 void registerCluster{{asCamelCased name false}}(Commands & commands)
336 const char * clusterName = "{{asCamelCased name false}}";
338 commands_list clusterCommands = {
339 {{#chip_server_cluster_commands}}
340 make_unique<{{asCamelCased clusterName false}}{{asCamelCased name false}}>(),
341 {{/chip_server_cluster_commands}}
342 make_unique<Discover{{asCamelCased name false}}Attributes>(),
343 {{#chip_server_cluster_attributes}}
344 make_unique<Read{{asCamelCased parent.name false}}{{asCamelCased name false}}>(),
345 {{#if (isWritableAttribute)}}
346 make_unique<Write{{asCamelCased parent.name false}}{{asCamelCased name false}}>(),
348 {{#if (isReportableAttribute)}}
349 make_unique<Report{{asCamelCased parent.name false}}{{asCamelCased name false}}>(),
351 {{/chip_server_cluster_attributes}}
354 commands.Register(clusterName, clusterCommands);
358 void registerClusters(Commands & commands)
361 registerCluster{{asCamelCased name false}}(commands);