Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) 2021 Project CHIP Authors |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include "system/SystemPacketBuffer.h" |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 20 | #include <app/ClusterStateCache.h> |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 21 | #include <app/InteractionModelEngine.h> |
| 22 | #include <tuple> |
| 23 | |
| 24 | namespace chip { |
| 25 | namespace app { |
| 26 | |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | // Determine how much space a StatusIB takes up on the wire. |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 30 | uint32_t SizeOfStatusIB(const StatusIB & aStatus) |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 31 | { |
| 32 | // 1 byte: anonymous tag control byte for struct. |
| 33 | // 1 byte: control byte for uint8 value. |
| 34 | // 1 byte: context-specific tag for uint8 value. |
| 35 | // 1 byte: the uint8 value. |
| 36 | // 1 byte: end of container. |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 37 | uint32_t size = 5; |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 38 | |
| 39 | if (aStatus.mClusterStatus.HasValue()) |
| 40 | { |
| 41 | // 1 byte: control byte for uint8 value. |
| 42 | // 1 byte: context-specific tag for uint8 value. |
| 43 | // 1 byte: the uint8 value. |
| 44 | size += 3; |
| 45 | } |
| 46 | |
| 47 | return size; |
| 48 | } |
| 49 | |
| 50 | } // anonymous namespace |
| 51 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 52 | template <bool CanEnableDataCaching> |
| 53 | CHIP_ERROR ClusterStateCacheT<CanEnableDataCaching>::GetElementTLVSize(TLV::TLVReader * apData, uint32_t & aSize) |
yunhanw-google | 3ef81a8 | 2022-05-19 10:46:23 -0700 | [diff] [blame] | 54 | { |
| 55 | Platform::ScopedMemoryBufferWithSize<uint8_t> backingBuffer; |
| 56 | TLV::TLVReader reader; |
| 57 | reader.Init(*apData); |
| 58 | size_t totalBufSize = reader.GetTotalLength(); |
| 59 | backingBuffer.Calloc(totalBufSize); |
| 60 | VerifyOrReturnError(backingBuffer.Get() != nullptr, CHIP_ERROR_NO_MEMORY); |
| 61 | TLV::ScopedBufferTLVWriter writer(std::move(backingBuffer), totalBufSize); |
| 62 | ReturnErrorOnFailure(writer.CopyElement(TLV::AnonymousTag(), reader)); |
| 63 | aSize = writer.GetLengthWritten(); |
| 64 | ReturnErrorOnFailure(writer.Finalize(backingBuffer)); |
| 65 | return CHIP_NO_ERROR; |
| 66 | } |
| 67 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 68 | template <bool CanEnableDataCaching> |
| 69 | CHIP_ERROR ClusterStateCacheT<CanEnableDataCaching>::UpdateCache(const ConcreteDataAttributePath & aPath, TLV::TLVReader * apData, |
| 70 | const StatusIB & aStatus) |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 71 | { |
| 72 | AttributeState state; |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 73 | bool endpointIsNew = false; |
| 74 | |
| 75 | if (mCache.find(aPath.mEndpointId) == mCache.end()) |
| 76 | { |
| 77 | // |
| 78 | // Since we might potentially be creating a new entry at mCache[aPath.mEndpointId][aPath.mClusterId] that |
| 79 | // wasn't there before, we need to check if an entry didn't exist there previously and remember that so that |
| 80 | // we can appropriately notify our clients of the addition of a new endpoint. |
| 81 | // |
| 82 | endpointIsNew = true; |
| 83 | } |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 84 | |
| 85 | if (apData) |
| 86 | { |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 87 | uint32_t elementSize = 0; |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 88 | ReturnErrorOnFailure(GetElementTLVSize(apData, elementSize)); |
| 89 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 90 | if constexpr (CanEnableDataCaching) |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 91 | { |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 92 | if (mCacheData) |
| 93 | { |
| 94 | Platform::ScopedMemoryBufferWithSize<uint8_t> backingBuffer; |
| 95 | backingBuffer.Calloc(elementSize); |
| 96 | VerifyOrReturnError(backingBuffer.Get() != nullptr, CHIP_ERROR_NO_MEMORY); |
| 97 | TLV::ScopedBufferTLVWriter writer(std::move(backingBuffer), elementSize); |
| 98 | ReturnErrorOnFailure(writer.CopyElement(TLV::AnonymousTag(), *apData)); |
| 99 | ReturnErrorOnFailure(writer.Finalize(backingBuffer)); |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 100 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 101 | state.template Set<AttributeData>(std::move(backingBuffer)); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | state.template Set<uint32_t>(elementSize); |
| 106 | } |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 107 | } |
| 108 | else |
| 109 | { |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 110 | state = elementSize; |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 111 | } |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 112 | |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 113 | // |
| 114 | // Clear out the committed data version and only set it again once we have received all data for this cluster. |
| 115 | // Otherwise, we may have incomplete data that looks like it's complete since it has a valid data version. |
| 116 | // |
| 117 | mCache[aPath.mEndpointId][aPath.mClusterId].mCommittedDataVersion.ClearValue(); |
| 118 | |
| 119 | // This commits a pending data version if the last report path is valid and it is different from the current path. |
| 120 | if (mLastReportDataPath.IsValidConcreteClusterPath() && mLastReportDataPath != aPath) |
| 121 | { |
| 122 | CommitPendingDataVersion(); |
| 123 | } |
| 124 | |
| 125 | bool foundEncompassingWildcardPath = false; |
| 126 | for (const auto & path : mRequestPathSet) |
| 127 | { |
| 128 | if (path.IncludesAllAttributesInCluster(aPath)) |
| 129 | { |
| 130 | foundEncompassingWildcardPath = true; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // if this data item is encompassed by a wildcard path, let's go ahead and update its pending data version. |
| 136 | if (foundEncompassingWildcardPath) |
| 137 | { |
| 138 | mCache[aPath.mEndpointId][aPath.mClusterId].mPendingDataVersion = aPath.mDataVersion; |
| 139 | } |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 140 | |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 141 | mLastReportDataPath = aPath; |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 142 | } |
| 143 | else |
| 144 | { |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 145 | if constexpr (CanEnableDataCaching) |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 146 | { |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 147 | if (mCacheData) |
| 148 | { |
| 149 | state.template Set<StatusIB>(aStatus); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | state.template Set<uint32_t>(SizeOfStatusIB(aStatus)); |
| 154 | } |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 155 | } |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 156 | else |
| 157 | { |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 158 | state = SizeOfStatusIB(aStatus); |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 159 | } |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // |
| 163 | // if the endpoint didn't exist previously, let's track the insertion |
| 164 | // so that we can inform our callback of a new endpoint being added appropriately. |
| 165 | // |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 166 | if (endpointIsNew) |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 167 | { |
| 168 | mAddedEndpoints.push_back(aPath.mEndpointId); |
| 169 | } |
| 170 | |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 171 | mCache[aPath.mEndpointId][aPath.mClusterId].mAttributes[aPath.mAttributeId] = std::move(state); |
| 172 | |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 173 | if (mCacheData) |
| 174 | { |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 175 | mChangedAttributeSet.insert(aPath); |
| 176 | } |
| 177 | |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 178 | return CHIP_NO_ERROR; |
| 179 | } |
| 180 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 181 | template <bool CanEnableDataCaching> |
| 182 | CHIP_ERROR ClusterStateCacheT<CanEnableDataCaching>::UpdateEventCache(const EventHeader & aEventHeader, TLV::TLVReader * apData, |
| 183 | const StatusIB * apStatus) |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 184 | { |
| 185 | if (apData) |
| 186 | { |
| 187 | // |
| 188 | // If we've already seen this event before, there's no more work to be done. |
| 189 | // |
| 190 | if (mHighestReceivedEventNumber.HasValue() && aEventHeader.mEventNumber <= mHighestReceivedEventNumber.Value()) |
| 191 | { |
| 192 | return CHIP_NO_ERROR; |
| 193 | } |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 194 | if (mCacheData) |
| 195 | { |
| 196 | System::PacketBufferHandle handle = System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes); |
| 197 | VerifyOrReturnError(!handle.IsNull(), CHIP_ERROR_NO_MEMORY); |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 198 | |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 199 | System::PacketBufferTLVWriter writer; |
| 200 | writer.Init(std::move(handle), false); |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 201 | |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 202 | ReturnErrorOnFailure(writer.CopyElement(TLV::AnonymousTag(), *apData)); |
| 203 | ReturnErrorOnFailure(writer.Finalize(&handle)); |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 204 | |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 205 | // |
| 206 | // Compact the buffer down to a more reasonably sized packet buffer |
| 207 | // if we can. |
| 208 | // |
| 209 | handle.RightSize(); |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 210 | |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 211 | EventData eventData; |
| 212 | eventData.first = aEventHeader; |
| 213 | eventData.second = std::move(handle); |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 214 | |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 215 | mEventDataCache.insert(std::move(eventData)); |
| 216 | } |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 217 | mHighestReceivedEventNumber.SetValue(aEventHeader.mEventNumber); |
| 218 | } |
| 219 | else if (apStatus) |
| 220 | { |
yunhanw-google | 0e896b4 | 2023-01-30 08:31:00 -0800 | [diff] [blame] | 221 | if (mCacheData) |
| 222 | { |
| 223 | mEventStatusCache[aEventHeader.mPath] = *apStatus; |
| 224 | } |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | return CHIP_NO_ERROR; |
| 228 | } |
| 229 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 230 | template <bool CanEnableDataCaching> |
| 231 | void ClusterStateCacheT<CanEnableDataCaching>::OnReportBegin() |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 232 | { |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 233 | mLastReportDataPath = ConcreteClusterPath(kInvalidEndpointId, kInvalidClusterId); |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 234 | mChangedAttributeSet.clear(); |
| 235 | mAddedEndpoints.clear(); |
Michael Spang | a2f1743 | 2022-01-26 20:52:14 -0500 | [diff] [blame] | 236 | mCallback.OnReportBegin(); |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 239 | template <bool CanEnableDataCaching> |
| 240 | void ClusterStateCacheT<CanEnableDataCaching>::CommitPendingDataVersion() |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 241 | { |
| 242 | if (!mLastReportDataPath.IsValidConcreteClusterPath()) |
| 243 | { |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | auto & lastClusterInfo = mCache[mLastReportDataPath.mEndpointId][mLastReportDataPath.mClusterId]; |
| 248 | if (lastClusterInfo.mPendingDataVersion.HasValue()) |
| 249 | { |
| 250 | lastClusterInfo.mCommittedDataVersion = lastClusterInfo.mPendingDataVersion; |
| 251 | lastClusterInfo.mPendingDataVersion.ClearValue(); |
| 252 | } |
| 253 | } |
| 254 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 255 | template <bool CanEnableDataCaching> |
| 256 | void ClusterStateCacheT<CanEnableDataCaching>::OnReportEnd() |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 257 | { |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 258 | CommitPendingDataVersion(); |
| 259 | mLastReportDataPath = ConcreteClusterPath(kInvalidEndpointId, kInvalidClusterId); |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 260 | std::set<std::tuple<EndpointId, ClusterId>> changedClusters; |
| 261 | |
| 262 | // |
| 263 | // Add the EndpointId and ClusterId into a set so that we only |
| 264 | // convey unique combinations in the subsequent OnClusterChanged callback. |
| 265 | // |
| 266 | for (auto & path : mChangedAttributeSet) |
| 267 | { |
| 268 | mCallback.OnAttributeChanged(this, path); |
| 269 | changedClusters.insert(std::make_tuple(path.mEndpointId, path.mClusterId)); |
| 270 | } |
| 271 | |
| 272 | for (auto & item : changedClusters) |
| 273 | { |
| 274 | mCallback.OnClusterChanged(this, std::get<0>(item), std::get<1>(item)); |
| 275 | } |
| 276 | |
| 277 | for (auto endpoint : mAddedEndpoints) |
| 278 | { |
| 279 | mCallback.OnEndpointAdded(this, endpoint); |
| 280 | } |
| 281 | |
Michael Spang | a2f1743 | 2022-01-26 20:52:14 -0500 | [diff] [blame] | 282 | mCallback.OnReportEnd(); |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 285 | template <> |
| 286 | CHIP_ERROR ClusterStateCacheT<true>::Get(const ConcreteAttributePath & path, TLV::TLVReader & reader) const |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 287 | { |
| 288 | CHIP_ERROR err; |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 289 | auto attributeState = GetAttributeState(path.mEndpointId, path.mClusterId, path.mAttributeId, err); |
| 290 | ReturnErrorOnFailure(err); |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 291 | |
| 292 | if (attributeState->template Is<StatusIB>()) |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 293 | { |
| 294 | return CHIP_ERROR_IM_STATUS_CODE_RECEIVED; |
| 295 | } |
| 296 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 297 | if (!attributeState->template Is<AttributeData>()) |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 298 | { |
| 299 | return CHIP_ERROR_KEY_NOT_FOUND; |
| 300 | } |
| 301 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 302 | reader.Init(attributeState->template Get<AttributeData>().Get(), attributeState->template Get<AttributeData>().AllocatedSize()); |
yunhanw-google | 3ef81a8 | 2022-05-19 10:46:23 -0700 | [diff] [blame] | 303 | return reader.Next(); |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 306 | template <> |
| 307 | CHIP_ERROR ClusterStateCacheT<false>::Get(const ConcreteAttributePath & path, TLV::TLVReader & reader) const |
| 308 | { |
| 309 | return CHIP_ERROR_KEY_NOT_FOUND; |
| 310 | } |
| 311 | |
| 312 | template <bool CanEnableDataCaching> |
| 313 | CHIP_ERROR ClusterStateCacheT<CanEnableDataCaching>::Get(EventNumber eventNumber, TLV::TLVReader & reader) const |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 314 | { |
| 315 | CHIP_ERROR err; |
| 316 | |
| 317 | auto eventData = GetEventData(eventNumber, err); |
| 318 | ReturnErrorOnFailure(err); |
| 319 | |
| 320 | System::PacketBufferTLVReader bufReader; |
| 321 | |
| 322 | bufReader.Init(eventData->second.Retain()); |
| 323 | ReturnErrorOnFailure(bufReader.Next()); |
| 324 | |
| 325 | reader.Init(bufReader); |
| 326 | return CHIP_NO_ERROR; |
| 327 | } |
| 328 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 329 | template <bool CanEnableDataCaching> |
| 330 | const typename ClusterStateCacheT<CanEnableDataCaching>::EndpointState * |
| 331 | ClusterStateCacheT<CanEnableDataCaching>::GetEndpointState(EndpointId endpointId, CHIP_ERROR & err) const |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 332 | { |
| 333 | auto endpointIter = mCache.find(endpointId); |
| 334 | if (endpointIter == mCache.end()) |
| 335 | { |
| 336 | err = CHIP_ERROR_KEY_NOT_FOUND; |
| 337 | return nullptr; |
| 338 | } |
| 339 | |
| 340 | err = CHIP_NO_ERROR; |
| 341 | return &endpointIter->second; |
| 342 | } |
| 343 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 344 | template <bool CanEnableDataCaching> |
| 345 | const typename ClusterStateCacheT<CanEnableDataCaching>::ClusterState * |
| 346 | ClusterStateCacheT<CanEnableDataCaching>::GetClusterState(EndpointId endpointId, ClusterId clusterId, CHIP_ERROR & err) const |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 347 | { |
| 348 | auto endpointState = GetEndpointState(endpointId, err); |
| 349 | if (err != CHIP_NO_ERROR) |
| 350 | { |
| 351 | return nullptr; |
| 352 | } |
| 353 | |
| 354 | auto clusterState = endpointState->find(clusterId); |
| 355 | if (clusterState == endpointState->end()) |
| 356 | { |
| 357 | err = CHIP_ERROR_KEY_NOT_FOUND; |
| 358 | return nullptr; |
| 359 | } |
| 360 | |
| 361 | err = CHIP_NO_ERROR; |
| 362 | return &clusterState->second; |
| 363 | } |
| 364 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 365 | template <bool CanEnableDataCaching> |
| 366 | const typename ClusterStateCacheT<CanEnableDataCaching>::AttributeState * |
| 367 | ClusterStateCacheT<CanEnableDataCaching>::GetAttributeState(EndpointId endpointId, ClusterId clusterId, AttributeId attributeId, |
| 368 | CHIP_ERROR & err) const |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 369 | { |
| 370 | auto clusterState = GetClusterState(endpointId, clusterId, err); |
| 371 | if (err != CHIP_NO_ERROR) |
| 372 | { |
| 373 | return nullptr; |
| 374 | } |
| 375 | |
| 376 | auto attributeState = clusterState->mAttributes.find(attributeId); |
| 377 | if (attributeState == clusterState->mAttributes.end()) |
| 378 | { |
| 379 | err = CHIP_ERROR_KEY_NOT_FOUND; |
| 380 | return nullptr; |
| 381 | } |
| 382 | |
| 383 | err = CHIP_NO_ERROR; |
| 384 | return &attributeState->second; |
| 385 | } |
| 386 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 387 | template <bool CanEnableDataCaching> |
| 388 | const typename ClusterStateCacheT<CanEnableDataCaching>::EventData * |
| 389 | ClusterStateCacheT<CanEnableDataCaching>::GetEventData(EventNumber eventNumber, CHIP_ERROR & err) const |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 390 | { |
| 391 | EventData compareKey; |
| 392 | |
| 393 | compareKey.first.mEventNumber = eventNumber; |
| 394 | auto eventData = mEventDataCache.find(std::move(compareKey)); |
| 395 | if (eventData == mEventDataCache.end()) |
| 396 | { |
| 397 | err = CHIP_ERROR_KEY_NOT_FOUND; |
| 398 | return nullptr; |
| 399 | } |
| 400 | |
| 401 | err = CHIP_NO_ERROR; |
| 402 | return &(*eventData); |
| 403 | } |
| 404 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 405 | template <bool CanEnableDataCaching> |
| 406 | void ClusterStateCacheT<CanEnableDataCaching>::OnAttributeData(const ConcreteDataAttributePath & aPath, TLV::TLVReader * apData, |
| 407 | const StatusIB & aStatus) |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 408 | { |
| 409 | // |
| 410 | // Since the cache itself is a ReadClient::Callback, it may be incorrectly passed in directly when registering with the |
| 411 | // ReadClient. This should be avoided, since that bypasses the built-in buffered reader adapter callback that is needed for |
| 412 | // lists to work correctly. |
| 413 | // |
| 414 | // Instead, the right callback should be retrieved using GetBufferedCallback(). |
| 415 | // |
| 416 | // To catch such errors, we validate that the provided concrete path never indicates a raw list item operation (which the |
| 417 | // buffered reader will handle and convert for us). |
| 418 | // |
| 419 | // |
| 420 | VerifyOrDie(!aPath.IsListItemOperation()); |
| 421 | |
Kundok Park | 035911e | 2022-03-23 22:16:54 -0700 | [diff] [blame] | 422 | // Copy the reader for forwarding |
| 423 | TLV::TLVReader dataSnapshot; |
| 424 | if (apData) |
| 425 | { |
| 426 | dataSnapshot.Init(*apData); |
| 427 | } |
| 428 | |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 429 | UpdateCache(aPath, apData, aStatus); |
| 430 | |
| 431 | // |
| 432 | // Forward the call through. |
| 433 | // |
Kundok Park | 035911e | 2022-03-23 22:16:54 -0700 | [diff] [blame] | 434 | mCallback.OnAttributeData(aPath, apData ? &dataSnapshot : nullptr, aStatus); |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 437 | template <bool CanEnableDataCaching> |
| 438 | CHIP_ERROR ClusterStateCacheT<CanEnableDataCaching>::GetVersion(const ConcreteClusterPath & aPath, |
| 439 | Optional<DataVersion> & aVersion) const |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 440 | { |
yunhanw-google | 3ef81a8 | 2022-05-19 10:46:23 -0700 | [diff] [blame] | 441 | VerifyOrReturnError(aPath.IsValidConcreteClusterPath(), CHIP_ERROR_INVALID_ARGUMENT); |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 442 | CHIP_ERROR err; |
yunhanw-google | 3ef81a8 | 2022-05-19 10:46:23 -0700 | [diff] [blame] | 443 | auto clusterState = GetClusterState(aPath.mEndpointId, aPath.mClusterId, err); |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 444 | ReturnErrorOnFailure(err); |
| 445 | aVersion = clusterState->mCommittedDataVersion; |
| 446 | return CHIP_NO_ERROR; |
| 447 | } |
| 448 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 449 | template <bool CanEnableDataCaching> |
| 450 | void ClusterStateCacheT<CanEnableDataCaching>::OnEventData(const EventHeader & aEventHeader, TLV::TLVReader * apData, |
| 451 | const StatusIB * apStatus) |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 452 | { |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 453 | VerifyOrDie(apData != nullptr || apStatus != nullptr); |
| 454 | |
| 455 | TLV::TLVReader dataSnapshot; |
| 456 | if (apData) |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 457 | { |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 458 | dataSnapshot.Init(*apData); |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 461 | UpdateEventCache(aEventHeader, apData, apStatus); |
| 462 | mCallback.OnEventData(aEventHeader, apData ? &dataSnapshot : nullptr, apStatus); |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 465 | template <> |
| 466 | CHIP_ERROR ClusterStateCacheT<true>::GetStatus(const ConcreteAttributePath & path, StatusIB & status) const |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 467 | { |
| 468 | CHIP_ERROR err; |
| 469 | |
| 470 | auto attributeState = GetAttributeState(path.mEndpointId, path.mClusterId, path.mAttributeId, err); |
| 471 | ReturnErrorOnFailure(err); |
| 472 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 473 | if (!attributeState->template Is<StatusIB>()) |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 474 | { |
| 475 | return CHIP_ERROR_INVALID_ARGUMENT; |
| 476 | } |
| 477 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 478 | status = attributeState->template Get<StatusIB>(); |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 479 | return CHIP_NO_ERROR; |
| 480 | } |
| 481 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 482 | template <> |
| 483 | CHIP_ERROR ClusterStateCacheT<false>::GetStatus(const ConcreteAttributePath & path, StatusIB & status) const |
| 484 | { |
| 485 | return CHIP_ERROR_INVALID_ARGUMENT; |
| 486 | } |
| 487 | |
| 488 | template <bool CanEnableDataCaching> |
| 489 | CHIP_ERROR ClusterStateCacheT<CanEnableDataCaching>::GetStatus(const ConcreteEventPath & path, StatusIB & status) const |
Jerry Johns | 964adbc | 2022-04-14 16:42:04 -0700 | [diff] [blame] | 490 | { |
| 491 | auto statusIter = mEventStatusCache.find(path); |
| 492 | if (statusIter == mEventStatusCache.end()) |
| 493 | { |
| 494 | return CHIP_ERROR_KEY_NOT_FOUND; |
| 495 | } |
| 496 | |
| 497 | status = statusIter->second; |
| 498 | return CHIP_NO_ERROR; |
| 499 | } |
| 500 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 501 | template <bool CanEnableDataCaching> |
| 502 | void ClusterStateCacheT<CanEnableDataCaching>::GetSortedFilters(std::vector<std::pair<DataVersionFilter, size_t>> & aVector) const |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 503 | { |
| 504 | for (auto const & endpointIter : mCache) |
| 505 | { |
| 506 | EndpointId endpointId = endpointIter.first; |
| 507 | for (auto const & clusterIter : endpointIter.second) |
| 508 | { |
| 509 | if (!clusterIter.second.mCommittedDataVersion.HasValue()) |
| 510 | { |
| 511 | continue; |
| 512 | } |
| 513 | DataVersion dataVersion = clusterIter.second.mCommittedDataVersion.Value(); |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 514 | size_t clusterSize = 0; |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 515 | ClusterId clusterId = clusterIter.first; |
| 516 | |
| 517 | for (auto const & attributeIter : clusterIter.second.mAttributes) |
| 518 | { |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 519 | if constexpr (CanEnableDataCaching) |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 520 | { |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 521 | if (attributeIter.second.template Is<StatusIB>()) |
| 522 | { |
| 523 | clusterSize += SizeOfStatusIB(attributeIter.second.template Get<StatusIB>()); |
| 524 | } |
| 525 | else if (attributeIter.second.template Is<uint32_t>()) |
| 526 | { |
| 527 | clusterSize += attributeIter.second.template Get<uint32_t>(); |
| 528 | } |
| 529 | else |
| 530 | { |
| 531 | VerifyOrDie(attributeIter.second.template Is<AttributeData>()); |
| 532 | TLV::TLVReader bufReader; |
| 533 | bufReader.Init(attributeIter.second.template Get<AttributeData>().Get(), |
| 534 | attributeIter.second.template Get<AttributeData>().AllocatedSize()); |
| 535 | ReturnOnFailure(bufReader.Next()); |
| 536 | // Skip to the end of the element. |
| 537 | ReturnOnFailure(bufReader.Skip()); |
| 538 | |
| 539 | // Compute the amount of value data |
| 540 | clusterSize += bufReader.GetLengthRead(); |
| 541 | } |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 542 | } |
| 543 | else |
| 544 | { |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 545 | clusterSize += attributeIter.second; |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 548 | |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 549 | if (clusterSize == 0) |
| 550 | { |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 551 | // No data in this cluster, so no point in sending a dataVersion |
| 552 | // along at all. |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 553 | continue; |
| 554 | } |
| 555 | |
| 556 | DataVersionFilter filter(endpointId, clusterId, dataVersion); |
| 557 | |
| 558 | aVector.push_back(std::make_pair(filter, clusterSize)); |
| 559 | } |
| 560 | } |
Boris Zbarsky | 343634a | 2023-04-24 13:37:18 -0400 | [diff] [blame] | 561 | |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 562 | std::sort(aVector.begin(), aVector.end(), |
| 563 | [](const std::pair<DataVersionFilter, size_t> & x, const std::pair<DataVersionFilter, size_t> & y) { |
| 564 | return x.second > y.second; |
| 565 | }); |
| 566 | } |
| 567 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 568 | template <bool CanEnableDataCaching> |
| 569 | CHIP_ERROR ClusterStateCacheT<CanEnableDataCaching>::OnUpdateDataVersionFilterList( |
| 570 | DataVersionFilterIBs::Builder & aDataVersionFilterIBsBuilder, const Span<AttributePathParams> & aAttributePaths, |
| 571 | bool & aEncodedDataVersionList) |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 572 | { |
| 573 | CHIP_ERROR err = CHIP_NO_ERROR; |
| 574 | TLV::TLVWriter backup; |
| 575 | |
yunhanw-google | 9490a5f | 2022-05-20 09:44:23 -0700 | [diff] [blame] | 576 | // Only put paths into mRequestPathSet if they cover clusters in their entirety and no other path in our path list |
| 577 | // points to a specific attribute from any of those clusters. |
| 578 | // this would help for data-out-of-sync issue when handling store data version for the particular case on two paths: (E1, C1, |
| 579 | // wildcard), (wildcard, C1, A1) |
| 580 | for (auto & attribute1 : aAttributePaths) |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 581 | { |
yunhanw-google | 9490a5f | 2022-05-20 09:44:23 -0700 | [diff] [blame] | 582 | if (attribute1.HasWildcardAttributeId()) |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 583 | { |
yunhanw-google | 9490a5f | 2022-05-20 09:44:23 -0700 | [diff] [blame] | 584 | bool intersected = false; |
| 585 | for (auto & attribute2 : aAttributePaths) |
| 586 | { |
| 587 | if (attribute2.HasWildcardAttributeId()) |
| 588 | { |
| 589 | continue; |
| 590 | } |
| 591 | |
| 592 | if (attribute1.Intersects(attribute2)) |
| 593 | { |
| 594 | intersected = true; |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | if (!intersected) |
| 600 | { |
| 601 | mRequestPathSet.insert(attribute1); |
| 602 | } |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
| 606 | std::vector<std::pair<DataVersionFilter, size_t>> filterVector; |
| 607 | GetSortedFilters(filterVector); |
| 608 | |
| 609 | aEncodedDataVersionList = false; |
| 610 | for (auto & filter : filterVector) |
| 611 | { |
| 612 | bool intersected = false; |
| 613 | aDataVersionFilterIBsBuilder.Checkpoint(backup); |
| 614 | |
| 615 | // if the particular cached cluster does not intersect with user provided attribute paths, skip the cached one |
| 616 | for (const auto & attributePath : aAttributePaths) |
| 617 | { |
| 618 | if (attributePath.IncludesAttributesInCluster(filter.first)) |
| 619 | { |
| 620 | intersected = true; |
| 621 | break; |
| 622 | } |
| 623 | } |
| 624 | if (!intersected) |
| 625 | { |
| 626 | continue; |
| 627 | } |
| 628 | |
Boris Zbarsky | e5b79ae | 2024-09-30 18:19:36 -0400 | [diff] [blame^] | 629 | SuccessOrExit(err = aDataVersionFilterIBsBuilder.EncodeDataVersionFilterIB(filter.first)); |
yunhanw-google | 57a2df3 | 2022-04-14 11:04:47 -0700 | [diff] [blame] | 630 | aEncodedDataVersionList = true; |
| 631 | } |
| 632 | |
| 633 | exit: |
| 634 | if (err == CHIP_ERROR_NO_MEMORY || err == CHIP_ERROR_BUFFER_TOO_SMALL) |
| 635 | { |
| 636 | ChipLogProgress(DataManagement, "OnUpdateDataVersionFilterList out of space; rolling back"); |
| 637 | aDataVersionFilterIBsBuilder.Rollback(backup); |
| 638 | err = CHIP_NO_ERROR; |
| 639 | } |
| 640 | return err; |
| 641 | } |
| 642 | |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 643 | template <bool CanEnableDataCaching> |
Boris Zbarsky | fe4764a | 2024-07-05 14:08:02 -0400 | [diff] [blame] | 644 | void ClusterStateCacheT<CanEnableDataCaching>::ClearAttributes(EndpointId endpointId) |
| 645 | { |
| 646 | mCache.erase(endpointId); |
| 647 | } |
| 648 | |
| 649 | template <bool CanEnableDataCaching> |
| 650 | void ClusterStateCacheT<CanEnableDataCaching>::ClearAttributes(const ConcreteClusterPath & cluster) |
| 651 | { |
| 652 | // Can't use GetEndpointState here, since that only handles const things. |
| 653 | auto endpointIter = mCache.find(cluster.mEndpointId); |
| 654 | if (endpointIter == mCache.end()) |
| 655 | { |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | auto & endpointState = endpointIter->second; |
| 660 | endpointState.erase(cluster.mClusterId); |
| 661 | } |
| 662 | |
| 663 | template <bool CanEnableDataCaching> |
| 664 | void ClusterStateCacheT<CanEnableDataCaching>::ClearAttribute(const ConcreteAttributePath & attribute) |
| 665 | { |
| 666 | // Can't use GetClusterState here, since that only handles const things. |
| 667 | auto endpointIter = mCache.find(attribute.mEndpointId); |
| 668 | if (endpointIter == mCache.end()) |
| 669 | { |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | auto & endpointState = endpointIter->second; |
| 674 | auto clusterIter = endpointState.find(attribute.mClusterId); |
| 675 | if (clusterIter == endpointState.end()) |
| 676 | { |
| 677 | return; |
| 678 | } |
| 679 | |
| 680 | auto & clusterState = clusterIter->second; |
| 681 | clusterState.mAttributes.erase(attribute.mAttributeId); |
| 682 | } |
| 683 | |
| 684 | template <bool CanEnableDataCaching> |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 685 | CHIP_ERROR ClusterStateCacheT<CanEnableDataCaching>::GetLastReportDataPath(ConcreteClusterPath & aPath) |
yunhanw-google | f2cd15f | 2022-09-26 16:25:02 -0700 | [diff] [blame] | 686 | { |
yunhanw-google | c1bfd9d | 2022-11-17 12:42:14 -0800 | [diff] [blame] | 687 | if (mLastReportDataPath.IsValidConcreteClusterPath()) |
| 688 | { |
| 689 | aPath = mLastReportDataPath; |
| 690 | return CHIP_NO_ERROR; |
| 691 | } |
| 692 | return CHIP_ERROR_INCORRECT_STATE; |
yunhanw-google | f2cd15f | 2022-09-26 16:25:02 -0700 | [diff] [blame] | 693 | } |
Boris Zbarsky | 2fcac2e | 2024-04-19 16:20:14 -0400 | [diff] [blame] | 694 | |
| 695 | // Ensure that our out-of-line template methods actually get compiled. |
| 696 | template class ClusterStateCacheT<true>; |
| 697 | template class ClusterStateCacheT<false>; |
| 698 | |
Jerry Johns | c8027a5 | 2021-12-13 17:20:15 -0800 | [diff] [blame] | 699 | } // namespace app |
| 700 | } // namespace chip |