forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerClusterInterfaceRegistry.cpp
162 lines (138 loc) · 5.07 KB
/
ServerClusterInterfaceRegistry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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; // Make sure current does not look like part of a list.
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; // Make sure current does not look like part of a list.
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