-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathThreadOperationalDataset.h
358 lines (315 loc) · 13.7 KB
/
ThreadOperationalDataset.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
*
* 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.
*/
#pragma once
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <lib/core/CHIPCore.h>
#include <lib/support/Span.h>
namespace chip {
namespace Thread {
class ThreadTLV;
inline constexpr size_t kChannel_NotSpecified = UINT8_MAX;
inline constexpr size_t kPANId_NotSpecified = UINT16_MAX;
inline constexpr size_t kSizeOperationalDataset = 254;
inline constexpr size_t kSizeNetworkName = 16;
inline constexpr size_t kSizeExtendedPanId = 8;
inline constexpr size_t kSizeMasterKey = 16;
inline constexpr size_t kSizeMeshLocalPrefix = 8;
inline constexpr size_t kSizePSKc = 16;
/**
* This class provides methods to manipulate Thread operational dataset.
*
*/
class OperationalDataset
{
public:
/**
* This method initializes the dataset with the given dataset.
*
* @param[in] aData Thread Operational dataset in octects.
*
* @retval CHIP_NO_ERROR Successfully initialized the dataset.
* @retval CHIP_ERROR_INVALID_ARGUMENT The dataset length @p aLength is too long or @p data is corrupted.
*/
CHIP_ERROR Init(ByteSpan aData);
/**
* This method retrieves Thread active timestamp from the dataset.
*
* @param[out] aActiveTimestamp A reference to receive the active timestamp.
*
* @retval CHIP_NO_ERROR Successfully retrieved the active timestamp.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND Thread active timestamp is not present in the dataset.
* @retval CHIP_ERROR_INVALID_TLV_ELEMENT If the TLV element is invalid.
*/
CHIP_ERROR GetActiveTimestamp(uint64_t & aActiveTimestamp) const;
/**
* This method sets Thread active timestamp to the dataset.
*
* @param[in] aActiveTimestamp The Thread active timestamp.
*
* @retval CHIP_NO_ERROR Successfully set the active timestamp.
* @retval CHIP_ERROR_NO_MEMORY Insufficient memory in the dataset for setting Thread active timestamp.
*/
CHIP_ERROR SetActiveTimestamp(uint64_t aActiveTimestamp);
/**
* This method retrieves Thread channel from the dataset.
*
* @param[out] aChannel A reference to receive the channel.
*
* @retval CHIP_NO_ERROR Successfully retrieved the channel.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND Thread channel is not present in the dataset.
* @retval CHIP_ERROR_INVALID_TLV_ELEMENT If the TLV element is invalid.
*/
CHIP_ERROR GetChannel(uint16_t & aChannel) const;
/**
* This method sets Thread channel to the dataset.
*
* @param[in] aChannel The Thread channel.
*
* @retval CHIP_NO_ERROR Successfully set the channel.
* @retval CHIP_ERROR_NO_MEMORY Insufficient memory in the dataset for setting Thread channel.
*/
CHIP_ERROR SetChannel(uint16_t aChannel);
/**
* This method retrieves Thread extended PAN ID from the dataset.
*
* @param[out] aExtendedPanId A reference to receive the extended PAN ID.
*
* @retval CHIP_NO_ERROR Successfully retrieved the extended PAN ID.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND Thread extended PAN ID is not present in the dataset.
* @retval CHIP_ERROR_INVALID_TLV_ELEMENT If the TLV element is invalid.
*/
CHIP_ERROR GetExtendedPanId(uint8_t (&aExtendedPanId)[kSizeExtendedPanId]) const;
/**
* This method retrieves the Thread extended PAN ID from the dataset, interpreted as a big endian number.
* @retval CHIP_NO_ERROR Successfully retrieved the extended PAN ID.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND Thread extended PAN ID is not present in the dataset.
*/
CHIP_ERROR GetExtendedPanId(uint64_t & extendedPanId) const;
/**
* This method returns a const ByteSpan to the extended PAN ID in the dataset.
* This can be used to pass the extended PAN ID to a cluster command without the use of external memory.
*
* Note: The returned span points into storage managed by this class,
* and must not be dereferenced beyond the lifetime of this object.
*
* @param[out] span A reference to receive the location of the extended PAN ID.
*
* @retval CHIP_NO_ERROR Successfully retrieved the extended PAN ID.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND Thread extended PAN ID is not present in the dataset.
* @retval CHIP_ERROR_INVALID_TLV_ELEMENT If the TLV element is invalid.
*/
CHIP_ERROR GetExtendedPanIdAsByteSpan(ByteSpan & span) const;
/**
* This method sets Thread extended PAN ID to the dataset.
*
* @param[in] aExtendedPanId The Thread extended PAN ID.
*
* @retval CHIP_NO_ERROR Successfully set the extended PAN ID.
* @retval CHIP_ERROR_NO_MEMORY Insufficient memory in the dataset for setting Thread extended PAN ID.
*/
CHIP_ERROR SetExtendedPanId(const uint8_t (&aExtendedPanId)[kSizeExtendedPanId]);
/**
* This method retrieves Thread master key from the dataset.
*
* @param[out] aMasterKey A reference to receive the master key.
*
* @retval CHIP_NO_ERROR Successfully retrieved the master key.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND Thread master key is not present in the dataset.
* @retval CHIP_ERROR_INVALID_TLV_ELEMENT If the TLV element is invalid.
*/
CHIP_ERROR GetMasterKey(uint8_t (&aMasterKey)[kSizeMasterKey]) const;
/**
* This method sets Thread master key to the dataset.
*
* @param[in] aMasterKey The Thread master key.
*
* @retval CHIP_NO_ERROR Successfully set the master key.
* @retval CHIP_ERROR_NO_MEMORY Insufficient memory in the dataset for setting Thread master key.
*/
CHIP_ERROR SetMasterKey(const uint8_t (&aMasterKey)[kSizeMasterKey]);
/**
* This method unsets Thread master key to the dataset.
*/
void UnsetMasterKey(void);
/**
* This method retrieves Thread mesh local prefix from the dataset.
*
* @param[out] aMeshLocalPrefix A reference to receive the mesh local prefix.
*
* @retval CHIP_NO_ERROR Successfully retrieved the mesh local prefix.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND Thread mesh local prefix is not present in the dataset.
* @retval CHIP_ERROR_INVALID_TLV_ELEMENT If the TLV element is invalid.
*/
CHIP_ERROR GetMeshLocalPrefix(uint8_t (&aMeshLocalPrefix)[kSizeMeshLocalPrefix]) const;
/**
* This method sets Thread mesh local prefix to the dataset.
*
* @param[in] aMeshLocalPrefix The Thread mesh local prefix.
*
* @retval CHIP_NO_ERROR Successfully set the Thread mesh local prefix.
* @retval CHIP_ERROR_NO_MEMORY Insufficient memory in the dataset for setting Thread mesh local prefix.
*/
CHIP_ERROR SetMeshLocalPrefix(const uint8_t (&aMeshLocalPrefix)[kSizeMeshLocalPrefix]);
/**
* This method retrieves Thread network name from the dataset.
*
* @param[out] aNetworkName A reference to receive the Thread network name.
*
* @retval CHIP_NO_ERROR Successfully retrieved the network name.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND Thread network name is not present in the dataset.
* @retval CHIP_ERROR_INVALID_TLV_ELEMENT If the TLV element is invalid.
*/
CHIP_ERROR GetNetworkName(char (&aNetworkName)[kSizeNetworkName + 1]) const;
/**
* This method sets Thread network name to the dataset.
*
* @param[in] aNetworkName The Thread network name.
*
* @retval CHIP_NO_ERROR Successfully set the network name.
* @retval CHIP_ERROR_NO_MEMORY Insufficient memory in the dataset for setting Thread network name.
*/
CHIP_ERROR SetNetworkName(const char * aNetworkName);
/**
* This method retrieves Thread PAN ID from the dataset.
*
* @param[out] aPanId A reference to receive the PAN ID.
*
* @retval CHIP_NO_ERROR Successfully retrieved the PAN ID.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND Thread PAN ID is not present in the dataset.
* @retval CHIP_ERROR_INVALID_TLV_ELEMENT If the TLV element is invalid.
*/
CHIP_ERROR GetPanId(uint16_t & aPanId) const;
/**
* This method sets Thread PAN ID to the dataset.
*
* @param[in] aPanId The Thread PAN ID.
*
* @retval CHIP_NO_ERROR Successfully set the PAN ID.
* @retval CHIP_ERROR_NO_MEMORY Insufficient memory in the dataset for setting Thread PAN ID.
*/
CHIP_ERROR SetPanId(uint16_t aPanId);
/**
* This method retrieves Thread PSKc from the dataset.
*
* @param[out] aPSKc A reference to receive the PSKc.
*
* @retval CHIP_NO_ERROR Successfully retrieved the PSKc.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND Thread PSKc is not present in the dataset.
* @retval CHIP_ERROR_INVALID_TLV_ELEMENT If the TLV element is invalid.
*/
CHIP_ERROR GetPSKc(uint8_t (&aPSKc)[kSizePSKc]) const;
/**
* This method sets Thread PSKc to the dataset.
*
* @param[in] aPSKc The Thread PSKc.
*
* @retval CHIP_NO_ERROR Successfully set the PSKc.
* @retval CHIP_ERROR_NO_MEMORY Insufficient memory in the dataset for setting Thread PSKc.
*/
CHIP_ERROR SetPSKc(const uint8_t (&aPSKc)[kSizePSKc]);
/**
* This method unsets Thread PSKc to the dataset.
*/
void UnsetPSKc(void);
/**
* Returns ByteSpan pointing to the channel mask within the dataset.
*
* Note: The returned span points into storage managed by this class,
* and must not be dereferenced beyond the lifetime of this object.
*
* @retval CHIP_NO_ERROR on success.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND if the channel mask is not present in the dataset.
* @retval CHIP_ERROR_INVALID_TLV_ELEMENT if the TLV element is invalid.
*/
CHIP_ERROR GetChannelMask(ByteSpan & aChannelMask) const;
/**
* This method sets the channel mask within the dataset.
*
* @retval CHIP_NO_ERROR on success.
* @retval CHIP_ERROR_NO_MEMORY if there is insufficient space within the dataset.
*/
CHIP_ERROR SetChannelMask(ByteSpan aChannelMask);
/**
* Retrieves the security policy from the dataset.
*
* @retval CHIP_NO_ERROR on success.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND if no security policy is present in the dataset.
* @retval CHIP_ERROR_INVALID_TLV_ELEMENT if the TLV element is invalid.
*/
CHIP_ERROR GetSecurityPolicy(uint32_t & aSecurityPolicy) const;
/**
* This method sets the security policy within the dataset.
*
* @retval CHIP_NO_ERROR on success.
* @retval CHIP_ERROR_NO_MEMORY if there is insufficient space within the dataset.
*/
CHIP_ERROR SetSecurityPolicy(uint32_t aSecurityPolicy);
/**
* Retrieves the delay timer from the dataset.
*
* @retval CHIP_NO_ERROR on success.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND if no security policy is present in the dataset.
* @retval CHIP_ERROR_INVALID_TLV_ELEMENT if the TLV element is invalid.
*/
CHIP_ERROR GetDelayTimer(uint32_t & aDelayMillis) const;
/**
* This method sets the delay timer within the dataset.
*
* @retval CHIP_NO_ERROR on success.
* @retval CHIP_ERROR_NO_MEMORY if there is insufficient space within the dataset.
*/
CHIP_ERROR SetDelayTimer(uint32_t aDelayMillis);
/**
* This method clears all data stored in the dataset.
*/
void Clear(void) { mLength = 0; }
/**
* This method checks if the dataset is ready for creating Thread network.
*/
bool IsCommissioned(void) const;
/**
* This method checks if the dataset is empty.
*/
bool IsEmpty() const { return mLength == 0; }
/**
* This method checks whether @p aData is formatted as ThreadTLVs.
*
* @note This method doesn't verify ThreadTLV values are valid.
*/
static bool IsValid(ByteSpan aData);
ByteSpan AsByteSpan(void) const { return ByteSpan(mData, mLength); }
private:
ThreadTLV * Locate(uint8_t aType)
{
return const_cast<ThreadTLV *>(const_cast<const OperationalDataset *>(this)->Locate(aType));
}
const ThreadTLV * Locate(uint8_t aType) const;
const ThreadTLV & Begin(void) const { return *reinterpret_cast<const ThreadTLV *>(&mData[0]); };
ThreadTLV & Begin(void) { return const_cast<ThreadTLV &>(const_cast<const OperationalDataset *>(this)->Begin()); }
const ThreadTLV & End(void) const { return *reinterpret_cast<const ThreadTLV *>(&mData[mLength]); };
ThreadTLV & End(void) { return const_cast<ThreadTLV &>(const_cast<const OperationalDataset *>(this)->End()); }
void Remove(uint8_t aType);
void Remove(ThreadTLV & aTlv);
ThreadTLV * MakeRoom(uint8_t aType, size_t aSize);
bool Has(uint8_t aType) const { return Locate(aType) != nullptr; }
uint8_t mData[kSizeOperationalDataset];
uint8_t mLength = 0;
};
} // namespace Thread
} // namespace chip