Skip to content

Commit 4031ccb

Browse files
Fix the CountUpdatedPresetsAfterApplyingPendingPresets utility functi… (#34780)
* Fix the CountUpdatedPresetsAfterApplyingPendingPresets utility function to return the size of the pending presets list * Restyled by clang-format * Remove unnecessary cast for numberOfPendingPresets * Update thermostat-server.cpp * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 7d9a332 commit 4031ccb

File tree

1 file changed

+8
-35
lines changed

1 file changed

+8
-35
lines changed

src/app/clusters/thermostat-server/thermostat-server.cpp

+8-35
Original file line numberDiff line numberDiff line change
@@ -321,46 +321,21 @@ bool IsPresetHandlePresentInPresets(Delegate * delegate, const ByteSpan & preset
321321
}
322322

323323
/**
324-
* @brief Returns the length of the list of presets if the pending presets were to be applied. The calculation is done by
325-
* adding the number of presets in Presets attribute list to the number of pending presets in the pending
326-
* presets list and subtracting the number of duplicate presets. This is called before changes are actually applied.
324+
* @brief Returns the length of the list of presets if the pending presets were to be applied. The size of the pending presets list
325+
* calculated, after all the constraint checks are done, is the new size of the updated Presets attribute since the pending
326+
* preset list is expected to have all existing presets with or without edits plus new presets.
327+
* This is called before changes are actually applied.
327328
*
328329
* @param[in] delegate The delegate to use.
329330
*
330331
* @return count of the updated Presets attribute if the pending presets were applied to it. Return 0 for error cases.
331332
*/
332-
uint8_t CountUpdatedPresetsAfterApplyingPendingPresets(Delegate * delegate)
333+
uint8_t CountNumberOfPendingPresets(Delegate * delegate)
333334
{
334-
uint8_t numberOfPresets = 0;
335-
uint8_t numberOfMatches = 0;
336335
uint8_t numberOfPendingPresets = 0;
337336

338337
VerifyOrReturnValue(delegate != nullptr, 0);
339338

340-
for (uint8_t i = 0; true; i++)
341-
{
342-
PresetStructWithOwnedMembers preset;
343-
CHIP_ERROR err = delegate->GetPresetAtIndex(i, preset);
344-
345-
if (err == CHIP_ERROR_PROVIDER_LIST_EXHAUSTED)
346-
{
347-
break;
348-
}
349-
if (err != CHIP_NO_ERROR)
350-
{
351-
ChipLogError(Zcl, "GetUpdatedPresetsCount: GetPresetAtIndex failed with error %" CHIP_ERROR_FORMAT, err.Format());
352-
return 0;
353-
}
354-
numberOfPresets++;
355-
356-
bool found = MatchingPendingPresetExists(delegate, preset);
357-
358-
if (found)
359-
{
360-
numberOfMatches++;
361-
}
362-
}
363-
364339
for (uint8_t i = 0; true; i++)
365340
{
366341
PresetStructWithOwnedMembers pendingPreset;
@@ -372,16 +347,14 @@ uint8_t CountUpdatedPresetsAfterApplyingPendingPresets(Delegate * delegate)
372347
}
373348
if (err != CHIP_NO_ERROR)
374349
{
375-
ChipLogError(Zcl, "GetUpdatedPresetsCount: GetPendingPresetAtIndex failed with error %" CHIP_ERROR_FORMAT,
350+
ChipLogError(Zcl, "CountNumberOfPendingPresets: GetPendingPresetAtIndex failed with error %" CHIP_ERROR_FORMAT,
376351
err.Format());
377352
return 0;
378353
}
379354
numberOfPendingPresets++;
380355
}
381356

382-
// TODO: #34546 - Need to support deletion of presets that are removed from Presets.
383-
// This API needs to modify its logic for the deletion case.
384-
return static_cast<uint8_t>(numberOfPresets + numberOfPendingPresets - numberOfMatches);
357+
return numberOfPendingPresets;
385358
}
386359

387360
/**
@@ -1538,7 +1511,7 @@ imcode commitPresets(Delegate * delegate, EndpointId endpoint)
15381511
}
15391512
}
15401513

1541-
uint8_t totalCount = CountUpdatedPresetsAfterApplyingPendingPresets(delegate);
1514+
uint8_t totalCount = CountNumberOfPendingPresets(delegate);
15421515

15431516
uint8_t numberOfPresetsSupported = delegate->GetNumberOfPresets();
15441517

0 commit comments

Comments
 (0)