Coverage Report

Created: 2025-08-28 06:31

/src/connectedhomeip/src/app/server-cluster/AttributeListBuilder.cpp
Line
Count
Source (jump to first uncovered line)
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/AttributeListBuilder.h>
18
19
#include <app/server-cluster/DefaultServerCluster.h>
20
21
namespace chip {
22
namespace app {
23
24
CHIP_ERROR AttributeListBuilder::Append(Span<const DataModel::AttributeEntry> mandatoryAttributes,
25
                                        Span<const DataModel::AttributeEntry> optionalAttributes,
26
                                        AttributeSet enabledOptionalAttributes)
27
0
{
28
    // determine how much data to append. This should only be called if generally we have something to append
29
0
    size_t append_size = mandatoryAttributes.size();
30
0
    for (const auto & entry : optionalAttributes)
31
0
    {
32
0
        if (enabledOptionalAttributes.IsSet(entry.attributeId))
33
0
        {
34
0
            append_size++;
35
0
        }
36
0
    }
37
38
0
    if (append_size > 0)
39
0
    {
40
        // NOTE: ReferenceExisting will APPEND data (and use heap) when some data already
41
        //       exists in the builder. This is why we ensure AppendCapacity for everything
42
        //       so that we do not perform extra allocations.
43
0
        ReturnErrorOnFailure(mBuilder.EnsureAppendCapacity(append_size + DefaultServerCluster::GlobalAttributes().size()));
44
0
        ReturnErrorOnFailure(mBuilder.ReferenceExisting(mandatoryAttributes));
45
46
0
        for (const auto & entry : optionalAttributes)
47
0
        {
48
0
            if (enabledOptionalAttributes.IsSet(entry.attributeId))
49
0
            {
50
0
                ReturnErrorOnFailure(mBuilder.Append(entry));
51
0
            }
52
0
        }
53
0
    }
54
55
    // NOTE: ReferenceExisting will APPEND data (and use heap) when some data already
56
    //       exists in the builder.
57
0
    return mBuilder.ReferenceExisting(DefaultServerCluster::GlobalAttributes());
58
0
}
59
60
CHIP_ERROR AttributeListBuilder::Append(Span<const DataModel::AttributeEntry> mandatoryAttributes,
61
                                        Span<const OptionalAttributeEntry> optionalAttributes)
62
0
{
63
    // determine how much data to append. This should only be called if generally we have something to append
64
0
    size_t append_size = mandatoryAttributes.size();
65
0
    for (const auto & entry : optionalAttributes)
66
0
    {
67
0
        if (entry.enabled)
68
0
        {
69
0
            append_size++;
70
0
        }
71
0
    }
72
73
0
    if (append_size > 0)
74
0
    {
75
        // NOTE: ReferenceExisting will APPEND data (and use heap) when some data already
76
        //       exists in the builder. This is why we ensure AppendCapacity for everything
77
        //       so that we do not perform extra allocations.
78
0
        ReturnErrorOnFailure(mBuilder.EnsureAppendCapacity(append_size + DefaultServerCluster::GlobalAttributes().size()));
79
0
        ReturnErrorOnFailure(mBuilder.ReferenceExisting(mandatoryAttributes));
80
81
0
        for (const auto & entry : optionalAttributes)
82
0
        {
83
0
            if (entry.enabled)
84
0
            {
85
0
                ReturnErrorOnFailure(mBuilder.Append(entry.metadata));
86
0
            }
87
0
        }
88
0
    }
89
90
    // NOTE: ReferenceExisting will APPEND data (and use heap) when some data already
91
    //       exists in the builder.
92
0
    return mBuilder.ReferenceExisting(DefaultServerCluster::GlobalAttributes());
93
0
}
94
} // namespace app
95
} // namespace chip