From acba7f83e2891e7d1bcb8f3e11a9d938d38f28e5 Mon Sep 17 00:00:00 2001 From: Nivi Sarkar <55898241+nivi-apple@users.noreply.github.com> Date: Sun, 25 Aug 2024 23:55:45 -0700 Subject: [PATCH] =?UTF-8?q?Move=20the=20constraint=20check=20for=20number?= =?UTF-8?q?=20of=20presets=20not=20to=20exceed=20the=20num=E2=80=A6=20(#35?= =?UTF-8?q?167)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Move the constraint check for number of presets not to exceed the number of presets supported from the atomic write commit command handling to the write request * Minor fix * Do not check if totalExpectedCount > 0 since its an unsigned integer - Also no need to check if numberOfPresetsSupported is 0. * Update src/app/clusters/thermostat-server/thermostat-server-presets.cpp * Update src/app/clusters/thermostat-server/thermostat-server-presets.cpp --------- Co-authored-by: Boris Zbarsky --- .../thermostat-server-presets.cpp | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/app/clusters/thermostat-server/thermostat-server-presets.cpp b/src/app/clusters/thermostat-server/thermostat-server-presets.cpp index e57c2f9c95f8fd..a56c94fa49834d 100644 --- a/src/app/clusters/thermostat-server/thermostat-server-presets.cpp +++ b/src/app/clusters/thermostat-server/thermostat-server-presets.cpp @@ -402,6 +402,20 @@ CHIP_ERROR ThermostatAttrAccess::AppendPendingPreset(Thermostat::Delegate * dele return CHIP_IM_GLOBAL_STATUS(ConstraintError); } + // Before adding this preset to the pending presets, if the expected length of the pending presets' list + // exceeds the total number of presets supported, return RESOURCE_EXHAUSTED. Note that the preset has not been appended yet. + + uint8_t numberOfPendingPresets = CountNumberOfPendingPresets(delegate); + + // We will be adding one more preset, so reject if the length is already at max. + if (numberOfPendingPresets >= delegate->GetNumberOfPresets()) + { + return CHIP_IM_GLOBAL_STATUS(ResourceExhausted); + } + + // TODO #34556 : Check if the number of presets for each presetScenario exceeds the max number of presets supported for that + // scenario. We plan to support only one preset for each presetScenario for our use cases so defer this for re-evaluation. + return delegate->AppendToPendingPresetList(preset); } @@ -504,25 +518,6 @@ Status ThermostatAttrAccess::PrecommitPresets(EndpointId endpoint) } } - uint8_t totalCount = CountNumberOfPendingPresets(delegate); - - uint8_t numberOfPresetsSupported = delegate->GetNumberOfPresets(); - - if (numberOfPresetsSupported == 0) - { - ChipLogError(Zcl, "emberAfThermostatClusterCommitPresetsSchedulesRequestCallback: Failed to get NumberOfPresets"); - return Status::InvalidInState; - } - - // If the expected length of the presets attribute with the applied changes exceeds the total number of presets supported, - // return RESOURCE_EXHAUSTED. Note that the changes are not yet applied. - if (numberOfPresetsSupported > 0 && totalCount > numberOfPresetsSupported) - { - return Status::ResourceExhausted; - } - - // TODO: Check if the number of presets for each presetScenario exceeds the max number of presets supported for that - // scenario. We plan to support only one preset for each presetScenario for our use cases so defer this for re-evaluation. return Status::Success; }