Skip to content

Commit

Permalink
Feature/energypreferences (project-chip#30244)
Browse files Browse the repository at this point in the history
* Adding the Energy Preferences XML

* Added just the xml and regenerated.  No implementations yet as
requested.

* regen

* put the cluster back into all-clusters after merge.

* Apply suggestions from code review

Added suggestions for ARRAY vs array

Co-authored-by: Boris Zbarsky <[email protected]>

* regenerate

* Finished up impl for energy-preferences

* Added some error checking for features, and constraint checking.

Also turned on the features in the all-clusters-app

* Restyled by whitespace

* Restyled by clang-format

* Fixed copy paste bug

* Removed some unecessary changes and a comment.

* initialize the delegate pointer

* Put the cluster back in the all-clusters app after the merge

* Fixed minor issues from review

* Restyled by whitespace

* Addressing some review concerns

* Some more comments resolved

* Better documentation, a set of braces I missed last time around, and
added an extra header include.

* fix build error after merge

* Restyled by whitespace

* Restyled by clang-format

* Update src/app/clusters/energy-preference-server/energy-preference-server.cpp

Co-authored-by: Boris Zbarsky <[email protected]>

* Implement proposed API changes with clearer delineation of storage

* Restyled by clang-format

* Typo

* Regen zap, update code to reflect the updated return from Feature::Get

* ZAP regen, to fix merge errors

* Apply suggestions from code review

Co-authored-by: Boris Zbarsky <[email protected]>

* Address suggestion re: optionality conversion

* Fix one more omission

* another small fix

* Fix up copyright headers

* Restyled by clang-format

* Reinitialize storage objects on every iteration

* fix typo

* Restyled by clang-format

* Apply suggestions from code review

Co-authored-by: Boris Zbarsky <[email protected]>

---------

Co-authored-by: Boris Zbarsky <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Robert Szewczyk <[email protected]>
  • Loading branch information
4 people authored Feb 20, 2024
1 parent 6350333 commit b8de5cc
Show file tree
Hide file tree
Showing 12 changed files with 723 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4455,6 +4455,40 @@ provisional cluster EnergyEvse = 153 {
timed command ClearTargets(): DefaultSuccess = 7;
}

/** This cluster provides an interface to specify preferences for how devices should consume energy. */
provisional cluster EnergyPreference = 155 {
revision 1;

enum EnergyPriorityEnum : enum8 {
kComfort = 0;
kSpeed = 1;
kEfficiency = 2;
kWaterConsumption = 3;
}

bitmap Feature : bitmap32 {
kEnergyBalance = 0x1;
kLowPowerModeSensitivity = 0x2;
}

struct BalanceStruct {
percent step = 0;
optional char_string<64> label = 1;
}

readonly attribute optional BalanceStruct energyBalances[] = 0;
attribute optional int8u currentEnergyBalance = 1;
readonly attribute optional EnergyPriorityEnum energyPriorities[] = 2;
readonly attribute optional BalanceStruct lowPowerModeSensitivities[] = 3;
attribute optional int8u currentLowPowerModeSensitivity = 4;
readonly attribute command_id generatedCommandList[] = 65528;
readonly attribute command_id acceptedCommandList[] = 65529;
readonly attribute event_id eventList[] = 65530;
readonly attribute attrib_id attributeList[] = 65531;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}

/** The Power Topology Cluster provides a mechanism for expressing how power is flowing between endpoints. */
provisional cluster PowerTopology = 156 {
revision 1;
Expand Down Expand Up @@ -8361,6 +8395,20 @@ endpoint 1 {
handle command ClearTargets;
}

server cluster EnergyPreference {
callback attribute energyBalances;
ram attribute currentEnergyBalance;
callback attribute energyPriorities;
callback attribute lowPowerModeSensitivities;
ram attribute currentLowPowerModeSensitivity;
callback attribute generatedCommandList;
callback attribute acceptedCommandList;
callback attribute eventList;
callback attribute attributeList;
ram attribute featureMap default = 3;
ram attribute clusterRevision default = 1;
}

server cluster PowerTopology {
callback attribute availableEndpoints;
callback attribute activeEndpoints;
Expand Down
186 changes: 186 additions & 0 deletions examples/all-clusters-app/all-clusters-common/all-clusters-app.zap
Original file line number Diff line number Diff line change
Expand Up @@ -14692,6 +14692,192 @@
}
]
},
{
"name": "Energy Preference",
"code": 155,
"mfgCode": null,
"define": "ENERGY_PREFERENCE_CLUSTER",
"side": "server",
"enabled": 1,
"attributes": [
{
"name": "EnergyBalances",
"code": 0,
"mfgCode": null,
"side": "server",
"type": "array",
"included": 1,
"storageOption": "External",
"singleton": 0,
"bounded": 0,
"defaultValue": null,
"reportable": 1,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "CurrentEnergyBalance",
"code": 1,
"mfgCode": null,
"side": "server",
"type": "int8u",
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
"defaultValue": "",
"reportable": 1,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "EnergyPriorities",
"code": 2,
"mfgCode": null,
"side": "server",
"type": "array",
"included": 1,
"storageOption": "External",
"singleton": 0,
"bounded": 0,
"defaultValue": null,
"reportable": 1,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "LowPowerModeSensitivities",
"code": 3,
"mfgCode": null,
"side": "server",
"type": "array",
"included": 1,
"storageOption": "External",
"singleton": 0,
"bounded": 0,
"defaultValue": null,
"reportable": 1,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "CurrentLowPowerModeSensitivity",
"code": 4,
"mfgCode": null,
"side": "server",
"type": "int8u",
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
"defaultValue": "",
"reportable": 1,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "GeneratedCommandList",
"code": 65528,
"mfgCode": null,
"side": "server",
"type": "array",
"included": 1,
"storageOption": "External",
"singleton": 0,
"bounded": 0,
"defaultValue": null,
"reportable": 1,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "AcceptedCommandList",
"code": 65529,
"mfgCode": null,
"side": "server",
"type": "array",
"included": 1,
"storageOption": "External",
"singleton": 0,
"bounded": 0,
"defaultValue": null,
"reportable": 1,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "EventList",
"code": 65530,
"mfgCode": null,
"side": "server",
"type": "array",
"included": 1,
"storageOption": "External",
"singleton": 0,
"bounded": 0,
"defaultValue": null,
"reportable": 1,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "AttributeList",
"code": 65531,
"mfgCode": null,
"side": "server",
"type": "array",
"included": 1,
"storageOption": "External",
"singleton": 0,
"bounded": 0,
"defaultValue": null,
"reportable": 1,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "FeatureMap",
"code": 65532,
"mfgCode": null,
"side": "server",
"type": "bitmap32",
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
"defaultValue": "3",
"reportable": 1,
"minInterval": 1,
"maxInterval": 65534,
"reportableChange": 0
},
{
"name": "ClusterRevision",
"code": 65533,
"mfgCode": null,
"side": "server",
"type": "int16u",
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
"defaultValue": "1",
"reportable": 1,
"minInterval": 0,
"maxInterval": 65344,
"reportableChange": 0
}
]
},
{
"name": "Window Covering",
"code": 258,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
*
* 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 <app/clusters/energy-preference-server/energy-preference-server.h>

using namespace chip;
using namespace chip::app::Clusters::EnergyPreference;
using namespace chip::app::Clusters::EnergyPreference::Structs;

static BalanceStruct::Type gsEnergyBalances[] = {
{ .step = 0, .label = Optional<chip::CharSpan>("Efficient"_span) },
{ .step = 50, .label = Optional<chip::CharSpan>() },
{ .step = 100, .label = Optional<chip::CharSpan>("Comfort"_span) },
};

static BalanceStruct::Type gsPowerBalances[] = {
{ .step = 0, .label = Optional<chip::CharSpan>("1 Minute"_span) },
{ .step = 12, .label = Optional<chip::CharSpan>("5 Minutes"_span) },
{ .step = 24, .label = Optional<chip::CharSpan>("10 Minutes"_span) },
{ .step = 36, .label = Optional<chip::CharSpan>("15 Minutes"_span) },
{ .step = 48, .label = Optional<chip::CharSpan>("20 Minutes"_span) },
{ .step = 60, .label = Optional<chip::CharSpan>("25 Minutes"_span) },
{ .step = 70, .label = Optional<chip::CharSpan>("30 Minutes"_span) },
{ .step = 80, .label = Optional<chip::CharSpan>("60 Minutes"_span) },
{ .step = 90, .label = Optional<chip::CharSpan>("120 Minutes"_span) },
{ .step = 100, .label = Optional<chip::CharSpan>("Never"_span) },
};

// assumes it'll be the only delegate for it's lifetime.
struct EPrefDelegate : public Delegate
{
EPrefDelegate();
virtual ~EPrefDelegate();

CHIP_ERROR GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
chip::Optional<chip::MutableCharSpan> & aOutLabel) override;
CHIP_ERROR GetEnergyPriorityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, EnergyPriorityEnum & priority) override;
CHIP_ERROR GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
chip::Optional<chip::MutableCharSpan> & aOutLabel) override;

