Apply Upstream code (2021-03-15)
[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
140     CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
141     {
142         ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command ({{asHex code 2}}) on endpoint %" PRIu16, endpointId);
143
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}});
147     }
148
149 private:
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);
152     {{else}}
153     chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
154     {{/if}}
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}};
159     {{else}}
160     {{chipType}} m{{asCamelCased label false}};
161     {{/if}}
162     {{/chip_server_cluster_command_arguments}}
163 };
164
165 {{/chip_server_cluster_commands}}
166
167 /*
168  * Discover Attributes
169  */
170 class Discover{{asCamelCased name false}}Attributes: public ModelCommand
171 {
172 public:
173     Discover{{asCamelCased name false}}Attributes(): ModelCommand("discover")
174     {
175         ModelCommand::AddArguments();
176     }
177
178     CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
179     {
180         ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x0C) on endpoint %" PRIu16, endpointId);
181
182         chip::Controller::{{asCamelCased name false}}Cluster cluster;
183         cluster.Associate(device, endpointId);
184         return cluster.DiscoverAttributes(onSuccessCallback->Cancel(), onFailureCallback->Cancel());
185     }
186
187 private:
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);
190 };
191
192 {{#chip_server_cluster_attributes}}
193 /*
194  * Attribute {{asCamelCased name false}}
195  */
196 class Read{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
197 {
198 public:
199     Read{{asCamelCased parent.name false}}{{asCamelCased name false}}(): ModelCommand("read")
200     {
201         AddArgument("attr-name", "{{asDelimitedCommand (asCamelCased name)}}");
202         ModelCommand::AddArguments();
203     }
204
205     CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
206     {
207         ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x00) on endpoint %" PRIu16, endpointId);
208
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());
212     }
213
214 private:
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);
217 };
218
219 {{#if (isWritableAttribute)}}
220 class Write{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
221 {
222 public:
223     Write{{asCamelCased parent.name false}}{{asCamelCased name false}}(): ModelCommand("write")
224     {
225         AddArgument("attr-name", "{{asDelimitedCommand (asCamelCased name)}}");
226         {{#if (isString type)}}
227         AddArgument("attr-value", &mValue);
228         {{else}}
229         AddArgument("attr-value", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, &mValue);
230         {{/if}}
231         ModelCommand::AddArguments();
232     }
233
234     CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
235     {
236         ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x01) on endpoint %" PRIu16, endpointId);
237
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}});
241     }
242
243 private:
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)}}
247     char * mValue;
248     {{else}}
249     {{chipType}} mValue;
250     {{/if}}
251 };
252
253 {{/if}}
254 {{#if (isReportableAttribute)}}
255 class Report{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
256 {
257 public:
258     Report{{asCamelCased parent.name false}}{{asCamelCased name false}}(): ModelCommand("report")
259     {
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);
265         {{/unless}}
266         ModelCommand::AddArguments();
267     }
268
269     CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
270     {
271         ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x06) on endpoint %" PRIu16, endpointId);
272
273         chip::Controller::{{asCamelCased parent.name false}}Cluster cluster;
274         cluster.Associate(device, endpointId);
275
276         CHIP_ERROR err = cluster.ReportAttribute{{asCamelCased name false}}(onReportCallback->Cancel());
277         if (err != CHIP_NO_ERROR)
278         {
279             return err;
280         }
281
282         return cluster.ConfigureAttribute{{asCamelCased name false}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mMinInterval, mMaxInterval{{#unless (isDiscreteType)}}, mChange{{/unless}});
283     }
284
285 private:
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;
293     {{/unless}}
294 };
295
296 {{/if}}
297 {{/chip_server_cluster_attributes}}
298 {{/chip_clusters}}
299
300 /*----------------------------------------------------------------------------*\
301 | Register all Clusters commands                                               |
302 \*----------------------------------------------------------------------------*/
303 {{#chip_clusters}}
304 void registerCluster{{asCamelCased name false}}(Commands & commands)
305 {
306     const char * clusterName = "{{asCamelCased name false}}";
307
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}}>(),
317         {{/if}}
318         {{#if (isReportableAttribute)}}
319         make_unique<Report{{asCamelCased parent.name false}}{{asCamelCased name false}}>(),
320         {{/if}}
321         {{/chip_server_cluster_attributes}}
322     };
323
324     commands.Register(clusterName, clusterCommands);
325 }
326 {{/chip_clusters}}
327
328 void registerClusters(Commands & commands)
329 {
330 {{#chip_clusters}}
331     registerCluster{{asCamelCased name false}}(commands);
332 {{/chip_clusters}}
333 }