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/codedriven/endpoint/EndpointInterfaceRegistry.h>
18 : #include <lib/support/CodeUtils.h>
19 :
20 : namespace chip {
21 : namespace app {
22 :
23 253 : CHIP_ERROR EndpointInterfaceRegistry::Register(EndpointInterfaceRegistration & entry)
24 : {
25 253 : VerifyOrReturnError(entry.next == nullptr, CHIP_ERROR_INVALID_ARGUMENT); // Should not be part of another list
26 252 : VerifyOrReturnError(entry.endpointInterface != nullptr, CHIP_ERROR_INVALID_ARGUMENT); // Should not be null
27 252 : VerifyOrReturnError(entry.endpointEntry.id != kInvalidEndpointId, CHIP_ERROR_INVALID_ARGUMENT); // Should not be invalid ID
28 251 : VerifyOrReturnError(Get(entry.endpointEntry.id) == nullptr, CHIP_ERROR_DUPLICATE_KEY_ID); // Check for duplicates
29 :
30 249 : entry.next = mRegistrations;
31 249 : mRegistrations = &entry;
32 :
33 249 : return CHIP_NO_ERROR;
34 : }
35 :
36 146 : CHIP_ERROR EndpointInterfaceRegistry::Unregister(EndpointId endpointId)
37 : {
38 146 : VerifyOrReturnError(endpointId != kInvalidEndpointId, CHIP_ERROR_INVALID_ARGUMENT);
39 :
40 146 : EndpointInterfaceRegistration * prev = nullptr;
41 146 : EndpointInterfaceRegistration * current = mRegistrations;
42 :
43 2896 : while (current != nullptr)
44 : {
45 2894 : if (current->endpointEntry.id == endpointId)
46 : {
47 144 : if (prev == nullptr) // Node to remove is the head
48 : {
49 48 : mRegistrations = current->next;
50 : }
51 : else
52 : {
53 96 : prev->next = current->next;
54 : }
55 144 : current->next = nullptr; // Clear the registration's next pointer
56 :
57 144 : if (mCachedEndpointId == endpointId) // Invalidate cache if the unregistered endpoint was cached
58 : {
59 30 : mCachedInterface = nullptr;
60 30 : mCachedEndpointId = kInvalidEndpointId;
61 : }
62 144 : return CHIP_NO_ERROR;
63 : }
64 2750 : prev = current;
65 2750 : current = current->next;
66 : }
67 2 : return CHIP_ERROR_NOT_FOUND;
68 : }
69 :
70 707 : EndpointInterface * EndpointInterfaceRegistry::Get(EndpointId endpointId)
71 : {
72 707 : if (mCachedEndpointId == endpointId && mCachedInterface != nullptr)
73 : {
74 33 : return mCachedInterface;
75 : }
76 :
77 : // The endpoint searched for is not cached, do a linear search for it
78 674 : EndpointInterfaceRegistration * current = mRegistrations;
79 :
80 25460 : while (current != nullptr)
81 : {
82 25030 : if (current->endpointEntry.id == endpointId)
83 : {
84 244 : mCachedInterface = current->endpointInterface;
85 244 : mCachedEndpointId = endpointId;
86 244 : return mCachedInterface;
87 : }
88 24786 : current = current->next;
89 : }
90 :
91 : // Not found
92 430 : return nullptr;
93 : }
94 :
95 : } // namespace app
96 : } // namespace chip
|