Skip to content

Commit

Permalink
More fixes to src/app/util/mock. (project-chip#32932)
Browse files Browse the repository at this point in the history
* More fixes to src/app/util/mock.

* Remove misplaced comment

* Remove misplaced comment

* Fix compilation for esp32

* Fix typo
  • Loading branch information
andy31415 authored Apr 11, 2024
1 parent f52eabf commit 9e0f288
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/app/util/mock/Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ namespace Test {
CHIP_ERROR ReadSingleMockClusterData(FabricIndex aAccessingFabricIndex, const app::ConcreteAttributePath & aPath,
app::AttributeReportIBs::Builder & aAttributeReports,
app::AttributeValueEncoder::AttributeEncodeState * apEncoderState);

/// Increase the current value for `GetVersion`
void BumpVersion();

/// Sets GetVersion to return 0
void ResetVersion();

/// Gets the current value for the version that will
/// be returned by emberAfDataVersionStorage
DataVersion GetVersion();

/// Configures the singular global mock attribute storage to use the specified configuration.
Expand Down
16 changes: 16 additions & 0 deletions src/app/util/mock/MockNodeConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ MockClusterConfig::MockClusterConfig(ClusterId aId, std::initializer_list<MockAt
mEmberCluster.mask = CLUSTER_MASK_SERVER;
mEmberCluster.eventCount = static_cast<uint16_t>(mEmberEventList.size());
mEmberCluster.eventList = mEmberEventList.data();

for (auto & attr : attributes)
{
mAttributeMetaData.push_back(attr.attributeMetaData);
}

// Make sure ember side has access to attribute metadata
mEmberCluster.attributes = mAttributeMetaData.data();
}

MockClusterConfig::MockClusterConfig(const MockClusterConfig & other) :
id(other.id), attributes(other.attributes), events(other.events), mEmberCluster(other.mEmberCluster),
mEmberEventList(other.mEmberEventList), mAttributeMetaData(other.mAttributeMetaData)
{
// Fix self-referencial dependencies after data copy
mEmberCluster.attributes = mAttributeMetaData.data();
}

const MockAttributeConfig * MockClusterConfig::attributeById(AttributeId attributeId, ptrdiff_t * outIndex) const
Expand Down
35 changes: 33 additions & 2 deletions src/app/util/mock/MockNodeConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

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

Expand All @@ -28,10 +29,35 @@
namespace chip {
namespace Test {

namespace internal {

constexpr EmberAfAttributeMetadata DefaultAttributeMetadata(chip::AttributeId id)
{
return EmberAfAttributeMetadata{
.defaultValue = EmberAfDefaultOrMinMaxAttributeValue(static_cast<uint32_t>(0)),
.attributeId = id,
.size = 4,
.attributeType = ZCL_INT32U_ATTRIBUTE_TYPE,
.mask = ATTRIBUTE_MASK_WRITABLE | ATTRIBUTE_MASK_NULLABLE,
};
}

} // namespace internal

struct MockAttributeConfig
{
MockAttributeConfig(AttributeId aId) : id(aId) {}
MockAttributeConfig(AttributeId aId) : id(aId), attributeMetaData(internal::DefaultAttributeMetadata(aId)) {}
MockAttributeConfig(AttributeId aId, EmberAfAttributeType type,
EmberAfAttributeMask mask = ATTRIBUTE_MASK_WRITABLE | ATTRIBUTE_MASK_NULLABLE) :
id(aId),
attributeMetaData(internal::DefaultAttributeMetadata(aId))
{
attributeMetaData.attributeType = type;
attributeMetaData.mask = mask;
}

const AttributeId id;
EmberAfAttributeMetadata attributeMetaData;
};

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

// Cluster-config is self-referential: mEmberCluster.attributes references mAttributeMetaData.data()
MockClusterConfig(const MockClusterConfig & other);
MockClusterConfig & operator=(const MockClusterConfig &) = delete;

const MockAttributeConfig * attributeById(AttributeId attributeId, ptrdiff_t * outIndex = nullptr) const;
const EmberAfCluster * emberCluster() const { return &mEmberCluster; }

Expand All @@ -55,13 +85,14 @@ struct MockClusterConfig
private:
EmberAfCluster mEmberCluster;
std::vector<EventId> mEmberEventList;
std::vector<EmberAfAttributeMetadata> mAttributeMetaData;
};

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

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

Expand Down
16 changes: 16 additions & 0 deletions src/app/util/mock/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,14 @@ void EnabledEndpointsWithServerCluster::EnsureMatchingEndpoint()
}

} // namespace app

namespace Test {

void ResetVersion()
{
dataVersion = 0;
}

void BumpVersion()
{
dataVersion++;
Expand Down Expand Up @@ -407,5 +413,15 @@ CHIP_ERROR ReadSingleMockClusterData(FabricIndex aAccessingFabricIndex, const Co
return attributeReport.EndOfAttributeReportIB();
}

void SetMockNodeConfig(const MockNodeConfig & config)
{
mockConfig = &config;
}

void ResetMockNodeConfig()
{
mockConfig = nullptr;
}

} // namespace Test
} // namespace chip

0 comments on commit 9e0f288

Please sign in to comment.