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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Implements Type-Length-Value (TLV) encoding and decoding as outlined
//! in the Thread 1.1.1 Specification. TLVs are used to serialize
//! information exchanged during mesh link establishment (MLE). MLE is
//! covered in Chapter 4.
//!
//! MLE messages consist of a command type and a series of TLV parameters.
//!
//! This module, as it stands, implements the minimum subset of TLVs
//! required to support MLE for attaching a Sleepy End Device (SED) to a
//! Thread network.
//!
//! MLE for network attaching comprises a four-step handshake that works
//! as follows:
//!
//! 1. A child device multicasts a Parent Request MLE command.
//! 2. Each potential parent device on the network unicasts a Parent
//!    Response MLE command.
//! 3. The child device selects a parent based on a hierarchy of
//!    connectivity metrics and unicasts a Child ID Request MLE
//!    command.
//! 4. The selected parent unicasts a Child ID Response MLE command.
//!
//! A TLV is comprised of three parts:
//!
//! 1. Type   - A one-byte TLV type number.
//! 2. Length - A one-byte number representing the length of the TLV
//!             value in bytes.
//! 3. Value  - The TLV value.
//!
//! For some TLVs, the TLV type number is shifted left by one to leave the
//! least significant bit to denote whether information in the TLV value
//! is stable. Stable network data is data that is expected to be stable
//! over weeks or months (Section 5.14).
//!
//! TLVs can be nested within a TLV value. Some types of Network Data
//! TLVs, for example, contain sub-TLVs inside of their TLV value.
//!
//! To simplify variable-length TLV value decoding in Rust, TLV values are
//! assumed to have a maximum length of 128 bytes. This assumption is made
//! only when a variable-length value must be decoded from network byte
//! order before it can be interpreted correctly. Excluded from this case
//! are variable-length values that contain data that must later be
//! decoded by the caller before being interpreted (for example,
//! sub-TLVs). Such a value is instead returned as a slice of the original
//! buffer passed to the decode function.
//!
//!
//! Author: Mateo Garcia <mateog@stanford.edu>

// TODO: Move the MLE explanation above to the MLE module, when it is created.

// NOTES FOR DEBUGGING:
// - .to_be() may not have been called on values wider than one byte
// - encode_bytes_be may have been used instead of encode_bytes
// - decode_bytes_be may have been used instead of decode_bytes
// - See 4.5.25 Active Operational Dataset TLV and 4.5.26 Pending Operational Dataset TLV
//    - Are Active and Pending Timestamp TLVs, respectively, required to be sent as well
//      if either of the dataset tlvs are sent?

use crate::net::stream::SResult;
use crate::net::stream::{decode_bytes_be, decode_u16, decode_u32, decode_u8};
use crate::net::stream::{encode_bytes, encode_bytes_be, encode_u16, encode_u32, encode_u8};
use core::mem;

const TL_WIDTH: usize = 2; // Type and length fields of TLV are each one byte.
const MAX_VALUE_FIELD_LENGTH: usize = 128; // Assume a TLV value will be no longer than 128 bytes.

