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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use fidl_fuchsia_bluetooth as fidl_bt;
use fidl_fuchsia_bluetooth_bredr::{
    self as fidl_bredr, ProfileDescriptor, ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
    ATTR_SERVICE_CLASS_ID_LIST,
};
use fidl_table_validation::ValidFidlTable;
use fuchsia_inspect as inspect;
use fuchsia_inspect_derive::{AttachError, Inspect, Unit};
use std::cmp::min;
use std::collections::HashSet;

use crate::assigned_numbers::{constants::SERVICE_CLASS_UUIDS, AssignedNumber};
use crate::error::Error;
use crate::types::Uuid;

/// The Protocol and Service Multiplexer (PSM) for L2cap connections.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Psm(u16);

impl Psm {
    /// PSMs commonly used in the codebase.
    pub const RFCOMM: Self = Self(fidl_bredr::PSM_RFCOMM);
    pub const HID_CONTROL: Self = Self(fidl_bredr::PSM_HID_CONTROL);
    pub const HID_INTERRUPT: Self = Self(fidl_bredr::PSM_HID_INTERRUPT);
    pub const AVDTP: Self = Self(fidl_bredr::PSM_AVDTP);
    pub const AVCTP: Self = Self(fidl_bredr::PSM_AVCTP);
    pub const AVCTP_BROWSE: Self = Self(fidl_bredr::PSM_AVCTP_BROWSE);
    pub const DYNAMIC: Self = Self(fidl_bredr::PSM_DYNAMIC);

    pub fn new(value: u16) -> Self {
        Self(value)
    }
}

impl From<Psm> for u16 {
    fn from(src: Psm) -> u16 {
        src.0
    }
}

/// Try to interpret a DataElement as a ProfileDesciptor.
/// Returns None if the DataElement is not in the correct format to represent a ProfileDescriptor.
pub fn elem_to_profile_descriptor(elem: &fidl_bredr::DataElement) -> Option<ProfileDescriptor> {
    if let fidl_bredr::DataElement::Sequence(seq) = elem {
        if seq.len() != 2 {
            return None;
        }

        if seq[0].is_none() {
            return None;
        }
        let profile_id = match **seq[0].as_ref().expect("not none") {
            fidl_bredr::DataElement::Uuid(uuid) => {
                let uuid: Uuid = uuid.into();
                match uuid.try_into() {
                    Err(_) => return None,
                    Ok(profile_id) => profile_id,
                }
            }
            _ => return None,
        };

        if seq[1].is_none() {
            return None;
        }
        let [major_version, minor_version] = match **seq[1].as_ref().expect("not none") {
            fidl_bredr::DataElement::Uint16(val) => val.to_be_bytes(),
            _ => return None,
        };
        return Some(ProfileDescriptor { profile_id, major_version, minor_version });
    }
    None
}

/// Find an element representing the Bluetooth Profile Descriptor List in `attributes`, and
/// convert the elements in the list into ProfileDescriptors.
/// Returns an Error if no matching element was found, or if any element of the list couldn't be converted
/// into a ProfileDescriptor.
pub fn find_profile_descriptors(
    attributes: &[fidl_bredr::Attribute],
) -> Result<Vec<ProfileDescriptor>, Error> {
    let attr = attributes
        .iter()
        .find(|a| a.id == ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST)
        .ok_or(Error::profile("missing profile descriptor"))?;

    let fidl_bredr::DataElement::Sequence(profiles) = &attr.element else {
        return Err(Error::profile("attribute element is invalidly formatted"));
    };
    let mut result = Vec::new();
    for elem in profiles {
        let elem = elem.as_ref().ok_or(Error::profile("null DataElement in sequence"))?;
        result.push(
            elem_to_profile_descriptor(&*elem)
                .ok_or(Error::profile("couldn't convert to a ProfileDescriptor"))?,
        );
    }
    if result.is_empty() {
        Err(Error::profile("no profile descriptor found"))
    } else {
        Ok(result)
    }
}

pub fn profile_descriptor_to_assigned(profile_desc: &ProfileDescriptor) -> Option<AssignedNumber> {
    SERVICE_CLASS_UUIDS.iter().find(|scn| profile_desc.profile_id as u16 == scn.number).cloned()
}

/// Returns the PSM from the provided `protocol`. Returns None if the protocol
/// is not L2CAP or does not contain a PSM.
pub fn psm_from_protocol(protocol: &Vec<ProtocolDescriptor>) -> Option<Psm> {
    for descriptor in protocol {
        if descriptor.protocol == fidl_bredr::ProtocolIdentifier::L2Cap {
            if descriptor.params.len() != 1 {
                return None;
            }

            if let DataElement::Uint16(psm) = descriptor.params[0] {
                return Some(Psm::new(psm));
            }
            return None;
        }
    }
    None
}

/// Search for a Service Class UUID from a list of attributes (such as returned via Service Search)
pub fn find_service_classes(
    attributes: &[fidl_fuchsia_bluetooth_bredr::Attribute],
) -> Vec<AssignedNumber> {
    let attr = match attributes.iter().find(|a| a.id == ATTR_SERVICE_CLASS_ID_LIST) {
        None => return vec![],
        Some(attr) => attr,
    };
    if let fidl_fuchsia_bluetooth_bredr::DataElement::Sequence(elems) = &attr.element {
        let uuids: Vec<Uuid> = elems
            .iter()
            .filter_map(|e| {
                e.as_ref().and_then(|e| {
                    if let fidl_fuchsia_bluetooth_bredr::DataElement::Uuid(uuid) = **e {
                        Some(uuid.into())
                    } else {
                        None
                    }
                })
            })
            .collect();
        SERVICE_CLASS_UUIDS
            .iter()
            .filter(|scn| uuids.contains(&Uuid::new16(scn.number)))
            .cloned()
            .collect()
    } else {
        return vec![];
    }
}

/// Given two SecurityRequirements, combines both into requirements as strict as either.
/// A stricter SecurityRequirements is defined as:
///   1) Authentication required is stricter than not.
///   2) Secure Connections required is stricter than not.
pub fn combine_security_requirements(
    reqs: &SecurityRequirements,
    other: &SecurityRequirements,
) -> SecurityRequirements {
    let authentication_required =
        match (reqs.authentication_required, other.authentication_required) {
            (Some(true), _) | (_, Some(true)) => Some(true),
            (Some(x), None) | (None, Some(x)) => Some(x),
            _ => None,
        };
    let secure_connections_required =
        match (reqs.secure_connections_required, other.secure_connections_required) {
            (Some(true), _) | (_, Some(true)) => Some(true),
            (Some(x), None) | (None, Some(x)) => Some(x),
            _ => None,
        };
    SecurityRequirements { authentication_required, secure_connections_required }
}

/// Given two ChannelParameters, combines both into a set of ChannelParameters
/// with the least requesting of resources.
/// This is defined as:
///   1) Basic requires fewer resources than ERTM.
///   2) A smaller SDU size is more restrictive.
pub fn combine_channel_parameters(
    params: &ChannelParameters,
    other: &ChannelParameters,
) -> ChannelParameters {
    let channel_mode = match (params.channel_mode, other.channel_mode) {
        (Some(fidl_bredr::ChannelMode::Basic), _) | (_, Some(fidl_bredr::ChannelMode::Basic)) => {
            Some(fidl_bredr::ChannelMode::Basic)
        }
        (Some(x), None) | (None, Some(x)) => Some(x),
        _ => None,
    };
    let max_rx_sdu_size = match (params.max_rx_sdu_size, other.max_rx_sdu_size) {
        (Some(rx1), Some(rx2)) => Some(min(rx1, rx2)),
        (Some(x), None) | (None, Some(x)) => Some(x),
        _ => None,
    };
    let security_requirements = match (&params.security_requirements, &other.security_requirements)
    {
        (Some(reqs1), Some(reqs2)) => Some(combine_security_requirements(reqs1, reqs2)),
        (Some(reqs), _) | (_, Some(reqs)) => Some(reqs.clone()),
        _ => None,
    };
    ChannelParameters { channel_mode, max_rx_sdu_size, security_requirements }
}

