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/MetadataList.h>
25 : #include <app/data-model-provider/MetadataTypes.h>
26 : #include <app/data-model-provider/OperationTypes.h>
27 : #include <app/server-cluster/ServerClusterContext.h>
28 : #include <lib/core/CHIPError.h>
29 : #include <lib/core/DataModelTypes.h>
30 : #include <lib/support/BitFlags.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 path to this cluster instance.
63 : ///
64 : /// This path (endpointid,clusterid) is expected to remain constant once the server
65 : /// cluster interface is in use.
66 : [[nodiscard]] virtual ConcreteClusterPath GetPath() 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 : [[nodiscard]] virtual DataVersion GetDataVersion() const = 0;
80 :
81 : [[nodiscard]] virtual BitFlags<DataModel::ClusterQualityFlags> GetClusterFlags() const = 0;
82 :
83 : ///////////////////////////////////// Attribute Support ////////////////////////////////////////////////////////
84 :
85 : /// Indicates the start/end of a series of list operations. This function will be called either before the first
86 : /// Write operation or after the last one of a series of consecutive attribute data values received for the same attribute.
87 : ///
88 : /// 1) This function will be called if the client tries to set a nullable list attribute to null.
89 : /// 2) This function will only be called at the beginning and end of a series of consecutive attribute data
90 : /// blocks for the same attribute, no matter what list operations those data blocks represent.
91 : /// 3) The opType argument indicates the type of notification (Start, Failure, Success).
92 4 : virtual void ListAttributeWriteNotification(const ConcreteAttributePath & aPath, DataModel::ListWriteOperation opType) {}
93 :
94 : /// Reads the value of an existing attribute.
95 : ///
96 : /// ReadAttribute MUST be done on an "existent" attribute path: only on attributes that are
97 : /// returned in an `Attributes` call for this cluster. ReadAttribute is not expected to perform
98 : /// that verification; the caller is responsible for it.
99 : ///
100 : /// `request.path` is expected to have `GetClusterId` as the cluster id as well as an attribute that is
101 : /// included in an `Attributes` call.
102 : ///
103 : /// This MUST HANDLE the following global attributes:
104 : /// - FeatureMap::Id
105 : /// - ClusterRevision::Id
106 : ///
107 : /// This function WILL NOT be called for attributes that can be derived from cluster metadata.
108 : /// Specifically this WILL NOT be called (and does not need to implement handling for) the
109 : /// following attribute IDs:
110 : /// - AcceptedCommandList::Id
111 : /// - AttributeList::Id
112 : /// - GeneratedCommandList::Id
113 : virtual DataModel::ActionReturnStatus ReadAttribute(const DataModel::ReadAttributeRequest & request,
114 : AttributeValueEncoder & encoder) = 0;
115 :
116 : /// Writes a value to an existing attribute.
117 : ///
118 : /// WriteAttribute MUST be done on an "existent" attribute path: only on attributes that are
119 : /// returned in an `Attributes` call for this cluster. WriteAttribute is not expected to perform
120 : /// that verification; the caller is responsible for it.
121 : ///
122 : /// `request.path` is expected to have `GetClusterId` as the cluster id as well as an attribute that is
123 : /// included in a `Attributes` call.
124 : virtual DataModel::ActionReturnStatus WriteAttribute(const DataModel::WriteAttributeRequest & request,
125 : AttributeValueDecoder & decoder) = 0;
126 :
127 : /// Retrieves the list of attributes supported by this cluster.
128 : ///
129 : /// Attribute list MUST contain global attributes.
130 : ///
131 : /// Specifically these attributes MUST always exist in the list for all clusters:
132 : /// - ClusterRevision::Id
133 : /// - FeatureMap::Id
134 : /// - AcceptedCommandList::Id
135 : /// - AttributeList::Id
136 : /// - GeneratedCommandList::Id
137 : /// See SPEC 7.13 Global Elements: `Global Attributes` table
138 : virtual CHIP_ERROR Attributes(const ConcreteClusterPath & path,
139 : DataModel::ListBuilder<DataModel::AttributeEntry> & builder) = 0;
140 :
141 : ///////////////////////////////////// Command Support /////////////////////////////////////////////////////////
142 :
143 : /// Handles the invocation of a command.
144 : ///
145 : /// `handler` is used to send back the response.
146 : /// - returning `nullopt` means that return value was placed in handler directly.
147 : /// This includes cases where command handling and value return will be done asynchronously.
148 : /// - returning a value other than Success implies an error reply (error and data are mutually exclusive)
149 : ///
150 : /// InvokeCommand MUST be done on an "existent" attribute path: only on commands that are
151 : /// returned in an `AcceptedCommand` call for this cluster.
152 : ///
153 : /// Return value expectations:
154 : /// - if a response has been placed into `handler` then std::nullopt MUST be returned. In particular
155 : /// note that CHIP_NO_ERROR is NOT the same as std::nullopt:
156 : /// > CHIP_NO_ERROR means handler had no status set and we expect the caller to AddStatus(success)
157 : /// > std::nullopt means that handler has added an appropriate data/status response
158 : /// - if a value is returned (not nullopt) then the handler response MUST NOT be filled. The caller
159 : /// will then issue `handler->AddStatus(request.path, <return_value>->GetStatusCode())`. This is a
160 : /// convenience to make writing Invoke calls easier.
161 : virtual std::optional<DataModel::ActionReturnStatus>
162 : InvokeCommand(const DataModel::InvokeRequest & request, chip::TLV::TLVReader & input_arguments, CommandHandler * handler) = 0;
163 :
164 : /// Retrieves a list of commands accepted by this cluster.
165 : ///
166 : /// Returning `CHIP_NO_ERROR` without adding anything to the `builder` list is expected
167 : /// if no commands are supported by the cluster.
168 : virtual CHIP_ERROR AcceptedCommands(const ConcreteClusterPath & path,
169 : DataModel::ListBuilder<DataModel::AcceptedCommandEntry> & builder) = 0;
170 :
171 : /// Retrieves a list of commands generated by this cluster.
172 : ///
173 : /// Returning `CHIP_NO_ERROR` without adding anything to the `builder` list is expected
174 : /// if no commands are generated by processing accepted commands.
175 : virtual CHIP_ERROR GeneratedCommands(const ConcreteClusterPath & path, DataModel::ListBuilder<CommandId> & builder) = 0;
176 : };
177 :
178 : } // namespace app
179 : } // namespace chip
|