Line data Source code
1 : /*
2 : * Copyright (c) 2025 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/AttributeValueDecoder.h>
20 : #include <app/AttributeValueEncoder.h>
21 : #include <app/CommandHandler.h>
22 : #include <app/ConcreteClusterPath.h>
23 : #include <app/data-model-provider/ActionReturnStatus.h>
24 : #include <app/data-model-provider/MetadataTypes.h>
25 : #include <app/data-model-provider/OperationTypes.h>
26 : #include <app/server-cluster/ServerClusterContext.h>
27 : #include <lib/core/CHIPError.h>
28 : #include <lib/core/DataModelTypes.h>
29 : #include <lib/support/BitFlags.h>
30 : #include <lib/support/ReadOnlyBuffer.h>
31 :
32 : namespace chip {
33 : namespace app {
34 :
35 : /// Defines how a cluster is being shut down, so that its
36 : /// cleanup behavior can be more controlled.
37 : enum class ClusterShutdownType
38 : {
39 : kClusterShutdown, // normal shutdown, generally assume timer and delegate cleanup
40 : kPermanentRemove, // permanent removal, can consider persistent storage cleanup
41 : };
42 :
43 : /// Handles cluster interactions for a specific set of cluster instances
44 : ///
45 : /// A `ServerClusterInterface` instance may be associated with multiple `endpointId/clusterId` paths.
46 : ///
47 : /// Provides metadata as well as interaction processing (attribute read/write and command handling).
48 : class ServerClusterInterface
49 : {
50 : public:
51 1062 : virtual ~ServerClusterInterface() = default;
52 :
53 : /// Starts up the server cluster interface.
54 : ///
55 : /// The `context` lifetime must be guaranteed to last
56 : /// until `Shutdown` is called:
57 : ///
58 : /// - You are allowed to take and use a pointer to it until
59 : /// shutdown is called.
60 : /// - If context is needed, you SHOULD store a pointer rather
61 : /// than a copy to save RAM usage.
62 : virtual CHIP_ERROR Startup(ServerClusterContext & context) = 0;
63 :
64 : /// A shutdown will always be paired with a corresponding Startup.
65 : virtual void Shutdown(ClusterShutdownType shutdownType) = 0;
66 :
67 : ///////////////////////////////////// Cluster Metadata Support //////////////////////////////////////////////////
68 :
69 : /// The paths that this cluster interfaces handles.
70 : ///
71 : /// - MUST contain at least one element
72 : /// - MUST remain constant once the server cluster interface is in use.
73 : ///
74 : [[nodiscard]] virtual Span<const ConcreteClusterPath> GetPaths() const = 0;
75 :
76 : /// Gets the data version for this cluster instance.
77 : ///
78 : /// Every cluster instance must have a data version.
79 : ///
80 : /// SPEC - 7.10.3. Cluster Data Version
81 : /// A cluster data version is a metadata increment-only counter value, maintained for each cluster instance.
82 : /// [...]
83 : /// A cluster data version SHALL increment or be set (wrap) to zero if incrementing would exceed its
84 : /// maximum value. A cluster data version SHALL be maintained for each cluster instance.
85 : /// [...]
86 : /// A cluster data version SHALL be incremented if any attribute data changes.
87 : ///
88 : /// Precondition:
89 : /// - `path` parameter MUST match one of the paths returned by GetPaths.
90 : [[nodiscard]] virtual DataVersion GetDataVersion(const ConcreteClusterPath & path) const = 0;
91 :
92 : /// Precondition:
93 : /// - `path` parameter MUST match one of the paths returned by GetPaths.
94 : [[nodiscard]] virtual BitFlags<DataModel::ClusterQualityFlags> GetClusterFlags(const ConcreteClusterPath &) const = 0;
95 :
96 : ///////////////////////////////////// Attribute Support ////////////////////////////////////////////////////////
97 :
98 : /// Indicates the start/end of a series of list operations. This function will be called either before the first
99 : /// Write operation or after the last one of a series of consecutive attribute data values received for the same attribute.
100 : ///
101 : /// 1) This function will be called if the client tries to set a nullable list attribute to null.
102 : /// 2) This function will only be called at the beginning and end of a series of consecutive attribute data
103 : /// blocks for the same attribute, no matter what list operations those data blocks represent.
104 : /// 3) The opType argument indicates the type of notification (Start, Failure, Success).
105 : ///
106 : /// Precondition:
107 : /// - `path` endpoint+cluster part MUST match one of the paths returned by GetPaths.
108 4 : virtual void ListAttributeWriteNotification(const ConcreteAttributePath & path, DataModel::ListWriteOperation opType,
109 : FabricIndex accessingFabric)
110 4 : {}
111 :
112 : /// Reads the value of an existing attribute.
113 : ///
114 : /// ReadAttribute MUST be done on an "existent" attribute path: only on attributes that are
115 : /// returned in an `Attributes` call for this cluster. ReadAttribute is not expected to perform
116 : /// that verification; the caller is responsible for it.
117 : ///
118 : /// `request.path` is expected to have `GetClusterId` as the cluster id as well as an attribute that is
119 : /// included in an `Attributes` call.
120 : ///
121 : /// This MUST HANDLE the following global attributes:
122 : /// - FeatureMap::Id
123 : /// - ClusterRevision::Id
124 : ///
125 : /// This function WILL NOT be called for attributes that can be derived from cluster metadata.
126 : /// Specifically this WILL NOT be called (and does not need to implement handling for) the
127 : /// following attribute IDs:
128 : /// - AcceptedCommandList::Id
129 : /// - AttributeList::Id
130 : /// - GeneratedCommandList::Id
131 : ///
132 : /// Precondition:
133 : /// - `request.path` endpoint+cluster part MUST match one of the paths returned by GetPaths.
134 : virtual DataModel::ActionReturnStatus ReadAttribute(const DataModel::ReadAttributeRequest & request,
135 : AttributeValueEncoder & encoder) = 0;
136 :
137 : /// Writes a value to an existing attribute.
138 : ///
139 : /// WriteAttribute MUST be done on an "existent" attribute path: only on attributes that are
140 : /// returned in an `Attributes` call for this cluster. WriteAttribute is not expected to perform
141 : /// that verification; the caller is responsible for it.
142 : ///
143 : /// `request.path` is expected to have `GetClusterId` as the cluster id as well as an attribute that is
144 : /// included in a `Attributes` call.
145 : ///
146 : /// Precondition:
147 : /// - `request.path` endpoint+cluster part MUST match one of the paths returned by GetPaths.
148 : virtual DataModel::ActionReturnStatus WriteAttribute(const DataModel::WriteAttributeRequest & request,
149 : AttributeValueDecoder & decoder) = 0;
150 :
151 : /// Retrieves the list of attributes supported by this cluster.
152 : ///
153 : /// Attribute list MUST contain global attributes.
154 : ///
155 : /// Specifically these attributes MUST always exist in the list for all clusters:
156 : /// - ClusterRevision::Id
157 : /// - FeatureMap::Id
158 : /// - AcceptedCommandList::Id
159 : /// - AttributeList::Id
160 : /// - GeneratedCommandList::Id
161 : /// See SPEC 7.13 Global Elements: `Global Attributes` table
162 : ///
163 : /// Precondition:
164 : /// - `path` MUST match one of the paths returned by GetPaths.
165 : virtual CHIP_ERROR Attributes(const ConcreteClusterPath & path, ReadOnlyBufferBuilder<DataModel::AttributeEntry> & builder) = 0;
166 :
167 : /// Retrieve information about a specific generated event.
168 : ///
169 : /// In particular information regarding access, so that event reads can be validated.
170 : virtual CHIP_ERROR EventInfo(const ConcreteEventPath & path, DataModel::EventEntry & eventInfo) = 0;
171 :
172 : ///////////////////////////////////// Command Support /////////////////////////////////////////////////////////
173 :
174 : /// Handles the invocation of a command.
175 : ///
176 : /// `handler` is used to send back the response.
177 : /// - returning `nullopt` means that return value was placed in handler directly.
178 : /// This includes cases where command handling and value return will be done asynchronously.
179 : /// - returning a value other than Success implies an error reply (error and data are mutually exclusive)
180 : ///
181 : /// InvokeCommand MUST be done on an "existent" attribute path: only on commands that are
182 : /// returned in an `AcceptedCommand` call for this cluster.
183 : ///
184 : /// Return value expectations:
185 : /// - if a response has been placed into `handler` then std::nullopt MUST be returned. In particular
186 : /// note that CHIP_NO_ERROR is NOT the same as std::nullopt:
187 : /// > CHIP_NO_ERROR means handler had no status set and we expect the caller to AddStatus(success)
188 : /// > std::nullopt means that handler has added an appropriate data/status response
189 : /// - if a value is returned (not nullopt) then the handler response MUST NOT be filled. The caller
190 : /// will then issue `handler->AddStatus(request.path, <return_value>->GetStatusCode())`. This is a
191 : /// convenience to make writing Invoke calls easier.
192 : ///
193 : /// Precondition:
194 : /// - `request.path` endpoint+cluster part MUST match one of the paths returned by GetPaths.
195 : virtual std::optional<DataModel::ActionReturnStatus>
196 : InvokeCommand(const DataModel::InvokeRequest & request, chip::TLV::TLVReader & input_arguments, CommandHandler * handler) = 0;
197 :
198 : /// Retrieves a list of commands accepted by this cluster.
199 : ///
200 : /// Returning `CHIP_NO_ERROR` without adding anything to the `builder` list is expected
201 : /// if no commands are supported by the cluster.
202 : ///
203 : /// Precondition:
204 : /// - `path` MUST match one of the paths returned by GetPaths.
205 : virtual CHIP_ERROR AcceptedCommands(const ConcreteClusterPath & path,
206 : ReadOnlyBufferBuilder<DataModel::AcceptedCommandEntry> & builder) = 0;
207 :
208 : /// Retrieves a list of commands generated by this cluster.
209 : ///
210 : /// Returning `CHIP_NO_ERROR` without adding anything to the `builder` list is expected
211 : /// if no commands are generated by processing accepted commands.
212 : ///
213 : /// Precondition:
214 : /// - `path` MUST match one of the paths returned by GetPaths.
215 : virtual CHIP_ERROR GeneratedCommands(const ConcreteClusterPath & path, ReadOnlyBufferBuilder<CommandId> & builder) = 0;
216 :
217 : /// Returns whether `GetPaths` contains the given path
218 : bool PathsContains(const ConcreteClusterPath & path);
219 : };
220 :
221 : } // namespace app
222 : } // namespace chip
|