|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include <app/AttributeAccessInterfaceRegistry.h> |
| 17 | + |
| 18 | +#include <app/AttributeAccessInterfaceCache.h> |
| 19 | + |
| 20 | +using namespace chip::app; |
| 21 | + |
| 22 | +namespace { |
| 23 | + |
| 24 | +AttributeAccessInterface * gAttributeAccessOverrides = nullptr; |
| 25 | +AttributeAccessInterfaceCache gAttributeAccessInterfaceCache; |
| 26 | + |
| 27 | +// shouldUnregister returns true if the given AttributeAccessInterface should be |
| 28 | +// unregistered. |
| 29 | +template <typename F> |
| 30 | +void UnregisterMatchingAttributeAccessInterfaces(F shouldUnregister) |
| 31 | +{ |
| 32 | + AttributeAccessInterface * prev = nullptr; |
| 33 | + AttributeAccessInterface * cur = gAttributeAccessOverrides; |
| 34 | + while (cur) |
| 35 | + { |
| 36 | + AttributeAccessInterface * next = cur->GetNext(); |
| 37 | + if (shouldUnregister(cur)) |
| 38 | + { |
| 39 | + // Remove it from the list |
| 40 | + if (prev) |
| 41 | + { |
| 42 | + prev->SetNext(next); |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + gAttributeAccessOverrides = next; |
| 47 | + } |
| 48 | + |
| 49 | + cur->SetNext(nullptr); |
| 50 | + |
| 51 | + // Do not change prev in this case. |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + prev = cur; |
| 56 | + } |
| 57 | + cur = next; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +} // namespace |
| 62 | + |
| 63 | +void unregisterAttributeAccessOverride(AttributeAccessInterface * attrOverride) |
| 64 | +{ |
| 65 | + gAttributeAccessInterfaceCache.Invalidate(); |
| 66 | + UnregisterMatchingAttributeAccessInterfaces([attrOverride](AttributeAccessInterface * entry) { return entry == attrOverride; }); |
| 67 | +} |
| 68 | + |
| 69 | +void unregisterAllAttributeAccessOverridesForEndpoint(EmberAfDefinedEndpoint * definedEndpoint) |
| 70 | +{ |
| 71 | + UnregisterMatchingAttributeAccessInterfaces( |
| 72 | + [endpoint = definedEndpoint->endpoint](AttributeAccessInterface * entry) { return entry->MatchesEndpoint(endpoint); }); |
| 73 | +} |
| 74 | + |
| 75 | +bool registerAttributeAccessOverride(AttributeAccessInterface * attrOverride) |
| 76 | +{ |
| 77 | + gAttributeAccessInterfaceCache.Invalidate(); |
| 78 | + for (auto * cur = gAttributeAccessOverrides; cur; cur = cur->GetNext()) |
| 79 | + { |
| 80 | + if (cur->Matches(*attrOverride)) |
| 81 | + { |
| 82 | + ChipLogError(InteractionModel, "Duplicate attribute override registration failed"); |
| 83 | + return false; |
| 84 | + } |
| 85 | + } |
| 86 | + attrOverride->SetNext(gAttributeAccessOverrides); |
| 87 | + gAttributeAccessOverrides = attrOverride; |
| 88 | + return true; |
| 89 | +} |
| 90 | + |
| 91 | +namespace chip { |
| 92 | +namespace app { |
| 93 | + |
| 94 | +app::AttributeAccessInterface * GetAttributeAccessOverride(EndpointId endpointId, ClusterId clusterId) |
| 95 | +{ |
| 96 | + using CacheResult = AttributeAccessInterfaceCache::CacheResult; |
| 97 | + |
| 98 | + AttributeAccessInterface * cached = nullptr; |
| 99 | + CacheResult result = gAttributeAccessInterfaceCache.Get(endpointId, clusterId, &cached); |
| 100 | + switch (result) |
| 101 | + { |
| 102 | + case CacheResult::kDefinitelyUnused: |
| 103 | + return nullptr; |
| 104 | + case CacheResult::kDefinitelyUsed: |
| 105 | + return cached; |
| 106 | + case CacheResult::kCacheMiss: |
| 107 | + default: |
| 108 | + // Did not cache yet, search set of AAI registered, and cache if found. |
| 109 | + for (app::AttributeAccessInterface * cur = gAttributeAccessOverrides; cur; cur = cur->GetNext()) |
| 110 | + { |
| 111 | + if (cur->Matches(endpointId, clusterId)) |
| 112 | + { |
| 113 | + gAttributeAccessInterfaceCache.MarkUsed(endpointId, clusterId, cur); |
| 114 | + return cur; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Did not find AAI registered: mark as definitely not using. |
| 119 | + gAttributeAccessInterfaceCache.MarkUnused(endpointId, clusterId); |
| 120 | + } |
| 121 | + |
| 122 | + return nullptr; |
| 123 | +} |
| 124 | + |
| 125 | +} // namespace app |
| 126 | +} // namespace chip |
0 commit comments