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