Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft for API review] Use caller allocation for interface registries #37899

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/app/server-cluster/ServerClusterInterface.h
Original file line number Diff line number Diff line change
@@ -43,7 +43,12 @@ class ServerClusterInterface
virtual ~ServerClusterInterface() = default;

///////////////////////////////////// Cluster Metadata Support //////////////////////////////////////////////////
[[nodiscard]] virtual ClusterId GetClusterId() const = 0;

/// The path where this cluster operates on.
///
/// This path (endpointid,clusterid) is expected to be fixed once the server
/// cluster interface is in use.
[[nodiscard]] virtual ConcreteClusterPath GetPath() const = 0;

/// Gets the data version for this cluster instance.
///
19 changes: 10 additions & 9 deletions src/app/server-cluster/tests/TestDefaultServerCluster.cpp
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@

#include <access/Privilege.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app/ConcreteClusterPath.h>
#include <app/data-model-provider/MetadataList.h>
#include <app/data-model-provider/MetadataTypes.h>
#include <app/data-model-provider/OperationTypes.h>
@@ -44,9 +45,9 @@ namespace {
class FakeDefaultServerCluster : public DefaultServerCluster
{
public:
FakeDefaultServerCluster(ClusterId id) : mClusterId(id) {}
FakeDefaultServerCluster(ConcreteClusterPath path) : mPath(path) {}

ClusterId GetClusterId() const override { return mClusterId; }
[[nodiscard]] ConcreteClusterPath GetPath() const override { return mPath; }

DataModel::ActionReturnStatus ReadAttribute(const DataModel::ReadAttributeRequest & request,
AttributeValueEncoder & encoder) override
@@ -64,14 +65,14 @@ class FakeDefaultServerCluster : public DefaultServerCluster
void TestIncreaseDataVersion() { IncreaseDataVersion(); }

private:
ClusterId mClusterId;
ConcreteClusterPath mPath;
};

} // namespace