size_t GetNumEnergyBalances(chip::EndpointId aEndpoint) override;
size_t GetNumLowPowerModeSensitivities(chip::EndpointId aEndpoint) override;
};

EPrefDelegate::EPrefDelegate() : Delegate()
{
VerifyOrDie(GetDelegate() == nullptr);
SetDelegate(this);
}

EPrefDelegate::~EPrefDelegate()
{
VerifyOrDie(GetDelegate() == this);
SetDelegate(nullptr);
}

size_t EPrefDelegate::GetNumEnergyBalances(chip::EndpointId aEndpoint)
{
return (ArraySize(gsEnergyBalances));
}

size_t EPrefDelegate::GetNumLowPowerModeSensitivities(chip::EndpointId aEndpoint)
{
return (ArraySize(gsEnergyBalances));
}

CHIP_ERROR
EPrefDelegate::GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
chip::Optional<chip::MutableCharSpan> & aOutLabel)
{
if (aIndex < GetNumEnergyBalances(aEndpoint))
{
aOutStep = gsEnergyBalances[aIndex].step;
if (gsEnergyBalances[aIndex].label.HasValue())
{
chip::CopyCharSpanToMutableCharSpan(gsEnergyBalances[aIndex].label.Value(), aOutLabel.Value());
}
else
{
aOutLabel.ClearValue();
}
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NOT_FOUND;
}

CHIP_ERROR
EPrefDelegate::GetEnergyPriorityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, EnergyPriorityEnum & priority)
{
static EnergyPriorityEnum priorities[] = { EnergyPriorityEnum::kEfficiency, EnergyPriorityEnum::kComfort };

if (aIndex < ArraySize(priorities))
{
priority = priorities[aIndex];
return CHIP_NO_ERROR;
}

return CHIP_ERROR_NOT_FOUND;
}

CHIP_ERROR
EPrefDelegate::GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
chip::Optional<chip::MutableCharSpan> & aOutLabel)
{
if (aIndex < GetNumLowPowerModeSensitivities(aEndpoint))
{
aOutStep = gsPowerBalances[aIndex].step;
if (gsPowerBalances[aIndex].label.HasValue())
{
chip::CopyCharSpanToMutableCharSpan(gsPowerBalances[aIndex].label.Value(), aOutLabel.Value());
}
else
{
aOutLabel.ClearValue();
}
return CHIP_NO_ERROR;
}

return CHIP_ERROR_NOT_FOUND;
}

static EPrefDelegate gsDelegate;
Loading

0 comments on commit b8de5cc

Please sign in to comment.