forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTxtFields.cpp
299 lines (256 loc) · 8.69 KB
/
TxtFields.cpp
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
294
295
296
297
298
299
/*
*
* Copyright (c) 2021 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.
*/
#include "TxtFields.h"
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdio>
#include <inttypes.h>
#include <limits>
#include <stdlib.h>
#include <string.h>
#include <lib/core/CHIPSafeCasts.h>
#include <lib/dnssd/Advertiser.h>
#include <lib/dnssd/Resolver.h>
#include <lib/support/BytesToHex.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/SafeInt.h>
namespace chip {
namespace Dnssd {
namespace Internal {
constexpr uint8_t kTCPClient = 1;
constexpr uint8_t kTCPServer = 2;
namespace {
char SafeToLower(uint8_t ch)
{
return static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
}
bool IsKey(const ByteSpan & key, const char * desired)
{
if (key.size() != strlen(desired))
{
return false;
}
auto desired_bytes = Uint8::from_const_char(desired);
for (size_t i = 0; i < key.size(); ++i)
{
if (SafeToLower(key.data()[i]) != SafeToLower(desired_bytes[i]))
{
return false;
}
}
return true;
}
uint32_t MakeU32FromAsciiDecimal(const ByteSpan & val, uint32_t defaultValue = 0)
{
// +1 because `digits10` means the number of decimal digits that fit in `uint32_t`,
// not how many digits are enough to represent any `uint32_t`
// +1 for null-terminator
char nullTerminatedValue[std::numeric_limits<uint32_t>::digits10 + 2];
// value is too long to store `uint32_t`
if (val.size() >= sizeof(nullTerminatedValue))
return defaultValue;
// value contains leading zeros
if (val.size() > 1 && *val.data() == static_cast<uint8_t>('0'))
return defaultValue;
Platform::CopyString(nullTerminatedValue, sizeof(nullTerminatedValue), val);
char * endPtr;
unsigned long num = strtoul(nullTerminatedValue, &endPtr, 10);
if (endPtr > nullTerminatedValue && *endPtr == '\0' && num != ULONG_MAX && CanCastTo<uint32_t>(num))
return static_cast<uint32_t>(num);
return defaultValue;
}
uint16_t MakeU16FromAsciiDecimal(const ByteSpan & val)
{
const uint32_t num = MakeU32FromAsciiDecimal(val);
return CanCastTo<uint16_t>(num) ? static_cast<uint16_t>(num) : 0;
}
uint8_t MakeU8FromAsciiDecimal(const ByteSpan & val)
{
const uint32_t num = MakeU32FromAsciiDecimal(val);
return CanCastTo<uint8_t>(num) ? static_cast<uint8_t>(num) : 0;
}
bool MakeBoolFromAsciiDecimal(const ByteSpan & val)
{
return val.size() == 1 && static_cast<char>(*val.data()) == '1';
}
std::optional<bool> MakeOptionalBoolFromAsciiDecimal(const ByteSpan & val)
{
char character = static_cast<char>(*val.data());
if (val.size() == 1 && ((character == '1') || (character == '0')))
{
return std::make_optional(character == '1');
}
return std::nullopt;
}
size_t GetPlusSignIdx(const ByteSpan & value)
{
// First value is the vendor id, second (after the +) is the product.
for (size_t i = 0; i < value.size(); ++i)
{
if (static_cast<char>(value.data()[i]) == '+')
{
return i;
}
}
return value.size();
}
} // namespace
uint16_t GetProduct(const ByteSpan & value)
{
size_t plussign = GetPlusSignIdx(value);
if (plussign < value.size() - 1)
{
const uint8_t * productStrStart = value.data() + plussign + 1;
size_t productStrLen = value.size() - plussign - 1;
return MakeU16FromAsciiDecimal(ByteSpan(productStrStart, productStrLen));
}
return 0;
}
uint16_t GetVendor(const ByteSpan & value)
{
size_t plussign = GetPlusSignIdx(value);
return MakeU16FromAsciiDecimal(ByteSpan(value.data(), plussign));
}
uint16_t GetLongDiscriminator(const ByteSpan & value)
{
return MakeU16FromAsciiDecimal(value);
}
uint8_t GetCommissioningMode(const ByteSpan & value)
{
return MakeU8FromAsciiDecimal(value);
}
uint32_t GetDeviceType(const ByteSpan & value)
{
return MakeU32FromAsciiDecimal(value);
}
void GetDeviceName(const ByteSpan & value, char * name)
{
Platform::CopyString(name, kMaxDeviceNameLen + 1, value);
}
void GetRotatingDeviceId(const ByteSpan & value, uint8_t * rotatingId, size_t * len)
{
*len = Encoding::HexToBytes(reinterpret_cast<const char *>(value.data()), value.size(), rotatingId, kMaxRotatingIdLen);
}
uint16_t GetPairingHint(const ByteSpan & value)
{
return MakeU16FromAsciiDecimal(value);
}
void GetPairingInstruction(const ByteSpan & value, char * pairingInstruction)
{
Platform::CopyString(pairingInstruction, kMaxPairingInstructionLen + 1, value);
}
uint8_t GetCommissionerPasscode(const ByteSpan & value)
{
return MakeBoolFromAsciiDecimal(value);
}
std::optional<System::Clock::Milliseconds32> GetRetryInterval(const ByteSpan & value)
{
const auto undefined = std::numeric_limits<uint32_t>::max();
const auto retryInterval = MakeU32FromAsciiDecimal(value, undefined);
if (retryInterval != undefined && retryInterval <= kMaxRetryInterval.count())
return std::make_optional(System::Clock::Milliseconds32(retryInterval));
return std::nullopt;
}
std::optional<System::Clock::Milliseconds16> GetRetryActiveThreshold(const ByteSpan & value)
{
const auto retryInterval = MakeU16FromAsciiDecimal(value);
if (retryInterval == 0)
{
return std::nullopt;
}
return std::make_optional(System::Clock::Milliseconds16(retryInterval));
}
TxtFieldKey GetTxtFieldKey(const ByteSpan & key)
{
for (auto & info : txtFieldInfo)
{
if (IsKey(key, info.keyStr))
{
return info.key;
}
}
return TxtFieldKey::kUnknown;
}
} // namespace Internal
void FillNodeDataFromTxt(const ByteSpan & key, const ByteSpan & val, CommissionNodeData & nodeData)
{
TxtFieldKey keyType = Internal::GetTxtFieldKey(key);
switch (keyType)
{
case TxtFieldKey::kLongDiscriminator:
nodeData.longDiscriminator = Internal::GetLongDiscriminator(val);
break;
case TxtFieldKey::kVendorProduct:
nodeData.vendorId = Internal::GetVendor(val);
nodeData.productId = Internal::GetProduct(val);
break;
case TxtFieldKey::kCommissioningMode:
nodeData.commissioningMode = Internal::GetCommissioningMode(val);
break;
case TxtFieldKey::kDeviceType:
nodeData.deviceType = Internal::GetDeviceType(val);
break;
case TxtFieldKey::kDeviceName:
Internal::GetDeviceName(val, nodeData.deviceName);
break;
case TxtFieldKey::kRotatingDeviceId:
Internal::GetRotatingDeviceId(val, nodeData.rotatingId, &nodeData.rotatingIdLen);
break;
case TxtFieldKey::kPairingInstruction:
Internal::GetPairingInstruction(val, nodeData.pairingInstruction);
break;
case TxtFieldKey::kPairingHint:
nodeData.pairingHint = Internal::GetPairingHint(val);
break;
case TxtFieldKey::kCommissionerPasscode:
nodeData.supportsCommissionerGeneratedPasscode = Internal::GetCommissionerPasscode(val);
break;
default:
FillNodeDataFromTxt(key, val, static_cast<CommonResolutionData &>(nodeData));
break;
}
}
void FillNodeDataFromTxt(const ByteSpan & key, const ByteSpan & value, CommonResolutionData & nodeData)
{
switch (Internal::GetTxtFieldKey(key))
{
case TxtFieldKey::kSessionIdleInterval:
nodeData.mrpRetryIntervalIdle = Internal::GetRetryInterval(value);
break;
case TxtFieldKey::kSessionActiveInterval:
nodeData.mrpRetryIntervalActive = Internal::GetRetryInterval(value);
break;
case TxtFieldKey::kSessionActiveThreshold:
nodeData.mrpRetryActiveThreshold = Internal::GetRetryActiveThreshold(value);
break;
case TxtFieldKey::kTcpSupported: {
// bit 0 is reserved and deprecated
uint8_t support = Internal::MakeU8FromAsciiDecimal(value);
nodeData.supportsTcpClient = (support & (1 << Internal::kTCPClient)) != 0;
nodeData.supportsTcpServer = (support & (1 << Internal::kTCPServer)) != 0;
break;
}
case TxtFieldKey::kLongIdleTimeICD:
nodeData.isICDOperatingAsLIT = Internal::MakeOptionalBoolFromAsciiDecimal(value);
break;
default:
break;
}
}
} // namespace Dnssd
} // namespace chip