Matter SDK Coverage Report
Current view: top level - data-model-providers/codegen - CodegenDataModelProvider_Read.cpp (source / functions) Coverage Total Hit
Test: SHA:3108862db59e5fa02f4a254cea1d5089c60155eb Lines: 100.0 % 30 30
Test Date: 2025-10-12 07:08:15 Functions: 100.0 % 2 2

            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 <optional>
      20              : 
      21              : #include <access/AccessControl.h>
      22              : #include <access/Privilege.h>
      23              : #include <access/RequestPath.h>
      24              : #include <app-common/zap-generated/attribute-type.h>
      25              : #include <app/AttributeAccessInterface.h>
      26              : #include <app/AttributeAccessInterfaceRegistry.h>
      27              : #include <app/AttributeValueEncoder.h>
      28              : #include <app/RequiredPrivilege.h>
      29              : #include <app/data-model/FabricScoped.h>
      30              : #include <app/util/af-types.h>
      31              : #include <app/util/attribute-metadata.h>
      32              : #include <app/util/attribute-storage-detail.h>
      33              : #include <app/util/attribute-storage-null-handling.h>
      34              : #include <app/util/attribute-storage.h>
      35              : #include <app/util/ember-io-storage.h>
      36              : #include <app/util/endpoint-config-api.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              : #include <lib/support/Span.h>
      42              : 
      43              : #include <zap-generated/endpoint_config.h>
      44              : 
      45              : namespace chip {
      46              : namespace app {
      47              : 
      48              : namespace {
      49              : 
      50              : using namespace chip::app::Compatibility::Internal;
      51              : using Protocols::InteractionModel::Status;
      52              : 
      53              : /// Attempts to read via an attribute access interface (AAI)
      54              : ///
      55              : /// If it returns a CHIP_ERROR, then this is a FINAL result (i.e. either failure or success).
      56              : ///
      57              : /// If it returns std::nullopt, then there is no AAI to handle the given path
      58              : /// and processing should figure out the value otherwise (generally from other ember data)
      59         1804 : std::optional<CHIP_ERROR> TryReadViaAccessInterface(const DataModel::ReadAttributeRequest & request, AttributeAccessInterface * aai,
      60              :                                                     AttributeValueEncoder & encoder)
      61              : {
      62              :     // Processing can happen only if an attribute access interface actually exists..
      63         1804 :     if (aai == nullptr)
      64              :     {
      65           41 :         return std::nullopt;
      66              :     }
      67              : 
      68         1763 :     CHIP_ERROR err = aai->Read(request, encoder);
      69              : 
      70         1763 :     if (err != CHIP_NO_ERROR)
      71              :     {
      72              :         // Implementation of 8.4.3.2 of the spec for path expansion
      73          139 :         if (request.path.mExpanded && (err == CHIP_IM_GLOBAL_STATUS(UnsupportedRead)))
      74              :         {
      75            1 :             return CHIP_NO_ERROR;
      76              :         }
      77              : 
      78          138 :         return err;
      79              :     }
      80              : 
      81              :     // If the encoder tried to encode, then a value should have been written.
      82              :     //   - if encode, assume DONE (i.e. FINAL CHIP_NO_ERROR)
      83              :     //   - if no encode, say that processing must continue
      84         3248 :     return encoder.TriedEncode() ? std::make_optional(CHIP_NO_ERROR) : std::nullopt;
      85              : }
      86              : 
      87              : } // namespace
      88              : 
      89              : /// separated-out ReadAttribute implementation (given existing complexity)
      90              : ///
      91              : /// Generally will:
      92              : ///    - validate ACL (only for non-internal requests)
      93              : ///    - Try to read attribute via the AttributeAccessInterface
      94              : ///    - Try to read the value from ember RAM storage
      95         1806 : DataModel::ActionReturnStatus CodegenDataModelProvider::ReadAttribute(const DataModel::ReadAttributeRequest & request,
      96              :                                                                       AttributeValueEncoder & encoder)
      97              : {
      98         1806 :     ChipLogDetail(DataManagement,
      99              :                   "Reading attribute: Cluster=" ChipLogFormatMEI " Endpoint=0x%x AttributeId=" ChipLogFormatMEI " (expanded=%d)",
     100              :                   ChipLogValueMEI(request.path.mClusterId), request.path.mEndpointId, ChipLogValueMEI(request.path.mAttributeId),
     101              :                   request.path.mExpanded);
     102              : 
     103              :     // Codegen logic specific: we accept AAI reads BEFORE server cluster interface, so that we are backwards compatible
     104              :     // in case some application installed AAI before Server Cluster Interfaces were supported
     105              :     const EmberAfAttributeMetadata * attributeMetadata =
     106         1806 :         emberAfLocateAttributeMetadata(request.path.mEndpointId, request.path.mClusterId, request.path.mAttributeId);
     107              : 
     108              :     // we only allow AAI on ember-registered clusters
     109         1806 :     if (attributeMetadata != nullptr)
     110              :     {
     111         1804 :         std::optional<CHIP_ERROR> aai_result = TryReadViaAccessInterface(
     112         1804 :             request, AttributeAccessInterfaceRegistry::Instance().Get(request.path.mEndpointId, request.path.mClusterId), encoder);
     113         1804 :         VerifyOrReturnError(!aai_result.has_value(), *aai_result);
     114              :     }
     115              : 
     116           43 :     if (auto * cluster = mRegistry.Get(request.path); cluster != nullptr)
     117              :     {
     118            4 :         return cluster->ReadAttribute(request, encoder);
     119              :     }
     120              : 
     121              :     // ReadAttribute requirement is that request.path is a VALID path inside the provider
     122              :     // metadata tree. Clients are supposed to validate this (and data version and other flags)
     123              :     // This SHOULD NEVER HAPPEN hence the general return code (seemed preferable to VerifyOrDie)
     124           39 :     VerifyOrReturnError(attributeMetadata != nullptr, Status::Failure);
     125              : 
     126              :     // At this point, we have to use ember directly to read the data.
     127              :     EmberAfAttributeSearchRecord record;
     128           39 :     record.endpoint                            = request.path.mEndpointId;
     129           39 :     record.clusterId                           = request.path.mClusterId;
     130           39 :     record.attributeId                         = request.path.mAttributeId;
     131           39 :     Protocols::InteractionModel::Status status = emAfReadOrWriteAttribute(
     132           39 :         &record, &attributeMetadata, gEmberAttributeIOBufferSpan.data(), static_cast<uint16_t>(gEmberAttributeIOBufferSpan.size()),
     133              :         /* write = */ false);
     134              : 
     135           39 :     if (status != Protocols::InteractionModel::Status::Success)
     136              :     {
     137            2 :         return CHIP_ERROR_IM_GLOBAL_STATUS_VALUE(status);
     138              :     }
     139              : 
     140           37 :     VerifyOrReturnError(attributeMetadata != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
     141              : 
     142           37 :     MutableByteSpan data = gEmberAttributeIOBufferSpan;
     143           37 :     Ember::EmberAttributeDataBuffer emberData(attributeMetadata, data);
     144           37 :     return encoder.Encode(emberData);
     145              : }
     146              : 
     147              : } // namespace app
     148              : } // namespace chip
        

Generated by: LCOV version 2.0-1