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 : #include "ServerClusterInterface.h"
18 : #include <app/server-cluster/DefaultServerCluster.h>
19 :
20 : #include <access/Privilege.h>
21 : #include <app-common/zap-generated/ids/Attributes.h>
22 : #include <app/ConcreteClusterPath.h>
23 : #include <app/data-model-provider/MetadataTypes.h>
24 : #include <crypto/RandUtils.h>
25 : #include <lib/support/BitFlags.h>
26 : #include <optional>
27 : #include <protocols/interaction_model/StatusCode.h>
28 :
29 : namespace chip {
30 : namespace app {
31 : namespace {
32 :
33 : using namespace chip::app::Clusters;
34 : using namespace chip::app::DataModel;
35 :
36 : constexpr std::array<AttributeEntry, 5> kGlobalAttributeEntries{ {
37 : {
38 : Globals::Attributes::FeatureMap::Id,
39 : BitFlags<AttributeQualityFlags>(),
40 : Access::Privilege::kView,
41 : std::nullopt,
42 : },
43 : {
44 : Globals::Attributes::ClusterRevision::Id,
45 : BitFlags<AttributeQualityFlags>(),
46 : Access::Privilege::kView,
47 : std::nullopt,
48 : },
49 : {
50 : Globals::Attributes::GeneratedCommandList::Id,
51 : BitFlags<AttributeQualityFlags>(AttributeQualityFlags::kListAttribute),
52 : Access::Privilege::kView,
53 : std::nullopt,
54 : },
55 : {
56 : Globals::Attributes::AcceptedCommandList::Id,
57 : BitFlags<AttributeQualityFlags>(AttributeQualityFlags::kListAttribute),
58 : Access::Privilege::kView,
59 : std::nullopt,
60 : },
61 : {
62 : Globals::Attributes::AttributeList::Id,
63 : BitFlags<AttributeQualityFlags>(AttributeQualityFlags::kListAttribute),
64 : Access::Privilege::kView,
65 : std::nullopt,
66 : },
67 : } };
68 :
69 : } // namespace
70 :
71 1644 : Span<const DataModel::AttributeEntry> DefaultServerCluster::GlobalAttributes()
72 : {
73 1644 : return { kGlobalAttributeEntries.data(), kGlobalAttributeEntries.size() };
74 : }
75 :
76 7 : CHIP_ERROR DefaultServerCluster::Attributes(const ConcreteClusterPath & path, ReadOnlyBufferBuilder<AttributeEntry> & builder)
77 : {
78 :
79 7 : return builder.ReferenceExisting(GlobalAttributes());
80 : }
81 :
82 457 : CHIP_ERROR DefaultServerCluster::Startup(ServerClusterContext & context)
83 : {
84 457 : VerifyOrReturnError(mContext == nullptr, CHIP_ERROR_ALREADY_INITIALIZED);
85 :
86 457 : mContext = &context;
87 :
88 : // SPEC - 7.10.3. Cluster Data Version
89 : // A cluster data version SHALL be initialized randomly when it is first published.
90 457 : mDataVersion = Crypto::GetRandU32();
91 :
92 457 : return CHIP_NO_ERROR;
93 : }
94 :
95 297 : void DefaultServerCluster::Shutdown(ClusterShutdownType)
96 : {
97 297 : mContext = nullptr;
98 297 : }
99 :
100 1149 : void DefaultServerCluster::NotifyAttributeChanged(AttributeId attributeId)
101 : {
102 1149 : IncreaseDataVersion();
103 :
104 1149 : VerifyOrReturn(mContext != nullptr);
105 1098 : mContext->interactionContext.dataModelChangeListener.MarkDirty({ mPath.mEndpointId, mPath.mClusterId, attributeId });
106 : }
107 :
108 10 : BitFlags<ClusterQualityFlags> DefaultServerCluster::GetClusterFlags(const ConcreteClusterPath &) const
109 : {
110 10 : return {};
111 : }
112 :
113 1 : ActionReturnStatus DefaultServerCluster::WriteAttribute(const WriteAttributeRequest & request, AttributeValueDecoder & decoder)
114 : {
115 1 : return Protocols::InteractionModel::Status::UnsupportedWrite;
116 : }
117 :
118 : std::optional<ActionReturnStatus>
119 1 : DefaultServerCluster::InvokeCommand(const InvokeRequest & request, chip::TLV::TLVReader & input_arguments, CommandHandler * handler)
120 : {
121 1 : return Protocols::InteractionModel::Status::UnsupportedCommand;
122 : }
123 :
124 3 : CHIP_ERROR DefaultServerCluster::AcceptedCommands(const ConcreteClusterPath & path,
125 : ReadOnlyBufferBuilder<AcceptedCommandEntry> & builder)
126 : {
127 3 : return CHIP_NO_ERROR;
128 : }
129 :
130 4 : CHIP_ERROR DefaultServerCluster::GeneratedCommands(const ConcreteClusterPath & path, ReadOnlyBufferBuilder<CommandId> & builder)
131 : {
132 4 : return CHIP_NO_ERROR;
133 : }
134 :
135 74 : DataModel::ActionReturnStatus DefaultServerCluster::NotifyAttributeChangedIfSuccess(AttributeId attributeId,
136 : DataModel::ActionReturnStatus status)
137 : {
138 74 : if (status.IsSuccess() && !status.IsNoOpSuccess())
139 : {
140 50 : NotifyAttributeChanged(attributeId);
141 : }
142 74 : return status;
143 : }
144 :
145 : } // namespace app
146 : } // namespace chip
|