/src/connectedhomeip/src/app/clusters/fixed-label-server/FixedLabelCluster.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021-2025 Project CHIP Authors |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include <app/clusters/fixed-label-server/FixedLabelCluster.h> |
18 | | #include <app/server-cluster/AttributeListBuilder.h> |
19 | | #include <clusters/FixedLabel/Metadata.h> |
20 | | |
21 | | namespace chip::app::Clusters { |
22 | | |
23 | | using namespace FixedLabel::Attributes; |
24 | | |
25 | | namespace { |
26 | | |
27 | | class AutoReleaseIterator |
28 | | { |
29 | | public: |
30 | | AutoReleaseIterator(DeviceLayer::DeviceInfoProvider & provider, EndpointId endpointId) : |
31 | 0 | mIterator(provider.IterateFixedLabel(endpointId)) |
32 | 0 | {} |
33 | | ~AutoReleaseIterator() |
34 | 0 | { |
35 | 0 | if (mIterator != nullptr) |
36 | 0 | { |
37 | 0 | mIterator->Release(); |
38 | 0 | } |
39 | 0 | } |
40 | | |
41 | 0 | bool IsValid() const { return mIterator != nullptr; } |
42 | | |
43 | 0 | DeviceLayer::DeviceInfoProvider::FixedLabelIterator * operator->() { return mIterator; } |
44 | | |
45 | | private: |
46 | | DeviceLayer::DeviceInfoProvider::FixedLabelIterator * mIterator; |
47 | | }; |
48 | | |
49 | | CHIP_ERROR ReadLabelList(EndpointId endpoint, AttributeValueEncoder & encoder, DeviceLayer::DeviceInfoProvider & provider) |
50 | 0 | { |
51 | 0 | AutoReleaseIterator it(provider, endpoint); |
52 | 0 | VerifyOrReturnValue(it.IsValid(), encoder.EncodeEmptyList()); |
53 | | |
54 | 0 | return encoder.EncodeList([&it](const auto & encod) -> CHIP_ERROR { |
55 | 0 | FixedLabel::Structs::LabelStruct::Type fixedlabel; |
56 | 0 | while (it->Next(fixedlabel)) |
57 | 0 | { |
58 | 0 | ReturnErrorOnFailure(encod.Encode(fixedlabel)); |
59 | 0 | } |
60 | 0 | return CHIP_NO_ERROR; |
61 | 0 | }); |
62 | 0 | } |
63 | | |
64 | | } // namespace |
65 | | |
66 | | FixedLabelCluster::FixedLabelCluster(EndpointId endpoint, DeviceLayer::DeviceInfoProvider & deviceInfoProvider) : |
67 | 2 | DefaultServerCluster({ endpoint, FixedLabel::Id }), mDeviceInfoProvider(deviceInfoProvider) |
68 | 2 | {} |
69 | | |
70 | | DataModel::ActionReturnStatus FixedLabelCluster::ReadAttribute(const DataModel::ReadAttributeRequest & request, |
71 | | AttributeValueEncoder & encoder) |
72 | 0 | { |
73 | 0 | switch (request.path.mAttributeId) |
74 | 0 | { |
75 | 0 | case LabelList::Id: |
76 | 0 | return ReadLabelList(mPath.mEndpointId, encoder, mDeviceInfoProvider); |
77 | 0 | case ClusterRevision::Id: |
78 | 0 | return encoder.Encode(FixedLabel::kRevision); |
79 | 0 | case FeatureMap::Id: |
80 | 0 | return encoder.Encode<uint32_t>(0); |
81 | 0 | default: |
82 | 0 | return Protocols::InteractionModel::Status::UnsupportedAttribute; |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | CHIP_ERROR FixedLabelCluster::Attributes(const ConcreteClusterPath & path, |
87 | | ReadOnlyBufferBuilder<DataModel::AttributeEntry> & builder) |
88 | 0 | { |
89 | 0 | AttributeListBuilder listBuilder(builder); |
90 | 0 | return listBuilder.Append(Span(FixedLabel::Attributes::kMandatoryMetadata), {}); |
91 | 0 | } |
92 | | |
93 | | } // namespace chip::app::Clusters |