Skip to content

Commit 9e0f288

Browse files
authored
More fixes to src/app/util/mock. (project-chip#32932)
* More fixes to src/app/util/mock. * Remove misplaced comment * Remove misplaced comment * Fix compilation for esp32 * Fix typo
1 parent f52eabf commit 9e0f288

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

src/app/util/mock/Functions.h

+8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ namespace Test {
3636
CHIP_ERROR ReadSingleMockClusterData(FabricIndex aAccessingFabricIndex, const app::ConcreteAttributePath & aPath,
3737
app::AttributeReportIBs::Builder & aAttributeReports,
3838
app::AttributeValueEncoder::AttributeEncodeState * apEncoderState);
39+
40+
/// Increase the current value for `GetVersion`
3941
void BumpVersion();
42+
43+
/// Sets GetVersion to return 0
44+
void ResetVersion();
45+
46+
/// Gets the current value for the version that will
47+
/// be returned by emberAfDataVersionStorage
4048
DataVersion GetVersion();
4149

4250
/// Configures the singular global mock attribute storage to use the specified configuration.

src/app/util/mock/MockNodeConfig.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ MockClusterConfig::MockClusterConfig(ClusterId aId, std::initializer_list<MockAt
7070
mEmberCluster.mask = CLUSTER_MASK_SERVER;
7171
mEmberCluster.eventCount = static_cast<uint16_t>(mEmberEventList.size());
7272
mEmberCluster.eventList = mEmberEventList.data();
73+
74+
for (auto & attr : attributes)
75+
{
76+
mAttributeMetaData.push_back(attr.attributeMetaData);
77+
}
78+
79+
// Make sure ember side has access to attribute metadata
80+
mEmberCluster.attributes = mAttributeMetaData.data();
81+
}
82+
83+
MockClusterConfig::MockClusterConfig(const MockClusterConfig & other) :
84+
id(other.id), attributes(other.attributes), events(other.events), mEmberCluster(other.mEmberCluster),
85+
mEmberEventList(other.mEmberEventList), mAttributeMetaData(other.mAttributeMetaData)
86+
{
87+
// Fix self-referencial dependencies after data copy
88+
mEmberCluster.attributes = mAttributeMetaData.data();
7389
}
7490

7591
const MockAttributeConfig * MockClusterConfig::attributeById(AttributeId attributeId, ptrdiff_t * outIndex) const

src/app/util/mock/MockNodeConfig.h

+33-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#pragma once
2020

21+
#include <app-common/zap-generated/attribute-type.h>
2122
#include <app/util/af-types.h>
2223
#include <lib/core/DataModelTypes.h>
2324

@@ -28,10 +29,35 @@
2829
namespace chip {
2930
namespace Test {
3031

32+
namespace internal {
33+
34+
constexpr EmberAfAttributeMetadata DefaultAttributeMetadata(chip::AttributeId id)
35+
{
36+
return EmberAfAttributeMetadata{
37+
.defaultValue = EmberAfDefaultOrMinMaxAttributeValue(static_cast<uint32_t>(0)),
38+
.attributeId = id,
39+
.size = 4,
40+
.attributeType = ZCL_INT32U_ATTRIBUTE_TYPE,
41+
.mask = ATTRIBUTE_MASK_WRITABLE | ATTRIBUTE_MASK_NULLABLE,
42+
};
43+
}
44+
45+
} // namespace internal
46+
3147
struct MockAttributeConfig
3248
{
33-
MockAttributeConfig(AttributeId aId) : id(aId) {}
49+
MockAttributeConfig(AttributeId aId) : id(aId), attributeMetaData(internal::DefaultAttributeMetadata(aId)) {}
50+
MockAttributeConfig(AttributeId aId, EmberAfAttributeType type,
51+
EmberAfAttributeMask mask = ATTRIBUTE_MASK_WRITABLE | ATTRIBUTE_MASK_NULLABLE) :
52+
id(aId),
53+
attributeMetaData(internal::DefaultAttributeMetadata(aId))
54+
{
55+
attributeMetaData.attributeType = type;
56+
attributeMetaData.mask = mask;
57+
}
58+
3459
const AttributeId id;
60+
EmberAfAttributeMetadata attributeMetaData;
3561
};
3662

3763
struct MockEventConfig
@@ -45,6 +71,10 @@ struct MockClusterConfig
4571
MockClusterConfig(ClusterId aId, std::initializer_list<MockAttributeConfig> aAttributes = {},
4672
std::initializer_list<MockEventConfig> aEvents = {});
4773

74+
// Cluster-config is self-referential: mEmberCluster.attributes references mAttributeMetaData.data()
75+
MockClusterConfig(const MockClusterConfig & other);
76+
MockClusterConfig & operator=(const MockClusterConfig &) = delete;
77+
4878
const MockAttributeConfig * attributeById(AttributeId attributeId, ptrdiff_t * outIndex = nullptr) const;
4979
const EmberAfCluster * emberCluster() const { return &mEmberCluster; }
5080

@@ -55,13 +85,14 @@ struct MockClusterConfig
5585
private:
5686
EmberAfCluster mEmberCluster;
5787
std::vector<EventId> mEmberEventList;
88+
std::vector<EmberAfAttributeMetadata> mAttributeMetaData;
5889
};
5990

6091
struct MockEndpointConfig
6192
{
6293
MockEndpointConfig(EndpointId aId, std::initializer_list<MockClusterConfig> aClusters = {});
6394

64-
// Cluster-config is self-referntial: mEmberCluster.clusters references mEmberClusters
95+
// Endpoint-config is self-referential: mEmberEndpoint.clusters references mEmberClusters.data()
6596
MockEndpointConfig(const MockEndpointConfig & other);
6697
MockEndpointConfig & operator=(const MockEndpointConfig &) = delete;
6798

src/app/util/mock/attribute-storage.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,14 @@ void EnabledEndpointsWithServerCluster::EnsureMatchingEndpoint()
307307
}
308308

309309
} // namespace app
310+
310311
namespace Test {
311312

313+
void ResetVersion()
314+
{
315+
dataVersion = 0;
316+
}
317+
312318
void BumpVersion()
313319
{
314320
dataVersion++;
@@ -407,5 +413,15 @@ CHIP_ERROR ReadSingleMockClusterData(FabricIndex aAccessingFabricIndex, const Co
407413
return attributeReport.EndOfAttributeReportIB();
408414
}
409415

416+
void SetMockNodeConfig(const MockNodeConfig & config)
417+
{
418+
mockConfig = &config;
419+
}
420+
421+
void ResetMockNodeConfig()
422+
{
423+
mockConfig = nullptr;
424+
}
425+
410426
} // namespace Test
411427
} // namespace chip

0 commit comments

Comments
 (0)