/// The basic building block for elements in a SDP record.
/// Corresponds directly to the FIDL `DataElement` definition - with the extra
/// properties of Clone and PartialEq.
/// See [fuchsia.bluetooth.bredr.DataElement] for more documentation.
#[derive(Clone, Debug, PartialEq)]
pub enum DataElement {
    Int8(i8),
    Int16(i16),
    Int32(i32),
    Int64(i64),
    Uint8(u8),
    Uint16(u16),
    Uint32(u32),
    Uint64(u64),
    Str(Vec<u8>),
    Url(String),
    Uuid(fidl_bt::Uuid),
    Bool(bool),
    Sequence(Vec<Box<DataElement>>),
    Alternatives(Vec<Box<DataElement>>),
}

impl From<&fidl_bredr::DataElement> for DataElement {
    fn from(src: &fidl_bredr::DataElement) -> DataElement {
        use fidl_bredr::DataElement as fDataElement;
        match src {
            fDataElement::Int8(x) => DataElement::Int8(*x),
            fDataElement::Int16(x) => DataElement::Int16(*x),
            fDataElement::Int32(x) => DataElement::Int32(*x),
            fDataElement::Int64(x) => DataElement::Int64(*x),
            fDataElement::Uint8(x) => DataElement::Uint8(*x),
            fDataElement::Uint16(x) => DataElement::Uint16(*x),
            fDataElement::Uint32(x) => DataElement::Uint32(*x),
            fDataElement::Uint64(x) => DataElement::Uint64(*x),
            // TODO(https://fxbug.dev/42058871) Replace clones with moves where possible.
            fDataElement::Str(v) => DataElement::Str(v.clone()),
            fDataElement::Url(s) => DataElement::Url(s.to_string()),
            fDataElement::Uuid(uuid) => DataElement::Uuid(uuid.clone()),
            fDataElement::B(b) => DataElement::Bool(*b),
            fDataElement::Sequence(x) => {
                let mapped = x
                    .into_iter()
                    .filter_map(|opt| opt.as_ref().map(|t| Box::new(DataElement::from(&**t))))
                    .collect::<Vec<_>>();
                DataElement::Sequence(mapped)
            }
            fDataElement::Alternatives(x) => {
                let mapped = x
                    .into_iter()
                    .filter_map(|opt| opt.as_ref().map(|t| Box::new(DataElement::from(&**t))))
                    .collect::<Vec<_>>();
                DataElement::Alternatives(mapped)
            }
        }
    }
}

impl From<&DataElement> for fidl_bredr::DataElement {
    fn from(src: &DataElement) -> fidl_bredr::DataElement {
        use fidl_bredr::DataElement as fDataElement;
        match src {
            DataElement::Int8(x) => fDataElement::Int8(*x),
            DataElement::Int16(x) => fDataElement::Int16(*x),
            DataElement::Int32(x) => fDataElement::Int32(*x),
            DataElement::Int64(x) => fDataElement::Int64(*x),
            DataElement::Uint8(x) => fDataElement::Uint8(*x),
            DataElement::Uint16(x) => fDataElement::Uint16(*x),
            DataElement::Uint32(x) => fDataElement::Uint32(*x),
            DataElement::Uint64(x) => fDataElement::Uint64(*x),
            DataElement::Str(v) => fDataElement::Str(v.clone()),
            DataElement::Url(s) => fDataElement::Url(s.to_string()),
            DataElement::Uuid(uuid) => fDataElement::Uuid(uuid.clone()),
            DataElement::Bool(b) => fDataElement::B(*b),
            DataElement::Sequence(x) => {
                let mapped = x
                    .into_iter()
                    .map(|t| Some(Box::new(fDataElement::from(&**t))))
                    .collect::<Vec<_>>();
                fDataElement::Sequence(mapped)
            }
            DataElement::Alternatives(x) => {
                let mapped = x
                    .into_iter()
                    .map(|t| Some(Box::new(fDataElement::from(&**t))))
                    .collect::<Vec<_>>();
                fDataElement::Alternatives(mapped)
            }
        }
    }
}

#[derive(Debug, PartialEq)]
pub struct DataElementConversionError {
    pub data_element: DataElement,
}

// Macro for generating impls for converting between rust types and their DataElement wrappers.
macro_rules! generate_data_element_conversion {
    ($variant: ident, $type: ty) => {
        impl TryFrom<DataElement> for $type {
            type Error = DataElementConversionError;

            fn try_from(data_element: DataElement) -> Result<$type, DataElementConversionError> {
                match data_element {
                    DataElement::$variant(x) => Ok(x),
                    _ => Err(DataElementConversionError { data_element }),
                }
            }
        }

        impl From<$type> for DataElement {
            fn from(x: $type) -> DataElement {
                DataElement::$variant(x)
            }
        }
    };
}

// Generate the impls for converting between rust types and their DataElement wrappers.
generate_data_element_conversion!(Int8, i8);
generate_data_element_conversion!(Int16, i16);
generate_data_element_conversion!(Int32, i32);
generate_data_element_conversion!(Int64, i64);
generate_data_element_conversion!(Uint8, u8);
generate_data_element_conversion!(Uint16, u16);
generate_data_element_conversion!(Uint32, u32);
generate_data_element_conversion!(Uint64, u64);
generate_data_element_conversion!(Str, Vec<u8>);
generate_data_element_conversion!(Uuid, fidl_bt::Uuid);
generate_data_element_conversion!(Url, String);
generate_data_element_conversion!(Bool, bool);

/// Information about a communications protocol.
/// Corresponds directly to the FIDL `ProtocolDescriptor` definition - with the extra
/// properties of Clone and PartialEq.
/// See [fuchsia.bluetooth.bredr.ProtocolDescriptor] for more documentation.
#[derive(Clone, Debug, PartialEq)]
pub struct ProtocolDescriptor {
    pub protocol: fidl_bredr::ProtocolIdentifier,
    pub params: Vec<DataElement>,
}

impl From<&fidl_bredr::ProtocolDescriptor> for ProtocolDescriptor {
    fn from(src: &fidl_bredr::ProtocolDescriptor) -> ProtocolDescriptor {
        let params = src.params.iter().map(|elem| DataElement::from(elem)).collect();
        ProtocolDescriptor { protocol: src.protocol, params }
    }
}

impl From<&ProtocolDescriptor> for fidl_bredr::ProtocolDescriptor {
    fn from(src: &ProtocolDescriptor) -> fidl_bredr::ProtocolDescriptor {
        let params = src.params.iter().map(|elem| fidl_bredr::DataElement::from(elem)).collect();
        fidl_bredr::ProtocolDescriptor { protocol: src.protocol, params }
    }
}

pub fn l2cap_connect_parameters(
    psm: Psm,
    mode: fidl_bredr::ChannelMode,
) -> fidl_bredr::ConnectParameters {
    fidl_bredr::ConnectParameters::L2cap(fidl_bredr::L2capParameters {
        psm: Some(psm.into()),
        parameters: Some(fidl_bredr::ChannelParameters {
            channel_mode: Some(mode),
            ..fidl_bredr::ChannelParameters::default()
        }),
        ..fidl_bredr::L2capParameters::default()
    })
}

