@@ -35,13 +35,67 @@ using namespace chip::app::Clusters::MeterIdentification::Attributes;
35
35
36
36
using chip::Protocols::InteractionModel::Status;
37
37
38
+ namespace {
39
+ bool NullableCharSpanCompare (const DataModel::Nullable<CharSpan> & a, const DataModel::Nullable<CharSpan> & b)
40
+ {
41
+ if (a.IsNull () && b.IsNull ())
42
+ {
43
+ return true ;
44
+ }
45
+
46
+ if (!a.IsNull () && !b.IsNull ())
47
+ {
48
+ return a.Value ().data_equal (b.Value ());
49
+ }
50
+
51
+ return false ;
52
+ }
53
+
54
+ CHIP_ERROR NullableCharSpanCopy (DataModel::Nullable<CharSpan> & dst, const DataModel::Nullable<CharSpan> & src)
55
+ {
56
+ const size_t len = src.IsNull () ? 0 : src.Value ().size ();
57
+ if (64 < len)
58
+ {
59
+ return CHIP_ERROR_INVALID_STRING_LENGTH;
60
+ }
61
+
62
+ if (!dst.IsNull ())
63
+ {
64
+ chip::Platform::MemoryFree (const_cast <char *>(dst.Value ().data ()));
65
+ dst.SetNull ();
66
+ }
67
+
68
+ if (!src.IsNull ())
69
+ {
70
+ auto * str = static_cast <char *>(chip::Platform::MemoryAlloc (1 + len));
71
+ if (nullptr == str)
72
+ {
73
+ return CHIP_ERROR_NO_MEMORY;
74
+ }
75
+
76
+ memcpy (str, src.Value ().data (), len);
77
+ str[len] = 0 ;
78
+ dst = MakeNullable (CharSpan (str, len));
79
+ }
80
+
81
+ return CHIP_NO_ERROR;
82
+ }
83
+ } // namespace
84
+
38
85
namespace chip {
39
86
namespace app {
40
87
namespace Clusters {
41
88
namespace MeterIdentification {
42
89
43
90
CHIP_ERROR Instance::Init ()
44
91
{
92
+ // Check if the cluster has been selected in zap
93
+ VerifyOrDie (emberAfContainsServer (mEndpointId , Id));
94
+ SetMeterType (std::nullopt);
95
+ SetPointOfDelivery (std::nullopt);
96
+ SetMeterSerialNumber (std::nullopt);
97
+ SetProtocolVersion (std::nullopt);
98
+ SetPowerThreshold (std::nullopt);
45
99
VerifyOrReturnError (AttributeAccessInterfaceRegistry::Instance ().Register (this ), CHIP_ERROR_INCORRECT_STATE);
46
100
return CHIP_NO_ERROR;
47
101
}
@@ -56,6 +110,114 @@ bool Instance::HasFeature(const Feature & aFeature) const
56
110
return mFeature .Has (aFeature);
57
111
}
58
112
113
+ CHIP_ERROR Instance::SetMeterType (const DataModel::Nullable<MeterTypeEnum> & newValue)
114
+ {
115
+ if (newValue.IsNull ())
116
+ {
117
+ if (mMeterType .IsNull ())
118
+ {
119
+ return CHIP_NO_ERROR;
120
+ }
121
+
122
+ mMeterType .SetNull ();
123
+ }
124
+ else
125
+ {
126
+ if (!mMeterType .IsNull () && mMeterType .Value () == newValue.Value ())
127
+ {
128
+ return CHIP_NO_ERROR;
129
+ }
130
+
131
+ if (MeterTypeEnum::kUnknownEnumValue == EnsureKnownEnumValue (newValue.Value ()))
132
+ {
133
+ return CHIP_ERROR_INVALID_INTEGER_VALUE;
134
+ }
135
+
136
+ mMeterType .SetNonNull (newValue.Value ());
137
+ }
138
+
139
+ MatterReportingAttributeChangeCallback (mEndpointId , MeterIdentification::Id, MeterType::Id);
140
+ return CHIP_NO_ERROR;
141
+ }
142
+
143
+ CHIP_ERROR Instance::SetPointOfDelivery (const DataModel::Nullable<CharSpan> & newValue)
144
+ {
145
+ if (NullableCharSpanCompare (newValue, mPointOfDelivery ))
146
+ {
147
+ return CHIP_NO_ERROR;
148
+ }
149
+
150
+ const CHIP_ERROR ret = NullableCharSpanCopy (mPointOfDelivery , newValue);
151
+ if (CHIP_NO_ERROR == ret)
152
+ {
153
+ MatterReportingAttributeChangeCallback (mEndpointId , MeterIdentification::Id, PointOfDelivery::Id);
154
+ }
155
+ return ret;
156
+ }
157
+
158
+ CHIP_ERROR Instance::SetMeterSerialNumber (const DataModel::Nullable<CharSpan> & newValue)
159
+ {
160
+ if (NullableCharSpanCompare (newValue, mMeterSerialNumber ))
161
+ {
162
+ return CHIP_NO_ERROR;
163
+ }
164
+
165
+ const CHIP_ERROR ret = NullableCharSpanCopy (mMeterSerialNumber , newValue);
166
+ if (CHIP_NO_ERROR == ret)
167
+ {
168
+ MatterReportingAttributeChangeCallback (mEndpointId , MeterIdentification::Id, MeterSerialNumber::Id);
169
+ }
170
+ return ret;
171
+ }
172
+
173
+ CHIP_ERROR Instance::SetProtocolVersion (const DataModel::Nullable<CharSpan> & newValue)
174
+ {
175
+ if (NullableCharSpanCompare (newValue, mProtocolVersion ))
176
+ {
177
+ return CHIP_NO_ERROR;
178
+ }
179
+
180
+ const CHIP_ERROR ret = NullableCharSpanCopy (mProtocolVersion , newValue);
181
+ if (CHIP_NO_ERROR == ret)
182
+ {
183
+ MatterReportingAttributeChangeCallback (mEndpointId , MeterIdentification::Id, ProtocolVersion::Id);
184
+ }
185
+ return ret;
186
+ }
187
+
188
+ CHIP_ERROR Instance::SetPowerThreshold (const DataModel::Nullable<Structs::PowerThresholdStruct::Type> & newValue)
189
+ {
190
+ if (newValue.IsNull ())
191
+ {
192
+ if (mPowerThreshold .IsNull ())
193
+ {
194
+ return CHIP_NO_ERROR;
195
+ }
196
+
197
+ mPowerThreshold .SetNull ();
198
+ }
199
+ else
200
+ {
201
+ if (!mPowerThreshold .IsNull () && (newValue.Value ().powerThreshold == mPowerThreshold .Value ().powerThreshold &&
202
+ newValue.Value ().apparentPowerThreshold == mPowerThreshold .Value ().apparentPowerThreshold &&
203
+ newValue.Value ().powerThresholdSource == mPowerThreshold .Value ().powerThresholdSource ))
204
+ {
205
+ return CHIP_NO_ERROR;
206
+ }
207
+
208
+ if (!(newValue.ExistingValueInEncodableRange () && (newValue.Value ().powerThresholdSource .IsNull () ||
209
+ PowerThresholdSourceEnum::kUnknownEnumValue != EnsureKnownEnumValue (newValue.Value ().powerThresholdSource .Value ()))))
210
+ {
211
+ return CHIP_ERROR_DECODE_FAILED;
212
+ }
213
+
214
+ mPowerThreshold .SetNonNull (newValue.Value ());
215
+ }
216
+
217
+ MatterReportingAttributeChangeCallback (mEndpointId , MeterIdentification::Id, PowerThreshold::Id);
218
+ return CHIP_NO_ERROR;
219
+ }
220
+
59
221
// AttributeAccessInterface
60
222
CHIP_ERROR Instance::Read (const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
61
223
{
@@ -67,24 +229,24 @@ CHIP_ERROR Instance::Read(const ConcreteReadAttributePath & aPath, AttributeValu
67
229
ReturnErrorOnFailure (aEncoder.Encode (mFeature ));
68
230
break ;
69
231
case MeterType::Id:
70
- ReturnErrorOnFailure (aEncoder.Encode (mDelegate . GetMeterType ()));
232
+ ReturnErrorOnFailure (aEncoder.Encode (GetMeterType ()));
71
233
break ;
72
234
73
235
case PointOfDelivery::Id:
74
- ReturnErrorOnFailure (aEncoder.Encode (mDelegate . GetPointOfDelivery ()));
236
+ ReturnErrorOnFailure (aEncoder.Encode (GetPointOfDelivery ()));
75
237
break ;
76
238
77
239
case MeterSerialNumber::Id:
78
- ReturnErrorOnFailure (aEncoder.Encode (mDelegate . GetMeterSerialNumber ()));
240
+ ReturnErrorOnFailure (aEncoder.Encode (GetMeterSerialNumber ())); ;
79
241
break ;
80
242
81
243
case ProtocolVersion::Id:
82
- ReturnErrorOnFailure (aEncoder.Encode (mDelegate . GetProtocolVersion ()));
244
+ ReturnErrorOnFailure (aEncoder.Encode (GetProtocolVersion ()));
83
245
break ;
84
246
85
247
case PowerThreshold::Id:
86
248
if (HasFeature (Feature::kPowerThreshold ))
87
- ReturnErrorOnFailure (aEncoder.Encode (mDelegate . GetPowerThreshold ()));
249
+ ReturnErrorOnFailure (aEncoder.Encode (GetPowerThreshold ()));
88
250
else
89
251
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
90
252
break ;
0 commit comments