rs-matter 0.3.0

Native Rust implementation of the Matter (Smart-Home) ecosystem
Documentation
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
/*
 *
 *    Copyright (c) 2023-2026 Project CHIP Authors
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

use core::iter::Empty;

use qrcodegen_no_heap::{QrCode, QrCodeEcc, Version};

use verhoeff::Verhoeff;

use crate::error::ErrorCode;
use crate::tlv::{EitherIter, TLVElement, TLVTag, TLV};
use crate::transport::network::mdns::CommissionableFilter;
use crate::utils::codec::base38;
use crate::utils::storage::WriteBuf;

use super::*;

#[cfg(feature = "qr-scan")]
pub mod scan;

/// The prefix of a Matter onboarding QR-code text payload.
pub const QR_PREFIX: &str = "MT:";

// See the spec. QR Code in the Matter specification
const LONG_BITS: usize = 12;
const VERSION_FIELD_LENGTH_IN_BITS: usize = 3;
const VENDOR_IDFIELD_LENGTH_IN_BITS: usize = 16;
const PRODUCT_IDFIELD_LENGTH_IN_BITS: usize = 16;
const COMMISSIONING_FLOW_FIELD_LENGTH_IN_BITS: usize = 2;
const RENDEZVOUS_INFO_FIELD_LENGTH_IN_BITS: usize = 8;
const PAYLOAD_DISCRIMINATOR_FIELD_LENGTH_IN_BITS: usize = LONG_BITS;
const SETUP_PINCODE_FIELD_LENGTH_IN_BITS: usize = 27;
const PADDING_FIELD_LENGTH_IN_BITS: usize = 4;
const TOTAL_PAYLOAD_DATA_SIZE_IN_BITS: usize = VERSION_FIELD_LENGTH_IN_BITS
    + VENDOR_IDFIELD_LENGTH_IN_BITS
    + PRODUCT_IDFIELD_LENGTH_IN_BITS
    + COMMISSIONING_FLOW_FIELD_LENGTH_IN_BITS
    + RENDEZVOUS_INFO_FIELD_LENGTH_IN_BITS
    + PAYLOAD_DISCRIMINATOR_FIELD_LENGTH_IN_BITS
    + SETUP_PINCODE_FIELD_LENGTH_IN_BITS
    + PADDING_FIELD_LENGTH_IN_BITS;

pub const TOTAL_PAYLOAD_DATA_SIZE_IN_BYTES: usize = TOTAL_PAYLOAD_DATA_SIZE_IN_BITS / 8;

// Spec CHIP-Common Reserved Tags
pub const SERIAL_NUMBER_TAG: u8 = 0x00;
pub const PBKDFITERATIONS_TAG: u8 = 0x01;
pub const BPKFSALT_TAG: u8 = 0x02;
pub const NUMBER_OFDEVICES_TAG: u8 = 0x03;
pub const COMMISSIONING_TIMEOUT_TAG: u8 = 0x04;

/// Commissioning flow type as per the Matter Core spec
#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum CommFlowType {
    /// Standard commissioning flow
    Standard = 0,
    /// Enhanced commissioning flow with user intent
    UserIntent = 1,
    /// Custom commissioning flow
    Custom = 2,
}

/// Type alias for no optional data function for the QR payload
pub type NoOptionalData = fn() -> Empty<Result<u8, Error>>;

/// Function that provides no optional data for the QR payload
pub fn no_optional_data() -> Empty<Result<u8, Error>> {
    core::iter::empty()
}

/// QR Code payload type
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct QrPayload<'a, T> {
    /// Payload version. Always 0
    version: u8,
    /// Discovery capabilities of the device
    discovery_capabilities: DiscoveryCapabilities,
    /// Commissioning flow type
    comm_flow: CommFlowType,
    /// Basic commissioning data
    comm_data: BasicCommData,
    /// Vendor ID
    vid: u16,
    /// Product ID
    pid: u16,
    /// Serial number of the device
    serial_no: &'a str,
    /// Optional extra data
    /// The data must be ordered by the tag of each TLV element in ascending order.
    optional_data: T,
}

impl<'a, T, I> QrPayload<'a, T>
where
    T: Fn() -> I,
    I: Iterator<Item = Result<u8, Error>> + 'a,
{
    /// Create a new QR payload from the device basic info config
    ///
    /// # Arguments
    /// - `discovery_capabilities` - Discovery capabilities of the device
    /// - `comm_flow` - Commissioning flow type
    /// - `comm_data` - Basic commissioning data
    /// - `dev_det` - Device basic info config
    /// - `optional_data` - Function that provides an iterator over optional TLV data bytes.
    ///   NOTE: Should be ordered by tag number in ascending order.
    pub const fn new_from_basic_info(
        discovery_capabilities: DiscoveryCapabilities,
        comm_flow: CommFlowType,
        comm_data: BasicCommData,
        dev_det: &'a BasicInfoConfig,
        optional_data: T,
    ) -> Self {
        Self::new(
            discovery_capabilities,
            comm_flow,
            comm_data,
            dev_det.vid,
            dev_det.pid,
            dev_det.serial_no,
            optional_data,
        )
    }

    /// Create a new QR payload
    ///
    /// # Arguments
    /// - `discovery_capabilities` - Discovery capabilities of the device
    /// - `comm_flow` - Commissioning flow type
    /// - `comm_data` - Basic commissioning data
    /// - `vid` - Vendor ID
    /// - `pid` - Product ID
    /// - `serial_no` - Serial number of the device
    /// - `optional_data` - Function that provides an iterator over optional TLV data bytes.
    ///   NOTE: Should be ordered by tag number in ascending order.
    pub const fn new(
        discovery_capabilities: DiscoveryCapabilities,
        comm_flow: CommFlowType,
        comm_data: BasicCommData,
        vid: u16,
        pid: u16,
        serial_no: &'a str,
        optional_data: T,
    ) -> Self {
        const DEFAULT_VERSION: u8 = 0;

        Self {
            version: DEFAULT_VERSION,
            discovery_capabilities,
            comm_flow,
            comm_data,
            vid,
            pid,
            serial_no,
            optional_data,
        }
    }

    /// Check if the QR payload is valid
    ///
    /// # Returns
    /// - `true` if the payload is valid
    /// - `false` otherwise
    pub fn is_valid(&self) -> bool {
        // 3-bit value specifying the QR code payload version.
        if self.version >= 1 << VERSION_FIELD_LENGTH_IN_BITS {
            return false;
        }

        if self.discovery_capabilities.is_empty() {
            return false;
        }

        let password = u32::from_le_bytes(*self.comm_data.password.access());
        if password >= 1 << SETUP_PINCODE_FIELD_LENGTH_IN_BITS {
            return false;
        }

        self.check_payload_common_constraints()
    }

    fn check_payload_common_constraints(&self) -> bool {
        #[repr(u16)]
        enum VendorId {
            CommonOrUnspecified = 0x0000,
            TestVendor4 = 0xFFF4,
        }

        impl VendorId {
            fn is_valid_operationally(vendor_id: u16) -> bool {
                (vendor_id != Self::CommonOrUnspecified as u16)
                    && (vendor_id <= Self::TestVendor4 as u16)
            }
        }

        // A version not equal to 0 would be invalid for v1 and would indicate new format (e.g. version 2)
        if self.version != 0 {
            return false;
        }

        if !Self::is_valid_setup_pin(u32::from_le_bytes(*self.comm_data.password.access())) {
            return false;
        }

        // VendorID must be unspecified (0) or in valid range expected.
        if VendorId::is_valid_operationally(self.vid)
            && (self.vid != VendorId::CommonOrUnspecified as u16)
        {
            return false;
        }

        // A value of 0x0000 SHALL NOT be assigned to a product since Product ID = 0x0000 is used for these specific cases:
        //  * To announce an anonymized Product ID as part of device discovery
        //  * To indicate an OTA software update file applies to multiple Product IDs equally.
        //  * To avoid confusion when presenting the Onboarding Payload for ECM with multiple nodes
        if self.pid == 0 && self.vid != VendorId::CommonOrUnspecified as u16 {
            return false;
        }

        true
    }

    fn is_valid_setup_pin(setup_pin: u32) -> bool {
        const SETUP_PINCODE_MAXIMUM_VALUE: u32 = 99999998;
        const SETUP_PINCODE_UNDEFINED_VALUE: u32 = 0;

        // SHALL be restricted to the values 0x0000001 to 0x5F5E0FE (00000001 to 99999998 in decimal), excluding the invalid Passcode
        // values.
        if setup_pin == SETUP_PINCODE_UNDEFINED_VALUE
            || setup_pin > SETUP_PINCODE_MAXIMUM_VALUE
            || setup_pin == 11111111
            || setup_pin == 22222222
            || setup_pin == 33333333
            || setup_pin == 44444444
            || setup_pin == 55555555
            || setup_pin == 66666666
            || setup_pin == 77777777
            || setup_pin == 88888888
            || setup_pin == 12345678
            || setup_pin == 87654321
        {
            return false;
        }

        true
    }

    /// Encode the QR text of this payload as a string into the provided buffer
    ///
    /// # Arguments
    /// - `buf` - Buffer to store the QR code string
    ///
    /// # Returns
    /// - On success, returns a tuple containing the QR code string and the remaining buffer
    /// - On failure, returns an error
    pub fn as_str<'b>(&self, buf: &'b mut [u8]) -> Result<(&'b str, &'b mut [u8]), Error> {
        let str_len = self.emit_chars().count();

        let (str_buf, remaining_buf) = buf.split_at_mut(str_len);

        let mut wb = WriteBuf::new(str_buf);
        for ch in self.emit_chars() {
            wb.le_u8(ch? as u8)?;
        }

        // Can't fail as `emit_chars` generates a valid UTF-8 string
        let str = unwrap!(core::str::from_utf8(str_buf).map_err(|_| ErrorCode::InvalidData));

        Ok((str, remaining_buf))
    }

    /// Emit the QR text of this payload as an iterator of characters
    pub fn emit_chars(&self) -> impl Iterator<Item = Result<char, Error>> + '_ {
        struct PackedBitsIterator<I>(I);

        impl<I> Iterator for PackedBitsIterator<I>
        where
            I: Iterator<Item = Result<bool, Error>>,
        {
            type Item = Result<(u32, u8), Error>;

            fn next(&mut self) -> Option<Self::Item> {
                let mut chunk = 0;
                let mut packed_bits = 0;

                for index in 0..24 {
                    // Up to 24 bits as we are enclding with Base38, which means up to 3 bytes at once
                    if let Some(bit) = self.0.next() {
                        let bit = match bit {
                            Ok(bit) => bit,
                            Err(err) => return Some(Err(err)),
                        };

                        chunk |= (bit as u32) << index;
                        packed_bits += 1;
                    } else {
                        break;
                    }
                }

                if packed_bits > 0 {
                    assert!(packed_bits % 8 == 0);

                    Some(Ok((chunk, packed_bits)))
                } else {
                    None
                }
            }
        }

        "MT:"
            .chars()
            .map(Result::Ok)
            .chain(
                PackedBitsIterator(self.emit_all_bits()).flat_map(|bits| match bits {
                    Ok((bits, bits_count)) => {
                        EitherIter::First(base38::encode_bits(bits, bits_count).map(Result::Ok))
                    }
                    Err(err) => EitherIter::Second(core::iter::once(Err(err))),
                }),
            )
    }

    fn emit_all_bits(&self) -> impl Iterator<Item = Result<bool, Error>> + '_ {
        Self::emit_bits(self.version as _, VERSION_FIELD_LENGTH_IN_BITS)
            .chain(Self::emit_bits(
                self.vid as _,
                VENDOR_IDFIELD_LENGTH_IN_BITS,
            ))
            .chain(Self::emit_bits(
                self.pid as _,
                PRODUCT_IDFIELD_LENGTH_IN_BITS,
            ))
            .chain(Self::emit_bits(
                self.comm_flow as _,
                COMMISSIONING_FLOW_FIELD_LENGTH_IN_BITS,
            ))
            .chain(Self::emit_bits(
                self.discovery_capabilities.bits() as _,
                RENDEZVOUS_INFO_FIELD_LENGTH_IN_BITS,
            ))
            .chain(Self::emit_bits(
                self.comm_data.discriminator as _,
                PAYLOAD_DISCRIMINATOR_FIELD_LENGTH_IN_BITS,
            ))
            .chain(Self::emit_bits(
                u32::from_le_bytes(*self.comm_data.password.access()),
                SETUP_PINCODE_FIELD_LENGTH_IN_BITS,
            ))
            .chain(Self::emit_bits(0, PADDING_FIELD_LENGTH_IN_BITS))
            .chain(
                self.emit_optional_tlv_data()
                    .flat_map(|bits| Self::emit_maybe_bits(bits.map(|bits| (bits as _, 8)))),
            )
    }

    fn emit_bits(input: u32, len: usize) -> impl Iterator<Item = Result<bool, Error>> {
        (0..len).map(move |i| Ok((input >> i) & 1 == 1))
    }

    fn emit_maybe_bits(
        bits: Result<(u32, usize), Error>,
    ) -> impl Iterator<Item = Result<bool, Error>> {
        match bits {
            Ok((input, len)) => EitherIter::First(Self::emit_bits(input, len)),
            Err(err) => EitherIter::Second(core::iter::once(Err(err))),
        }
    }

    fn emit_optional_tlv_data(&self) -> impl Iterator<Item = Result<u8, Error>> + '_ {
        if self.serial_no.is_empty() && (self.optional_data)().next().is_none() {
            return EitherIter::First(core::iter::empty());
        }

        let serial_no = if self.serial_no.is_empty() {
            EitherIter::First(core::iter::empty())
        } else {
            EitherIter::Second(
                TLV::utf8(TLVTag::Context(SERIAL_NUMBER_TAG), self.serial_no).into_tlv_iter(),
            )
        };

        EitherIter::Second(
            TLV::structure(TLVTag::Anonymous)
                .into_tlv_iter()
                .chain(serial_no)
                .flat_map(TLV::result_into_bytes_iter)
                .chain((self.optional_data)())
                .chain(
                    TLV::end_container()
                        .into_tlv_iter()
                        .flat_map(TLV::result_into_bytes_iter),
                ),
        )
    }
}

impl<'a> QrPayload<'a, &'a [u8]> {
    /// Parse a Matter onboarding QR-code text (an `MT:` payload) into a [`QrPayload`].
    ///
    /// This is the inverse of the [`QrPayload::as_str`] / [`QrPayload::emit_chars`]
    /// encoding path, and the entry point a commissioner uses to turn a scanned QR
    /// string into the discriminator, passcode, VID/PID etc. it needs to commission
    /// the device.
    ///
    /// `buf` is a scratch buffer that the parsed payload borrows from: the trailing
    /// optional-TLV data (and hence any serial number decoded out of it) points into
    /// it, so `buf` must outlive the returned payload. A buffer of
    /// [`TOTAL_PAYLOAD_DATA_SIZE_IN_BYTES`] plus the optional-data length is enough;
    /// the base38 body never decodes to more bytes than the input string.
    ///
    /// The returned payload's `optional_data` is the raw optional-TLV byte slice (an
    /// anonymous TLV structure), which is empty when the QR carries no optional data.
    ///
    /// # Errors
    /// Returns [`ErrorCode::InvalidData`] if the string is not a well-formed `MT:`
    /// payload (missing prefix, invalid base38, too short, or an out-of-range field).
    pub fn parse(qr: &str, buf: &'a mut [u8]) -> Result<Self, Error> {
        // Strip the `MT:` prefix.
        let body = qr.strip_prefix(QR_PREFIX).ok_or(ErrorCode::InvalidData)?;

        // Base38-decode the body into `buf`.
        let mut len = 0;
        for byte in base38::decode(body) {
            let byte = byte?;
            *buf.get_mut(len).ok_or(ErrorCode::BufferTooSmall)? = byte;
            len += 1;
        }
        let decoded = &buf[..len];

        // The fixed part of the payload is `TOTAL_PAYLOAD_DATA_SIZE_IN_BITS` bits
        // (`TOTAL_PAYLOAD_DATA_SIZE_IN_BYTES` bytes); anything beyond it is the
        // optional-TLV data.
        if decoded.len() < TOTAL_PAYLOAD_DATA_SIZE_IN_BYTES {
            return Err(ErrorCode::InvalidData.into());
        }

        let mut reader = BitReader::new(decoded);

        // Read the fixed fields in the same order (and LSB-first bit order) as
        // `emit_all_bits` writes them.
        let version = reader.read(VERSION_FIELD_LENGTH_IN_BITS)? as u8;
        let vid = reader.read(VENDOR_IDFIELD_LENGTH_IN_BITS)? as u16;
        let pid = reader.read(PRODUCT_IDFIELD_LENGTH_IN_BITS)? as u16;
        let comm_flow =
            CommFlowType::from_bits(reader.read(COMMISSIONING_FLOW_FIELD_LENGTH_IN_BITS)? as u8)?;
        let discovery_capabilities = DiscoveryCapabilities::from_bits_truncate(
            reader.read(RENDEZVOUS_INFO_FIELD_LENGTH_IN_BITS)? as u8,
        );
        let discriminator = reader.read(PAYLOAD_DISCRIMINATOR_FIELD_LENGTH_IN_BITS)? as u16;
        let passcode = reader.read(SETUP_PINCODE_FIELD_LENGTH_IN_BITS)?;
        // Padding bits (must be present but are ignored).
        let _ = reader.read(PADDING_FIELD_LENGTH_IN_BITS)?;

        // The remaining whole bytes are the optional-TLV data.
        let optional_data = &decoded[TOTAL_PAYLOAD_DATA_SIZE_IN_BYTES..];

        // If present, the serial number is a context-`SERIAL_NUMBER_TAG` UTF-8 string
        // in the anonymous optional-TLV structure. Borrow it out of the blob.
        let serial_no = Self::parse_serial_no(optional_data).unwrap_or("");

        let payload = Self {
            version,
            discovery_capabilities,
            comm_flow,
            comm_data: BasicCommData {
                password: passcode.to_le_bytes().into(),
                discriminator,
            },
            vid,
            pid,
            serial_no,
            optional_data,
        };

        Ok(payload)
    }

    /// Extract the serial number (context tag [`SERIAL_NUMBER_TAG`]) from the
    /// optional-TLV structure, if present.
    fn parse_serial_no(optional_data: &'a [u8]) -> Option<&'a str> {
        if optional_data.is_empty() {
            return None;
        }

        let root = TLVElement::new(optional_data);
        let serial = root.structure().ok()?.find_ctx(SERIAL_NUMBER_TAG).ok()?;

        serial.utf8().ok()
    }

    /// The payload version (always 0 for v1 QR codes).
    pub const fn version(&self) -> u8 {
        self.version
    }

    /// The device's advertised discovery capabilities.
    pub const fn discovery_capabilities(&self) -> DiscoveryCapabilities {
        self.discovery_capabilities
    }

    /// The commissioning flow type.
    pub const fn comm_flow(&self) -> CommFlowType {
        self.comm_flow
    }

    /// The 12-bit discriminator.
    pub const fn discriminator(&self) -> u16 {
        self.comm_data.discriminator
    }

    /// The setup passcode (PIN).
    pub fn passcode(&self) -> u32 {
        u32::from_le_bytes(*self.comm_data.password.access())
    }

    /// The Vendor ID.
    pub const fn vid(&self) -> u16 {
        self.vid
    }

    /// The Product ID.
    pub const fn pid(&self) -> u16 {
        self.pid
    }

    /// The serial number, or an empty string if the QR carries none.
    pub const fn serial_no(&self) -> &'a str {
        self.serial_no
    }

    /// The raw optional-TLV data (an anonymous TLV structure), empty if absent.
    pub const fn optional_data(&self) -> &'a [u8] {
        self.optional_data
    }

    /// A [`CommissionableFilter`] that discovers the device this QR code describes.
    ///
    /// A QR code carries the **full 12-bit** discriminator, so this filters on
    /// `discriminator` and can narrow discovery to a single device. (Contrast
    /// [`QrPayload::<()>::commissionable_filter`], built from a manual pairing code,
    /// which can only filter on the short discriminator.)
    ///
    /// Only the discriminator is set. The vendor and product IDs are deliberately
    /// *not* included even though the QR carries them: per the Matter Core spec a
    /// device may advertise an **anonymized** Product ID of 0 during discovery, so
    /// filtering on the QR's PID would fail to find such a device. Add them to the
    /// returned filter if the extra selectivity is wanted and the device is known
    /// not to anonymize.
    pub fn commissionable_filter(&self) -> CommissionableFilter {
        CommissionableFilter {
            discriminator: Some(self.discriminator()),
            ..Default::default()
        }
    }
}

impl<'a> QrPayload<'a, ()> {
    /// Parse a Matter **manual pairing code** (the 11- or 21-digit decimal string
    /// printed on a device, e.g. `34970112332` / `3497-0112-332`) into a
    /// [`QrPayload`].
    ///
    /// This is the "typed by a human" onboarding format, and it is the counterpart
    /// of [`BasicCommData::compute_pairing_code`]. It is deliberately a *different*
    /// `T` from [`QrPayload::parse`] (`()` rather than `&[u8]`), because a manual
    /// pairing code carries strictly less information than a QR code, and the
    /// resulting payload must not be mistaken for one:
    ///
    /// - Only the **upper 4 bits** of the discriminator are carried (the "short
    ///   discriminator"). Per the Matter Core spec: *"For machine-readable formats,
    ///   the full 12-bit Discriminator is used. For the Manual Pairing Code, only
    ///   the upper 4 bits out of the 12-bit Discriminator are used."* Read it via
    ///   [`Self::short_discriminator`] - there is deliberately no `discriminator()`
    ///   accessor on this `T`, so a 4-bit value can never be mistaken for a 12-bit
    ///   one.
    /// - **No discovery capabilities** are carried, so a commissioner cannot know
    ///   whether to look for the device over BLE, SoftAP or IP, and must try all of
    ///   them.
    /// - **No serial number and no optional TLV data** are carried (hence `T = ()`).
    /// - The commissioning flow is only *implied* - see [`Self::comm_flow`].
    ///
    /// The Verhoeff check digit is verified. Separators (`-` and spaces) are
    /// ignored, so both the compact and the "pretty" printed forms are accepted.
    ///
    /// # Errors
    /// Returns [`ErrorCode::InvalidData`] if the code is not a well-formed v1 manual
    /// pairing code: wrong length, a non-digit, a bad check digit, a first digit of
    /// 8 or 9 (which per spec indicates a future format version), a VID/PID-present
    /// flag inconsistent with the length, or an out-of-range digit group.
    pub fn parse_pairing_code(code: &str) -> Result<Self, Error> {
        /// Length of the manual pairing code without vendor/product IDs.
        const SHORT_CODE_LEN: usize = 11;
        /// Length of the manual pairing code with vendor/product IDs.
        const LONG_CODE_LEN: usize = 21;

        // Strip the separators of the "pretty" form (e.g. `3497-0112-332`).
        let mut digits: heapless::String<LONG_CODE_LEN> = heapless::String::new();
        for ch in code.chars() {
            if matches!(ch, '-' | ' ') {
                continue;
            }

            if !ch.is_ascii_digit() {
                return Err(ErrorCode::InvalidData.into());
            }

            digits
                .push(ch)
                .map_err(|_| Error::from(ErrorCode::InvalidData))?;
        }

        let long_form = match digits.len() {
            SHORT_CODE_LEN => false,
            LONG_CODE_LEN => true,
            _ => return Err(ErrorCode::InvalidData.into()),
        };

        // The trailing check digit protects against typos and transpositions.
        if !digits.validate_verhoeff_check_digit() {
            return Err(ErrorCode::InvalidData.into());
        }

        // DIGIT[1] := (VID_PID_PRESENT << 2) | (DISCRIMINATOR >> 10)
        //
        // A leading digit of 8 or 9 is invalid for v1 and would indicate a future
        // payload version.
        let digit1 = Self::digits_at(&digits, 0, 1)?;
        if digit1 > 7 {
            return Err(ErrorCode::InvalidData.into());
        }

        let vid_pid_present = digit1 >> 2 == 1;
        // The flag and the code length must agree.
        if vid_pid_present != long_form {
            return Err(ErrorCode::InvalidData.into());
        }

        // The two most significant bits (11..10) of the discriminator.
        let disc_bits_11_10 = (digit1 & 0x3) as u16;

        // DIGIT[2..6] := ((DISCRIMINATOR & 0x300) << 6) | (PASSCODE & 0x3FFF)
        let group = Self::digits_at(&digits, 1, 5)?;
        if group > 0xFFFF {
            return Err(ErrorCode::InvalidData.into());
        }

        // Bits 9..8 of the discriminator, and the low 14 bits of the passcode.
        let disc_bits_9_8 = ((group >> 14) & 0x3) as u16;
        let passcode_low = group & 0x3FFF;

        // DIGIT[7..10] := (PASSCODE >> 14)
        let passcode_high = Self::digits_at(&digits, 6, 4)?;
        if passcode_high > 0x1FFF {
            return Err(ErrorCode::InvalidData.into());
        }

        let passcode = (passcode_high << 14) | passcode_low;

        // The short discriminator is exactly the upper 4 bits (11..8) of the full
        // 12-bit discriminator.
        let short_discriminator = (disc_bits_11_10 << 2) | disc_bits_9_8;

        // DIGIT[11..15] := VENDOR_ID, DIGIT[16..20] := PRODUCT_ID (long form only)
        let (vid, pid) = if long_form {
            let vid = Self::digits_at(&digits, 10, 5)?;
            let pid = Self::digits_at(&digits, 15, 5)?;

            if vid > 0xFFFF || pid > 0xFFFF {
                return Err(ErrorCode::InvalidData.into());
            }

            (vid as u16, pid as u16)
        } else {
            (0, 0)
        };

        Ok(Self {
            // Not carried; a valid v1 code is version 0 by construction (a leading
            // digit of 8 or 9 - i.e. a future version - is rejected above).
            version: 0,
            // Not carried at all. Empty means "unknown - try every transport",
            // rather than "the device supports none".
            discovery_capabilities: DiscoveryCapabilities::empty(),
            // Not carried directly; per spec the *variant* implies it, and doubles as
            // our short/long-form discriminant. See `Self::comm_flow`.
            comm_flow: if long_form {
                CommFlowType::Custom
            } else {
                CommFlowType::Standard
            },
            comm_data: BasicCommData {
                password: passcode.to_le_bytes().into(),
                // NOTE: this 12-bit field holds the 4-bit *short* discriminator for a
                // manual pairing code. Only `short_discriminator()` exposes it.
                discriminator: short_discriminator,
            },
            vid,
            pid,
            // Not carried.
            serial_no: "",
            // A manual pairing code has no optional data - hence `T = ()`.
            optional_data: (),
        })
    }

    /// Parse `len` decimal digits starting at `offset`.
    fn digits_at(digits: &str, offset: usize, len: usize) -> Result<u32, Error> {
        digits
            .get(offset..offset + len)
            .ok_or(ErrorCode::InvalidData)?
            .parse()
            .map_err(|_| ErrorCode::InvalidData.into())
    }

    /// The **short** (4-bit) discriminator - the upper 4 bits of the device's full
    /// 12-bit discriminator.
    ///
    /// This is all a manual pairing code carries, so it can only narrow discovery to
    /// 1-in-16 devices. Feed it to
    /// [`CommissionableFilter::short_discriminator`](crate::transport::network::mdns::CommissionableFilter),
    /// *not* to the full-discriminator filter.
    pub const fn short_discriminator(&self) -> u8 {
        self.comm_data.discriminator as u8
    }

    /// The setup passcode (PIN). Carried in full (27 bits).
    pub fn passcode(&self) -> u32 {
        u32::from_le_bytes(*self.comm_data.password.access())
    }

    /// The Vendor and Product IDs, if this is the 21-digit variant that carries them
    /// (`None` for the 11-digit variant).
    pub fn vid_pid(&self) -> Option<(u16, u16)> {
        (!matches!(self.comm_flow, CommFlowType::Standard)).then_some((self.vid, self.pid))
    }

    /// The commissioning flow, as far as the code determines it.
    ///
    /// A manual pairing code has no dedicated commissioning-flow field; per the
    /// Matter Core spec the *variant* implies it:
    /// - The 11-digit variant (no VID/PID) means the commissioner *"SHALL assume it
    ///   is a standard flow device"* - so this returns `Some(CommFlowType::Standard)`.
    /// - The 21-digit variant (with VID/PID) is used for **both** the User-intent and
    ///   the Custom flow, and the code alone cannot distinguish them - so this
    ///   returns `None`. To resolve it, look the VID/PID up in the Distributed
    ///   Compliance Ledger (see [`Self::vid_pid`]).
    pub fn comm_flow(&self) -> Option<CommFlowType> {
        matches!(self.comm_flow, CommFlowType::Standard).then_some(CommFlowType::Standard)
    }

    /// A [`CommissionableFilter`] that discovers the device this manual pairing code
    /// describes.
    ///
    /// A manual pairing code only carries the **short** (4-bit) discriminator, so
    /// this necessarily filters on `short_discriminator` - which narrows discovery
    /// to 1-in-16 devices rather than to exactly one. Getting this right is the
    /// whole point of the method: the short value must not end up in the filter's
    /// full-discriminator field, where it would match nothing.
    ///
    /// The vendor and product IDs are not included even when the 21-digit variant
    /// carries them, since a device may advertise an anonymized Product ID of 0
    /// during discovery.
    pub fn commissionable_filter(&self) -> CommissionableFilter {
        CommissionableFilter {
            short_discriminator: Some(self.short_discriminator()),
            ..Default::default()
        }
    }
}

impl CommFlowType {
    /// Decode a 2-bit commissioning-flow field.
    fn from_bits(bits: u8) -> Result<Self, Error> {
        match bits {
            0 => Ok(Self::Standard),
            1 => Ok(Self::UserIntent),
            2 => Ok(Self::Custom),
            _ => Err(ErrorCode::InvalidData.into()),
        }
    }
}

/// A little-endian, LSB-first bit reader over a byte slice - the inverse of the
/// `emit_bits` bit order used by the QR encoder (`(input >> i) & 1`).
struct BitReader<'a> {
    data: &'a [u8],
    /// Absolute bit position of the next bit to read.
    pos: usize,
}

impl<'a> BitReader<'a> {
    const fn new(data: &'a [u8]) -> Self {
        Self { data, pos: 0 }
    }

    /// Read `len` bits (0..=32) as a little-endian value, LSB-first.
    fn read(&mut self, len: usize) -> Result<u32, Error> {
        debug_assert!(len <= 32);

        if self.pos + len > self.data.len() * 8 {
            return Err(ErrorCode::InvalidData.into());
        }

        let mut value = 0u32;
        for i in 0..len {
            let bit_pos = self.pos + i;
            let byte = self.data[bit_pos / 8];
            let bit = (byte >> (bit_pos % 8)) & 1;
            value |= (bit as u32) << i;
        }

        self.pos += len;

        Ok(value)
    }
}

/// QR Code text type
///
/// Used when emitting the QR code in different text formats
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum QrTextType {
    /// Pure ASCII text
    /// Compatible with all consoles
    Ascii,
    /// ANSI
    Ansi,
    /// Unicode
    Unicode,
}

/// QR Code representation
pub struct Qr<'a>(QrCode<'a>);

impl<'a> Qr<'a> {
    /// Create a new QR code from the given text
    ///
    /// # Arguments
    /// - `text` - Text to encode in the QR code
    /// - `tmp_buf` - Temporary buffer for QR code generation
    /// - `out_buf` - Output buffer for the QR code
    ///
    /// # Returns
    /// - On success, returns the generated QR code
    /// - On failure, returns an error
    pub fn compute(text: &str, tmp_buf: &mut [u8], out_buf: &'a mut [u8]) -> Result<Self, Error> {
        let needed_version = Version::new(Self::version(text));

        let qr = QrCode::encode_text(
            text,
            tmp_buf,
            out_buf,
            QrCodeEcc::Medium,
            needed_version,
            needed_version,
            None,
            false,
        )
        .map_err(|_| ErrorCode::BufferTooSmall)?;

        Ok(Self(qr))
    }

    /// Get the size of the QR code
    pub fn size(&self) -> u32 {
        self.0.size() as _
    }

    /// Get the module value at the given coordinates
    pub fn get_module(&self, x: i32, y: i32) -> bool {
        self.0.get_module(x, y)
    }

    /// Encode the QR as a string into the provided buffer
    ///
    /// # Arguments
    /// - `text_type` - Type of text to return (ASCII, ANSI, Unicode)
    /// - `border` - Border size
    /// - `invert` - Whether to invert the colors (black on a white background)
    /// - `out_buf` - Output buffer for the rendered string
    ///
    /// # Returns
    /// - On success, returns a tuple containing the rendered string and the remaining buffer
    /// - On failure, returns an error
    pub fn as_str<'b>(
        &self,
        text_type: QrTextType,
        border: u8,
        invert: bool,
        out_buf: &'b mut [u8],
    ) -> Result<(&'b str, &'b mut [u8]), Error> {
        let mut offset = 0;

        for c in self.emit_chars(text_type, border, invert) {
            let mut dst = [0; 4];
            let bytes = c.encode_utf8(&mut dst).as_bytes();

            if offset + bytes.len() > out_buf.len() {
                return Err(ErrorCode::BufferTooSmall.into());
            } else {
                out_buf[offset..offset + bytes.len()].copy_from_slice(bytes);
                offset += bytes.len();
            }
        }

        let (str_buf, remaining_buf) = out_buf.split_at_mut(offset);

        // Can't fail as `emit_chars` generates a valid UTF-8 string
        let str = unwrap!(core::str::from_utf8(str_buf).map_err(|_| ErrorCode::InvalidData));

        Ok((str, remaining_buf))
    }

    /// Encode a single line of the QR as a string into the provided buffer
    ///
    /// # Arguments
    /// - `text_type` - Type of text to return (ASCII, ANSI, Unicode)
    /// - `border` - Border size
    /// - `invert` - Whether to invert the colors (black on a white background)
    /// - `nl` - Whether to add a newline at the end of the line
    /// - `y` - Y coordinate of the line to render
    /// - `out_buf` - Output buffer for the rendered string
    ///
    /// # Returns
    /// - On success, returns a tuple containing the rendered string and the remaining buffer
    /// - On failure, returns an error
    pub fn line_as_str<'b>(
        &self,
        text_type: QrTextType,
        border: u8,
        invert: bool,
        nl: bool,
        y: i32,
        out_buf: &'b mut [u8],
    ) -> Result<(&'b str, &'b mut [u8]), Error> {
        let mut offset = 0;

        for c in self.emit_line_chars(text_type, border, invert, nl, y) {
            let mut dst = [0; 4];
            let bytes = c.encode_utf8(&mut dst).as_bytes();

            if offset + bytes.len() > out_buf.len() {
                return Err(ErrorCode::BufferTooSmall.into());
            } else {
                out_buf[offset..offset + bytes.len()].copy_from_slice(bytes);
                offset += bytes.len();
            }
        }

        let (str_buf, remaining_buf) = out_buf.split_at_mut(offset);

        // Can't fail as `emit_chars` generates a valid UTF-8 string
        let str = unwrap!(core::str::from_utf8(str_buf).map_err(|_| ErrorCode::InvalidData));

        Ok((str, remaining_buf))
    }

    /// Get an iterator over the indexes of the lines of the QR code including borders
    ///
    /// # Arguments
    /// - `text_type` - Type of text to return (ASCII, ANSI, Unicode)
    /// - `border` - Border size
    pub fn lines_range(
        &self,
        text_type: QrTextType,
        border: u8,
    ) -> impl Iterator<Item = i32> + '_ + 'a {
        let iborder: i32 = border as _;

        (-iborder..self.size() as i32 + iborder)
            .filter(move |y| !matches!(text_type, QrTextType::Unicode) || (*y - -iborder) % 2 == 0)
    }

    /// Get an iterator over the characters of the rendered QR code
    ///
    /// # Arguments
    /// - `text_type` - Type of text to return (ASCII, ANSI, Unicode)
    /// - `border` - Border size
    /// - `invert` - Whether to invert the colors (black on a white background)
    ///
    /// # Returns
    /// - An iterator over the characters of the rendered QR code
    pub fn emit_chars(
        &self,
        text_type: QrTextType,
        border: u8,
        invert: bool,
    ) -> impl Iterator<Item = char> + use<'_, 'a> {
        self.lines_range(text_type, border)
            .flat_map(move |y| self.emit_line_chars(text_type, border, invert, true, y))
    }

    /// Get an iterator over the characters of a single line of the rendered QR code
    ///
    /// # Arguments
    /// - `text_type` - Type of text to return (ASCII, ANSI, Unicode)
    /// - `border` - Border size
    /// - `invert` - Whether to invert the colors (black on a white background)
    /// - `nl` - Whether to add a newline at the end of the line
    /// - `y` - Y coordinate of the line to render
    ///
    /// # Returns
    /// - An iterator over the characters of the rendered line
    pub fn emit_line_chars(
        &self,
        text_type: QrTextType,
        border: u8,
        invert: bool,
        nl: bool,
        y: i32,
    ) -> impl Iterator<Item = char> + use<'_, 'a> {
        let border: i32 = border as _;

        (-border..self.size() as i32 + border + 1)
            .map(move |x| (x, y))
            .map(move |(x, y)| {
                if x < self.size() as i32 + border {
                    let white = !self.get_module(x, y) ^ invert;

                    match text_type {
                        QrTextType::Ascii => {
                            if white {
                                "#"
                            } else {
                                " "
                            }
                        }
                        QrTextType::Ansi => {
                            let prev_white = if x > -border {
                                Some(self.get_module(x - 1, y))
                            } else {
                                None
                            }
                            .map(|prev_white| !prev_white ^ invert);

                            if prev_white != Some(white) {
                                if white {
                                    "\x1b[47m "
                                } else {
                                    "\x1b[40m "
                                }
                            } else {
                                " "
                            }
                        }
                        QrTextType::Unicode => {
                            if white == !self.get_module(x, y + 1) ^ invert {
                                if white {
                                    "\u{2588}"
                                } else {
                                    " "
                                }
                            } else if white {
                                "\u{2580}"
                            } else {
                                "\u{2584}"
                            }
                        }
                    }
                } else {
                    match text_type {
                        QrTextType::Ascii => {
                            if nl {
                                "\n"
                            } else {
                                ""
                            }
                        }
                        _ => {
                            if nl {
                                "\x1b[0m\n"
                            } else {
                                "\x1b[0m"
                            }
                        }
                    }
                }
            })
            .flat_map(str::chars)
    }

    fn version(qr_code_text: &str) -> u8 {
        match qr_code_text.len() {
            0..=38 => 2,
            39..=61 => 3,
            62..=90 => 4,
            _ => 5,
        }
    }
}

/// QR Code text renderer
pub enum QrTextRenderer<'a> {
    /// ASCII renderer
    Ascii(Qr<'a>),
    /// ANSI renderer
    Ansi(Qr<'a>),
    /// Unicode renderer
    Unicode(Qr<'a>),
}

impl<'a> QrTextRenderer<'a> {
    /// Render the complete QR code as a string into the provided buffer
    ///
    /// # Arguments
    /// - `border` - Border size
    /// - `invert` - Whether to invert the colors (black on a white background)
    /// - `out_buf` - Output buffer for the rendered string
    ///
    /// # Returns
    /// - On success, returns a tuple containing the rendered string and the remaining buffer
    /// - On failure, returns an error
    pub fn render<'b>(
        &self,
        border: u8,
        invert: bool,
        out_buf: &'b mut [u8],
    ) -> Result<(&'b str, &'b mut [u8]), Error> {
        let mut offset = 0;

        for c in self.render_iter(border, invert) {
            let mut dst = [0; 4];
            let bytes = c.encode_utf8(&mut dst).as_bytes();

            if offset + bytes.len() > out_buf.len() {
                return Err(ErrorCode::BufferTooSmall.into());
            } else {
                out_buf[offset..offset + bytes.len()].copy_from_slice(bytes);
                offset += bytes.len();
            }
        }

        let (str_buf, remaining_buf) = out_buf.split_at_mut(offset);

        // Can't fail as `emit_chars` generates a valid UTF-8 string
        let str = unwrap!(core::str::from_utf8(str_buf).map_err(|_| ErrorCode::InvalidData));

        Ok((str, remaining_buf))
    }

    /// Render a single line of the QR code as a string into the provided buffer
    ///
    /// # Arguments
    /// - `border` - Border size
    /// - `invert` - Whether to invert the colors (black on a white background)
    /// - `nl` - Whether to add a newline at the end of the line
    /// - `y` - Y coordinate of the line to render
    /// - `out_buf` - Output buffer for the rendered string
    ///
    /// # Returns
    /// - On success, returns a tuple containing the rendered string and the remaining buffer
    /// - On failure, returns an error
    pub fn render_line<'b>(
        &self,
        border: u8,
        invert: bool,
        nl: bool,
        y: i32,
        out_buf: &'b mut [u8],
    ) -> Result<(&'b str, &'b mut [u8]), Error> {
        let mut offset = 0;

        for c in self.render_line_iter(border, invert, nl, y) {
            let mut dst = [0; 4];
            let bytes = c.encode_utf8(&mut dst).as_bytes();

            if offset + bytes.len() > out_buf.len() {
                return Err(ErrorCode::BufferTooSmall.into());
            } else {
                out_buf[offset..offset + bytes.len()].copy_from_slice(bytes);
                offset += bytes.len();
            }
        }

        let (str_buf, remaining_buf) = out_buf.split_at_mut(offset);

        // Can't fail as `emit_chars` generates a valid UTF-8 string
        let str = unwrap!(core::str::from_utf8(str_buf).map_err(|_| ErrorCode::InvalidData));

        Ok((str, remaining_buf))
    }

    /// Get an iterator over the indexes of the lines of the QR code including borders
    ///
    /// # Arguments
    /// - `border` - Border size
    pub fn lines_range(&self, border: u8) -> impl Iterator<Item = i32> + '_ + 'a {
        let unicode = matches!(self, Self::Unicode(_));
        let iborder: i32 = border as _;

        (-iborder..self.qr().size() as i32 + iborder)
            .filter(move |y| !unicode || (*y - -iborder) % 2 == 0)
    }

    /// Get an iterator over the characters of the rendered QR code
    ///
    /// # Arguments
    /// - `border` - Border size
    /// - `invert` - Whether to invert the colors (black on a white background)
    ///
    /// # Returns
    /// - An iterator over the characters of the rendered QR code
    pub fn render_iter(
        &self,
        border: u8,
        invert: bool,
    ) -> impl Iterator<Item = char> + use<'_, 'a> {
        self.lines_range(border)
            .flat_map(move |y| self.render_line_iter(border, invert, true, y))
    }

    /// Get an iterator over the characters of a single line of the rendered QR code
    ///
    /// # Arguments
    /// - `border` - Border size
    /// - `invert` - Whether to invert the colors (black on a white background)
    /// - `nl` - Whether to add a newline at the end of the line
    /// - `y` - Y coordinate of the line to render
    ///
    /// # Returns
    /// - An iterator over the characters of the rendered line
    pub fn render_line_iter(
        &self,
        border: u8,
        invert: bool,
        nl: bool,
        y: i32,
    ) -> impl Iterator<Item = char> + use<'_, 'a> {
        let border: i32 = border as _;

        (-border..self.qr().size() as i32 + border + 1)
            .map(move |x| (x, y))
            .map(move |(x, y)| {
                if x < self.qr().size() as i32 + border {
                    let white = !self.qr().get_module(x, y) ^ invert;

                    match self {
                        Self::Ascii(_) => {
                            if white {
                                "#"
                            } else {
                                " "
                            }
                        }
                        Self::Ansi(_) => {
                            let prev_white = if x > -border {
                                Some(self.qr().get_module(x - 1, y))
                            } else {
                                None
                            }
                            .map(|prev_white| !prev_white ^ invert);

                            if prev_white != Some(white) {
                                if white {
                                    "\x1b[47m "
                                } else {
                                    "\x1b[40m "
                                }
                            } else {
                                " "
                            }
                        }
                        Self::Unicode(_) => {
                            if white == !self.qr().get_module(x, y + 1) ^ invert {
                                if white {
                                    "\u{2588}"
                                } else {
                                    " "
                                }
                            } else if white {
                                "\u{2580}"
                            } else {
                                "\u{2584}"
                            }
                        }
                    }
                } else {
                    match self {
                        Self::Ascii(_) => {
                            if nl {
                                "\n"
                            } else {
                                ""
                            }
                        }
                        _ => {
                            if nl {
                                "\x1b[0m\n"
                            } else {
                                "\x1b[0m"
                            }
                        }
                    }
                }
            })
            .flat_map(str::chars)
    }

    #[inline(always)]
    pub fn qr(&self) -> &Qr<'a> {
        match self {
            Self::Ascii(qr) => qr,
            Self::Ansi(qr) => qr,
            Self::Unicode(qr) => qr,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn can_base38_encode() {
        const QR_CODE: &str = "MT:YNJV7VSC00CMVH7SR00";

        let comm_data = BasicCommData {
            password: 34567890_u32.to_le_bytes().into(),
            discriminator: 2976,
        };
        let dev_det = BasicInfoConfig {
            vid: 9050,
            pid: 65279,
            ..Default::default()
        };

        let disc_cap = DiscoveryCapabilities::BLE;
        let qr_code_data = QrPayload::new_from_basic_info(
            disc_cap,
            CommFlowType::Standard,
            comm_data,
            &dev_det,
            no_optional_data,
        );
        let mut buf = [0; 1024];
        let data_str = unwrap!(qr_code_data.as_str(&mut buf), "Failed to encode").0;
        assert_eq!(data_str, QR_CODE)
    }

    #[test]
    fn can_base38_encode_with_vendor_data() {
        const QR_CODE: &str = "MT:-24J0AFN00KA064IJ3P0IXZB0DK5N1K8SQ1RYCU1-A40";

        let comm_data = BasicCommData {
            password: 20202021_u32.to_le_bytes().into(),
            discriminator: 3840,
        };
        let dev_det = BasicInfoConfig {
            vid: 65521,
            pid: 32769,
            serial_no: "1234567890",
            ..Default::default()
        };

        let disc_cap = DiscoveryCapabilities::IP;
        let qr_code_data = QrPayload::new_from_basic_info(
            disc_cap,
            CommFlowType::Standard,
            comm_data,
            &dev_det,
            no_optional_data,
        );
        let mut buf = [0; 1024];
        let data_str = unwrap!(qr_code_data.as_str(&mut buf), "Failed to encode").0;
        assert_eq!(data_str, QR_CODE)
    }

    #[test]
    fn can_base38_encode_with_optional_data() {
        const QR_CODE: &str =
            "MT:-24J0AFN00KA064IJ3P0IXZB0DK5N1K8SQ1RYCU1UXH34YY0V3KY.O3DKN440F710Q940";
        const OPTIONAL_DEFAULT_STRING_TAG: u8 = 0x82; // Vendor "test" tag
        const OPTIONAL_DEFAULT_STRING_VALUE: &str = "myData";

        const OPTIONAL_DEFAULT_INT_TAG: u8 = 0x83; // Vendor "test" tag
        const OPTIONAL_DEFAULT_INT_VALUE: i32 = 65550;

        let comm_data = BasicCommData {
            password: 20202021_u32.to_le_bytes().into(),
            discriminator: 3840,
        };
        let dev_det = BasicInfoConfig {
            vid: 65521,
            pid: 32769,
            serial_no: "1234567890",
            ..Default::default()
        };

        let disc_cap = DiscoveryCapabilities::IP;
        let optional_data = || {
            TLV::utf8(
                TLVTag::Context(OPTIONAL_DEFAULT_STRING_TAG),
                OPTIONAL_DEFAULT_STRING_VALUE,
            )
            .into_tlv_iter()
            .chain(
                TLV::i32(
                    TLVTag::Context(OPTIONAL_DEFAULT_INT_TAG),
                    OPTIONAL_DEFAULT_INT_VALUE,
                )
                .into_tlv_iter(),
            )
            .flat_map(TLV::result_into_bytes_iter)
        };

        let qr_code_data = QrPayload::new_from_basic_info(
            disc_cap,
            CommFlowType::Standard,
            comm_data,
            &dev_det,
            optional_data,
        );

        let mut buf = [0; 1024];
        let data_str = unwrap!(qr_code_data.as_str(&mut buf), "Failed to encode").0;
        assert_eq!(data_str, QR_CODE)
    }

    #[test]
    fn parse_qr_basic() {
        // Same vector as `can_base38_encode` (BLE, no optional data).
        const QR_CODE: &str = "MT:YNJV7VSC00CMVH7SR00";

        let mut buf = [0; 128];
        let payload = unwrap!(QrPayload::parse(QR_CODE, &mut buf), "Failed to parse");

        assert_eq!(payload.version(), 0);
        assert_eq!(payload.vid(), 9050);
        assert_eq!(payload.pid(), 65279);
        assert_eq!(payload.comm_flow(), CommFlowType::Standard);
        assert_eq!(payload.discovery_capabilities(), DiscoveryCapabilities::BLE);
        assert_eq!(payload.discriminator(), 2976);
        assert_eq!(payload.passcode(), 34567890);
        assert_eq!(payload.serial_no(), "");
        assert!(payload.optional_data().is_empty());
    }

    #[test]
    fn parse_qr_with_serial_no() {
        // Same vector as `can_base38_encode_with_vendor_data` (IP, serial number).
        const QR_CODE: &str = "MT:-24J0AFN00KA064IJ3P0IXZB0DK5N1K8SQ1RYCU1-A40";

        let mut buf = [0; 128];
        let payload = unwrap!(QrPayload::parse(QR_CODE, &mut buf), "Failed to parse");

        assert_eq!(payload.vid(), 65521);
        assert_eq!(payload.pid(), 32769);
        assert_eq!(payload.discovery_capabilities(), DiscoveryCapabilities::IP);
        assert_eq!(payload.discriminator(), 3840);
        assert_eq!(payload.passcode(), 20202021);
        assert_eq!(payload.serial_no(), "1234567890");
        assert!(!payload.optional_data().is_empty());
    }

    #[test]
    fn parse_qr_round_trips_through_encode() {
        // Parse a QR, then re-encode from the parsed *fields* and expect the same
        // string. (We rebuild via the fields rather than replaying the raw optional
        // blob, since the encoder re-wraps the serial number into the optional-TLV
        // structure itself - here the serial is the only optional content.)
        const QR_CODE: &str = "MT:-24J0AFN00KA064IJ3P0IXZB0DK5N1K8SQ1RYCU1-A40";

        let mut buf = [0; 128];
        let payload = unwrap!(QrPayload::parse(QR_CODE, &mut buf), "Failed to parse");

        let reencoded = QrPayload::new(
            payload.discovery_capabilities(),
            payload.comm_flow(),
            payload.comm_data.clone(),
            payload.vid(),
            payload.pid(),
            payload.serial_no(),
            no_optional_data,
        );

        let mut out = [0; 256];
        let s = unwrap!(reencoded.as_str(&mut out), "Failed to encode").0;
        assert_eq!(s, QR_CODE);
    }

    #[test]
    fn parse_pairing_code_round_trips_with_encoder() {
        // The two vectors from `code.rs`'s `can_compute_pairing_code`, parsed back.
        for (code, passcode, discriminator) in [
            ("00876800071", 123456_u32, 250_u16),
            ("26318621095", 34567890, 2976),
        ] {
            let payload = unwrap!(QrPayload::parse_pairing_code(code), "Failed to parse");

            assert_eq!(payload.passcode(), passcode);
            // Only the upper 4 bits of the discriminator survive a manual code.
            assert_eq!(payload.short_discriminator(), (discriminator >> 8) as u8);
            // The 11-digit variant implies the standard flow and carries no VID/PID.
            assert_eq!(payload.comm_flow(), Some(CommFlowType::Standard));
            assert_eq!(payload.vid_pid(), None);
            // Nothing tells us how to reach the device.
            assert!(payload.discovery_capabilities.is_empty());

            // And the code we parsed re-computes from the parsed passcode + a
            // discriminator whose upper 4 bits match.
            let comm_data = BasicCommData {
                password: payload.passcode().to_le_bytes().into(),
                discriminator,
            };
            assert_eq!(comm_data.compute_pairing_code(), code);
        }
    }

    #[test]
    fn commissionable_filter_uses_the_right_discriminator_field() {
        // A QR carries the full 12-bit discriminator...
        let mut buf = [0; 128];
        let qr = unwrap!(
            QrPayload::parse("MT:-24J0AFN00KA064IJ3P0IXZB0DK5N1K8SQ1RYCU1-A40", &mut buf),
            "Failed to parse"
        );
        let filter = qr.commissionable_filter();

        assert_eq!(filter.discriminator, Some(3840));
        assert_eq!(filter.short_discriminator, None);

        // ... while a manual pairing code only carries the upper 4 bits, which must
        // land in `short_discriminator` (0xF00 >> 8 == 15) and *not* in the
        // full-discriminator field, where it would match nothing.
        let manual = unwrap!(QrPayload::parse_pairing_code("34970112332"), "Failed");
        let filter = manual.commissionable_filter();

        assert_eq!(filter.short_discriminator, Some(15));
        assert_eq!(filter.discriminator, None);

        // Neither constrains vendor/product: a device may advertise an anonymized
        // Product ID of 0 during discovery.
        assert_eq!(filter.vendor_id, None);
        assert_eq!(filter.product_id, None);
    }

    #[test]
    fn parse_pairing_code_accepts_pretty_form() {
        // The canonical CHIP test device: discriminator 3840, passcode 20202021.
        let compact = unwrap!(QrPayload::parse_pairing_code("34970112332"), "compact");
        let pretty = unwrap!(QrPayload::parse_pairing_code("3497-0112-332"), "pretty");

        assert_eq!(compact.passcode(), 20202021);
        assert_eq!(compact.short_discriminator(), 15); // 3840 >> 8
        assert_eq!(pretty.passcode(), compact.passcode());
        assert_eq!(pretty.short_discriminator(), compact.short_discriminator());
    }

    #[test]
    fn parse_pairing_code_rejects_bad_input() {
        // Bad Verhoeff check digit (last digit of the valid code bumped).
        assert!(QrPayload::parse_pairing_code("34970112333").is_err());
        // Not a digit.
        assert!(QrPayload::parse_pairing_code("3497011233X").is_err());
        // Wrong length (neither 11 nor 21).
        assert!(QrPayload::parse_pairing_code("3497011233").is_err());
        // A leading digit of 8/9 indicates a future payload version, not v1.
        assert!(QrPayload::parse_pairing_code("94970112332").is_err());
        // VID/PID-present flag set (leading digit >= 4) but only 11 digits.
        assert!(QrPayload::parse_pairing_code("74970112332").is_err());
    }

    #[test]
    fn parse_qr_rejects_bad_input() {
        let mut buf = [0; 128];

        // Missing `MT:` prefix.
        assert!(QrPayload::parse("YNJV7VSC00CMVH7SR00", &mut buf).is_err());
        // Invalid base38 character (`!`).
        assert!(QrPayload::parse("MT:YNJV7VSC00CMVH7SR0!", &mut buf).is_err());
        // Too short to hold the fixed payload.
        assert!(QrPayload::parse("MT:00", &mut buf).is_err());
    }
}