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 7 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
278 changes: 203 additions & 75 deletions examples/chef/common/chef-fan-control-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,34 @@ 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 SetSpeedCurrent(uint8_t aNewSpeedCurrent);
void SetPercentCurrent(uint8_t aNewPercentCurrent);
void SetSpeedSetting(DataModel::Nullable<uint8_t> aNewSpeedSetting);
static FanControl::FanModeEnum SpeedToFanMode(uint8_t speed);
};

static std::unique_ptr<ChefFanControlManager> mFanControlManager;
Expand Down Expand Up @@ -99,98 +113,207 @@ 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);
ChipLogProgress(NotSpecified, "ChefFanControlManager::HandleFanControlAttributeChange");
switch (attributeId)
{
case FanControl::Attributes::PercentSetting::Id: {
ChipLogProgress(NotSpecified, "ChefFanControlManager::HandleFanControlAttributeChange PercentSetting");
DataModel::Nullable<Percent> percentSetting =
NumericAttributeTraits<DataModel::Nullable<Percent>>::StorageToWorking(*value);
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved

// If the PercentSetting was set to Null (likely the mode was set to kAuto).
// Update PercentCurrent to 0 here as it can't be nullable and we don't have a "real"
// value for the Fan percentage. The spec doesn't specify what to do in this case.
// If not Null, PercentCurrent tracks PercentSetting's value.
SetPercentCurrent(percentSetting.IsNull() ? 0 : percentSetting.Value());
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
break;
}

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

DataModel::Nullable<uint8_t> speedSetting = NumericAttributeTraits<DataModel::Nullable<uint8_t>>::StorageToWorking(*value);
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
SetSpeedCurrent(speedSetting.IsNull() ? 0 : speedSetting.Value());
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
// Determine if the speed change should also change the fan mode
FanControl::Attributes::FanMode::Set(mEndpoint, SpeedToFanMode(mSpeedCurrent));
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;
}

switch (aPath.mAttributeId)
default: {
break;
}
}
}

FanControl::FanModeEnum ChefFanControlManager::SpeedToFanMode(uint8_t speed)
{
if (speed == 0)
{
case SpeedSetting::Id: {
Nullable<uint8_t> newSpeedSetting;
ReturnErrorOnFailure(aDecoder.Decode(newSpeedSetting));
return FanControl::FanModeEnum::kOff;
}
if (speed <= FAN_MODE_LOW_UPPER_BOUND)
{
return FanControl::FanModeEnum::kLow;
}
if (speed <= FAN_MODE_MEDIUM_UPPER_BOUND)
{
return FanControl::FanModeEnum::kMedium;
}
if (speed <= FAN_MODE_HIGH_UPPER_BOUND)
{
return FanControl::FanModeEnum::kHigh;
}
return FanControl::FanModeEnum::kUnknownEnumValue;
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
}

void ChefFanControlManager::SetPercentCurrent(uint8_t aNewPercentCurrent)
{
if (aNewPercentCurrent != mPercentCurrent)
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}

ChipLogDetail(NotSpecified, "ChefFanControlManager::SetPercentCurrent: %d", aNewPercentCurrent);
mPercentCurrent = aNewPercentCurrent;
Status status = FanControl::Attributes::PercentCurrent::Set(mEndpoint, mPercentCurrent);
if (status != Status::Success)
{
ChipLogError(NotSpecified, "ChefFanControlManager::SetPercentCurrent: failed to set PercentCurrent attribute: %d",
to_underlying(status));
}
}

// Ensure new speed is in bounds
void ChefFanControlManager::SetSpeedCurrent(uint8_t aNewSpeedCurrent)
{
if (aNewSpeedCurrent != mSpeedCurrent)
{
return;
}

ChipLogDetail(NotSpecified, "ChefFanControlManager::SetSpeedCurrent: %d", aNewSpeedCurrent);
mSpeedCurrent = aNewSpeedCurrent;
Status status = FanControl::Attributes::SpeedCurrent::Set(mEndpoint, aNewSpeedCurrent);
if (status != Status::Success)
{
ChipLogError(NotSpecified, "ChefFanControlManager::SetSpeedCurrent: failed to set SpeedCurrent attribute: %d",
to_underlying(status));
}
}

