Skip to content

Commit b8de5cc

Browse files
drempelgbzbarsky-applerestyled-commitsrobszewczyk
authored
Feature/energypreferences (#30244)
* 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 <bzbarsky@apple.com> * 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 <bzbarsky@apple.com> * 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 <bzbarsky@apple.com> * 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 <bzbarsky@apple.com> --------- Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Robert Szewczyk <szewczyk@google.com>
1 parent 6350333 commit b8de5cc

File tree

12 files changed

+723
-3
lines changed

12 files changed

+723
-3
lines changed

examples/all-clusters-app/all-clusters-common/all-clusters-app.matter

+48
Original file line numberDiff line numberDiff line change
@@ -4455,6 +4455,40 @@ provisional cluster EnergyEvse = 153 {
44554455
timed command ClearTargets(): DefaultSuccess = 7;
44564456
}
44574457

4458+
/** This cluster provides an interface to specify preferences for how devices should consume energy. */
4459+
provisional cluster EnergyPreference = 155 {
4460+
revision 1;
4461+
4462+
enum EnergyPriorityEnum : enum8 {
4463+
kComfort = 0;
4464+
kSpeed = 1;
4465+
kEfficiency = 2;
4466+
kWaterConsumption = 3;
4467+
}
4468+
4469+
bitmap Feature : bitmap32 {
4470+
kEnergyBalance = 0x1;
4471+
kLowPowerModeSensitivity = 0x2;
4472+
}
4473+
4474+
struct BalanceStruct {
4475+
percent step = 0;
4476+
optional char_string<64> label = 1;
4477+
}
4478+
4479+
readonly attribute optional BalanceStruct energyBalances[] = 0;
4480+
attribute optional int8u currentEnergyBalance = 1;
4481+
readonly attribute optional EnergyPriorityEnum energyPriorities[] = 2;
4482+
readonly attribute optional BalanceStruct lowPowerModeSensitivities[] = 3;
4483+
attribute optional int8u currentLowPowerModeSensitivity = 4;
4484+
readonly attribute command_id generatedCommandList[] = 65528;
4485+
readonly attribute command_id acceptedCommandList[] = 65529;
4486+
readonly attribute event_id eventList[] = 65530;
4487+
readonly attribute attrib_id attributeList[] = 65531;
4488+
readonly attribute bitmap32 featureMap = 65532;
4489+
readonly attribute int16u clusterRevision = 65533;
4490+
}
4491+
44584492
/** The Power Topology Cluster provides a mechanism for expressing how power is flowing between endpoints. */
44594493
provisional cluster PowerTopology = 156 {
44604494
revision 1;
@@ -8361,6 +8395,20 @@ endpoint 1 {
83618395
handle command ClearTargets;
83628396
}
83638397

8398+
server cluster EnergyPreference {
8399+
callback attribute energyBalances;
8400+
ram attribute currentEnergyBalance;
8401+
callback attribute energyPriorities;
8402+
callback attribute lowPowerModeSensitivities;
8403+
ram attribute currentLowPowerModeSensitivity;
8404+
callback attribute generatedCommandList;
8405+
callback attribute acceptedCommandList;
8406+
callback attribute eventList;
8407+
callback attribute attributeList;
8408+
ram attribute featureMap default = 3;
8409+
ram attribute clusterRevision default = 1;
8410+
}
8411+
83648412
server cluster PowerTopology {
83658413
callback attribute availableEndpoints;
83668414
callback attribute activeEndpoints;

examples/all-clusters-app/all-clusters-common/all-clusters-app.zap

+186
Original file line numberDiff line numberDiff line change
@@ -14692,6 +14692,192 @@
1469214692
}
1469314693
]
1469414694
},
14695+
{
14696+
"name": "Energy Preference",
14697+
"code": 155,
14698+
"mfgCode": null,
14699+
"define": "ENERGY_PREFERENCE_CLUSTER",
14700+
"side": "server",
14701+
"enabled": 1,
14702+
"attributes": [
14703+
{
14704+
"name": "EnergyBalances",
14705+
"code": 0,
14706+
"mfgCode": null,
14707+
"side": "server",
14708+
"type": "array",
14709+
"included": 1,
14710+
"storageOption": "External",
14711+
"singleton": 0,
14712+
"bounded": 0,
14713+
"defaultValue": null,
14714+
"reportable": 1,
14715+
"minInterval": 1,
14716+
"maxInterval": 65534,
14717+
"reportableChange": 0
14718+
},
14719+
{
14720+
"name": "CurrentEnergyBalance",
14721+
"code": 1,
14722+
"mfgCode": null,
14723+
"side": "server",
14724+
"type": "int8u",
14725+
"included": 1,
14726+
"storageOption": "RAM",
14727+
"singleton": 0,
14728+
"bounded": 0,
14729+
"defaultValue": "",
14730+
"reportable": 1,
14731+
"minInterval": 1,
14732+
"maxInterval": 65534,
14733+
"reportableChange": 0
14734+
},
14735+
{
14736+
"name": "EnergyPriorities",
14737+
"code": 2,
14738+
"mfgCode": null,
14739+
"side": "server",
14740+
"type": "array",
14741+
"included": 1,
14742+
"storageOption": "External",
14743+
"singleton": 0,
14744+
"bounded": 0,
14745+
"defaultValue": null,
14746+
"reportable": 1,
14747+
"minInterval": 1,
14748+
"maxInterval": 65534,
14749+
"reportableChange": 0
14750+
},
14751+
{
14752+
"name": "LowPowerModeSensitivities",
14753+
"code": 3,
14754+
"mfgCode": null,
14755+
"side": "server",
14756+
"type": "array",
14757+
"included": 1,
14758+
"storageOption": "External",
14759+
"singleton": 0,
14760+
"bounded": 0,
14761+
"defaultValue": null,
14762+
"reportable": 1,
14763+
"minInterval": 1,
14764+
"maxInterval": 65534,
14765+
"reportableChange": 0
14766+
},
14767+
{
14768+
"name": "CurrentLowPowerModeSensitivity",
14769+
"code": 4,
14770+
"mfgCode": null,
14771+
"side": "server",
14772+
"type": "int8u",
14773+
"included": 1,
14774+
"storageOption": "RAM",
14775+
"singleton": 0,
14776+
"bounded": 0,
14777+
"defaultValue": "",
14778+
"reportable": 1,
14779+
"minInterval": 1,
14780+
"maxInterval": 65534,
14781+
"reportableChange": 0
14782+
},
14783+
{
14784+
"name": "GeneratedCommandList",
14785+
"code": 65528,
14786+
"mfgCode": null,
14787+
"side": "server",
14788+
"type": "array",
14789+
"included": 1,
14790+
"storageOption": "External",
14791+
"singleton": 0,
14792+
"bounded": 0,
14793+
"defaultValue": null,
14794+
"reportable": 1,
14795+
"minInterval": 1,
14796+
"maxInterval": 65534,
14797+
"reportableChange": 0
14798+
},
14799+
{
14800+
"name": "AcceptedCommandList",
14801+
"code": 65529,
14802+
"mfgCode": null,
14803+
"side": "server",
14804+
"type": "array",
14805+
"included": 1,
14806+
"storageOption": "External",
14807+
"singleton": 0,
14808+
"bounded": 0,
14809+
"defaultValue": null,
14810+
"reportable": 1,
14811+
"minInterval": 1,
14812+
"maxInterval": 65534,
14813+
"reportableChange": 0
14814+
},
14815+
{
14816+
"name": "EventList",
14817+
"code": 65530,
14818+
"mfgCode": null,
14819+
"side": "server",
14820+
"type": "array",
14821+
"included": 1,
14822+
"storageOption": "External",
14823+
"singleton": 0,
14824+
"bounded": 0,
14825+
"defaultValue": null,
14826+
"reportable": 1,
14827+
"minInterval": 1,
14828+
"maxInterval": 65534,
14829+
"reportableChange": 0
14830+
},
14831+
{
14832+
"name": "AttributeList",
14833+
"code": 65531,
14834+
"mfgCode": null,
14835+
"side": "server",
14836+
"type": "array",
14837+
"included": 1,
14838+
"storageOption": "External",
14839+
"singleton": 0,
14840+
"bounded": 0,
14841+
"defaultValue": null,
14842+
"reportable": 1,
14843+
"minInterval": 1,
14844+
"maxInterval": 65534,
14845+
"reportableChange": 0
14846+
},
14847+
{
14848+
"name": "FeatureMap",
14849+
"code": 65532,
14850+
"mfgCode": null,
14851+
"side": "server",
14852+
"type": "bitmap32",
14853+
"included": 1,
14854+
"storageOption": "RAM",
14855+
"singleton": 0,
14856+
"bounded": 0,
14857+
"defaultValue": "3",
14858+
"reportable": 1,
14859+
"minInterval": 1,
14860+
"maxInterval": 65534,
14861+
"reportableChange": 0
14862+
},
14863+
{
14864+
"name": "ClusterRevision",
14865+
"code": 65533,
14866+
"mfgCode": null,
14867+
"side": "server",
14868+
"type": "int16u",
14869+
"included": 1,
14870+
"storageOption": "RAM",
14871+
"singleton": 0,
14872+
"bounded": 0,
14873+
"defaultValue": "1",
14874+
"reportable": 1,
14875+
"minInterval": 0,
14876+
"maxInterval": 65344,
14877+
"reportableChange": 0
14878+
}
14879+
]
14880+
},
1469514881
{
1469614882
"name": "Window Covering",
1469714883
"code": 258,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include <app/clusters/energy-preference-server/energy-preference-server.h>
20+
21+
using namespace chip;
22+
using namespace chip::app::Clusters::EnergyPreference;
23+
using namespace chip::app::Clusters::EnergyPreference::Structs;
24+
25+
static BalanceStruct::Type gsEnergyBalances[] = {
26+
{ .step = 0, .label = Optional<chip::CharSpan>("Efficient"_span) },
27+
{ .step = 50, .label = Optional<chip::CharSpan>() },
28+
{ .step = 100, .label = Optional<chip::CharSpan>("Comfort"_span) },
29+
};
30+
31+
static BalanceStruct::Type gsPowerBalances[] = {
32+
{ .step = 0, .label = Optional<chip::CharSpan>("1 Minute"_span) },
33+
{ .step = 12, .label = Optional<chip::CharSpan>("5 Minutes"_span) },
34+
{ .step = 24, .label = Optional<chip::CharSpan>("10 Minutes"_span) },
35+
{ .step = 36, .label = Optional<chip::CharSpan>("15 Minutes"_span) },
36+
{ .step = 48, .label = Optional<chip::CharSpan>("20 Minutes"_span) },
37+
{ .step = 60, .label = Optional<chip::CharSpan>("25 Minutes"_span) },
38+
{ .step = 70, .label = Optional<chip::CharSpan>("30 Minutes"_span) },
39+
{ .step = 80, .label = Optional<chip::CharSpan>("60 Minutes"_span) },
40+
{ .step = 90, .label = Optional<chip::CharSpan>("120 Minutes"_span) },
41+
{ .step = 100, .label = Optional<chip::CharSpan>("Never"_span) },
42+
};
43+
44+
// assumes it'll be the only delegate for it's lifetime.
45+
struct EPrefDelegate : public Delegate
46+
{
47+
EPrefDelegate();
48+
virtual ~EPrefDelegate();
49+
50+
CHIP_ERROR GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
51+
chip::Optional<chip::MutableCharSpan> & aOutLabel) override;
52+
CHIP_ERROR GetEnergyPriorityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, EnergyPriorityEnum & priority) override;
53+
CHIP_ERROR GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
54+
chip::Optional<chip::MutableCharSpan> & aOutLabel) override;
55+
56+
size_t GetNumEnergyBalances(chip::EndpointId aEndpoint) override;
57+
size_t GetNumLowPowerModeSensitivities(chip::EndpointId aEndpoint) override;
58+
};
59+
60+
EPrefDelegate::EPrefDelegate() : Delegate()
61+
{
62+
VerifyOrDie(GetDelegate() == nullptr);
63+
SetDelegate(this);
64+
}
65+
66+
EPrefDelegate::~EPrefDelegate()
67+
{
68+
VerifyOrDie(GetDelegate() == this);
69+
SetDelegate(nullptr);
70+
}
71+
72+
size_t EPrefDelegate::GetNumEnergyBalances(chip::EndpointId aEndpoint)
73+
{
74+
return (ArraySize(gsEnergyBalances));
75+
}
76+
77+
size_t EPrefDelegate::GetNumLowPowerModeSensitivities(chip::EndpointId aEndpoint)
78+
{
79+
return (ArraySize(gsEnergyBalances));
80+
}
81+
82+
CHIP_ERROR
83+
EPrefDelegate::GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
84+
chip::Optional<chip::MutableCharSpan> & aOutLabel)
85+
{
86+
if (aIndex < GetNumEnergyBalances(aEndpoint))
87+
{
88+
aOutStep = gsEnergyBalances[aIndex].step;
89+
if (gsEnergyBalances[aIndex].label.HasValue())
90+
{
91+
chip::CopyCharSpanToMutableCharSpan(gsEnergyBalances[aIndex].label.Value(), aOutLabel.Value());
92+
}
93+
else
94+
{
95+
aOutLabel.ClearValue();
96+
}
97+
return CHIP_NO_ERROR;
98+
}
99+
return CHIP_ERROR_NOT_FOUND;
100+
}
101+
102+
CHIP_ERROR
103+
EPrefDelegate::GetEnergyPriorityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, EnergyPriorityEnum & priority)
104+
{
105+
static EnergyPriorityEnum priorities[] = { EnergyPriorityEnum::kEfficiency, EnergyPriorityEnum::kComfort };
106+
107+
if (aIndex < ArraySize(priorities))
108+
{
109+
priority = priorities[aIndex];
110+
return CHIP_NO_ERROR;
111+
}
112+
113+
return CHIP_ERROR_NOT_FOUND;
114+
}
115+
116+
CHIP_ERROR
117+
EPrefDelegate::GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
118+
chip::Optional<chip::MutableCharSpan> & aOutLabel)
119+
{
120+
if (aIndex < GetNumLowPowerModeSensitivities(aEndpoint))
121+
{
122+
aOutStep = gsPowerBalances[aIndex].step;
123+
if (gsPowerBalances[aIndex].label.HasValue())
124+
{
125+
chip::CopyCharSpanToMutableCharSpan(gsPowerBalances[aIndex].label.Value(), aOutLabel.Value());
126+
}
127+
else
128+
{
129+
aOutLabel.ClearValue();
130+
}
131+
return CHIP_NO_ERROR;
132+
}
133+
134+
return CHIP_ERROR_NOT_FOUND;
135+
}
136+
137+
static EPrefDelegate gsDelegate;

0 commit comments

Comments
 (0)