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