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/data-model-provider/MetadataTypes.h>
20 : #include <app/server-cluster/OptionalAttributeSet.h>
21 : #include <lib/core/CHIPError.h>
22 : #include <lib/support/ReadOnlyBuffer.h>
23 :
24 : namespace chip {
25 : namespace app {
26 :
27 : /// Provides a centralized implementation of the very common operation of
28 : /// appending a list of attributes to a ReadOnlyBufferBuilder.
29 : ///
30 : /// The intent is that the more complex logic of `Append` to be shared across
31 : /// cluster implementations, so that flash size is kept small.
32 : ///
33 : /// Append handles both mandatory and optional attributes and also handles
34 : /// the required auto-add of `GlobalAttributes`.
35 : class AttributeListBuilder
36 : {
37 : public:
38 25 : AttributeListBuilder(ReadOnlyBufferBuilder<DataModel::AttributeEntry> & builder) : mBuilder(builder) {}
39 :
40 : /// Constructs a list of cluster attributes, typically for responding to a
41 : /// `ServerClusterInterface::Attributes` call.
42 : ///
43 : /// It allows for one call that will add to the buffer all of:
44 : /// - mandatoryAttributes (all of them)
45 : /// - optionalAttributes IF AND ONLY IF enabledOptionalAttributes is set for them
46 : /// - GlobalAttributes() (all of them)
47 : CHIP_ERROR Append(Span<const DataModel::AttributeEntry> mandatoryAttributes,
48 : Span<const DataModel::AttributeEntry> optionalAttributes, AttributeSet enabledOptionalAttributes);
49 :
50 : struct OptionalAttributeEntry
51 : {
52 : bool enabled; // Is this optional attribute enabled?
53 : const DataModel::AttributeEntry & metadata; // Metadata for the attribute
54 : };
55 :
56 : /// Appends the given attributes to the builder.
57 : ///
58 : /// It is very common to have a set of mandatory and a set of optional attributes for a
59 : /// cluster. This method allows for a single call to set up all of the given attributes in `builder`:
60 : /// - mandatoryAttributes
61 : /// - optionalAttributes IF AND ONLY IF they are enabled
62 : /// - all of `GlobalAttributes()`
63 : ///
64 : /// NOTE: initializing separate OptionalAttributeEntry often costs flash. Prefer using AttributeSet if possible
65 : /// (historically shown to consume less flash)
66 : CHIP_ERROR Append(Span<const DataModel::AttributeEntry> mandatoryAttributes,
67 : Span<const OptionalAttributeEntry> optionalAttributes);
68 :
69 : private:
70 : ReadOnlyBufferBuilder<DataModel::AttributeEntry> & mBuilder;
71 : };
72 :
73 : } // namespace app
74 : } // namespace chip
|