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 5306 : ContextAttributesChangeListener(DataModel::ProviderChangeListener & listener) : mListener(listener) {}
55 5260 : 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 5302 : 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 5302 : if (aai == nullptr)
72 : {
73 137 : return std::nullopt;
74 : }
75 :
76 5165 : CHIP_ERROR err = aai->Write(path, decoder);
77 :
78 5165 : if (err != CHIP_NO_ERROR)
79 : {
80 14 : 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 5306 : DataModel::ActionReturnStatus CodegenDataModelProvider::WriteAttribute(const DataModel::WriteAttributeRequest & request,
92 : AttributeValueDecoder & decoder)
93 : {
94 : // we must be started up to accept writes (we make use of the context below)
95 5306 : VerifyOrReturnError(mContext.has_value(), CHIP_ERROR_INCORRECT_STATE);
96 :
97 : // Codegen logic specific: we accept AAI writes BEFORE server cluster interface, so that we are backwards compatible
98 : // in case some application installed AAI before Server Cluster Interfaces were supported
99 5306 : ContextAttributesChangeListener change_listener(mContext->dataModelChangeListener);
100 :
101 : const EmberAfAttributeMetadata * attributeMetadata =
102 5306 : emberAfLocateAttributeMetadata(request.path.mEndpointId, request.path.mClusterId, request.path.mAttributeId);
103 :
104 5306 : if (attributeMetadata != nullptr)
105 : {
106 : // AAI is only allowed on ember-attributes
107 : AttributeAccessInterface * aai =
108 5302 : AttributeAccessInterfaceRegistry::Instance().Get(request.path.mEndpointId, request.path.mClusterId);
109 5302 : std::optional<CHIP_ERROR> aai_result = TryWriteViaAccessInterface(request.path, aai, decoder);
110 5302 : if (aai_result.has_value())
111 : {
112 5161 : if (*aai_result == CHIP_NO_ERROR)
113 : {
114 : // TODO: this is awkward since it provides AAI no control over this, specifically
115 : // AAI may not want to increase versions for some attributes that are Q
116 5147 : emberAfAttributeChanged(request.path.mEndpointId, request.path.mClusterId, request.path.mAttributeId,
117 : &change_listener);
118 : }
119 5161 : return *aai_result;
120 : }
121 : }
122 :
123 : // If ServerClusterInterface is available, it provides the final answer
124 145 : if (auto * cluster = mRegistry.Get(request.path); cluster != nullptr)
125 : {
126 5 : return cluster->WriteAttribute(request, decoder);
127 : }
128 :
129 : // WriteAttribute requirement is that request.path is a VALID path inside the provider
130 : // metadata tree. Clients are supposed to validate this (and data version and other flags)
131 : // This SHOULD NEVER HAPPEN hence the general return code (seemed preferable to VerifyOrDie)
132 140 : VerifyOrReturnError(attributeMetadata != nullptr, Status::Failure);
133 :
134 140 : MutableByteSpan dataBuffer = gEmberAttributeIOBufferSpan;
135 : {
136 140 : Ember::EmberAttributeDataBuffer emberData(attributeMetadata, dataBuffer);
137 140 : ReturnErrorOnFailure(decoder.Decode(emberData));
138 : }
139 :
140 : Protocols::InteractionModel::Status status;
141 :
142 116 : if (dataBuffer.size() > attributeMetadata->size)
143 : {
144 1 : ChipLogDetail(Zcl, "Data to write exceeds the attribute size claimed.");
145 1 : return Status::InvalidValue;
146 : }
147 :
148 115 : EmberAfWriteDataInput dataInput(dataBuffer.data(), attributeMetadata->attributeType);
149 :
150 115 : dataInput.SetChangeListener(&change_listener);
151 : // TODO: dataInput.SetMarkDirty() should be according to `ChangesOmmited`
152 :
153 115 : if (request.operationFlags.Has(DataModel::OperationFlags::kInternal))
154 : {
155 : // Internal requests use the non-External interface that has less enforcement
156 : // than the external version (e.g. does not check/enforce writable settings, does not
157 : // validate attribute types) - see attribute-table.h documentation for details.
158 0 : status = emberAfWriteAttribute(request.path, dataInput);
159 : }
160 : else
161 : {
162 115 : status = emAfWriteAttributeExternal(request.path, dataInput);
163 : }
164 :
165 115 : if (status != Protocols::InteractionModel::Status::Success)
166 : {
167 2 : return status;
168 : }
169 :
170 113 : return CHIP_NO_ERROR;
171 5306 : }
172 :
173 1879 : void CodegenDataModelProvider::ListAttributeWriteNotification(const ConcreteAttributePath & aPath,
174 : DataModel::ListWriteOperation opType)
175 : {
176 :
177 : // NOTE: for backwards compatibility, we process AAI logic BEFORE Server Cluster Interface
178 : // so that AttributeAccessInterface logic works if one was installed before Server Cluster Interface
179 : // support was introduced in the SDK.
180 1879 : AttributeAccessInterface * aai = AttributeAccessInterfaceRegistry::Instance().Get(aPath.mEndpointId, aPath.mClusterId);
181 1879 : if (aai != nullptr)
182 : {
183 1874 : switch (opType)
184 : {
185 937 : case DataModel::ListWriteOperation::kListWriteBegin:
186 937 : aai->OnListWriteBegin(aPath);
187 937 : break;
188 12 : case DataModel::ListWriteOperation::kListWriteFailure:
189 12 : aai->OnListWriteEnd(aPath, false);
190 12 : break;
191 925 : case DataModel::ListWriteOperation::kListWriteSuccess:
192 925 : aai->OnListWriteEnd(aPath, true);
193 925 : break;
194 : }
195 :
196 : // We fall through here and will notify any ServerClusterInterface as well.
197 : // This is NOT ideal because AAI may or may not fully intercept the write,
198 : // So we do not know which of the ::Write behavior AAI uses:
199 : // - write succeeds (so SCI should not be notified)
200 : // - AAI falls-through (so SCI should process the request)
201 : //
202 : // for now we err on the side of notifying both.
203 : }
204 :
205 1879 : if (auto * cluster = mRegistry.Get(aPath); cluster != nullptr)
206 : {
207 0 : cluster->ListAttributeWriteNotification(aPath, opType);
208 0 : return;
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
|