diff --git a/examples/all-clusters-app/all-clusters-common/all-clusters-app.matter b/examples/all-clusters-app/all-clusters-common/all-clusters-app.matter index dc2d6fe75a3e82..299a480168ec51 100644 --- a/examples/all-clusters-app/all-clusters-common/all-clusters-app.matter +++ b/examples/all-clusters-app/all-clusters-common/all-clusters-app.matter @@ -8813,14 +8813,23 @@ endpoint 1 { ram attribute minSetpointDeadBand default = 0x19; ram attribute controlSequenceOfOperation default = 0x04; ram attribute systemMode default = 0x01; + callback attribute presetTypes; + ram attribute numberOfPresets default = 0; + ram attribute activePresetHandle; + callback attribute presets; + ram attribute presetsSchedulesEditable; callback attribute generatedCommandList; callback attribute acceptedCommandList; callback attribute eventList; callback attribute attributeList; - ram attribute featureMap default = 0x0023; + ram attribute featureMap default = 0x0123; ram attribute clusterRevision default = 6; handle command SetpointRaiseLower; + handle command SetActivePresetRequest; + handle command StartPresetsSchedulesEditRequest; + handle command CancelPresetsSchedulesEditRequest; + handle command CommitPresetsSchedulesRequest; } server cluster FanControl { diff --git a/examples/all-clusters-app/all-clusters-common/all-clusters-app.zap b/examples/all-clusters-app/all-clusters-common/all-clusters-app.zap index 31b6a05057dc74..a052433002d237 100644 --- a/examples/all-clusters-app/all-clusters-common/all-clusters-app.zap +++ b/examples/all-clusters-app/all-clusters-common/all-clusters-app.zap @@ -16393,6 +16393,38 @@ "source": "client", "isIncoming": 1, "isEnabled": 1 + }, + { + "name": "SetActivePresetRequest", + "code": 6, + "mfgCode": null, + "source": "client", + "isIncoming": 1, + "isEnabled": 1 + }, + { + "name": "StartPresetsSchedulesEditRequest", + "code": 7, + "mfgCode": null, + "source": "client", + "isIncoming": 1, + "isEnabled": 1 + }, + { + "name": "CancelPresetsSchedulesEditRequest", + "code": 8, + "mfgCode": null, + "source": "client", + "isIncoming": 1, + "isEnabled": 1 + }, + { + "name": "CommitPresetsSchedulesRequest", + "code": 9, + "mfgCode": null, + "source": "client", + "isIncoming": 1, + "isEnabled": 1 } ], "attributes": [ @@ -16636,6 +16668,86 @@ "maxInterval": 65344, "reportableChange": 0 }, + { + "name": "PresetTypes", + "code": 72, + "mfgCode": null, + "side": "server", + "type": "array", + "included": 1, + "storageOption": "External", + "singleton": 0, + "bounded": 0, + "defaultValue": "", + "reportable": 1, + "minInterval": 1, + "maxInterval": 65534, + "reportableChange": 0 + }, + { + "name": "NumberOfPresets", + "code": 74, + "mfgCode": null, + "side": "server", + "type": "int8u", + "included": 1, + "storageOption": "RAM", + "singleton": 0, + "bounded": 0, + "defaultValue": "0", + "reportable": 1, + "minInterval": 1, + "maxInterval": 65534, + "reportableChange": 0 + }, + { + "name": "ActivePresetHandle", + "code": 78, + "mfgCode": null, + "side": "server", + "type": "octet_string", + "included": 1, + "storageOption": "RAM", + "singleton": 0, + "bounded": 0, + "defaultValue": "", + "reportable": 1, + "minInterval": 1, + "maxInterval": 65534, + "reportableChange": 0 + }, + { + "name": "Presets", + "code": 80, + "mfgCode": null, + "side": "server", + "type": "array", + "included": 1, + "storageOption": "External", + "singleton": 0, + "bounded": 0, + "defaultValue": "", + "reportable": 1, + "minInterval": 1, + "maxInterval": 65534, + "reportableChange": 0 + }, + { + "name": "PresetsSchedulesEditable", + "code": 82, + "mfgCode": null, + "side": "server", + "type": "boolean", + "included": 1, + "storageOption": "RAM", + "singleton": 0, + "bounded": 0, + "defaultValue": "", + "reportable": 1, + "minInterval": 1, + "maxInterval": 65534, + "reportableChange": 0 + }, { "name": "GeneratedCommandList", "code": 65528, @@ -16710,7 +16822,7 @@ "storageOption": "RAM", "singleton": 0, "bounded": 0, - "defaultValue": "0x0023", + "defaultValue": "0x0123", "reportable": 1, "minInterval": 0, "maxInterval": 65344, @@ -26554,4 +26666,4 @@ "parentEndpointIdentifier": null } ] -} +} \ No newline at end of file diff --git a/examples/all-clusters-app/linux/BUILD.gn b/examples/all-clusters-app/linux/BUILD.gn index e8671e701a7227..5b83d8ba085dc4 100644 --- a/examples/all-clusters-app/linux/BUILD.gn +++ b/examples/all-clusters-app/linux/BUILD.gn @@ -64,6 +64,7 @@ source_set("chip-all-clusters-common") { "${chip_root}/examples/all-clusters-app/all-clusters-common/src/tcc-mode.cpp", "${chip_root}/examples/all-clusters-app/all-clusters-common/src/water-heater-mode.cpp", "${chip_root}/examples/all-clusters-app/linux/diagnostic-logs-provider-delegate-impl.cpp", + "${chip_root}/examples/all-clusters-app/linux/thermostat-delegate-impl.cpp", "${chip_root}/examples/energy-management-app/energy-management-common/src/ChargingTargetsMemMgr.cpp", "${chip_root}/examples/energy-management-app/energy-management-common/src/DeviceEnergyManagementDelegateImpl.cpp", "${chip_root}/examples/energy-management-app/energy-management-common/src/DeviceEnergyManagementManager.cpp", diff --git a/examples/all-clusters-app/linux/include/thermostat-delegate-impl.h b/examples/all-clusters-app/linux/include/thermostat-delegate-impl.h new file mode 100644 index 00000000000000..c4daef5fde1d6a --- /dev/null +++ b/examples/all-clusters-app/linux/include/thermostat-delegate-impl.h @@ -0,0 +1,100 @@ +/* + * + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +namespace chip { +namespace app { +namespace Clusters { +namespace Thermostat { + +/** + * The ThermostatDelegate class serves as the instance delegate for storing Presets related information and providing it to the + * Thermostat server code. It also manages the presets attribute and provides methods to write to presets, edit presets, maintain a + * pending presets list and either commit the presets when requested or discard the changes. It also provides APIs to get and set + * the attribute values. + * + */ + +static constexpr uint8_t kMaxNumberOfPresetTypes = 6; + +// TODO: #34556 Support multiple presets of each type. +// We will support only one preset of each preset type. +static constexpr uint8_t kMaxNumberOfPresetsOfEachType = 1; + +class ThermostatDelegate : public Delegate +{ +public: + static inline ThermostatDelegate & GetInstance() { return sInstance; } + + CHIP_ERROR GetPresetTypeAtIndex(size_t index, Structs::PresetTypeStruct::Type & presetType) override; + + uint8_t GetNumberOfPresets() override; + + CHIP_ERROR GetPresetAtIndex(size_t index, PresetStructWithOwnedMembers & preset) override; + + CHIP_ERROR GetActivePresetHandle(MutableByteSpan & activePresetHandle) override; + + CHIP_ERROR SetActivePresetHandle(const DataModel::Nullable & newActivePresetHandle) override; + + CHIP_ERROR AppendToPendingPresetList(const Structs::PresetStruct::Type & preset) override; + + CHIP_ERROR GetPendingPresetAtIndex(size_t index, PresetStructWithOwnedMembers & preset) override; + + CHIP_ERROR ApplyPendingPresets() override; + + void ClearPendingPresetList() override; + +private: + static ThermostatDelegate sInstance; + + ThermostatDelegate(); + ~ThermostatDelegate() = default; + + ThermostatDelegate(const ThermostatDelegate &) = delete; + ThermostatDelegate & operator=(const ThermostatDelegate &) = delete; + + /** + * @brief Initializes the preset types array with all preset types corresponding to PresetScenarioEnum. + */ + void InitializePresetTypes(); + + /** + * @brief Initializes the presets array with some sample presets for testing. + */ + void InitializePresets(); + + uint8_t mNumberOfPresets; + + Structs::PresetTypeStruct::Type mPresetTypes[kMaxNumberOfPresetTypes]; + PresetStructWithOwnedMembers mPresets[kMaxNumberOfPresetTypes * kMaxNumberOfPresetsOfEachType]; + PresetStructWithOwnedMembers mPendingPresets[kMaxNumberOfPresetTypes * kMaxNumberOfPresetsOfEachType]; + + uint8_t mNextFreeIndexInPendingPresetsList; + uint8_t mNextFreeIndexInPresetsList; + + uint8_t mActivePresetHandleData[kPresetHandleSize]; + size_t mActivePresetHandleDataSize; +}; + +} // namespace Thermostat +} // namespace Clusters +} // namespace app +} // namespace chip diff --git a/examples/all-clusters-app/linux/main-common.cpp b/examples/all-clusters-app/linux/main-common.cpp index bc547d5416377c..f462ed7cd1cb19 100644 --- a/examples/all-clusters-app/linux/main-common.cpp +++ b/examples/all-clusters-app/linux/main-common.cpp @@ -25,6 +25,7 @@ #include "dishwasher-mode.h" #include "energy-evse-modes.h" #include "include/diagnostic-logs-provider-delegate-impl.h" +#include "include/thermostat-delegate-impl.h" #include "include/tv-callbacks.h" #include "laundry-dryer-controls-delegate-impl.h" #include "laundry-washer-controls-delegate-impl.h" @@ -46,6 +47,7 @@ #include #include #include +#include #include #include #include @@ -323,3 +325,12 @@ void emberAfDiagnosticLogsClusterInitCallback(chip::EndpointId endpoint) DiagnosticLogsServer::Instance().SetDiagnosticLogsProviderDelegate(endpoint, &logProvider); } + +using namespace chip::app::Clusters::Thermostat; +void emberAfThermostatClusterInitCallback(EndpointId endpoint) +{ + // Register the delegate for the Thermostat + auto & delegate = ThermostatDelegate::GetInstance(); + + SetDefaultDelegate(endpoint, &delegate); +} diff --git a/examples/all-clusters-app/linux/thermostat-delegate-impl.cpp b/examples/all-clusters-app/linux/thermostat-delegate-impl.cpp new file mode 100644 index 00000000000000..491e44a311c3e8 --- /dev/null +++ b/examples/all-clusters-app/linux/thermostat-delegate-impl.cpp @@ -0,0 +1,234 @@ +/* + * + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "include/thermostat-delegate-impl.h" + +#include +#include +#include + +using namespace chip; +using namespace chip::app; +using namespace chip::app::Clusters::Thermostat; +using namespace chip::app::Clusters::Thermostat::Structs; + +ThermostatDelegate ThermostatDelegate::sInstance; + +namespace { + +/** + * @brief Checks if the presets are matching i.e the presetHandles are the same. + * + * @param[in] preset The preset to check. + * @param[in] presetToMatch The preset to match with. + * + * @return true If the presets match, false otherwise. If both preset handles are null, returns false + */ +bool PresetHandlesExistAndMatch(const PresetStructWithOwnedMembers & preset, const PresetStructWithOwnedMembers & presetToMatch) +{ + return !preset.GetPresetHandle().IsNull() && !presetToMatch.GetPresetHandle().IsNull() && + preset.GetPresetHandle().Value().data_equal(presetToMatch.GetPresetHandle().Value()); +} + +} // anonymous namespace + +ThermostatDelegate::ThermostatDelegate() +{ + mNumberOfPresets = kMaxNumberOfPresetTypes * kMaxNumberOfPresetsOfEachType; + mNextFreeIndexInPresetsList = 0; + mNextFreeIndexInPendingPresetsList = 0; + + InitializePresetTypes(); + InitializePresets(); + + memset(mActivePresetHandleData, 0, sizeof(mActivePresetHandleData)); + mActivePresetHandleDataSize = 0; +} + +void ThermostatDelegate::InitializePresetTypes() +{ + PresetScenarioEnum presetScenarioEnumArray[kMaxNumberOfPresetTypes] = { + PresetScenarioEnum::kOccupied, PresetScenarioEnum::kUnoccupied, PresetScenarioEnum::kSleep, + PresetScenarioEnum::kWake, PresetScenarioEnum::kVacation, PresetScenarioEnum::kGoingToSleep + }; + static_assert(ArraySize(presetScenarioEnumArray) <= ArraySize(mPresetTypes)); + + uint8_t index = 0; + for (PresetScenarioEnum presetScenario : presetScenarioEnumArray) + { + mPresetTypes[index].presetScenario = presetScenario; + mPresetTypes[index].numberOfPresets = kMaxNumberOfPresetsOfEachType; + mPresetTypes[index].presetTypeFeatures = + (presetScenario == PresetScenarioEnum::kOccupied || presetScenario == PresetScenarioEnum::kUnoccupied) + ? PresetTypeFeaturesBitmap::kAutomatic + : PresetTypeFeaturesBitmap::kSupportsNames; + index++; + } +} + +void ThermostatDelegate::InitializePresets() +{ + // Initialize the presets with 2 built in presets - occupied and unoccupied. + PresetScenarioEnum presetScenarioEnumArray[2] = { PresetScenarioEnum::kOccupied, PresetScenarioEnum::kUnoccupied }; + static_assert(ArraySize(presetScenarioEnumArray) <= ArraySize(mPresets)); + + uint8_t index = 0; + for (PresetScenarioEnum presetScenario : presetScenarioEnumArray) + { + mPresets[index].SetPresetScenario(presetScenario); + + // Set the preset handle to the preset scenario value as a unique id. + const uint8_t handle[] = { static_cast(presetScenario) }; + mPresets[index].SetPresetHandle(DataModel::MakeNullable(ByteSpan(handle))); + mPresets[index].SetName(NullOptional); + int16_t coolingSetpointValue = static_cast(2500 + (index * 100)); + mPresets[index].SetCoolingSetpoint(MakeOptional(coolingSetpointValue)); + + int16_t heatingSetpointValue = static_cast(2100 - (index * 100)); + mPresets[index].SetHeatingSetpoint(MakeOptional(heatingSetpointValue)); + mPresets[index].SetBuiltIn(DataModel::MakeNullable(true)); + index++; + } + + // Set the value of the next free index in the presets list. + mNextFreeIndexInPresetsList = index; +} + +CHIP_ERROR ThermostatDelegate::GetPresetTypeAtIndex(size_t index, PresetTypeStruct::Type & presetType) +{ + if (index < ArraySize(mPresetTypes)) + { + presetType = mPresetTypes[index]; + return CHIP_NO_ERROR; + } + return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED; +} + +uint8_t ThermostatDelegate::GetNumberOfPresets() +{ + return mNumberOfPresets; +} + +CHIP_ERROR ThermostatDelegate::GetPresetAtIndex(size_t index, PresetStructWithOwnedMembers & preset) +{ + if (index < mNextFreeIndexInPresetsList) + { + preset = mPresets[index]; + return CHIP_NO_ERROR; + } + return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED; +} + +CHIP_ERROR ThermostatDelegate::GetActivePresetHandle(MutableByteSpan & activePresetHandle) +{ + return CopySpanToMutableSpan(ByteSpan(mActivePresetHandleData, mActivePresetHandleDataSize), activePresetHandle); +} + +CHIP_ERROR ThermostatDelegate::SetActivePresetHandle(const DataModel::Nullable & newActivePresetHandle) +{ + if (!newActivePresetHandle.IsNull()) + { + size_t newActivePresetHandleSize = newActivePresetHandle.Value().size(); + if (newActivePresetHandleSize > sizeof(mActivePresetHandleData)) + { + ChipLogError(NotSpecified, + "Failed to set ActivePresetHandle. newActivePresetHandle size %u is larger than preset handle size %u", + static_cast(newActivePresetHandleSize), static_cast(kPresetHandleSize)); + return CHIP_ERROR_NO_MEMORY; + } + memcpy(mActivePresetHandleData, newActivePresetHandle.Value().data(), newActivePresetHandleSize); + mActivePresetHandleDataSize = newActivePresetHandleSize; + ChipLogDetail(NotSpecified, "Set ActivePresetHandle to "); + ChipLogByteSpan(NotSpecified, newActivePresetHandle.Value()); + } + else + { + memset(mActivePresetHandleData, 0, sizeof(mActivePresetHandleData)); + mActivePresetHandleDataSize = 0; + ChipLogDetail(NotSpecified, "Clear ActivePresetHandle"); + } + return CHIP_NO_ERROR; +} + +CHIP_ERROR ThermostatDelegate::AppendToPendingPresetList(const PresetStruct::Type & preset) +{ + if (mNextFreeIndexInPendingPresetsList < ArraySize(mPendingPresets)) + { + mPendingPresets[mNextFreeIndexInPendingPresetsList].SetPresetScenario(preset.presetScenario); + mPendingPresets[mNextFreeIndexInPendingPresetsList].SetPresetHandle(preset.presetHandle); + mPendingPresets[mNextFreeIndexInPendingPresetsList].SetName(preset.name); + mPendingPresets[mNextFreeIndexInPendingPresetsList].SetCoolingSetpoint(preset.coolingSetpoint); + mPendingPresets[mNextFreeIndexInPendingPresetsList].SetHeatingSetpoint(preset.heatingSetpoint); + mPendingPresets[mNextFreeIndexInPendingPresetsList].SetBuiltIn(preset.builtIn); + mNextFreeIndexInPendingPresetsList++; + return CHIP_NO_ERROR; + } + return CHIP_ERROR_WRITE_FAILED; +} + +CHIP_ERROR ThermostatDelegate::GetPendingPresetAtIndex(size_t index, PresetStructWithOwnedMembers & preset) +{ + if (index < mNextFreeIndexInPendingPresetsList) + { + preset = mPendingPresets[index]; + return CHIP_NO_ERROR; + } + return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED; +} + +CHIP_ERROR ThermostatDelegate::ApplyPendingPresets() +{ + + // TODO: #34546 - Need to support deletion of presets that are removed from Presets. + for (uint8_t indexInPendingPresets = 0; indexInPendingPresets < mNextFreeIndexInPendingPresetsList; indexInPendingPresets++) + { + const PresetStructWithOwnedMembers & pendingPreset = mPendingPresets[indexInPendingPresets]; + + bool found = false; + for (uint8_t indexInPresets = 0; indexInPresets < mNextFreeIndexInPresetsList; indexInPresets++) + { + if (PresetHandlesExistAndMatch(mPresets[indexInPresets], pendingPreset)) + { + found = true; + + // Replace the preset with the pending preset + mPresets[indexInPresets] = pendingPreset; + } + } + + // If pending preset was not found in the Presets list, append to the Presets list. + if (!found) + { + + mPresets[mNextFreeIndexInPresetsList] = pendingPreset; + + // TODO: #34556 Since we support only one preset of each type, using the octet string containing the preset scenario + // suffices as the unique preset handle. Need to fix this to actually provide unique handles once multiple presets of + // each type are supported. + const uint8_t handle[] = { static_cast(pendingPreset.GetPresetScenario()) }; + mPresets[mNextFreeIndexInPresetsList].SetPresetHandle(DataModel::MakeNullable(ByteSpan(handle))); + mNextFreeIndexInPresetsList++; + } + } + return CHIP_NO_ERROR; +} + +void ThermostatDelegate::ClearPendingPresetList() +{ + mNextFreeIndexInPendingPresetsList = 0; +}