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/ConcreteClusterPath.h>
20 : #include <app/server-cluster/ServerClusterContext.h>
21 : #include <app/server-cluster/ServerClusterInterface.h>
22 : #include <lib/core/DataModelTypes.h>
23 : #include <lib/support/Span.h>
24 :
25 : namespace chip::app {
26 :
27 : /// A proxy for a ServerClusterInterface that adds data version management and attribute change notification
28 : /// capability that works together with an underlying server cluster interface:
29 : /// - a ServerClusterContext received from startup is retained
30 : /// - a data version delta is maintained compared to underlying, to account for extra data
31 : ///
32 : /// This class wraps an existing ServerClusterInterface instance and is intended to be
33 : /// used as a base class for extending cluster functionality, like adding new attributes
34 : /// or commands to an existing server cluster interface.
35 : ///
36 : /// The class is intended to wrap a SINGLE cluster path for a given interface, even if the interface
37 : /// itself supports multiple paths.
38 : ///
39 : /// Expected usage is that the `ServerClusterExtension` is registered while the `underlying` interface
40 : /// is not. This can be used to wrap an existing registered interface like:
41 : ///
42 : /// ```
43 : /// ServerClusterInterface *underlying = registry.Get(clusterPath);
44 : /// registry.Unregister(underlying);
45 : /// auto extension = std::make_unique<RegisteredServerCluster<Extension>>(clusterPath, *underlying);
46 : /// registry.Register(extension->Registration());
47 : /// ```
48 : ///
49 : /// An extension could be chained like `Extension2(path2, Extension1(path1, underlying))` if more than
50 : /// one extension is desired.
51 : ///
52 : /// NOTES:
53 : /// - if changing an attribute (via WriteAttribute or as part of other operations), remember to call
54 : /// NotifyAttributeChanged so that attribute subscriptions work correctly.
55 : class ServerClusterExtension : public ServerClusterInterface
56 : {
57 : public:
58 4 : explicit ServerClusterExtension(const ConcreteClusterPath & path, ServerClusterInterface & underlying) :
59 4 : mClusterPath(path), mUnderlying(underlying)
60 4 : {}
61 :
62 : CHIP_ERROR Startup(ServerClusterContext & context) override;
63 : void Shutdown(ClusterShutdownType type) override;
64 : [[nodiscard]] Span<const ConcreteClusterPath> GetPaths() const override;
65 : [[nodiscard]] DataVersion GetDataVersion(const ConcreteClusterPath & path) const override;
66 : [[nodiscard]] BitFlags<DataModel::ClusterQualityFlags> GetClusterFlags(const ConcreteClusterPath & path) const override;
67 : DataModel::ActionReturnStatus WriteAttribute(const DataModel::WriteAttributeRequest & request,
68 : AttributeValueDecoder & decoder) override;
69 : void ListAttributeWriteNotification(const ConcreteAttributePath & path, DataModel::ListWriteOperation opType,
70 : FabricIndex accessingFabric) override;
71 : DataModel::ActionReturnStatus ReadAttribute(const DataModel::ReadAttributeRequest & request,
72 : AttributeValueEncoder & encoder) override;
73 : CHIP_ERROR Attributes(const ConcreteClusterPath & path, ReadOnlyBufferBuilder<DataModel::AttributeEntry> & builder) override;
74 : CHIP_ERROR EventInfo(const ConcreteEventPath & path, DataModel::EventEntry & eventInfo) override;
75 : std::optional<DataModel::ActionReturnStatus> InvokeCommand(const DataModel::InvokeRequest & request,
76 : chip::TLV::TLVReader & input_arguments,
77 : CommandHandler * handler) override;
78 : CHIP_ERROR AcceptedCommands(const ConcreteClusterPath & path,
79 : ReadOnlyBufferBuilder<DataModel::AcceptedCommandEntry> & builder) override;
80 : CHIP_ERROR GeneratedCommands(const ConcreteClusterPath & path, ReadOnlyBufferBuilder<CommandId> & builder) override;
81 :
82 : protected:
83 : const ConcreteClusterPath mClusterPath;
84 : ServerClusterInterface & mUnderlying;
85 :
86 : // Cluster context, set on Startup and reset to nullptr on shutdown.
87 : ServerClusterContext * mContext = nullptr;
88 :
89 : // A data version increment for when the extension's data changes.
90 : // Since data version is explicitly random to start and wraps, the extension's
91 : // version is computed as "underlying version + delta".
92 : DataVersion mVersionDelta = 0;
93 :
94 : /// Mark the given attribute as changed:
95 : /// - calls the underlying context if available (i.e. make sure reporting works)
96 : /// - updates internal version delta
97 : void NotifyAttributeChanged(AttributeId id);
98 : };
99 :
100 : } // namespace chip::app
|