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