Matter SDK Coverage Report
Current view: top level - data-model-providers/codegen - CodegenDataModelProvider.h (source / functions) Coverage Total Hit
Test: SHA:2a48c1efeab1c0f76f3adb3a0940b0f7de706453 Lines: 100.0 % 7 7
Test Date: 2026-01-31 08:14:20 Functions: 100.0 % 4 4

            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              : #pragma once
      18              : 
      19              : #include <app/data-model-provider/Provider.h>
      20              : 
      21              : #include <app/CommandHandlerInterface.h>
      22              : #include <app/ConcreteAttributePath.h>
      23              : #include <app/ConcreteCommandPath.h>
      24              : #include <app/data-model-provider/ActionReturnStatus.h>
      25              : #include <app/data-model-provider/MetadataTypes.h>
      26              : #include <app/server-cluster/SingleEndpointServerClusterRegistry.h>
      27              : #include <app/util/af-types.h>
      28              : #include <lib/core/CHIPPersistentStorageDelegate.h>
      29              : #include <lib/support/ReadOnlyBuffer.h>
      30              : 
      31              : namespace chip {
      32              : namespace app {
      33              : 
      34              : /// An implementation of `DataModel::Provider` that relies on code-generation
      35              : /// via zap/ember.
      36              : ///
      37              : /// The Ember framework uses generated files (like endpoint-config.h and various
      38              : /// other generated metadata) to provide a cluster model.
      39              : ///
      40              : /// This class will use global functions generally residing in `app/util`
      41              : /// as well as application-specific overrides to provide data model functionality.
      42              : ///
      43              : /// Given that this relies on global data at link time, there generally can be
      44              : /// only one CodegenDataModelProvider per application. Per-cluster CodegenIntegration
      45              : /// functions access the global singleton instance via `CodegenDataModelProvider::Instance()`.
      46              : class CodegenDataModelProvider : public DataModel::Provider
      47              : {
      48              : public:
      49              :     // access to the typed global singleton of this class.
      50              :     static CodegenDataModelProvider & Instance();
      51              : 
      52              :     /// clears out internal caching. Especially useful in unit tests,
      53              :     /// where path caching does not really apply (the same path may result in different outcomes)
      54          180 :     void Reset() { mPreviouslyFoundCluster = std::nullopt; }
      55              : 
      56          313 :     void SetPersistentStorageDelegate(PersistentStorageDelegate * delegate)
      57              :     {
      58          313 :         VerifyOrDie(!mContext.has_value()); // can't change once started
      59          313 :         mPersistentStorageDelegate = delegate;
      60          313 :     }
      61              :     PersistentStorageDelegate * GetPersistentStorageDelegate() { return mPersistentStorageDelegate; }
      62              : 
      63           16 :     SingleEndpointServerClusterRegistry & Registry() { return mRegistry; }
      64              : 
      65              :     /// Generic model implementations
      66              :     CHIP_ERROR Startup(DataModel::InteractionModelContext context) override;
      67              :     CHIP_ERROR Shutdown() override;
      68              : 
      69              :     DataModel::ActionReturnStatus ReadAttribute(const DataModel::ReadAttributeRequest & request,
      70              :                                                 AttributeValueEncoder & encoder) override;
      71              :     DataModel::ActionReturnStatus WriteAttribute(const DataModel::WriteAttributeRequest & request,
      72              :                                                  AttributeValueDecoder & decoder) override;
      73              : 
      74              :     void ListAttributeWriteNotification(const ConcreteAttributePath & aPath, DataModel::ListWriteOperation opType,
      75              :                                         FabricIndex accessingFabric) override;
      76              :     std::optional<DataModel::ActionReturnStatus> InvokeCommand(const DataModel::InvokeRequest & request,
      77              :                                                                TLV::TLVReader & input_arguments, CommandHandler * handler) override;
      78              : 
      79              :     /// attribute tree iteration
      80              :     CHIP_ERROR Endpoints(ReadOnlyBufferBuilder<DataModel::EndpointEntry> & out) override;
      81              :     CHIP_ERROR DeviceTypes(EndpointId endpointId, ReadOnlyBufferBuilder<DataModel::DeviceTypeEntry> & builder) override;
      82              :     CHIP_ERROR ClientClusters(EndpointId endpointId, ReadOnlyBufferBuilder<ClusterId> & builder) override;
      83              :     CHIP_ERROR ServerClusters(EndpointId endpointId, ReadOnlyBufferBuilder<DataModel::ServerClusterEntry> & builder) override;
      84              : #if CHIP_CONFIG_USE_ENDPOINT_UNIQUE_ID
      85              :     CHIP_ERROR EndpointUniqueID(EndpointId endpointId, MutableCharSpan & epUniqueId) override;
      86              : #endif
      87              :     CHIP_ERROR EventInfo(const ConcreteEventPath & path, DataModel::EventEntry & eventInfo) override;
      88              :     CHIP_ERROR GeneratedCommands(const ConcreteClusterPath & path, ReadOnlyBufferBuilder<CommandId> & builder) override;
      89              :     CHIP_ERROR AcceptedCommands(const ConcreteClusterPath & path,
      90              :                                 ReadOnlyBufferBuilder<DataModel::AcceptedCommandEntry> & builder) override;
      91              :     CHIP_ERROR Attributes(const ConcreteClusterPath & path, ReadOnlyBufferBuilder<DataModel::AttributeEntry> & builder) override;
      92              : 
      93              :     void Temporary_ReportAttributeChanged(const AttributePathParams & path) override;
      94              : 
      95              : protected:
      96              :     // Temporary hack for a test: Initializes the data model for testing purposes only.
      97              :     // This method serves as a placeholder and should NOT be used outside of specific tests.
      98              :     // It is expected to be removed or replaced with a proper implementation in the future.TODO:(#36837).
      99              :     virtual void InitDataModelForTesting();
     100              : 
     101              : private:
     102              :     // Context is available after startup and cleared in shutdown.
     103              :     // This has a value for as long as we assume the context is valid.
     104              :     std::optional<DataModel::InteractionModelContext> mContext;
     105              : 
     106              :     // Iteration is often done in a tight loop going through all values.
     107              :     // To avoid N^2 iterations, cache a hint of where something is positioned
     108              :     uint16_t mEndpointIterationHint = 0;
     109              : 
     110              :     // represents a remembered cluster reference that has been found as
     111              :     // looking for clusters is very common (for every attribute iteration)
     112              :     struct ClusterReference
     113              :     {
     114              :         ConcreteClusterPath path;
     115              :         const EmberAfCluster * cluster;
     116              : 
     117          672 :         ClusterReference(const ConcreteClusterPath p, const EmberAfCluster * c) : path(p), cluster(c) {}
     118              :     };
     119              : 
     120              :     enum class ClusterSide : uint8_t
     121              :     {
     122              :         kServer,
     123              :         kClient,
     124              :     };
     125              : 
     126              :     std::optional<ClusterReference> mPreviouslyFoundCluster;
     127              :     unsigned mEmberMetadataStructureGeneration = 0;
     128              : 
     129              :     // Ember requires a persistence provider, so we make sure we can always have something
     130              :     PersistentStorageDelegate * mPersistentStorageDelegate = nullptr;
     131              : 
     132              :     SingleEndpointServerClusterRegistry mRegistry;
     133              : 
     134              :     /// Finds the specified ember cluster
     135              :     ///
     136              :     /// Effectively the same as `emberAfFindServerCluster` except with some caching capabilities
     137              :     const EmberAfCluster * FindServerCluster(const ConcreteClusterPath & path);
     138              : 
     139              :     /// Find the index of the given endpoint id
     140              :     std::optional<unsigned> TryFindEndpointIndex(EndpointId id) const;
     141              : };
     142              : 
     143              : } // namespace app
     144              : } // namespace chip
        

Generated by: LCOV version 2.0-1