/// Type-Length-Value structure.
pub enum Tlv<'a> {
    SourceAddress(u16),
    Mode(u8),
    Timeout(u32),
    Challenge([u8; 8]), // Byte string max length 8 bytes.
    Response([u8; 8]),  // Byte string max length 8 bytes.
    LinkLayerFrameCounter(u32),
    // LinkQuality,                  // TLV type Not used in Thread
    // NetworkParameter,             // TLV type Not used in Thread
    MleFrameCounter(u32),
    /*
    TODO: Not required to implement MLE for SED
    Route64,
    */
    Address16(u16),
    LeaderData {
        partition_id: u32,
        weighting: u8,
        data_version: u8,
        stable_data_version: u8,
        leader_router_id: u8,
    },
    NetworkData(&'a [u8]),
    TlvRequest(&'a [u8]),
    ScanMask(u8),
    Connectivity {
        parent_priority: u8,
        link_quality_3: u8,
        link_quality_2: u8,
        link_quality_1: u8,
        leader_cost: u8,
        id_sequence: u8,
        active_routers: u8,
        sed_buffer_size: Option<u16>,
        sed_datagram_count: Option<u8>,
    },
    LinkMargin(u8),
    Status(u8),
    Version(u16),
    /*
    TODO: Not required to implement MLE for SED
    AddressRegistration
    AddressRegistration
    Channel
    PanId
    ActiveTimestamp
    PendingTimestamp
    ThreadDiscovery
    */
    ActiveOperationalDataset(&'a [u8]),
    PendingOperationalDataset(&'a [u8]),
}

pub fn unwrap_tlv_offset(res: SResult) -> usize {
    match res {
        SResult::Done(val, ()) => val,
        _ => 0,
    }
}

impl Tlv<'_> {
    /// Serializes TLV data in `buf` into the format specific to the TLV
    /// type.
    pub fn encode(&self, buf: &mut [u8]) -> SResult {
        match *self {
            Tlv::SourceAddress(ref mac_address) => {
                let value_width = mem::size_of::<u16>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u16, mac_address.to_be());
                stream_done!(offset)
            }
            Tlv::Mode(ref mode) => {
                let value_width = mem::size_of::<u8>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u8, *mode);
                stream_done!(offset)
            }
            Tlv::Timeout(ref max_transmit_interval) => {
                let value_width = mem::size_of::<u32>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u32, max_transmit_interval.to_be());
                stream_done!(offset)
            }
            Tlv::Challenge(ref byte_str) => {
                let value_width = byte_str.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes_be, byte_str);
                stream_done!(offset)
            }
            Tlv::Response(ref byte_str) => {
                let value_width = byte_str.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes_be, byte_str);
                stream_done!(offset)
            }
            Tlv::LinkLayerFrameCounter(ref frame_counter) => {
                let value_width = mem::size_of::<u32>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u32, frame_counter.to_be());
                stream_done!(offset)
            }
            Tlv::MleFrameCounter(ref frame_counter) => {
                let value_width = mem::size_of::<u32>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u32, frame_counter.to_be());
                stream_done!(offset)
            }
            Tlv::Address16(ref mac_address) => {
                let value_width = mem::size_of::<u16>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u16, mac_address.to_be());
                stream_done!(offset)
            }
            Tlv::LeaderData {
                partition_id,
                weighting,
                data_version,
                stable_data_version,
                leader_router_id,
            } => {
                let value_width = mem::size_of::<u32>()
                    + mem::size_of::<u8>()
                    + mem::size_of::<u8>()
                    + mem::size_of::<u8>()
                    + mem::size_of::<u8>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u32, partition_id.to_be());
                offset = enc_consume!(buf, offset; encode_u8, weighting);
                offset = enc_consume!(buf, offset; encode_u8, data_version);
                offset = enc_consume!(buf, offset; encode_u8, stable_data_version);
                offset = enc_consume!(buf, offset; encode_u8, leader_router_id);
                stream_done!(offset)
            }
            Tlv::NetworkData(network_data_tlvs) => {
                let value_width = network_data_tlvs.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes, network_data_tlvs);
                stream_done!(offset)
            }
            Tlv::TlvRequest(tlv_codes) => {
                let value_width = tlv_codes.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes, tlv_codes);
                stream_done!(offset)
            }
            Tlv::ScanMask(ref scan_mask) => {
                let value_width = mem::size_of::<u8>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u8, *scan_mask);
                stream_done!(offset)
            }
            Tlv::Connectivity {
                parent_priority,
                link_quality_3,
                link_quality_2,
                link_quality_1,
                leader_cost,
                id_sequence,
                active_routers,
                sed_buffer_size,
                sed_datagram_count,
            } => {
                let base_width = mem::size_of::<u8>()
                    + mem::size_of::<u8>()
                    + mem::size_of::<u8>()
                    + mem::size_of::<u8>()
                    + mem::size_of::<u8>()
                    + mem::size_of::<u8>()
                    + mem::size_of::<u8>();
                let sed_buf_size_width = match sed_buffer_size {
                    None => 0,
                    Some(_) => mem::size_of::<u16>(),
                };
                let sed_datagram_cnt_width = match sed_datagram_count {
                    None => 0,
                    Some(_) => mem::size_of::<u8>(),
                };
                let value_width = base_width + sed_buf_size_width + sed_datagram_cnt_width;
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u8, parent_priority);
                offset = enc_consume!(buf, offset; encode_u8, link_quality_3);
                offset = enc_consume!(buf, offset; encode_u8, link_quality_2);
                offset = enc_consume!(buf, offset; encode_u8, link_quality_1);
                offset = enc_consume!(buf, offset; encode_u8, leader_cost);
                offset = enc_consume!(buf, offset; encode_u8, id_sequence);
                offset = enc_consume!(buf, offset; encode_u8, active_routers);
                if let Some(ref buf_size) = sed_buffer_size {
                    offset = enc_consume!(buf, offset; encode_u16, buf_size.to_be());
                }
                if let Some(ref datagram_cnt) = sed_datagram_count {
                    offset = enc_consume!(buf, offset; encode_u8, *datagram_cnt);
                }
                stream_done!(offset)
            }
            Tlv::LinkMargin(ref link_margin) => {
                let value_width = mem::size_of::<u8>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u8, *link_margin);
                stream_done!(offset)
            }
            Tlv::Status(ref status) => {
                let value_width = mem::size_of::<u8>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u8, *status);
                stream_done!(offset)
            }
            Tlv::Version(ref version) => {
                let value_width = mem::size_of::<u16>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u16, *version);
                stream_done!(offset)
            }
            Tlv::ActiveOperationalDataset(network_mgmt_tlvs) => {
                let value_width = network_mgmt_tlvs.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes, network_mgmt_tlvs);
                stream_done!(offset)
            }
            Tlv::PendingOperationalDataset(network_mgmt_tlvs) => {
                let value_width = network_mgmt_tlvs.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes, network_mgmt_tlvs);
                stream_done!(offset)
            }
        }
    }

    fn encode_tl(&self, buf: &mut [u8], value_width: usize) -> SResult {
        stream_len_cond!(buf, TL_WIDTH + value_width);
        buf[0] = TlvType::from(self) as u8;
        buf[1] = value_width as u8;
        stream_done!(TL_WIDTH)
    }

    /// Deserializes TLV data from `buf` into the TLV variant specific to
    /// the TLV type.
    /// `SResult::Error` is returned if the type field does not match any
    /// implemented TLV type.
    pub fn decode(buf: &[u8]) -> SResult<Tlv> {
        let (offset, tlv_type) = dec_try!(buf; decode_u8);
        let tlv_type = TlvType::from(tlv_type);
        let (offset, length) = dec_try!(buf, offset; decode_u8);
        match tlv_type {
            TlvType::SourceAddress => {
                let (offset, mac_address) = dec_try!(buf, offset; decode_u16);
                stream_done!(offset, Tlv::SourceAddress(mac_address))
            }
            TlvType::Mode => {
                let (offset, mode) = dec_try!(buf, offset; decode_u8);
                stream_done!(offset, Tlv::Mode(mode))
            }
            TlvType::Timeout => {
                let (offset, max_transmit_interval) = dec_try!(buf, offset; decode_u32);
                stream_done!(offset, Tlv::Timeout(max_transmit_interval))
            }
            TlvType::Challenge => {
                let mut byte_str = [0u8; 8];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut byte_str);
                stream_done!(offset, Tlv::Challenge(byte_str))
            }
            TlvType::Response => {
                let mut byte_str = [0u8; 8];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut byte_str);
                stream_done!(offset, Tlv::Response(byte_str))
            }
            TlvType::LinkLayerFrameCounter => {
                let (offset, frame_counter) = dec_try!(buf, offset; decode_u32);
                stream_done!(offset, Tlv::LinkLayerFrameCounter(frame_counter))
            }
            TlvType::MleFrameCounter => {
                let (offset, frame_counter) = dec_try!(buf, offset; decode_u32);
                stream_done!(offset, Tlv::MleFrameCounter(frame_counter))
            }
            TlvType::Address16 => {
                let (offset, mac_address) = dec_try!(buf, offset; decode_u16);
                stream_done!(offset, Tlv::Address16(mac_address))
            }
            TlvType::LeaderData => {
                let (offset, partition_id) = dec_try!(buf, offset; decode_u32);
                let (offset, weighting) = dec_try!(buf, offset; decode_u8);
                let (offset, data_version) = dec_try!(buf, offset; decode_u8);
                let (offset, stable_data_version) = dec_try!(buf, offset; decode_u8);
                let (offset, leader_router_id) = dec_try!(buf, offset; decode_u8);
                stream_done!(
                    offset,
                    Tlv::LeaderData {
                        partition_id,
                        weighting,
                        data_version,
                        stable_data_version,
                        leader_router_id,
                    }
                )
            }
            TlvType::NetworkData => stream_done!(
                offset + length as usize,
                Tlv::NetworkData(&buf[offset..offset + length as usize])
            ),
            TlvType::TlvRequest => stream_done!(
                offset + length as usize,
                Tlv::TlvRequest(&buf[offset..offset + length as usize])
            ),
            TlvType::ScanMask => {
                let (offset, scan_mask) = dec_try!(buf, offset; decode_u8);
                stream_done!(offset, Tlv::ScanMask(scan_mask))
            }
            TlvType::Connectivity => {
                let (offset, parent_priority) = dec_try!(buf, offset; decode_u8);
                let (offset, link_quality_3) = dec_try!(buf, offset; decode_u8);
                let (offset, link_quality_2) = dec_try!(buf, offset; decode_u8);
                let (offset, link_quality_1) = dec_try!(buf, offset; decode_u8);
                let (offset, leader_cost) = dec_try!(buf, offset; decode_u8);
                let (offset, id_sequence) = dec_try!(buf, offset; decode_u8);
                let (offset, active_routers) = dec_try!(buf, offset; decode_u8);
                let mut offset = offset;
                let mut sed_buffer_size = None;
                if offset + mem::size_of::<u16>() < length as usize {
                    let (new_offset, sed_buffer_size_raw) = dec_try!(buf, offset; decode_u16);
                    offset = new_offset;
                    sed_buffer_size = Some(sed_buffer_size_raw);
                }
                let mut sed_datagram_count = None;
                if offset + mem::size_of::<u8>() < length as usize {
                    let (new_offset, sed_datagram_count_raw) = dec_try!(buf, offset; decode_u8);
                    offset = new_offset;
                    sed_datagram_count = Some(sed_datagram_count_raw);
                }
                stream_done!(
                    offset,
                    Tlv::Connectivity {
                        parent_priority,
                        link_quality_3,
                        link_quality_2,
                        link_quality_1,
                        leader_cost,
                        id_sequence,
                        active_routers,
                        sed_buffer_size,
                        sed_datagram_count,
                    }
                )
            }
            TlvType::LinkMargin => {
                let (offset, link_margin) = dec_try!(buf, offset; decode_u8);
                stream_done!(offset, Tlv::LinkMargin(link_margin))
            }
            TlvType::Status => {
                let (offset, status) = dec_try!(buf, offset; decode_u8);
                stream_done!(offset, Tlv::Status(status))
            }
            TlvType::Version => {
                let (offset, version) = dec_try!(buf, offset; decode_u16);
                stream_done!(offset, Tlv::Version(version))
            }
            TlvType::ActiveOperationalDataset => stream_done!(
                offset + length as usize,
                Tlv::ActiveOperationalDataset(&buf[offset..offset + length as usize])
            ),
            TlvType::PendingOperationalDataset => stream_done!(
                offset + length as usize,
                Tlv::PendingOperationalDataset(&buf[offset..offset + length as usize])
            ),
            TlvType::NotPresent => stream_err!(),
        }
    }
}