TEST(TestDefaultServerCluster, TestDataVersion)
{
FakeDefaultServerCluster cluster(1);
FakeDefaultServerCluster cluster({ 1, 2 });

DataVersion v1 = cluster.GetDataVersion();
cluster.TestIncreaseDataVersion();
@@ -80,13 +81,13 @@ TEST(TestDefaultServerCluster, TestDataVersion)

TEST(TestDefaultServerCluster, TestFlagsDefault)
{
FakeDefaultServerCluster cluster(1);
FakeDefaultServerCluster cluster({ 1, 2 });
ASSERT_EQ(cluster.GetClusterFlags().Raw(), 0u);
}

TEST(TestDefaultServerCluster, AttributesDefault)
{
FakeDefaultServerCluster cluster(1);
FakeDefaultServerCluster cluster({ 1, 2 });

DataModel::ListBuilder<AttributeEntry> attributes;

@@ -114,7 +115,7 @@ TEST(TestDefaultServerCluster, AttributesDefault)

TEST(TestDefaultServerCluster, CommandsDefault)
{
FakeDefaultServerCluster cluster(1);
FakeDefaultServerCluster cluster({ 1, 2 });

DataModel::ListBuilder<AcceptedCommandEntry> acceptedCommands;
ASSERT_EQ(cluster.AcceptedCommands({ 1, 1 }, acceptedCommands), CHIP_NO_ERROR);
@@ -127,7 +128,7 @@ TEST(TestDefaultServerCluster, CommandsDefault)

TEST(TestDefaultServerCluster, WriteAttributeDefault)
{
FakeDefaultServerCluster cluster(1);
FakeDefaultServerCluster cluster({ 1, 2 });

WriteOperation test(0 /* endpoint */, 1 /* cluster */, 1234 /* attribute */);
test.SetSubjectDescriptor(kAdminSubjectDescriptor);
@@ -140,7 +141,7 @@ TEST(TestDefaultServerCluster, WriteAttributeDefault)

TEST(TestDefaultServerCluster, InvokeDefault)
{
FakeDefaultServerCluster cluster(1);
FakeDefaultServerCluster cluster({ 1, 2 });

TLV::TLVReader tlvReader;
InvokeRequest request;
14 changes: 14 additions & 0 deletions src/data-model-providers/codegen/BUILD.gn
Original file line number Diff line number Diff line change
@@ -45,3 +45,17 @@ source_set("instance-header") {
# generally being unit tests or data_model.gni/data_model.cmake files)
sources = [ "Instance.h" ]
}

source_set("registry") {
sources = [
"ServerClusterInterfaceRegistry.cpp",
"ServerClusterInterfaceRegistry.h",
]

public_deps = [
"${chip_root}/src/app:paths",
"${chip_root}/src/app/server-cluster",
"${chip_root}/src/lib/core:types",
"${chip_root}/src/lib/support",
]
}
162 changes: 162 additions & 0 deletions src/data-model-providers/codegen/ServerClusterInterfaceRegistry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Copyright (c) 2025 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <data-model-providers/codegen/ServerClusterInterfaceRegistry.h>

#include <app/ConcreteClusterPath.h>
#include <app/server-cluster/ServerClusterInterface.h>
#include <lib/core/CHIPError.h>
#include <lib/core/DataModelTypes.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>

namespace chip {
namespace app {

ServerClusterInterfaceRegistry::~ServerClusterInterfaceRegistry()
{
while (mRegistrations != nullptr)
{
ServerClusterRegistration * next = mRegistrations->next;
mRegistrations->next = nullptr;
mRegistrations = next;
}
}

CHIP_ERROR ServerClusterInterfaceRegistry::Register(ServerClusterRegistration & entry)
{
// we have no strong way to check if entry is already registered somewhere else, so we use "next" as some
// form of double-check
VerifyOrReturnError(entry.next == nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(entry.serverClusterInterface != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

ConcreteClusterPath path = entry.serverClusterInterface->GetPath();

VerifyOrReturnError(path.HasValidIds(), CHIP_ERROR_INVALID_ARGUMENT);

// Double-checking for duplicates makes the checks O(n^2) on the total number of registered
// items. We preserve this however we may want to make this optional at some point in time.
VerifyOrReturnError(Get(path) == nullptr, CHIP_ERROR_DUPLICATE_KEY_ID);

entry.next = mRegistrations;
mRegistrations = &entry;

return CHIP_NO_ERROR;
}

ServerClusterInterface * ServerClusterInterfaceRegistry::Unregister(const ConcreteClusterPath & path)
{
ServerClusterRegistration * prev = nullptr;
ServerClusterRegistration * current = mRegistrations;

while (current != nullptr)
{
if (current->serverClusterInterface->GetPath() == path)
{
// take the item out of the current list and return it.
ServerClusterRegistration * next = current->next;

if (prev == nullptr)
{
mRegistrations = next;
}
else
{
prev->next = next;
}

if (mCachedInterface == current->serverClusterInterface)
{
mCachedInterface = nullptr;
}

current->next = nullptr; // some clearing
return current->serverClusterInterface;
}

prev = current;
current = current->next;
}

// Not found.
return nullptr;
}

ServerClusterInterfaceRegistry::ClustersList ServerClusterInterfaceRegistry::ClustersOnEndpoint(EndpointId endpointId)
{
return { mRegistrations, endpointId };
}

void ServerClusterInterfaceRegistry::UnregisterAllFromEndpoint(EndpointId endpointId)
{
ServerClusterRegistration * prev = nullptr;
ServerClusterRegistration * current = mRegistrations;
while (current != nullptr)
{
if (current->serverClusterInterface->GetPath().mEndpointId == endpointId)
{
if (mCachedInterface == current->serverClusterInterface)
{
mCachedInterface = nullptr;
}
if (prev == nullptr)
{
mRegistrations = current->next;
}
else
{
prev->next = current->next;
}
ServerClusterRegistration * actual_next = current->next;
current->next = nullptr; // some clearing
current = actual_next;
}
else
{
prev = current;
current = current->next;
}
}
}

ServerClusterInterface * ServerClusterInterfaceRegistry::Get(const ConcreteClusterPath & path)
{
// Check the cache to speed things up
if ((mCachedInterface != nullptr) && (mCachedInterface->GetPath() == path))
{
return mCachedInterface;
}

// The cluster searched for is not cached, do a linear search for it
ServerClusterRegistration * current = mRegistrations;

while (current != nullptr)
{
if (current->serverClusterInterface->GetPath() == path)
{
mCachedInterface = current->serverClusterInterface;
return mCachedInterface;
}

current = current->next;
}

// not found
return nullptr;
}

} // namespace app
} // namespace chip
Loading
Loading