/// A generic attribute used for protocol information.
/// Corresponds directly to the FIDL `Attribute` definition - with the extra
/// properties of Clone and PartialEq.
/// See [fuchsia.bluetooth.bredr.Attribute] for more documentation.
#[derive(Clone, Debug, PartialEq)]
pub struct Attribute {
    pub id: u16,
    pub element: DataElement,
}

impl From<&fidl_bredr::Attribute> for Attribute {
    fn from(src: &fidl_bredr::Attribute) -> Attribute {
        Attribute { id: src.id, element: DataElement::from(&src.element) }
    }
}

impl From<&Attribute> for fidl_bredr::Attribute {
    fn from(src: &Attribute) -> fidl_bredr::Attribute {
        fidl_bredr::Attribute { id: src.id, element: fidl_bredr::DataElement::from(&src.element) }
    }
}

/// Human-readable information about a service.
/// Corresponds directly to the FIDL `Information` definition - with the extra
/// properties of Clone and PartialEq.
/// See [fuchsia.bluetooth.bredr.Information] for more documentation.
#[derive(Clone, Debug, PartialEq)]
pub struct Information {
    pub language: String,
    pub name: Option<String>,
    pub description: Option<String>,
    pub provider: Option<String>,
}

impl TryFrom<&fidl_bredr::Information> for Information {
    type Error = Error;

    fn try_from(src: &fidl_bredr::Information) -> Result<Information, Self::Error> {
        let language = match src.language.as_ref().map(String::as_str) {
            None | Some("") => return Err(Error::missing("bredr.Information.language")),
            Some(l) => l.to_string().clone(),
        };

        Ok(Information {
            language,
            name: src.name.clone(),
            description: src.description.clone(),
            provider: src.provider.clone(),
        })
    }
}

impl TryFrom<&Information> for fidl_bredr::Information {
    type Error = Error;

    fn try_from(src: &Information) -> Result<fidl_bredr::Information, Self::Error> {
        if src.language.is_empty() {
            return Err(Error::missing("Information.language"));
        }

        Ok(fidl_bredr::Information {
            language: Some(src.language.clone()),
            name: src.name.clone(),
            description: src.description.clone(),
            provider: src.provider.clone(),
            ..Default::default()
        })
    }
}

/// Definition of a service that is to be advertised via Bluetooth BR/EDR.
/// Corresponds directly to the FIDL `ServiceDefinition` definition - with the extra
/// properties of Clone and PartialEq.
/// See [fuchsia.bluetooth.bredr.ServiceDefinition] for more documentation.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ServiceDefinition {
    pub service_class_uuids: Vec<Uuid>,
    pub protocol_descriptor_list: Vec<ProtocolDescriptor>,
    pub additional_protocol_descriptor_lists: Vec<Vec<ProtocolDescriptor>>,
    pub profile_descriptors: Vec<fidl_bredr::ProfileDescriptor>,
    pub information: Vec<Information>,
    pub additional_attributes: Vec<Attribute>,
}

impl ServiceDefinition {
    /// Returns the primary PSM associated with this ServiceDefinition.
    pub fn primary_psm(&self) -> Option<Psm> {
        psm_from_protocol(&self.protocol_descriptor_list)
    }

    /// Returns the additional PSMs associated with this ServiceDefinition.
    pub fn additional_psms(&self) -> HashSet<Psm> {
        self.additional_protocol_descriptor_lists
            .iter()
            .filter_map(|protocol| psm_from_protocol(protocol))
            .collect()
    }

    /// Returns the PSM in the GOEP L2CAP Attribute, if provided.
    pub fn goep_l2cap_psm(&self) -> Option<Psm> {
        const GOEP_L2CAP_PSM_ATTRIBUTE: u16 = 0x0200;
        let Some(attribute) =
            self.additional_attributes.iter().find(|attr| attr.id == GOEP_L2CAP_PSM_ATTRIBUTE)
        else {
            return None;
        };

        let DataElement::Uint16(psm) = attribute.element else {
            return None;
        };

        Some(Psm::new(psm))
    }

    /// Returns all the PSMs associated with this ServiceDefinition.
    ///
    /// It's possible that the definition doesn't provide any PSMs, in which
    /// case the returned set will be empty.
    pub fn psm_set(&self) -> HashSet<Psm> {
        let mut psms = self.additional_psms();
        if let Some(psm) = self.primary_psm() {
            let _ = psms.insert(psm);
        }
        if let Some(psm) = self.goep_l2cap_psm() {
            let _ = psms.insert(psm);
        }

        psms
    }
}

impl TryFrom<&fidl_bredr::ServiceDefinition> for ServiceDefinition {
    type Error = Error;

    fn try_from(src: &fidl_bredr::ServiceDefinition) -> Result<ServiceDefinition, Self::Error> {
        let service_class_uuids = match &src.service_class_uuids {
            Some(uuids) if !uuids.is_empty() => uuids.iter().map(Uuid::from).collect(),
            _ => {
                return Err(Error::conversion(
                    "bredr.ServiceDefinition.service_class_uuids is empty",
                ))
            }
        };

        let protocol_descriptor_list: Vec<ProtocolDescriptor> = src
            .protocol_descriptor_list
            .as_ref()
            .map_or(vec![], |p| p.into_iter().map(|d| ProtocolDescriptor::from(d)).collect());
        let additional_protocol_descriptor_lists: Vec<Vec<ProtocolDescriptor>> =
            src.additional_protocol_descriptor_lists.as_ref().map_or(vec![], |desc_lists| {
                desc_lists
                    .into_iter()
                    .map(|desc_list| {
                        desc_list.into_iter().map(|d| ProtocolDescriptor::from(d)).collect()
                    })
                    .collect()
            });
        let profile_descriptors: Vec<fidl_bredr::ProfileDescriptor> =
            src.profile_descriptors.clone().unwrap_or(vec![]);
        let information: Result<Vec<Information>, Error> =
            src.information.as_ref().map_or(Ok(vec![]), |infos| {
                infos.into_iter().map(|i| Information::try_from(i)).collect()
            });
        let additional_attributes: Vec<Attribute> = src
            .additional_attributes
            .as_ref()
            .map_or(vec![], |attrs| attrs.into_iter().map(|a| Attribute::from(a)).collect());

        Ok(ServiceDefinition {
            service_class_uuids,
            protocol_descriptor_list,
            additional_protocol_descriptor_lists,
            profile_descriptors,
            information: information?,
            additional_attributes,
        })
    }
}

impl TryFrom<&ServiceDefinition> for fidl_bredr::ServiceDefinition {
    type Error = Error;