/// Value encoded in the type field of a Type-Length-Value (TLV)
/// structure.
#[repr(u8)]
pub enum TlvType {
    SourceAddress = 0,
    Mode = 1,
    Timeout = 2,
    Challenge = 3,
    Response = 4,
    LinkLayerFrameCounter = 5,
    // LinkQuality = 6,         // TLV type not used in Thread
    // NetworkParameter = 7,    // TLV type not used in Thread
    MleFrameCounter = 8,
    /*
    TODO: Not required to implement MLE for SED
    Route64 = 9,
    */
    Address16 = 10,
    LeaderData = 11,
    NetworkData = 12,
    TlvRequest = 13,
    ScanMask = 14,
    Connectivity = 15,
    LinkMargin = 16,
    Status = 17,
    Version = 18,
    /*
    TODO: Not required to implement MLE for SED
    AddressRegistration = 19,
    Channel = 20,
    PanId = 21,
    ActiveTimestamp = 22,
    PendingTimestamp = 23,
    */
    ActiveOperationalDataset = 24,
    PendingOperationalDataset = 25,
    /*
    TODO: Not required to implement MLE for SED
    ThreadDiscovery = 26,
    */
    NotPresent,
}

impl From<u8> for TlvType {
    fn from(tlv_type: u8) -> Self {
        match tlv_type {
            0 => TlvType::SourceAddress,
            1 => TlvType::Mode,
            2 => TlvType::Timeout,
            3 => TlvType::Challenge,
            4 => TlvType::Response,
            5 => TlvType::LinkLayerFrameCounter,
            8 => TlvType::MleFrameCounter,
            10 => TlvType::Address16,
            11 => TlvType::LeaderData,
            12 => TlvType::NetworkData,
            13 => TlvType::TlvRequest,
            14 => TlvType::ScanMask,
            15 => TlvType::Connectivity,
            16 => TlvType::LinkMargin,
            17 => TlvType::Status,
            18 => TlvType::Version,
            24 => TlvType::ActiveOperationalDataset,
            25 => TlvType::PendingOperationalDataset,
            _ => TlvType::NotPresent,
        }
    }
}

impl From<&Tlv<'_>> for TlvType {
    fn from(tlv: &Tlv<'_>) -> Self {
        match *tlv {
            Tlv::SourceAddress(_) => TlvType::SourceAddress,
            Tlv::Mode(_) => TlvType::Mode,
            Tlv::Timeout(_) => TlvType::Timeout,
            Tlv::Challenge(_) => TlvType::Challenge,
            Tlv::Response(_) => TlvType::Response,
            Tlv::LinkLayerFrameCounter(_) => TlvType::LinkLayerFrameCounter,
            Tlv::MleFrameCounter(_) => TlvType::MleFrameCounter,
            Tlv::Address16(_) => TlvType::Address16,
            Tlv::LeaderData { .. } => TlvType::LeaderData,
            Tlv::NetworkData(_) => TlvType::NetworkData,
            Tlv::TlvRequest(_) => TlvType::TlvRequest,
            Tlv::ScanMask(_) => TlvType::ScanMask,
            Tlv::Connectivity { .. } => TlvType::Connectivity,
            Tlv::LinkMargin(_) => TlvType::LinkMargin,
            Tlv::Status(_) => TlvType::Status,
            Tlv::Version(_) => TlvType::Version,
            Tlv::ActiveOperationalDataset(_) => TlvType::ActiveOperationalDataset,
            Tlv::PendingOperationalDataset(_) => TlvType::PendingOperationalDataset,
        }
    }
}

/// Used in Mode TLV.
#[repr(u8)]
pub enum LinkMode {
    ReceiverOnWhenIdle = 0b0000_1000,
    SecureDataRequests = 0b0000_0100,
    FullThreadDevice = 0b0000_0010,
    FullNetworkDataRequired = 0b0000_0001,
}

/// Used in Scan Mask TLV.
#[repr(u8)]
pub enum MulticastResponder {
    Router = 0b1000_0000,
    EndDevice = 0b0100_0000,
}

/// Used in Connectivity TLV.
pub enum ParentPriority {
    // Reserved = 0b1000_0000
    High = 0b0100_0000,
    Medium = 0b0000_0000,
    Low = 0b1100_0000,
}

/// These TLVs are contained within the value of a Network Data TLV.
/// See Section 5.18.
pub enum NetworkDataTlv<'a> {
    Prefix {
        domain_id: u8,
        prefix_length_bits: u8,
        prefix: [u8; 3], // IPv6 prefix max length 48 bits.
        sub_tlvs: &'a [u8],
    },
    CommissioningData {
        com_length: u8,
        com_data: [u8; MAX_VALUE_FIELD_LENGTH],
    },
    Service {
        thread_enterprise_number: bool,
        // See 5.18.6.
        s_id: u8,
        s_enterprise_number: u32,
        s_service_data_length: u8,
        s_service_data: [u8; MAX_VALUE_FIELD_LENGTH],
        sub_tlvs: &'a [u8],
    },
}