void ChefFanControlManager::FanModeWriteCallback(FanControl::FanModeEnum aNewFanMode)
{
ChipLogDetail(NotSpecified, "ChefFanControlManager::FanModeWriteCallback: %d", to_underlying(aNewFanMode));
switch (aNewFanMode)
{
case FanControl::FanModeEnum::kOff: {
if (mSpeedCurrent != 0)
{
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);
}
DataModel::Nullable<uint8_t> speedSetting(0);
SetSpeedSetting(speedSetting);
}

// Only act on changed.
if (newSpeedSetting != mSpeedSetting)
break;
}
case FanControl::FanModeEnum::kLow: {
if (mSpeedCurrent < FAN_MODE_LOW_LOWER_BOUND || mSpeedCurrent > FAN_MODE_LOW_UPPER_BOUND)
{
mSpeedSetting = newSpeedSetting;

// 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);
DataModel::Nullable<uint8_t> speedSetting(FAN_MODE_LOW_LOWER_BOUND);
SetSpeedSetting(speedSetting);
}

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)
case FanControl::FanModeEnum::kMedium: {
if (mSpeedCurrent < FAN_MODE_MEDIUM_LOWER_BOUND || mSpeedCurrent > FAN_MODE_MEDIUM_UPPER_BOUND)
{
return CHIP_IM_GLOBAL_STATUS(ConstraintError);
DataModel::Nullable<uint8_t> speedSetting(FAN_MODE_MEDIUM_LOWER_BOUND);
SetSpeedSetting(speedSetting);
}

// Only act on changed.
if (newPercentSetting != mPercentSetting)
break;
}
case FanControl::FanModeEnum::kOn:
case FanControl::FanModeEnum::kHigh: {
if (mSpeedCurrent < FAN_MODE_HIGH_LOWER_BOUND || mSpeedCurrent > FAN_MODE_HIGH_UPPER_BOUND)
{
mPercentSetting = newPercentSetting;

// 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);
DataModel::Nullable<uint8_t> speedSetting(FAN_MODE_HIGH_LOWER_BOUND);
SetSpeedSetting(speedSetting);
}

break;
}
default:
case FanControl::FanModeEnum::kSmart:
case FanControl::FanModeEnum::kAuto: {
ChipLogProgress(NotSpecified, "ChefFanControlManager::FanModeWriteCallback: Auto");
break;
}

// Fall through goes to attribute store legacy handling.
return CHIP_NO_ERROR;
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);

switch (aPath.mAttributeId)
if (aNewSpeedSetting.IsNull())
{
case PercentCurrent::Id: {
// Current percents always tracks setting immediately in our implementation.
return aEncoder.Encode(mPercentSetting.ValueOr(0));
ChipLogError(NotSpecified, "ChefFanControlManager::SetSpeedSetting: invalid value");
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
return;
}
case PercentSetting::Id: {
return aEncoder.Encode(mPercentSetting);
}
case SpeedCurrent::Id: {
// Current speed always tracks setting immediately in our implementation.
return aEncoder.Encode(mSpeedSetting.ValueOr(0));

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));
}
}
case SpeedSetting::Id: {
return aEncoder.Encode(mSpeedSetting);
}

void ChefFanControlManager::Init()
{
DataModel::Nullable<Percent> percentSetting = GetPercentSetting();
SetPercentCurrent(percentSetting.IsNull() ? 0 : percentSetting.Value());

DataModel::Nullable<uint8_t> speedSetting = GetSpeedSetting();
SetSpeedCurrent(speedSetting.IsNull() ? 0 : speedSetting.Value());
soares-sergio marked this conversation as resolved.
Show resolved Hide resolved
}

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 +322,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
9 changes: 9 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,12 @@ void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath &

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

/** @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