nRF Connect SDK API 3.3.99
Loading...
Searching...
No Matches
matter_bridged_device.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
7#pragma once
8
9#include "app/server-cluster/ServerClusterInterfaceRegistry.h"
10#include "data-model-providers/codegen/CodegenDataModelProvider.h"
11#include <app-common/zap-generated/ids/Attributes.h>
12#include <app-common/zap-generated/ids/Clusters.h>
13#include <app/AttributeAccessInterfaceRegistry.h>
14#include <app/CommandHandlerInterfaceRegistry.h>
15#include <app/DefaultTimerDelegate.h>
16#include <app/clusters/identify-server/IdentifyCluster.h>
17#include <app/util/attribute-storage.h>
18#include <platform/ConfigurationManager.h>
19
20namespace Nrf
21{
22
23/* Definitions of helper macros that are used across all bridged device types to create common mandatory clusters in a
24 * consistent way. */
25/* Declare Descriptor cluster attributes */
26#define DESCRIPTOR_CLUSTER_ATTRIBUTES(descriptorAttrs) \
27 DECLARE_DYNAMIC_ATTRIBUTE_LIST_BEGIN(descriptorAttrs) \
28 DECLARE_DYNAMIC_ATTRIBUTE(chip::app::Clusters::Descriptor::Attributes::DeviceTypeList::Id, ARRAY, \
29 Nrf::MatterBridgedDevice::kDescriptorAttributeArraySize, 0), /* device list */ \
30 DECLARE_DYNAMIC_ATTRIBUTE(chip::app::Clusters::Descriptor::Attributes::ServerList::Id, ARRAY, \
31 Nrf::MatterBridgedDevice::kDescriptorAttributeArraySize, 0), /* server list \
32 */ \
33 DECLARE_DYNAMIC_ATTRIBUTE(chip::app::Clusters::Descriptor::Attributes::ClientList::Id, ARRAY, \
34 Nrf::MatterBridgedDevice::kDescriptorAttributeArraySize, 0), /* client list \
35 */ \
36 DECLARE_DYNAMIC_ATTRIBUTE(chip::app::Clusters::Descriptor::Attributes::PartsList::Id, ARRAY, \
37 Nrf::MatterBridgedDevice::kDescriptorAttributeArraySize, 0), /* parts list \
38 */ \
39 DECLARE_DYNAMIC_ATTRIBUTE(chip::app::Clusters::Descriptor::Attributes::FeatureMap::Id, BITMAP32, 4, \
40 0), /* feature map */ \
41 DECLARE_DYNAMIC_ATTRIBUTE_LIST_END();
42
43/* Declare Bridged Device Basic Information cluster attributes */
44#define BRIDGED_DEVICE_BASIC_INFORMATION_CLUSTER_ATTRIBUTES(bridgedDeviceBasicAttrs) \
45 DECLARE_DYNAMIC_ATTRIBUTE_LIST_BEGIN(bridgedDeviceBasicAttrs) \
46 DECLARE_DYNAMIC_ATTRIBUTE(chip::app::Clusters::BridgedDeviceBasicInformation::Attributes::NodeLabel::Id, \
47 CHAR_STRING, Nrf::MatterBridgedDevice::kNodeLabelSize, \
48 ZAP_ATTRIBUTE_MASK(WRITABLE)), /* NodeLabel */ \
49 DECLARE_DYNAMIC_ATTRIBUTE( \
50 chip::app::Clusters::BridgedDeviceBasicInformation::Attributes::Reachable::Id, BOOLEAN, 1, \
51 0), /* Reachable */ \
52 DECLARE_DYNAMIC_ATTRIBUTE( \
53 chip::app::Clusters::BridgedDeviceBasicInformation::Attributes::UniqueID::Id, CHAR_STRING, 32, \
54 0), /* UniqueID */ \
55 DECLARE_DYNAMIC_ATTRIBUTE( \
56 chip::app::Clusters::BridgedDeviceBasicInformation::Attributes::FeatureMap::Id, BITMAP32, 4, \
57 0), /* feature map */ \
58 DECLARE_DYNAMIC_ATTRIBUTE_LIST_END();
59
60/* Codedriven delegate for the Identify cluster */
61class IdentifyBridgedDeviceDelegateImpl : public chip::app::Clusters::IdentifyDelegate {
62public:
63 void OnIdentifyStart(chip::app::Clusters::IdentifyCluster &cluster) override
64 {
65 ChipLogError(DeviceLayer, "Starting bridged device identify on endpoint %d",
66 cluster.GetPaths()[0].mEndpointId);
67 }
68
69 void OnIdentifyStop(chip::app::Clusters::IdentifyCluster &cluster) override
70 {
71 ChipLogError(DeviceLayer, "Stopping bridged device identify on endpoint %d",
72 cluster.GetPaths()[0].mEndpointId);
73 mIsTriggerEffectEnabled = false;
74 }
75
76 void OnTriggerEffect(chip::app::Clusters::IdentifyCluster &cluster) override
77 {
78 ChipLogError(DeviceLayer, "Triggering effect on endpoint %d", cluster.GetPaths()[0].mEndpointId);
79 mIsTriggerEffectEnabled = true;
80 }
81
82 bool IsTriggerEffectEnabled() const override { return mIsTriggerEffectEnabled; }
83
84private:
85 bool mIsTriggerEffectEnabled = false;
86};
89public:
90 enum DeviceType : uint16_t {
91 BridgedNode = 0x0013,
92 OnOffLight = 0x0100,
93 OnOffLightSwitch = 0x0103,
97 };
98 using IdentifyType = chip::app::Clusters::Identify::IdentifyTypeEnum;
99 static constexpr uint8_t kDefaultDynamicEndpointVersion = 3;
100 static constexpr uint8_t kNodeLabelSize = 32;
101 static constexpr uint8_t kUniqueIDSize = chip::DeviceLayer::ConfigurationManager::kMaxUniqueIDLength;
102 static constexpr uint8_t kDescriptorAttributeArraySize = 254;
103
104 explicit MatterBridgedDevice(const char *uniqueID, const char *nodeLabel)
105 {
106 if (uniqueID) {
107 memcpy(mUniqueID, uniqueID, strnlen(uniqueID, Nrf::MatterBridgedDevice::kUniqueIDSize));
108 }
109 if (nodeLabel) {
110 memcpy(mNodeLabel, nodeLabel, strnlen(nodeLabel, Nrf::MatterBridgedDevice::kNodeLabelSize));
111 }
112 }
113 virtual ~MatterBridgedDevice()
114 {
115 if (mIdentifyCluster.IsConstructed()) {
116 chip::app::CodegenDataModelProvider::Instance().Registry().Unregister(
117 &mIdentifyCluster.Cluster());
118 mIdentifyCluster.Destroy();
119 }
120
121 chip::Platform::MemoryFree(mDataVersion);
122 }
123
124 void Init(chip::EndpointId endpoint)
125 {
126 mEndpointId = endpoint;
127
128 mIdentifyCluster.Create(
129 chip::app::Clusters::IdentifyCluster::Config(mEndpointId, mTimerDelegate)
130 .WithIdentifyType(chip::app::Clusters::Identify::IdentifyTypeEnum::kVisibleIndicator)
131 .WithDelegate(&mIdentifyDelegate));
132
133 CHIP_ERROR err = chip::app::CodegenDataModelProvider::Instance().Registry().Register(
134 mIdentifyCluster.Registration());
135 if (err != CHIP_NO_ERROR) {
136 ChipLogError(DeviceLayer, "Failed to register Identify cluster: %s", ErrorStr(err));
138 }
140 chip::EndpointId GetEndpointId() const { return mEndpointId; }
141
142 virtual uint16_t GetDeviceType() const = 0;
143 virtual CHIP_ERROR HandleRead(chip::ClusterId clusterId, chip::AttributeId attributeId, uint8_t *buffer,
144 uint16_t maxReadLength) = 0;
145 virtual CHIP_ERROR HandleWrite(chip::ClusterId clusterId, chip::AttributeId attributeId, uint8_t *buffer,
146 size_t size) = 0;
147 virtual CHIP_ERROR HandleAttributeChange(chip::ClusterId clusterId, chip::AttributeId attributeId, void *data,
148 size_t dataSize) = 0;
149
150 CHIP_ERROR CopyAttribute(const void *attribute, size_t attributeSize, void *buffer, uint16_t maxBufferSize);
151 CHIP_ERROR HandleWriteDeviceBasicInformation(chip::ClusterId clusterId, chip::AttributeId attributeId,
152 void *data, size_t dataSize);
153 CHIP_ERROR HandleReadBridgedDeviceBasicInformation(chip::AttributeId attributeId, uint8_t *buffer,
154 uint16_t maxReadLength);
155
156 CHIP_ERROR HandleReadIdentify(chip::AttributeId attributeId, uint8_t *buffer, uint16_t maxReadLength);
157 CHIP_ERROR HandleWriteIdentify(chip::AttributeId attributeId, void *data, size_t dataSize);
159 bool GetIsReachable() const { return mIsReachable; }
160 const char *GetUniqueID() const { return mUniqueID; }
161 const char *GetNodeLabel() const { return mNodeLabel; }
162 static constexpr uint16_t GetBridgedDeviceBasicInformationClusterRevision() { return 4; }
163 static constexpr uint32_t GetBridgedDeviceBasicInformationFeatureMap() { return 0; }
164 static constexpr uint16_t GetIdentifyClusterRevision() { return 6; }
165 static constexpr uint32_t GetIdentifyClusterFeatureMap() { return 0; }
167 EmberAfEndpointType *mEp;
168 const EmberAfDeviceType *mDeviceTypeList;
169 size_t mDeviceTypeListSize;
170 chip::DataVersion *mDataVersion;
173protected:
174 void SetIsReachable(bool isReachable) { mIsReachable = isReachable; }
175 void SetNodeLabel(void *data, size_t size);
176
177 chip::EndpointId mEndpointId{};
178
179private:
180 bool mIsReachable = true;
181 char mUniqueID[kUniqueIDSize] = "";
182 char mNodeLabel[kNodeLabelSize] = "";
183 chip::app::LazyRegisteredServerCluster<chip::app::Clusters::IdentifyCluster> mIdentifyCluster;
184 chip::app::DefaultTimerDelegate mTimerDelegate;
185 IdentifyBridgedDeviceDelegateImpl mIdentifyDelegate;
186};
187
188} /* namespace Nrf */
void OnTriggerEffect(chip::app::Clusters::IdentifyCluster &cluster) override
Definition matter_bridged_device.h:73
void OnIdentifyStop(chip::app::Clusters::IdentifyCluster &cluster) override
Definition matter_bridged_device.h:66
void OnIdentifyStart(chip::app::Clusters::IdentifyCluster &cluster) override
Definition matter_bridged_device.h:60
bool IsTriggerEffectEnabled() const override
Definition matter_bridged_device.h:79
Definition matter_bridged_device.h:58
static constexpr uint16_t GetBridgedDeviceBasicInformationClusterRevision()
Definition matter_bridged_device.h:159
void Init(chip::EndpointId endpoint)
Definition matter_bridged_device.h:121
virtual uint16_t GetDeviceType() const =0
CHIP_ERROR HandleReadBridgedDeviceBasicInformation(chip::AttributeId attributeId, uint8_t *buffer, uint16_t maxReadLength)
virtual CHIP_ERROR HandleWrite(chip::ClusterId clusterId, chip::AttributeId attributeId, uint8_t *buffer, size_t size)=0
static constexpr uint8_t kNodeLabelSize
Definition matter_bridged_device.h:97
bool GetIsReachable() const
Definition matter_bridged_device.h:156
static constexpr uint8_t kDefaultDynamicEndpointVersion
Definition matter_bridged_device.h:96
static constexpr uint32_t GetIdentifyClusterFeatureMap()
Definition matter_bridged_device.h:162
const char * GetNodeLabel() const
Definition matter_bridged_device.h:158
virtual CHIP_ERROR HandleRead(chip::ClusterId clusterId, chip::AttributeId attributeId, uint8_t *buffer, uint16_t maxReadLength)=0
size_t mDataVersionSize
Definition matter_bridged_device.h:168
chip::EndpointId GetEndpointId() const
Definition matter_bridged_device.h:137
CHIP_ERROR HandleWriteDeviceBasicInformation(chip::ClusterId clusterId, chip::AttributeId attributeId, void *data, size_t dataSize)
const char * GetUniqueID() const
Definition matter_bridged_device.h:157
virtual CHIP_ERROR HandleAttributeChange(chip::ClusterId clusterId, chip::AttributeId attributeId, void *data, size_t dataSize)=0
MatterBridgedDevice(const char *uniqueID, const char *nodeLabel)
Definition matter_bridged_device.h:101
chip::app::Clusters::Identify::IdentifyTypeEnum IdentifyType
Definition matter_bridged_device.h:95
virtual ~MatterBridgedDevice()
Definition matter_bridged_device.h:110
chip::EndpointId mEndpointId
Definition matter_bridged_device.h:174
EmberAfEndpointType * mEp
Definition matter_bridged_device.h:164
CHIP_ERROR HandleWriteIdentify(chip::AttributeId attributeId, void *data, size_t dataSize)
chip::DataVersion * mDataVersion
Definition matter_bridged_device.h:167
CHIP_ERROR HandleReadIdentify(chip::AttributeId attributeId, uint8_t *buffer, uint16_t maxReadLength)
static constexpr uint32_t GetBridgedDeviceBasicInformationFeatureMap()
Definition matter_bridged_device.h:160
const EmberAfDeviceType * mDeviceTypeList
Definition matter_bridged_device.h:165
static constexpr uint16_t GetIdentifyClusterRevision()
Definition matter_bridged_device.h:161
CHIP_ERROR CopyAttribute(const void *attribute, size_t attributeSize, void *buffer, uint16_t maxBufferSize)
DeviceType
Definition matter_bridged_device.h:87
@ TemperatureSensor
Definition matter_bridged_device.h:91
@ HumiditySensor
Definition matter_bridged_device.h:92
@ OnOffLightSwitch
Definition matter_bridged_device.h:90
@ GenericSwitch
Definition matter_bridged_device.h:93
@ BridgedNode
Definition matter_bridged_device.h:88
@ OnOffLight
Definition matter_bridged_device.h:89
void SetNodeLabel(void *data, size_t size)
void SetIsReachable(bool isReachable)
Definition matter_bridged_device.h:171
size_t mDeviceTypeListSize
Definition matter_bridged_device.h:166
static constexpr uint8_t kUniqueIDSize
Definition matter_bridged_device.h:98
static constexpr uint8_t kDescriptorAttributeArraySize
Definition matter_bridged_device.h:99
Definition matter_bridged_device.h:85
Definition ble_connectivity_manager.h:21