Adding new clusters and device types to the codegen#
This document outlines the process needed to add a new Matter device type and related clusters. Obviously, the steps below assume that the related Matter specifications were properly reviewed and approved.
Add the cluster definitions to the SDK#
The following steps should be followed to add new cluster definitions to the SDK.
Add your new cluster definition to an appropriately-name file in this directory: src/app/zap-templates/zcl/data-model/chip
Add references to each new cluster definition to these files:
If it is a derived cluster, add a reference to the base cluster definition. (e.g. in mode-base-cluster.xml you may need to add cluster codes - otherwise you may get strange exceptions which aren’t clear when running regen_all.py)
<struct name="ModeOptionStruct"> <cluster code="0x0051"/> <!-- Laundry Washer Mode --> <cluster code="YOUR NEW CLUSTER ID"/> </struct>
Enable your new cluster in the Python and Android clients in src/controller/data_model/controller-clusters.zap
You will need the ZAP tool to edit the ZAP file.
Unless you already have the tool installed, you can use one of the nightly builds
Use the ZAP GUI tool to edit the file above:
From the command line, navigate to the directory containing controller-clusters.zap
Run zap:
../../../scripts/tools/zap/run_zaptool.sh controller-clusters.zap
. Alternatively, run the zap tool and navigate to the zap file that you wish to open, or run as./scripts/tools/zap/run_zaptool.sh src/controller/data_model/controller-clusters.zap
.In the gui, select
Endpoint-1
from the left pane.Open the cluster group, for example,
Appliances
.Find the cluster to be enabled, e.g.
Dishwasher Control
.In the Enable column, select “Client” from the drop-down box.
Click
File->Save
to save the configuration.Close the GUI.
Add entries for your new cluster to BUILD.gn in the outputs section of the java-jni-generate bits. The entries should look like “jni/YourClusterNameClient-InvokeSubscribeImpl.cpp” and “jni/YourClusterNameClient-ReadImpl.cpp”.
Add an entry to the ClientDirectories section of src/app/zap_cluster_list.json.
Update chip-tool
Regenerate all zap generated code using
./scripts/tools/zap_regen_all.py
Rebuild chip-tool and it will have new cluster support:
./scripts/examples/gn_build_example.sh examples/chip-tool SOME-PATH/
Add the device type definition to the SDK#
Add the XML definition of the device to matter-devices.xml
Implement the incoming command behaviors common to all applications. The parsing of the payload from TLV to a C++ struct is done by code auto-generated from the XML (see zap-generated) The rest of the functionality must be manually implemented. Note: for the auto-generated code run one of the following:
for everything:
./scripts/tools/zap_regen_all.py
just for the app-common part:
./scripts/tools/zap/generate.py -t src/app/common/templates/templates.json -o zzz_generated/app-common/app-common/zap-generated src/controller/data_model/controller-clusters.zap
Implement the read/write/storage operations for the attributes of any type, list, or struct which are not the global attributes present in all clusters. For example, there’s no need to implement CommandList, AttributeList etc. For the attributes which are not list of struct type, the handling code is implemented generically so most likely no work is needed for them.
Implement any attribute spec requirements that are common to all applications. For example: code to enforce specific ranges, code for handling the interactions between attributes etc.
Implement Code and Tests#
Implement the clusters, the example cluster server application and add the related SDK tests.
Implement new clusters here: src/app/clusters
Implement tests here: src/app/tests/suites
Implement the example cluster server application:
The YAML tests will run against this server
Depending on the clusters, there are two options:
Enable them in the all-clusters-app and use that as the example cluster server app. If choosing this option, consider adding an example application that has just the relevant clusters enabled; this part is a nice to have.
If the clusters have complicated global application requirements consider using a separate example app. see the door lock, bridge, TV, OTA clusters.
NOTES: If adding a new cluster derived from
mode-base
into examples/all-clusters-app/ (Assume your new cluster is calledXZYMode
):Create your new
xyz-mode-cluster.xml
in src/app/zap-templates/zcl/data-model/chip (as above). Note that this is a much cut down cluster definition compared to normal clusters, since it derives from mode-base-cluster.xml. See dishwasher-mode-cluster.xml as an example. Note you should review if you need theStartUpMode
andOnMode
attributes based on the spec.Check that you have added your cluster code into mode-base-cluster.xml
<struct name="ModeTagStruct"> <cluster code="0xXXXX">
- replaceXXXX
with your cluster ID<struct name="ModeOptionStruct"> <cluster code="0xXXXX">
- replaceXXXX
with your cluster ID<bitmap name="Feature" type="bitmap32"> <cluster code="0xXXXX">
- replaceXXXX
with your cluster ID
Add your new Mode definitions in
xyz-mode.h
. In this header you define the modes / labels and tags. See dishwasher-mode.h as an example.Add your new stub to instantiate the mode. See dishwasher-mode.cpp as an example.
In examples/all-clusters-app/linux/main-common.cpp:
Add
#include "xyz-mode.h"
In
ApplicationShutdown()
, add a call to yourClusters::XYZMode::Shutdown();
.
In examples/all-clusters-app/linux/BUILD.gn, add the
xyz-mode.cpp
file to thesources
section.In src/app/common/templates/config-data.yaml:
Add a
<XYZMode>::ModeTag
to theEnumsNotUsedAsTypeInXML
section.Add an
XYZ Mode
entry to theCommandHandlerInterfaceOnlyClusters
section.
In src/app/util/util.cpp, in the
// Cluster Init Functions...
section, add a voidMatterXYZModePluginServerInitCallback() {}
definition.In src/app/zap-templates/zcl/zcl-with-test-extensions.json:
Add the
xyz-mode-cluster.xml
to thexmlFile
listIn the
attributeAccessInterfaceAttributes
entry, add your new entry"XYZ Mode": [ "SupportedModes", "CurrentMode", "FeatureMap" ]
- this will mean ZAP won’t generate code to handle these attributes
In src/app/zap_cluster_list.json:
Add your
XYZ_MODE_CLUSTER: []
toClientDirectories: { }
objectAdd your
"XYZ_MODE_CLUSTER": ["mode-base-server"]
to"ServerDirectories": { }
The code under src/app/tests/suites/certification for YAML or src/python_testing for Python should ideally implement the test plan (section 4 below).
A test under src/app/tests/suites (not certification) can test anything, in addition to, or preceding the official YAML representing the official test plan.
Add the test plan, using the templates below:
Also, ask to be added to the private
csg-tt-test-plans
Slack channel.Note: the CHIP-Tool reference client is generated from XML
If applicable, add tests:
For relatively simple tests, add YAML tests here: src/app/tests/suites/certification. Remember to update this file: src/app/tests/suites/certification/PICS.yaml
For more complex tests, add Python tests here: src/python_testing
To add tests to CI, if applicable:
Add the Python tests here: .github/workflows/tests.yaml. Remember to provide all arguments required for each Python script, such as the endpoint PIXIT.
Add the YAML tests by editing this file: src/app/tests/suites/ciTests.json
Create a section (“MyDevice”) which lists all YAML tests for your device
Add the section’s name to the list of items named “collection”
Do a ZAP code regen:
./scripts/tools/zap_regen_all.py
.
Add the device type spec to the test plan tools:
The file above is used by src/app/tests/suites/certification/Test_TC_DESC_2_1.yaml
Note: the plan is to make the DM tools generate the device type requirements data based on the spec, so the above will become obsolete.
Add the device type to Chef: examples/chef/devices
Q & A#
Q1: What kind of devices can be used for the test events? Can one of them be the example cluster server app running on a RasPI? Do the independent realizations need to run on vendor hardware or can they also run on generic hardware, such as ESP32 or RasPI?
A1: one realization can be the test harness + the all clusters example app + RasPI. the two independent realizations need to run on target hardware, which may be mock-ups, prototypes etc
Q2: How can the Chef tool be used for any of the deliverables above?
A2: TBD
Q3: What is the process for using the Zap tool in order to auto-generate code and to commit the results to the git repo?
A3: Search for zap_regen above. Add all the changed files to the repo after.
Q4: Where can the older cluster definitions be found?
A4: src/app/zap-templates/zcl/data-model/silabs/general.xml
Q5: Where can I find ZAP documentation?
A5: https://github.com/project-chip/zap/blob/master/README.md