    fn try_from(src: &ServiceDefinition) -> Result<fidl_bredr::ServiceDefinition, Self::Error> {
        if src.service_class_uuids.is_empty() {
            return Err(Error::conversion("ServiceDefinitions.service_class_uuids is empty"));
        }
        let service_class_uuids = src.service_class_uuids.iter().map(fidl_bt::Uuid::from).collect();

        let protocol_descriptor_list: Vec<fidl_bredr::ProtocolDescriptor> = src
            .protocol_descriptor_list
            .iter()
            .map(|d| fidl_bredr::ProtocolDescriptor::from(d))
            .collect();
        let additional_protocol_descriptor_lists: Vec<Vec<fidl_bredr::ProtocolDescriptor>> = src
            .additional_protocol_descriptor_lists
            .iter()
            .map(|desc_list| {
                desc_list.into_iter().map(|d| fidl_bredr::ProtocolDescriptor::from(d)).collect()
            })
            .collect();
        let profile_descriptors: Vec<fidl_bredr::ProfileDescriptor> =
            src.profile_descriptors.clone();
        let information: Result<Vec<fidl_bredr::Information>, Error> =
            src.information.iter().map(|i| fidl_bredr::Information::try_from(i)).collect();
        let additional_attributes: Vec<fidl_bredr::Attribute> =
            src.additional_attributes.iter().map(|a| fidl_bredr::Attribute::from(a)).collect();

        Ok(fidl_bredr::ServiceDefinition {
            service_class_uuids: Some(service_class_uuids),
            protocol_descriptor_list: Some(protocol_descriptor_list),
            additional_protocol_descriptor_lists: Some(additional_protocol_descriptor_lists),
            profile_descriptors: Some(profile_descriptors),
            information: Some(information?),
            additional_attributes: Some(additional_attributes),
            ..Default::default()
        })
    }
}

/// Authentication and permission requirements for an advertised service.
/// Corresponds directly to the FIDL `SecurityRequirements` definition - with the extra properties
/// of Clone and PartialEq.
/// See [fuchsia.bluetooth.bredr.SecurityRequirements] for more documentation.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SecurityRequirements {
    pub authentication_required: Option<bool>,
    pub secure_connections_required: Option<bool>,
}

impl From<&fidl_bredr::SecurityRequirements> for SecurityRequirements {
    fn from(src: &fidl_bredr::SecurityRequirements) -> SecurityRequirements {
        SecurityRequirements {
            authentication_required: src.authentication_required,
            secure_connections_required: src.secure_connections_required,
        }
    }
}

impl From<&SecurityRequirements> for fidl_bredr::SecurityRequirements {
    fn from(src: &SecurityRequirements) -> fidl_bredr::SecurityRequirements {
        fidl_bredr::SecurityRequirements {
            authentication_required: src.authentication_required,
            secure_connections_required: src.secure_connections_required,
            ..Default::default()
        }
    }
}

/// Minimum SDU size the service is capable of accepting.
/// See [fuchsia.bluetooth.bredr.ChannelParameters] for more documentation.
const MIN_RX_SDU_SIZE: u16 = 48;

/// Preferred L2CAP channel parameters for an advertised service.
/// Corresponds directly to the FIDL `ChannelParameters` definition - with the extra properties
/// of Clone and PartialEq.
/// The invariants of the FIDL definition are enforced - the max SDU size must be >= 48.
/// See [fuchsia.bluetooth.bredr.ChannelParameters] for more documentation.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ChannelParameters {
    pub channel_mode: Option<fidl_bredr::ChannelMode>,
    pub max_rx_sdu_size: Option<u16>,
    pub security_requirements: Option<SecurityRequirements>,
}

impl TryFrom<&fidl_bredr::ChannelParameters> for ChannelParameters {
    type Error = Error;

    fn try_from(src: &fidl_bredr::ChannelParameters) -> Result<ChannelParameters, Self::Error> {
        if let Some(size) = src.max_rx_sdu_size {
            if size < MIN_RX_SDU_SIZE {
                return Err(Error::conversion(format!(
                    "bredr.ChannelParameters.max_rx_sdu_size is too small: {size}"
                )));
            }
        }

        Ok(ChannelParameters {
            channel_mode: src.channel_mode,
            max_rx_sdu_size: src.max_rx_sdu_size,
            security_requirements: src
                .security_requirements
                .as_ref()
                .map(SecurityRequirements::from),
        })
    }
}

impl TryFrom<&ChannelParameters> for fidl_bredr::ChannelParameters {
    type Error = Error;

    fn try_from(src: &ChannelParameters) -> Result<fidl_bredr::ChannelParameters, Self::Error> {
        if let Some(size) = src.max_rx_sdu_size {
            if size < MIN_RX_SDU_SIZE {
                return Err(Error::conversion(format!(
                    "ChannelParameters.max_rx_sdu_size is too small: {size}"
                )));
            }
        }

        Ok(fidl_bredr::ChannelParameters {
            channel_mode: src.channel_mode,
            max_rx_sdu_size: src.max_rx_sdu_size,
            security_requirements: src
                .security_requirements
                .as_ref()
                .map(fidl_bredr::SecurityRequirements::from),
            ..Default::default()
        })
    }
}

#[derive(Debug, Clone, ValidFidlTable, PartialEq)]
#[fidl_table_src(fidl_bredr::ScoConnectionParameters)]
pub struct ValidScoConnectionParameters {
    pub parameter_set: fidl_bredr::HfpParameterSet,
    pub air_coding_format: fidl_bredr::CodingFormat,
    pub air_frame_size: u16,
    pub io_bandwidth: u32,
    pub io_coding_format: fidl_bredr::CodingFormat,
    pub io_frame_size: u16,
    #[fidl_field_type(optional)]
    pub io_pcm_data_format: Option<fidl_fuchsia_hardware_audio::SampleFormat>,
    #[fidl_field_type(optional)]
    pub io_pcm_sample_payload_msb_position: Option<u8>,
    pub path: fidl_bredr::DataPath,
}

impl Unit for ValidScoConnectionParameters {
    type Data = inspect::Node;
    fn inspect_create(&self, parent: &inspect::Node, name: impl AsRef<str>) -> Self::Data {
        let mut node = parent.create_child(name.as_ref());
        self.inspect_update(&mut node);
        node
    }

    fn inspect_update(&self, data: &mut Self::Data) {
        data.record_string("parameter_set", &format!("{:?}", self.parameter_set));
        data.record_string("air_coding_format", &format!("{:?}", self.air_coding_format));
        data.record_uint("air_frame_size", self.air_frame_size.into());
        data.record_uint("io_bandwidth", self.io_bandwidth.into());
        data.record_string("io_coding_format", &format!("{:?}", self.io_coding_format));
        data.record_uint("io_frame_size", self.io_frame_size.into());
        if let Some(io_pcm_data_format) = &self.io_pcm_data_format {
            data.record_string("io_pcm_data_format", &format!("{:?}", io_pcm_data_format));
        }
        if let Some(io_pcm_sample_payload_msb_position) = &self.io_pcm_sample_payload_msb_position {
            data.record_uint(
                "io_pcm_sample_payload_msb_position",
                (*io_pcm_sample_payload_msb_position).into(),
            );
        }
        data.record_string("path", &format!("{:?}", self.path));
    }
}

