-
Notifications
You must be signed in to change notification settings - Fork 2.4k
[Fix] Apply preset setpoints when changing active preset #43587
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
ff9378b
ca6eb82
4a13b08
c10c4cb
16082d7
1d2a162
eddb5a0
cc54de0
44dab5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| #include "thermostat-server-presets.h" | ||
| #include "thermostat-server.h" | ||
|
|
||
| #include <app-common/zap-generated/attributes/Accessors.h> | ||
| #include <platform/internal/CHIPDeviceLayerInternal.h> | ||
|
|
||
| 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<ByteSpan> & presetHandle, | ||
| PresetStructWithOwnedMembers & matchingPreset) | ||
| CHIP_ERROR GetMatchingPresetInPresets(Delegate * delegate, const ByteSpan & presetHandle, | ||
| PresetStructWithOwnedMembers & matchingPreset, bool & found) | ||
| { | ||
| VerifyOrReturnValue(delegate != nullptr, false); | ||
| VerifyOrReturnError(delegate != nullptr, CHIP_ERROR_INVALID_ARGUMENT); | ||
| found = false; | ||
lboue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| for (uint8_t i = 0; true; i++) | ||
| { | ||
|
|
@@ -140,16 +145,17 @@ bool GetMatchingPresetInPresets(Delegate * delegate, const DataModel::Nullable<B | |
| if (err != CHIP_NO_ERROR) | ||
| { | ||
| ChipLogError(Zcl, "GetMatchingPresetInPresets: GetPresetAtIndex failed with error %" CHIP_ERROR_FORMAT, err.Format()); | ||
| return false; | ||
| return err; | ||
| } | ||
|
|
||
| // Note: presets coming from our delegate always have a handle. | ||
| if (presetHandle.Value().data_equal(matchingPreset.GetPresetHandle().Value())) | ||
| if (presetHandle.data_equal(matchingPreset.GetPresetHandle().Value())) | ||
| { | ||
| return true; | ||
| found = true; | ||
| return CHIP_NO_ERROR; | ||
lboue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| return false; | ||
| return CHIP_NO_ERROR; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -302,10 +308,44 @@ Status ThermostatAttrAccess::SetActivePreset(EndpointId endpoint, DataModel::Nul | |
| return Status::InvalidInState; | ||
| } | ||
|
|
||
| // If the preset handle passed in the command is not present in the Presets attribute, return INVALID_COMMAND. | ||
| if (!presetHandle.IsNull() && !IsPresetHandlePresentInPresets(delegate, presetHandle.Value())) | ||
| if (!presetHandle.IsNull()) | ||
| { | ||
| return Status::InvalidCommand; | ||
| PresetStructWithOwnedMembers matchingPreset; | ||
| // Look up the preset once and reuse it to apply any setpoints it carries. | ||
| bool found = false; | ||
| CHIP_ERROR lookupErr = GetMatchingPresetInPresets(delegate, presetHandle.Value(), matchingPreset, found); | ||
| if (lookupErr != CHIP_NO_ERROR) | ||
| { | ||
| return Status::InvalidInState; | ||
lboue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| if (!found) | ||
| { | ||
| return Status::InvalidCommand; | ||
lboue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| Optional<int16_t> coolingSetpointValue = matchingPreset.GetCoolingSetpoint(); | ||
| if (coolingSetpointValue.HasValue()) | ||
| { | ||
| int16_t constrainedCoolingSetpoint = EnforceCoolingSetpointLimits(coolingSetpointValue.Value(), endpoint); | ||
| Status status = OccupiedCoolingSetpoint::Set(endpoint, constrainedCoolingSetpoint); | ||
| if (status != Status::Success) | ||
lboue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| ChipLogError(Zcl, "Failed to set OccupiedCoolingSetpoint with status %u", to_underlying(status)); | ||
| return status; | ||
| } | ||
|
Comment on lines
+331
to
+337
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I addressed the re-entrancy concern directly in I also kept the existing error propagation behavior ( |
||
| } | ||
|
|
||
| Optional<int16_t> heatingSetpointValue = matchingPreset.GetHeatingSetpoint(); | ||
| if (heatingSetpointValue.HasValue()) | ||
| { | ||
| int16_t constrainedHeatingSetpoint = EnforceHeatingSetpointLimits(heatingSetpointValue.Value(), endpoint); | ||
| Status status = OccupiedHeatingSetpoint::Set(endpoint, constrainedHeatingSetpoint); | ||
| if (status != Status::Success) | ||
lboue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| ChipLogError(Zcl, "Failed to set OccupiedHeatingSetpoint with status %u", to_underlying(status)); | ||
| return status; | ||
| } | ||
| } | ||
|
Comment on lines
+328
to
+350
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This re-entrancy path is now guarded in |
||
| } | ||
|
|
||
| CHIP_ERROR err = delegate->SetActivePresetHandle(presetHandle); | ||
|
|
@@ -341,7 +381,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); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.