Line data Source code
1 : /*
2 : * Copyright (c) 2021-2024 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/MessageDef/AttributeReportIBs.h>
20 : #include <app/data-model/Encode.h>
21 : #include <app/data-model/FabricScoped.h>
22 : #include <app/data-model/List.h> // So we can encode lists
23 : #include <lib/core/CHIPError.h>
24 :
25 : #include <type_traits>
26 :
27 : namespace chip {
28 : namespace app {
29 :
30 : /**
31 : * The AttributeReportBuilder is a helper class for filling a single report in AttributeReportIBs.
32 : *
33 : * Possible usage of AttributeReportBuilder might be:
34 : *
35 : * AttributeReportBuilder builder;
36 : * ReturnErrorOnFailure(builder.PrepareAttribute(...));
37 : * ReturnErrorOnFailure(builder.Encode(...));
38 : * ReturnErrorOnFailure(builder.FinishAttribute());
39 : */
40 : class AttributeReportBuilder
41 : {
42 : public:
43 : /**
44 : * PrepareAttribute encodes the "header" part of an attribute report including the path and data version.
45 : * Path will be encoded according to section 10.5.4.3.1 in the spec.
46 : * Note: Only append is supported currently (encode a null list index), other operations won't encode a list index in the
47 : * attribute path field.
48 : * TODO: Add support for encoding a single element in the list (path with a valid list index).
49 : */
50 : CHIP_ERROR PrepareAttribute(AttributeReportIBs::Builder & aAttributeReportIBs, const ConcreteDataAttributePath & aPath,
51 : DataVersion aDataVersion);
52 :
53 : /**
54 : * FinishAttribute encodes the "footer" part of an attribute report (it closes the containers opened in PrepareAttribute)
55 : */
56 : CHIP_ERROR FinishAttribute(AttributeReportIBs::Builder & aAttributeReportIBs);
57 :
58 : /**
59 : * EncodeValue encodes the value field of the report, it should be called exactly once.
60 : */
61 : template <typename T, std::enable_if_t<!DataModel::IsFabricScoped<T>::value, bool> = true>
62 1921 : CHIP_ERROR EncodeValue(AttributeReportIBs::Builder & aAttributeReportIBs, TLV::Tag tag, const T & item)
63 : {
64 1921 : return DataModel::Encode(*(aAttributeReportIBs.GetAttributeReport().GetAttributeData().GetWriter()), tag, item);
65 : }
66 :
67 : template <typename T, std::enable_if_t<DataModel::IsFabricScoped<T>::value, bool> = true>
68 : CHIP_ERROR EncodeValue(AttributeReportIBs::Builder & aAttributeReportIBs, TLV::Tag tag, const T & item,
69 : FabricIndex accessingFabricIndex)
70 : {
71 : return DataModel::EncodeForRead(*(aAttributeReportIBs.GetAttributeReport().GetAttributeData().GetWriter()), tag,
72 : accessingFabricIndex, item);
73 : }
74 : };
75 :
76 : } // namespace app
77 : } // namespace chip
|