Matter SDK Coverage Report
Current view: top level - data-model-providers/codegen - CodegenDataModelProvider_Write.cpp (source / functions) Coverage Total Hit
Test: SHA:2a48c1efeab1c0f76f3adb3a0940b0f7de706453 Lines: 85.7 % 56 48
Test Date: 2026-01-31 08:14:20 Functions: 75.0 % 4 3

            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.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 <data-model-providers/codegen/EmberAttributeDataBuffer.h>
      37              : #include <lib/core/CHIPError.h>
      38              : #include <lib/support/CodeUtils.h>
      39              : #include <lib/support/odd-sized-integers.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              : /// Attempts to write via an attribute access interface (AAI)
      51              : ///
      52              : /// If it returns a CHIP_ERROR, then this is a FINAL result (i.e. either failure or success)
      53              : ///
      54              : /// If it returns std::nullopt, then there is no AAI to handle the given path
      55              : /// and processing should figure out the value otherwise (generally from other ember data)
      56         5302 : std::optional<CHIP_ERROR> TryWriteViaAccessInterface(const ConcreteDataAttributePath & path, AttributeAccessInterface * aai,
      57              :                                                      AttributeValueDecoder & decoder)
      58              : {
      59              :     // Processing can happen only if an attribute access interface actually exists..
      60         5302 :     if (aai == nullptr)
      61              :     {
      62          137 :         return std::nullopt;
      63              :     }
      64              : 
      65         5165 :     CHIP_ERROR err = aai->Write(path, decoder);
      66              : 
      67        10330 :     if (err != CHIP_NO_ERROR)
      68              :     {
      69           14 :         return std::make_optional(err);
      70              :     }
      71              : 
      72              :     // If the decoder tried to decode, then a value should have been read for processing.
      73              :     //   - if decoding was done, assume DONE (i.e. final CHIP_NO_ERROR)
      74              :     //   -  otherwise, if no decoding done, return that processing must continue via nullopt
      75        10302 :     return decoder.TriedDecode() ? std::make_optional(CHIP_NO_ERROR) : std::nullopt;
      76              : }
      77              : 
      78              : } // namespace
      79              : 
      80         5306 : DataModel::ActionReturnStatus CodegenDataModelProvider::WriteAttribute(const DataModel::WriteAttributeRequest & request,
      81              :                                                                        AttributeValueDecoder & decoder)
      82              : {
      83              :     // we must be started up to accept writes (we make use of the context below)
      84         5306 :     VerifyOrReturnError(mContext.has_value(), CHIP_ERROR_INCORRECT_STATE);
      85              : 
      86              :     const EmberAfAttributeMetadata * attributeMetadata =
      87         5306 :         emberAfLocateAttributeMetadata(request.path.mEndpointId, request.path.mClusterId, request.path.mAttributeId);
      88              : 
      89         5306 :     if (attributeMetadata != nullptr)
      90              :     {
      91              :         // AAI is only allowed on ember-attributes
      92              :         AttributeAccessInterface * aai =
      93         5302 :             AttributeAccessInterfaceRegistry::Instance().Get(request.path.mEndpointId, request.path.mClusterId);
      94         5302 :         std::optional<CHIP_ERROR> aai_result = TryWriteViaAccessInterface(request.path, aai, decoder);
      95         5302 :         if (aai_result.has_value())
      96              :         {
      97        10322 :             if (*aai_result == CHIP_NO_ERROR)
      98              :             {
      99              :                 // TODO: this is awkward since it provides AAI no control over this, specifically
     100              :                 //       AAI may not want to increase versions for some attributes that are Q
     101         5147 :                 emberAfAttributeChanged(request.path.mEndpointId, request.path.mClusterId, request.path.mAttributeId,
     102         5147 :                                         &mContext->dataModelChangeListener);
     103              :             }
     104         5161 :             return *aai_result;
     105              :         }
     106              :     }
     107              : 
     108              :     // If ServerClusterInterface is available, it provides the final answer
     109          145 :     if (auto * cluster = mRegistry.Get(request.path); cluster != nullptr)
     110              :     {
     111            5 :         return cluster->WriteAttribute(request, decoder);
     112              :     }
     113              : 
     114              :     // WriteAttribute requirement is that request.path is a VALID path inside the provider
     115              :     // metadata tree. Clients are supposed to validate this (and data version and other flags)
     116              :     // This SHOULD NEVER HAPPEN hence the general return code (seemed preferable to VerifyOrDie)
     117          140 :     VerifyOrReturnError(attributeMetadata != nullptr, Status::Failure);
     118              : 
     119          140 :     MutableByteSpan dataBuffer = gEmberAttributeIOBufferSpan;
     120              :     {
     121          140 :         Ember::EmberAttributeDataBuffer emberData(attributeMetadata, dataBuffer);
     122          140 :         ReturnErrorOnFailure(decoder.Decode(emberData));
     123              :     }
     124              : 
     125              :     Protocols::InteractionModel::Status status;
     126              : 
     127          116 :     if (dataBuffer.size() > attributeMetadata->size)
     128              :     {
     129            1 :         ChipLogDetail(Zcl, "Data to write exceeds the attribute size claimed.");
     130            1 :         return Status::InvalidValue;
     131              :     }
     132              : 
     133          115 :     EmberAfWriteDataInput dataInput(dataBuffer.data(), attributeMetadata->attributeType);
     134              : 
     135          115 :     dataInput.SetChangeListener(&mContext->dataModelChangeListener);
     136              :     // TODO: dataInput.SetMarkDirty() should be according to `ChangesOmmited`
     137              : 
     138          115 :     if (request.operationFlags.Has(DataModel::OperationFlags::kInternal))
     139              :     {
     140              :         // Internal requests use the non-External interface that has less enforcement
     141              :         // than the external version (e.g. does not check/enforce writable settings, does not
     142              :         // validate attribute types) - see attribute-table.h documentation for details.
     143            0 :         status = emberAfWriteAttribute(request.path, dataInput);
     144              :     }
     145              :     else
     146              :     {
     147          115 :         status = emAfWriteAttributeExternal(request.path, dataInput);
     148              :     }
     149              : 
     150          115 :     if (status != Protocols::InteractionModel::Status::Success)
     151              :     {
     152            2 :         return status;
     153              :     }
     154              : 
     155          113 :     return CHIP_NO_ERROR;
     156              : }
     157              : 
     158         1879 : void CodegenDataModelProvider::ListAttributeWriteNotification(const ConcreteAttributePath & aPath,
     159              :                                                               DataModel::ListWriteOperation opType, FabricIndex accessingFabric)
     160              : {
     161              :     // NOTE: for backwards compatibility, we process AAI logic BEFORE Server Cluster Interface
     162              :     //       so that AttributeAccessInterface logic works if one was installed before Server Cluster Interface
     163              :     //       support was introduced in the SDK.
     164         1879 :     AttributeAccessInterface * aai = AttributeAccessInterfaceRegistry::Instance().Get(aPath.mEndpointId, aPath.mClusterId);
     165         1879 :     if (aai != nullptr)
     166              :     {
     167         1874 :         switch (opType)
     168              :         {
     169          937 :         case DataModel::ListWriteOperation::kListWriteBegin:
     170          937 :             aai->OnListWriteBegin(aPath);
     171          937 :             break;
     172           12 :         case DataModel::ListWriteOperation::kListWriteFailure:
     173           12 :             aai->OnListWriteEnd(aPath, false);
     174           12 :             break;
     175          925 :         case DataModel::ListWriteOperation::kListWriteSuccess:
     176          925 :             aai->OnListWriteEnd(aPath, true);
     177          925 :             break;
     178              :         }
     179              : 
     180              :         // We fall through here and will notify any ServerClusterInterface as well.
     181              :         // This is NOT ideal because AAI may or may not fully intercept the write,
     182              :         // So we do not know which of the ::Write behavior AAI uses:
     183              :         //   - write succeeds (so SCI should not be notified)
     184              :         //   - AAI falls-through (so SCI should process the request)
     185              :         //
     186              :         // for now we err on the side of notifying both.
     187              :     }
     188              : 
     189         1879 :     if (auto * cluster = mRegistry.Get(aPath); cluster != nullptr)
     190              :     {
     191            0 :         cluster->ListAttributeWriteNotification(aPath, opType, accessingFabric);
     192            0 :         return;
     193              :     }
     194              : }
     195              : 
     196            0 : void CodegenDataModelProvider::Temporary_ReportAttributeChanged(const AttributePathParams & path)
     197              : {
     198              :     // we must be started up to process changes since we use the context
     199            0 :     VerifyOrReturn(mContext.has_value());
     200              : 
     201            0 :     if (path.mClusterId != kInvalidClusterId)
     202              :     {
     203            0 :         emberAfAttributeChanged(path.mEndpointId, path.mClusterId, path.mAttributeId, &mContext->dataModelChangeListener);
     204              :     }
     205              :     else
     206              :     {
     207              :         // When the path has wildcard cluster Id, call the emberAfEndpointChanged to mark attributes on the given endpoint
     208              :         // as having changing, but do NOT increase/alter any cluster data versions, as this happens when a bridged endpoint is
     209              :         // added or removed from a bridge and the cluster data is not changed during the process.
     210            0 :         emberAfEndpointChanged(path.mEndpointId, &mContext->dataModelChangeListener);
     211              :     }
     212              : }
     213              : 
     214              : } // namespace app
     215              : } // namespace chip
        

Generated by: LCOV version 2.0-1