forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetupPayload.h
293 lines (249 loc) · 10.9 KB
/
SetupPayload.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
*
* Copyright (c) 2020 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* This file describes a QRCode Setup Payload class to hold
* data enumerated from a byte stream
*/
#pragma once
#include <cstdint>
#include <map>
#include <string>
#include <vector>
#include <lib/core/CHIPError.h>
#include <lib/core/Optional.h>
#include <lib/support/BitFlags.h>
#include <lib/support/SetupDiscriminator.h>
namespace chip {
// See section 5.1.2. QR Code in the Matter specification
const int kVersionFieldLengthInBits = 3;
const int kVendorIDFieldLengthInBits = 16;
const int kProductIDFieldLengthInBits = 16;
const int kCommissioningFlowFieldLengthInBits = 2;
const int kRendezvousInfoFieldLengthInBits = 8;
const int kPayloadDiscriminatorFieldLengthInBits = SetupDiscriminator::kLongBits;
const int kSetupPINCodeFieldLengthInBits = 27;
const int kPaddingFieldLengthInBits = 4;
const int kRawVendorTagLengthInBits = 7;
// See section 5.1.3. Manual Pairing Code in the Matter specification
const int kManualSetupDiscriminatorFieldLengthInBits = SetupDiscriminator::kShortBits;
const int kManualSetupChunk1DiscriminatorMsbitsPos = 0;
const int kManualSetupChunk1DiscriminatorMsbitsLength = 2;
const int kManualSetupChunk1VidPidPresentBitPos =
(kManualSetupChunk1DiscriminatorMsbitsPos + kManualSetupChunk1DiscriminatorMsbitsLength);
const int kManualSetupChunk2PINCodeLsbitsPos = 0;
const int kManualSetupChunk2PINCodeLsbitsLength = 14;
const int kManualSetupChunk2DiscriminatorLsbitsPos = (kManualSetupChunk2PINCodeLsbitsPos + kManualSetupChunk2PINCodeLsbitsLength);
const int kManualSetupChunk2DiscriminatorLsbitsLength = 2;
const int kManualSetupChunk3PINCodeMsbitsPos = 0;
const int kManualSetupChunk3PINCodeMsbitsLength = 13;
const int kManualSetupShortCodeCharLength = 10;
const int kManualSetupLongCodeCharLength = 20;
const int kManualSetupCodeChunk1CharLength = 1;
const int kManualSetupCodeChunk2CharLength = 5;
const int kManualSetupCodeChunk3CharLength = 4;
const int kManualSetupVendorIdCharLength = 5;
const int kManualSetupProductIdCharLength = 5;
// Spec 5.1.4.2 CHIP-Common Reserved Tags
inline constexpr uint8_t kSerialNumberTag = 0x00;
inline constexpr uint8_t kPBKDFIterationsTag = 0x01;
inline constexpr uint8_t kBPKFSaltTag = 0x02;
inline constexpr uint8_t kNumberOFDevicesTag = 0x03;
inline constexpr uint8_t kCommissioningTimeoutTag = 0x04;
inline constexpr uint32_t kSetupPINCodeMaximumValue = 99999998;
inline constexpr uint32_t kSetupPINCodeUndefinedValue = 0;
// clang-format off
const int kTotalPayloadDataSizeInBits =
kVersionFieldLengthInBits +
kVendorIDFieldLengthInBits +
kProductIDFieldLengthInBits +
kCommissioningFlowFieldLengthInBits +
kRendezvousInfoFieldLengthInBits +
kPayloadDiscriminatorFieldLengthInBits +
kSetupPINCodeFieldLengthInBits +
kPaddingFieldLengthInBits;
// clang-format on
const int kTotalPayloadDataSizeInBytes = kTotalPayloadDataSizeInBits / 8;
const char * const kQRCodePrefix = "MT:";
/// The rendezvous type this device supports.
enum class RendezvousInformationFlag : uint8_t
{
kNone = 0, ///< Device does not support any method for rendezvous
kSoftAP = 1 << 0, ///< Device supports Wi-Fi softAP
kBLE = 1 << 1, ///< Device supports BLE
kOnNetwork = 1 << 2, ///< Device supports Setup on network
};
using RendezvousInformationFlags = chip::BitFlags<RendezvousInformationFlag, uint8_t>;
enum class CommissioningFlow : uint8_t
{
kStandard = 0, ///< Device automatically enters pairing mode upon power-up
kUserActionRequired, ///< Device requires a user interaction to enter pairing mode
kCustom, ///< Commissioning steps should be retrieved from the distributed compliance ledger
};
/**
* A parent struct to hold onboarding payload contents without optional info,
* for compatibility with devices that don't support std::string or STL.
*/
struct PayloadContents
{
uint8_t version = 0;
uint16_t vendorID = 0;
uint16_t productID = 0;
CommissioningFlow commissioningFlow = CommissioningFlow::kStandard;
// rendezvousInformation is Optional, because a payload parsed from a manual
// numeric code would not have any rendezvousInformation available. A
// payload parsed from a QR code would always have a value for
// rendezvousInformation.
Optional<RendezvousInformationFlags> rendezvousInformation;
SetupDiscriminator discriminator;
uint32_t setUpPINCode = 0;
bool isValidQRCodePayload() const;
bool isValidManualCode() const;
bool operator==(const PayloadContents & input) const;
static bool IsValidSetupPIN(uint32_t setupPIN);
private:
bool CheckPayloadCommonConstraints() const;
};
enum optionalQRCodeInfoType
{
optionalQRCodeInfoTypeUnknown,
optionalQRCodeInfoTypeString,
optionalQRCodeInfoTypeInt32,
optionalQRCodeInfoTypeInt64,
optionalQRCodeInfoTypeUInt32,
optionalQRCodeInfoTypeUInt64
};
/**
* A structure to hold optional QR Code info
*/
struct OptionalQRCodeInfo
{
OptionalQRCodeInfo() { int32 = 0; }
/*@{*/
uint8_t tag; /**< the tag number of the optional info */
enum optionalQRCodeInfoType type; /**< the type (String or Int) of the optional info */
std::string data; /**< the string value if type is optionalQRCodeInfoTypeString, otherwise should not be set */
int32_t int32; /**< the integer value if type is optionalQRCodeInfoTypeInt, otherwise should not be set */
/*@}*/
};
struct OptionalQRCodeInfoExtension : OptionalQRCodeInfo
{
OptionalQRCodeInfoExtension()
{
int32 = 0;
int64 = 0;
uint32 = 0;
uint64 = 0;
}
int64_t int64;
uint64_t uint32;
uint64_t uint64;
};
class SetupPayload : public PayloadContents
{
friend class QRCodeSetupPayloadGenerator;
friend class QRCodeSetupPayloadParser;
public:
/** @brief A function to add an optional vendor data
* @param tag tag number in the [0x80-0xFF] range
* @param data String representation of data to add
* @return Returns a CHIP_ERROR on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR addOptionalVendorData(uint8_t tag, std::string data);
/** @brief A function to add an optional vendor data
* @param tag 7 bit [0-127] tag number
* @param data Integer representation of data to add
* @return Returns a CHIP_ERROR on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR addOptionalVendorData(uint8_t tag, int32_t data);
/** @brief A function to remove an optional vendor data
* @param tag 7 bit [0-127] tag number
* @return Returns a CHIP_ERROR_KEY_NOT_FOUND on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR removeOptionalVendorData(uint8_t tag);
/**
* @brief A function to retrieve the vector of OptionalQRCodeInfo infos
* @return Returns a vector of optionalQRCodeInfos
**/
std::vector<OptionalQRCodeInfo> getAllOptionalVendorData() const;
/** @brief A function to add a string serial number
* @param serialNumber string serial number
* @return Returns a CHIP_ERROR on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR addSerialNumber(std::string serialNumber);
/** @brief A function to add a uint32_t serial number
* @param serialNumber uint32_t serial number
* @return Returns a CHIP_ERROR on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR addSerialNumber(uint32_t serialNumber);
/** @brief A function to retrieve serial number as a string
* @param outSerialNumber retrieved string serial number
* @return Returns a CHIP_ERROR on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR getSerialNumber(std::string & outSerialNumber) const;
/** @brief A function to remove the serial number from the payload
* @return Returns a CHIP_ERROR_KEY_NOT_FOUND on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR removeSerialNumber();
bool operator==(const SetupPayload & input) const;
private:
std::map<uint8_t, OptionalQRCodeInfo> optionalVendorData;
std::map<uint8_t, OptionalQRCodeInfoExtension> optionalExtensionData;
/** @brief Checks if the tag is CHIP Common type
* @param tag Tag to be checked
* @return Returns True if the tag is of Common type
**/
static bool IsCommonTag(uint8_t tag);
/** @brief Checks if the tag is vendor-specific
* @param tag Tag to be checked
* @return Returns True if the tag is Vendor-specific
**/
static bool IsVendorTag(uint8_t tag);
/** @brief A function to add an optional QR Code info vendor object
* @param info Optional QR code info object to add
* @return Returns a CHIP_ERROR_INVALID_ARGUMENT on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR addOptionalVendorData(const OptionalQRCodeInfo & info);
/** @brief A function to add an optional QR Code info CHIP object
* @param info Optional QR code info object to add
* @return Returns a CHIP_ERROR_INVALID_ARGUMENT on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR addOptionalExtensionData(const OptionalQRCodeInfoExtension & info);
/**
* @brief A function to retrieve the vector of CHIPQRCodeInfo infos
* @return Returns a vector of CHIPQRCodeInfos
**/
std::vector<OptionalQRCodeInfoExtension> getAllOptionalExtensionData() const;
/** @brief A function to retrieve an optional QR Code info vendor object
* @param tag 7 bit [0-127] tag number
* @param info retrieved OptionalQRCodeInfo object
* @return Returns a CHIP_ERROR_KEY_NOT_FOUND on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR getOptionalVendorData(uint8_t tag, OptionalQRCodeInfo & info) const;
/** @brief A function to retrieve an optional QR Code info extended object
* @param tag 8 bit [128-255] tag number
* @param info retrieved OptionalQRCodeInfoExtension object
* @return Returns a CHIP_ERROR_KEY_NOT_FOUND on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR getOptionalExtensionData(uint8_t tag, OptionalQRCodeInfoExtension & info) const;
/** @brief A function to retrieve the associated expected numeric value for a tag
* @param tag 8 bit [0-255] tag number
* @return Returns an optionalQRCodeInfoType value
**/
optionalQRCodeInfoType getNumericTypeFor(uint8_t tag) const;
};
} // namespace chip