impl NetworkDataTlv<'_> {
    /// Serializes TLV data in `buf` into the format specific to the
    /// Network Data TLV type.
    pub fn encode(&self, buf: &mut [u8], stable: bool) -> SResult {
        match *self {
            NetworkDataTlv::Prefix {
                domain_id,
                prefix_length_bits,
                prefix,
                sub_tlvs,
            } => {
                let value_width =
                    mem::size_of::<u8>() + mem::size_of::<u8>() + prefix.len() + sub_tlvs.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width, stable);
                offset = enc_consume!(buf, offset; encode_u8, domain_id);
                offset = enc_consume!(buf, offset; encode_u8, prefix_length_bits);
                offset = enc_consume!(buf, offset; encode_bytes_be, &prefix);
                offset = enc_consume!(buf, offset; encode_bytes, sub_tlvs);
                stream_done!(offset)
            }
            NetworkDataTlv::CommissioningData {
                com_length,
                com_data,
            } => {
                let value_width = com_length as usize;
                let mut offset = enc_consume!(buf; self; encode_tl, value_width, stable);
                offset = enc_consume!(buf, offset; encode_bytes_be, &com_data);
                stream_done!(offset)
            }
            NetworkDataTlv::Service {
                thread_enterprise_number,
                s_id,
                s_enterprise_number,
                s_service_data_length,
                s_service_data,
                sub_tlvs,
            } => {
                let value_width = mem::size_of::<u8>()
                    + mem::size_of::<u32>()
                    + mem::size_of::<u8>()
                    + s_service_data.len()
                    + sub_tlvs.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width, stable);
                let t_bit: u8 = if thread_enterprise_number {
                    1u8 << 7
                } else {
                    0
                };
                let first_byte: u8 = t_bit | (0b1111 & s_id);
                offset = enc_consume!(buf, offset; encode_u8, first_byte);
                offset = enc_consume!(buf, offset; encode_u32, s_enterprise_number.to_be());
                offset = enc_consume!(buf, offset; encode_u8, s_service_data_length);
                offset = enc_consume!(buf, offset; encode_bytes_be, &s_service_data);
                offset = enc_consume!(buf, offset; encode_bytes, sub_tlvs);
                stream_done!(offset)
            }
        }
    }

    fn encode_tl(&self, buf: &mut [u8], value_width: usize, stable: bool) -> SResult {
        stream_len_cond!(buf, TL_WIDTH + value_width);
        let stable_bit = u8::from(stable);
        buf[0] = (NetworkDataTlvType::from(self) as u8) << 1 | stable_bit;
        buf[1] = value_width as u8;
        stream_done!(TL_WIDTH)
    }

    /// Deserializes TLV data from `buf` into the Network Data TLV variant
    /// specific to the TLV type.
    /// Returns NetworkDataTlv and true if the data stable, false
    /// otherwise.
    /// `SResult::Error` is returned if the type field does not match any
    /// implemented TLV type.
    pub fn decode(buf: &[u8]) -> SResult<(NetworkDataTlv, bool)> {
        let (offset, tlv_type_field) = dec_try!(buf; decode_u8);
        let tlv_type_raw = tlv_type_field >> 1;
        let tlv_type = NetworkDataTlvType::from(tlv_type_raw);
        let stable = (tlv_type_field & 1u8) > 0;
        let (offset, length) = dec_try!(buf, offset; decode_u8);
        match tlv_type {
            NetworkDataTlvType::Prefix => {
                let (offset, domain_id) = dec_try!(buf, offset; decode_u8);
                let (offset, prefix_length_bits) = dec_try!(buf, offset; decode_u8);
                let mut prefix = [0u8; 3];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut prefix);
                stream_done!(
                    offset + length as usize,
                    (
                        NetworkDataTlv::Prefix {
                            domain_id,
                            prefix_length_bits,
                            prefix,
                            sub_tlvs: &buf[offset..offset + length as usize],
                        },
                        stable
                    )
                )
            }
            NetworkDataTlvType::CommissioningData => {
                let (offset, com_length) = dec_try!(buf, offset; decode_u8);
                let mut com_data = [0u8; MAX_VALUE_FIELD_LENGTH];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut com_data);
                stream_done!(
                    offset,
                    (
                        NetworkDataTlv::CommissioningData {
                            com_length,
                            com_data,
                        },
                        stable
                    )
                )
            }
            NetworkDataTlvType::Service => {
                let (offset, first_byte) = dec_try!(buf, offset; decode_u8);
                let thread_enterprise_number = (first_byte >> 7) > 0;
                let s_id = first_byte & 0b1111;
                let (offset, s_enterprise_number) = dec_try!(buf, offset; decode_u32);
                let (offset, s_service_data_length) = dec_try!(buf, offset; decode_u8);
                let mut s_service_data = [0u8; MAX_VALUE_FIELD_LENGTH];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut s_service_data);
                stream_done!(
                    offset + length as usize,
                    (
                        NetworkDataTlv::Service {
                            thread_enterprise_number,
                            s_id,
                            s_enterprise_number,
                            s_service_data_length,
                            s_service_data,
                            sub_tlvs: &buf[offset..offset + length as usize],
                        },
                        stable
                    )
                )
            }
            NetworkDataTlvType::NotPresent => stream_err!(),
        }
    }
}

/// Value encoded in the type field of a Network Data TLV.
/// Gaps in type numbers are filled by PrefixSubTlv and ServiceSubTlv.
#[repr(u8)]
pub enum NetworkDataTlvType {
    Prefix = 1,
    CommissioningData = 4,
    Service = 5,
    NotPresent,
}

impl From<u8> for NetworkDataTlvType {
    fn from(tlv_type: u8) -> Self {
        match tlv_type {
            1 => NetworkDataTlvType::Prefix,
            4 => NetworkDataTlvType::CommissioningData,
            5 => NetworkDataTlvType::Service,
            _ => NetworkDataTlvType::NotPresent,
        }
    }
}

impl From<&NetworkDataTlv<'_>> for NetworkDataTlvType {
    fn from(network_data_tlv: &NetworkDataTlv<'_>) -> Self {
        match *network_data_tlv {
            NetworkDataTlv::Prefix { .. } => NetworkDataTlvType::Prefix,
            NetworkDataTlv::CommissioningData { .. } => NetworkDataTlvType::CommissioningData,
            NetworkDataTlv::Service { .. } => NetworkDataTlvType::Service,
        }
    }
}

