Skip to content
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

chef: Fix FanControl mode/speed interdependence behavior #36256

Merged
merged 16 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
281 changes: 212 additions & 69 deletions examples/chef/common/chef-fan-control-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,33 @@ using namespace chip::app::Clusters::FanControl::Attributes;
using Protocols::InteractionModel::Status;

namespace {
class ChefFanControlManager : public AttributeAccessInterface, public Delegate
class ChefFanControlManager : public Delegate
{
public:
ChefFanControlManager(EndpointId aEndpointId) :
AttributeAccessInterface(Optional<EndpointId>(aEndpointId), FanControl::Id), Delegate(aEndpointId)
{}
ChefFanControlManager(EndpointId aEndpointId) : Delegate(aEndpointId) {}

CHIP_ERROR Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder) override;
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
void Init();
void HandleFanControlAttributeChange(AttributeId attributeId, uint8_t type, uint16_t size, uint8_t * value);
Status HandleStep(StepDirectionEnum aDirection, bool aWrap, bool aLowestOff) override;
DataModel::Nullable<uint8_t> GetSpeedSetting();
DataModel::Nullable<Percent> GetPercentSetting();

private:
Nullable<uint8_t> mPercentSetting{};
Nullable<uint8_t> mSpeedSetting{};
uint8_t mPercentCurrent;
uint8_t mSpeedCurrent;
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved

// Fan Mode Limits
static constexpr int FAN_MODE_LOW_LOWER_BOUND = 1;
static constexpr int FAN_MODE_LOW_UPPER_BOUND = 3;
static constexpr int FAN_MODE_MEDIUM_LOWER_BOUND = 4;
static constexpr int FAN_MODE_MEDIUM_UPPER_BOUND = 7;
static constexpr int FAN_MODE_HIGH_LOWER_BOUND = 8;
static constexpr int FAN_MODE_HIGH_UPPER_BOUND = 10;
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved

void FanModeWriteCallback(FanControl::FanModeEnum aNewFanMode);
void SpeedSettingWriteCallback(uint8_t aNewSpeedSetting);
void PercentSettingWriteCallback(uint8_t aNewPercentSetting);
void SetSpeedSetting(DataModel::Nullable<uint8_t> aNewSpeedSetting);
};

static std::unique_ptr<ChefFanControlManager> mFanControlManager;
Expand Down Expand Up @@ -99,98 +112,223 @@ Status ChefFanControlManager::HandleStep(StepDirectionEnum aDirection, bool aWra
return SpeedSetting::Set(mEndpoint, newSpeedSetting);
}