impl Inspect for &mut ValidScoConnectionParameters {
    fn iattach(self, parent: &inspect::Node, name: impl AsRef<str>) -> Result<(), AttachError> {
        // The created node is owned by the provided `parent`.
        parent.record(self.inspect_create(parent, name));
        Ok(())
    }
}

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

    #[test]
    fn test_find_descriptors_fails_with_no_descriptors() {
        assert!(find_profile_descriptors(&[]).is_err());

        let mut attributes = vec![fidl_bredr::Attribute {
            id: 0x3001,
            element: fidl_bredr::DataElement::Uint32(0xF00FC0DE),
        }];

        assert!(find_profile_descriptors(&attributes).is_err());

        // Wrong element type
        attributes.push(fidl_bredr::Attribute {
            id: fidl_bredr::ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
            element: fidl_bredr::DataElement::Uint32(0xABADC0DE),
        });

        assert!(find_profile_descriptors(&attributes).is_err());

        // Empty sequence
        attributes[1].element = fidl_bredr::DataElement::Sequence(vec![]);

        assert!(find_profile_descriptors(&attributes).is_err());
    }

    #[test]
    fn test_find_descriptors_returns_descriptors() {
        let attributes = vec![fidl_bredr::Attribute {
            id: fidl_bredr::ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
            element: fidl_bredr::DataElement::Sequence(vec![
                Some(Box::new(fidl_bredr::DataElement::Sequence(vec![
                    Some(Box::new(fidl_bredr::DataElement::Uuid(Uuid::new16(0x1101).into()))),
                    Some(Box::new(fidl_bredr::DataElement::Uint16(0x0103))),
                ]))),
                Some(Box::new(fidl_bredr::DataElement::Sequence(vec![
                    Some(Box::new(fidl_bredr::DataElement::Uuid(Uuid::new16(0x113A).into()))),
                    Some(Box::new(fidl_bredr::DataElement::Uint16(0x0302))),
                ]))),
            ]),
        }];

        let result = find_profile_descriptors(&attributes);
        assert!(result.is_ok());
        let result = result.expect("result");
        assert_eq!(2, result.len());

        assert_eq!(fidl_bredr::ServiceClassProfileIdentifier::SerialPort, result[0].profile_id);
        assert_eq!(1, result[0].major_version);
        assert_eq!(3, result[0].minor_version);
    }

    #[test]
    fn test_find_service_classes_attribute_missing() {
        assert_eq!(find_service_classes(&[]), Vec::new());
        let attributes = vec![fidl_bredr::Attribute {
            id: fidl_bredr::ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
            element: fidl_bredr::DataElement::Sequence(vec![
                Some(Box::new(fidl_bredr::DataElement::Sequence(vec![
                    Some(Box::new(fidl_bredr::DataElement::Uuid(Uuid::new16(0x1101).into()))),
                    Some(Box::new(fidl_bredr::DataElement::Uint16(0x0103))),
                ]))),
                Some(Box::new(fidl_bredr::DataElement::Sequence(vec![
                    Some(Box::new(fidl_bredr::DataElement::Uuid(Uuid::new16(0x113A).into()))),
                    Some(Box::new(fidl_bredr::DataElement::Uint16(0x0302))),
                ]))),
            ]),
        }];
        assert_eq!(find_service_classes(&attributes), Vec::new());
    }

    #[test]
    fn test_find_service_classes_wrong_type() {
        let attributes = vec![fidl_bredr::Attribute {
            id: fidl_bredr::ATTR_SERVICE_CLASS_ID_LIST,
            element: fidl_bredr::DataElement::Uint32(0xc0defae5u32),
        }];
        assert_eq!(find_service_classes(&attributes), Vec::new());
    }

    #[test]
    fn test_find_service_classes_returns_known_classes() {
        let attribute = fidl_bredr::Attribute {
            id: fidl_bredr::ATTR_SERVICE_CLASS_ID_LIST,
            element: fidl_bredr::DataElement::Sequence(vec![Some(Box::new(
                fidl_bredr::DataElement::Uuid(Uuid::new16(0x1101).into()),
            ))]),
        };

        let result = find_service_classes(&[attribute]);
        assert_eq!(1, result.len());
        let assigned_num = result.first().unwrap();
        assert_eq!(0x1101, assigned_num.number); // 0x1101 is the 16-bit UUID of SerialPort
        assert_eq!("SerialPort", assigned_num.name);

        let unknown_uuids = fidl_bredr::Attribute {
            id: fidl_bredr::ATTR_SERVICE_CLASS_ID_LIST,
            element: fidl_bredr::DataElement::Sequence(vec![
                Some(Box::new(fidl_bredr::DataElement::Uuid(Uuid::new16(0x1101).into()))),
                Some(Box::new(fidl_bredr::DataElement::Uuid(Uuid::new16(0xc0de).into()))),
            ]),
        };

        // Discards unknown UUIDs
        let result = find_service_classes(&[unknown_uuids]);
        assert_eq!(1, result.len());
        let assigned_num = result.first().unwrap();
        assert_eq!(0x1101, assigned_num.number); // 0x1101 is the 16-bit UUID of SerialPort
        assert_eq!("SerialPort", assigned_num.name);
    }

    #[test]
    fn test_psm_from_protocol() {
        let empty = vec![];
        assert_eq!(None, psm_from_protocol(&empty));

        let no_psm = vec![ProtocolDescriptor {
            protocol: fidl_bredr::ProtocolIdentifier::L2Cap,
            params: vec![],
        }];
        assert_eq!(None, psm_from_protocol(&no_psm));

        let psm = Psm::new(10);
        let valid_psm = vec![ProtocolDescriptor {
            protocol: fidl_bredr::ProtocolIdentifier::L2Cap,
            params: vec![DataElement::Uint16(psm.into())],
        }];
        assert_eq!(Some(psm), psm_from_protocol(&valid_psm));

        let rfcomm = vec![
            ProtocolDescriptor {
                protocol: fidl_bredr::ProtocolIdentifier::L2Cap,
                params: vec![], // PSM omitted for RFCOMM.
            },
            ProtocolDescriptor {
                protocol: fidl_bredr::ProtocolIdentifier::Rfcomm,
                params: vec![DataElement::Uint8(10)], // Server channel
            },
        ];
        assert_eq!(None, psm_from_protocol(&rfcomm));
    }

    #[test]
    fn test_elem_to_profile_descriptor_works() {
        let element = fidl_bredr::DataElement::Sequence(vec![
            Some(Box::new(fidl_bredr::DataElement::Uuid(Uuid::new16(0x1101).into()))),
            Some(Box::new(fidl_bredr::DataElement::Uint16(0x0103))),
        ]);

        let descriptor =
            elem_to_profile_descriptor(&element).expect("descriptor should be returned");

        assert_eq!(fidl_bredr::ServiceClassProfileIdentifier::SerialPort, descriptor.profile_id);
        assert_eq!(1, descriptor.major_version);
        assert_eq!(3, descriptor.minor_version);
    }

    #[test]
    fn test_elem_to_profile_descriptor_wrong_element_types() {
        let element = fidl_bredr::DataElement::Sequence(vec![
            Some(Box::new(fidl_bredr::DataElement::Uint16(0x1101))),
            Some(Box::new(fidl_bredr::DataElement::Uint16(0x0103))),
        ]);
        assert!(elem_to_profile_descriptor(&element).is_none());

        let element = fidl_bredr::DataElement::Sequence(vec![
            Some(Box::new(fidl_bredr::DataElement::Uuid(Uuid::new16(0x1101).into()))),
            Some(Box::new(fidl_bredr::DataElement::Uint32(0x0103))),
        ]);
        assert!(elem_to_profile_descriptor(&element).is_none());

        let element = fidl_bredr::DataElement::Sequence(vec![Some(Box::new(
            fidl_bredr::DataElement::Uint32(0x0103),
        ))]);
        assert!(elem_to_profile_descriptor(&element).is_none());

        let element = fidl_bredr::DataElement::Sequence(vec![None]);
        assert!(elem_to_profile_descriptor(&element).is_none());

        let element = fidl_bredr::DataElement::Uint32(0xDEADC0DE);
        assert!(elem_to_profile_descriptor(&element).is_none());
    }

    #[test]
    fn test_invalid_information_fails_gracefully() {
        let empty_language = "".to_string();

        let invalid_local = Information {
            language: empty_language.clone(),
            name: None,
            description: None,
            provider: None,
        };
        let fidl = fidl_bredr::Information::try_from(&invalid_local);
        assert!(fidl.is_err());

        // No language.
        let local = Information::try_from(&fidl_bredr::Information::default());
        assert!(local.is_err());

        let empty_lang_fidl =
            fidl_bredr::Information { language: Some(empty_language), ..Default::default() };
        let local = Information::try_from(&empty_lang_fidl);
        assert!(local.is_err());
    }

    #[test]
    fn get_psm_from_empty_service_definition() {
        let def = ServiceDefinition {
            service_class_uuids: vec![Uuid::new32(1234)],
            protocol_descriptor_list: vec![],
            additional_protocol_descriptor_lists: vec![],
            profile_descriptors: vec![],
            information: vec![],
            additional_attributes: vec![],
        };

        assert_eq!(def.primary_psm(), None);
        assert_eq!(def.additional_psms(), HashSet::new());
        assert_eq!(def.psm_set(), HashSet::new());
    }

    #[test]
    fn test_get_psm_from_service_definition() {
        let uuid = Uuid::new32(1234);
        let psm1 = Psm(10);
        let psm2 = Psm(12);
        let psm3 = Psm(4000);
        let mut def = ServiceDefinition {
            service_class_uuids: vec![uuid],
            protocol_descriptor_list: vec![ProtocolDescriptor {
                protocol: fidl_bredr::ProtocolIdentifier::L2Cap,
                params: vec![DataElement::Uint16(psm1.into())],
            }],
            additional_protocol_descriptor_lists: vec![],
            profile_descriptors: vec![],
            information: vec![],
            additional_attributes: vec![],
        };

        // Primary protocol contains one PSM.
        assert_eq!(def.primary_psm(), Some(psm1));
        assert_eq!(def.additional_psms(), HashSet::new());
        assert_eq!(def.psm_set(), HashSet::from([psm1]));

        def.additional_protocol_descriptor_lists = vec![
            vec![ProtocolDescriptor {
                protocol: fidl_bredr::ProtocolIdentifier::L2Cap,
                params: vec![DataElement::Uint16(psm2.into())],
            }],
            vec![ProtocolDescriptor {
                protocol: fidl_bredr::ProtocolIdentifier::Avdtp,
                params: vec![DataElement::Uint16(0x0103)],
            }],
        ];

        // Additional protocol contains one PSM.
        assert_eq!(def.primary_psm(), Some(psm1));
        assert_eq!(def.additional_psms(), HashSet::from([psm2]));
        assert_eq!(def.psm_set(), HashSet::from([psm1, psm2]));

        // Additional attributes contain one PSM.
        def.additional_attributes =
            vec![Attribute { id: 0x0200, element: DataElement::Uint16(psm3.into()) }];
        assert_eq!(def.primary_psm(), Some(psm1));
        assert_eq!(def.additional_psms(), HashSet::from([psm2]));
        assert_eq!(def.goep_l2cap_psm(), Some(psm3));
        assert_eq!(def.psm_set(), HashSet::from([psm1, psm2, psm3]));
    }

    #[test]
    fn test_service_definition_conversions() {
        let uuid = fidl_bt::Uuid { value: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] };
        let prof_descs = vec![ProfileDescriptor {
            profile_id: fidl_bredr::ServiceClassProfileIdentifier::AvRemoteControl,
            major_version: 1,
            minor_version: 6,
        }];
        let language = "en".to_string();
        let name = "foobar".to_string();
        let description = "fake".to_string();
        let provider = "random".to_string();
        let attribute_id = 0x3001;
        let attribute_value = 0xF00FC0DE;

        let local = ServiceDefinition {
            service_class_uuids: vec![uuid.into()],
            protocol_descriptor_list: vec![ProtocolDescriptor {
                protocol: fidl_bredr::ProtocolIdentifier::L2Cap,
                params: vec![DataElement::Uint16(10)],
            }],
            additional_protocol_descriptor_lists: vec![
                vec![ProtocolDescriptor {
                    protocol: fidl_bredr::ProtocolIdentifier::L2Cap,
                    params: vec![DataElement::Uint16(12)],
                }],
                vec![ProtocolDescriptor {
                    protocol: fidl_bredr::ProtocolIdentifier::Avdtp,
                    params: vec![DataElement::Uint16(3)],
                }],
            ],
            profile_descriptors: prof_descs.clone(),
            information: vec![Information {
                language: language.clone(),
                name: Some(name.clone()),
                description: Some(description.clone()),
                provider: Some(provider.clone()),
            }],
            additional_attributes: vec![Attribute {
                id: attribute_id,
                element: DataElement::Sequence(vec![Box::new(DataElement::Uint32(
                    attribute_value,
                ))]),
            }],
        };

        let fidl = fidl_bredr::ServiceDefinition {
            service_class_uuids: Some(vec![uuid]),
            protocol_descriptor_list: Some(vec![fidl_bredr::ProtocolDescriptor {
                protocol: fidl_bredr::ProtocolIdentifier::L2Cap,
                params: vec![fidl_bredr::DataElement::Uint16(10)],
            }]),
            additional_protocol_descriptor_lists: Some(vec![
                vec![fidl_bredr::ProtocolDescriptor {
                    protocol: fidl_bredr::ProtocolIdentifier::L2Cap,
                    params: vec![fidl_bredr::DataElement::Uint16(12)],
                }],
                vec![fidl_bredr::ProtocolDescriptor {
                    protocol: fidl_bredr::ProtocolIdentifier::Avdtp,
                    params: vec![fidl_bredr::DataElement::Uint16(3)],
                }],
            ]),
            profile_descriptors: Some(prof_descs.clone()),
            information: Some(vec![fidl_bredr::Information {
                language: Some(language.clone()),
                name: Some(name.clone()),
                description: Some(description.clone()),
                provider: Some(provider.clone()),
                ..Default::default()
            }]),
            additional_attributes: Some(vec![fidl_bredr::Attribute {
                id: attribute_id,
                element: fidl_bredr::DataElement::Sequence(vec![Some(Box::new(
                    fidl_bredr::DataElement::Uint32(attribute_value),
                ))]),
            }]),
            ..Default::default()
        };

        // Converting from local ServiceDefinition to the FIDL ServiceDefinition should work.
        let local_to_fidl: fidl_bredr::ServiceDefinition =
            fidl_bredr::ServiceDefinition::try_from(&local).expect("should work");
        assert_eq!(local_to_fidl, fidl);

        // Converting from FIDL ServiceDefinition to the local ServiceDefinition should work.
        let fidl_to_local: ServiceDefinition =
            ServiceDefinition::try_from(&fidl).expect("should work");
        assert_eq!(fidl_to_local, local);
    }

    #[test]
    fn test_invalid_service_definition_fails_gracefully() {
        let no_uuids_fidl = fidl_bredr::ServiceDefinition::default();
        let fidl_to_local = ServiceDefinition::try_from(&no_uuids_fidl);
        assert!(fidl_to_local.is_err());

        let empty_uuids_fidl = fidl_bredr::ServiceDefinition {
            service_class_uuids: Some(vec![]),
            ..Default::default()
        };
        let fidl_to_local = ServiceDefinition::try_from(&empty_uuids_fidl);
        assert!(fidl_to_local.is_err());
    }

    #[test]
    fn test_channel_parameters_conversions() {
        let channel_mode = Some(fidl_bredr::ChannelMode::EnhancedRetransmission);
        let max_rx_sdu_size = Some(MIN_RX_SDU_SIZE);

        let local =
            ChannelParameters { channel_mode, max_rx_sdu_size, security_requirements: None };
        let fidl =
            fidl_bredr::ChannelParameters { channel_mode, max_rx_sdu_size, ..Default::default() };

        let local_to_fidl =
            fidl_bredr::ChannelParameters::try_from(&local).expect("conversion should work");
        assert_eq!(local_to_fidl, fidl);

        let fidl_to_local = ChannelParameters::try_from(&fidl).expect("conversion should work");
        assert_eq!(fidl_to_local, local);

        // Empty FIDL parameters is OK.
        let fidl = fidl_bredr::ChannelParameters::default();
        let expected = ChannelParameters {
            channel_mode: None,
            max_rx_sdu_size: None,
            security_requirements: None,
        };

        let fidl_to_local = ChannelParameters::try_from(&fidl).expect("conversion should work");
        assert_eq!(fidl_to_local, expected);
    }

    #[test]
    fn test_invalid_channel_parameters_fails_gracefully() {
        let too_small_sdu = Some(MIN_RX_SDU_SIZE - 1);
        let local = ChannelParameters {
            channel_mode: None,
            max_rx_sdu_size: too_small_sdu,
            security_requirements: None,
        };
        let fidl =
            fidl_bredr::ChannelParameters { max_rx_sdu_size: too_small_sdu, ..Default::default() };

        let local_to_fidl = fidl_bredr::ChannelParameters::try_from(&local);
        assert!(local_to_fidl.is_err());

        let fidl_to_local = ChannelParameters::try_from(&fidl);
        assert!(fidl_to_local.is_err());
    }

    #[test]
    fn test_security_requirements_conversions() {
        let authentication_required = Some(false);
        let secure_connections_required = Some(true);

        let local = SecurityRequirements { authentication_required, secure_connections_required };
        let fidl = fidl_bredr::SecurityRequirements {
            authentication_required,
            secure_connections_required,
            ..Default::default()
        };

        let local_to_fidl = fidl_bredr::SecurityRequirements::from(&local);
        assert_eq!(local_to_fidl, fidl);

        let fidl_to_local = SecurityRequirements::from(&fidl);
        assert_eq!(fidl_to_local, local);
    }

    #[test]
    fn test_combine_security_requirements() {
        let req1 = SecurityRequirements {
            authentication_required: None,
            secure_connections_required: None,
        };
        let req2 = SecurityRequirements {
            authentication_required: None,
            secure_connections_required: None,
        };
        let expected = SecurityRequirements {
            authentication_required: None,
            secure_connections_required: None,
        };
        assert_eq!(combine_security_requirements(&req1, &req2), expected);

        let req1 = SecurityRequirements {
            authentication_required: Some(true),
            secure_connections_required: None,
        };
        let req2 = SecurityRequirements {
            authentication_required: None,
            secure_connections_required: Some(true),
        };
        let expected = SecurityRequirements {
            authentication_required: Some(true),
            secure_connections_required: Some(true),
        };
        assert_eq!(combine_security_requirements(&req1, &req2), expected);

        let req1 = SecurityRequirements {
            authentication_required: Some(false),
            secure_connections_required: Some(true),
        };
        let req2 = SecurityRequirements {
            authentication_required: None,
            secure_connections_required: Some(true),
        };
        let expected = SecurityRequirements {
            authentication_required: Some(false),
            secure_connections_required: Some(true),
        };
        assert_eq!(combine_security_requirements(&req1, &req2), expected);

        let req1 = SecurityRequirements {
            authentication_required: Some(true),
            secure_connections_required: Some(false),
        };
        let req2 = SecurityRequirements {
            authentication_required: Some(false),
            secure_connections_required: Some(true),
        };
        let expected = SecurityRequirements {
            authentication_required: Some(true),
            secure_connections_required: Some(true),
        };
        assert_eq!(combine_security_requirements(&req1, &req2), expected);
    }

    #[test]
    fn test_combine_channel_parameters() {
        let p1 = ChannelParameters::default();
        let p2 = ChannelParameters::default();
        let expected = ChannelParameters::default();
        assert_eq!(combine_channel_parameters(&p1, &p2), expected);

        let p1 = ChannelParameters {
            channel_mode: Some(fidl_bredr::ChannelMode::EnhancedRetransmission),
            max_rx_sdu_size: None,
            security_requirements: None,
        };
        let p2 = ChannelParameters {
            channel_mode: Some(fidl_bredr::ChannelMode::Basic),
            max_rx_sdu_size: Some(70),
            security_requirements: None,
        };
        let expected = ChannelParameters {
            channel_mode: Some(fidl_bredr::ChannelMode::Basic),
            max_rx_sdu_size: Some(70),
            security_requirements: None,
        };
        assert_eq!(combine_channel_parameters(&p1, &p2), expected);

        let empty_seq_reqs = SecurityRequirements::default();
        let p1 = ChannelParameters {
            channel_mode: None,
            max_rx_sdu_size: Some(75),
            security_requirements: Some(empty_seq_reqs.clone()),
        };
        let p2 = ChannelParameters {
            channel_mode: Some(fidl_bredr::ChannelMode::EnhancedRetransmission),
            max_rx_sdu_size: None,
            security_requirements: None,
        };
        let expected = ChannelParameters {
            channel_mode: Some(fidl_bredr::ChannelMode::EnhancedRetransmission),
            max_rx_sdu_size: Some(75),
            security_requirements: Some(empty_seq_reqs),
        };
        assert_eq!(combine_channel_parameters(&p1, &p2), expected);

        let reqs1 = SecurityRequirements {
            authentication_required: Some(true),
            secure_connections_required: None,
        };
        let reqs2 = SecurityRequirements {
            authentication_required: Some(false),
            secure_connections_required: Some(false),
        };
        let combined_reqs = combine_security_requirements(&reqs1, &reqs2);
        let p1 = ChannelParameters {
            channel_mode: None,
            max_rx_sdu_size: Some(90),
            security_requirements: Some(reqs1),
        };
        let p2 = ChannelParameters {
            channel_mode: Some(fidl_bredr::ChannelMode::Basic),
            max_rx_sdu_size: Some(70),
            security_requirements: Some(reqs2),
        };
        let expected = ChannelParameters {
            channel_mode: Some(fidl_bredr::ChannelMode::Basic),
            max_rx_sdu_size: Some(70),
            security_requirements: Some(combined_reqs),
        };
        assert_eq!(combine_channel_parameters(&p1, &p2), expected);
    }

    #[test]
    fn local_sco_parameters_inspect_tree() {
        let inspect = inspect::Inspector::default();
        assert_data_tree!(inspect, root: {});

        let params = fidl_bredr::ScoConnectionParameters {
            parameter_set: Some(fidl_bredr::HfpParameterSet::D1),
            air_coding_format: Some(fidl_bredr::CodingFormat::Cvsd),
            air_frame_size: Some(60),
            io_bandwidth: Some(16000),
            io_coding_format: Some(fidl_bredr::CodingFormat::LinearPcm),
            io_frame_size: Some(16),
            io_pcm_data_format: Some(fidl_fuchsia_hardware_audio::SampleFormat::PcmSigned),
            io_pcm_sample_payload_msb_position: Some(1),
            path: Some(fidl_bredr::DataPath::Offload),
            ..Default::default()
        };

        let mut local: ValidScoConnectionParameters = params.try_into().expect("can convert");
        assert_data_tree!(inspect, root: {});

        let _ = local.iattach(&inspect.root(), "state").expect("can attach inspect");
        assert_data_tree!(inspect, root: {
            state: {
                parameter_set: "D1",
                air_coding_format: "Cvsd",
                air_frame_size: 60u64,
                io_bandwidth: 16000u64,
                io_coding_format: "LinearPcm",
                io_frame_size: 16u64,
                io_pcm_data_format: "PcmSigned",
                io_pcm_sample_payload_msb_position: 1u64,
                path: "Offload",
            }
        });
    }

    #[test]
    fn data_element_primitve_conversions() {
        type Result<T> = std::result::Result<T, DataElementConversionError>;

        let rust_u8 = 8u8;
        let data_element_uint8 = DataElement::Uint8(8u8);
        let data_element_uint8_into: DataElement = rust_u8.into();
        let rust_u8_ok: Result<u8> = data_element_uint8.clone().try_into();
        let rust_u8_err: Result<u16> = data_element_uint8.clone().try_into();
        assert_eq!(data_element_uint8_into, data_element_uint8);
        assert_eq!(rust_u8_ok, Ok(rust_u8));
        assert_eq!(
            rust_u8_err,
            Err(DataElementConversionError { data_element: data_element_uint8 })
        );

        let rust_i8 = 9i8;
        let data_element_int8 = DataElement::Int8(9i8);
        let data_element_int8_into: DataElement = rust_i8.into();
        let rust_i8_ok: Result<i8> = data_element_int8.clone().try_into();
        let rust_i8_err: Result<u16> = data_element_int8.clone().try_into();
        assert_eq!(data_element_int8_into, data_element_int8);
        assert_eq!(rust_i8_ok, Ok(rust_i8));
        assert_eq!(
            rust_i8_err,
            Err(DataElementConversionError { data_element: data_element_int8 })
        );

        let rust_u16 = 16u16;
        let data_element_uint16 = DataElement::Uint16(16u16);
        let data_element_uint16_into: DataElement = rust_u16.into();
        let rust_u16_ok: Result<u16> = data_element_uint16.clone().try_into();
        let rust_u16_err: Result<i16> = data_element_uint16.clone().try_into();
        assert_eq!(data_element_uint16_into, data_element_uint16);
        assert_eq!(rust_u16_ok, Ok(rust_u16));
        assert_eq!(
            rust_u16_err,
            Err(DataElementConversionError { data_element: data_element_uint16 })
        );

        let rust_i16 = 17i16;
        let data_element_int16 = DataElement::Int16(17i16);
        let data_element_int16_into: DataElement = rust_i16.into();
        let rust_i16_ok: Result<i16> = data_element_int16.clone().try_into();
        let rust_i16_err: Result<u16> = data_element_int16.clone().try_into();
        assert_eq!(data_element_int16_into, data_element_int16);
        assert_eq!(rust_i16_ok, Ok(rust_i16));
        assert_eq!(
            rust_i16_err,
            Err(DataElementConversionError { data_element: data_element_int16 })
        );

        let rust_u32 = 32u32;
        let data_element_uint32 = DataElement::Uint32(32u32);
        let data_element_uint32_into: DataElement = rust_u32.into();
        let rust_u32_ok: Result<u32> = data_element_uint32.clone().try_into();
        let rust_u32_err: Result<u16> = data_element_uint32.clone().try_into();
        assert_eq!(data_element_uint32_into, data_element_uint32);
        assert_eq!(rust_u32_ok, Ok(rust_u32));
        assert_eq!(
            rust_u32_err,
            Err(DataElementConversionError { data_element: data_element_uint32 })
        );

        let rust_i32 = 33i32;
        let data_element_int32 = DataElement::Int32(33i32);
        let data_element_int32_into: DataElement = rust_i32.into();
        let rust_i32_ok: Result<i32> = data_element_int32.clone().try_into();
        let rust_i32_err: Result<u16> = data_element_int32.clone().try_into();
        assert_eq!(data_element_int32_into, data_element_int32);
        assert_eq!(rust_i32_ok, Ok(rust_i32));
        assert_eq!(
            rust_i32_err,
            Err(DataElementConversionError { data_element: data_element_int32 })
        );

        let rust_u64 = 64u64;
        let data_element_uint64 = DataElement::Uint64(64u64);
        let data_element_uint64_into: DataElement = rust_u64.into();
        let rust_u64_ok: Result<u64> = data_element_uint64.clone().try_into();
        let rust_u64_err: Result<u16> = data_element_uint64.clone().try_into();
        assert_eq!(data_element_uint64_into, data_element_uint64);
        assert_eq!(rust_u64_ok, Ok(rust_u64));
        assert_eq!(
            rust_u64_err,
            Err(DataElementConversionError { data_element: data_element_uint64 })
        );

        let rust_i64 = 65i64;
        let data_element_int64 = DataElement::Int64(65i64);
        let data_element_int64_into: DataElement = rust_i64.into();
        let rust_i64_ok: Result<i64> = data_element_int64.clone().try_into();
        let rust_i64_err: Result<u16> = data_element_int64.clone().try_into();
        assert_eq!(data_element_int64_into, data_element_int64);
        assert_eq!(rust_i64_ok, Ok(rust_i64));
        assert_eq!(
            rust_i64_err,
            Err(DataElementConversionError { data_element: data_element_int64 })
        );

        let rust_vec = "ABC".as_bytes().to_vec();
        let data_element_str = DataElement::Str("ABC".as_bytes().to_vec());
        let data_element_str_into: DataElement = rust_vec.clone().into();
        let rust_vec_ok: Result<Vec<u8>> = data_element_str.clone().try_into();
        let rust_vec_err: Result<u16> = data_element_str.clone().try_into();
        assert_eq!(data_element_str_into, data_element_str);
        assert_eq!(rust_vec_ok, Ok(rust_vec));
        assert_eq!(
            rust_vec_err,
            Err(DataElementConversionError { data_element: data_element_str })
        );

        let rust_uuid: fidl_bt::Uuid = Uuid::new16(0x1101).into();
        let data_element_uuid = DataElement::Uuid(Uuid::new16(0x1101).into());
        let data_element_uuid_into: DataElement = rust_uuid.clone().into();
        let rust_uuid_ok: Result<fidl_bt::Uuid> = data_element_uuid.clone().try_into();
        let rust_uuid_err: Result<u16> = data_element_uuid.clone().try_into();
        assert_eq!(data_element_uuid_into, data_element_uuid);
        assert_eq!(rust_uuid_ok, Ok(rust_uuid));
        assert_eq!(
            rust_uuid_err,
            Err(DataElementConversionError { data_element: data_element_uuid })
        );

        let rust_string = String::from("ABC");
        let data_element_url = DataElement::Url(String::from("ABC"));
        let data_element_url_into: DataElement = rust_string.clone().into();
        let rust_string_ok: Result<String> = data_element_url.clone().try_into();
        let rust_string_err: Result<u16> = data_element_url.clone().try_into();
        assert_eq!(data_element_url_into, data_element_url);
        assert_eq!(rust_string_ok, Ok(rust_string));
        assert_eq!(
            rust_string_err,
            Err(DataElementConversionError { data_element: data_element_url })
        );

        let rust_bool = true;
        let data_element_bool = DataElement::Bool(true);
        let data_element_bool_into: DataElement = rust_bool.into();
        let rust_bool_ok: Result<bool> = data_element_bool.clone().try_into();
        let rust_bool_err: Result<u16> = data_element_bool.clone().try_into();
        assert_eq!(data_element_bool_into, data_element_bool);
        assert_eq!(rust_bool_ok, Ok(rust_bool));
        assert_eq!(
            rust_bool_err,
            Err(DataElementConversionError { data_element: data_element_bool })
        );
    }
}