Line data Source code
1 : /*
2 : * Copyright (c) 2025 Project CHIP Authors
3 : * All rights reserved.
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 : #include <data-model-providers/endpoint/EndpointProviderRegistry.h>
18 : #include <lib/support/CodeUtils.h>
19 :
20 : namespace chip {
21 : namespace app {
22 :
23 212 : CHIP_ERROR EndpointProviderRegistry::Register(EndpointProviderRegistration & entry)
24 : {
25 212 : VerifyOrReturnError(entry.next == nullptr, CHIP_ERROR_INVALID_ARGUMENT); // Should not be part of another list
26 211 : VerifyOrReturnError(entry.endpointProviderInterface != nullptr, CHIP_ERROR_INVALID_ARGUMENT); // Should not be null
27 :
28 211 : auto newEndpointId = entry.endpointProviderInterface->GetEndpointEntry().id;
29 211 : VerifyOrReturnError(newEndpointId != kInvalidEndpointId, CHIP_ERROR_INVALID_ARGUMENT);
30 211 : VerifyOrReturnError(Get(newEndpointId) == nullptr, CHIP_ERROR_DUPLICATE_KEY_ID); // Check for duplicates
31 :
32 209 : entry.next = mRegistrations;
33 209 : mRegistrations = &entry;
34 :
35 209 : return CHIP_NO_ERROR;
36 : }
37 :
38 105 : CHIP_ERROR EndpointProviderRegistry::Unregister(EndpointId endpointId)
39 : {
40 105 : VerifyOrReturnError(endpointId != kInvalidEndpointId, CHIP_ERROR_INVALID_ARGUMENT);
41 :
42 105 : EndpointProviderRegistration * prev = nullptr;
43 105 : EndpointProviderRegistration * current = mRegistrations;
44 :
45 2853 : while (current != nullptr)
46 : {
47 2851 : if (current->endpointProviderInterface->GetEndpointEntry().id == endpointId)
48 : {
49 103 : if (prev == nullptr) // Node to remove is the head
50 : {
51 9 : mRegistrations = current->next;
52 : }
53 : else
54 : {
55 94 : prev->next = current->next;
56 : }
57 103 : current->next = nullptr; // Clear the registration's next pointer
58 :
59 103 : if (mCachedEndpointId == endpointId) // Invalidate cache if the unregistered endpoint was cached
60 : {
61 3 : mCachedInterface = nullptr;
62 3 : mCachedEndpointId = kInvalidEndpointId;
63 : }
64 103 : return CHIP_NO_ERROR;
65 : }
66 2748 : prev = current;
67 2748 : current = current->next;
68 : }
69 2 : return CHIP_NO_ERROR;
70 : }
71 :
72 622 : EndpointProviderInterface * EndpointProviderRegistry::Get(EndpointId endpointId)
73 : {
74 622 : if (mCachedEndpointId == endpointId && mCachedInterface != nullptr)
75 : {
76 103 : return mCachedInterface;
77 : }
78 :
79 : // The endpoint searched for is not cached, do a linear search for it
80 519 : EndpointProviderRegistration * current = mRegistrations;
81 :
82 25273 : while (current != nullptr)
83 : {
84 24960 : if (current->endpointProviderInterface->GetEndpointEntry().id == endpointId)
85 : {
86 206 : mCachedInterface = current->endpointProviderInterface;
87 206 : mCachedEndpointId = endpointId;
88 206 : return mCachedInterface;
89 : }
90 24754 : current = current->next;
91 : }
92 :
93 : // Not found
94 313 : return nullptr;
95 : }
96 :
97 : } // namespace app
98 : } // namespace chip
|