/// These TLVs are contained within the value of a Prefix TLV.
pub enum PrefixSubTlv<'a> {
    HasRoute(&'a [u8]),
    BorderRouter(&'a [u8]),
    SixLoWpanId {
        context_id_compress: bool,
        context_id: u8,
        context_length: u8,
    },
}

impl PrefixSubTlv<'_> {
    /// Serializes TLV data in `buf` into the format specific to the
    /// Prefix sub-TLV type.
    pub fn encode(&self, buf: &mut [u8], stable: bool) -> SResult {
        match *self {
            PrefixSubTlv::HasRoute(r_border_router_16s) => {
                let value_width = r_border_router_16s.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width, stable);
                offset = enc_consume!(buf, offset; encode_bytes, r_border_router_16s);
                stream_done!(offset)
            }
            PrefixSubTlv::BorderRouter(p_border_router_16s) => {
                let value_width = p_border_router_16s.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width, stable);
                offset = enc_consume!(buf, offset; encode_bytes, p_border_router_16s);
                stream_done!(offset)
            }
            PrefixSubTlv::SixLoWpanId {
                context_id_compress,
                context_id,
                context_length,
            } => {
                let value_width = mem::size_of::<u8>() + mem::size_of::<u8>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width, stable);
                let compress_bit = u8::from(context_id_compress);
                let first_byte = (compress_bit << 4) | (context_id & 0b1111);
                offset = enc_consume!(buf, offset; encode_u8, first_byte);
                offset = enc_consume!(buf, offset; encode_u8, context_length);
                stream_done!(offset)
            }
        }
    }

    fn encode_tl(&self, buf: &mut [u8], value_width: usize, stable: bool) -> SResult {
        stream_len_cond!(buf, TL_WIDTH + value_width);
        let stable_bit = u8::from(stable);
        buf[0] = (PrefixSubTlvType::from(self) as u8) << 1 | stable_bit;
        buf[1] = value_width as u8;
        stream_done!(TL_WIDTH)
    }

    /// Deserializes TLV data from `buf` into the Prefix sub-TLV variant
    /// specific to the TLV type.
    /// Returns PrefixSubTlv and true if the data stable, false
    /// otherwise.
    /// `SResult::Error` is returned if the type field does not match any
    /// implemented TLV type.
    pub fn decode(buf: &[u8]) -> SResult<(PrefixSubTlv, bool)> {
        let (offset, tlv_type_field) = dec_try!(buf; decode_u8);
        let tlv_type_raw = tlv_type_field >> 1;
        let tlv_type = PrefixSubTlvType::from(tlv_type_raw);
        let stable = (tlv_type_field & 1u8) > 0;
        let (offset, length) = dec_try!(buf, offset; decode_u8);
        match tlv_type {
            PrefixSubTlvType::HasRoute => stream_done!(
                offset + length as usize,
                (
                    PrefixSubTlv::HasRoute(&buf[offset..offset + length as usize]),
                    stable
                )
            ),
            PrefixSubTlvType::BorderRouter => stream_done!(
                offset + length as usize,
                (
                    PrefixSubTlv::BorderRouter(&buf[offset..offset + length as usize]),
                    stable
                )
            ),
            PrefixSubTlvType::SixLoWpanId => {
                let (offset, first_byte) = dec_try!(buf, offset; decode_u8);
                let context_id_compress = (first_byte & 0b1_0000) > 0;
                let context_id = first_byte & 0b1111;
                let (offset, context_length) = dec_try!(buf, offset; decode_u8);
                stream_done!(
                    offset,
                    (
                        PrefixSubTlv::SixLoWpanId {
                            context_id_compress,
                            context_id,
                            context_length,
                        },
                        stable
                    )
                )
            }
            PrefixSubTlvType::NotPresent => stream_err!(),
        }
    }
}

/// Value encoded in the type field of a Prefix sub-TLV.
/// Gaps in type numbers are filled by NetworkDataTlv and ServiceSubTlv.
#[repr(u8)]
pub enum PrefixSubTlvType {
    HasRoute = 0,
    BorderRouter = 2,
    SixLoWpanId = 3,
    NotPresent,
}

impl From<u8> for PrefixSubTlvType {
    fn from(tlv_type: u8) -> Self {
        match tlv_type {
            0 => PrefixSubTlvType::HasRoute,
            2 => PrefixSubTlvType::BorderRouter,
            3 => PrefixSubTlvType::SixLoWpanId,
            _ => PrefixSubTlvType::NotPresent,
        }
    }
}

impl From<&PrefixSubTlv<'_>> for PrefixSubTlvType {
    fn from(prefix_sub_tlv: &PrefixSubTlv<'_>) -> Self {
        match *prefix_sub_tlv {
            PrefixSubTlv::HasRoute(_) => PrefixSubTlvType::HasRoute,
            PrefixSubTlv::BorderRouter(_) => PrefixSubTlvType::BorderRouter,
            PrefixSubTlv::SixLoWpanId { .. } => PrefixSubTlvType::SixLoWpanId,
        }
    }
}

/// Used in Has Route TLV.
pub struct HasRouteTlvValue {
    // See 5.18.1.
    r_border_router_16: u16,
    r_preference: u8,
}

impl HasRouteTlvValue {
    /// Serializes this Has Route TLV value into `buf`.
    pub fn encode(&self, buf: &mut [u8]) -> SResult {
        stream_len_cond!(buf, 3);
        let mut offset = enc_consume!(buf, 0; encode_u16, self.r_border_router_16.to_be());
        let last_byte = (self.r_preference & 0b11) << 6;
        offset = enc_consume!(buf, offset; encode_u8, last_byte);
        stream_done!(offset)
    }

    /// Deserializes Has Route TLV value from `buf` and returns it.
    pub fn decode(buf: &[u8]) -> SResult<HasRouteTlvValue> {
        stream_len_cond!(buf, 3);
        let (offset, r_border_router_16) = dec_try!(buf; decode_u16);
        let (offset, last_byte) = dec_try!(buf, offset; decode_u8);
        let r_preference = last_byte >> 6;
        stream_done!(
            offset,
            HasRouteTlvValue {
                r_border_router_16,
                r_preference,
            }
        )
    }
}

/// Used in Border Router TLV.
pub struct BorderRouterTlvValue {
    // See 5.18.3.
    p_border_router_16: u16,
    p_bits: u16,
}

/// Used in Border Router TLV value.
#[repr(u16)]
pub enum BorderRouterTlvValueBit {
    // See 5.18.3 for a more detailed explanation of each.
    Prf = 0b1100_0000_0000_0000, // Preference
    P = 0b0010_0000_0000_0000,   // Preferred
    S = 0b0001_0000_0000_0000,   // SLAAC
    D = 0b0000_1000_0000_0000,   // DHCP
    C = 0b0000_0100_0000_0000,   // Configure
    R = 0b0000_0010_0000_0000,   // Default
    O = 0b0000_0001_0000_0000,   // On mesh
    N = 0b0000_0000_1000_0000,   // NDDNS
}

impl BorderRouterTlvValue {
    /// Serializes this Border Route TLV value into `buf`.
    pub fn encode(&self, buf: &mut [u8]) -> SResult {
        stream_len_cond!(buf, 4); // Each Border Router TLV value is 32 bits wide.
        let mut offset = enc_consume!(buf, 0; encode_u16, self.p_border_router_16.to_be());
        offset = enc_consume!(buf, offset; encode_u16, self.p_bits.to_be());
        stream_done!(offset)
    }

    /// Deserializes Border Route TLV value from `buf` and returns it.
    pub fn decode(buf: &[u8]) -> SResult<BorderRouterTlvValue> {
        let (offset, p_border_router_16) = dec_try!(buf; decode_u16);
        let (offset, p_bits) = dec_try!(buf, offset; decode_u16);
        stream_done!(
            offset,
            BorderRouterTlvValue {
                p_border_router_16,
                p_bits,
            }
        )
    }
}

/// These TLVs are contained within the value of a Service TLV.
pub enum ServiceSubTlv {
    Server {
        // See 5.18.6.
        s_server_16: u16,
        s_server_data: [u8; MAX_VALUE_FIELD_LENGTH],
    },
}

