17
17
*/
18
18
19
19
#include < meter-identification-instance.h>
20
-
21
20
#include < app/clusters/meter-identification-server/MeterIdentificationTestEventTriggerHandler.h>
22
21
23
22
using namespace chip ;
@@ -29,42 +28,94 @@ namespace {
29
28
30
29
class OldMeterIdentificationAttributes
31
30
{
31
+ private:
32
+
33
+ Instance * mInstance = nullptr ;
34
+ static constexpr size_t kMaximumStringSize = 64 ;
35
+ char mPointOfDeliveryBuf [kMaximumStringSize ] = {};
36
+ char mMeterSerialNumberBuf [kMaximumStringSize ] = {};
37
+ char mProtocolVersionBuf [kMaximumStringSize ] = {};
32
38
DataModel::Nullable<MeterTypeEnum> mMeterType ;
33
39
DataModel::Nullable<CharSpan> mPointOfDelivery ;
34
40
DataModel::Nullable<CharSpan> mMeterSerialNumber ;
35
41
DataModel::Nullable<CharSpan> mProtocolVersion ;
36
42
DataModel::Nullable<Globals::Structs::PowerThresholdStruct::Type> mPowerThreshold ;
37
43
38
- private:
44
+ static bool NullableCharSpanCompare (const DataModel::Nullable<CharSpan> & a, const DataModel::Nullable<CharSpan> & b)
45
+ {
46
+ if (a.IsNull () && b.IsNull ())
47
+ {
48
+ return true ;
49
+ }
39
50
40
- Instance * mInstance = nullptr ;
51
+ if (!a.IsNull () && !b.IsNull ())
52
+ {
53
+ return a.Value ().data_equal (b.Value ());
54
+ }
41
55
42
- static void SetCharSpan (DataModel::Nullable<CharSpan> & charSpan, const DataModel::Nullable<CharSpan> && value)
56
+ return false ;
57
+ }
58
+
59
+ void SavePointOfDelivery (const DataModel::Nullable<CharSpan> & newValue)
43
60
{
44
- if (!charSpan. IsNull ( ))
61
+ if (NullableCharSpanCompare (newValue, mPointOfDelivery ))
45
62
{
46
- chip::Platform::MemoryFree (const_cast <char *>(charSpan.Value ().data ()));
47
- charSpan.SetNull ();
63
+ return ;
48
64
}
49
65
50
- if (!value .IsNull ())
66
+ if (!mPointOfDelivery .IsNull ())
51
67
{
52
- const size_t len = value.Value ().size ();
53
- if (auto * str = static_cast <char *>(chip::Platform::MemoryAlloc (len)))
54
- {
55
- memcpy (str, value.Value ().data (), len);
56
- str[len] = 0 ;
57
- charSpan = DataModel::MakeNullable (CharSpan (str, len));
58
- }
68
+ mPointOfDelivery .SetNull ();
69
+ }
70
+
71
+ if (!newValue.IsNull ())
72
+ {
73
+ const size_t len = newValue.IsNull () ? 0 : newValue.Value ().size () < kMaximumStringSize ?
74
+ newValue.Value ().size () : kMaximumStringSize ;
75
+ memcpy (mPointOfDeliveryBuf , newValue.Value ().data (), len);
76
+ mPointOfDelivery = chip::app::DataModel::MakeNullable (CharSpan (mPointOfDeliveryBuf , len));
59
77
}
60
78
}
61
79
62
- static void CleanCharSpan ( DataModel::Nullable<CharSpan> & charSpan )
80
+ void SaveMeterSerialNumber ( const DataModel::Nullable<CharSpan> & newValue )
63
81
{
64
- if (!charSpan. IsNull ( ))
82
+ if (NullableCharSpanCompare (newValue, mMeterSerialNumber ))
65
83
{
66
- chip::Platform::MemoryFree (const_cast <char *>(charSpan.Value ().data ()));
67
- charSpan.SetNull ();
84
+ return ;
85
+ }
86
+
87
+ if (!mMeterSerialNumber .IsNull ())
88
+ {
89
+ mMeterSerialNumber .SetNull ();
90
+ }
91
+
92
+ if (!newValue.IsNull ())
93
+ {
94
+ const size_t len = newValue.IsNull () ? 0 : newValue.Value ().size () < kMaximumStringSize ?
95
+ newValue.Value ().size () : kMaximumStringSize ;
96
+ memcpy (mMeterSerialNumberBuf , newValue.Value ().data (), len);
97
+ mMeterSerialNumber = chip::app::DataModel::MakeNullable (CharSpan (mMeterSerialNumberBuf , len));
98
+ }
99
+ }
100
+
101
+ void SaveProtocolVersion (const DataModel::Nullable<CharSpan> & newValue)
102
+ {
103
+ if (NullableCharSpanCompare (newValue, mProtocolVersion ))
104
+ {
105
+ return ;
106
+ }
107
+
108
+ if (!mProtocolVersion .IsNull ())
109
+ {
110
+ mProtocolVersion .SetNull ();
111
+ }
112
+
113
+ if (!newValue.IsNull ())
114
+ {
115
+ const size_t len = newValue.IsNull () ? 0 : newValue.Value ().size () < kMaximumStringSize ?
116
+ newValue.Value ().size () : kMaximumStringSize ;
117
+ memcpy (mProtocolVersionBuf , newValue.Value ().data (), len);
118
+ mProtocolVersion = chip::app::DataModel::MakeNullable (CharSpan (mProtocolVersionBuf , len));
68
119
}
69
120
}
70
121
@@ -95,7 +146,7 @@ class OldMeterIdentificationAttributes
95
146
else
96
147
{
97
148
ret = string;
98
- ++(*(ret.rbegin () + std:: distance (string.rbegin (), rit)));
149
+ ++(*(ret.rbegin () + distance (string.rbegin (), rit)));
99
150
break ;
100
151
}
101
152
}
@@ -108,21 +159,25 @@ class OldMeterIdentificationAttributes
108
159
mInstance = GetInstance ();
109
160
VerifyOrDieWithMsg (mInstance , AppServer, " Meter Identification instance is null" );
110
161
mMeterType = mInstance ->GetMeterType ();
111
- SetCharSpan ( mPointOfDelivery , mInstance ->GetPointOfDelivery ());
112
- SetCharSpan ( mMeterSerialNumber , mInstance ->GetMeterSerialNumber ());
113
- SetCharSpan ( mProtocolVersion , mInstance ->GetProtocolVersion ());
162
+ SavePointOfDelivery ( mInstance ->GetPointOfDelivery ());
163
+ SaveMeterSerialNumber ( mInstance ->GetMeterSerialNumber ());
164
+ SaveProtocolVersion ( mInstance ->GetProtocolVersion ());
114
165
const auto && powerThreshold = mInstance ->GetPowerThreshold ();
115
166
if (!powerThreshold.IsNull ())
116
167
{
117
168
mPowerThreshold .SetNonNull (powerThreshold.Value ());
118
169
}
170
+ else
171
+ {
172
+ mPowerThreshold .SetNull ();
173
+ }
119
174
}
120
175
121
176
void ClearAttributes ()
122
177
{
123
- CleanCharSpan ( mPointOfDelivery );
124
- CleanCharSpan ( mMeterSerialNumber );
125
- CleanCharSpan ( mProtocolVersion );
178
+ mPointOfDelivery . SetNull ( );
179
+ mMeterSerialNumber . SetNull ( );
180
+ mProtocolVersion . SetNull ( );
126
181
mMeterType .SetNull ();
127
182
mPowerThreshold .SetNull ();
128
183
mInstance = nullptr ;
@@ -145,12 +200,12 @@ class OldMeterIdentificationAttributes
145
200
VerifyOrDieWithMsg (mInstance , AppServer, " Meter Identification instance is null" );
146
201
if (mInstance ->GetMeterType ().IsNull ())
147
202
{
148
- mInstance ->SetMeterType (DataModel::MakeNullable ( static_cast <MeterTypeEnum>( 0 )) );
203
+ mInstance ->SetMeterType (chip::app:: DataModel::NullNullable );
149
204
}
150
205
else
151
206
{
152
207
mInstance ->SetMeterType (DataModel::MakeNullable (static_cast <MeterTypeEnum>(1 +
153
- static_cast <std::underlying_type<MeterTypeEnum>::type> (mInstance ->GetMeterType ().Value ()))));
208
+ to_underlying (mInstance ->GetMeterType ().Value ()))));
154
209
}
155
210
156
211
if (mInstance ->GetPointOfDelivery ().IsNull ())
@@ -188,16 +243,16 @@ class OldMeterIdentificationAttributes
188
243
189
244
if (mInstance ->GetPowerThreshold ().IsNull ())
190
245
{
191
- mInstance ->SetPowerThreshold (DataModel::MakeNullable (( Globals::Structs::PowerThresholdStruct::Type) {Optional<int64_t >(0 ),
192
- Optional<int64_t >(0 ), static_cast < Globals::PowerThresholdSourceEnum>( 0 )} ));
246
+ mInstance ->SetPowerThreshold (DataModel::MakeNullable (Globals::Structs::PowerThresholdStruct::Type ( {Optional<int64_t >(0 ),
247
+ Optional<int64_t >(0 ), Globals::PowerThresholdSourceEnum:: kContract }) ));
193
248
}
194
249
else
195
250
{
196
251
auto powerThreshold = mInstance ->GetPowerThreshold ();
197
252
++powerThreshold.Value ().powerThreshold .Value ();
198
253
++powerThreshold.Value ().apparentPowerThreshold .Value ();
199
254
powerThreshold.Value ().powerThresholdSource .Value () = static_cast <Globals::PowerThresholdSourceEnum>(1 +
200
- static_cast <std::underlying_type<Globals::PowerThresholdSourceEnum>::type> (powerThreshold.Value ().powerThresholdSource .Value ()));
255
+ to_underlying (powerThreshold.Value ().powerThresholdSource .Value ()));
201
256
mInstance ->SetPowerThreshold (std::move (powerThreshold));
202
257
}
203
258
}
0 commit comments