-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathFakeAttributeAccess.cpp
More file actions
273 lines (248 loc) · 11.2 KB
/
FakeAttributeAccess.cpp
File metadata and controls
273 lines (248 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
*
* Copyright (c) 2026 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "FakeAttributeAccess.h"
#include <optional>
#include "pigweed/rpc_services/internal/StatusUtils.h"
#include <app/ConcreteAttributePath.h>
#include <app/data-model/Nullable.h>
#include <app/util/config.h>
#include <pigweed/rpc_services/AccessInterceptor.h>
#include <pigweed/rpc_services/AccessInterceptorRegistry.h>
#if MATTER_DM_TEMPERATURE_MEASUREMENT_CLUSTER_SERVER_ENDPOINT_COUNT > 0
#include <app/clusters/temperature-measurement-server/CodegenIntegration.h>
#endif
#if MATTER_DM_VALVE_CONFIGURATION_AND_CONTROL_CLUSTER_SERVER_ENDPOINT_COUNT > 0
#include <app/clusters/valve-configuration-and-control-server/CodegenIntegration.h>
#endif
#if MATTER_DM_ILLUMINANCE_MEASUREMENT_CLUSTER_SERVER_ENDPOINT_COUNT > 0
#include <app/clusters/illuminance-measurement-server/CodegenIntegration.h>
#endif
#if MATTER_DM_OCCUPANCY_SENSING_CLUSTER_SERVER_ENDPOINT_COUNT > 0
#include <app/clusters/occupancy-sensor-server/CodegenIntegration.h>
#endif
namespace chip {
namespace app {
namespace Clusters {
namespace Chef {
/**
* Once clusters are converted to code driven, fake attribute access through datamodel provider
* will be blocked. Therefore we make this attribute accessor to allow fake attribute access for
* code driven apps.
*/
class AttributeAccessor : public chip::rpc::PigweedDebugAccessInterceptor
{
public:
std::optional<::pw::Status> Write(const ConcreteDataAttributePath & path, AttributeValueDecoder & decoder) override
{
ChipLogProgress(Zcl, "Inside AttributeAccessor::Write for Cluster: " ChipLogFormatMEI " , Attribute: " ChipLogFormatMEI,
ChipLogValueMEI(path.mClusterId), ChipLogValueMEI(path.mAttributeId));
switch (path.mClusterId)
{
#if MATTER_DM_TEMPERATURE_MEASUREMENT_CLUSTER_SERVER_ENDPOINT_COUNT > 0
case TemperatureMeasurement::Id:
switch (path.mAttributeId)
{
case TemperatureMeasurement::Attributes::MeasuredValue::Id: {
DataModel::Nullable<int16_t> measuredValue;
CHIP_ERROR err = decoder.Decode(measuredValue);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "[Pw] Failed to decode measuredValue: %" CHIP_ERROR_FORMAT, err.Format());
return ::pw::Status::Internal();
}
err = TemperatureMeasurement::SetMeasuredValue(path.mEndpointId, measuredValue);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "[Pw] Failed to set measuredValue: %" CHIP_ERROR_FORMAT, err.Format());
return ::pw::Status::Internal();
}
if (measuredValue.IsNull())
{
ChipLogProgress(Zcl, "[Pw] Successfully set measuredValue to null.");
}
else
{
ChipLogProgress(Zcl, "[Pw] Successfully set measuredValue to %d.", measuredValue.Value());
}
return ::pw::OkStatus();
}
case TemperatureMeasurement::Attributes::MinMeasuredValue::Id:
case TemperatureMeasurement::Attributes::MaxMeasuredValue::Id: {
auto temperatureMeasurement = TemperatureMeasurement::FindClusterOnEndpoint(path.mEndpointId);
if (temperatureMeasurement == nullptr)
{
return ::pw::Status::Internal();
}
DataModel::Nullable<int16_t> value;
CHIP_ERROR err = decoder.Decode(value);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "[Pw] Failed to decode measured value: %" CHIP_ERROR_FORMAT, err.Format());
return ::pw::Status::Internal();
}
DataModel::Nullable<int16_t> min;
DataModel::Nullable<int16_t> max;
if (path.mAttributeId == TemperatureMeasurement::Attributes::MinMeasuredValue::Id)
{
min = value;
max = temperatureMeasurement->GetMaxMeasuredValue();
}
else
{
min = temperatureMeasurement->GetMinMeasuredValue();
max = value;
}
err = TemperatureMeasurement::SetMeasuredValueRange(path.mEndpointId, min, max);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "[Pw] Failed to set measured value range: %" CHIP_ERROR_FORMAT, err.Format());
return ::pw::Status::Internal();
}
return ::pw::OkStatus();
}
}
break;
#endif // MATTER_DM_TEMPERATURE_MEASUREMENT_CLUSTER_SERVER_ENDPOINT_COUNT > 0
#if MATTER_DM_VALVE_CONFIGURATION_AND_CONTROL_CLUSTER_SERVER_ENDPOINT_COUNT > 0
case ValveConfigurationAndControl::Id:
switch (path.mAttributeId)
{
case ValveConfigurationAndControl::Attributes::CurrentLevel::Id:
Percent level;
CHIP_ERROR err = decoder.Decode(level);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "[Pw] Failed to decode currentLevel: %" CHIP_ERROR_FORMAT, err.Format());
return ::pw::Status::Internal();
}
err = ValveConfigurationAndControl::UpdateCurrentLevel(path.mEndpointId, level);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "[Pw] Failed to update currentLevel: %" CHIP_ERROR_FORMAT, err.Format());
return ::pw::Status::Internal();
}
ChipLogProgress(Zcl, "[Pw] Successfully set current level to " ChipLogFormatMEI ".", ChipLogValueMEI(level));
return ::pw::OkStatus();
}
break;
#endif // MATTER_DM_VALVE_CONFIGURATION_AND_CONTROL_CLUSTER_SERVER_ENDPOINT_COUNT > 0
#if MATTER_DM_ILLUMINANCE_MEASUREMENT_CLUSTER_SERVER_ENDPOINT_COUNT > 0
case IlluminanceMeasurement::Id:
switch (path.mAttributeId)
{
case IlluminanceMeasurement::Attributes::MeasuredValue::Id: {
DataModel::Nullable<uint16_t> measuredValue;
CHIP_ERROR err = decoder.Decode(measuredValue);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "[Pw] Failed to decode measuredValue: %" CHIP_ERROR_FORMAT, err.Format());
return ::pw::Status::Internal();
}
err = IlluminanceMeasurement::SetMeasuredValue(path.mEndpointId, measuredValue);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "[Pw] Failed to set measuredValue: %" CHIP_ERROR_FORMAT, err.Format());
return ::pw::Status::Internal();
}
if (measuredValue.IsNull())
{
ChipLogProgress(Zcl, "[Pw] Successfully set measuredValue to null.");
}
else
{
ChipLogProgress(Zcl, "[Pw] Successfully set measuredValue to %u.", measuredValue.Value());
}
return ::pw::OkStatus();
}
case IlluminanceMeasurement::Attributes::MinMeasuredValue::Id:
case IlluminanceMeasurement::Attributes::MaxMeasuredValue::Id: {
auto illuminanceMeasurement = IlluminanceMeasurement::FindClusterOnEndpoint(path.mEndpointId);
if (illuminanceMeasurement == nullptr)
{
return ::pw::Status::Internal();
}
DataModel::Nullable<uint16_t> value;
CHIP_ERROR err = decoder.Decode(value);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "[Pw] Failed to decode measured value: %" CHIP_ERROR_FORMAT, err.Format());
return ::pw::Status::Internal();
}
DataModel::Nullable<uint16_t> min;
DataModel::Nullable<uint16_t> max;
if (path.mAttributeId == IlluminanceMeasurement::Attributes::MinMeasuredValue::Id)
{
min = value;
max = illuminanceMeasurement->GetMaxMeasuredValue();
}
else
{
min = illuminanceMeasurement->GetMinMeasuredValue();
max = value;
}
err = IlluminanceMeasurement::SetMeasuredValueRange(path.mEndpointId, min, max);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "[Pw] Failed to set measured value range: %" CHIP_ERROR_FORMAT, err.Format());
return ::pw::Status::Internal();
}
return ::pw::OkStatus();
}
}
break;
#endif // MATTER_DM_ILLUMINANCE_MEASUREMENT_CLUSTER_SERVER_ENDPOINT_COUNT > 0
#if MATTER_DM_OCCUPANCY_SENSING_CLUSTER_SERVER_ENDPOINT_COUNT > 0
case OccupancySensing::Id:
switch (path.mAttributeId)
{
case OccupancySensing::Attributes::Occupancy::Id: {
BitMask<OccupancySensing::OccupancyBitmap> occupancy;
CHIP_ERROR err = decoder.Decode(occupancy);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "[Pw] Failed to decode occupancy: %" CHIP_ERROR_FORMAT, err.Format());
return ::pw::Status::Internal();
}
auto occupancySensing = OccupancySensing::FindClusterOnEndpoint(path.mEndpointId);
if (occupancySensing == nullptr)
{
return ::pw::Status::Internal();
}
occupancySensing->SetOccupancy(occupancy.Has(OccupancySensing::OccupancyBitmap::kOccupied));
ChipLogProgress(Zcl, "[Pw] Successfully set occupancy to %d.",
occupancy.Has(OccupancySensing::OccupancyBitmap::kOccupied));
return ::pw::OkStatus();
}
}
break;
#endif // MATTER_DM_OCCUPANCY_SENSING_CLUSTER_SERVER_ENDPOINT_COUNT > 0
}
return std::nullopt;
}
};
namespace {
static AttributeAccessor gAttributeAccessor;
} // namespace
void RegisterAttributeAccessor()
{
chip::rpc::PigweedDebugAccessInterceptorRegistry::Instance().Register(&gAttributeAccessor);
}
} // namespace Chef
} // namespace Clusters
} // namespace app
} // namespace chip