Skip to content

Commit bd80505

Browse files
samples: matter: Added custom events to the Matter MS sample
Added custom events to NordicDevKit and Basic Information clusters: * UserButtonChanged emitted on UserButton state change * RandomNumberChanged emitted on RandomNumber change Updated the documentation to contain steps for the events. Additionally fixed ids for attributes and commands that were not compatible with the spec and fixed few minor issues in the documentation. Signed-off-by: Kamil Kasperczyk <kamil.kasperczyk@nordicsemi.no>
1 parent 385e5bf commit bd80505

File tree

13 files changed

+776
-532
lines changed

13 files changed

+776
-532
lines changed

doc/nrf/protocols/matter/getting_started/custom_clusters.rst

+11-9
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ See the description of each element in the following tabs:
108108
<code>0xFFF1FC01</code>
109109
<define>MY_NEW_CLUSTER</define>
110110
<description>The MyNewCluster cluster showcases a cluster manufacturer extensions</description>
111-
<attribute side="server" code="0x00" define="MY_ATTRIBUTE" type="boolean" writable="true" default="false" optional="false">MyAttribute</attribute>
112-
<command source="client" code="0x02" name="MyCommand" response="MyCommandResponse" optional="false">
111+
<attribute side="server" code="0xFFF10000" define="MY_ATTRIBUTE" type="boolean" writable="true" default="false" optional="false">MyAttribute</attribute>
112+
<command source="client" code="0xFFF10000" name="MyCommand" response="MyCommandResponse" optional="false">
113113
<description>Command that takes two uint8 arguments and returns their sum.</description>
114114
<arg name="arg1" type="int8u"/>
115115
<arg name="arg2" type="int8u"/>
116116
</command>
117-
<event source="server" code="0x01" name="MyEvent" optional="false">
117+
<event source="server" code="0xFFF10000" name="MyEvent" priority="info" optional="false">
118118
<description>Event that is generated by the server.</description>
119119
<arg name="arg1" type="int8u"/>
120120
</event>
@@ -153,6 +153,8 @@ See the description of each element in the following tabs:
153153
* ``source`` - Specifies whether the event originates from the client or server.
154154
* ``code`` - A unique identifier for the event within the cluster.
155155
* ``name`` - The name of the event.
156+
* ``priority`` - The priority of the event.
157+
The valid values are ``debug``, ``info``, and ``critical``.
156158
* ``optional`` - Indicates whether the event is optional.
157159
* ``description`` - A brief description of the event's purpose and functionality.
158160
* ``arg`` - An argument for the event, specifying its name and type.
@@ -169,7 +171,7 @@ See the description of each element in the following tabs:
169171
<arg name="arg1" type="int8u"/>
170172
<arg name="arg2" type="int8u"/>
171173
</command>
172-
<event source="server" code="0x04" name="ExtendedEvent" optional="false">
174+
<event source="server" code="0x04" name="ExtendedEvent" priority="info" optional="false">
173175
<description>Event that is generated by the server.</description>
174176
<arg name="arg1" type="int8u"/>
175177
</event>
@@ -297,13 +299,13 @@ You can use the following template for the :file:`MyCluster.xml` file:
297299
<code>0xFFF1FC01</code>
298300
<define>MY_NEW_CLUSTER</define>
299301
<description>The MyNewCluster cluster showcases a cluster manufacturer extensions</description>
300-
<attribute side="server" code="0x00" define="MY_ATTRIBUTE" type="boolean" writable="true" default="false" optional="false">MyAttribute</attribute>
301-
<command source="client" code="0x02" name="MyCommand" optional="false">
302+
<attribute side="server" code="0xFFF10000" define="MY_ATTRIBUTE" type="boolean" writable="true" default="false" optional="false">MyAttribute</attribute>
303+
<command source="client" code="0xFFF10000" name="MyCommand" optional="false">
302304
<description>Command that takes two uint8 arguments and returns their sum.</description>
303305
<arg name="arg1" type="int8u"/>
304306
<arg name="arg2" type="int8u"/>
305307
</command>
306-
<event source="server" code="0x01" name="MyEvent" optional="false">
308+
<event source="server" code="0xFFF10000" name="MyEvent" priority="info" optional="false">
307309
<description>Event that is generated by the server.</description>
308310
<arg name="arg1" type="int8u"/>
309311
</event>
@@ -315,7 +317,7 @@ You can use the following template for the :file:`MyCluster.xml` file:
315317
<arg name="arg1" type="int8u"/>
316318
<arg name="arg2" type="int8u"/>
317319
</command>
318-
<event source="server" code="0x04" name="ExtendedEvent" optional="false">
320+
<event source="server" code="0x04" name="ExtendedEvent" priority="info" optional="false">
319321
<description>Event that is generated by the server.</description>
320322
<arg name="arg1" type="int8u"/>
321323
</event>
@@ -361,7 +363,7 @@ This guide focuses on the :ref:`ug_matter_gs_tools_matter_west_commands_zap_tool
361363

362364
.. code-block::
363365
364-
west zap-gui -j ./zcl.json ./MyCluster.xml
366+
west zap-gui -j ./zcl.json --clusters ./MyCluster.xml
365367
366368
This example command copies the original :file:`<default Matter SDK location>/src/app/zap-templates/zcl/zcl.json` file, adds the :file:`MyCluster.xml` cluster, and saves the new :file:`zcl.json` file in the sample directory.
367369
The newly generated :file:`zcl.json` file is used as an input to the ZAP tool.

samples/matter/manufacturer_specific/README.rst

+32-17
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,33 @@ The development kits for this sample offer the following IPv6 network support fo
3737

3838
Overview
3939
********
40+
41+
The sample starts the Bluetooth® LE advertising automatically and prepares the Matter device for commissioning into a Matter-enabled IPv6 network.
42+
4043
.. tabs::
4144

4245
.. group-tab:: nRF52, nRF53 and nRF70 DKs
4346

44-
The sample starts the Bluetooth® LE advertising automatically and prepares the Matter device for commissioning into a Matter-enabled IPv6 network.
4547
The sample uses the **LED 1** to show the state of the connection.
4648
You can press **Button 1** to start the factory reset when needed.
4749
**Button 2** is used to set the state of the ``NordicDevkit`` cluster's attribute, ``UserButton``.
48-
Matter command ``SetLED`` is used to control the state of ``UserLED``.
49-
It takes one argument - the action to be performed (``0`` to turn the LED off, ``1`` to turn it on, ``2`` to toggle the state).
5050
**LED 2** reflects the state of the ``UserLED``.
51-
The ``NordicDevkit`` cluster introduces a writable ``DevKitName`` attribute, of string type as well.
52-
The sample additionally extends the ``Basic Information`` cluster with a ``RandomNumber`` attribute and ``GenerateRandom`` command that updates the ``RandomNumber`` with a random value.
5351

5452
.. group-tab:: nRF54 DKs
5553

56-
The sample starts the Bluetooth LE advertising automatically and prepares the Matter device for commissioning into a Matter-enabled IPv6 network.
5754
The sample uses the **LED 0** to show the state of the connection.
5855
You can press **Button 0** to start the factory reset when needed.
5956
**Button 1** is used to set the state of the ``NordicDevkit`` cluster's attribute, ``UserButton``.
60-
Matter command ``SetLED`` is used to control the state of ``UserLED``.
61-
It takes one argument - the action to be performed (``0`` to turn the LED off, ``1`` to turn it on, ``2`` to toggle the state).
6257
**LED 1** reflects the state of the ``UserLED``.
63-
The ``NordicDevkit`` cluster introduces a writable ``DevKitName`` attribute, of string type as well.
64-
The sample additionally extends the ``Basic Information`` cluster with a ``RandomNumber`` attribute and ``GenerateRandom`` command that updates the ``RandomNumber`` with a random value.
6558

59+
The Matter command ``SetLED`` is used to control the state of ``UserLED``.
60+
It takes one argument - the action to be performed (``0`` to turn the LED off, ``1`` to turn it on, ``2`` to toggle the state).
61+
The ``UserButtonChanged`` event is generated when the ``UserButton`` attribute is changed.
62+
63+
The ``NordicDevkit`` cluster introduces a writable ``DevKitName`` attribute, of string type as well.
64+
The sample additionally extends the ``Basic Information`` cluster with a ``RandomNumber`` attribute and ``GenerateRandom`` command that updates the ``RandomNumber`` with a random value.
65+
The ``RandomNumberChanged`` event is generated when the ``RandomNumber`` attribute is changed.
66+
The ``RandomNumber`` attribute value is not persistent and it is generated on each application's boot.
6667

6768
Custom manufacturer-specific cluster
6869
====================================
@@ -132,7 +133,7 @@ User interface
132133
.. include:: /includes/matter_sample_button.txt
133134

134135
Button 2:
135-
Sets the state of ``UserButton`` attribute in the ``NordicDevkit`` cluster.
136+
Sets the state of ``UserButton`` attribute in the ``NordicDevkit`` cluster to ``true`` on press and ``false`` on release.
136137

137138
.. include:: /includes/matter_segger_usb.txt
138139

@@ -148,7 +149,7 @@ User interface
148149
.. include:: /includes/matter_sample_button.txt
149150

150151
Button 1:
151-
Sets the state of ``UserButton`` attribute in the ``NordicDevkit`` cluster.
152+
Sets the state of ``UserButton`` attribute in the ``NordicDevkit`` cluster to ``true`` on press and ``false`` on release.
152153

153154
.. include:: /includes/matter_segger_usb.txt
154155

@@ -234,24 +235,24 @@ To test ``NordicDevkit`` cluster's attributes and commands, complete the followi
234235
.. parsed-literal::
235236
:class: highlight
236237
237-
any read-by-id read-by-id 0xFFF1FC01 *attribute-id* 1 1
238+
any read-by-id 0xFFF1FC01 *attribute-id* 1 1
238239
239-
* *attribute-id* is the attribute's ID, equal to ``1`` for ``DevKitName``, ``2`` for ``UserLED`` and ``3`` for ``UserButton`` attributes for the ``NordicDevkit`` cluster in this sample.
240+
* *attribute-id* is the attribute's ID, equal to ``0xFFF10000`` for ``DevKitName``, ``0xFFF10001`` for ``UserLED`` and ``0xFFF10002`` for ``UserButton`` attributes for the ``NordicDevkit`` cluster in this sample.
240241
#. Verify that all attributes have been read correctly and are equal to the default values defined in cluster's configuration.
241242
#. Write the ``DevkitName`` attribute:
242243

243244
.. parsed-literal::
244245
:class: highlight
245246
246-
any write-by-id 0xFFF1FC01 1 "NewName" 1 1
247+
any write-by-id 0xFFF1FC01 0xFFF10000 "NewName" 1 1
247248
248249
#. Read the ``DevkitName`` attribute again to check if it has changed.
249250
#. Send the ``SetLED`` command to the device to control the LED state:
250251

251252
.. parsed-literal::
252253
:class: highlight
253254
254-
any command-by-id 0xFFF1FC01 0 '{ "0x0": "u:*action*" }' 1 1
255+
any command-by-id 0xFFF1FC01 0xFFF10000 '{ "0x0": "u:*action*" }' 1 1
255256
256257
* *action* is the action that should be performed on LED attribute: ``0`` to turn the LED off, ``1`` to turn it on, ``2`` to toggle the state.
257258

@@ -261,9 +262,16 @@ To test ``NordicDevkit`` cluster's attributes and commands, complete the followi
261262
.. parsed-literal::
262263
:class: highlight
263264
264-
any subscribe-by-id 0xFFF1FC01 3 0 120 1 1
265+
any subscribe-by-id 0xFFF1FC01 0xFFF10002 0 120 1 1
265266
266267
#. Press the button assigned to the ``UserButton`` and check if the attribute state is updated in the chip-tool.
268+
#. Read the ``UserButtonChanged`` event to check that events were generated on ``UserButton`` attribute changes.
269+
270+
.. parsed-literal::
271+
:class: highlight
272+
273+
any read-event-by-id 0xFFF1FC01 0xFFF10000 1 1
274+
267275
#. Read the ``Basic Information`` cluster's ``RandomNumber`` attribute:
268276

269277
.. parsed-literal::
@@ -279,6 +287,13 @@ To test ``NordicDevkit`` cluster's attributes and commands, complete the followi
279287
any command-by-id 0x0028 0 '{}' 1 0
280288
281289
#. Verify that the random value has been generated and the attribute value is updated.
290+
#. Read the ``Basic Information`` cluster's ``RandomNumberChanged`` event to check that events were generated on ``RandomNumber`` attribute changes.
291+
292+
.. parsed-literal::
293+
:class: highlight
294+
295+
any read-event-by-id 0x0028 0x4 1 0
296+
282297
#. Reboot the device, restart the chip-tool, and check if the attributes are persisting after joining the network.
283298

284299
Upgrading the device firmware

samples/matter/manufacturer_specific/src/app_task.cpp

+28-6
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
#include <app-common/zap-generated/attributes/Accessors.h>
1616

17+
#include <app/EventLogging.h>
18+
#include <app/util/attribute-storage.h>
1719
#include <setup_payload/OnboardingCodesUtil.h>
1820

19-
#include <zephyr/random/random.h>
20-
#include <zephyr/logging/log.h>
2121
#include <dk_buttons_and_leds.h>
22+
#include <zephyr/logging/log.h>
23+
#include <zephyr/random/random.h>
2224

2325
LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);
2426

@@ -35,7 +37,7 @@ constexpr EndpointId kNordicDevKitEndpointId = 1;
3537

3638
static void ButtonEventHandler(Nrf::ButtonState /* unused */, Nrf::ButtonMask has_changed)
3739
{
38-
/* Handle button press */
40+
/* Handle button press */
3941
if (ConnectivityMgrImpl().IsIPv6NetworkProvisioned() && ConnectivityMgrImpl().IsIPv6NetworkEnabled() &&
4042
BUTTON2_MASK & has_changed) {
4143
AppTask::Instance().UpdateNordicDevkitClusterState();
@@ -50,19 +52,29 @@ void AppTask::UpdateNordicDevkitClusterState()
5052

5153
dk_read_buttons(&button_state, nullptr);
5254

53-
status = Clusters::NordicDevKit::Attributes::UserLED::Set(kNordicDevKitEndpointId,
54-
Nrf::GetBoard().GetLED(Nrf::DeviceLeds::LED2).GetState());
55+
status = Clusters::NordicDevKit::Attributes::UserLED::Set(
56+
kNordicDevKitEndpointId, Nrf::GetBoard().GetLED(Nrf::DeviceLeds::LED2).GetState());
5557

5658
if (status != Protocols::InteractionModel::Status::Success) {
5759
LOG_ERR("Updating NordicDevkit cluster failed: %x", to_underlying(status));
5860
}
5961

6062
status = Clusters::NordicDevKit::Attributes::UserButton::Set(kNordicDevKitEndpointId,
61-
BUTTON2_MASK & button_state);
63+
BUTTON2_MASK & button_state);
6264

6365
if (status != Protocols::InteractionModel::Status::Success) {
6466
LOG_ERR("Updating NordicDevkit cluster failed: %x", to_underlying(status));
6567
}
68+
69+
for (auto endpoint : EnabledEndpointsWithServerCluster(Clusters::NordicDevKit::Id)) {
70+
/* If NordicDevKit cluster is implemented on this endpoint */
71+
Clusters::NordicDevKit::Events::UserButtonChanged::Type event;
72+
EventNumber eventNumber;
73+
74+
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) {
75+
ChipLogError(Zcl, "Failed to emit UserButtonChanged event");
76+
}
77+
}
6678
});
6779
}
6880

@@ -77,6 +89,16 @@ void AppTask::UpdateBasicInformationClusterState()
7789
if (status != Protocols::InteractionModel::Status::Success) {
7890
LOG_ERR("Updating Basic information cluster failed: %x", to_underlying(status));
7991
}
92+
93+
for (auto endpoint : EnabledEndpointsWithServerCluster(Clusters::BasicInformation::Id)) {
94+
/* If Basic cluster is implemented on this endpoint */
95+
Clusters::BasicInformation::Events::RandomNumberChanged::Type event;
96+
EventNumber eventNumber;
97+
98+
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) {
99+
ChipLogError(Zcl, "Failed to emit RandomNumberChanged event");
100+
}
101+
}
80102
});
81103
}
82104

samples/matter/manufacturer_specific/src/default_zap/NordicDevKitCluster.xml

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,25 @@
1212
<code>0xFFF1FC01</code>
1313
<define>NORDIC_DEV_KIT_CLUSTER</define>
1414
<description>The NordicDevKit cluster showcases a cluster manufacturer extensions</description>
15-
<command source="client" code="0x00" name="SetLED" optional="false">
15+
<command source="client" code="0xFFF10000" name="SetLED" optional="false">
1616
<description>Change LED state</description>
1717
<arg name="Action" type="LEDActionEnum" optional="false"/>
1818
</command>
19-
<attribute side="server" code="0x01" define="DEV_KIT_NAME" type="char_string" writable="true" optional="false" default="Nordic Development Kit">DevKitName</attribute>
20-
<attribute side="server" code="0x02" define="USER_LED" type="boolean" length="0" writable="false" reportable="false" isNullable="false" default="false" optional="false" apiMaturity="NaN">UserLED<description/><access op="read" role="view"/></attribute>
21-
<attribute side="server" code="0x03" define="USER_BUTTON" type="boolean" length="0" writable="false" reportable="false" isNullable="false" default="false" optional="false" apiMaturity="NaN">UserButton<description/><access op="read" role="view"/></attribute>
19+
<attribute side="server" code="0xFFF10000" define="DEV_KIT_NAME" type="char_string" writable="true" optional="false" default="Nordic Development Kit">DevKitName</attribute>
20+
<attribute side="server" code="0xFFF10001" define="USER_LED" type="boolean" length="0" writable="false" reportable="false" isNullable="false" default="false" optional="false" apiMaturity="NaN">UserLED<description/><access op="read" role="view"/></attribute>
21+
<attribute side="server" code="0xFFF10002" define="USER_BUTTON" type="boolean" length="0" writable="false" reportable="false" isNullable="false" default="false" optional="false" apiMaturity="NaN">UserButton<description/><access op="read" role="view"/></attribute>
22+
<event side="server" code="0xFFF10000" name="UserButtonChanged" priority="info" optional="false">
23+
<description>The UserButtonChanged event SHALL be emitted by a Node as soon as the User Button is pressed or released.</description>
24+
</event>
2225
</cluster>
2326
<clusterExtension code="0x0028">
2427
<command source="client" code="0x00" name="GenerateRandom" optional="false">
2528
<description>Generate random number</description>
2629
</command>
2730
<attribute side="server" code="0x17" define="RANDOM_NUMBER" type="int16u" writable="true" optional="false" default="0">RandomNumber</attribute>
31+
<event side="server" code="0x4" name="RandomNumberChanged" priority="info" optional="true">
32+
<description>The RandomNumberChanged event SHALL be emitted by a Node as soon as the RandomNumber attribute is changed.</description>
33+
</event>
2834
</clusterExtension>
2935
<deviceType>
3036
<name>nordic-dev-kit</name>

0 commit comments

Comments
 (0)