Skip to content

Commit c2e5453

Browse files
Add ZAP templates to src/app (project-chip#3638)
* Moved templates out of PR project-chip#3464 * fix PR comments * Moved template to src/app * Fix PR comments * Renamed src/app/zap to src/app/zap-templates * removed CLI define * Switch to pragma once * Restyled by whitespace * Restyled by clang-format * Restyled by prettier-json * Restyled by prettier-markdown Co-authored-by: Restyled.io <commits@restyled.io>
1 parent d652be4 commit c2e5453

19 files changed

+663
-0
lines changed

src/app/docs/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ Framework including the file you are reading right now.
2929
This directory contains all of the cluster implementations included in the CHIP
3030
ZCL Application Framework.
3131

32+
### <code>/src/app/zap-templates</code>
33+
34+
This directory contains all of the templates and helpers specific to CHIP for
35+
the ZAP tool.
36+
3237
## Public APIs
3338

3439
### Attribute changes

src/app/zap-templates/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# ZAP generation templates
2+
3+
## What is this repo?
4+
5+
This directory contains generation templates for ZAP, ZCL Advanced Platform.
6+
7+
**IMPORTANT**: Changes to templates will affect all examples.
8+
9+
# Useful command for CHIP
10+
11+
Run ZAP with UI to configure endpoints and clusters
12+
13+
```
14+
cd ./third_party/zap/repo/
15+
node src-script/zap-start.js --logToStdout --gen ../../../src/app/zap-templates/chip-templates.json
16+
```
17+
18+
Generate files in headless mode
19+
20+
```
21+
cd ./third_party/zap/repo/
22+
node src-script/zap-generate.js -z ./zcl-builtin/silabs/zcl.json -g ../../../src/app/zap-templates/chip-templates.json -i <path to *.zap file> -o <Path to /gen/ folder>
23+
```
24+
25+
For more information please see the documentation under `docs/` in
26+
[ZAP](https://github.com/project-chip/zap)

src/app/zap-templates/af-structs.zapt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{chip_header}}
2+
3+
// Prevent multiple inclusion
4+
#pragma once
5+
6+
#include <stdint.h>
7+
#include "enums.h"
8+
9+
{{#zcl_structs}}
10+
11+
// Struct for {{label}}
12+
typedef struct _{{asType label}} {
13+
{{#zcl_struct_items}}
14+
{{ident}}{{asUnderlyingType type}} {{asSymbol label}};
15+
{{/zcl_struct_items}}
16+
} {{asUnderlyingType label}};
17+
{{/zcl_structs}}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{{chip_header}}
2+
3+
// Prevent multiple inclusion
4+
#pragma once
5+
6+
// Attribute masks modify how attributes are used by the framework
7+
//
8+
// Attribute that has this mask is NOT read-only
9+
#define ATTRIBUTE_MASK_WRITABLE (0x01)
10+
// Attribute that has this mask is saved to a token
11+
#define ATTRIBUTE_MASK_TOKENIZE (0x02)
12+
// Attribute that has this mask has a min/max values
13+
#define ATTRIBUTE_MASK_MIN_MAX (0x04)
14+
// Manufacturer specific attribute
15+
#define ATTRIBUTE_MASK_MANUFACTURER_SPECIFIC (0x08)
16+
// Attribute deferred to external storage
17+
#define ATTRIBUTE_MASK_EXTERNAL_STORAGE (0x10)
18+
// Attribute is singleton
19+
#define ATTRIBUTE_MASK_SINGLETON (0x20)
20+
// Attribute is a client attribute
21+
#define ATTRIBUTE_MASK_CLIENT (0x40)
22+
23+
// Cluster masks modify how clusters are used by the framework
24+
//
25+
// Does this cluster have init function?
26+
#define CLUSTER_MASK_INIT_FUNCTION (0x01)
27+
// Does this cluster have attribute changed function?
28+
#define CLUSTER_MASK_ATTRIBUTE_CHANGED_FUNCTION (0x02)
29+
// Does this cluster have default response function?
30+
#define CLUSTER_MASK_DEFAULT_RESPONSE_FUNCTION (0x04)
31+
// Does this cluster have message sent function?
32+
#define CLUSTER_MASK_MESSAGE_SENT_FUNCTION (0x08)
33+
// Does this cluster have manufacturer specific attribute changed function?
34+
#define CLUSTER_MASK_MANUFACTURER_SPECIFIC_ATTRIBUTE_CHANGED_FUNCTION (0x10)
35+
// Does this cluster have pre-attribute changed function?
36+
#define CLUSTER_MASK_PRE_ATTRIBUTE_CHANGED_FUNCTION (0x20)
37+
// Cluster is a server
38+
#define CLUSTER_MASK_SERVER (0x40)
39+
// Cluster is a client
40+
#define CLUSTER_MASK_CLIENT (0x80)
41+
42+
// Command masks modify meanings of commands
43+
//
44+
// Is sending of this client command supported
45+
#define COMMAND_MASK_OUTGOING_CLIENT (0x01)
46+
// Is sending of this server command supported
47+
#define COMMAND_MASK_OUTGOING_SERVER (0x02)
48+
// Is receiving of this client command supported
49+
#define COMMAND_MASK_INCOMING_CLIENT (0x04)
50+
// Is receiving of this server command supported
51+
#define COMMAND_MASK_INCOMING_SERVER (0x08)
52+
// Is this command manufacturer specific?
53+
#define COMMAND_MASK_MANUFACTURER_SPECIFIC (0x10)
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{{chip_header}}
2+
3+
// Prevent multiple inclusion
4+
#pragma once
5+
6+
{{#zcl_clusters}}
7+
// Attribute ids for cluster: {{label}}
8+
9+
{{#zcl_attributes_client}}
10+
{{#first}}
11+
// Client attributes
12+
{{/first}}
13+
#define ZCL_{{asDelimitedMacro define}}_ATTRIBUTE_ID ({{asHex code 4}})
14+
{{#last}}
15+
16+
{{/last}}
17+
{{/zcl_attributes_client}}
18+
{{#zcl_attributes_server}}
19+
{{#first}}
20+
// Server attributes
21+
{{/first}}
22+
#define ZCL_{{asDelimitedMacro define}}_ATTRIBUTE_ID ({{asHex code 4}})
23+
{{#last}}
24+
25+
{{/last}}
26+
{{/zcl_attributes_server}}
27+
{{/zcl_clusters}}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{{chip_header}}
2+
3+
// Prevent multiple inclusion
4+
#pragma once
5+
6+
// ZCL attribute types
7+
enum {
8+
{{#zcl_atomics}}
9+
{{ident}}ZCL_{{asDelimitedMacro name}}_ATTRIBUTE_TYPE = {{asHex atomicId 2}}, // {{description}}
10+
{{/zcl_atomics}}
11+
};
12+
13+
// ZCL attribute sizes
14+
#define ZAP_GENERATED_ATTRIBUTE_SIZES { \
15+
{{#zcl_atomics}}
16+
{{#if size}}
17+
{{ident}}ZCL_{{asDelimitedMacro name}}_ATTRIBUTE_TYPE, {{size}}, \
18+
{{/if}}
19+
{{/zcl_atomics}}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{{chip_header}}
2+
3+
#include <stdint.h>
4+
5+
#include "af-structs.h"
6+
#include "call-command-handler.h"
7+
#include "callback.h"
8+
#include "command-id.h"
9+
#include "util.h"
10+
11+
{{#all_user_clusters}}
12+
{{#if (isEnabled enabled)}}
13+
EmberAfStatus emberAf{{asCamelCased name false}}Cluster{{asCamelCased side false}}CommandParse(EmberAfClusterCommand * cmd);
14+
{{/if}}
15+
{{/all_user_clusters}}
16+
17+
18+
static EmberAfStatus status(bool wasHandled, bool clusterExists, bool mfgSpecific)
19+
{
20+
if (wasHandled)
21+
{
22+
return EMBER_ZCL_STATUS_SUCCESS;
23+
}
24+
else if (mfgSpecific)
25+
{
26+
return EMBER_ZCL_STATUS_UNSUP_MANUF_CLUSTER_COMMAND;
27+
}
28+
else if (clusterExists)
29+
{
30+
return EMBER_ZCL_STATUS_UNSUP_COMMAND;
31+
}
32+
else
33+
{
34+
return EMBER_ZCL_STATUS_UNSUPPORTED_CLUSTER;
35+
}
36+
}
37+
38+
39+
// Main command parsing controller.
40+
EmberAfStatus emberAfClusterSpecificCommandParse(EmberAfClusterCommand * cmd)
41+
{
42+
EmberAfStatus result = status(false, false, cmd->mfgSpecific);
43+
if (cmd->direction == (uint8_t) ZCL_DIRECTION_SERVER_TO_CLIENT &&
44+
emberAfContainsClientWithMfgCode(cmd->apsFrame->destinationEndpoint, cmd->apsFrame->clusterId, cmd->mfgCode))
45+
{
46+
switch (cmd->apsFrame->clusterId)
47+
{
48+
{{#all_user_clusters}}
49+
{{#if (isEnabled enabled)}}
50+
{{#if (isClient side) }}
51+
case ZCL_{{asDelimitedMacro define}}_ID :
52+
result = emberAf{{asCamelCased name false}}Cluster{{asCamelCased side false}}CommandParse(cmd);
53+
break;
54+
{{/if}}
55+
{{/if}}
56+
{{/all_user_clusters}}
57+
default:
58+
// Unrecognized cluster ID, error status will apply.
59+
break;
60+
}
61+
}
62+
else if (cmd->direction == (uint8_t) ZCL_DIRECTION_CLIENT_TO_SERVER &&
63+
emberAfContainsServerWithMfgCode(cmd->apsFrame->destinationEndpoint, cmd->apsFrame->clusterId, cmd->mfgCode))
64+
{
65+
switch (cmd->apsFrame->clusterId)
66+
{
67+
{{#all_user_clusters}}
68+
{{#if (isEnabled enabled)}}
69+
{{#unless (isClient side) }}
70+
case ZCL_{{asDelimitedMacro define}}_ID :
71+
result = emberAf{{asCamelCased name false}}Cluster{{asCamelCased side false}}CommandParse(cmd);
72+
break;
73+
{{/unless}}
74+
{{/if}}
75+
{{/all_user_clusters}}
76+
default:
77+
// Unrecognized cluster ID, error status will apply.
78+
break;
79+
}
80+
}
81+
return result;
82+
}
83+
84+
// Cluster specific command parsing
85+
86+
{{#all_user_clusters}}
87+
{{#if (isEnabled enabled)}}
88+
EmberAfStatus emberAf{{asCamelCased name false}}Cluster{{asCamelCased side false}}CommandParse(EmberAfClusterCommand * cmd)
89+
{
90+
bool wasHandled = false;
91+
92+
if (!cmd->mfgSpecific)
93+
{
94+
switch (cmd->commandId)
95+
{
96+
{{#all_user_cluster_commands}}
97+
{{#if (isStrEqual clusterName parent.name)}}
98+
{{#if (isCommandAvailable parent.side incoming outgoing commandSource name)}}
99+
case ZCL_{{asDelimitedMacro name}}_COMMAND_ID: {
100+
{{#if (zcl_command_arguments_count this.id)}}
101+
uint32_t argOffset = 0;
102+
{{#zcl_command_arguments}}
103+
{{asUnderlyingType type}} * {{asSymbol label}} = ({{asUnderlyingType type}} *)(cmd->buffer + argOffset);
104+
{{#unless (isLastElement index count)}}
105+
argOffset+= sizeof({{asUnderlyingType type}});
106+
{{/unless}}
107+
{{/zcl_command_arguments}}
108+
109+
wasHandled = emberAf{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}Callback({{#zcl_command_arguments}} *{{asSymbol label}}{{#unless (isLastElement index count)}}, {{/unless}}{{/zcl_command_arguments}});
110+
{{else}}
111+
wasHandled = emberAf{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}Callback();
112+
{{/if}}
113+
break;
114+
}
115+
{{/if}}
116+
{{/if}}
117+
{{/all_user_cluster_commands}}
118+
default: {
119+
// Unrecognized command ID, error status will apply.
120+
break;
121+
}
122+
}
123+
}
124+
return status(wasHandled, true, cmd->mfgSpecific);
125+
}
126+
{{/if}}
127+
{{/all_user_clusters}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{chip_header}}
2+
3+
// Prevent multiple inclusion
4+
#pragma once
5+
6+
#include "af-types.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{{chip_header}}
2+
3+
#include "callback.h"
4+
#include "cluster-id.h"
5+
6+
// Cluster Init Functions
7+
void emberAfClusterInitCallback(uint8_t endpoint, EmberAfClusterId clusterId)
8+
{
9+
switch (clusterId)
10+
{
11+
{{#all_user_clusters_names}}
12+
case ZCL_{{asDelimitedMacro define}}_ID :
13+
emberAf{{asCamelCased name false}}ClusterInitCallback(endpoint);
14+
break;
15+
{{/all_user_clusters_names}}
16+
default:
17+
// Unrecognized cluster ID
18+
break;
19+
}
20+
}
21+
22+
{{#all_user_clusters_names}}
23+
void __attribute__((weak)) emberAf{{asCamelCased name false}}ClusterInitCallback(uint8_t endpoint)
24+
{
25+
// To prevent warning
26+
(void) endpoint;
27+
}
28+
{{/all_user_clusters_names}}
29+
30+
// Cluster Command callback
31+
32+
{{#all_user_clusters}}
33+
{{#if (isEnabled enabled)}}
34+
{{#all_user_cluster_commands}}
35+
{{#if (isStrEqual clusterName parent.name)}}
36+
{{#if (isCommandAvailable parent.side incoming outgoing commandSource)}}
37+
/**
38+
* @brief {{parent.name}} Cluster {{name}} Command callback
39+
{{#if (zcl_command_arguments_count this.id)}}
40+
{{#zcl_command_arguments}}
41+
* @param {{asCamelCased label}}
42+
{{/zcl_command_arguments}}
43+
{{/if}}
44+
*/
45+
46+
{{#if (zcl_command_arguments_count this.id)}}
47+
bool __attribute__((weak)) emberAf{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}Callback({{#zcl_command_arguments}} {{asUnderlyingType type}} {{asSymbol label}}{{#unless (isLastElement index count)}}, {{/unless}}{{/zcl_command_arguments}})
48+
{
49+
// To prevent warning
50+
{{#zcl_command_arguments}}
51+
(void) {{asSymbol label}};
52+
{{/zcl_command_arguments}}
53+
54+
return false;
55+
}
56+
{{else}}
57+
bool __attribute__((weak)) emberAf{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}Callback(void)
58+
{
59+
return false;
60+
}
61+
{{/if}}
62+
63+
{{/if}}
64+
{{/if}}
65+
{{/all_user_cluster_commands}}
66+
{{/if}}
67+
{{/all_user_clusters}}

0 commit comments

Comments
 (0)