|
| 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/codegen/ServerClusterInterfaceRegistry.h> |
| 18 | + |
| 19 | +#include <app/ConcreteClusterPath.h> |
| 20 | +#include <app/server-cluster/ServerClusterInterface.h> |
| 21 | +#include <lib/core/CHIPError.h> |
| 22 | +#include <lib/core/DataModelTypes.h> |
| 23 | +#include <lib/support/CHIPMem.h> |
| 24 | +#include <lib/support/CodeUtils.h> |
| 25 | + |
| 26 | +namespace chip { |
| 27 | +namespace app { |
| 28 | + |
| 29 | +ServerClusterInterfaceRegistry::~ServerClusterInterfaceRegistry() |
| 30 | +{ |
| 31 | + while (mRegistrations != nullptr) |
| 32 | + { |
| 33 | + ServerClusterRegistration * next = mRegistrations->next; |
| 34 | + mRegistrations->next = nullptr; |
| 35 | + mRegistrations = next; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +CHIP_ERROR ServerClusterInterfaceRegistry::Register(ServerClusterRegistration & entry) |
| 40 | +{ |
| 41 | + // we have no strong way to check if entry is already registered somewhere else, so we use "next" as some |
| 42 | + // form of double-check |
| 43 | + VerifyOrReturnError(entry.next == nullptr, CHIP_ERROR_INVALID_ARGUMENT); |
| 44 | + VerifyOrReturnError(entry.serverClusterInterface != nullptr, CHIP_ERROR_INVALID_ARGUMENT); |
| 45 | + |
| 46 | + ConcreteClusterPath path = entry.serverClusterInterface->GetPath(); |
| 47 | + |
| 48 | + VerifyOrReturnError(path.HasValidIds(), CHIP_ERROR_INVALID_ARGUMENT); |
| 49 | + |
| 50 | + // Double-checking for duplicates makes the checks O(n^2) on the total number of registered |
| 51 | + // items. We preserve this however we may want to make this optional at some point in time. |
| 52 | + VerifyOrReturnError(Get(path) == nullptr, CHIP_ERROR_DUPLICATE_KEY_ID); |
| 53 | + |
| 54 | + entry.next = mRegistrations; |
| 55 | + mRegistrations = &entry; |
| 56 | + |
| 57 | + return CHIP_NO_ERROR; |
| 58 | +} |
| 59 | + |
| 60 | +ServerClusterInterface * ServerClusterInterfaceRegistry::Unregister(const ConcreteClusterPath & path) |
| 61 | +{ |
| 62 | + ServerClusterRegistration * prev = nullptr; |
| 63 | + ServerClusterRegistration * current = mRegistrations; |
| 64 | + |
| 65 | + while (current != nullptr) |
| 66 | + { |
| 67 | + if (current->serverClusterInterface->GetPath() == path) |
| 68 | + { |
| 69 | + // take the item out of the current list and return it. |
| 70 | + ServerClusterRegistration * next = current->next; |
| 71 | + |
| 72 | + if (prev == nullptr) |
| 73 | + { |
| 74 | + mRegistrations = next; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + prev->next = next; |
| 79 | + } |
| 80 | + |
| 81 | + if (mCachedInterface == current->serverClusterInterface) |
| 82 | + { |
| 83 | + mCachedInterface = nullptr; |
| 84 | + } |
| 85 | + |
| 86 | + current->next = nullptr; // Make sure current does not look like part of a list. |
| 87 | + return current->serverClusterInterface; |
| 88 | + } |
| 89 | + |
| 90 | + prev = current; |
| 91 | + current = current->next; |
| 92 | + } |
| 93 | + |
| 94 | + // Not found. |
| 95 | + return nullptr; |
| 96 | +} |
| 97 | + |
| 98 | +ServerClusterInterfaceRegistry::ClustersList ServerClusterInterfaceRegistry::ClustersOnEndpoint(EndpointId endpointId) |
| 99 | +{ |
| 100 | + return { mRegistrations, endpointId }; |
| 101 | +} |
| 102 | + |
| 103 | +void ServerClusterInterfaceRegistry::UnregisterAllFromEndpoint(EndpointId endpointId) |
| 104 | +{ |
| 105 | + ServerClusterRegistration * prev = nullptr; |
| 106 | + ServerClusterRegistration * current = mRegistrations; |
| 107 | + while (current != nullptr) |
| 108 | + { |
| 109 | + if (current->serverClusterInterface->GetPath().mEndpointId == endpointId) |
| 110 | + { |
| 111 | + if (mCachedInterface == current->serverClusterInterface) |
| 112 | + { |
| 113 | + mCachedInterface = nullptr; |
| 114 | + } |
| 115 | + if (prev == nullptr) |
| 116 | + { |
| 117 | + mRegistrations = current->next; |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + prev->next = current->next; |
| 122 | + } |
| 123 | + ServerClusterRegistration * actual_next = current->next; |
| 124 | + current->next = nullptr; // Make sure current does not look like part of a list. |
| 125 | + current = actual_next; |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + prev = current; |
| 130 | + current = current->next; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +ServerClusterInterface * ServerClusterInterfaceRegistry::Get(const ConcreteClusterPath & path) |
| 136 | +{ |
| 137 | + // Check the cache to speed things up |
| 138 | + if ((mCachedInterface != nullptr) && (mCachedInterface->GetPath() == path)) |
| 139 | + { |
| 140 | + return mCachedInterface; |
| 141 | + } |
| 142 | + |
| 143 | + // The cluster searched for is not cached, do a linear search for it |
| 144 | + ServerClusterRegistration * current = mRegistrations; |
| 145 | + |
| 146 | + while (current != nullptr) |
| 147 | + { |
| 148 | + if (current->serverClusterInterface->GetPath() == path) |
| 149 | + { |
| 150 | + mCachedInterface = current->serverClusterInterface; |
| 151 | + return mCachedInterface; |
| 152 | + } |
| 153 | + |
| 154 | + current = current->next; |
| 155 | + } |
| 156 | + |
| 157 | + // not found |
| 158 | + return nullptr; |
| 159 | +} |
| 160 | + |
| 161 | +} // namespace app |
| 162 | +} // namespace chip |
0 commit comments