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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
/*
* Copyright (c) 2016, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* This file includes definitions for MLE functionality required by the Thread Child, Router, and Leader roles.
*/
#ifndef MLE_HPP_
#define MLE_HPP_
#include "openthread-core-config.h"
#include "common/encoding.hpp"
#include "common/locator.hpp"
#include "common/timer.hpp"
#include "mac/mac.hpp"
#include "meshcop/joiner_router.hpp"
#include "net/udp6.hpp"
#include "thread/mle_tlvs.hpp"
#include "thread/mle_types.hpp"
#include "thread/topology.hpp"
namespace ot {
/**
* @addtogroup core-mle MLE
*
* @brief
* This module includes definitions for the MLE protocol.
*
* @{
*
* @defgroup core-mle-core Core
* @defgroup core-mle-router Router
* @defgroup core-mle-tlvs TLVs
*
* @}
*/
/**
* @namespace ot::Mle
*
* @brief
* This namespace includes definitions for the MLE protocol.
*/
namespace Mle {
/**
* @addtogroup core-mle-core
*
* @brief
* This module includes definitions for MLE functionality required by the Thread Child, Router, and Leader roles.
*
* @{
*
*/
/**
* This class implements MLE Header generation and parsing.
*
*/
OT_TOOL_PACKED_BEGIN
class Header
{
public:
/**
* MLE Command Types.
*
*/
enum Command
{
kCommandLinkRequest = 0, ///< Link Request
kCommandLinkAccept = 1, ///< Link Accept
kCommandLinkAcceptAndRequest = 2, ///< Link Accept and Reject
kCommandLinkReject = 3, ///< Link Reject
kCommandAdvertisement = 4, ///< Advertisement
kCommandUpdate = 5, ///< Update
kCommandUpdateRequest = 6, ///< Update Request
kCommandDataRequest = 7, ///< Data Request
kCommandDataResponse = 8, ///< Data Response
kCommandParentRequest = 9, ///< Parent Request
kCommandParentResponse = 10, ///< Parent Response
kCommandChildIdRequest = 11, ///< Child ID Request
kCommandChildIdResponse = 12, ///< Child ID Response
kCommandChildUpdateRequest = 13, ///< Child Update Request
kCommandChildUpdateResponse = 14, ///< Child Update Response
kCommandAnnounce = 15, ///< Announce
kCommandDiscoveryRequest = 16, ///< Discovery Request
kCommandDiscoveryResponse = 17, ///< Discovery Response
kCommandTimeSync = 99, ///< Time Sync (applicable when OPENTHREAD_CONFIG_TIME_SYNC_ENABLE enabled)
};
/**
* MLE Security Suite
*
*/
enum SecuritySuite
{
k154Security = 0, ///< IEEE 802.15.4-2006 security.
kNoSecurity = 255, ///< No security enabled.
};
/**
* This method initializes the MLE header.
*
*/
void Init(void)
{
mSecuritySuite = k154Security;
mSecurityControl = Mac::Frame::kSecEncMic32;
}
/**
* This method indicates whether or not the TLV appears to be well-formed.
*
* @retval TRUE If the TLV appears to be well-formed.
* @retval FALSE If the TLV does not appear to be well-formed.
*
*/
bool IsValid(void) const
{
return (mSecuritySuite == kNoSecurity) ||
(mSecuritySuite == k154Security &&
mSecurityControl == (Mac::Frame::kKeyIdMode2 | Mac::Frame::kSecEncMic32));
}
/**
* This method returns the MLE header and Command Type length.
*
* @returns The MLE header and Command Type length.
*
*/
uint8_t GetLength(void) const
{
return sizeof(mSecuritySuite) + sizeof(mCommand) +
((mSecuritySuite == k154Security)
? sizeof(mSecurityControl) + sizeof(mFrameCounter) + sizeof(mKeySource) + sizeof(mKeyIndex)
: 0);
}
/**
* This method returns the Security Suite value.
*
* @returns The Security Suite value.
*
*/
SecuritySuite GetSecuritySuite(void) const { return static_cast<SecuritySuite>(mSecuritySuite); }
/**
* This method sets the Security Suite value.
*
* @param[in] aSecuritySuite The Security Suite value.
*
*/
void SetSecuritySuite(SecuritySuite aSecuritySuite) { mSecuritySuite = static_cast<uint8_t>(aSecuritySuite); }
/**
* This method returns the MLE header length (excluding the Command Type).
*
* @returns The MLE header length (excluding the Command Type).
*
*/
uint8_t GetHeaderLength(void) const
{
return sizeof(mSecurityControl) + sizeof(mFrameCounter) + sizeof(mKeySource) + sizeof(mKeyIndex);
}
/**
* This method returns a pointer to first byte of the MLE header.
*
* @returns A pointer to the first byte of the MLE header.
*
*/
const uint8_t *GetBytes(void) const { return reinterpret_cast<const uint8_t *>(&mSecuritySuite); }
/**
* This method returns the Security Control value.
*
* @returns The Security Control value.
*
*/
uint8_t GetSecurityControl(void) const { return mSecurityControl; }
/**
* This method indicates whether or not the Key ID Mode is set to 2.
*
* @retval TRUE If the Key ID Mode is set to 2.
* @retval FALSE If the Key ID Mode is not set to 2.
*
*/
bool IsKeyIdMode2(void) const { return (mSecurityControl & Mac::Frame::kKeyIdModeMask) == Mac::Frame::kKeyIdMode2; }
/**
* This method sets the Key ID Mode to 2.
*
*/
void SetKeyIdMode2(void)
{
mSecurityControl = (mSecurityControl & ~Mac::Frame::kKeyIdModeMask) | Mac::Frame::kKeyIdMode2;
}
/**
* This method returns the Key ID value.
*
* @returns The Key ID value.
*
*/
uint32_t GetKeyId(void) const { return Encoding::BigEndian::HostSwap32(mKeySource); }
/**
* This method sets the Key ID value.
*
* @param[in] aKeySequence The Key ID value.
*
*/
void SetKeyId(uint32_t aKeySequence)
{
mKeySource = Encoding::BigEndian::HostSwap32(aKeySequence);
mKeyIndex = (aKeySequence & 0x7f) + 1;
}
/**
* This method returns the Frame Counter value.
*
* @returns The Frame Counter value.
*
*/
uint32_t GetFrameCounter(void) const { return Encoding::LittleEndian::HostSwap32(mFrameCounter); }
/**
* This method sets the Frame Counter value.
*
* @param[in] aFrameCounter The Frame Counter value.
*
*/
void SetFrameCounter(uint32_t aFrameCounter) { mFrameCounter = Encoding::LittleEndian::HostSwap32(aFrameCounter); }
/**
* This method returns the Command Type value.
*
* @returns The Command Type value.
*
*/
Command GetCommand(void) const
{
return static_cast<Command>((mSecuritySuite == kNoSecurity) ? mSecurityControl : mCommand);
}
/**
* This method sets the Command Type value.
*
* @param[in] aCommand The Command Type value.
*
*/
void SetCommand(Command aCommand)
{
if (mSecuritySuite == kNoSecurity)
{
mSecurityControl = static_cast<uint8_t>(aCommand);
}
else
{
mCommand = static_cast<uint8_t>(aCommand);
}
}
private:
uint8_t mSecuritySuite;
uint8_t mSecurityControl;
uint32_t mFrameCounter;
uint32_t mKeySource;
uint8_t mKeyIndex;
uint8_t mCommand;
} OT_TOOL_PACKED_END;
/**
* This class implements MLE functionality required by the Thread EndDevices, Router, and Leader roles.
*
*/
class Mle : public InstanceLocator
{
public:
/**
* This constructor initializes the MLE object.
*
* @param[in] aInstance A reference to the OpenThread instance.
*
*/
explicit Mle(Instance &aInstance);
/**
* This method enables MLE.
*
* @retval OT_ERROR_NONE Successfully enabled MLE.
* @retval OT_ERROR_ALREADY MLE was already enabled.
*
*/
otError Enable(void);
/**
* This method disables MLE.
*
* @retval OT_ERROR_NONE Successfully disabled MLE.
*
*/
otError Disable(void);
/**
* This method starts the MLE protocol operation.
*
* @param[in] aAnnounceAttach True if attach on the announced thread network with newer active timestamp,
* or False if not.
*
* @retval OT_ERROR_NONE Successfully started the protocol operation.
* @retval OT_ERROR_ALREADY The protocol operation was already started.
*
*/
otError Start(bool aAnnounceAttach);
/**
* This method stops the MLE protocol operation.
*
* @param[in] aClearNetworkDatasets True to clear network datasets, False not.
*
*/
void Stop(bool aClearNetworkDatasets);
/**
* This method restores network information from non-volatile memory.
*
* @retval OT_ERROR_NONE Successfully restore the network information.
* @retval OT_ERROR_NOT_FOUND There is no valid network information stored in non-volatile memory.
*
*/
otError Restore(void);
/**
* This method stores network information into non-volatile memory.
*
* @retval OT_ERROR_NONE Successfully store the network information.
* @retval OT_ERROR_NO_BUFS Could not store the network information due to insufficient memory space.
*
*/
otError Store(void);
/**
* This function pointer is called on receiving an MLE Discovery Response message.
*
* @param[in] aResult A valid pointer to the Discovery Response information or NULL when the Discovery completes.
* @param[in] aContext A pointer to application-specific context.
*
*/
typedef void (*DiscoverHandler)(otActiveScanResult *aResult, void *aContext);
/**
* This method initiates a Thread Discovery.
*
* @param[in] aScanChannels A bit vector indicating which channels to scan.
* @param[in] aPanId The PAN ID filter (set to Broadcast PAN to disable filter).
* @param[in] aJoiner Value of the Joiner Flag in the Discovery Request TLV.
* @param[in] aEnableFiltering Enable filtering out MLE discovery responses that don't match our factory
* assigned EUI64.
* @param[in] aHandler A pointer to a function that is called on receiving an MLE Discovery Response.
* @param[in] aContext A pointer to arbitrary context information.
*
* @retval OT_ERROR_NONE Successfully started a Thread Discovery.
* @retval OT_ERROR_BUSY Thread Discovery is already in progress.
*
*/
otError Discover(const Mac::ChannelMask &aScanChannels,
uint16_t aPanId,
bool aJoiner,
bool aEnableFiltering,
DiscoverHandler aCallback,
void * aContext);
/**
* This method indicates whether or not an MLE Thread Discovery is currently in progress.
*
* @returns true if an MLE Thread Discovery is in progress, false otherwise.
*
*/
bool IsDiscoverInProgress(void) const { return mDiscoverInProgress; }
/**
* This method is called by the MeshForwarder to indicate that discovery is complete.
*
*/
void HandleDiscoverComplete(void);
/**
* This method generates an MLE Announce message.
*
* @param[in] aChannel The channel to use when transmitting.
* @param[in] aOrphanAnnounce To indicate if MLE Announce is sent from an orphan end device.
*
*/
void SendAnnounce(uint8_t aChannel, bool aOrphanAnnounce);
/**
* This method causes the Thread interface to detach from the Thread network.
*
* @retval OT_ERROR_NONE Successfully detached from the Thread network.
* @retval OT_ERROR_INVALID_STATE MLE is Disabled.
*
*/
otError BecomeDetached(void);
/**
* This method causes the Thread interface to attempt an MLE attach.
*
* @param[in] aMode Indicates what partitions to attach to.
*
* @retval OT_ERROR_NONE Successfully began the attach process.
* @retval OT_ERROR_INVALID_STATE MLE is Disabled.
* @retval OT_ERROR_BUSY An attach process is in progress.
*
*/
otError BecomeChild(AttachMode aMode);
/**
* This method indicates whether or not the Thread device is attached to a Thread network.
*
* @retval TRUE Attached to a Thread network.
* @retval FALSE Not attached to a Thread network.
*
*/
bool IsAttached(void) const;
/**
* This method indicates whether device is currently attaching or not.
*
* Note that an already attached device may also be in attaching state. Examples of this include a leader/router
* trying to attach to a better partition, or a child trying to find a better parent (when feature
* `OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE` is enabled).
*
* @retval TRUE Device is currently trying to attach.
* @retval FALSE Device is not in middle of attach process.
*
*/
bool IsAttaching(void) const { return (mAttachState != kAttachStateIdle); }
/**
* This method returns the current Thread device role.
*
* @returns The current Thread device role.
*
*/
DeviceRole GetRole(void) const { return mRole; }
/**
* This method indicates whether device role is disabled.
*
* @retval TRUE Device role is disabled.
* @retval FALSE Device role is not disabled.
*
*/
bool IsDisabled(void) const { return (mRole == kRoleDisabled); }
/**
* This method indicates whether device role is detached.
*
* @retval TRUE Device role is detached.
* @retval FALSE Device role is not detached.
*
*/
bool IsDetached(void) const { return (mRole == kRoleDetached); }
/**
* This method indicates whether device role is child.
*
* @retval TRUE Device role is child.
* @retval FALSE Device role is not child.
*
*/
bool IsChild(void) const { return (mRole == kRoleChild); }
/**
* This method indicates whether device role is router.
*
* @retval TRUE Device role is router.
* @retval FALSE Device role is not router.
*
*/
bool IsRouter(void) const { return (mRole == kRoleRouter); }
/**
* This method indicates whether device role is leader.
*
* @retval TRUE Device role is leader.
* @retval FALSE Device role is not leader.
*
*/
bool IsLeader(void) const { return (mRole == kRoleLeader); }
/**
* This method indicates whether device role is either router or leader.
*
* @retval TRUE Device role is either router or leader.
* @retval FALSE Device role is neither router nor leader.
*
*/
bool IsRouterOrLeader(void) const;
/**
* This method returns the Device Mode as reported in the Mode TLV.
*
* @returns The Device Mode as reported in the Mode TLV.
*
*/
DeviceMode GetDeviceMode(void) const { return mDeviceMode; }
/**
* This method sets the Device Mode as reported in the Mode TLV.
*
* @param[in] aDeviceMode The device mode to set.
*
* @retval OT_ERROR_NONE Successfully set the Mode TLV.
* @retval OT_ERROR_INVALID_ARGS The mode combination specified in @p aMode is invalid.
*
*/
otError SetDeviceMode(DeviceMode aDeviceMode);
/**
* This method indicates whether or not the device is rx-on-when-idle.
*
* @returns TRUE if rx-on-when-idle, FALSE otherwise.
*
*/
bool IsRxOnWhenIdle(void) const { return mDeviceMode.IsRxOnWhenIdle(); }
/**
* This method indicates whether or not the device is a Full Thread Device.
*
* @returns TRUE if a Full Thread Device, FALSE otherwise.
*
*/
bool IsFullThreadDevice(void) const { return mDeviceMode.IsFullThreadDevice(); }
/**
* This method indicates whether or not the device uses secure IEEE 802.15.4 Data Request messages.
*
* @returns TRUE if using secure IEEE 802.15.4 Data Request messages, FALSE otherwise.
*
*/
bool IsSecureDataRequest(void) const { return mDeviceMode.IsSecureDataRequest(); }
/**
* This method indicates whether or not the device requests Full Network Data.
*
* @returns TRUE if requests Full Network Data, FALSE otherwise.
*
*/
bool IsFullNetworkData(void) const { return mDeviceMode.IsFullNetworkData(); }
/**
* This method indicates whether or not the device is a Minimal End Device.
*
* @returns TRUE if the device is a Minimal End Device, FALSE otherwise.
*
*/
bool IsMinimalEndDevice(void) const { return mDeviceMode.IsMinimalEndDevice(); }
/**
* This method returns a pointer to the Mesh Local Prefix.
*
* @returns A reference to the Mesh Local Prefix.
*
*/
const MeshLocalPrefix &GetMeshLocalPrefix(void) const
{
return reinterpret_cast<const MeshLocalPrefix &>(mMeshLocal16.GetAddress());
}
/**
* This method sets the Mesh Local Prefix.
*
* @param[in] aMeshLocalPrefix A reference to the Mesh Local Prefix.
*
*/
void SetMeshLocalPrefix(const MeshLocalPrefix &aMeshLocalPrefix);
/**
* This method applies the Mesh Local Prefix.
*
*/
void ApplyMeshLocalPrefix(void);
/**
* This method returns a reference to the Thread link-local address.
*
* The Thread link local address is derived using IEEE802.15.4 Extended Address as Interface Identifier.
*
* @returns A reference to the Thread link local address.
*
*/
const Ip6::Address &GetLinkLocalAddress(void) const { return mLinkLocal64.GetAddress(); }
/**
* This method updates the link local address.
*
* Call this method when the IEEE 802.15.4 Extended Address has changed.
*
*/
void UpdateLinkLocalAddress(void);
/**
* This method returns a reference to the link-local all Thread nodes multicast address.
*
* @returns A reference to the link-local all Thread nodes multicast address.
*
*/
const Ip6::Address &GetLinkLocalAllThreadNodesAddress(void) const { return mLinkLocalAllThreadNodes.GetAddress(); }
/**
* This method returns a reference to the realm-local all Thread nodes multicast address.
*
* @returns A reference to the realm-local all Thread nodes multicast address.
*
*/
const Ip6::Address &GetRealmLocalAllThreadNodesAddress(void) const
{
return mRealmLocalAllThreadNodes.GetAddress();
}
/**
* This method gets the parent when operating in End Device mode.
*
* @returns A reference to the parent.
*
*/
Router &GetParent(void) { return mParent; }
/**
* This method get the parent candidate.
*
* The parent candidate is valid when attempting to attach to a new parent.
*
*/
Router &GetParentCandidate(void) { return mParentCandidate; }
/**
* This method indicates whether or not an IPv6 address is an RLOC.
*
* @retval TRUE If @p aAddress is an RLOC.
* @retval FALSE If @p aAddress is not an RLOC.
*
*/
bool IsRoutingLocator(const Ip6::Address &aAddress) const;
/**
* This method indicates whether or not an IPv6 address is an ALOC.
*
* @retval TRUE If @p aAddress is an ALOC.
* @retval FALSE If @p aAddress is not an ALOC.
*
*/
bool IsAnycastLocator(const Ip6::Address &aAddress) const;
/**
* This method indicates whether or not an IPv6 address is a Mesh Local Address.
*
* @retval TRUE If @p aAddress is a Mesh Local Address.
* @retval FALSE If @p aAddress is not a Mesh Local Address.
*
*/
bool IsMeshLocalAddress(const Ip6::Address &aAddress) const;
/**
* This method returns the MLE Timeout value.
*
* @returns The MLE Timeout value in seconds.
*
*/
uint32_t GetTimeout(void) const { return mTimeout; }
/**
* This method sets the MLE Timeout value.
*
* @param[in] aTimeout The Timeout value in seconds.
*
*/
void SetTimeout(uint32_t aTimeout);
/**
* This method returns the RLOC16 assigned to the Thread interface.
*
* @returns The RLOC16 assigned to the Thread interface.
*
*/
uint16_t GetRloc16(void) const;
/**
* This method returns a reference to the RLOC assigned to the Thread interface.
*
* @returns A reference to the RLOC assigned to the Thread interface.
*
*/
const Ip6::Address &GetMeshLocal16(void) const { return mMeshLocal16.GetAddress(); }
/**
* This method returns a reference to the ML-EID assigned to the Thread interface.
*
* @returns A reference to the ML-EID assigned to the Thread interface.
*
*/
const Ip6::Address &GetMeshLocal64(void) const { return mMeshLocal64.GetAddress(); }
/**
* This method returns the Router ID of the Leader.
*
* @returns The Router ID of the Leader.
*
*/
uint8_t GetLeaderId(void) const { return mLeaderData.GetLeaderRouterId(); }
/**
* This method retrieves the Leader's RLOC.
*
* @param[out] aAddress A reference to the Leader's RLOC.
*
* @retval OT_ERROR_NONE Successfully retrieved the Leader's RLOC.
* @retval OT_ERROR_DETACHED The Thread interface is not currently attached to a Thread Partition.
*
*/
otError GetLeaderAddress(Ip6::Address &aAddress) const;
/**
* This method retrieves the Leader's ALOC.
*
* @param[out] aAddress A reference to the Leader's ALOC.
*
* @retval OT_ERROR_NONE Successfully retrieved the Leader's ALOC.
* @retval OT_ERROR_DETACHED The Thread interface is not currently attached to a Thread Partition.
*
*/
otError GetLeaderAloc(Ip6::Address &aAddress) const { return GetLocatorAddress(aAddress, kAloc16Leader); }
/**
* This method computes the Commissioner's ALOC.
*
* @param[out] aAddress A reference to the Commissioner's ALOC.
* @param[in] aSessionId Commissioner session id.
*
* @retval OT_ERROR_NONE Successfully retrieved the Commissioner's ALOC.
* @retval OT_ERROR_DETACHED The Thread interface is not currently attached to a Thread Partition.
*
*/
otError GetCommissionerAloc(Ip6::Address &aAddress, uint16_t aSessionId) const
{
return GetLocatorAddress(aAddress, CommissionerAloc16FromId(aSessionId));
}
/**
* This method retrieves the Service ALOC for given Service ID.
*
* @param[in] aServiceId Service ID to get ALOC for.
* @param[out] aAddress A reference to the Service ALOC.
*
* @retval OT_ERROR_NONE Successfully retrieved the Service ALOC.
* @retval OT_ERROR_DETACHED The Thread interface is not currently attached to a Thread Partition.
*
*/
otError GetServiceAloc(uint8_t aServiceId, Ip6::Address &aAddress) const;
/**
* This method returns the most recently received Leader Data.
*
* @returns A reference to the most recently received Leader Data.
*
*/
const LeaderData &GetLeaderData(void);
/**
* This method derives the Child ID from a given RLOC16.
*
* @param[in] aRloc16 The RLOC16 value.
*
* @returns The Child ID portion of an RLOC16.
*
*/
static uint16_t ChildIdFromRloc16(uint16_t aRloc16) { return aRloc16 & kMaxChildId; }
/**
* This method derives the Router ID portion from a given RLOC16.
*
* @param[in] aRloc16 The RLOC16 value.
*
* @returns The Router ID portion of an RLOC16.
*
*/
static uint8_t RouterIdFromRloc16(uint16_t aRloc16) { return aRloc16 >> kRouterIdOffset; }
/**
* This method returns whether the two RLOC16 have the same Router ID.
*
* @param[in] aRloc16A The first RLOC16 value.
* @param[in] aRloc16B The second RLOC16 value.
*
* @returns true if the two RLOC16 have the same Router ID, false otherwise.
*
*/
static bool RouterIdMatch(uint16_t aRloc16A, uint16_t aRloc16B)
{
return RouterIdFromRloc16(aRloc16A) == RouterIdFromRloc16(aRloc16B);
}
/**
* This method returns the Service ID corresponding to a Service ALOC16.
*
* @param[in] aAloc16 The Servicer ALOC16 value.
*
* @returns The Service ID corresponding to given ALOC16.
*
*/
static uint8_t ServiceIdFromAloc(uint16_t aAloc16) { return static_cast<uint8_t>(aAloc16 - kAloc16ServiceStart); }
/**
* This method returns the Service Aloc corresponding to a Service ID.
*
* @param[in] aServiceId The Service ID value.
*
* @returns The Service ALOC16 corresponding to given ID.
*
*/
static uint16_t ServiceAlocFromId(uint8_t aServiceId)
{
return static_cast<uint16_t>(aServiceId + kAloc16ServiceStart);
}
/**
* This method returns the Commissioner Aloc corresponding to a Commissioner Session ID.
*
* @param[in] aSessionId The Commissioner Session ID value.
*
* @returns The Commissioner ALOC16 corresponding to given ID.
*
*/
static uint16_t CommissionerAloc16FromId(uint16_t aSessionId)
{
return static_cast<uint16_t>((aSessionId & kAloc16CommissionerMask) + kAloc16CommissionerStart);
}
/**
* This method derives RLOC16 from a given Router ID.
*
* @param[in] aRouterId The Router ID value.
*
* @returns The RLOC16 corresponding to the given Router ID.
*
*/
static uint16_t Rloc16FromRouterId(uint8_t aRouterId)
{
return static_cast<uint16_t>(aRouterId << kRouterIdOffset);
}
/**
* This method indicates whether or not @p aRloc16 refers to an active router.
*
* @param[in] aRloc16 The RLOC16 value.
*
* @retval TRUE If @p aRloc16 refers to an active router.
* @retval FALSE If @p aRloc16 does not refer to an active router.
*
*/
static bool IsActiveRouter(uint16_t aRloc16) { return ChildIdFromRloc16(aRloc16) == 0; }
/**
* This method returns a reference to the send queue.
*
* @returns A reference to the send queue.
*
*/
const MessageQueue &GetMessageQueue(void) const { return mDelayedResponses; }
/**
* This method frees multicast MLE Data Response from Delayed Message Queue if any.
*
*/
void RemoveDelayedDataResponseMessage(void);
/**
* This method converts a device role into a human-readable string.
*
*/
static const char *RoleToString(DeviceRole aRole);
/**
* This method gets the MLE counters.
*
* @returns A reference to the MLE counters.
*
*/
const otMleCounters &GetCounters(void) const { return mCounters; }
/**
* This method resets the MLE counters.
*
*/
void ResetCounters(void) { memset(&mCounters, 0, sizeof(mCounters)); }
/**
* This function registers the client callback that is called when processing an MLE Parent Response message.
*
* @param[in] aCallback A pointer to a function that is called to deliver MLE Parent Response data.
* @param[in] aContext A pointer to application-specific context.
*
*/
void RegisterParentResponseStatsCallback(otThreadParentResponseCallback aCallback, void *aContext);
/**
* This method requests MLE layer to prepare and send a shorter version of Child ID Request message by only
* including the mesh-local IPv6 address in the Address Registration TLV.
*
* This method should be called when a previous MLE Child ID Request message would require fragmentation at 6LoWPAN
* layer.
*
*/
void RequestShorterChildIdRequest(void);
/**
* This method gets the RLOC or ALOC of a given RLOC16 or ALOC16.
*
* @param[out] aAddress A reference to the RLOC or ALOC.
* @param[in] aLocator RLOC16 or ALOC16.
*
* @retval OT_ERROR_NONE If got the RLOC or ALOC successfully.
* @retval OT_ERROR_DETACHED If device is detached.
*
*/
otError GetLocatorAddress(Ip6::Address &aAddress, uint16_t aLocator) const;
protected:
/**
* States during attach (when searching for a parent).
*
*/
enum AttachState
{
kAttachStateIdle, ///< Not currently searching for a parent.
kAttachStateProcessAnnounce, ///< Waiting to process a received Announce (to switch channel/pan-id).
kAttachStateStart, ///< Starting to look for a parent.
kAttachStateParentRequestRouter, ///< Searching for a Router to attach to.
kAttachStateParentRequestReed, ///< Searching for Routers or REEDs to attach to.
kAttachStateAnnounce, ///< Send Announce messages
kAttachStateChildIdRequest, ///< Sending a Child ID Request message.
};
/**
* States when reattaching network using stored dataset
*
*/
enum ReattachState
{
kReattachStop = 0, ///< Reattach process is disabled or finished
kReattachStart = 1, ///< Start reattach process
kReattachActive = 2, ///< Reattach using stored Active Dataset
kReattachPending = 3, ///< Reattach using stored Pending Dataset
};
enum
{
kMleMaxResponseDelay = 1000u, ///< Maximum delay before responding to a multicast request.
};
/**
* This enumeration type is used in `AppendAddressRegistration()` to determine which addresses to include in the
* appended Address Registration TLV.
*
*/
enum AddressRegistrationMode
{
kAppendAllAddresses, ///< Append all addresses (unicast/multicast) in Address Registration TLV.
kAppendMeshLocalOnly, ///< Only append the Mesh Local (ML-EID) address in Address Registration TLV.
};
/**
* This type represents a Challenge (or Response) data.
*
*/
struct Challenge
{
uint8_t mBuffer[kMaxChallengeSize]; ///< Buffer containing the challenge/response byte sequence.
uint8_t mLength; ///< Challenge length (in bytes).
/**
* This method generates a cryptographically secure random sequence to populate the challenge data.
*
*/
void GenerateRandom(void);
/**
* This method indicates whether the Challenge matches a given buffer.
*
* @param[in] aBuffer A pointer to a buffer to compare with the Challenge.
* @param[in] aLength Length of @p aBuffer (in bytes).
*
* @retval TRUE If the Challenge matches the given buffer.
* @retval FALSE If the Challenge does not match the given buffer.
*
*/
bool Matches(const uint8_t *aBuffer, uint8_t aLength) const;
/**
* This method indicates whether two Challenge data byte sequences are equal or not.
*
* @param[in] aOther Another Challenge data to compare.
*
* @retval TRUE If the two Challenges match.
* @retval FALSE If the two Challenges do not match.
*
*/
bool operator==(const Challenge &aOther) const { return Matches(aOther.mBuffer, aOther.mLength); }
};
/**
* This type represents list of requested TLVs in a TLV Request TLV.
*
*/
struct RequestedTlvs
{
enum
{
kMaxNumTlvs = 16, ///< Maximum number of TLVs in request array.
};
uint8_t mTlvs[kMaxNumTlvs]; ///< Array of requested TLVs.
uint8_t mNumTlvs; ///< Number of TLVs in the array.
};
/**
* This method allocates a new message buffer for preparing an MLE message.
*
* @returns A pointer to the message or NULL if insufficient message buffers are available.
*
*/
Message *NewMleMessage(void);
/**
* This method sets the device role.
*
* @param[in] aRole A device role.
*
*/
void SetRole(DeviceRole aRole);
/**
* This method sets the attach state
*
* @param[in] aState An attach state
*
*/
void SetAttachState(AttachState aState);
/**
* This method appends an MLE header to a message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aCommand The MLE Command Type.
*
* @retval OT_ERROR_NONE Successfully appended the header.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the header.
*
*/
otError AppendHeader(Message &aMessage, Header::Command aCommand);
/**
* This method appends a Source Address TLV to a message.
*
* @param[in] aMessage A reference to the message.
*
* @retval OT_ERROR_NONE Successfully appended the Source Address TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Source Address TLV.
*
*/
otError AppendSourceAddress(Message &aMessage);
/**
* This method appends a Mode TLV to a message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aMode The Device Mode.
*
* @retval OT_ERROR_NONE Successfully appended the Mode TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Mode TLV.
*
*/
otError AppendMode(Message &aMessage, DeviceMode aMode);
/**
* This method appends a Timeout TLV to a message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aTimeout The Timeout value.
*
* @retval OT_ERROR_NONE Successfully appended the Timeout TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Timeout TLV.
*
*/
otError AppendTimeout(Message &aMessage, uint32_t aTimeout);
/**
* This method appends a Challenge TLV to a message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aChallenge A pointer to the Challenge value.
* @param[in] aChallengeLength The length of the Challenge value in bytes.
*
* @retval OT_ERROR_NONE Successfully appended the Challenge TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Challenge TLV.
*
*/
otError AppendChallenge(Message &aMessage, const uint8_t *aChallenge, uint8_t aChallengeLength);
/**
* This method appends a Challenge TLV to a message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aChallenge A reference to the Challenge data.
*
* @retval OT_ERROR_NONE Successfully appended the Challenge TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Challenge TLV.
*
*/
otError AppendChallenge(Message &aMessage, const Challenge &aChallenge);
/**
* This method reads Challenge TLV from a message.
*
* @param[in] aMessage A reference to the message.
* @param[out] aChallenge A reference to the Challenge data where to output the read value.
*
* @retval OT_ERROR_NONE Successfully read the Challenge TLV.
* @retval OT_ERROR_NOT_FOUND Challenge TLV was not found in the message.
* @retval OT_ERROR_PARSE Challenge TLV was found but could not be parsed.
*
*/
otError ReadChallenge(const Message &aMessage, Challenge &aChallenge);
/**
* This method appends a Response TLV to a message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aResponse A reference to the Response data.
*
* @retval OT_ERROR_NONE Successfully appended the Response TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Response TLV.
*
*/
otError AppendResponse(Message &aMessage, const Challenge &aResponse);
/**
* This method reads Response TLV from a message.
*
* @param[in] aMessage A reference to the message.
* @param[out] aResponse A reference to the Response data where to output the read value.
*
* @retval OT_ERROR_NONE Successfully read the Response TLV.
* @retval OT_ERROR_NOT_FOUND Response TLV was not found in the message.
* @retval OT_ERROR_PARSE Response TLV was found but could not be parsed.
*
*/
otError ReadResponse(const Message &aMessage, Challenge &aResponse);
/**
* This method appends a Link Frame Counter TLV to a message.
*
* @param[in] aMessage A reference to the message.
*
* @retval OT_ERROR_NONE Successfully appended the Link Frame Counter TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Link Frame Counter TLV.
*
*/
otError AppendLinkFrameCounter(Message &aMessage);
/**
* This method appends an MLE Frame Counter TLV to a message.
*
* @param[in] aMessage A reference to the message.
*
* @retval OT_ERROR_NONE Successfully appended the Frame Counter TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the MLE Frame Counter TLV.
*
*/
otError AppendMleFrameCounter(Message &aMessage);
/**
* This method appends an Address16 TLV to a message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aRloc16 The RLOC16 value.
*
* @retval OT_ERROR_NONE Successfully appended the Address16 TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Address16 TLV.
*
*/
otError AppendAddress16(Message &aMessage, uint16_t aRloc16);
/**
* This method appends a Network Data TLV to the message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aStableOnly TRUE to append stable data, FALSE otherwise.
*
* @retval OT_ERROR_NONE Successfully appended the Network Data TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Network Data TLV.
*
*/
otError AppendNetworkData(Message &aMessage, bool aStableOnly);
/**
* This method appends a TLV Request TLV to a message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aTlvs A pointer to the list of TLV types.
* @param[in] aTlvsLength The number of TLV types in @p aTlvs
*
* @retval OT_ERROR_NONE Successfully appended the TLV Request TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the TLV Request TLV.
*
*/
otError AppendTlvRequest(Message &aMessage, const uint8_t *aTlvs, uint8_t aTlvsLength);
/**
* This method reads TLV Request TLV from a message.
*
* @param[in] aMessage A reference to the message.
* @param[out] aRequestedTlvs A reference to output the read list of requested TLVs.
*
* @retval OT_ERROR_NONE Successfully read the TLV.
* @retval OT_ERROR_NOT_FOUND TLV was not found in the message.
* @retval OT_ERROR_PARSE TLV was found but could not be parsed.
*
*/
otError FindTlvRequest(const Message &aMessage, RequestedTlvs &aRequestedTlvs);
/**
* This method appends a Leader Data TLV to a message.
*
* @param[in] aMessage A reference to the message.
*
* @retval OT_ERROR_NONE Successfully appended the Leader Data TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Leader Data TLV.
*
*/
otError AppendLeaderData(Message &aMessage);
/**
* This method reads Leader Data TLV from a message.
*
* @param[in] aMessage A reference to the message.
* @param[out] aLeaderData A reference to output the Leader Data.
*
* @retval OT_ERROR_NONE Successfully read the TLV.
* @retval OT_ERROR_NOT_FOUND TLV was not found in the message.
* @retval OT_ERROR_PARSE TLV was found but could not be parsed.
*
*/
otError ReadLeaderData(const Message &aMessage, LeaderData &aLeaderData);
/**
* This method appends a Scan Mask TLV to a message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aScanMask The Scan Mask value.
*
* @retval OT_ERROR_NONE Successfully appended the Scan Mask TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Scan Mask TLV.
*
*/
otError AppendScanMask(Message &aMessage, uint8_t aScanMask);
/**
* This method appends a Status TLV to a message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aStatus The Status value.
*
* @retval OT_ERROR_NONE Successfully appended the Status TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Status TLV.
*
*/
otError AppendStatus(Message &aMessage, StatusTlv::Status aStatus);
/**
* This method appends a Link Margin TLV to a message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aLinkMargin The Link Margin value.
*
* @retval OT_ERROR_NONE Successfully appended the Link Margin TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Link Margin TLV.
*
*/
otError AppendLinkMargin(Message &aMessage, uint8_t aLinkMargin);
/**
* This method appends a Version TLV to a message.
*
* @param[in] aMessage A reference to the message.
*
* @retval OT_ERROR_NONE Successfully appended the Version TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Version TLV.
*
*/
otError AppendVersion(Message &aMessage);
/**
* This method appends an Address Registration TLV to a message.
*
* @param[in] aMessage A reference to the message.
* @param[in] aMode Determines which addresses to include in the TLV (see `AddressRegistrationMode`).
*
* @retval OT_ERROR_NONE Successfully appended the Address Registration TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Address Registration TLV.
*
*/
otError AppendAddressRegistration(Message &aMessage, AddressRegistrationMode aMode = kAppendAllAddresses);
#if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
/**
* This method appends a Time Request TLV to a message.
*
* @param[in] aMessage A reference to the message.
*
* @retval OT_ERROR_NONE Successfully appended the Time Request TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Time Request TLV.
*
*/
otError AppendTimeRequest(Message &aMessage);
/**
* This method appends a Time Parameter TLV to a message.
*
* @param[in] aMessage A reference to the message.
*
* @retval OT_ERROR_NONE Successfully appended the Time Parameter TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Time Parameter TLV.
*
*/
otError AppendTimeParameter(Message &aMessage);
/**
* This method appends a XTAL Accuracy TLV to a message.
*
* @param[in] aMessage A reference to the message.
*
* @retval OT_ERROR_NONE Successfully appended the XTAL Accuracy TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the XTAl Accuracy TLV.
*
*/
otError AppendXtalAccuracy(Message &aMessage);
#endif // OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
/**
* This method appends a Active Timestamp TLV to a message.
*
* @param[in] aMessage A reference to the message.
*
* @retval OT_ERROR_NONE Successfully appended the Active Timestamp TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Active Timestamp TLV.
*
*/
otError AppendActiveTimestamp(Message &aMessage);
/**
* This method appends a Pending Timestamp TLV to a message.
*
* @param[in] aMessage A reference to the message.
*
* @retval OT_ERROR_NONE Successfully appended the Pending Timestamp TLV.
* @retval OT_ERROR_NO_BUFS Insufficient buffers available to append the Pending Timestamp TLV.
*
*/
otError AppendPendingTimestamp(Message &aMessage);
/**
* This method checks if the destination is reachable.
*
* @param[in] aMeshDest The RLOC16 of the destination.
* @param[in] aIp6Header The IPv6 header of the message.
*
* @retval OT_ERROR_NONE The destination is reachable.
* @retval OT_ERROR_NO_ROUTE The destination is not reachable and the message should be dropped.
*
*/
otError CheckReachability(uint16_t aMeshDest, Ip6::Header &aIp6Header);
/**
* This method returns a pointer to the neighbor object.
*
* @param[in] aAddress A reference to the MAC address.
*
* @returns A pointer to the neighbor object.
*
*/
Neighbor *GetNeighbor(const Mac::Address &aAddress);
/**
* This method returns a pointer to the neighbor object.
*
* @param[in] aAddress A reference to the MAC short address.
*
* @returns A pointer to the neighbor object.
*
*/
Neighbor *GetNeighbor(Mac::ShortAddress aAddress);
/**
* This method returns a pointer to the neighbor object.
*
* @param[in] aAddress A reference to the MAC extended address.
*
* @returns A pointer to the neighbor object.
*
*/
Neighbor *GetNeighbor(const Mac::ExtAddress &aAddress);
/**
* This method returns a pointer to the neighbor object.
*
* @param[in] aAddress A reference to the IPv6 address.
*
* @returns A pointer to the neighbor object.
*
*/
Neighbor *GetNeighbor(const Ip6::Address &aAddress)
{
OT_UNUSED_VARIABLE(aAddress);
return NULL;
}
/**
* This method returns the next hop towards an RLOC16 destination.
*
* @param[in] aDestination The RLOC16 of the destination.
*
* @returns A RLOC16 of the next hop if a route is known, kInvalidRloc16 otherwise.
*
*/
Mac::ShortAddress GetNextHop(uint16_t aDestination) const;
/**
* This method generates an MLE Data Request message.
*
* @param[in] aDestination A reference to the IPv6 address of the destination.
* @param[in] aTlvs A pointer to requested TLV types.
* @param[in] aTlvsLength The number of TLV types in @p aTlvs.
* @param[in] aDelay Delay in milliseconds before the Data Request message is sent.
*
* @retval OT_ERROR_NONE Successfully generated an MLE Data Request message.
* @retval OT_ERROR_NO_BUFS Insufficient buffers to generate the MLE Data Request message.
*
*/
otError SendDataRequest(const Ip6::Address &aDestination,
const uint8_t * aTlvs,
uint8_t aTlvsLength,
uint16_t aDelay);
/**
* This method generates an MLE Child Update Request message.
*
* @retval OT_ERROR_NONE Successfully generated an MLE Child Update Request message.
* @retval OT_ERROR_NO_BUFS Insufficient buffers to generate the MLE Child Update Request message.
*
*/
otError SendChildUpdateRequest(void);
/**
* This method generates an MLE Child Update Response message.
*
* @param[in] aTlvs A pointer to requested TLV types.
* @param[in] aNumTlvs The number of TLV types in @p aTlvs.
* @param[in] aChallenge The Challenge for the response.
*
* @retval OT_ERROR_NONE Successfully generated an MLE Child Update Response message.
* @retval OT_ERROR_NO_BUFS Insufficient buffers to generate the MLE Child Update Response message.
*
*/
otError SendChildUpdateResponse(const uint8_t *aTlvs, uint8_t aNumTlvs, const Challenge &aChallenge);
/**
* This method submits an MLE message to the UDP socket.
*
* @param[in] aMessage A reference to the message.
* @param[in] aDestination A reference to the IPv6 address of the destination.
*
* @retval OT_ERROR_NONE Successfully submitted the MLE message.
* @retval OT_ERROR_NO_BUFS Insufficient buffers to form the rest of the MLE message.
*
*/
otError SendMessage(Message &aMessage, const Ip6::Address &aDestination);
/**
* This method sets the RLOC16 assigned to the Thread interface.
*
* @param[in] aRloc16 The RLOC16 to set.
*
*/
void SetRloc16(uint16_t aRloc16);
/**
* This method sets the Device State to Detached.
*
*/
void SetStateDetached(void);
/**
* This method sets the Device State to Child.
*
*/
void SetStateChild(uint16_t aRloc16);
/**
* This method sets the Leader's Partition ID, Weighting, and Router ID values.
*
* @param[in] aPartitionId The Leader's Partition ID value.
* @param[in] aWeighting The Leader's Weighting value.
* @param[in] aLeaderRouterId The Leader's Router ID value.
*
*/
void SetLeaderData(uint32_t aPartitionId, uint8_t aWeighting, uint8_t aLeaderRouterId);
/**
* This method adds a message to the message queue. The queued message will be transmitted after given delay.
*
* @param[in] aMessage The message to transmit after given delay.
* @param[in] aDestination The IPv6 address of the recipient of the message.
* @param[in] aDelay The delay in milliseconds before transmission of the message.
*
* @retval OT_ERROR_NONE Successfully queued the message to transmit after the delay.
* @retval OT_ERROR_NO_BUFS Insufficient buffers to queue the message.
*
*/
otError AddDelayedResponse(Message &aMessage, const Ip6::Address &aDestination, uint16_t aDelay);
/**
* This method prints an MLE log message with an IPv6 address.
*
* @param[in] aLogString The log message string.
* @param[in] aAddress The IPv6 address of the peer.
*
*/
void LogMleMessage(const char *aLogString, const Ip6::Address &aAddress) const;
/**
* This method prints an MLE log message with an IPv6 address and RLOC16.
*
* @param[in] aLogString The log message string.
* @param[in] aAddress The IPv6 address of the peer.
* @param[in] aRloc The RLOC16.
*
*/
void LogMleMessage(const char *aLogString, const Ip6::Address &aAddress, uint16_t aRloc) const;
/**
* This method triggers MLE Announce on previous channel after the Thread device successfully
* attaches and receives the new Active Commissioning Dataset if needed.
*
* MTD would send Announce immediately after attached.
* FTD would delay to send Announce after tried to become Router or decided to stay in REED role.
*
*/
void InformPreviousChannel(void);
/**
* This method indicates whether or not in announce attach process.
*
* @retval true if attaching/attached on the announced parameters, false otherwise.
*
*/
bool IsAnnounceAttach(void) const { return mAlternatePanId != Mac::kPanIdBroadcast; }
#if (OPENTHREAD_CONFIG_LOG_LEVEL >= OT_LOG_LEVEL_NOTE) && (OPENTHREAD_CONFIG_LOG_MLE == 1)
/**
* This method converts an `AttachMode` enumeration value into a human-readable string.
*
* @param[in] aMode An attach mode
*
* @returns A human-readable string corresponding to the attach mode.
*
*/
static const char *AttachModeToString(AttachMode aMode);
/**
* This method converts an `AttachState` enumeration value into a human-readable string.
*
* @param[in] aState An attach state
*
* @returns A human-readable string corresponding to the attach state.
*
*/
static const char *AttachStateToString(AttachState aState);
/**
* This method converts a `ReattachState` enumeration value into a human-readable string.
*
* @param[in] aState A reattach state
*
* @returns A human-readable string corresponding to the reattach state.
*
*/
static const char *ReattachStateToString(ReattachState aState);
#endif
Ip6::NetifUnicastAddress mLeaderAloc; ///< Leader anycast locator
LeaderData mLeaderData; ///< Last received Leader Data TLV.
bool mRetrieveNewNetworkData; ///< Indicating new Network Data is needed if set.
DeviceRole mRole; ///< Current Thread role.
Router mParent; ///< Parent information.
Router mParentCandidate; ///< Parent candidate information.
DeviceMode mDeviceMode; ///< Device mode setting.
AttachState mAttachState; ///< The parent request state.
ReattachState mReattachState; ///< Reattach state
uint16_t mAttachCounter; ///< Attach attempt counter.
uint16_t mAnnounceDelay; ///< Delay in between sending Announce messages during attach.
TimerMilli mAttachTimer; ///< The timer for driving the attach process.
TimerMilli mDelayedResponseTimer; ///< The timer to delay MLE responses.
TimerMilli mMessageTransmissionTimer; ///< The timer for (re-)sending of MLE messages (e.g. Child Update).
uint8_t mParentLeaderCost;
private:
enum
{
kMleHopLimit = 255,
kMleSecurityTagSize = 4, // Security tag size in bytes.
// Parameters related to "periodic parent search" feature (CONFIG_ENABLE_PERIODIC_PARENT_SEARCH).
// All timer intervals are converted to milliseconds.
kParentSearchCheckInterval = (OPENTHREAD_CONFIG_PARENT_SEARCH_CHECK_INTERVAL * 1000u),
kParentSearchBackoffInterval = (OPENTHREAD_CONFIG_PARENT_SEARCH_BACKOFF_INTERVAL * 1000u),
kParentSearchJitterInterval = (15 * 1000u),
kParentSearchRssThreadhold = OPENTHREAD_CONFIG_PARENT_SEARCH_RSS_THRESHOLD,
// Parameters for "attach backoff" feature (CONFIG_ENABLE_ATTACH_BACKOFF) - Intervals are in milliseconds.
kAttachBackoffMinInterval = OPENTHREAD_CONFIG_MLE_ATTACH_BACKOFF_MINIMUM_INTERVAL,
kAttachBackoffMaxInterval = OPENTHREAD_CONFIG_MLE_ATTACH_BACKOFF_MAXIMUM_INTERVAL,
kAttachBackoffJitter = OPENTHREAD_CONFIG_MLE_ATTACH_BACKOFF_JITTER_INTERVAL,
};
enum ParentRequestType
{
kParentRequestTypeRouters, ///< Parent Request to all routers.
kParentRequestTypeRoutersAndReeds, ///< Parent Request to all routers and REEDs.
};
enum ChildUpdateRequestState
{
kChildUpdateRequestNone, ///< No pending or active Child Update Request.
kChildUpdateRequestPending, ///< Pending Child Update Request due to relative OT_CHANGED event.
kChildUpdateRequestActive, ///< Child Update Request has been sent and Child Update Response is expected.
};
enum DataRequestState
{
kDataRequestNone, ///< Not waiting for a Data Response.
kDataRequestActive, ///< Data Request has been sent, Data Response is expected.
};
struct DelayedResponseMetadata
{
otError AppendTo(Message &aMessage) const { return aMessage.Append(this, sizeof(*this)); }
void ReadFrom(const Message &aMessage);
void RemoveFrom(Message &aMessage) const;
Ip6::Address mDestination; // IPv6 address of the message destination.
TimeMilli mSendTime; // Time when the message shall be sent.
};
static void HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags);
void HandleStateChanged(otChangedFlags aFlags);
static void HandleAttachTimer(Timer &aTimer);
void HandleAttachTimer(void);
static void HandleDelayedResponseTimer(Timer &aTimer);
void HandleDelayedResponseTimer(void);
static void HandleMessageTransmissionTimer(Timer &aTimer);
void HandleMessageTransmissionTimer(void);
static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
void HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
void ScheduleMessageTransmissionTimer(void);
otError ReadChallengeOrResponse(const Message &aMessage, uint8_t aTlvType, Challenge &aBuffer);
void HandleAdvertisement(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, Neighbor *aNeighbor);
void HandleChildIdResponse(const Message & aMessage,
const Ip6::MessageInfo &aMessageInfo,
const Neighbor * aNeighbor);
void HandleChildUpdateRequest(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, Neighbor *aNeighbor);
void HandleChildUpdateResponse(const Message & aMessage,
const Ip6::MessageInfo &aMessageInfo,
const Neighbor * aNeighbor);
void HandleDataResponse(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, const Neighbor *aNeighbor);
void HandleParentResponse(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, uint32_t aKeySequence);
void HandleAnnounce(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
void HandleDiscoveryResponse(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
otError HandleLeaderData(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
void ProcessAnnounce(void);
bool HasUnregisteredAddress(void);
uint32_t GetAttachStartDelay(void) const;
otError SendParentRequest(ParentRequestType aType);
otError SendChildIdRequest(void);
otError SendOrphanAnnounce(void);
bool PrepareAnnounceState(void);
void SendAnnounce(uint8_t aChannel, bool aOrphanAnnounce, const Ip6::Address &aDestination);
uint32_t Reattach(void);
bool IsBetterParent(uint16_t aRloc16,
uint8_t aLinkQuality,
uint8_t aLinkMargin,
const ConnectivityTlv &aConnectivityTlv,
uint8_t aVersion);
bool IsNetworkDataNewer(const LeaderData &aLeaderData);
#if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
/**
* This method scans for network data from the leader and updates IP addresses assigned to this
* interface to make sure that all Service ALOCs (0xfc10-0xfc1f) are properly set.
*/
void UpdateServiceAlocs(void);
#endif
#if OPENTHREAD_CONFIG_MLE_INFORM_PREVIOUS_PARENT_ON_REATTACH
void InformPreviousParent(void);
#endif
#if OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
static void HandleParentSearchTimer(Timer &aTimer);
void HandleParentSearchTimer(void);
void StartParentSearchTimer(void);
void UpdateParentSearchState(void);
#endif
MessageQueue mDelayedResponses;
Challenge mParentRequestChallenge;
AttachMode mParentRequestMode;
int8_t mParentPriority;
uint8_t mParentLinkQuality3;
uint8_t mParentLinkQuality2;
uint8_t mParentLinkQuality1;
uint16_t mParentSedBufferSize;
uint8_t mParentSedDatagramCount;
uint8_t mChildUpdateAttempts;
ChildUpdateRequestState mChildUpdateRequestState;
uint8_t mDataRequestAttempts;
DataRequestState mDataRequestState;
AddressRegistrationMode mAddressRegistrationMode;
uint8_t mParentLinkMargin;
bool mParentIsSingleton;
bool mReceivedResponseFromParent;
LeaderData mParentLeaderData;
Challenge mParentCandidateChallenge;
Ip6::UdpSocket mSocket;
uint32_t mTimeout;
DiscoverHandler mDiscoverHandler;
void * mDiscoverContext;
uint16_t mDiscoverCcittIndex;
uint16_t mDiscoverAnsiIndex;
bool mDiscoverInProgress;
bool mDiscoverEnableFiltering;
#if OPENTHREAD_CONFIG_MLE_INFORM_PREVIOUS_PARENT_ON_REATTACH
uint16_t mPreviousParentRloc;
#endif
#if OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
bool mParentSearchIsInBackoff : 1;
bool mParentSearchBackoffWasCanceled : 1;
bool mParentSearchRecentlyDetached : 1;
TimeMilli mParentSearchBackoffCancelTime;
TimerMilli mParentSearchTimer;
#endif
uint8_t mAnnounceChannel;
uint8_t mAlternateChannel;
uint16_t mAlternatePanId;
uint64_t mAlternateTimestamp;
#if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
Ip6::NetifUnicastAddress mServiceAlocs[kMaxServiceAlocs];
#endif
otMleCounters mCounters;
Ip6::NetifUnicastAddress mLinkLocal64;
Ip6::NetifUnicastAddress mMeshLocal64;
Ip6::NetifUnicastAddress mMeshLocal16;
Ip6::NetifMulticastAddress mLinkLocalAllThreadNodes;
Ip6::NetifMulticastAddress mRealmLocalAllThreadNodes;
Notifier::Callback mNotifierCallback;
otThreadParentResponseCallback mParentResponseCb;
void * mParentResponseCbContext;
};
} // namespace Mle
/**
* @}
*
*/
} // namespace ot
#endif // MLE_HPP_