Skip to content

Commit

Permalink
Modifying ReplacementProductListManager to be generic
Browse files Browse the repository at this point in the history
Addressing feedback from PR #28095 and also addressing #28148
  • Loading branch information
cliffamzn committed Jul 21, 2023
1 parent 5862b9e commit 948e7fa
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,18 @@ class HepaFilterMonitoringInstance : public chip::app::Clusters::ResourceMonitor
class StaticReplacementProductListManager : public chip::app::Clusters::ResourceMonitoring::ReplacementProductListManager
{
public:
uint8_t Size() override { return mReplacementProductListSize; };

CHIP_ERROR Next(chip::app::Clusters::ResourceMonitoring::Attributes::ReplacementProductStruct::Type & item) override;
CHIP_ERROR Next(chip::app::Clusters::ResourceMonitoring::Attributes::GenericReplacementProductStruct::GenericType & item) override;

~StaticReplacementProductListManager() {}
StaticReplacementProductListManager(
chip::app::Clusters::ResourceMonitoring::Attributes::ReplacementProductStruct::Type * aReplacementProductsList,
chip::app::Clusters::ResourceMonitoring::Attributes::GenericReplacementProductStruct::GenericType * aReplacementProductsList,
uint8_t aReplacementProductListSize)
{
mReplacementProductsList = aReplacementProductsList;
mReplacementProductListSize = aReplacementProductListSize;
}

private:
chip::app::Clusters::ResourceMonitoring::Attributes::ReplacementProductStruct::Type * mReplacementProductsList;
chip::app::Clusters::ResourceMonitoring::Attributes::GenericReplacementProductStruct::GenericType * mReplacementProductsList;
uint8_t mReplacementProductListSize;
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::ResourceMonitoring;
using namespace ResourceMonitoring::Attributes::GenericReplacementProductStruct;
using chip::Protocols::InteractionModel::Status;

constexpr std::bitset<4> gHepaFilterFeatureMap{ static_cast<uint32_t>(Feature::kCondition) |
Expand All @@ -37,16 +38,12 @@ constexpr std::bitset<4> gActivatedCarbonFeatureMap{ static_cast<uint32_t>(Featu
static HepaFilterMonitoringInstance * gHepaFilterInstance = nullptr;
static ActivatedCarbonFilterMonitoringInstance * gActivatedCarbonFilterInstance = nullptr;

static ResourceMonitoring::Attributes::ReplacementProductStruct::Type sReplacementProductsList[] = {
{ .productIdentifierType = ProductIdentifierTypeEnum::kUpc,
.productIdentifierValue = CharSpan::fromCharString("111112222233") },
{ .productIdentifierType = ProductIdentifierTypeEnum::kGtin8, .productIdentifierValue = CharSpan::fromCharString("gtin8xxx") },
{ .productIdentifierType = ProductIdentifierTypeEnum::kEan,
.productIdentifierValue = CharSpan::fromCharString("4444455555666") },
{ .productIdentifierType = ProductIdentifierTypeEnum::kGtin14,
.productIdentifierValue = CharSpan::fromCharString("gtin14xxxxxxxx") },
{ .productIdentifierType = ProductIdentifierTypeEnum::kOem,
.productIdentifierValue = CharSpan::fromCharString("oem20xxxxxxxxxxxxxxx") },
static GenericType sReplacementProductsList[] = {
GenericType(ProductIdentifierTypeEnum::kUpc, CharSpan::fromCharString("111112222233")),
GenericType(ProductIdentifierTypeEnum::kGtin8, CharSpan::fromCharString("gtin8xxx")),
GenericType(ProductIdentifierTypeEnum::kEan, CharSpan::fromCharString("4444455555666")),
GenericType(ProductIdentifierTypeEnum::kGtin14, CharSpan::fromCharString("gtin14xxxxxxxx")),
GenericType(ProductIdentifierTypeEnum::kOem, CharSpan::fromCharString("oem20xxxxxxxxxxxxxxx")),
};
StaticReplacementProductListManager sReplacementProductListManager(&sReplacementProductsList[0],
ArraySize(sReplacementProductsList));
Expand Down Expand Up @@ -105,7 +102,7 @@ void emberAfHepaFilterMonitoringClusterInitCallback(chip::EndpointId endpoint)
gHepaFilterInstance->Init();
}

CHIP_ERROR StaticReplacementProductListManager::Next(Attributes::ReplacementProductStruct::Type & item)
CHIP_ERROR StaticReplacementProductListManager::Next(Attributes::GenericReplacementProductStruct::GenericType & item)
{
if (mIndex < mReplacementProductListSize)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,18 @@ namespace ResourceMonitoring {
class StaticReplacementProductListManager : public ReplacementProductListManager
{
public:
uint8_t Size() override { return mReplacementProductListSize; };

CHIP_ERROR Next(Attributes::ReplacementProductStruct::Type & item) override;
CHIP_ERROR Next(Attributes::GenericReplacementProductStruct::GenericType & item) override;

~StaticReplacementProductListManager() {}
StaticReplacementProductListManager(Attributes::ReplacementProductStruct::Type * aReplacementProductsList,
StaticReplacementProductListManager(Attributes::GenericReplacementProductStruct::GenericType * aReplacementProductsList,
uint8_t aReplacementProductListSize)
{
mReplacementProductsList = aReplacementProductsList;
mReplacementProductListSize = aReplacementProductListSize;
}

private:
Attributes::ReplacementProductStruct::Type * mReplacementProductsList;
Attributes::GenericReplacementProductStruct::GenericType * mReplacementProductsList;
uint8_t mReplacementProductListSize;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::ResourceMonitoring;

CHIP_ERROR StaticReplacementProductListManager::Next(Attributes::ReplacementProductStruct::Type & item)
CHIP_ERROR StaticReplacementProductListManager::Next(Attributes::GenericReplacementProductStruct::GenericType & item)
{
if (mIndex < mReplacementProductListSize)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,15 @@ using namespace chip;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::ActivatedCarbonFilterMonitoring;
using namespace chip::app::Clusters::ResourceMonitoring;
using namespace ResourceMonitoring::Attributes::GenericReplacementProductStruct;
using chip::Protocols::InteractionModel::Status;

static ResourceMonitoring::Attributes::ReplacementProductStruct::Type sActivatedCarbonFilterReplacementProductsList[] = {
{ .productIdentifierType = ResourceMonitoring::ProductIdentifierTypeEnum::kUpc,
.productIdentifierValue = CharSpan::fromCharString("111112222233") },
{ .productIdentifierType = ResourceMonitoring::ProductIdentifierTypeEnum::kGtin8,
.productIdentifierValue = CharSpan::fromCharString("gtin8xca") },
{ .productIdentifierType = ResourceMonitoring::ProductIdentifierTypeEnum::kEan,
.productIdentifierValue = CharSpan::fromCharString("4444455555666") },
{ .productIdentifierType = ResourceMonitoring::ProductIdentifierTypeEnum::kGtin14,
.productIdentifierValue = CharSpan::fromCharString("gtin14xcarbonx") },
{ .productIdentifierType = ResourceMonitoring::ProductIdentifierTypeEnum::kOem,
.productIdentifierValue = CharSpan::fromCharString("oem20xcarbonxxxxxxxx") },
static ResourceMonitoring::Attributes::GenericReplacementProductStruct::GenericType sActivatedCarbonFilterReplacementProductsList[] = {
GenericType(ResourceMonitoring::ProductIdentifierTypeEnum::kUpc, CharSpan::fromCharString("111112222233")),
GenericType(ResourceMonitoring::ProductIdentifierTypeEnum::kGtin8, CharSpan::fromCharString("gtin8xca")),
GenericType(ResourceMonitoring::ProductIdentifierTypeEnum::kEan, CharSpan::fromCharString("4444455555666")),
GenericType(ResourceMonitoring::ProductIdentifierTypeEnum::kGtin14, CharSpan::fromCharString("gtin14xcarbonx")),
GenericType(ResourceMonitoring::ProductIdentifierTypeEnum::kOem, CharSpan::fromCharString("oem20xcarbonxxxxxxxx")),
};
StaticReplacementProductListManager
sActivatedCarbonFilterReplacementProductListManager(&sActivatedCarbonFilterReplacementProductsList[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,15 @@
using namespace chip;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::ResourceMonitoring;
using namespace ResourceMonitoring::Attributes::GenericReplacementProductStruct;
using chip::Protocols::InteractionModel::Status;

static ResourceMonitoring::Attributes::ReplacementProductStruct::Type sHepaFilterReplacementProductsList[] = {
{ .productIdentifierType = ResourceMonitoring::ProductIdentifierTypeEnum::kUpc,
.productIdentifierValue = CharSpan::fromCharString("111112222233") },
{ .productIdentifierType = ResourceMonitoring::ProductIdentifierTypeEnum::kGtin8,
.productIdentifierValue = CharSpan::fromCharString("gtin8xhe") },
{ .productIdentifierType = ResourceMonitoring::ProductIdentifierTypeEnum::kEan,
.productIdentifierValue = CharSpan::fromCharString("4444455555666") },
{ .productIdentifierType = ResourceMonitoring::ProductIdentifierTypeEnum::kGtin14,
.productIdentifierValue = CharSpan::fromCharString("gtin14xhepaxxx") },
{ .productIdentifierType = ResourceMonitoring::ProductIdentifierTypeEnum::kOem,
.productIdentifierValue = CharSpan::fromCharString("oem20xhepaxxxxxxxxxx") },
static GenericType sHepaFilterReplacementProductsList[] = {
GenericType(ResourceMonitoring::ProductIdentifierTypeEnum::kUpc, CharSpan::fromCharString("111112222233")),
GenericType(ResourceMonitoring::ProductIdentifierTypeEnum::kGtin8, CharSpan::fromCharString("gtin8xhe")),
GenericType(ResourceMonitoring::ProductIdentifierTypeEnum::kEan, CharSpan::fromCharString("4444455555666")),
GenericType(ResourceMonitoring::ProductIdentifierTypeEnum::kGtin14, CharSpan::fromCharString("gtin14xhepaxxx")),
GenericType(ResourceMonitoring::ProductIdentifierTypeEnum::kOem, CharSpan::fromCharString("oem20xhepaxxxxxxxxxx")),
};
StaticReplacementProductListManager sHepaFilterReplacementProductListManager(&sHepaFilterReplacementProductsList[0],
ArraySize(sHepaFilterReplacementProductsList));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ class ReplacementProductListManager

void Reset() { mIndex = 0; }

// Returns total size of Replacement Products List.
virtual uint8_t Size() = 0;

virtual CHIP_ERROR Next(Attributes::ReplacementProductStruct::Type & item) = 0;
virtual CHIP_ERROR Next(Attributes::GenericReplacementProductStruct::GenericType & item) = 0;

protected:
uint8_t mIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ CHIP_ERROR DecodableType::Decode(TLV::TLVReader & reader)
} // namespace Commands

namespace Attributes {
namespace ReplacementProductStruct {
namespace GenericReplacementProductStruct {

CHIP_ERROR Type::Encode(TLV::TLVWriter & writer, TLV::Tag tag) const
CHIP_ERROR GenericType::Encode(TLV::TLVWriter & writer, TLV::Tag tag) const
{
TLV::TLVType outer;
ReturnErrorOnFailure(writer.StartContainer(tag, TLV::kTLVType_Structure, outer));
Expand All @@ -57,7 +57,7 @@ CHIP_ERROR Type::Encode(TLV::TLVWriter & writer, TLV::Tag tag) const
return CHIP_NO_ERROR;
}

} // namespace ReplacementProductStruct
} // namespace GenericReplacementProductStruct
} // namespace Attributes

} // namespace ResourceMonitoring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,37 +141,39 @@ struct TypeInfo
};
} // namespace LastChangedTime

namespace ReplacementProductStruct {
namespace GenericReplacementProductStruct {
enum class Fields : uint8_t
{
kProductIdentifierType = 0,
kProductIdentifierValue = 1,
};

struct Type
class GenericType : protected app::Clusters::HepaFilterMonitoring::Structs::ReplacementProductStruct::Type
{
public:
ProductIdentifierTypeEnum productIdentifierType = static_cast<ProductIdentifierTypeEnum>(0);
chip::CharSpan productIdentifierValue;

CHIP_ERROR Decode(TLV::TLVReader & reader);
private:
ResourceMonitoring::ProductIdentifierTypeEnum productIdentifierType;
chip::CharSpan productIdentifierValue;

static constexpr bool kIsFabricScoped = false;

CHIP_ERROR Encode(TLV::TLVWriter & writer, TLV::Tag tag) const;
public:
GenericType() {}
GenericType(ResourceMonitoring::ProductIdentifierTypeEnum aProductIdentifierType, chip::CharSpan aProductIdentifierValue)
{
productIdentifierType = aProductIdentifierType;
productIdentifierValue = aProductIdentifierValue;
}
virtual ~GenericType() = default;
static constexpr bool kIsFabricScoped = false;
CHIP_ERROR Encode(TLV::TLVWriter & writer, TLV::Tag tag) const;
};

using DecodableType = Type;

} // namespace ReplacementProductStruct
} // namespace GenericReplacementProductStruct

namespace ReplacementProductList {
static constexpr AttributeId Id = 0x00000005;
struct TypeInfo
{
using Type = chip::app::DataModel::List<const ReplacementProductStruct::Type>;
using DecodableType = chip::app::DataModel::DecodableList<ReplacementProductStruct::Type>;
using DecodableArgType = const chip::app::DataModel::DecodableList<ReplacementProductStruct::Type> &;
using Type = chip::app::DataModel::List<const GenericReplacementProductStruct::GenericType>;
using DecodableType = chip::app::DataModel::DecodableList<GenericReplacementProductStruct::GenericType>;
using DecodableArgType = const chip::app::DataModel::DecodableList<GenericReplacementProductStruct::GenericType> &;

static constexpr AttributeId GetAttributeId() { return Attributes::ReplacementProductList::Id; }
static constexpr bool MustUseTimedWrite() { return false; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ Status Instance::OnResetCondition()
}
}

ReplacementProductListManager * productListManagerInstance = Instance::GetReplacementProductListManagerInstance();
if (nullptr != productListManagerInstance)
{
productListManagerInstance->Reset();
}

// call application specific post reset logic
status = PostResetCondition();
return status;
Expand Down Expand Up @@ -245,7 +251,7 @@ CHIP_ERROR Instance::EnumerateAcceptedCommands(const ConcreteClusterPath & clust
CHIP_ERROR Instance::ReadReplacableProductList(AttributeValueEncoder & aEncoder)
{
CHIP_ERROR err;
if (Instance::HasFeature(ResourceMonitoring::Feature::kReplacementProductList))
if (HasFeature(ResourceMonitoring::Feature::kReplacementProductList))
{
ReplacementProductListManager * productListManagerInstance = Instance::GetReplacementProductListManagerInstance();
if (nullptr == productListManagerInstance)
Expand All @@ -257,12 +263,14 @@ CHIP_ERROR Instance::ReadReplacableProductList(AttributeValueEncoder & aEncoder)
productListManagerInstance->Reset();

err = aEncoder.EncodeList([&](const auto & encoder) -> CHIP_ERROR {
Attributes::ReplacementProductStruct::Type replacementProductStruct;
while (productListManagerInstance->Next(replacementProductStruct) == CHIP_NO_ERROR)
Attributes::GenericReplacementProductStruct::GenericType replacementProductStruct;
CHIP_ERROR iteratorError;
while (CHIP_ERROR_PROVIDER_LIST_EXHAUSTED != iteratorError)
{
ReturnErrorOnFailure(encoder.Encode(replacementProductStruct));
iteratorError = productListManagerInstance->Next(replacementProductStruct);
}
return CHIP_NO_ERROR;
return (CHIP_ERROR_PROVIDER_LIST_EXHAUSTED != iteratorError) ? iteratorError : CHIP_NO_ERROR;
});
}
return CHIP_NO_ERROR;
Expand Down

0 comments on commit 948e7fa

Please sign in to comment.