impl ServiceSubTlv {
    /// Serializes TLV data in `buf` into the format specific to the
    /// Service sub-TLV type.
    pub fn encode(&self, buf: &mut [u8], stable: bool) -> SResult {
        match *self {
            ServiceSubTlv::Server {
                s_server_16,
                s_server_data,
            } => {
                let value_width = mem::size_of::<u16>() + s_server_data.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width, stable);
                offset = enc_consume!(buf, offset; encode_u16, s_server_16.to_be());
                offset = enc_consume!(buf, offset; encode_bytes_be, &s_server_data);
                stream_done!(offset)
            }
        }
    }

    fn encode_tl(&self, buf: &mut [u8], value_width: usize, stable: bool) -> SResult {
        stream_len_cond!(buf, TL_WIDTH + value_width);
        let stable_bit = u8::from(stable);
        buf[0] = (ServiceSubTlvType::from(self) as u8) << 1 | stable_bit;
        buf[1] = value_width as u8;
        stream_done!(TL_WIDTH)
    }

    /// Deserializes TLV data from `buf` into the Service sub-TLV variant
    /// specific to the TLV type.
    /// Returns ServiceSubTlv and true if the data stable, false
    /// otherwise.
    /// `SResult::Error` is returned if the type field does not match any
    /// implemented TLV type.
    pub fn decode(buf: &[u8]) -> SResult<(ServiceSubTlv, bool)> {
        let (offset, tlv_type_field) = dec_try!(buf; decode_u8);
        let tlv_type_raw = tlv_type_field >> 1;
        let tlv_type = ServiceSubTlvType::from(tlv_type_raw);
        let stable = (tlv_type_field & 1u8) > 0;
        let (offset, _) = dec_try!(buf, offset; decode_u8);
        match tlv_type {
            ServiceSubTlvType::Server => {
                let (offset, s_server_16) = dec_try!(buf, offset; decode_u16);
                let mut s_server_data = [0u8; MAX_VALUE_FIELD_LENGTH];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut s_server_data);
                stream_done!(
                    offset,
                    (
                        ServiceSubTlv::Server {
                            s_server_16,
                            s_server_data,
                        },
                        stable
                    )
                )
            }
            ServiceSubTlvType::NotPresent => stream_err!(),
        }
    }
}

/// Value encoded in the type field of a Service sub-TLV.
/// Gaps in type numbers are filled by NetworkDataTlv and PrefixSubTlv.
#[repr(u8)]
pub enum ServiceSubTlvType {
    Server = 6,
    NotPresent,
}

impl From<u8> for ServiceSubTlvType {
    fn from(tlv_type: u8) -> Self {
        match tlv_type {
            6 => ServiceSubTlvType::Server,
            _ => ServiceSubTlvType::NotPresent,
        }
    }
}

impl From<&ServiceSubTlv> for ServiceSubTlvType {
    fn from(service_sub_tlv: &ServiceSubTlv) -> Self {
        match *service_sub_tlv {
            ServiceSubTlv::Server { .. } => ServiceSubTlvType::Server,
        }
    }
}

/// These TLVs are contained within the value of a Pending Operational
/// Dataset TLV or an Active Operational Dataset TLV.
/// See Section 8.10.1.
pub enum NetworkManagementTlv<'a> {
    Channel {
        channel_page: u8,
        channel: u16,
    },
    PanId(u16),
    ExtendedPanId([u8; 8]),             // Extended PAN ID length 8 bytes.
    NetworkName([u8; 16]),              // Network name max length 16 bytes.
    Pskc([u8; 16]),                     // PSKc max length 16 bytes.
    NetworkMasterKey([u8; 16]),         // Master key length 128 bits = 16 bytes.
    NetworkKeySequenceCounter([u8; 4]), // Counter length 4 bytes.
    NetworkMeshLocalPrefix([u8; 8]),    // Mesh-Local Prefix length 8 bytes.
    SteeringData([u8; 16]),             // Bloom filter max length 16 bytes.
    BorderAgentLocator(u16),
    CommissionerId([u8; 64]), // Commissioner ID max length 64 bytes.
    CommissionerSessionId(u16),
    SecurityPolicy {
        rotation_time: u16,
        policy_bits: u8,
    },
    ActiveTimestamp {
        timestamp_seconds: [u8; 3], // Timestamp seconds is a 48-bit Unix time value.
        timestamp_ticks: u16,
        u_bit: bool,
    },
    CommissionerUdpPort(u16),
    PendingTimestamp {
        timestamp_seconds: [u8; 3], // Timestamp seconds is a 48-bit Unix time value.
        timestamp_ticks: u16,
        u_bit: bool,
    },
    DelayTimer(u32),
    ChannelMask(&'a [u8]),
}

