15
15
#include " cluster_revision_table.hpp"
16
16
#include " sl_log.h"
17
17
#include < attribute_state_cache.hpp>
18
+
18
19
#define LOG_TAG " cluster_emulator"
19
20
20
21
namespace unify ::matter_bridge {
@@ -26,7 +27,7 @@ namespace unify::matter_bridge {
26
27
using namespace chip ::app;
27
28
using namespace chip ::app::Clusters;
28
29
29
- static uint32_t read_cluster_revision (const ConcreteReadAttributePath & aPath)
30
+ uint32_t ClusterEmulator:: read_cluster_revision (const ConcreteReadAttributePath & aPath) const
30
31
{
31
32
if (zap_cluster_revisions.count (aPath.mClusterId ))
32
33
{
@@ -38,8 +39,9 @@ static uint32_t read_cluster_revision(const ConcreteReadAttributePath & aPath)
38
39
}
39
40
}
40
41
41
- static uint32_t read_feature_map_revision (const ConcreteReadAttributePath & aPath)
42
+ uint32_t ClusterEmulator:: read_feature_map_revision (const ConcreteReadAttributePath & aPath) const
42
43
{
44
+ sl_log_debug (LOG_TAG, " Reading feature map for cluster %d" , aPath.mClusterId );
43
45
switch (aPath.mClusterId )
44
46
{
45
47
case ColorControl::Id: {
@@ -57,6 +59,7 @@ static uint32_t read_feature_map_revision(const ConcreteReadAttributePath & aPat
57
59
}
58
60
break ;
59
61
case OnOff::Id:
62
+ sl_log_debug (LOG_TAG, " Returning feature map for OnOff %d" , ON_OFF_LIGHTING_FEATURE_MAP_MASK);
60
63
return ON_OFF_LIGHTING_FEATURE_MAP_MASK;
61
64
case LevelControl::Id:
62
65
// / Check if OnOff is supported
@@ -70,19 +73,33 @@ static uint32_t read_feature_map_revision(const ConcreteReadAttributePath & aPat
70
73
}
71
74
72
75
void ClusterEmulator::add_emulated_commands_and_attributes (
73
- const std::unordered_map<std::string, node_state_monitor::cluster> & unify_clusters ,
74
- matter_cluster_builder & cluster_builder) const
76
+ const node_state_monitor::cluster & unify_cluster ,
77
+ matter_cluster_builder & cluster_builder)
75
78
{
76
79
// We always need to add the feature map and cluster
77
80
cluster_builder.attributes .push_back (EmberAfAttributeMetadata{ ZCL_FEATURE_MAP_SERVER_ATTRIBUTE_ID, ZCL_BITMAP32_ATTRIBUTE_TYPE,
78
81
4 , ZAP_ATTRIBUTE_MASK (EXTERNAL_STORAGE), ZAP_EMPTY_DEFAULT () });
79
82
cluster_builder.attributes .push_back (EmberAfAttributeMetadata{ ZCL_CLUSTER_REVISION_SERVER_ATTRIBUTE_ID,
80
83
ZCL_INT16U_ATTRIBUTE_TYPE, 2 ,
81
84
ZAP_ATTRIBUTE_MASK (EXTERNAL_STORAGE), ZAP_EMPTY_DEFAULT () });
85
+
86
+ // Add emulation for commands and attributes for the cluster
87
+ auto it = cluster_emulators_string_map.find (unify_cluster.cluster_name );
88
+ if (it != cluster_emulators_string_map.end ()) {
89
+ auto emulated_result = it->second ->emulate (unify_cluster, cluster_builder, cluster_emulators_attribute_id_map, cluster_emulators_command_id_map);
90
+ if (emulated_result != CHIP_NO_ERROR) {
91
+ sl_log_error (LOG_TAG, " Failed to add emulated commands and attributes for cluster %s" , unify_cluster.cluster_name .c_str ());
92
+ }
93
+ }
82
94
}
83
95
84
- bool ClusterEmulator::is_command_emulated (const ConcreteCommandPath &) const
85
- {
96
+ bool ClusterEmulator::is_command_emulated (const ConcreteCommandPath & cPath) const
97
+ {
98
+ auto it = cluster_emulators_command_id_map.find (cPath.mClusterId );
99
+ if (it != cluster_emulators_command_id_map.end ())
100
+ {
101
+ return it->second .find (cPath.mCommandId ) != it->second .end ();
102
+ }
86
103
return false ;
87
104
}
88
105
@@ -99,33 +116,56 @@ bool ClusterEmulator::is_attribute_emulated(const ConcreteAttributePath & aPath)
99
116
;
100
117
}
101
118
119
+ auto it = cluster_emulators_attribute_id_map.find (aPath.mClusterId );
120
+ if (it != cluster_emulators_attribute_id_map.end ())
121
+ {
122
+ return it->second .find (aPath.mAttributeId ) != it->second .end ();
123
+ }
124
+
102
125
return false ;
103
126
}
104
127
105
128
CHIP_ERROR ClusterEmulator::invoke_command (CommandHandlerInterface::HandlerContext & handlerContext) const
106
129
{
130
+ if (is_command_emulated (handlerContext.mRequestPath ))
131
+ {
132
+ return cluster_emulators_command_id_map.at (handlerContext.mRequestPath .mClusterId ).at (handlerContext.mRequestPath .mCommandId )->command (handlerContext);
133
+ }
134
+
107
135
return CHIP_ERROR_NOT_IMPLEMENTED;
108
136
}
109
137
110
- CHIP_ERROR ClusterEmulator::read_attribute (const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) const
138
+ CHIP_ERROR ClusterEmulator::read_attribute (const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
111
139
{
140
+ sl_log_debug (LOG_TAG, " Reading attribute %d" , aPath.mAttributeId );
112
141
switch (aPath.mAttributeId )
113
142
{
114
- case 0xFFFD : // Cluster Revision
115
- return aEncoder.Encode (read_cluster_revision (aPath));
116
- case 0xFFFC : // FeatureMap
117
- return aEncoder.Encode (read_feature_map_revision (aPath));
143
+ case ZCL_CLUSTER_REVISION_SERVER_ATTRIBUTE_ID : // Cluster Revision
144
+ return aEncoder.Encode (this -> read_cluster_revision (aPath));
145
+ case ZCL_FEATURE_MAP_SERVER_ATTRIBUTE_ID : // FeatureMap
146
+ return aEncoder.Encode (this -> read_feature_map_revision (aPath));
118
147
case 0xFFFE : // EventList
119
148
return aEncoder.Encode (0 );
120
149
default :;
121
150
;
122
151
}
152
+
153
+ sl_log_debug (LOG_TAG, " Cluster specific attribute emulation attribute id %d for cluster id" , aPath.mAttributeId , aPath.mClusterId );
154
+ if (is_attribute_emulated (aPath))
155
+ {
156
+ return cluster_emulators_attribute_id_map.at (aPath.mClusterId ).at (aPath.mAttributeId )->read_attribute (aPath, aEncoder);
157
+ }
123
158
124
159
return CHIP_ERROR_INVALID_ARGUMENT;
125
160
}
126
161
127
162
CHIP_ERROR ClusterEmulator::write_attribute (const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder) const
128
163
{
164
+ if (is_attribute_emulated (aPath))
165
+ {
166
+ return cluster_emulators_attribute_id_map.at (aPath.mClusterId ).at (aPath.mAttributeId )->write_attribute (aPath, aDecoder);
167
+ }
168
+
129
169
return CHIP_ERROR_NOT_IMPLEMENTED;
130
170
}
131
171
} // namespace unify::matter_bridge
0 commit comments