Line data Source code
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 :
27 : #if CHIP_HAVE_CONFIG_H
28 : #include <setup_payload/CHIPAdditionalDataPayloadBuildConfig.h>
29 : #endif
30 :
31 : #include <lib/core/CHIPError.h>
32 : #include <lib/support/BitFlags.h>
33 : #include <system/TLVPacketBufferBackingStore.h>
34 :
35 : namespace chip {
36 : namespace RotatingDeviceId {
37 : static constexpr unsigned kLifetimeCounterSize = 2;
38 : static constexpr unsigned kHashSuffixLength = 16;
39 : static constexpr unsigned kMaxLength = kLifetimeCounterSize + kHashSuffixLength;
40 : static constexpr unsigned kHexMaxLength = kMaxLength * 2 + 1;
41 : } // namespace RotatingDeviceId
42 :
43 : enum class AdditionalDataFields : int8_t
44 : {
45 : NotSpecified = 0x00,
46 : RotatingDeviceId = 0x01
47 : };
48 :
49 : struct AdditionalDataPayloadGeneratorParams
50 : {
51 : #if CHIP_ENABLE_ROTATING_DEVICE_ID
52 : uint16_t rotatingDeviceIdLifetimeCounter;
53 : ByteSpan rotatingDeviceIdUniqueId;
54 : #endif
55 : };
56 :
57 : class AdditionalDataPayloadGenerator
58 : {
59 :
60 : public:
61 1 : AdditionalDataPayloadGenerator() {}
62 :
63 : /**
64 : * Generate additional data payload (i.e. TLV encoded).
65 : *
66 : * @param params parameters needed to generate additional data payload
67 : * @param bufferHandle output buffer handle
68 : * @param additionalDataFields bitfield for what fields should be generated in the additional data
69 : *
70 : * @retval #CHIP_ERROR_INVALID_TLV_TAG
71 : * If the specified tag value is invalid or inappropriate in the context
72 : * in which the value is being written.
73 : * @retval #CHIP_ERROR_BUFFER_TOO_SMALL
74 : * If writing the value would exceed the limit on the maximum number of
75 : * bytes specified when the writer was initialized.
76 : * @retval #CHIP_ERROR_NO_MEMORY
77 : * If an attempt to allocate an output buffer failed due to lack of
78 : * memory.
79 : * @retval other Other CHIP or platform-specific errors returned by the configured
80 : * TLVBackingStore
81 : *
82 : */
83 : CHIP_ERROR generateAdditionalDataPayload(AdditionalDataPayloadGeneratorParams & params,
84 : chip::System::PacketBufferHandle & bufferHandle,
85 : BitFlags<AdditionalDataFields> additionalDataFields);
86 :
87 : #if CHIP_ENABLE_ROTATING_DEVICE_ID
88 : /**
89 : * Generate Rotating Device ID in Binary Format
90 : *
91 : * @param params parameters needed to generate additional data payload
92 : * @param [in,out] rotatingDeviceIdBuffer as input, the buffer to use for
93 : * the binary data. As output, will have its size set to
94 : * the actual size used upon successful generation
95 : */
96 : CHIP_ERROR generateRotatingDeviceIdAsBinary(AdditionalDataPayloadGeneratorParams & params,
97 : MutableByteSpan & rotatingDeviceIdBuffer);
98 :
99 : /**
100 : * Generate Device Rotating ID in String Format
101 : *
102 : * @param params parameters needed to generate additional data payload
103 : * @param rotatingDeviceIdBuffer rotating device id buffer
104 : * @param rotatingDeviceIdBufferSize the current size of the supplied buffer
105 : * @param rotatingDeviceIdValueOutputSize the number of chars making up the actual value of the returned rotating device id
106 : * excluding the null terminator
107 : *
108 : * @return Returns a CHIP_ERROR on error, CHIP_NO_ERROR otherwise.
109 : *
110 : */
111 : CHIP_ERROR generateRotatingDeviceIdAsHexString(AdditionalDataPayloadGeneratorParams & params, char * rotatingDeviceIdBuffer,
112 : size_t rotatingDeviceIdBufferSize, size_t & rotatingDeviceIdValueOutputSize);
113 : #endif
114 : };
115 :
116 : } // namespace chip
|