Skip to content

Commit 61be1e5

Browse files
committed
Implement proposed API changes with clearer delineation of storage
1 parent b2ab365 commit 61be1e5

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

examples/all-clusters-app/all-clusters-common/src/energy-preference-delegate.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ struct EPrefDelegate : public Delegate
2929
EPrefDelegate();
3030
virtual ~EPrefDelegate();
3131

32-
CHIP_ERROR GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, BalanceStruct::Type & balance) override;
32+
CHIP_ERROR GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep, chip::MutableCharSpan & aOutLabel) override;
3333
CHIP_ERROR GetEnergyPriorityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, EnergyPriorityEnum & priority) override;
34-
CHIP_ERROR GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, BalanceStruct::Type & balance) override;
34+
CHIP_ERROR GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep, chip::MutableCharSpan & aOutLabel) override;
3535

3636
size_t GetNumEnergyBalances(chip::EndpointId aEndpoint) override;
3737
size_t GetNumLowPowerModeSensitivities(chip::EndpointId aEndpoint) override;
@@ -60,11 +60,12 @@ size_t EPrefDelegate::GetNumLowPowerModeSensitivities(chip::EndpointId aEndpoint
6060
}
6161

6262
CHIP_ERROR
63-
EPrefDelegate::GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, BalanceStruct::Type & balance)
63+
EPrefDelegate::GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep, chip::MutableCharSpan & aOutLabel)
6464
{
6565
if (aIndex < GetNumEnergyBalances(aEndpoint))
6666
{
67-
balance = gsEnergyBalances[aIndex];
67+
aOutStep = gsEnergyBalances[aIndex].step;
68+
chip::CopyCharSpanToMutableCharSpan(gsEnergyBalances[aIndex].label.ValueOr(""_span), aOutLabel);
6869
return CHIP_NO_ERROR;
6970
}
7071
return CHIP_ERROR_NOT_FOUND;
@@ -85,11 +86,12 @@ EPrefDelegate::GetEnergyPriorityAtIndex(chip::EndpointId aEndpoint, size_t aInde
8586
}
8687

8788
CHIP_ERROR
88-
EPrefDelegate::GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, BalanceStruct::Type & balance)
89+
EPrefDelegate::GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep, chip::MutableCharSpan & aOutLabel)
8990
{
9091
if (aIndex < GetNumLowPowerModeSensitivities(aEndpoint))
9192
{
92-
balance = gsPowerBalances[aIndex];
93+
aOutStep = gsPowerBalances[aIndex].step;
94+
chip::CopyCharSpanToMutableCharSpan(gsPowerBalances[aIndex].label.ValueOr(""_span), aOutLabel);
9395
return CHIP_NO_ERROR;
9496
}
9597

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ CHIP_ERROR EnergyPrefAttrAccess::Read(const ConcreteReadAttributePath & aPath, A
6969
if (gsDelegate != nullptr)
7070
{
7171
return aEncoder.EncodeList([endpoint](const auto & encoder) -> CHIP_ERROR {
72-
BalanceStruct::Type balance;
72+
chip::Percent step;
73+
char buffer[64];
74+
chip::MutableCharSpan label(buffer);
7375
size_t index = 0;
7476
CHIP_ERROR err = CHIP_NO_ERROR;
75-
while ((err = gsDelegate->GetEnergyBalanceAtIndex(endpoint, index, balance)) == CHIP_NO_ERROR)
77+
while ((err = gsDelegate->GetEnergyBalanceAtIndex(endpoint, index, step, label)) == CHIP_NO_ERROR)
7678
{
79+
BalanceStruct::Type balance = {step, Optional<CharSpan>(label)};
7780
ReturnErrorOnFailure(encoder.Encode(balance));
7881
index++;
7982
}
@@ -119,11 +122,14 @@ CHIP_ERROR EnergyPrefAttrAccess::Read(const ConcreteReadAttributePath & aPath, A
119122
if (gsDelegate != nullptr)
120123
{
121124
return aEncoder.EncodeList([endpoint](const auto & encoder) -> CHIP_ERROR {
122-
BalanceStruct::Type balance;
125+
chip::Percent step;
126+
char buffer[64];
127+
chip::MutableCharSpan label(buffer);
123128
size_t index = 0;
124129
CHIP_ERROR err = CHIP_NO_ERROR;
125-
while ((err = gsDelegate->GetLowPowerModeSensitivityAtIndex(endpoint, index, balance)) == CHIP_NO_ERROR)
130+
while ((err = gsDelegate->GetLowPowerModeSensitivityAtIndex(endpoint, index, step, label)) == CHIP_NO_ERROR)
126131
{
132+
BalanceStruct::Type balance = {step, Optional<CharSpan>(label)};
127133
ReturnErrorOnFailure(encoder.Encode(balance));
128134
index++;
129135
}

src/app/clusters/energy-preference-server/energy-preference-server.h

+27-5
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,25 @@ struct Delegate
3636

3737
/**
3838
* Get an Energy Balance.
39+
8
40+
* The delegate method is called by the cluster to fill out the
41+
* values for the list in EnergyBalances attribute. Storage for
42+
* both aOutStep and aOutLabel is provided by the caller.
43+
*
3944
* @param aEndpoint The endpoint to query.
4045
* @param aIndex The index of the balance, with 0 representing the first one.
41-
* @param aOutBalance The BalanceStruct to copy the data into.
46+
* @param aOutStep The Step value from BalanceStruct
47+
*
48+
* @param aOutLabel The Label value from BalanceStruct. Storage is
49+
* provided by the caller, and is large enough to accomodate the
50+
* longest label (64 chars), on return the size of the span is
51+
* adjusted to reflect the length of the value.
52+
*
4253
* @return CHIP_ERROR_NOT_FOUND if the index is out of range.
4354
*/
4455
virtual CHIP_ERROR
4556
GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex,
46-
chip::app::Clusters::EnergyPreference::Structs::BalanceStruct::Type & aOutBalance) = 0;
57+
chip::Percent & aOutStep, chip::MutableCharSpan & aOutLabel) = 0;
4758

4859
/**
4960
* Get an Energy Priority.
@@ -56,15 +67,26 @@ struct Delegate
5667
chip::app::Clusters::EnergyPreference::EnergyPriorityEnum & aOutPriority) = 0;
5768

5869
/**
59-
* Get a Power Sensitity Balance Struct.
70+
* Get a Power Sensitity Balance Struct data at the specified index.
71+
*
72+
* The delegate method is called by the cluster to fill out the
73+
* values for the list in LowPowerSensitivities attribute. Storage for
74+
* both aOutStep and aOutLabel is provided by the caller.
75+
*
6076
* @param aEndpoint The endpoint to query.
6177
* @param aIndex The index of the priority, with 0 representing the first one.
62-
* @param aOutBalance The BalanceStruct to copy the data into.
78+
* @param aOutStep The Step value from BalanceStruct
79+
*
80+
* @param aOutLabel The Label value from BalanceStruct. Storage is
81+
* provided by the caller, and is large enough to accomodate the
82+
* longest label (64 chars), on return the size of the span is
83+
* adjusted to reflect the length of the value.
84+
*
6385
* @return CHIP_ERROR_NOT_FOUND if the index is out of range.
6486
*/
6587
virtual CHIP_ERROR
6688
GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex,
67-
chip::app::Clusters::EnergyPreference::Structs::BalanceStruct::Type & aOutBalance) = 0;
89+
chip::Percent & aOutStep, chip::MutableCharSpan & aOutLabel) = 0;
6890

6991
/**
7092
* Get the number of energy balances this endpoint has.

0 commit comments

Comments
 (0)