Skip to content

Commit b183cde

Browse files
authored
Helper changes for PowerSource codedriven implementation (project-chip#72119) (project-chip#72250)
* Pull helper changes from project-chip#72119 * Compile fix
1 parent dd767e6 commit b183cde

8 files changed

Lines changed: 118 additions & 129 deletions

File tree

examples/evse-app/evse-common/src/EVSEManufacturerImpl.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ CHIP_ERROR FindNextTarget(const BitMask<EnergyEvse::TargetDayOfWeekBitmap> dayOf
130130
uint16_t & targetTimeMinutesPastMidnight_m, DataModel::Nullable<Percent> & targetSoC,
131131
DataModel::Nullable<int64_t> & targetAddedEnergy_mWh, bool bAllowTargetsInPast)
132132
{
133-
EnergyEvse::Structs::ChargingTargetScheduleStruct::Type entry;
134-
135133
uint16_t minTimeToTarget_m = 24 * 60; // 24 hours
136134
bool bFound = false;
137135

src/app/server-cluster/OptionalAttributeSet.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ struct IsOneOf<T>
5757
class AttributeSet
5858
{
5959
public:
60-
explicit AttributeSet(uint32_t initialValue) : mSetBits(initialValue) {}
60+
constexpr explicit AttributeSet(uint32_t initialValue) : mSetBits(initialValue) {}
6161

62-
AttributeSet() = default;
63-
AttributeSet(const AttributeSet & other) = default;
64-
AttributeSet(AttributeSet && other) = default;
65-
AttributeSet & operator=(const AttributeSet & other) = default;
66-
AttributeSet & operator=(AttributeSet && other) = default;
62+
constexpr AttributeSet() = default;
63+
constexpr AttributeSet(const AttributeSet & other) = default;
64+
constexpr AttributeSet(AttributeSet && other) = default;
65+
constexpr AttributeSet & operator=(const AttributeSet & other) = default;
66+
constexpr AttributeSet & operator=(AttributeSet && other) = default;
6767

6868
// Checks if an attribute ID is set.
6969
//
@@ -85,6 +85,8 @@ class AttributeSet
8585
return Set(id, true);
8686
}
8787

88+
constexpr uint32_t Raw() const { return mSetBits; }
89+
8890
protected:
8991
constexpr AttributeSet & Set(AttributeId id, bool value = true)
9092
{
@@ -140,9 +142,9 @@ template <AttributeId... OptionalAttributeIds>
140142
class OptionalAttributeSet : public AttributeSet
141143
{
142144
public:
143-
explicit OptionalAttributeSet(uint32_t initialValue) : AttributeSet(initialValue & All()) {}
144-
OptionalAttributeSet(const AttributeSet & initialValue) : AttributeSet(initialValue) {}
145-
OptionalAttributeSet() = default;
145+
constexpr explicit OptionalAttributeSet(uint32_t initialValue) : AttributeSet(initialValue & All()) {}
146+
constexpr OptionalAttributeSet(const AttributeSet & initialValue) : AttributeSet(initialValue) {}
147+
constexpr OptionalAttributeSet() = default;
146148

147149
template <uint32_t ATTRIBUTE_ID>
148150
constexpr OptionalAttributeSet & Set(bool value = true)

src/app/server-cluster/testing/BUILD.gn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ source_set("testing") {
2929
"TestAttributeChangeListener.h",
3030
"TestEventGenerator.h",
3131
"TestServerClusterContext.h",
32-
"ValidateGlobalAttributes.cpp",
3332
"ValidateGlobalAttributes.h",
3433
]
3534

src/app/server-cluster/testing/ValidateGlobalAttributes.cpp

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/app/server-cluster/testing/ValidateGlobalAttributes.h

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ namespace Testing {
3434
///
3535
/// Parameters:
3636
/// cluster - The cluster interface to test.
37-
/// expected - An initializer list of expected attribute entries (may be empty for only globals)
37+
/// expected - initializer_list or any other iterable of expected attribute entries supporting `std::size(container)` syntax.
38+
/// May be empty for global attributes only.
3839
///
3940
/// @note This function will assert (die) if `cluster.GetPaths()` does not return exactly one path.
4041
///
@@ -43,16 +44,39 @@ namespace Testing {
4344
/// ClusterImpl cluster(kTestEndpointId, ....);
4445
/// ASSERT_TRUE(IsAttributesListEqualTo(cluster, { Attributes::SomeAttribute::kMetadataEntry }));
4546
/// ```
46-
bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster,
47-
std::initializer_list<const app::DataModel::AttributeEntry> expected);
4847

49-
/// Overload of IsAttributesListEqualTo that accepts a standard vector of attribute entries.
50-
///
51-
/// Parameters:
52-
/// cluster - The cluster interface to test.
53-
/// expected - A vector containing the expected attribute entries (must include all
54-
/// non-global, non-optional attributes).
55-
bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster, const std::vector<app::DataModel::AttributeEntry> & expected);
48+
template <class T>
49+
bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster, const T & expected)
50+
{
51+
VerifyOrDie(cluster.GetPaths().size() == 1);
52+
auto path = cluster.GetPaths()[0];
53+
ReadOnlyBufferBuilder<app::DataModel::AttributeEntry> attributesBuilder;
54+
if (CHIP_ERROR err = cluster.Attributes(path, attributesBuilder); err != CHIP_NO_ERROR)
55+
{
56+
ChipLogError(Test, "Failed to get attributes list from cluster. Error: %" CHIP_ERROR_FORMAT, err.Format());
57+
return false;
58+
}
59+
60+
ReadOnlyBufferBuilder<app::DataModel::AttributeEntry> expectedBuilder;
61+
62+
SuccessOrDie(expectedBuilder.EnsureAppendCapacity(std::size(expected)));
63+
for (const auto & entry : expected)
64+
{
65+
SuccessOrDie(expectedBuilder.Append(entry));
66+
}
67+
68+
SuccessOrDie(expectedBuilder.AppendElements(app::DefaultServerCluster::GlobalAttributes()));
69+
70+
return EqualAttributeSets(attributesBuilder.TakeBuffer(), expectedBuilder.TakeBuffer());
71+
}
72+
73+
// Overload for std::initializer_list to not get "template argument deduction failed" when calling `IsAttributesListEqualTo(cluster,
74+
// {...})`
75+
template <typename T = const app::DataModel::AttributeEntry>
76+
bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster, std::initializer_list<T> expected)
77+
{
78+
return IsAttributesListEqualTo<std::initializer_list<T>>(cluster, expected);
79+
}
5680

5781
/// Compares the accepted commands of a cluster against an expected set.
5882
///
@@ -61,7 +85,8 @@ bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster, const std::v
6185
///
6286
/// Parameters:
6387
/// cluster - The cluster interface to test.
64-
/// expected - An initializer list of expected accepted command entries. May be empty.
88+
/// expected - initializer_list or any other iterable of expected accepted command entries supporting `std::size(container)`
89+
/// syntax. May be empty.
6590
///
6691
/// @note This function will assert (die) if `cluster.GetPaths()` does not return exactly one path.
6792
///
@@ -71,8 +96,36 @@ bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster, const std::v
7196
/// ClusterImpl cluster(kTestEndpointId, ...);
7297
/// ASSERT_TRUE(IsAcceptedCommandsListEqualTo(cluster, { Commands::SomeCommand::kMetadataEntry }));
7398
/// ```
74-
bool IsAcceptedCommandsListEqualTo(app::ServerClusterInterface & cluster,
75-
std::initializer_list<const app::DataModel::AcceptedCommandEntry> expected);
99+
template <class T>
100+
bool IsAcceptedCommandsListEqualTo(app::ServerClusterInterface & cluster, const T & expected)
101+
{
102+
VerifyOrDie(cluster.GetPaths().size() == 1);
103+
auto path = cluster.GetPaths()[0];
104+
ReadOnlyBufferBuilder<app::DataModel::AcceptedCommandEntry> commandsBuilder;
105+
if (CHIP_ERROR err = cluster.AcceptedCommands(path, commandsBuilder); err != CHIP_NO_ERROR)
106+
{
107+
ChipLogError(Test, "Failed to get accepted commands list from cluster. Error: %" CHIP_ERROR_FORMAT, err.Format());
108+
return false;
109+
}
110+
111+
ReadOnlyBufferBuilder<app::DataModel::AcceptedCommandEntry> expectedBuilder;
112+
113+
SuccessOrDie(expectedBuilder.EnsureAppendCapacity(std::size(expected)));
114+
for (const auto & entry : expected)
115+
{
116+
SuccessOrDie(expectedBuilder.Append(entry));
117+
}
118+
119+
return EqualAcceptedCommandSets(commandsBuilder.TakeBuffer(), expectedBuilder.TakeBuffer());
120+
}
121+
122+
// Overload for std::initializer_list to not get "template argument deduction failed" when calling
123+
// `IsAcceptedCommandsListEqualTo(cluster, {...})`
124+
template <typename T = const app::DataModel::AcceptedCommandEntry>
125+
bool IsAcceptedCommandsListEqualTo(app::ServerClusterInterface & cluster, std::initializer_list<T> expected)
126+
{
127+
return IsAcceptedCommandsListEqualTo<std::initializer_list<T>>(cluster, expected);
128+
}
76129

77130
/// Compares the generated commands of a cluster against an expected set.
78131
///
@@ -81,7 +134,8 @@ bool IsAcceptedCommandsListEqualTo(app::ServerClusterInterface & cluster,
81134
///
82135
/// Parameters:
83136
/// cluster - The cluster interface to test.
84-
/// expected - An initializer list of expected generated command entries. May be empty.
137+
/// expected - initializer_list or any other iterable of expected generated command entries supporting `std::size(container)`
138+
/// syntax. May be empty.
85139
///
86140
/// @note This function will assert (die) if `cluster.GetPaths()` does not return exactly one path.
87141
///
@@ -90,7 +144,36 @@ bool IsAcceptedCommandsListEqualTo(app::ServerClusterInterface & cluster,
90144
/// ClusterImpl cluster(kTestEndpointId, ...);
91145
/// ASSERT_TRUE(IsGeneratedCommandsListEqualTo(cluster, { Commands::SomeCommandResponse::kMetadataEntry }));
92146
/// ```
93-
bool IsGeneratedCommandsListEqualTo(app::ServerClusterInterface & cluster, std::initializer_list<const CommandId> expected);
147+
template <class T>
148+
bool IsGeneratedCommandsListEqualTo(app::ServerClusterInterface & cluster, const T & expected)
149+
{
150+
VerifyOrDie(cluster.GetPaths().size() == 1);
151+
auto path = cluster.GetPaths()[0];
152+
ReadOnlyBufferBuilder<CommandId> commandsBuilder;
153+
if (CHIP_ERROR err = cluster.GeneratedCommands(path, commandsBuilder); err != CHIP_NO_ERROR)
154+
{
155+
ChipLogError(Test, "Failed to get generated commands list from cluster. Error: %" CHIP_ERROR_FORMAT, err.Format());
156+
return false;
157+
}
158+
159+
ReadOnlyBufferBuilder<CommandId> expectedBuilder;
160+
161+
SuccessOrDie(expectedBuilder.EnsureAppendCapacity(std::size(expected)));
162+
for (const auto & entry : expected)
163+
{
164+
SuccessOrDie(expectedBuilder.Append(entry));
165+
}
166+
167+
return EqualGeneratedCommandSets(commandsBuilder.TakeBuffer(), expectedBuilder.TakeBuffer());
168+
}
169+
170+
// Overload for std::initializer_list to not get "template argument deduction failed" when calling
171+
// `IsGeneratedCommandsListEqualTo(cluster, {...})`
172+
template <typename T = const CommandId>
173+
bool IsGeneratedCommandsListEqualTo(app::ServerClusterInterface & cluster, std::initializer_list<T> expected)
174+
{
175+
return IsGeneratedCommandsListEqualTo<std::initializer_list<T>>(cluster, expected);
176+
}
94177

95178
} // namespace Testing
96179
} // namespace chip

src/lib/core/Optional.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ inline constexpr NullOptionalType NullOptional{};
4343
/**
4444
* Pairs an object with a boolean value to determine if the object value
4545
* is actually valid or not.
46+
*
47+
* @note Despite the `constexpr` keywords throughout the class, this class is not a literal type and thus cannot be used in
48+
* compile-time contexts. This is mainly because of the use of placement new.
4649
*/
4750
template <class T>
4851
class Optional

src/lib/support/BitFlags.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class BitFlags
7070
*
7171
* @param other Flag(s) to set. Any flags not set in @a other are unaffected.
7272
*/
73-
BitFlags & Set(const BitFlags & other)
73+
constexpr BitFlags & Set(const BitFlags & other)
7474
{
7575
mValue |= other.mValue;
7676
return *this;
@@ -144,7 +144,7 @@ class BitFlags
144144
* @note Flags passed need not be set; this test only requires that no *other* flags be set.
145145
*/
146146
template <typename... Args>
147-
bool HasOnly(Args &&... args) const
147+
constexpr bool HasOnly(Args &&... args) const
148148
{
149149
return (mValue & Or(std::forward<Args>(args)...)) == mValue;
150150
}
@@ -157,7 +157,7 @@ class BitFlags
157157
* False if any given flag is not set.
158158
*/
159159
template <typename... Args>
160-
bool HasAll(Args &&... args) const
160+
constexpr bool HasAll(Args &&... args) const
161161
{
162162
const IntegerType all = Or(std::forward<Args>(args)...);
163163
return (mValue & all) == all;
@@ -171,7 +171,7 @@ class BitFlags
171171
* False if all given flags are not set.
172172
*/
173173
template <typename... Args>
174-
bool HasAny(Args &&... args) const
174+
constexpr bool HasAny(Args &&... args) const
175175
{
176176
return (mValue & Or(std::forward<Args>(args)...)) != 0;
177177
}

src/lib/support/BitMask.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class BitMask : public BitFlags<FlagsEnum, StorageType>
3838
constexpr BitMask(BitFlags<FlagsEnum, StorageType> && other) : BitFlags<FlagsEnum, StorageType>(std::move(other)) {}
3939
BitMask(const BitMask &) = default;
4040

41-
explicit BitMask(FlagsEnum value) : BitFlags<FlagsEnum, StorageType>(value) {}
42-
explicit BitMask(IntegerType value) : BitFlags<FlagsEnum, StorageType>(value) {}
41+
constexpr explicit BitMask(FlagsEnum value) : BitFlags<FlagsEnum, StorageType>(value) {}
42+
constexpr explicit BitMask(IntegerType value) : BitFlags<FlagsEnum, StorageType>(value) {}
4343

4444
template <typename... Args>
4545
constexpr BitMask(FlagsEnum flag, Args &&... args) : BitFlags<FlagsEnum, StorageType>(flag, std::forward<Args>(args)...)

0 commit comments

Comments
 (0)