Skip to content

Commit

Permalink
Implement proposed API changes with clearer delineation of storage
Browse files Browse the repository at this point in the history
  • Loading branch information
robszewczyk committed Feb 15, 2024
1 parent 9c2a8cc commit 29f2a79
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ struct EPrefDelegate : public Delegate
EPrefDelegate();
virtual ~EPrefDelegate();

CHIP_ERROR GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, BalanceStruct::Type & balance) override;
CHIP_ERROR GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep, 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, BalanceStruct::Type & balance) override;
CHIP_ERROR GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep, chip::MutableCharSpan & aOutLabel) override;

size_t GetNumEnergyBalances(chip::EndpointId aEndpoint) override;
size_t GetNumLowPowerModeSensitivities(chip::EndpointId aEndpoint) override;
Expand Down Expand Up @@ -60,11 +60,12 @@ size_t EPrefDelegate::GetNumLowPowerModeSensitivities(chip::EndpointId aEndpoint
}

CHIP_ERROR
EPrefDelegate::GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, BalanceStruct::Type & balance)
EPrefDelegate::GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep, chip::MutableCharSpan & aOutLabel)
{
if (aIndex < GetNumEnergyBalances(aEndpoint))
{
balance = gsEnergyBalances[aIndex];
aOutStep = gsEnergyBalances[aIndex].step;
chip::CopyCharSpanToMutableCharSpan(gsEnergyBalances[aIndex].label.ValueOr(""_span), aOutLabel);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NOT_FOUND;
Expand All @@ -85,11 +86,12 @@ EPrefDelegate::GetEnergyPriorityAtIndex(chip::EndpointId aEndpoint, size_t aInde
}

CHIP_ERROR
EPrefDelegate::GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, BalanceStruct::Type & balance)
EPrefDelegate::GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep, chip::MutableCharSpan & aOutLabel)
{
if (aIndex < GetNumLowPowerModeSensitivities(aEndpoint))
{
balance = gsPowerBalances[aIndex];
aOutStep = gsPowerBalances[aIndex].step;
chip::CopyCharSpanToMutableCharSpan(gsPowerBalances[aIndex].label.ValueOr(""_span), aOutLabel);
return CHIP_NO_ERROR;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ CHIP_ERROR EnergyPrefAttrAccess::Read(const ConcreteReadAttributePath & aPath, A
if (gsDelegate != nullptr)
{
return aEncoder.EncodeList([endpoint](const auto & encoder) -> CHIP_ERROR {
BalanceStruct::Type balance;
chip::Percent step;
char buffer[64];
chip::MutableCharSpan label(buffer);
size_t index = 0;
CHIP_ERROR err = CHIP_NO_ERROR;
while ((err = gsDelegate->GetEnergyBalanceAtIndex(endpoint, index, balance)) == CHIP_NO_ERROR)
while ((err = gsDelegate->GetEnergyBalanceAtIndex(endpoint, index, step, label)) == CHIP_NO_ERROR)
{
BalanceStruct::Type balance = {step, Optional<CharSpan>(label)};
ReturnErrorOnFailure(encoder.Encode(balance));
index++;
}
Expand Down Expand Up @@ -119,11 +122,14 @@ CHIP_ERROR EnergyPrefAttrAccess::Read(const ConcreteReadAttributePath & aPath, A
if (gsDelegate != nullptr)
{
return aEncoder.EncodeList([endpoint](const auto & encoder) -> CHIP_ERROR {
BalanceStruct::Type balance;
chip::Percent step;
char buffer[64];
chip::MutableCharSpan label(buffer);
size_t index = 0;
CHIP_ERROR err = CHIP_NO_ERROR;
while ((err = gsDelegate->GetLowPowerModeSensitivityAtIndex(endpoint, index, balance)) == CHIP_NO_ERROR)
while ((err = gsDelegate->GetLowPowerModeSensitivityAtIndex(endpoint, index, step,label)) == CHIP_NO_ERROR)
{
BalanceStruct::Type balance = {step, Optional<CharSpan>(label)};
ReturnErrorOnFailure(encoder.Encode(balance));
index++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,25 @@ struct Delegate

/**
* Get an Energy Balance.
8
* The delegate method is called by the cluster to fill out the
* values for the list in EnergyBalances attribute. Storage for
* both aOutStep and aOutLabel is provided by the caller.
*
* @param aEndpoint The endpoint to query.
* @param aIndex The index of the balance, with 0 representing the first one.
* @param aOutBalance The BalanceStruct to copy the data into.
* @param aOutStep The Step value from BalanceStruct
*
* @param aOutLabel The Label value from BalanceStruct. Storage is
* provided by the caller, and is large enough to accomodate the
* longest label (64 chars), on return the size of the span is
* adjusted to reflect the length of the value.
*
* @return CHIP_ERROR_NOT_FOUND if the index is out of range.
*/
virtual CHIP_ERROR
GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex,
chip::app::Clusters::EnergyPreference::Structs::BalanceStruct::Type & aOutBalance) = 0;
chip::Percent & aOutStep, chip::MutableCharSpan & aOutLabel) = 0;

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

/**
* Get a Power Sensitity Balance Struct.
* Get a Power Sensitity Balance Struct data at the specified index.
*
* The delegate method is called by the cluster to fill out the
* values for the list in LowPowerSensitivities attribute. Storage for
* both aOutStep and aOutLabel is provided by the caller.
*
* @param aEndpoint The endpoint to query.
* @param aIndex The index of the priority, with 0 representing the first one.
* @param aOutBalance The BalanceStruct to copy the data into.
* @param aOutStep The Step value from BalanceStruct
*
* @param aOutLabel The Label value from BalanceStruct. Storage is
* provided by the caller, and is large enough to accomodate the
* longest label (64 chars), on return the size of the span is
* adjusted to reflect the length of the value.
*
* @return CHIP_ERROR_NOT_FOUND if the index is out of range.
*/
virtual CHIP_ERROR
GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex,
chip::app::Clusters::EnergyPreference::Structs::BalanceStruct::Type & aOutBalance) = 0;
chip::Percent & aOutStep, chip::MutableCharSpan & aOutLabel) = 0;

/**
* Get the number of energy balances this endpoint has.
Expand Down

0 comments on commit 29f2a79

Please sign in to comment.