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