|
| 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" |
| 20 | +#include <app/AttributeCache.h> |
| 21 | +#include <app/InteractionModelEngine.h> |
| 22 | +#include <tuple> |
| 23 | + |
| 24 | +namespace chip { |
| 25 | +namespace app { |
| 26 | + |
| 27 | +CHIP_ERROR AttributeCache::UpdateCache(const ConcreteDataAttributePath & aPath, TLV::TLVReader * apData, const StatusIB & aStatus) |
| 28 | +{ |
| 29 | + AttributeState state; |
| 30 | + System::PacketBufferHandle handle; |
| 31 | + System::PacketBufferTLVWriter writer; |
| 32 | + |
| 33 | + if (apData) |
| 34 | + { |
| 35 | + handle = System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes); |
| 36 | + |
| 37 | + writer.Init(std::move(handle), false); |
| 38 | + |
| 39 | + ReturnErrorOnFailure(writer.CopyElement(TLV::AnonymousTag, *apData)); |
| 40 | + ReturnErrorOnFailure(writer.Finalize(&handle)); |
| 41 | + |
| 42 | + // |
| 43 | + // Compact the buffer down to a more reasonably sized packet buffer |
| 44 | + // if we can. |
| 45 | + // |
| 46 | + handle.RightSize(); |
| 47 | + |
| 48 | + state.Set<System::PacketBufferHandle>(std::move(handle)); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + state.Set<StatusIB>(aStatus); |
| 53 | + } |
| 54 | + |
| 55 | + // |
| 56 | + // if the endpoint didn't exist previously, let's track the insertion |
| 57 | + // so that we can inform our callback of a new endpoint being added appropriately. |
| 58 | + // |
| 59 | + if (mCache.find(aPath.mEndpointId) == mCache.end()) |
| 60 | + { |
| 61 | + mAddedEndpoints.push_back(aPath.mEndpointId); |
| 62 | + } |
| 63 | + |
| 64 | + mCache[aPath.mEndpointId][aPath.mClusterId][aPath.mAttributeId] = std::move(state); |
| 65 | + mChangedAttributeSet.insert(aPath); |
| 66 | + return CHIP_NO_ERROR; |
| 67 | +} |
| 68 | + |
| 69 | +void AttributeCache::OnReportBegin(const ReadClient * apReadClient) |
| 70 | +{ |
| 71 | + mChangedAttributeSet.clear(); |
| 72 | + mAddedEndpoints.clear(); |
| 73 | + mCallback.OnReportBegin(apReadClient); |
| 74 | +} |
| 75 | + |
| 76 | +void AttributeCache::OnReportEnd(const ReadClient * apReadClient) |
| 77 | +{ |
| 78 | + std::set<std::tuple<EndpointId, ClusterId>> changedClusters; |
| 79 | + |
| 80 | + // |
| 81 | + // Add the EndpointId and ClusterId into a set so that we only |
| 82 | + // convey unique combinations in the subsequent OnClusterChanged callback. |
| 83 | + // |
| 84 | + for (auto & path : mChangedAttributeSet) |
| 85 | + { |
| 86 | + mCallback.OnAttributeChanged(this, path); |
| 87 | + changedClusters.insert(std::make_tuple(path.mEndpointId, path.mClusterId)); |
| 88 | + } |
| 89 | + |
| 90 | + for (auto & item : changedClusters) |
| 91 | + { |
| 92 | + mCallback.OnClusterChanged(this, std::get<0>(item), std::get<1>(item)); |
| 93 | + } |
| 94 | + |
| 95 | + for (auto endpoint : mAddedEndpoints) |
| 96 | + { |
| 97 | + mCallback.OnEndpointAdded(this, endpoint); |
| 98 | + } |
| 99 | + |
| 100 | + mCallback.OnReportEnd(apReadClient); |
| 101 | +} |
| 102 | + |
| 103 | +void AttributeCache::OnAttributeData(const ReadClient * apReadClient, const ConcreteDataAttributePath & aPath, |
| 104 | + TLV::TLVReader * apData, const StatusIB & aStatus) |
| 105 | +{ |
| 106 | + // |
| 107 | + // Since the cache itself is a ReadClient::Callback, it may be incorrectly passed in directly when registering with the |
| 108 | + // ReadClient. This should be avoided, since that bypasses the built-in buffered reader adapter callback that is needed for |
| 109 | + // lists to work correctly. |
| 110 | + // |
| 111 | + // Instead, the right callback should be retrieved using GetBufferedCallback(). |
| 112 | + // |
| 113 | + // To catch such errors, we validate that the provided concrete path never indicates a raw list item operation (which the |
| 114 | + // buffered reader will handle and convert for us). |
| 115 | + // |
| 116 | + // |
| 117 | + VerifyOrDie(!aPath.IsListItemOperation()); |
| 118 | + |
| 119 | + UpdateCache(aPath, apData, aStatus); |
| 120 | + |
| 121 | + // |
| 122 | + // Forward the call through. |
| 123 | + // |
| 124 | + mCallback.OnAttributeData(apReadClient, aPath, apData, aStatus); |
| 125 | +} |
| 126 | + |
| 127 | +CHIP_ERROR AttributeCache::Get(const ConcreteAttributePath & path, TLV::TLVReader & reader) |
| 128 | +{ |
| 129 | + CHIP_ERROR err; |
| 130 | + |
| 131 | + auto attributeState = GetAttributeState(path.mEndpointId, path.mClusterId, path.mAttributeId, err); |
| 132 | + ReturnErrorOnFailure(err); |
| 133 | + |
| 134 | + if (attributeState->Is<StatusIB>()) |
| 135 | + { |
| 136 | + return CHIP_ERROR_IM_STATUS_CODE_RECEIVED; |
| 137 | + } |
| 138 | + |
| 139 | + System::PacketBufferTLVReader bufReader; |
| 140 | + |
| 141 | + bufReader.Init(attributeState->Get<System::PacketBufferHandle>().Retain()); |
| 142 | + ReturnErrorOnFailure(bufReader.Next()); |
| 143 | + |
| 144 | + reader.Init(bufReader); |
| 145 | + return CHIP_NO_ERROR; |
| 146 | +} |
| 147 | + |
| 148 | +AttributeCache::EndpointState * AttributeCache::GetEndpointState(EndpointId endpointId, CHIP_ERROR & err) |
| 149 | +{ |
| 150 | + auto endpointIter = mCache.find(endpointId); |
| 151 | + if (endpointIter == mCache.end()) |
| 152 | + { |
| 153 | + err = CHIP_ERROR_KEY_NOT_FOUND; |
| 154 | + return nullptr; |
| 155 | + } |
| 156 | + |
| 157 | + err = CHIP_NO_ERROR; |
| 158 | + return &endpointIter->second; |
| 159 | +} |
| 160 | + |
| 161 | +AttributeCache::ClusterState * AttributeCache::GetClusterState(EndpointId endpointId, ClusterId clusterId, CHIP_ERROR & err) |
| 162 | +{ |
| 163 | + auto endpointState = GetEndpointState(endpointId, err); |
| 164 | + if (err != CHIP_NO_ERROR) |
| 165 | + { |
| 166 | + return nullptr; |
| 167 | + } |
| 168 | + |
| 169 | + auto clusterState = endpointState->find(clusterId); |
| 170 | + if (clusterState == endpointState->end()) |
| 171 | + { |
| 172 | + err = CHIP_ERROR_KEY_NOT_FOUND; |
| 173 | + return nullptr; |
| 174 | + } |
| 175 | + |
| 176 | + err = CHIP_NO_ERROR; |
| 177 | + return &clusterState->second; |
| 178 | +} |
| 179 | + |
| 180 | +AttributeCache::AttributeState * AttributeCache::GetAttributeState(EndpointId endpointId, ClusterId clusterId, |
| 181 | + AttributeId attributeId, CHIP_ERROR & err) |
| 182 | +{ |
| 183 | + auto clusterState = GetClusterState(endpointId, clusterId, err); |
| 184 | + if (err != CHIP_NO_ERROR) |
| 185 | + { |
| 186 | + return nullptr; |
| 187 | + } |
| 188 | + |
| 189 | + auto attributeState = clusterState->find(attributeId); |
| 190 | + if (attributeState == clusterState->end()) |
| 191 | + { |
| 192 | + err = CHIP_ERROR_KEY_NOT_FOUND; |
| 193 | + return nullptr; |
| 194 | + } |
| 195 | + |
| 196 | + err = CHIP_NO_ERROR; |
| 197 | + return &attributeState->second; |
| 198 | +} |
| 199 | + |
| 200 | +CHIP_ERROR AttributeCache::GetStatus(const ConcreteAttributePath & path, StatusIB & status) |
| 201 | +{ |
| 202 | + CHIP_ERROR err; |
| 203 | + |
| 204 | + auto attributeState = GetAttributeState(path.mEndpointId, path.mClusterId, path.mAttributeId, err); |
| 205 | + ReturnErrorOnFailure(err); |
| 206 | + |
| 207 | + if (!attributeState->Is<StatusIB>()) |
| 208 | + { |
| 209 | + return CHIP_ERROR_INVALID_ARGUMENT; |
| 210 | + } |
| 211 | + |
| 212 | + status = attributeState->Get<StatusIB>(); |
| 213 | + return CHIP_NO_ERROR; |
| 214 | +} |
| 215 | + |
| 216 | +} // namespace app |
| 217 | +} // namespace chip |
0 commit comments