Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / chip-tool / templates / commands.zapt
1 {{> header}}
2
3 #pragma once
4
5 #include <cstdint>
6
7 #include "ModelCommand.h"
8 #include "gen/CHIPClientCallbacks.h"
9 #include <controller/CHIPClusters.h>
10 #include <lib/core/CHIPSafeCasts.h>
11
12 static void OnDefaultSuccessResponse(void * context)
13 {
14     ChipLogProgress(chipTool, "Default Success Response");
15
16     ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
17     command->SetCommandExitStatus(true);
18 }
19
20 static void OnDefaultFailureResponse(void * context, uint8_t status)
21 {
22     ChipLogProgress(chipTool, "Default Failure Response: 0x%02x", status);
23
24     ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
25     command->SetCommandExitStatus(false);
26 }
27
28 static void OnBooleanAttributeResponse(void * context, bool value)
29 {
30     ChipLogProgress(chipTool, "Boolean attribute Response: %d", value);
31
32     ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
33     command->SetCommandExitStatus(true);
34 }
35
36 static void OnInt8uAttributeResponse(void * context, uint8_t value)
37 {
38     ChipLogProgress(chipTool, "Int8u attribute Response: %" PRIu8, value);
39
40     ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
41     command->SetCommandExitStatus(true);
42 }
43
44 static void OnInt16uAttributeResponse(void * context, uint16_t value)
45 {
46     ChipLogProgress(chipTool, "Int16u attribute Response: %" PRIu16, value);
47
48     ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
49     command->SetCommandExitStatus(true);
50 }
51
52 static void OnInt32uAttributeResponse(void * context, uint32_t value)
53 {
54     ChipLogProgress(chipTool, "Int32u attribute Response: %" PRIu32, value);
55
56     ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
57     command->SetCommandExitStatus(true);
58 }
59
60 static void OnInt64uAttributeResponse(void * context, uint64_t value)
61 {
62     ChipLogProgress(chipTool, "Int64u attribute Response: %" PRIu64, value);
63
64     ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
65     command->SetCommandExitStatus(true);
66 }
67
68 static void OnInt16sAttributeResponse(void * context, int16_t value)
69 {
70     ChipLogProgress(chipTool, "Int16s attribute Response: %" PRId16, value);
71
72     ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
73     command->SetCommandExitStatus(true);
74 }
75
76 static void OnStringAttributeResponse(void * context, const chip::ByteSpan value)
77 {
78     char * str = (char *)malloc(value.size() * sizeof(char));
79     memmove(str, value.data(), value.size());
80     str[value.size()] = '\0';
81     free(str);
82
83     ChipLogProgress(chipTool, "String attribute Response: %s (%" PRIu16 ")", str, strlen(str));
84
85     ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
86     command->SetCommandExitStatus(true);
87 }
88
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}})
97 {
98     ChipLogProgress(chipTool, "{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}");
99
100     ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
101     command->SetCommandExitStatus(true);
102 }
103
104 {{/if}}
105 {{/if}}
106 {{/if}}
107 {{/all_user_cluster_commands}}
108 {{/if}}
109 {{/if}}
110 {{/all_user_clusters}}
111
112 {{> clusters_header}}
113
114 {{#chip_clusters}}
115 constexpr chip::ClusterId k{{asCamelCased name false}}ClusterId = {{asHex code 4}};
116 {{/chip_clusters}}
117
118 {{#chip_clusters}}
119 {{> cluster_header}}
120
121 {{#chip_server_cluster_commands}}
122 /*
123  * Command {{asCamelCased name false}}
124  */
125 class {{asCamelCased clusterName false}}{{asCamelCased name false}}: public ModelCommand
126 {
127 public:
128     {{asCamelCased clusterName false}}{{asCamelCased name false}}(): ModelCommand("{{asDelimitedCommand name}}")
129     {
130         {{#chip_server_cluster_command_arguments}}
131         {{#if (isString type)}}
132         AddArgument("{{asCamelCased label}}", &m{{asCamelCased label false}});
133         {{else}}
134         AddArgument("{{asCamelCased label}}", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, &m{{asCamelCased label false}});
135         {{/if}}
136         {{/chip_server_cluster_command_arguments}}
137         ModelCommand::AddArguments();
138     }
139     ~{{asCamelCased clusterName false}}{{asCamelCased name false}}()
140     {
141       delete onSuccessCallback;
142       delete onFailureCallback;
143     }
144
145     CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
146     {
147         ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command ({{asHex code 2}}) on endpoint %" PRIu16, endpointId);
148
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}});
152     }
153
154 private:
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);
157     {{else}}
158     chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
159     {{/if}}
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}};
164     {{else}}
165     {{chipType}} m{{asCamelCased label false}};
166     {{/if}}
167     {{/chip_server_cluster_command_arguments}}
168 };
169
170 {{/chip_server_cluster_commands}}
171
172 /*
173  * Discover Attributes
174  */
175 class Discover{{asCamelCased name false}}Attributes: public ModelCommand
176 {
177 public:
178     Discover{{asCamelCased name false}}Attributes(): ModelCommand("discover")
179     {
180         ModelCommand::AddArguments();
181     }
182
183     ~Discover{{asCamelCased name false}}Attributes()
184     {
185       delete onSuccessCallback;
186       delete onFailureCallback;
187     }
188
189     CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
190     {
191         ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x0C) on endpoint %" PRIu16, endpointId);
192
193         chip::Controller::{{asCamelCased name false}}Cluster cluster;
194         cluster.Associate(device, endpointId);
195         return cluster.DiscoverAttributes(onSuccessCallback->Cancel(), onFailureCallback->Cancel());
196     }
197
198 private:
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);
201 };
202
203 {{#chip_server_cluster_attributes}}
204 /*
205  * Attribute {{asCamelCased name false}}
206  */
207 class Read{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
208 {
209 public:
210     Read{{asCamelCased parent.name false}}{{asCamelCased name false}}(): ModelCommand("read")
211     {
212         AddArgument("attr-name", "{{asDelimitedCommand (asCamelCased name)}}");
213         ModelCommand::AddArguments();
214     }
215
216     ~Read{{asCamelCased parent.name false}}{{asCamelCased name false}}()
217     {
218       delete onSuccessCallback;
219       delete onFailureCallback;
220     }
221
222     CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
223     {
224         ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x00) on endpoint %" PRIu16, endpointId);
225
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());
229     }
230
231 private:
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);
234 };
235
236 {{#if (isWritableAttribute)}}
237 class Write{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
238 {
239 public:
240     Write{{asCamelCased parent.name false}}{{asCamelCased name false}}(): ModelCommand("write")
241     {
242         AddArgument("attr-name", "{{asDelimitedCommand (asCamelCased name)}}");
243         {{#if (isString type)}}
244         AddArgument("attr-value", &mValue);
245         {{else}}
246         AddArgument("attr-value", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, &mValue);
247         {{/if}}
248         ModelCommand::AddArguments();
249     }
250
251     ~Write{{asCamelCased parent.name false}}{{asCamelCased name false}}()
252     {
253       delete onSuccessCallback;
254       delete onFailureCallback;
255     }
256
257     CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
258     {
259         ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x01) on endpoint %" PRIu16, endpointId);
260
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}});
264     }
265
266 private:
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)}}
270     char * mValue;
271     {{else}}
272     {{chipType}} mValue;
273     {{/if}}
274 };
275
276 {{/if}}
277 {{#if (isReportableAttribute)}}
278 class Report{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
279 {
280 public:
281     Report{{asCamelCased parent.name false}}{{asCamelCased name false}}(): ModelCommand("report")
282     {
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);
288         {{/unless}}
289         ModelCommand::AddArguments();
290     }
291
292     ~Report{{asCamelCased parent.name false}}{{asCamelCased name false}}()
293     {
294       delete onSuccessCallback;
295       delete onFailureCallback;
296       delete onReportCallback;
297     }
298
299     CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
300     {
301         ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x06) on endpoint %" PRIu16, endpointId);
302
303         chip::Controller::{{asCamelCased parent.name false}}Cluster cluster;
304         cluster.Associate(device, endpointId);
305
306         CHIP_ERROR err = cluster.ReportAttribute{{asCamelCased name false}}(onReportCallback->Cancel());
307         if (err != CHIP_NO_ERROR)
308         {
309             return err;
310         }
311
312         return cluster.ConfigureAttribute{{asCamelCased name false}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mMinInterval, mMaxInterval{{#unless (isDiscreteType)}}, mChange{{/unless}});
313     }
314
315 private:
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;
323     {{/unless}}
324 };
325
326 {{/if}}
327 {{/chip_server_cluster_attributes}}
328 {{/chip_clusters}}
329
330 /*----------------------------------------------------------------------------*\
331 | Register all Clusters commands                                               |
332 \*----------------------------------------------------------------------------*/
333 {{#chip_clusters}}
334 void registerCluster{{asCamelCased name false}}(Commands & commands)
335 {
336     const char * clusterName = "{{asCamelCased name false}}";
337
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}}>(),
347         {{/if}}
348         {{#if (isReportableAttribute)}}
349         make_unique<Report{{asCamelCased parent.name false}}{{asCamelCased name false}}>(),
350         {{/if}}
351         {{/chip_server_cluster_attributes}}
352     };
353
354     commands.Register(clusterName, clusterCommands);
355 }
356 {{/chip_clusters}}
357
358 void registerClusters(Commands & commands)
359 {
360 {{#chip_clusters}}
361     registerCluster{{asCamelCased name false}}(commands);
362 {{/chip_clusters}}
363 }