6b48edaad3f10e7749674784f96c9bb4d1087375
[platform/upstream/connectedhomeip.git] / src / setup_payload / AdditionalDataPayloadGenerator.h
1 /**
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
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
18 /**
19  *    @file
20  *      This file provides a utility to generate Additional Data payload and its members
21  *      (e.g. rotating device id)
22  *
23  */
24
25 #pragma once
26 #include <core/CHIPError.h>
27 #include <support/BitFlags.h>
28 #include <system/TLVPacketBufferBackingStore.h>
29
30 namespace chip {
31 namespace RotatingDeviceId {
32 static constexpr unsigned kLifetimeCounterSize = 2;
33 static constexpr unsigned kHashSuffixLength    = 16;
34 static constexpr unsigned kMaxLength           = kLifetimeCounterSize + kHashSuffixLength;
35 static constexpr unsigned kHexMaxLength        = kMaxLength * 2 + 1;
36 } // namespace RotatingDeviceId
37
38 enum class AdditionalDataFields : int8_t
39 {
40     NotSpecified     = 0x00,
41     RotatingDeviceId = 0x01
42 };
43
44 class AdditionalDataPayloadGenerator
45 {
46
47 public:
48     AdditionalDataPayloadGenerator() {}
49
50     /**
51      * Generate additional data payload (i.e. TLV encoded).
52      *
53      * @param lifetimeCounter lifetime counter
54      * @param serialNumberBuffer null-terminated serial number buffer
55      * @param serialNumberBufferSize size of the serial number buffer supplied.
56      * @param bufferHandle output buffer handle
57      * @param additionalDataFields bitfield for what fields should be generated in the additional data
58      *
59      * @retval #CHIP_ERROR_INVALID_TLV_TAG
60      *                              If the specified tag value is invalid or inappropriate in the context
61      *                              in which the value is being written.
62      * @retval #CHIP_ERROR_BUFFER_TOO_SMALL
63      *                              If writing the value would exceed the limit on the maximum number of
64      *                              bytes specified when the writer was initialized.
65      * @retval #CHIP_ERROR_NO_MEMORY
66      *                              If an attempt to allocate an output buffer failed due to lack of
67      *                              memory.
68      * @retval other                Other CHIP or platform-specific errors returned by the configured
69      *                              TLVBackingStore
70      *
71      */
72     CHIP_ERROR generateAdditionalDataPayload(uint16_t lifetimeCounter, const char * serialNumberBuffer,
73                                              size_t serialNumberBufferSize, chip::System::PacketBufferHandle & bufferHandle,
74                                              BitFlags<uint8_t, AdditionalDataFields> additionalDataFields);
75     // Generate Device Rotating ID
76     /**
77      * Generate additional data payload (i.e. TLV encoded).
78      *
79      * @param lifetimeCounter lifetime counter
80      * @param serialNumberBuffer null-terminated serial number buffer
81      * @param serialNumberBufferSize size of the serial number buffer supplied.
82      * @param rotatingDeviceIdBuffer rotating device id buffer
83      * @param rotatingDeviceIdBufferSize the current size of the supplied buffer
84      * @param rotatingDeviceIdValueOutputSize the number of chars making up the actual value of the returned rotating device id
85      *
86      * @return Returns a CHIP_ERROR on error, CHIP_NO_ERROR otherwise.
87      *
88      */
89     CHIP_ERROR generateRotatingDeviceId(uint16_t lifetimeCounter, const char * serialNumberBuffer, size_t serialNumberBufferSize,
90                                         char * rotatingDeviceIdBuffer, size_t rotatingDeviceIdBufferSize,
91                                         size_t & rotatingDeviceIdValueOutputSize);
92 };
93
94 } // namespace chip