diff --git a/src/app/clusters/thermostat-server/thermostat-server-presets.cpp b/src/app/clusters/thermostat-server/thermostat-server-presets.cpp index 16ed77d251be50..d35b2796934e02 100644 --- a/src/app/clusters/thermostat-server/thermostat-server-presets.cpp +++ b/src/app/clusters/thermostat-server/thermostat-server-presets.cpp @@ -18,6 +18,7 @@ #include "thermostat-server-presets.h" #include "thermostat-server.h" +#include #include using namespace chip; @@ -116,18 +117,22 @@ bool MatchingPendingPresetExists(Delegate * delegate, const PresetStructWithOwne /** * @brief Finds and returns an entry in the Presets attribute list that matches - * a preset, if such an entry exists. The presetToMatch must have a preset handle. + * the given preset handle, if such an entry exists. * * @param[in] delegate The delegate to use. - * @param[in] presetToMatch The preset to match with. - * @param[out] matchingPreset The preset in the Presets attribute list that has the same PresetHandle as the presetToMatch. + * @param[in] presetHandle The preset handle to match against entries in the Presets attribute list. + * @param[out] matchingPreset The preset in the Presets attribute list that has the same PresetHandle as @p presetHandle. + * Only valid if @p found is set to true. + * @param[out] found Set to true if a matching preset was found; set to false if no matching preset exists. * - * @return true if a matching entry was found in the presets attribute list, false otherwise. + * @return CHIP_NO_ERROR if the lookup completed (regardless of whether a matching preset was found); + * otherwise an appropriate error code. */ -bool GetMatchingPresetInPresets(Delegate * delegate, const DataModel::Nullable & presetHandle, - PresetStructWithOwnedMembers & matchingPreset) +CHIP_ERROR GetMatchingPresetInPresets(Delegate * delegate, const ByteSpan & presetHandle, + PresetStructWithOwnedMembers & matchingPreset, bool & found) { - VerifyOrReturnValue(delegate != nullptr, false); + found = false; + VerifyOrReturnError(delegate != nullptr, CHIP_ERROR_INVALID_ARGUMENT); for (uint8_t i = 0; true; i++) { @@ -140,16 +145,18 @@ bool GetMatchingPresetInPresets(Delegate * delegate, const DataModel::Nullable coolingSetpointValue = matchingPreset.GetCoolingSetpoint(); + if (coolingSetpointValue.HasValue()) + { + int16_t constrainedCoolingSetpoint = EnforceCoolingSetpointLimits(coolingSetpointValue.Value(), endpoint); + Status status = OccupiedCoolingSetpoint::Set(endpoint, constrainedCoolingSetpoint); + if (status != Status::Success) + { + ChipLogError(Zcl, "Failed to set OccupiedCoolingSetpoint with status %u", to_underlying(status)); + return status; + } + } + + Optional heatingSetpointValue = matchingPreset.GetHeatingSetpoint(); + if (heatingSetpointValue.HasValue()) + { + int16_t constrainedHeatingSetpoint = EnforceHeatingSetpointLimits(heatingSetpointValue.Value(), endpoint); + Status status = OccupiedHeatingSetpoint::Set(endpoint, constrainedHeatingSetpoint); + if (status != Status::Success) + { + ChipLogError(Zcl, "Failed to set OccupiedHeatingSetpoint with status %u", to_underlying(status)); + return status; + } + } } CHIP_ERROR err = delegate->SetActivePresetHandle(presetHandle); @@ -341,7 +383,13 @@ CHIP_ERROR ThermostatAttrAccess::AppendPendingPreset(Thermostat::Delegate * dele // Per spec we need to check that: // (a) There is an existing non-pending preset with this handle. PresetStructWithOwnedMembers matchingPreset; - if (!GetMatchingPresetInPresets(delegate, preset.GetPresetHandle().Value(), matchingPreset)) + bool found = false; + CHIP_ERROR lookupErr = GetMatchingPresetInPresets(delegate, preset.GetPresetHandle().Value(), matchingPreset, found); + if (lookupErr != CHIP_NO_ERROR) + { + return CHIP_IM_GLOBAL_STATUS(InvalidInState); + } + if (!found) { return CHIP_IM_GLOBAL_STATUS(NotFound); }