Skip to content

Commit c28d2d9

Browse files
committed
Code review fixes
1 parent c3d97ca commit c28d2d9

File tree

1 file changed

+86
-31
lines changed

1 file changed

+86
-31
lines changed

examples/all-clusters-app/all-clusters-common/src/MeterIdentificationEventTriggers.cpp

+86-31
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818

1919
#include <meter-identification-instance.h>
20-
2120
#include <app/clusters/meter-identification-server/MeterIdentificationTestEventTriggerHandler.h>
2221

2322
using namespace chip;
@@ -29,42 +28,94 @@ namespace {
2928

3029
class OldMeterIdentificationAttributes
3130
{
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] = {};
3238
DataModel::Nullable<MeterTypeEnum> mMeterType;
3339
DataModel::Nullable<CharSpan> mPointOfDelivery;
3440
DataModel::Nullable<CharSpan> mMeterSerialNumber;
3541
DataModel::Nullable<CharSpan> mProtocolVersion;
3642
DataModel::Nullable<Globals::Structs::PowerThresholdStruct::Type> mPowerThreshold;
3743

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+
}
3950

40-
Instance * mInstance = nullptr;
51+
if (!a.IsNull() && !b.IsNull())
52+
{
53+
return a.Value().data_equal(b.Value());
54+
}
4155

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)
4360
{
44-
if (!charSpan.IsNull())
61+
if (NullableCharSpanCompare(newValue, mPointOfDelivery))
4562
{
46-
chip::Platform::MemoryFree(const_cast<char *>(charSpan.Value().data()));
47-
charSpan.SetNull();
63+
return;
4864
}
4965

50-
if (!value.IsNull())
66+
if (!mPointOfDelivery.IsNull())
5167
{
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));
5977
}
6078
}
6179

62-
static void CleanCharSpan(DataModel::Nullable<CharSpan> & charSpan)
80+
void SaveMeterSerialNumber(const DataModel::Nullable<CharSpan> & newValue)
6381
{
64-
if (!charSpan.IsNull())
82+
if (NullableCharSpanCompare(newValue, mMeterSerialNumber))
6583
{
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));
68119
}
69120
}
70121

@@ -95,7 +146,7 @@ class OldMeterIdentificationAttributes
95146
else
96147
{
97148
ret = string;
98-
++(*(ret.rbegin() + std::distance(string.rbegin(), rit)));
149+
++(*(ret.rbegin() + distance(string.rbegin(), rit)));
99150
break;
100151
}
101152
}
@@ -108,21 +159,25 @@ class OldMeterIdentificationAttributes
108159
mInstance = GetInstance();
109160
VerifyOrDieWithMsg(mInstance, AppServer, "Meter Identification instance is null");
110161
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());
114165
const auto && powerThreshold = mInstance->GetPowerThreshold();
115166
if (!powerThreshold.IsNull())
116167
{
117168
mPowerThreshold.SetNonNull(powerThreshold.Value());
118169
}
170+
else
171+
{
172+
mPowerThreshold.SetNull();
173+
}
119174
}
120175

121176
void ClearAttributes()
122177
{
123-
CleanCharSpan(mPointOfDelivery);
124-
CleanCharSpan(mMeterSerialNumber);
125-
CleanCharSpan(mProtocolVersion);
178+
mPointOfDelivery.SetNull();
179+
mMeterSerialNumber.SetNull();
180+
mProtocolVersion.SetNull();
126181
mMeterType.SetNull();
127182
mPowerThreshold.SetNull();
128183
mInstance = nullptr;
@@ -145,12 +200,12 @@ class OldMeterIdentificationAttributes
145200
VerifyOrDieWithMsg(mInstance, AppServer, "Meter Identification instance is null");
146201
if (mInstance->GetMeterType().IsNull())
147202
{
148-
mInstance->SetMeterType(DataModel::MakeNullable(static_cast<MeterTypeEnum>(0)));
203+
mInstance->SetMeterType(chip::app::DataModel::NullNullable);
149204
}
150205
else
151206
{
152207
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()))));
154209
}
155210

156211
if (mInstance->GetPointOfDelivery().IsNull())
@@ -188,16 +243,16 @@ class OldMeterIdentificationAttributes
188243

189244
if (mInstance->GetPowerThreshold().IsNull())
190245
{
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})));
193248
}
194249
else
195250
{
196251
auto powerThreshold = mInstance->GetPowerThreshold();
197252
++powerThreshold.Value().powerThreshold.Value();
198253
++powerThreshold.Value().apparentPowerThreshold.Value();
199254
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()));
201256
mInstance->SetPowerThreshold(std::move(powerThreshold));
202257
}
203258
}

0 commit comments

Comments
 (0)