Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the CountUpdatedPresetsAfterApplyingPendingPresets utility functi… #34780

Merged
merged 5 commits into from
Aug 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 8 additions & 35 deletions src/app/clusters/thermostat-server/thermostat-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,46 +321,21 @@ bool IsPresetHandlePresentInPresets(Delegate * delegate, const ByteSpan & preset
}

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

VerifyOrReturnValue(delegate != nullptr, 0);

for (uint8_t i = 0; true; i++)
{
PresetStructWithOwnedMembers preset;
CHIP_ERROR err = delegate->GetPresetAtIndex(i, preset);

if (err == CHIP_ERROR_PROVIDER_LIST_EXHAUSTED)
{
break;
}
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "GetUpdatedPresetsCount: GetPresetAtIndex failed with error %" CHIP_ERROR_FORMAT, err.Format());
return 0;
}
numberOfPresets++;

bool found = MatchingPendingPresetExists(delegate, preset);

if (found)
{
numberOfMatches++;
}
}

for (uint8_t i = 0; true; i++)
{
PresetStructWithOwnedMembers pendingPreset;
Expand All @@ -372,16 +347,14 @@ uint8_t CountUpdatedPresetsAfterApplyingPendingPresets(Delegate * delegate)
}
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "GetUpdatedPresetsCount: GetPendingPresetAtIndex failed with error %" CHIP_ERROR_FORMAT,
ChipLogError(Zcl, "CountNumberOfPendingPresets: GetPendingPresetAtIndex failed with error %" CHIP_ERROR_FORMAT,
err.Format());
return 0;
}
numberOfPendingPresets++;
}

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

/**
Expand Down Expand Up @@ -1538,7 +1511,7 @@ imcode commitPresets(Delegate * delegate, EndpointId endpoint)
}
}

uint8_t totalCount = CountUpdatedPresetsAfterApplyingPendingPresets(delegate);
uint8_t totalCount = CountNumberOfPendingPresets(delegate);

uint8_t numberOfPresetsSupported = delegate->GetNumberOfPresets();

Expand Down
Loading