Matter SDK Coverage Report
Current view: top level - data-model-providers/codegen - CodegenDataModelProvider_Write.cpp (source / functions) Coverage Total Hit
Test: SHA:3f9cd168e84cd831b7699126f5296f5c5498690f Lines: 93.9 % 49 46
Test Date: 2026-04-27 19:52:19 Functions: 100.0 % 3 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/ConcreteAttributePath.h>
      24              : #include <app/GlobalAttributes.h>
      25              : #include <app/RequiredPrivilege.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              :                 // AAI write was successful. We still need to bump the version and notify our listeners.
     100              :                 // ember attribute changed will increase version and call back the global codegen provider (which should be `this`)
     101         5147 :                 emberAfAttributeChanged(request.path.mEndpointId, request.path.mClusterId, request.path.mAttributeId);
     102              :             }
     103         5161 :             return *aai_result;
     104              :         }
     105              :     }
     106              : 
     107              :     // If ServerClusterInterface is available, it provides the final answer
     108          145 :     if (auto * cluster = mRegistry.Get(request.path); cluster != nullptr)
     109              :     {
     110            5 :         return cluster->WriteAttribute(request, decoder);
     111              :     }
     112              : 
     113              :     // WriteAttribute requirement is that request.path is a VALID path inside the provider
     114              :     // metadata tree. Clients are supposed to validate this (and data version and other flags)
     115              :     // This SHOULD NEVER HAPPEN hence the general return code (seemed preferable to VerifyOrDie)
     116          140 :     VerifyOrReturnError(attributeMetadata != nullptr, Status::Failure);
     117              : 
     118          140 :     MutableByteSpan dataBuffer = gEmberAttributeIOBufferSpan;
     119              :     {
     120          140 :         Ember::EmberAttributeDataBuffer emberData(attributeMetadata, dataBuffer);
     121          140 :         ReturnErrorOnFailure(decoder.Decode(emberData));
     122              :     }
     123              : 
     124              :     Protocols::InteractionModel::Status status;
     125              : 
     126          116 :     if (dataBuffer.size() > attributeMetadata->size)
     127              :     {
     128            1 :         ChipLogDetail(Zcl, "Data to write exceeds the attribute size claimed.");
     129            1 :         return Status::InvalidValue;
     130              :     }
     131              : 
     132          115 :     EmberAfWriteDataInput dataInput(dataBuffer.data(), attributeMetadata->attributeType);
     133              : 
     134              :     // TODO: dataInput.SetMarkDirty() should be according to `ChangesOmmited`
     135              : 
     136          115 :     if (request.subjectDescriptor.authMode == Access::AuthMode::kInternalDeviceAccess)
     137              :     {
     138              :         // Internal requests use the non-External interface that has less enforcement
     139              :         // than the external version (e.g. does not check/enforce writable settings, does not
     140              :         // validate attribute types) - see attribute-table.h documentation for details.
     141            0 :         status = emberAfWriteAttribute(request.path, dataInput);
     142              :     }
     143              :     else
     144              :     {
     145          115 :         status = emAfWriteAttributeExternal(request.path, dataInput);
     146              :     }
     147              : 
     148          115 :     if (status != Protocols::InteractionModel::Status::Success)
     149              :     {
     150            2 :         return status;
     151              :     }
     152              : 
     153          113 :     return CHIP_NO_ERROR;
     154              : }
     155              : 
     156         1879 : void CodegenDataModelProvider::ListAttributeWriteNotification(const ConcreteAttributePath & aPath,
     157              :                                                               DataModel::ListWriteOperation opType, FabricIndex accessingFabric)
     158              : {
     159              :     // NOTE: for backwards compatibility, we process AAI logic BEFORE Server Cluster Interface
     160              :     //       so that AttributeAccessInterface logic works if one was installed before Server Cluster Interface
     161              :     //       support was introduced in the SDK.
     162         1879 :     AttributeAccessInterface * aai = AttributeAccessInterfaceRegistry::Instance().Get(aPath.mEndpointId, aPath.mClusterId);
     163         1879 :     if (aai != nullptr)
     164              :     {
     165         1874 :         switch (opType)
     166              :         {
     167          937 :         case DataModel::ListWriteOperation::kListWriteBegin:
     168          937 :             aai->OnListWriteBegin(aPath);
     169          937 :             break;
     170           12 :         case DataModel::ListWriteOperation::kListWriteFailure:
     171           12 :             aai->OnListWriteEnd(aPath, false);
     172           12 :             break;
     173          925 :         case DataModel::ListWriteOperation::kListWriteSuccess:
     174          925 :             aai->OnListWriteEnd(aPath, true);
     175          925 :             break;
     176              :         }
     177              : 
     178              :         // We fall through here and will notify any ServerClusterInterface as well.
     179              :         // This is NOT ideal because AAI may or may not fully intercept the write,
     180              :         // So we do not know which of the ::Write behavior AAI uses:
     181              :         //   - write succeeds (so SCI should not be notified)
     182              :         //   - AAI falls-through (so SCI should process the request)
     183              :         //
     184              :         // for now we err on the side of notifying both.
     185              :     }
     186              : 
     187         1879 :     if (auto * cluster = mRegistry.Get(aPath); cluster != nullptr)
     188              :     {
     189            0 :         cluster->ListAttributeWriteNotification(aPath, opType, accessingFabric);
     190            0 :         return;
     191              :     }
     192              : }
     193              : 
     194              : } // namespace app
     195              : } // namespace chip
        

Generated by: LCOV version 2.0-1