Line data Source code
1 : /*
2 : * Copyright (c) 2024 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/CodegenDataModelProvider.h>
18 :
19 : #include <access/AccessControl.h>
20 : #include <app-common/zap-generated/attribute-type.h>
21 : #include <app/AttributeAccessInterface.h>
22 : #include <app/AttributeAccessInterfaceRegistry.h>
23 : #include <app/GlobalAttributes.h>
24 : #include <app/RequiredPrivilege.h>
25 : #include <app/data-model-provider/ProviderChangeListener.h>
26 : #include <app/data-model/FabricScoped.h>
27 : #include <app/reporting/reporting.h>
28 : #include <app/util/af-types.h>
29 : #include <app/util/attribute-metadata.h>
30 : #include <app/util/attribute-storage-detail.h>
31 : #include <app/util/attribute-storage-null-handling.h>
32 : #include <app/util/attribute-storage.h>
33 : #include <app/util/attribute-table-detail.h>
34 : #include <app/util/attribute-table.h>
35 : #include <app/util/ember-io-storage.h>
36 : #include <app/util/ember-strings.h>
37 : #include <app/util/odd-sized-integers.h>
38 : #include <data-model-providers/codegen/EmberAttributeDataBuffer.h>
39 : #include <lib/core/CHIPError.h>
40 : #include <lib/support/CodeUtils.h>
41 :
42 : #include <zap-generated/endpoint_config.h>
43 :
44 : namespace chip {
45 : namespace app {
46 : namespace {
47 :
48 : using namespace chip::app::Compatibility::Internal;
49 : using Protocols::InteractionModel::Status;
50 :
51 : class ContextAttributesChangeListener : public AttributesChangedListener
52 : {
53 : public:
54 5301 : ContextAttributesChangeListener(DataModel::ProviderChangeListener & listener) : mListener(listener) {}
55 5261 : void MarkDirty(const AttributePathParams & path) override { mListener.MarkDirty(path); }
56 :
57 : private:
58 : DataModel::ProviderChangeListener & mListener;
59 : };
60 :
61 : /// Attempts to write via an attribute access interface (AAI)
62 : ///
63 : /// If it returns a CHIP_ERROR, then this is a FINAL result (i.e. either failure or success)
64 : ///
65 : /// If it returns std::nullopt, then there is no AAI to handle the given path
66 : /// and processing should figure out the value otherwise (generally from other ember data)
67 5301 : std::optional<CHIP_ERROR> TryWriteViaAccessInterface(const ConcreteDataAttributePath & path, AttributeAccessInterface * aai,
68 : AttributeValueDecoder & decoder)
69 : {
70 : // Processing can happen only if an attribute access interface actually exists..
71 5301 : if (aai == nullptr)
72 : {
73 137 : return std::nullopt;
74 : }
75 :
76 5164 : CHIP_ERROR err = aai->Write(path, decoder);
77 :
78 5164 : if (err != CHIP_NO_ERROR)
79 : {
80 13 : return std::make_optional(err);
81 : }
82 :
83 : // If the decoder tried to decode, then a value should have been read for processing.
84 : // - if decoding was done, assume DONE (i.e. final CHIP_NO_ERROR)
85 : // - otherwise, if no decoding done, return that processing must continue via nullopt
86 10302 : return decoder.TriedDecode() ? std::make_optional(CHIP_NO_ERROR) : std::nullopt;
87 : }
88 :
89 : } // namespace
90 :
91 5305 : DataModel::ActionReturnStatus CodegenDataModelProvider::WriteAttribute(const DataModel::WriteAttributeRequest & request,
92 : AttributeValueDecoder & decoder)
93 : {
94 5305 : if (auto * cluster = mRegistry.Get(request.path); cluster != nullptr)
95 : {
96 3 : return cluster->WriteAttribute(request, decoder);
97 : }
98 :
99 : // we must be started up to accept writes (we make use of the context below)
100 5302 : VerifyOrReturnError(mContext.has_value(), CHIP_ERROR_INCORRECT_STATE);
101 :
102 : const EmberAfAttributeMetadata * attributeMetadata =
103 5302 : emberAfLocateAttributeMetadata(request.path.mEndpointId, request.path.mClusterId, request.path.mAttributeId);
104 :
105 : // WriteAttribute requirement is that request.path is a VALID path inside the provider
106 : // metadata tree. Clients are supposed to validate this (and data version and other flags)
107 : // This SHOULD NEVER HAPPEN hence the general return code (seemed preferable to VerifyOrDie)
108 5302 : VerifyOrReturnError(attributeMetadata != nullptr, Status::Failure);
109 :
110 5302 : if (request.path.mDataVersion.HasValue())
111 : {
112 2 : DataVersion * versionPtr = emberAfDataVersionStorage(request.path);
113 :
114 2 : if (versionPtr == nullptr)
115 : {
116 0 : ChipLogError(DataManagement, "Unable to get cluster info for Endpoint 0x%x, Cluster " ChipLogFormatMEI,
117 : request.path.mEndpointId, ChipLogValueMEI(request.path.mClusterId));
118 0 : return Status::DataVersionMismatch;
119 : }
120 :
121 2 : if (request.path.mDataVersion.Value() != *versionPtr)
122 : {
123 1 : ChipLogError(DataManagement, "Write Version mismatch for Endpoint 0x%x, Cluster " ChipLogFormatMEI,
124 : request.path.mEndpointId, ChipLogValueMEI(request.path.mClusterId));
125 1 : return Status::DataVersionMismatch;
126 : }
127 : }
128 :
129 5301 : ContextAttributesChangeListener change_listener(mContext->dataModelChangeListener);
130 :
131 : AttributeAccessInterface * aai =
132 5301 : AttributeAccessInterfaceRegistry::Instance().Get(request.path.mEndpointId, request.path.mClusterId);
133 5301 : std::optional<CHIP_ERROR> aai_result = TryWriteViaAccessInterface(request.path, aai, decoder);
134 5301 : if (aai_result.has_value())
135 : {
136 5160 : if (*aai_result == CHIP_NO_ERROR)
137 : {
138 : // TODO: this is awkward since it provides AAI no control over this, specifically
139 : // AAI may not want to increase versions for some attributes that are Q
140 5147 : emberAfAttributeChanged(request.path.mEndpointId, request.path.mClusterId, request.path.mAttributeId, &change_listener);
141 : }
142 5160 : return *aai_result;
143 : }
144 :
145 141 : MutableByteSpan dataBuffer = gEmberAttributeIOBufferSpan;
146 : {
147 141 : Ember::EmberAttributeDataBuffer emberData(attributeMetadata, dataBuffer);
148 141 : ReturnErrorOnFailure(decoder.Decode(emberData));
149 : }
150 :
151 : Protocols::InteractionModel::Status status;
152 :
153 117 : if (dataBuffer.size() > attributeMetadata->size)
154 : {
155 1 : ChipLogDetail(Zcl, "Data to write exceeds the attribute size claimed.");
156 1 : return Status::InvalidValue;
157 : }
158 :
159 116 : EmberAfWriteDataInput dataInput(dataBuffer.data(), attributeMetadata->attributeType);
160 :
161 116 : dataInput.SetChangeListener(&change_listener);
162 : // TODO: dataInput.SetMarkDirty() should be according to `ChangesOmmited`
163 :
164 116 : if (request.operationFlags.Has(DataModel::OperationFlags::kInternal))
165 : {
166 : // Internal requests use the non-External interface that has less enforcement
167 : // than the external version (e.g. does not check/enforce writable settings, does not
168 : // validate attribute types) - see attribute-table.h documentation for details.
169 0 : status = emberAfWriteAttribute(request.path, dataInput);
170 : }
171 : else
172 : {
173 116 : status = emAfWriteAttributeExternal(request.path, dataInput);
174 : }
175 :
176 116 : if (status != Protocols::InteractionModel::Status::Success)
177 : {
178 2 : return status;
179 : }
180 :
181 114 : return CHIP_NO_ERROR;
182 5301 : }
183 :
184 1879 : void CodegenDataModelProvider::ListAttributeWriteNotification(const ConcreteAttributePath & aPath,
185 : DataModel::ListWriteOperation opType)
186 : {
187 1879 : if (auto * cluster = mRegistry.Get(aPath); cluster != nullptr)
188 : {
189 0 : cluster->ListAttributeWriteNotification(aPath, opType);
190 0 : return;
191 : }
192 :
193 1879 : AttributeAccessInterface * aai = AttributeAccessInterfaceRegistry::Instance().Get(aPath.mEndpointId, aPath.mClusterId);
194 :
195 1879 : if (aai != nullptr)
196 : {
197 1874 : switch (opType)
198 : {
199 937 : case DataModel::ListWriteOperation::kListWriteBegin:
200 937 : aai->OnListWriteBegin(aPath);
201 937 : break;
202 12 : case DataModel::ListWriteOperation::kListWriteFailure:
203 12 : aai->OnListWriteEnd(aPath, false);
204 12 : break;
205 925 : case DataModel::ListWriteOperation::kListWriteSuccess:
206 925 : aai->OnListWriteEnd(aPath, true);
207 925 : break;
208 : }
209 : }
210 : }
211 :
212 0 : void CodegenDataModelProvider::Temporary_ReportAttributeChanged(const AttributePathParams & path)
213 : {
214 : // we must be started up to process changes since we use the context
215 0 : VerifyOrReturn(mContext.has_value());
216 :
217 0 : ContextAttributesChangeListener change_listener(mContext->dataModelChangeListener);
218 0 : if (path.mClusterId != kInvalidClusterId)
219 : {
220 0 : emberAfAttributeChanged(path.mEndpointId, path.mClusterId, path.mAttributeId, &change_listener);
221 : }
222 : else
223 : {
224 : // When the path has wildcard cluster Id, call the emberAfEndpointChanged to mark attributes on the given endpoint
225 : // as having changing, but do NOT increase/alter any cluster data versions, as this happens when a bridged endpoint is
226 : // added or removed from a bridge and the cluster data is not changed during the process.
227 0 : emberAfEndpointChanged(path.mEndpointId, &change_listener);
228 : }
229 0 : }
230 :
231 : } // namespace app
232 : } // namespace chip
|