16
16
* limitations under the License.
17
17
*/
18
18
19
- #include < meter-identification-delegate .h>
19
+ #include < meter-identification-instance .h>
20
20
21
21
#include < app/clusters/meter-identification-server/MeterIdentificationTestEventTriggerHandler.h>
22
22
@@ -37,7 +37,7 @@ class OldMeterIdentificationAttributes
37
37
38
38
private:
39
39
40
- DataModel::Nullable<MeterIdentificationDelegate *> mDelegate ;
40
+ Instance * mInstance = nullptr ;
41
41
42
42
static void SetCharSpan (DataModel::Nullable<CharSpan> & charSpan, const DataModel::Nullable<CharSpan> && value)
43
43
{
@@ -50,15 +50,12 @@ class OldMeterIdentificationAttributes
50
50
if (!value.IsNull ())
51
51
{
52
52
const size_t len = value.Value ().size ();
53
- auto * str = static_cast <char *>(chip::Platform::MemoryAlloc (1 + len));
54
- if (nullptr == str)
53
+ if (auto * str = static_cast <char *>(chip::Platform::MemoryAlloc (1 + len)))
55
54
{
56
- return ;
55
+ memcpy (str, value.Value ().data (), len);
56
+ str[len] = 0 ;
57
+ charSpan = DataModel::MakeNullable (CharSpan (str, len));
57
58
}
58
-
59
- memcpy (str, value.Value ().data (), len);
60
- str[len] = 0 ;
61
- charSpan = DataModel::MakeNullable (CharSpan (str, len));
62
59
}
63
60
}
64
61
@@ -71,8 +68,14 @@ class OldMeterIdentificationAttributes
71
68
}
72
69
}
73
70
71
+ /* *
72
+ * Lexicographically increments a string.
73
+ *
74
+ * @param string A string to lexicographically increment
75
+ */
74
76
static std::string IncrementString (const std::string &&string)
75
77
{
78
+ // The lexicographically smallest and largest characters
76
79
constexpr char minC = ' ' , maxC = ' ~' ;
77
80
78
81
std::string ret{minC};
@@ -102,13 +105,13 @@ class OldMeterIdentificationAttributes
102
105
103
106
void SaveAttributes ()
104
107
{
105
- mDelegate . SetNonNull ( GetDelegate () );
106
- VerifyOrDieWithMsg (mDelegate . Value () != nullptr , AppServer, " Meter Identification Delegate is null" );
107
- mMeterType = mDelegate . Value () ->GetMeterType ();
108
- SetCharSpan (mPointOfDelivery , mDelegate . Value () ->GetPointOfDelivery ());
109
- SetCharSpan (mMeterSerialNumber , mDelegate . Value () ->GetMeterSerialNumber ());
110
- SetCharSpan (mProtocolVersion , mDelegate . Value () ->GetProtocolVersion ());
111
- const auto && powerThreshold = mDelegate . Value () ->GetPowerThreshold ();
108
+ mInstance = GetInstance ( );
109
+ VerifyOrDieWithMsg (mInstance , AppServer, " Meter Identification instance is null" );
110
+ mMeterType = mInstance ->GetMeterType ();
111
+ SetCharSpan (mPointOfDelivery , mInstance ->GetPointOfDelivery ());
112
+ SetCharSpan (mMeterSerialNumber , mInstance ->GetMeterSerialNumber ());
113
+ SetCharSpan (mProtocolVersion , mInstance ->GetProtocolVersion ());
114
+ const auto && powerThreshold = mInstance ->GetPowerThreshold ();
112
115
if (!powerThreshold.IsNull ())
113
116
{
114
117
mPowerThreshold .SetNonNull (powerThreshold.Value ());
@@ -122,84 +125,85 @@ class OldMeterIdentificationAttributes
122
125
CleanCharSpan (mProtocolVersion );
123
126
mMeterType .SetNull ();
124
127
mPowerThreshold .SetNull ();
125
- mDelegate . SetNull () ;
128
+ mInstance = nullptr ;
126
129
}
127
130
128
131
void RestoreAttributes () const
129
132
{
130
- if (! mDelegate . IsNull () )
133
+ if (mInstance )
131
134
{
132
- mDelegate . Value () ->SetMeterType (mMeterType );
133
- mDelegate . Value () ->SetPointOfDelivery (mPointOfDelivery );
134
- mDelegate . Value () ->SetMeterSerialNumber (mMeterSerialNumber );
135
- mDelegate . Value () ->SetProtocolVersion (mProtocolVersion );
136
- mDelegate . Value () ->SetPowerThreshold (mPowerThreshold );
135
+ mInstance ->SetMeterType (mMeterType );
136
+ mInstance ->SetPointOfDelivery (mPointOfDelivery );
137
+ mInstance ->SetMeterSerialNumber (mMeterSerialNumber );
138
+ mInstance ->SetProtocolVersion (mProtocolVersion );
139
+ mInstance ->SetPowerThreshold (mPowerThreshold );
137
140
}
138
141
}
139
142
140
143
void IncreaseAttributes ()
141
144
{
142
- if (mDelegate .Value ()->GetMeterType ().IsNull ())
145
+ VerifyOrDieWithMsg (mInstance , AppServer, " Meter Identification instance is null" );
146
+ if (mInstance ->GetMeterType ().IsNull ())
143
147
{
144
- mDelegate . Value () ->SetMeterType (DataModel::MakeNullable (static_cast <MeterTypeEnum>(0 )));
148
+ mInstance ->SetMeterType (DataModel::MakeNullable (static_cast <MeterTypeEnum>(0 )));
145
149
}
146
150
else
147
151
{
148
- mDelegate . Value () ->SetMeterType (DataModel::MakeNullable (static_cast <MeterTypeEnum>(1 +
149
- static_cast <std::underlying_type<MeterTypeEnum>::type>(mDelegate . Value () ->GetMeterType ().Value ()))));
152
+ mInstance ->SetMeterType (DataModel::MakeNullable (static_cast <MeterTypeEnum>(1 +
153
+ static_cast <std::underlying_type<MeterTypeEnum>::type>(mInstance ->GetMeterType ().Value ()))));
150
154
}
151
155
152
- if (mDelegate . Value () ->GetPointOfDelivery ().IsNull ())
156
+ if (mInstance ->GetPointOfDelivery ().IsNull ())
153
157
{
154
- mDelegate . Value () ->SetPointOfDelivery (DataModel::MakeNullable (CharSpan::fromCharString (" " )));
158
+ mInstance ->SetPointOfDelivery (DataModel::MakeNullable (CharSpan::fromCharString (" " )));
155
159
}
156
160
else
157
161
{
158
- mDelegate . Value () ->SetPointOfDelivery (DataModel::MakeNullable (CharSpan::fromCharString (IncrementString (
159
- mDelegate . Value () ->GetPointOfDelivery ().Value ().data ()).c_str ())));
162
+ mInstance ->SetPointOfDelivery (DataModel::MakeNullable (CharSpan::fromCharString (IncrementString (
163
+ mInstance ->GetPointOfDelivery ().Value ().data ()).c_str ())));
160
164
}
161
165
162
- if (mDelegate . Value () ->GetMeterSerialNumber ().IsNull ())
166
+ if (mInstance ->GetMeterSerialNumber ().IsNull ())
163
167
{
164
- mDelegate . Value () ->SetMeterSerialNumber (DataModel::MakeNullable (CharSpan::fromCharString (" " )));
168
+ mInstance ->SetMeterSerialNumber (DataModel::MakeNullable (CharSpan::fromCharString (" " )));
165
169
}
166
170
else
167
171
{
168
- mDelegate . Value () ->SetMeterSerialNumber (DataModel::MakeNullable (CharSpan::fromCharString (IncrementString (
169
- mDelegate . Value () ->GetMeterSerialNumber ().Value ().data ()).c_str ())));
172
+ mInstance ->SetMeterSerialNumber (DataModel::MakeNullable (CharSpan::fromCharString (IncrementString (
173
+ mInstance ->GetMeterSerialNumber ().Value ().data ()).c_str ())));
170
174
}
171
175
172
- if (mDelegate . Value () ->GetProtocolVersion ().IsNull ())
176
+ if (mInstance ->GetProtocolVersion ().IsNull ())
173
177
{
174
- mDelegate . Value () ->SetProtocolVersion (DataModel::MakeNullable (CharSpan::fromCharString (" " )));
178
+ mInstance ->SetProtocolVersion (DataModel::MakeNullable (CharSpan::fromCharString (" " )));
175
179
}
176
180
else
177
181
{
178
- mDelegate . Value () ->SetProtocolVersion (DataModel::MakeNullable (CharSpan::fromCharString (IncrementString (
179
- mDelegate . Value () ->GetProtocolVersion ().Value ().data ()).c_str ())));
182
+ mInstance ->SetProtocolVersion (DataModel::MakeNullable (CharSpan::fromCharString (IncrementString (
183
+ mInstance ->GetProtocolVersion ().Value ().data ()).c_str ())));
180
184
}
181
185
182
- if (mDelegate . Value () ->GetPowerThreshold ().IsNull ())
186
+ if (mInstance ->GetPowerThreshold ().IsNull ())
183
187
{
184
- mDelegate . Value () ->SetPowerThreshold (DataModel::MakeNullable ((Structs::PowerThresholdStruct::Type){Optional<int64_t >(0 ),
188
+ mInstance ->SetPowerThreshold (DataModel::MakeNullable ((Structs::PowerThresholdStruct::Type){Optional<int64_t >(0 ),
185
189
Optional<int64_t >(0 ), static_cast <PowerThresholdSourceEnum>(0 )}));
186
190
}
187
191
else
188
192
{
189
- auto powerThreshold = mDelegate . Value () ->GetPowerThreshold ();
193
+ auto powerThreshold = mInstance ->GetPowerThreshold ();
190
194
++powerThreshold.Value ().powerThreshold .Value ();
191
195
++powerThreshold.Value ().apparentPowerThreshold .Value ();
192
196
powerThreshold.Value ().powerThresholdSource .Value () = static_cast <PowerThresholdSourceEnum>(1 +
193
197
static_cast <std::underlying_type<PowerThresholdSourceEnum>::type>(powerThreshold.Value ().powerThresholdSource .Value ()));
194
- mDelegate . Value () ->SetPowerThreshold (std::move (powerThreshold));
198
+ mInstance ->SetPowerThreshold (std::move (powerThreshold));
195
199
}
196
200
}
197
201
198
202
public:
199
203
200
204
void Update ()
201
205
{
202
- if (mDelegate . IsNull () )
206
+ if (! mInstance )
203
207
{
204
208
SaveAttributes ();
205
209
}
0 commit comments