From 12a9d1c4ce0b603826e3920368d2df4f1e522594 Mon Sep 17 00:00:00 2001 From: Nivedita Sarkar Date: Mon, 5 Aug 2024 10:28:34 -0700 Subject: [PATCH 1/5] Fix the CountUpdatedPresetsAfterApplyingPendingPresets utility function to return the size of the pending presets list --- .../thermostat-server/thermostat-server.cpp | 39 +++---------------- 1 file changed, 6 insertions(+), 33 deletions(-) diff --git a/src/app/clusters/thermostat-server/thermostat-server.cpp b/src/app/clusters/thermostat-server/thermostat-server.cpp index afdb1b12afe763..c13a2175b43899 100644 --- a/src/app/clusters/thermostat-server/thermostat-server.cpp +++ b/src/app/clusters/thermostat-server/thermostat-server.cpp @@ -321,9 +321,10 @@ 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. * @@ -331,36 +332,10 @@ bool IsPresetHandlePresentInPresets(Delegate * delegate, const ByteSpan & preset */ uint8_t CountUpdatedPresetsAfterApplyingPendingPresets(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; @@ -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, "CountUpdatedPresetsAfterApplyingPendingPresets: 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(numberOfPresets + numberOfPendingPresets - numberOfMatches); + return static_cast(numberOfPendingPresets); } /** From e1e4ee68d7a7af527a8620dccb3cd5d783f2e5b7 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 5 Aug 2024 17:29:51 +0000 Subject: [PATCH 2/5] Restyled by clang-format --- src/app/clusters/thermostat-server/thermostat-server.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/app/clusters/thermostat-server/thermostat-server.cpp b/src/app/clusters/thermostat-server/thermostat-server.cpp index c13a2175b43899..d1014a5f76ee57 100644 --- a/src/app/clusters/thermostat-server/thermostat-server.cpp +++ b/src/app/clusters/thermostat-server/thermostat-server.cpp @@ -347,8 +347,10 @@ uint8_t CountUpdatedPresetsAfterApplyingPendingPresets(Delegate * delegate) } if (err != CHIP_NO_ERROR) { - ChipLogError(Zcl, "CountUpdatedPresetsAfterApplyingPendingPresets: GetPendingPresetAtIndex failed with error %" CHIP_ERROR_FORMAT, - err.Format()); + ChipLogError( + Zcl, + "CountUpdatedPresetsAfterApplyingPendingPresets: GetPendingPresetAtIndex failed with error %" CHIP_ERROR_FORMAT, + err.Format()); return 0; } numberOfPendingPresets++; From 9912039021e99a5a015626a7657c3838027c3fcd Mon Sep 17 00:00:00 2001 From: Nivedita Sarkar Date: Mon, 5 Aug 2024 10:40:31 -0700 Subject: [PATCH 3/5] Remove unnecessary cast for numberOfPendingPresets --- src/app/clusters/thermostat-server/thermostat-server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/clusters/thermostat-server/thermostat-server.cpp b/src/app/clusters/thermostat-server/thermostat-server.cpp index d1014a5f76ee57..9198bd0dd4188b 100644 --- a/src/app/clusters/thermostat-server/thermostat-server.cpp +++ b/src/app/clusters/thermostat-server/thermostat-server.cpp @@ -356,7 +356,7 @@ uint8_t CountUpdatedPresetsAfterApplyingPendingPresets(Delegate * delegate) numberOfPendingPresets++; } - return static_cast(numberOfPendingPresets); + return numberOfPendingPresets; } /** From e6e19a2f882a2503ad9330914d99d8b51d11ef52 Mon Sep 17 00:00:00 2001 From: Nivi Sarkar <55898241+nivi-apple@users.noreply.github.com> Date: Mon, 5 Aug 2024 18:37:17 -0700 Subject: [PATCH 4/5] Update thermostat-server.cpp --- src/app/clusters/thermostat-server/thermostat-server.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/clusters/thermostat-server/thermostat-server.cpp b/src/app/clusters/thermostat-server/thermostat-server.cpp index 9198bd0dd4188b..2fcb87d817760d 100644 --- a/src/app/clusters/thermostat-server/thermostat-server.cpp +++ b/src/app/clusters/thermostat-server/thermostat-server.cpp @@ -330,7 +330,7 @@ bool IsPresetHandlePresentInPresets(Delegate * delegate, const ByteSpan & preset * * @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 numberOfPendingPresets = 0; @@ -349,7 +349,7 @@ uint8_t CountUpdatedPresetsAfterApplyingPendingPresets(Delegate * delegate) { ChipLogError( Zcl, - "CountUpdatedPresetsAfterApplyingPendingPresets: GetPendingPresetAtIndex failed with error %" CHIP_ERROR_FORMAT, + "CountNumberOfPendingPresets: GetPendingPresetAtIndex failed with error %" CHIP_ERROR_FORMAT, err.Format()); return 0; } @@ -1513,7 +1513,7 @@ imcode commitPresets(Delegate * delegate, EndpointId endpoint) } } - uint8_t totalCount = CountUpdatedPresetsAfterApplyingPendingPresets(delegate); + uint8_t totalCount = CountNumberOfPendingPresets(delegate); uint8_t numberOfPresetsSupported = delegate->GetNumberOfPresets(); From 4008a0c1e19ade97e3c9a80db4d83ce800d3d688 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Tue, 6 Aug 2024 01:38:06 +0000 Subject: [PATCH 5/5] Restyled by clang-format --- src/app/clusters/thermostat-server/thermostat-server.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/app/clusters/thermostat-server/thermostat-server.cpp b/src/app/clusters/thermostat-server/thermostat-server.cpp index 2fcb87d817760d..fb60b046183b28 100644 --- a/src/app/clusters/thermostat-server/thermostat-server.cpp +++ b/src/app/clusters/thermostat-server/thermostat-server.cpp @@ -347,10 +347,8 @@ uint8_t CountNumberOfPendingPresets(Delegate * delegate) } if (err != CHIP_NO_ERROR) { - ChipLogError( - Zcl, - "CountNumberOfPendingPresets: GetPendingPresetAtIndex failed with error %" CHIP_ERROR_FORMAT, - err.Format()); + ChipLogError(Zcl, "CountNumberOfPendingPresets: GetPendingPresetAtIndex failed with error %" CHIP_ERROR_FORMAT, + err.Format()); return 0; } numberOfPendingPresets++;