impl NetworkManagementTlv<'_> {
    /// Serializes TLV data in `buf` into the format specific to the
    /// Network Management TLV type.
    pub fn encode(&self, buf: &mut [u8]) -> SResult {
        match *self {
            NetworkManagementTlv::Channel {
                channel_page,
                channel,
            } => {
                // `channel_page` should be 0 (See 8.10.1.1.1)
                // `channel` should be 11-26 (See 8.10.1.1.2)
                let value_width = mem::size_of::<u8>() + mem::size_of::<u16>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u8, channel_page);
                offset = enc_consume!(buf, offset; encode_u16, channel.to_be());
                stream_done!(offset)
            }
            NetworkManagementTlv::PanId(ref pan_id) => {
                let value_width = mem::size_of::<u16>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u16, pan_id.to_be());
                stream_done!(offset)
            }
            NetworkManagementTlv::ExtendedPanId(ref extended_pan_id) => {
                let value_width = extended_pan_id.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes_be, extended_pan_id);
                stream_done!(offset)
            }
            NetworkManagementTlv::NetworkName(ref network_name) => {
                stream_cond!(network_name.len() <= 16);
                let value_width = network_name.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes_be, network_name);
                stream_done!(offset)
            }
            NetworkManagementTlv::Pskc(ref pskc) => {
                stream_cond!(pskc.len() <= 16);
                let value_width = pskc.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes_be, pskc);
                stream_done!(offset)
            }
            NetworkManagementTlv::NetworkMasterKey(ref network_key) => {
                let value_width = network_key.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes_be, network_key);
                stream_done!(offset)
            }
            NetworkManagementTlv::NetworkKeySequenceCounter(ref counter) => {
                let value_width = counter.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes_be, counter);
                stream_done!(offset)
            }
            NetworkManagementTlv::NetworkMeshLocalPrefix(ref prefix) => {
                let value_width = prefix.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes_be, prefix);
                stream_done!(offset)
            }
            NetworkManagementTlv::SteeringData(ref bloom_filter) => {
                stream_cond!(bloom_filter.len() <= 16);
                let value_width = bloom_filter.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes_be, bloom_filter);
                stream_done!(offset)
            }
            NetworkManagementTlv::BorderAgentLocator(ref rloc_16) => {
                let value_width = mem::size_of::<u16>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u16, rloc_16.to_be());
                stream_done!(offset)
            }
            NetworkManagementTlv::CommissionerId(ref commissioner_id) => {
                stream_cond!(commissioner_id.len() <= 64);
                let value_width = commissioner_id.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes_be, commissioner_id);
                stream_done!(offset)
            }
            NetworkManagementTlv::CommissionerSessionId(ref session_id) => {
                let value_width = mem::size_of::<u16>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u16, session_id.to_be());
                stream_done!(offset)
            }
            NetworkManagementTlv::SecurityPolicy {
                rotation_time,
                policy_bits,
            } => {
                let value_width = mem::size_of::<u16>() + mem::size_of::<u8>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u16, rotation_time.to_be());
                offset = enc_consume!(buf, offset; encode_u8, policy_bits);
                stream_done!(offset)
            }
            NetworkManagementTlv::ActiveTimestamp {
                timestamp_seconds,
                timestamp_ticks,
                u_bit,
            } => {
                let value_width = timestamp_seconds.len() + mem::size_of::<u16>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes_be, &timestamp_seconds);
                let u_bit_val = u16::from(u_bit);
                let end_bytes = (timestamp_ticks << 1) | u_bit_val;
                offset = enc_consume!(buf, offset; encode_u16, end_bytes.to_be());
                stream_done!(offset)
            }
            NetworkManagementTlv::CommissionerUdpPort(udp_port) => {
                let value_width = mem::size_of::<u16>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u16, udp_port.to_be());
                stream_done!(offset)
            }
            NetworkManagementTlv::PendingTimestamp {
                timestamp_seconds,
                timestamp_ticks,
                u_bit,
            } => {
                let value_width = timestamp_seconds.len() + mem::size_of::<u16>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes_be, &timestamp_seconds);
                let u_bit_val = u16::from(u_bit);
                let end_bytes = (timestamp_ticks << 1) | u_bit_val;
                offset = enc_consume!(buf, offset; encode_u16, end_bytes.to_be());
                stream_done!(offset)
            }
            NetworkManagementTlv::DelayTimer(time_remaining) => {
                let value_width = mem::size_of::<u32>();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_u32, time_remaining.to_be());
                stream_done!(offset)
            }
            NetworkManagementTlv::ChannelMask(entries) => {
                let value_width = entries.len();
                let mut offset = enc_consume!(buf; self; encode_tl, value_width);
                offset = enc_consume!(buf, offset; encode_bytes, entries);
                stream_done!(offset)
            }
        }
    }

    fn encode_tl(&self, buf: &mut [u8], value_width: usize) -> SResult {
        stream_len_cond!(buf, TL_WIDTH + value_width);
        buf[0] = NetworkManagementTlvType::from(self) as u8;
        buf[1] = value_width as u8;
        stream_done!(TL_WIDTH)
    }

    /// Deserializes TLV data from `buf` into the Network Management TLV
    /// variant specific to the TLV type.
    /// Returns ServiceSubTlv and true if the data stable, false
    /// otherwise.
    /// `SResult::Error` is returned if the type field does not match any
    /// implemented TLV type.
    pub fn decode(buf: &[u8]) -> SResult<NetworkManagementTlv> {
        let (offset, tlv_type_raw) = dec_try!(buf; decode_u8);
        let tlv_type = NetworkManagementTlvType::from(tlv_type_raw);
        let (offset, length) = dec_try!(buf, offset; decode_u8);
        match tlv_type {
            NetworkManagementTlvType::Channel => {
                let (offset, channel_page) = dec_try!(buf, offset; decode_u8);
                let (offset, channel) = dec_try!(buf, offset; decode_u16);
                stream_done!(
                    offset,
                    NetworkManagementTlv::Channel {
                        channel_page,
                        channel,
                    }
                )
            }
            NetworkManagementTlvType::PanId => {
                let (offset, pan_id) = dec_try!(buf, offset; decode_u16);
                stream_done!(offset, NetworkManagementTlv::PanId(pan_id))
            }
            NetworkManagementTlvType::ExtendedPanId => {
                let mut extended_pan_id = [0u8; 8];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut extended_pan_id);
                stream_done!(offset, NetworkManagementTlv::ExtendedPanId(extended_pan_id))
            }
            NetworkManagementTlvType::NetworkName => {
                let mut network_name = [0u8; 16];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut network_name);
                stream_done!(offset, NetworkManagementTlv::NetworkName(network_name))
            }
            NetworkManagementTlvType::Pskc => {
                let mut pskc = [0u8; 16];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut pskc);
                stream_done!(offset, NetworkManagementTlv::Pskc(pskc))
            }
            NetworkManagementTlvType::NetworkMasterKey => {
                let mut network_key = [0u8; 16];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut network_key);
                stream_done!(offset, NetworkManagementTlv::NetworkMasterKey(network_key))
            }
            NetworkManagementTlvType::NetworkKeySequenceCounter => {
                let mut counter = [0u8; 4];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut counter);
                stream_done!(
                    offset,
                    NetworkManagementTlv::NetworkKeySequenceCounter(counter)
                )
            }
            NetworkManagementTlvType::NetworkMeshLocalPrefix => {
                let mut prefix = [0u8; 8];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut prefix);
                stream_done!(offset, NetworkManagementTlv::NetworkMeshLocalPrefix(prefix))
            }
            NetworkManagementTlvType::SteeringData => {
                let mut bloom_filter = [0u8; 16];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut bloom_filter);
                stream_done!(offset, NetworkManagementTlv::SteeringData(bloom_filter))
            }
            NetworkManagementTlvType::BorderAgentLocator => {
                let (offset, rloc_16) = dec_try!(buf, offset; decode_u16);
                stream_done!(offset, NetworkManagementTlv::BorderAgentLocator(rloc_16))
            }
            NetworkManagementTlvType::CommissionerId => {
                let mut commissioner_id = [0u8; 64];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut commissioner_id);
                stream_done!(
                    offset,
                    NetworkManagementTlv::CommissionerId(commissioner_id)
                )
            }
            NetworkManagementTlvType::CommissionerSessionId => {
                let (offset, session_id) = dec_try!(buf, offset; decode_u16);
                stream_done!(
                    offset,
                    NetworkManagementTlv::CommissionerSessionId(session_id)
                )
            }
            NetworkManagementTlvType::SecurityPolicy => {
                let (offset, rotation_time) = dec_try!(buf, offset; decode_u16);
                let (offset, policy_bits) = dec_try!(buf, offset; decode_u8);
                stream_done!(
                    offset,
                    NetworkManagementTlv::SecurityPolicy {
                        rotation_time,
                        policy_bits,
                    }
                )
            }
            NetworkManagementTlvType::ActiveTimestamp => {
                let mut timestamp_seconds = [0u8; 3];
                let offset = dec_consume!(buf, offset; decode_bytes_be, &mut timestamp_seconds);
                let (offset, timestamp_ticks) = dec_try!(buf, offset; decode_u16);
                stream_done!(
                    offset,
                    NetworkManagementTlv::ActiveTimestamp {
                        timestamp_seconds,
                        timestamp_ticks: timestamp_ticks >> 1,
                        u_bit: (timestamp_ticks & 1u16) > 0,
                    }
                )
            }
            NetworkManagementTlvType::CommissionerUdpPort => {
                let (offset, udp_port) = dec_try!(buf, offset; decode_u16);
                stream_done!(offset, NetworkManagementTlv::CommissionerUdpPort(udp_port))
            }
            NetworkManagementTlvType::PendingTimestamp => {
                let mut timestamp_seconds = [0u8; 3];
                let offset = dec_consume!(buf; decode_bytes_be, &mut timestamp_seconds);
                let (offset, timestamp_ticks) = dec_try!(buf, offset; decode_u16);
                stream_done!(
                    offset,
                    NetworkManagementTlv::PendingTimestamp {
                        timestamp_seconds,
                        timestamp_ticks: timestamp_ticks >> 1,
                        u_bit: (timestamp_ticks & 1u16) > 0,
                    }
                )
            }
            NetworkManagementTlvType::DelayTimer => {
                let (offset, time_remaining) = dec_try!(buf, offset; decode_u32);
                stream_done!(offset, NetworkManagementTlv::DelayTimer(time_remaining))
            }
            NetworkManagementTlvType::ChannelMask => stream_done!(
                offset + length as usize,
                NetworkManagementTlv::ChannelMask(&buf[offset..offset + length as usize])
            ),
            NetworkManagementTlvType::NotPresent => stream_err!(),
        }
    }
}

