Skip to content

Commit 064f3a1

Browse files
committed
Reinitialize storage objects on every iteration
1 parent cdfca87 commit 064f3a1

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/app/clusters/energy-preference-server/energy-preference-server.cpp

+26-19
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,22 @@ CHIP_ERROR EnergyPrefAttrAccess::Read(const ConcreteReadAttributePath & aPath, A
6969
if (gsDelegate != nullptr)
7070
{
7171
return aEncoder.EncodeList([endpoint](const auto & encoder) -> CHIP_ERROR {
72-
chip::Percent step;
73-
char buffer[64];
74-
chip::Optional<chip::MutableCharSpan> label{ chip::MutableCharSpan(buffer) };
7572
size_t index = 0;
7673
CHIP_ERROR err = CHIP_NO_ERROR;
77-
while ((err = gsDelegate->GetEnergyBalanceAtIndex(endpoint, index, step, label)) == CHIP_NO_ERROR)
74+
do
7875
{
79-
BalanceStruct::Type balance = { step,
80-
label.HasValue() ? Optional<CharSpan>(label.Value()) : Optional<CharSpan>() };
81-
ReturnErrorOnFailure(encoder.Encode(balance));
82-
index++;
83-
}
76+
Percent step;
77+
char buffer[64];
78+
Optional<MutableCharSpan> label{ MutableCharSpan(buffer) };
79+
if ((err = gsDelegate->GetEnergyBalanceAtIndex(endpoint, index, step, label)) == CHIP_NO_ERROR)
80+
{
81+
BalanceStruct::Type balance = { step,
82+
label.HasValue() ? Optional<CharSpan>(label.Value()) : Optional<CharSpan>() };
83+
ReturnErrorOnFailure(encoder.Encode(balance));
84+
index++;
85+
}
86+
} while (err == CHIP_NO_ERROR);
87+
8488
if (err == CHIP_ERROR_NOT_FOUND)
8589
{
8690
return CHIP_NO_ERROR;
@@ -123,21 +127,24 @@ CHIP_ERROR EnergyPrefAttrAccess::Read(const ConcreteReadAttributePath & aPath, A
123127
if (gsDelegate != nullptr)
124128
{
125129
return aEncoder.EncodeList([endpoint](const auto & encoder) -> CHIP_ERROR {
126-
chip::Percent step;
127-
char buffer[64];
128-
chip::Optional<chip::MutableCharSpan> label{ chip::MutableCharSpan(buffer) };
129130
size_t index = 0;
130131
CHIP_ERROR err = CHIP_NO_ERROR;
131-
while ((err = gsDelegate->GetLowPowerModeSensitivityAtIndex(endpoint, index, step, label)) == CHIP_NO_ERROR)
132+
do
132133
{
133-
BalanceStruct::Type balance = { step,
134-
label.HasValue() ? Optional<CharSpan>(label.Value()) : Optional<CharSpan>() };
135-
ReturnErrorOnFailure(encoder.Encode(balance));
136-
index++;
137-
}
134+
Percent step;
135+
char buffer[64];
136+
Optional<MutableCharSpan> label{ MutableCharSpan(buffer) };
137+
if ((err = gsDelegate->GetLowPowerModeSensitivityAtIndex(endpoint, index, step, label)) == CHIP_NO_ERROR)
138+
{
139+
BalanceStruct::Type balance = { step,
140+
label.HasValue() ? Optional<CharSpan>(label.Value()) : Optional<CharSpan>() };
141+
ReturnErrorOnFailure(encoder.Encode(balance));
142+
index++;
143+
}
144+
} while err == CHIP_NO_ERROR);
138145
if (err == CHIP_ERROR_NOT_FOUND)
139146
{
140-
return CHIP_NO_ERROR;
147+
return CHIP_NO_ERROR;
141148
}
142149
return err;
143150
});

0 commit comments

Comments
 (0)