-
Notifications
You must be signed in to change notification settings - Fork 2k
/
commands.zapt
444 lines (375 loc) · 17.3 KB
/
commands.zapt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
{{> header}}
#pragma once
#include <cstdint>
#include <app-common/zap-generated/cluster-objects.h>
#include <app/data-model/DecodableList.h>
#include <commands/clusters/ModelCommand.h>
#include <lib/core/CHIPSafeCasts.h>
#include <lib/support/BytesToHex.h>
#include <zap-generated/CHIPClientCallbacks.h>
#include <zap-generated/CHIPClusters.h>
static void OnDefaultSuccessResponse(void * context)
{
ChipLogProgress(chipTool, "Default Success Response");
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
static void OnDefaultFailureResponse(void * context, uint8_t status)
{
ChipLogProgress(chipTool, "Default Failure Response: 0x%02x", status);
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_ERROR_INTERNAL);
}
static void OnDefaultSuccess(void * context, const chip::app::DataModel::NullObjectType & data)
{
OnDefaultSuccessResponse(context);
};
static void OnDefaultFailure(void * context, EmberAfStatus status)
{
OnDefaultFailureResponse(context, status);
};
static void OnBooleanAttributeResponse(void * context, bool value)
{
ChipLogProgress(chipTool, "Boolean attribute Response: %d", value);
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
static void OnInt8uAttributeResponse(void * context, uint8_t value)
{
ChipLogProgress(chipTool, "Int8u attribute Response: %" PRIu8, value);
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
static void OnInt16uAttributeResponse(void * context, uint16_t value)
{
ChipLogProgress(chipTool, "Int16u attribute Response: %" PRIu16, value);
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
static void OnInt32uAttributeResponse(void * context, uint32_t value)
{
ChipLogProgress(chipTool, "Int32u attribute Response: %" PRIu32, value);
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
static void OnInt64uAttributeResponse(void * context, uint64_t value)
{
ChipLogProgress(chipTool, "Int64u attribute Response: %" PRIu64, value);
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
static void OnInt8sAttributeResponse(void * context, int8_t value)
{
ChipLogProgress(chipTool, "Int8s attribute Response: %" PRId8, value);
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
static void OnInt16sAttributeResponse(void * context, int16_t value)
{
ChipLogProgress(chipTool, "Int16s attribute Response: %" PRId16, value);
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
static void OnInt32sAttributeResponse(void * context, int32_t value)
{
ChipLogProgress(chipTool, "Int32s attribute Response: %" PRId32, value);
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
static void OnInt64sAttributeResponse(void * context, int64_t value)
{
ChipLogProgress(chipTool, "Int64s attribute Response: %" PRId64, value);
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
static void OnOctetStringAttributeResponse(void * context, const chip::ByteSpan value)
{
char buffer[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
if (CHIP_NO_ERROR == chip::Encoding::BytesToUppercaseHexString(value.data(), value.size(), &buffer[0], CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE)) {
ChipLogProgress(chipTool, "OctetString attribute Response: %s", buffer);
} else {
ChipLogProgress(chipTool, "OctetString attribute Response len: %zu", value.size());
}
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
static void OnCharStringAttributeResponse(void * context, const chip::CharSpan value)
{
ChipLogProgress(chipTool, "CharString attribute Response: %.*s", static_cast<int>(value.size()), value.data());
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
{{#chip_client_clusters}}
{{#chip_server_cluster_attributes}}
{{#if isList}}
static void On{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}ListAttributeResponse(void * context, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} list)
{
ModelCommand * command = static_cast<ModelCommand *>(context);
size_t count = 0;
CHIP_ERROR err = list.ComputeSize(&count);
if (err != CHIP_NO_ERROR) {
command->SetCommandExitStatus(err);
return;
}
ChipLogProgress(chipTool, "On{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}ListAttributeResponse: %zu entries", count);
auto iter = list.begin();
uint16_t i = 0;
while (iter.Next())
{
++i;
#if CHIP_PROGRESS_LOGGING
auto & entry = iter.GetValue();
{{#if isStruct}}
ChipLogProgress(chipTool, "{{type}}[%" PRIu16 "]:", i);
{{#chip_attribute_list_entryTypes}}
{{~#*inline "field"}}entry.{{asLowerCamelCase name}}{{#if isOptional}}.Value(){{/if}}{{/inline~}}
{{~#*inline "fieldValue"}}{{>field}}{{#if isNullable}}.Value(){{/if}}{{/inline~}}
{{#if isOptional}}
if (entry.{{asLowerCamelCase name}}.HasValue()) {
{{/if}}
{{#if isNullable}}
if ({{>field}}.IsNull()) {
ChipLogProgress(chipTool, " {{asSymbol label}}: null");
} else {
{{/if}}
{{#if isArray}}
{{! TODO: Add support for printing list member of struct element of list attribute }}
ChipLogProgress(chipTool, " {{asSymbol label}}: list member of struct element of list attribute printing not supported yet");
{{else if (isOctetString type)}}
ChipLogProgress(Zcl, " {{asSymbol label}}: %zu", {{>fieldValue}}.size());
{{else if (isCharString type)}}
ChipLogProgress(Zcl, " {{asSymbol label}}: %.*s", static_cast<int>({{>fieldValue}}.size()), {{>fieldValue}}.data());
{{else if isStruct}}
{{! TODO: Add support for printing struct member of struct element of list attribute }}
ChipLogProgress(chipTool, " {{asSymbol label}}: struct member of struct element of list attribute printing not supported yet");
{{else}}
ChipLogProgress(chipTool, " {{asSymbol label}}: {{asPrintFormat type}}", {{>fieldValue}});
{{/if}}
{{#if isNullable}}
}
{{/if}}
{{#if isOptional}}
}
{{/if}}
{{/chip_attribute_list_entryTypes}}
{{else}}
{{#if (isOctetString type)}}
ChipLogProgress(Zcl, " {{asSymbol label}}: %zu", entry.size());
{{else if (isCharString type)}}
ChipLogProgress(Zcl, " {{asSymbol label}}: %.*s", static_cast<int>(entry.size()), entry.data());
{{else}}
ChipLogProgress(chipTool, "{{type}}[%" PRIu16 "]: {{asPrintFormat type}}", i, entry);
{{/if}}
{{/if}}
#endif // CHIP_PROGRESS_LOGGING
}
command->SetCommandExitStatus(iter.GetStatus());
}
{{/if}}
{{/chip_server_cluster_attributes}}
{{/chip_client_clusters}}
{{#chip_client_clusters}}
{{#chip_cluster_responses}}
static void On{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}Success(void * context, const chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::DecodableType & data)
{
ChipLogProgress(Zcl, "Received {{asUpperCamelCase name}}:");
{{#chip_cluster_response_arguments}}
{{~#*inline "field"}}data.{{asLowerCamelCase label}}{{/inline~}}
{{#if isArray}}
ChipLogProgress(Zcl, " {{label}}: Array printing is not implemented yet.");
{{else if isOptional}}
ChipLogProgress(Zcl, " {{label}}: Optional printing is not implemented yet.");
{{else if (isOctetString type)}}
ChipLogProgress(Zcl, " {{label}}: %zu", {{>field}}.size());
{{else if (isCharString type)}}
ChipLogProgress(Zcl, " {{label}}: %.*s", static_cast<int>({{>field}}.size()), {{>field}}.data());
{{else}}
ChipLogProgress(Zcl, " {{label}}: {{asPrintFormat type}}", {{>field}});
{{/if}}
{{/chip_cluster_response_arguments}}
ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
};
{{/chip_cluster_responses}}
{{/chip_client_clusters}}
{{> clusters_header}}
{{#chip_client_clusters}}
{{> cluster_header}}
{{#chip_cluster_commands}}
/*
* Command {{asUpperCamelCase name}}
*/
class {{asUpperCamelCase clusterName}}{{asUpperCamelCase name}}: public ModelCommand
{
public:
{{asUpperCamelCase clusterName}}{{asUpperCamelCase name}}(): ModelCommand("{{asDelimitedCommand name}}")
{
{{#chip_cluster_command_arguments}}
{{~#*inline "field"}}mRequest.{{asLowerCamelCase label}}{{/inline~}}
{{#if isArray}}
// {{label}} Array parsing is not supported yet
{{else if isStruct}}
// {{label}} Struct parsing is not supported yet
{{else if (isString type)}}
AddArgument("{{asUpperCamelCase label}}", &{{>field}});
{{else}}
AddArgument("{{asUpperCamelCase label}}", {{asTypeMinValue type}}, {{asTypeMaxValue type}},
{{~#if ./isEnum}}reinterpret_cast<std::underlying_type_t<decltype({{>field}})> *>(&{{>field}})
{{else ./isBitmap}}reinterpret_cast<std::underlying_type_t<chip::app::Clusters::{{asUpperCamelCase parent.clusterName}}::{{asUpperCamelCase type}}> *>(&{{>field}})
{{else}}&{{>field}}
{{/if~}}
);
{{/if}}
{{/chip_cluster_command_arguments}}
ModelCommand::AddArguments();
}
CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
{
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 8}}) command ({{asHex code 8}}) on endpoint %" PRIu8, endpointId);
{{#chip_cluster_command_non_expanded_arguments}}
{{#if isArray}}
{{! TODO Implement complex types parsing in order to properly set the request parameters }}
mRequest.{{asLowerCamelCase label}} = {{zapTypeToEncodableClusterObjectType type ns=parent.parent.name}}();
{{/if}}
{{/chip_cluster_command_non_expanded_arguments}}
chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster;
cluster.Associate(device, endpointId);
return cluster.InvokeCommand(mRequest, this, {{#if hasSpecificResponse}}On{{asUpperCamelCase parent.name}}{{asUpperCamelCase response.name}}Success{{else}}OnDefaultSuccess{{/if}}, OnDefaultFailure);
}
private:
chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::Type mRequest;
};
{{/chip_cluster_commands}}
{{#chip_server_cluster_attributes}}
/*
* Attribute {{asUpperCamelCase name}}
*/
class Read{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}: public ModelCommand
{
public:
Read{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}(): ModelCommand("read")
{
AddArgument("attr-name", "{{asDelimitedCommand (asUpperCamelCase name)}}");
ModelCommand::AddArguments();
}
~Read{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}()
{
delete onSuccessCallback;
delete onFailureCallback;
}
CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
{
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x00) on endpoint %" PRIu8, endpointId);
chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster;
cluster.Associate(device, endpointId);
return cluster.ReadAttribute{{asUpperCamelCase name}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel());
}
private:
{{#if isList}}
chip::Callback::Callback<{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}ListAttributeCallback> * onSuccessCallback = new chip::Callback::Callback<{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}ListAttributeCallback>(On{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}ListAttributeResponse, this);
{{else}}
chip::Callback::Callback<{{chipCallback.name}}AttributeCallback> * onSuccessCallback = new chip::Callback::Callback<{{chipCallback.name}}AttributeCallback>(On{{chipCallback.name}}AttributeResponse, this);
{{/if}}
chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
};
{{#if isWritableAttribute}}
class Write{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}: public ModelCommand
{
public:
Write{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}(): ModelCommand("write")
{
AddArgument("attr-name", "{{asDelimitedCommand (asUpperCamelCase name)}}");
{{#if (isString type)}}
AddArgument("attr-value", &mValue);
{{else}}
AddArgument("attr-value", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, &mValue);
{{/if}}
ModelCommand::AddArguments();
}
~Write{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}()
{
delete onSuccessCallback;
delete onFailureCallback;
}
CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
{
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x01) on endpoint %" PRIu8, endpointId);
chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster;
cluster.Associate(device, endpointId);
return cluster.WriteAttribute{{asUpperCamelCase name}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mValue);
}
private:
chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
{{chipType}} mValue;
};
{{/if}}
{{#if isReportableAttribute}}
class Report{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}: public ModelCommand
{
public:
Report{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}(): ModelCommand("report")
{
AddArgument("attr-name", "{{asDelimitedCommand (asUpperCamelCase name)}}");
AddArgument("min-interval", 0, UINT16_MAX, &mMinInterval);
AddArgument("max-interval", 0, UINT16_MAX, &mMaxInterval);
ModelCommand::AddArguments();
}
~Report{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}()
{
delete onSuccessCallback;
delete onFailureCallback;
delete onReportCallback;
}
CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
{
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x06) on endpoint %" PRIu8, endpointId);
chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster;
cluster.Associate(device, endpointId);
CHIP_ERROR err = cluster.ReportAttribute{{asUpperCamelCase name}}(onReportCallback->Cancel());
if (err != CHIP_NO_ERROR)
{
return err;
}
return cluster.SubscribeAttribute{{asUpperCamelCase name}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mMinInterval, mMaxInterval);
}
private:
chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
chip::Callback::Callback<{{chipCallback.name}}AttributeCallback> * onReportCallback = new chip::Callback::Callback<{{chipCallback.name}}AttributeCallback>(On{{chipCallback.name}}AttributeResponse, this);
uint16_t mMinInterval;
uint16_t mMaxInterval;
};
{{/if}}
{{/chip_server_cluster_attributes}}
{{/chip_client_clusters}}
/*----------------------------------------------------------------------------*\
| Register all Clusters commands |
\*----------------------------------------------------------------------------*/
{{#chip_client_clusters}}
void registerCluster{{asUpperCamelCase name}}(Commands & commands)
{
const char * clusterName = "{{asUpperCamelCase name}}";
commands_list clusterCommands = {
{{#chip_cluster_commands}}
make_unique<{{asUpperCamelCase clusterName}}{{asUpperCamelCase name}}>(), //
{{/chip_cluster_commands}}
{{#chip_server_cluster_attributes}}
make_unique<Read{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}>(), //
{{#if isWritableAttribute}}
make_unique<Write{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}>(), //
{{/if}}
{{#if isReportableAttribute}}
make_unique<Report{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}>(), //
{{/if}}
{{/chip_server_cluster_attributes}}
};
commands.Register(clusterName, clusterCommands);
}
{{/chip_client_clusters}}
void registerClusters(Commands & commands)
{
{{#chip_client_clusters}}
registerCluster{{asUpperCamelCase name}}(commands);
{{/chip_client_clusters}}
}