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