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/FabricScoped.h>
26 : #include <app/reporting/reporting.h>
27 : #include <app/util/af-types.h>
28 : #include <app/util/attribute-metadata.h>
29 : #include <app/util/attribute-storage-detail.h>
30 : #include <app/util/attribute-storage-null-handling.h>
31 : #include <app/util/attribute-storage.h>
32 : #include <app/util/attribute-table-detail.h>
33 : #include <app/util/attribute-table.h>
34 : #include <app/util/ember-io-storage.h>
35 : #include <app/util/ember-strings.h>
36 : #include <app/util/odd-sized-integers.h>
37 : #include <data-model-providers/codegen/EmberAttributeDataBuffer.h>
38 : #include <lib/core/CHIPError.h>
39 : #include <lib/support/CodeUtils.h>
40 :
41 : #include <zap-generated/endpoint_config.h>
42 :
43 : namespace chip {
44 : namespace app {
45 : namespace {
46 :
47 : using namespace chip::app::Compatibility::Internal;
48 : using Protocols::InteractionModel::Status;
49 :
50 : class ContextAttributesChangeListener : public AttributesChangedListener
51 : {
52 : public:
53 5301 : ContextAttributesChangeListener(const DataModel::InteractionModelContext & context) : mListener(context.dataModelChangeListener)
54 5301 : {}
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 : const EmberAfAttributeMetadata * attributeMetadata =
100 5302 : emberAfLocateAttributeMetadata(request.path.mEndpointId, request.path.mClusterId, request.path.mAttributeId);
101 :
102 : // WriteAttribute requirement is that request.path is a VALID path inside the provider
103 : // metadata tree. Clients are supposed to validate this (and data version and other flags)
104 : // This SHOULD NEVER HAPPEN hence the general return code (seemed preferable to VerifyOrDie)
105 5302 : VerifyOrReturnError(attributeMetadata != nullptr, Status::Failure);
106 :
107 5302 : if (request.path.mDataVersion.HasValue())
108 : {
109 2 : DataVersion * versionPtr = emberAfDataVersionStorage(request.path);
110 :
111 2 : if (versionPtr == nullptr)
112 : {
113 0 : ChipLogError(DataManagement, "Unable to get cluster info for Endpoint 0x%x, Cluster " ChipLogFormatMEI,
114 : request.path.mEndpointId, ChipLogValueMEI(request.path.mClusterId));
115 0 : return Status::DataVersionMismatch;
116 : }
117 :
118 2 : if (request.path.mDataVersion.Value() != *versionPtr)
119 : {
120 1 : ChipLogError(DataManagement, "Write Version mismatch for Endpoint 0x%x, Cluster " ChipLogFormatMEI,
121 : request.path.mEndpointId, ChipLogValueMEI(request.path.mClusterId));
122 1 : return Status::DataVersionMismatch;
123 : }
124 : }
125 :
126 5301 : ContextAttributesChangeListener change_listener(CurrentContext());
127 :
128 : AttributeAccessInterface * aai =
129 5301 : AttributeAccessInterfaceRegistry::Instance().Get(request.path.mEndpointId, request.path.mClusterId);
130 5301 : std::optional<CHIP_ERROR> aai_result = TryWriteViaAccessInterface(request.path, aai, decoder);
131 5301 : if (aai_result.has_value())
132 : {
133 5160 : if (*aai_result == CHIP_NO_ERROR)
134 : {
135 : // TODO: this is awkward since it provides AAI no control over this, specifically
136 : // AAI may not want to increase versions for some attributes that are Q
137 5147 : emberAfAttributeChanged(request.path.mEndpointId, request.path.mClusterId, request.path.mAttributeId, &change_listener);
138 : }
139 5160 : return *aai_result;
140 : }
141 :
142 141 : MutableByteSpan dataBuffer = gEmberAttributeIOBufferSpan;
143 : {
144 141 : Ember::EmberAttributeDataBuffer emberData(attributeMetadata, dataBuffer);
145 141 : ReturnErrorOnFailure(decoder.Decode(emberData));
146 : }
147 :
148 : Protocols::InteractionModel::Status status;
149 :
150 117 : if (dataBuffer.size() > attributeMetadata->size)
151 : {
152 1 : ChipLogDetail(Zcl, "Data to write exceeds the attribute size claimed.");
153 1 : return Status::InvalidValue;
154 : }
155 :
156 116 : EmberAfWriteDataInput dataInput(dataBuffer.data(), attributeMetadata->attributeType);
157 :
158 116 : dataInput.SetChangeListener(&change_listener);
159 : // TODO: dataInput.SetMarkDirty() should be according to `ChangesOmmited`
160 :
161 116 : if (request.operationFlags.Has(DataModel::OperationFlags::kInternal))
162 : {
163 : // Internal requests use the non-External interface that has less enforcement
164 : // than the external version (e.g. does not check/enforce writable settings, does not
165 : // validate attribute types) - see attribute-table.h documentation for details.
166 0 : status = emberAfWriteAttribute(request.path, dataInput);
167 : }
168 : else
169 : {
170 116 : status = emAfWriteAttributeExternal(request.path, dataInput);
171 : }
172 :
173 116 : if (status != Protocols::InteractionModel::Status::Success)
174 : {
175 2 : return status;
176 : }
177 :
178 114 : return CHIP_NO_ERROR;
179 5301 : }
180 :
181 1879 : void CodegenDataModelProvider::ListAttributeWriteNotification(const ConcreteAttributePath & aPath,
182 : DataModel::ListWriteOperation opType)
183 : {
184 1879 : if (auto * cluster = mRegistry.Get(aPath); cluster != nullptr)
185 : {
186 0 : cluster->ListAttributeWriteNotification(aPath, opType);
187 0 : return;
188 : }
189 :
190 1879 : AttributeAccessInterface * aai = AttributeAccessInterfaceRegistry::Instance().Get(aPath.mEndpointId, aPath.mClusterId);
191 :
192 1879 : if (aai != nullptr)
193 : {
194 1874 : switch (opType)
195 : {
196 937 : case DataModel::ListWriteOperation::kListWriteBegin:
197 937 : aai->OnListWriteBegin(aPath);
198 937 : break;
199 12 : case DataModel::ListWriteOperation::kListWriteFailure:
200 12 : aai->OnListWriteEnd(aPath, false);
201 12 : break;
202 925 : case DataModel::ListWriteOperation::kListWriteSuccess:
203 925 : aai->OnListWriteEnd(aPath, true);
204 925 : break;
205 : }
206 : }
207 : }
208 :
209 0 : void CodegenDataModelProvider::Temporary_ReportAttributeChanged(const AttributePathParams & path)
210 : {
211 0 : ContextAttributesChangeListener change_listener(CurrentContext());
212 0 : if (path.mClusterId != kInvalidClusterId)
213 : {
214 0 : emberAfAttributeChanged(path.mEndpointId, path.mClusterId, path.mAttributeId, &change_listener);
215 : }
216 : else
217 : {
218 : // When the path has wildcard cluster Id, call the emberAfEndpointChanged to mark attributes on the given endpoint
219 : // as having changing, but do NOT increase/alter any cluster data versions, as this happens when a bridged endpoint is
220 : // added or removed from a bridge and the cluster data is not changed during the process.
221 0 : emberAfEndpointChanged(path.mEndpointId, &change_listener);
222 : }
223 0 : }
224 :
225 : } // namespace app
226 : } // namespace chip
|