CHIP_ERROR ChefFanControlManager::Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder)
void ChefFanControlManager::HandleFanControlAttributeChange(AttributeId attributeId, uint8_t type, uint16_t size, uint8_t * value)
{
VerifyOrDie(aPath.mClusterId == FanControl::Id);
VerifyOrDie(aPath.mEndpointId == mEndpoint);

switch (aPath.mAttributeId)
ChipLogProgress(NotSpecified, "ChefFanControlManager::HandleFanControlAttributeChange");
switch (attributeId)
{
case SpeedSetting::Id: {
Nullable<uint8_t> newSpeedSetting;
ReturnErrorOnFailure(aDecoder.Decode(newSpeedSetting));

// Ensure new speed is in bounds
case FanControl::Attributes::PercentSetting::Id: {
ChipLogProgress(NotSpecified, "ChefFanControlManager::HandleFanControlAttributeChange PercentSetting");
DataModel::Nullable<Percent> percentSetting = static_cast<DataModel::Nullable<uint8_t>>(*value);
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
if (percentSetting.IsNull())
{
uint8_t maxSpeedSetting = 0;
Protocols::InteractionModel::Status status = SpeedMax::Get(mEndpoint, &maxSpeedSetting);
VerifyOrReturnError(status == Protocols::InteractionModel::Status::Success, CHIP_IM_GLOBAL_STATUS(Failure));

if (!newSpeedSetting.IsNull() && newSpeedSetting.Value() > maxSpeedSetting)
{
return CHIP_IM_GLOBAL_STATUS(ConstraintError);
}
PercentSettingWriteCallback(0);
}

// Only act on changed.
if (newSpeedSetting != mSpeedSetting)
else
{
mSpeedSetting = newSpeedSetting;
PercentSettingWriteCallback(percentSetting.Value());
}
break;
}

// Mark both the setting AND the current dirty, since the current always
// tracks the target for our product.
MatterReportingAttributeChangeCallback(mEndpoint, FanControl::Id, Attributes::SpeedSetting::Id);
MatterReportingAttributeChangeCallback(mEndpoint, FanControl::Id, Attributes::SpeedCurrent::Id);
case FanControl::Attributes::SpeedSetting::Id: {
ChipLogProgress(NotSpecified, "ChefFanControlManager::HandleFanControlAttributeChange SpeedSetting");

DataModel::Nullable<uint8_t> speedSetting = static_cast<DataModel::Nullable<uint8_t>>(*value);
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
if (speedSetting.IsNull())
{
SpeedSettingWriteCallback(0);
}
else
{
SpeedSettingWriteCallback(speedSetting.Value());
}
break;
}

case FanControl::Attributes::FanMode::Id: {
ChipLogProgress(NotSpecified, "ChefFanControlManager::HandleFanControlAttributeChange FanMode");

FanControl::FanModeEnum fanMode = static_cast<FanControl::FanModeEnum>(*value);
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
FanModeWriteCallback(fanMode);
break;
}
case PercentSetting::Id: {
Nullable<uint8_t> newPercentSetting;
ReturnErrorOnFailure(aDecoder.Decode(newPercentSetting));

// Ensure new speed in percent is valid.
if (!newPercentSetting.IsNull() && newPercentSetting.Value() > 100)
default: {
break;
}
}
}

void ChefFanControlManager::PercentSettingWriteCallback(uint8_t aNewPercentSetting)
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
{
if (aNewPercentSetting != mPercentCurrent)
{
ChipLogDetail(NotSpecified, "ChefFanControlManager::PercentSettingWriteCallback: %d", aNewPercentSetting);
mPercentCurrent = aNewPercentSetting;
Status status = FanControl::Attributes::PercentCurrent::Set(mEndpoint, mPercentCurrent);
if (status != Status::Success)
{
return CHIP_IM_GLOBAL_STATUS(ConstraintError);
ChipLogError(NotSpecified,
"ChefFanControlManager::PercentSettingWriteCallback: failed to set PercentCurrent attribute: %d",
to_underlying(status));
return;
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

// Only act on changed.
if (newPercentSetting != mPercentSetting)
void ChefFanControlManager::SpeedSettingWriteCallback(uint8_t aNewSpeedSetting)
{
if (aNewSpeedSetting != mSpeedCurrent)
{
ChipLogDetail(NotSpecified, "ChefFanControlManager::SpeedSettingWriteCallback: %d", aNewSpeedSetting);
mSpeedCurrent = aNewSpeedSetting;
Status status = FanControl::Attributes::SpeedCurrent::Set(mEndpoint, aNewSpeedSetting);
if (status != Status::Success)
{
mPercentSetting = newPercentSetting;
ChipLogError(NotSpecified, "ChefFanControlManager::SpeedSettingWriteCallback: failed to set SpeedCurrent attribute: %d",
to_underlying(status));
return;
}

// Mark both the setting AND the current dirty, since the current always
// tracks the target for our product.
MatterReportingAttributeChangeCallback(mEndpoint, FanControl::Id, Attributes::PercentSetting::Id);
MatterReportingAttributeChangeCallback(mEndpoint, FanControl::Id, Attributes::PercentCurrent::Id);
// Determine if the speed change should also change the fan mode
if (mSpeedCurrent == 0)
{
FanControl::Attributes::FanMode::Set(mEndpoint, FanControl::FanModeEnum::kOff);
}
else if (mSpeedCurrent <= FAN_MODE_LOW_UPPER_BOUND)
{
FanControl::Attributes::FanMode::Set(mEndpoint, FanControl::FanModeEnum::kLow);
}
else if (mSpeedCurrent <= FAN_MODE_MEDIUM_UPPER_BOUND)
{
FanControl::Attributes::FanMode::Set(mEndpoint, FanControl::FanModeEnum::kMedium);
}
else if (mSpeedCurrent <= FAN_MODE_HIGH_UPPER_BOUND)
{
FanControl::Attributes::FanMode::Set(mEndpoint, FanControl::FanModeEnum::kHigh);
}
}
}

void ChefFanControlManager::FanModeWriteCallback(FanControl::FanModeEnum aNewFanMode)
{
ChipLogDetail(NotSpecified, "ChefFanControlManager::FanModeWriteCallback: %d", (uint8_t) aNewFanMode);
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
switch (aNewFanMode)
{
case FanControl::FanModeEnum::kOff: {
if (mSpeedCurrent != 0)
{
DataModel::Nullable<uint8_t> speedSetting(0);
SetSpeedSetting(speedSetting);
}
break;
}
default:
case FanControl::FanModeEnum::kLow: {
if (mSpeedCurrent < FAN_MODE_LOW_LOWER_BOUND || mSpeedCurrent > FAN_MODE_LOW_UPPER_BOUND)
{
DataModel::Nullable<uint8_t> speedSetting(FAN_MODE_LOW_LOWER_BOUND);
SetSpeedSetting(speedSetting);
}
break;
}

// Fall through goes to attribute store legacy handling.
return CHIP_NO_ERROR;
case FanControl::FanModeEnum::kMedium: {
if (mSpeedCurrent < FAN_MODE_MEDIUM_LOWER_BOUND || mSpeedCurrent > FAN_MODE_MEDIUM_UPPER_BOUND)
{
DataModel::Nullable<uint8_t> speedSetting(FAN_MODE_MEDIUM_LOWER_BOUND);
SetSpeedSetting(speedSetting);
}
break;
}
case FanControl::FanModeEnum::kOn:
case FanControl::FanModeEnum::kHigh: {
if (mSpeedCurrent < FAN_MODE_HIGH_LOWER_BOUND || mSpeedCurrent > FAN_MODE_HIGH_UPPER_BOUND)
{
DataModel::Nullable<uint8_t> speedSetting(FAN_MODE_HIGH_LOWER_BOUND);
SetSpeedSetting(speedSetting);
}
break;
}
case FanControl::FanModeEnum::kSmart:
case FanControl::FanModeEnum::kAuto: {
ChipLogProgress(NotSpecified, "ChefFanControlManager::FanModeWriteCallback: Auto");
break;
}
case FanControl::FanModeEnum::kUnknownEnumValue: {
ChipLogProgress(NotSpecified, "ChefFanControlManager::FanModeWriteCallback: Unknown");
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
}

CHIP_ERROR ChefFanControlManager::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
void ChefFanControlManager::SetSpeedSetting(DataModel::Nullable<uint8_t> aNewSpeedSetting)
{
VerifyOrDie(aPath.mClusterId == FanControl::Id);
VerifyOrDie(aPath.mEndpointId == mEndpoint);
if (aNewSpeedSetting.IsNull())
{
ChipLogError(NotSpecified, "ChefFanControlManager::SetSpeedSetting: invalid value");
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
return;
}

if (aNewSpeedSetting.Value() != mSpeedCurrent)
{
Status status = FanControl::Attributes::SpeedSetting::Set(mEndpoint, aNewSpeedSetting);
if (status != Status::Success)
{
ChipLogError(NotSpecified, "ChefFanControlManager::SetSpeedSetting: failed to set SpeedSetting attribute: %d",
to_underlying(status));
}
}
}

switch (aPath.mAttributeId)
void ChefFanControlManager::Init()
{
DataModel::Nullable<Percent> percentSetting = GetPercentSetting();
if (percentSetting.IsNull())
{
PercentSettingWriteCallback(0);
}
else
{
case PercentCurrent::Id: {
// Current percents always tracks setting immediately in our implementation.
return aEncoder.Encode(mPercentSetting.ValueOr(0));
PercentSettingWriteCallback(percentSetting.Value());
}
case PercentSetting::Id: {
return aEncoder.Encode(mPercentSetting);

DataModel::Nullable<uint8_t> speedSetting = GetSpeedSetting();
if (speedSetting.IsNull())
{
SpeedSettingWriteCallback(0);
}
case SpeedCurrent::Id: {
// Current speed always tracks setting immediately in our implementation.
return aEncoder.Encode(mSpeedSetting.ValueOr(0));
else
{
SpeedSettingWriteCallback(speedSetting.Value());
}
case SpeedSetting::Id: {
return aEncoder.Encode(mSpeedSetting);
}

DataModel::Nullable<Percent> ChefFanControlManager::GetPercentSetting()
{
DataModel::Nullable<Percent> percentSetting;
Status status = FanControl::Attributes::PercentSetting::Get(mEndpoint, percentSetting);

if (status != Status::Success)
{
ChipLogError(NotSpecified, "ChefFanControlManager::GetPercentSetting: failed to get PercentSetting attribute: %d",
to_underlying(status));
}
default:
break;

return percentSetting;
}

DataModel::Nullable<uint8_t> ChefFanControlManager::GetSpeedSetting()
{
DataModel::Nullable<uint8_t> speedSetting;
Status status = FanControl::Attributes::SpeedSetting::Get(mEndpoint, speedSetting);

if (status != Status::Success)
{
ChipLogError(NotSpecified, "ChefFanControlManager::GetSpeedSetting: failed to get SpeedSetting attribute: %d",
to_underlying(status));
}
return CHIP_NO_ERROR;

return speedSetting;
}

} // anonymous namespace
Expand All @@ -199,6 +337,11 @@ void emberAfFanControlClusterInitCallback(EndpointId endpoint)
{
VerifyOrDie(!mFanControlManager);
mFanControlManager = std::make_unique<ChefFanControlManager>(endpoint);
AttributeAccessInterfaceRegistry::Instance().Register(mFanControlManager.get());
FanControl::SetDefaultDelegate(endpoint, mFanControlManager.get());
mFanControlManager->Init();
}

void HandleFanControlAttributeChange(AttributeId attributeId, uint8_t type, uint16_t size, uint8_t * value)
{
mFanControlManager->HandleFanControlAttributeChange(attributeId, type, size, value);
}
22 changes: 22 additions & 0 deletions examples/chef/common/chef-fan-control-manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
*
* Copyright (c) 2024 Project CHIP Authors
*
* 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 <lib/core/DataModelTypes.h>

#ifdef MATTER_DM_PLUGIN_FAN_CONTROL_SERVER
void HandleFanControlAttributeChange(AttributeId attributeId, uint8_t type, uint16_t size, uint8_t * value);
#endif
7 changes: 7 additions & 0 deletions examples/chef/common/stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const Clusters::Descriptor::Structs::SemanticTagStruct::Type freezerTagList[]
#include "chef-operational-state-delegate-impl.h"
#endif // MATTER_DM_PLUGIN_OPERATIONAL_STATE_SERVER

#ifdef MATTER_DM_PLUGIN_FAN_CONTROL_SERVER
#include "chef-fan-control-manager.h"
#endif // MATTER_DM_PLUGIN_FAN_CONTROL_SERVER
#ifdef MATTER_DM_PLUGIN_TEMPERATURE_CONTROL_SERVER
#include "temperature-control/static-supported-temperature-levels.h"
#endif // MATTER_DM_PLUGIN_TEMPERATURE_CONTROL_SERVER
Expand Down Expand Up @@ -253,6 +256,10 @@ void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath &

// WIP Apply attribute change to Light
}
else if (clusterId == FanControl::Id)
{
HandleFanControlAttributeChange(attributeId, type, size, value);
}
}

/** @brief OnOff Cluster Init
Expand Down
2 changes: 1 addition & 1 deletion examples/chef/devices/rootnode_fan_7N2TobIlOX.matter
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,7 @@ endpoint 1 {
ram attribute fanModeSequence default = 2;
persist attribute percentSetting default = 0x00;
ram attribute percentCurrent default = 0x00;
ram attribute speedMax default = 100;
ram attribute speedMax default = 10;
persist attribute speedSetting default = 0x00;
persist attribute speedCurrent default = 0;
ram attribute rockSupport default = 0x03;
Expand Down
Loading
Loading