/// Value encoded in the type field of a Network Management TLV.
#[repr(u8)]
pub enum NetworkManagementTlvType {
    Channel = 0,
    PanId = 1,
    ExtendedPanId = 2,
    NetworkName = 3,
    Pskc = 4,
    NetworkMasterKey = 5,
    NetworkKeySequenceCounter = 6,
    NetworkMeshLocalPrefix = 7,
    SteeringData = 8,
    BorderAgentLocator = 9,
    CommissionerId = 10,
    CommissionerSessionId = 11,
    SecurityPolicy = 12,
    ActiveTimestamp = 14,
    CommissionerUdpPort = 15,
    PendingTimestamp = 51,
    DelayTimer = 52,
    ChannelMask = 53,
    NotPresent,
}

impl From<u8> for NetworkManagementTlvType {
    fn from(type_num: u8) -> Self {
        match type_num {
            0 => NetworkManagementTlvType::Channel,
            1 => NetworkManagementTlvType::PanId,
            2 => NetworkManagementTlvType::ExtendedPanId,
            3 => NetworkManagementTlvType::NetworkName,
            4 => NetworkManagementTlvType::Pskc,
            5 => NetworkManagementTlvType::NetworkMasterKey,
            6 => NetworkManagementTlvType::NetworkKeySequenceCounter,
            7 => NetworkManagementTlvType::NetworkMeshLocalPrefix,
            8 => NetworkManagementTlvType::SteeringData,
            9 => NetworkManagementTlvType::BorderAgentLocator,
            10 => NetworkManagementTlvType::CommissionerId,
            11 => NetworkManagementTlvType::CommissionerSessionId,
            12 => NetworkManagementTlvType::SecurityPolicy,
            14 => NetworkManagementTlvType::ActiveTimestamp,
            15 => NetworkManagementTlvType::CommissionerUdpPort,
            51 => NetworkManagementTlvType::PendingTimestamp,
            52 => NetworkManagementTlvType::DelayTimer,
            53 => NetworkManagementTlvType::ChannelMask,
            _ => NetworkManagementTlvType::NotPresent,
        }
    }
}

impl From<&NetworkManagementTlv<'_>> for NetworkManagementTlvType {
    fn from(network_mgmt_tlv: &NetworkManagementTlv<'_>) -> Self {
        match *network_mgmt_tlv {
            NetworkManagementTlv::Channel { .. } => NetworkManagementTlvType::Channel,
            NetworkManagementTlv::PanId(_) => NetworkManagementTlvType::PanId,
            NetworkManagementTlv::ExtendedPanId(_) => NetworkManagementTlvType::ExtendedPanId,
            NetworkManagementTlv::NetworkName(_) => NetworkManagementTlvType::NetworkName,
            NetworkManagementTlv::Pskc(_) => NetworkManagementTlvType::Pskc,
            NetworkManagementTlv::NetworkMasterKey(_) => NetworkManagementTlvType::NetworkMasterKey,
            NetworkManagementTlv::NetworkKeySequenceCounter(_) => {
                NetworkManagementTlvType::NetworkKeySequenceCounter
            }
            NetworkManagementTlv::NetworkMeshLocalPrefix(_) => {
                NetworkManagementTlvType::NetworkMeshLocalPrefix
            }
            NetworkManagementTlv::SteeringData(_) => NetworkManagementTlvType::SteeringData,
            NetworkManagementTlv::BorderAgentLocator(_) => {
                NetworkManagementTlvType::BorderAgentLocator
            }
            NetworkManagementTlv::CommissionerId(_) => NetworkManagementTlvType::CommissionerId,
            NetworkManagementTlv::CommissionerSessionId(_) => {
                NetworkManagementTlvType::CommissionerSessionId
            }
            NetworkManagementTlv::SecurityPolicy { .. } => NetworkManagementTlvType::SecurityPolicy,
            NetworkManagementTlv::ActiveTimestamp { .. } => {
                NetworkManagementTlvType::ActiveTimestamp
            }
            NetworkManagementTlv::CommissionerUdpPort(_) => {
                NetworkManagementTlvType::CommissionerUdpPort
            }
            NetworkManagementTlv::PendingTimestamp { .. } => {
                NetworkManagementTlvType::PendingTimestamp
            }
            NetworkManagementTlv::DelayTimer(_) => NetworkManagementTlvType::DelayTimer,
            NetworkManagementTlv::ChannelMask(_) => NetworkManagementTlvType::ChannelMask,
        }
    }
}

/// Used in Security Policy TLV.
/// See 8.10.1.15
#[repr(u8)]
pub enum SecurityPolicy {
    O = 0b1000_0000, // Out-of-band commissioning enabled.
    N = 0b0100_0000, // Native commissioning using PSKc is allowed.
    R = 0b0010_0000, // Thread 1.x Routers are enabled.
    C = 0b0001_0000, // External commissioner authentication is allowed using PSKc.
    B = 0b0000_1000, // Thread 1.x Beacons are enabled.
}

/// Used in Channel Mask TLV.
pub struct ChannelMaskEntry {
    channel_page: u8,
    mask_length: u8,
    channel_mask: [u8; MAX_VALUE_FIELD_LENGTH],
}

impl ChannelMaskEntry {
    /// Serializes this Channel Mask Entry into `buf`.
    pub fn encode(&self, buf: &mut [u8]) -> SResult {
        let mut offset = enc_consume!(buf, 0; encode_u8, self.channel_page);
        offset = enc_consume!(buf, offset; encode_u8, self.mask_length);
        offset = enc_consume!(buf, offset; encode_bytes_be, &self.channel_mask);
        stream_done!(offset)
    }

    /// Deserializes Channel Mask Entry from `buf` and returns it.
    pub fn decode(buf: &[u8]) -> SResult<ChannelMaskEntry> {
        let (offset, channel_page) = dec_try!(buf; decode_u8);
        let (offset, mask_length) = dec_try!(buf, offset; decode_u8);
        let mut channel_mask = [0u8; MAX_VALUE_FIELD_LENGTH];
        let offset = dec_consume!(buf, offset; decode_bytes_be, &mut channel_mask);
        stream_done!(
            offset,
            ChannelMaskEntry {
                channel_page,
                mask_length,
                channel_mask,
            }
        )
    }
}