android-auto 0.3.8

A crate for implementing the android auto protocol.
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
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
//! This crate provides android auto functionality for devices wishing to comunicate using the android auto protocol.

#![deny(missing_docs)]
#![deny(clippy::missing_docs_in_private_items)]

use std::{
    collections::HashSet,
    io::{Cursor, Read, Write},
    sync::Arc,
};

mod cert;
mod ssl;
use ssl::*;

#[cfg(not(any(feature = "wireless", feature = "usb")))]
compile_error!("One of wireless or usb features must be enabled, both is also ok");

use ::protobuf::Message;
use Wifi::ChannelDescriptor;
#[cfg(feature = "wireless")]
use bluetooth_rust::{
    BluetoothRfcommConnectableAsyncTrait, BluetoothRfcommProfileAsyncTrait, BluetoothStream,
};
use futures::StreamExt;
use rustls::pki_types::{CertificateDer, pem::PemObject};
use tokio::{
    io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
    sync::RwLockReadGuard,
};

mod avinput;
use avinput::*;
mod bluetooth;
use bluetooth::*;
mod common;
use common::*;
mod control;
use control::*;
mod input;
use input::*;
mod mediaaudio;
use mediaaudio::*;
mod mediastatus;
use mediastatus::*;
mod navigation;
use navigation::*;
mod sensor;
use sensor::*;
mod speechaudio;
use speechaudio::*;
mod sysaudio;
use sysaudio::*;
mod video;
use video::*;

#[cfg(feature = "usb")]
mod usb;

pub use protobuf;

/// Used to implement a future that never returns
pub struct Never<T>(std::marker::PhantomData<T>);

impl<T> Never<T> {
    /// Construct a new Self
    pub fn new() -> Self {
        Never(std::marker::PhantomData)
    }
}

impl<T> std::future::Future for Never<T> {
    type Output = T;

    fn poll(
        self: std::pin::Pin<&mut Self>,
        _cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        std::task::Poll::Pending
    }
}

/// Errors that can occur when trying to receive frames
#[derive(Debug)]
pub enum FrameReceiptError {
    /// A timeout occurred when trying to receive the frame header
    TimeoutHeader,
    /// The connection was disconnected
    Disconnected,
    /// An unexpected error receiving the frame channel id
    UnexpectedDuringFrameChannel(std::io::Error),
    /// An unexpected error receiving the frame header
    UnexpectedDuringFrameHeader(std::io::Error),
    /// An unexpected error receiving the frame length
    UnexpectedDuringFrameLength(std::io::Error),
    /// An unexpected error receiving the frame contents
    UnexpectedDuringFrameContents(std::io::Error),
    /// An error occurred calling read_tls with the received frame payload
    TlsReadError(std::io::Error),
    /// An error occurred processing tls data received
    TlsProcessingError(rustls::Error),
}

/// An error that can occur when transmitting a frame
#[derive(Debug)]
pub enum FrameTransmissionError {
    /// A timeout occurred while transmitting
    Timeout,
    /// The connection was disconnected
    Disconnected,
    /// An unexpected error
    Unexpected(std::io::Error),
    /// An ssl specific error
    SslError(SslError),
}

impl From<SslError> for FrameTransmissionError {
    fn from(value: SslError) -> Self {
        Self::SslError(value)
    }
}

/// A sequence error in frames received
#[derive(Debug)]
pub enum FrameSequenceError {
    /// Video data was received with the video channel not being open
    VideoChannelNotOpen,
}

/// Errors that can occur when either sending or receiving frames
#[derive(Debug)]
pub enum FrameIoError {
    /// An error receiving a frame
    Rx(FrameReceiptError),
    /// An error sending a frame
    Tx(FrameTransmissionError),
    /// A shutdown was requested
    ShutdownRequested,
    /// The client has an incompatible version
    IncompatibleVersion(u16, u16),
    /// An error occurred during the ssl handshake
    SslHandshake(String),
    /// A logical error due to frames not being received in the expected order
    Sequence(FrameSequenceError),
    /// An error occurred opening the audio input channel
    AudioInputOpenError,
    /// An error occurred closing the audio input channel
    AudioInputCloseError,
}

/// Errors that can occur during communication with a client
#[derive(Debug)]
pub enum ClientError {
    /// The root certificate for the ssl communications was invalid
    InvalidRootCert,
    /// The client certificate was invalid
    InvalidClientCertificate,
    /// The client private key was invalid
    InvalidClientPrivateKey,
    /// A communication error
    IoError(FrameIoError),
    /// An ssl error
    SslError(tokio::sync::mpsc::error::SendError<ssl::SslThreadData>),
}

impl From<tokio::sync::mpsc::error::SendError<ssl::SslThreadData>> for ClientError {
    fn from(value: tokio::sync::mpsc::error::SendError<ssl::SslThreadData>) -> Self {
        Self::SslError(value)
    }
}

impl From<tokio::sync::mpsc::error::SendError<ssl::SslThreadData>> for FrameIoError {
    fn from(value: tokio::sync::mpsc::error::SendError<ssl::SslThreadData>) -> Self {
        Self::SslHandshake(value.to_string())
    }
}

impl From<FrameTransmissionError> for FrameIoError {
    fn from(value: FrameTransmissionError) -> Self {
        Self::Tx(value)
    }
}

impl From<String> for FrameIoError {
    fn from(value: String) -> Self {
        FrameIoError::SslHandshake(value)
    }
}

impl From<FrameSequenceError> for FrameIoError {
    fn from(value: FrameSequenceError) -> Self {
        FrameIoError::Sequence(value)
    }
}

impl From<FrameIoError> for ClientError {
    fn from(value: FrameIoError) -> Self {
        ClientError::IoError(value)
    }
}

/// The list of channel handlers for the current android auto instance
static CHANNEL_HANDLERS: tokio::sync::RwLock<Vec<ChannelHandler>> =
    tokio::sync::RwLock::const_new(Vec::new());

/// The types of connections that can exist, exists to make it possible for the usb and wireless features to work with tokio::select macro
pub enum ConnectionType {
    /// The variant for usb connections
    #[cfg(feature = "usb")]
    Usb(usb::AndroidAutoUsb),
    /// The variant for wireless connections
    #[cfg(feature = "wireless")]
    Wireless(tokio::net::TcpStream),
}

impl ConnectionType {
    /// Run the connection
    async fn run<T: AndroidAutoMainTrait + ?Sized>(
        self,
        config: AndroidAutoConfiguration,
        main: &Box<T>,
    ) {
        match self {
            #[cfg(feature = "usb")]
            ConnectionType::Usb(a) => {
                let stream = a.into_split();
                let _ = handle_client_generic(stream.0, stream.1, config, main).await;
            }
            #[cfg(feature = "wireless")]
            ConnectionType::Wireless(w) => {
                let stream = w.into_split();
                let a = handle_client_generic(stream.0, stream.1, config, main).await;
                log::error!("The error for wifi is {:?}", a);
            }
        }
    }
}

/// The base trait for crate users to implement
#[async_trait::async_trait]
pub trait AndroidAutoMainTrait:
    AndroidAutoSensorTrait
    + AndroidAutoAudioOutputTrait
    + AndroidAutoInputChannelTrait
    + AndroidAutoAudioInputTrait
    + AndroidAutoVideoChannelTrait
    + Send
    + Sync
{
    /// Implement this to indicate that bluetooth hardware is possible, return None if bluetooth hardware is not present
    #[inline(always)]
    fn supports_bluetooth(&self) -> Option<&dyn AndroidAutoBluetoothTrait> {
        None
    }

    #[cfg(feature = "wireless")]
    /// Implement this to support wireless android auto communications
    #[inline(always)]
    fn supports_wireless(&self) -> Option<Arc<dyn AndroidAutoWirelessTrait>> {
        None
    }

    /// Implement this to support wired android auto communications
    #[inline(always)]
    fn supports_wired(&self) -> Option<Arc<dyn AndroidAutoWiredTrait>> {
        None
    }

    /// Implement this to support navigation
    fn supports_navigation(&self) -> Option<&dyn AndroidAutoNavigationTrait> {
        None
    }

    /// A method of receiving the ping times for the head unit
    async fn ping_time_microseconds(&self, micros: i64) {
        log::info!("Ping response is {} microseconds", micros);
    }

    /// The android auto device just connected
    async fn connect(&self);

    /// The android auto device disconnected
    async fn disconnect(&self);

    /// Retrieve the receiver so that the user can send messages to the android auto compatible device or crate
    async fn get_receiver(&self)
    -> Option<tokio::sync::mpsc::Receiver<SendableAndroidAutoMessage>>;

    #[cfg(feature = "usb")]
    /// Run a single usb device for android auto
    async fn do_usb_iteration(
        &self,
        d: nusb::DeviceInfo,
        config: &AndroidAutoConfiguration,
        setup: &AndroidAutoSetup,
    ) -> Result<ConnectionType, ()> {
        let main = self;
        match d.open().await {
            Ok(d) => {
                let aoa = usb::get_aoa_protocol(&d).await;
                log::info!("AOA is {:?}", aoa);
                usb::identify_accessory(&d).await;
                usb::accessory_start(&d).await;
            }
            Err(e) => {
                log::error!("Failed to open android device {e}");
                return Err(());
            }
        }
        match tokio::time::timeout(
            std::time::Duration::from_millis(2000),
            usb::wait_for_accessory(),
        )
        .await
        {
            Ok(Ok(newdev)) => {
                let _ = newdev.reset().await;
            }
            Ok(Err(e)) => {
                log::error!("Failed to get accessory {e}");
                return Err(());
            }
            Err(_e) => {
                log::error!("Timeout get accessory");
                return Err(());
            }
        }
        match tokio::time::timeout(
            std::time::Duration::from_millis(2000),
            usb::wait_for_accessory(),
        )
        .await
        {
            Ok(Ok(newdev)) => {
                log::info!("AOA DEV IS {:?}", newdev);
                let aoa = usb::claim_aoa_interface(&newdev).await;
                let aauto = usb::AndroidAutoUsb::new(aoa);
                if let Some(aauto) = aauto {
                    log::info!("got aoa interface?");
                    return Ok(ConnectionType::Usb(aauto));
                } else {
                    Err(())
                }
            }
            Ok(Err(e)) => {
                log::error!("Failed to get accessory 2 {e}");
                Err(())
            }
            Err(_e) => {
                log::error!("Timeout get accessory 2");
                return Err(());
            }
        }
    }

    /// Does a usb run
    async fn usb_run(
        &self,
        config: &AndroidAutoConfiguration,
        setup: &AndroidAutoSetup,
    ) -> (ConnectionType, AsyncFn, AsyncFn) {
        #[cfg(feature = "usb")]
        {
            if self.supports_wired().is_some() {
                if let Ok(mut watcher) = nusb::watch_devices() {
                    use futures::StreamExt;
                    log::info!("Looking for usb devices");
                    let looper = async |watcher: &mut nusb::hotplug::HotplugWatch| {
                        loop {
                            if let Some(dev) = watcher.next().await {
                                use nusb::hotplug::HotplugEvent;
                                if let HotplugEvent::Connected(di) = dev {
                                    if usb::is_android_device(&di) {
                                        log::info!("Hotplug device {:?}", di);
                                        tokio::time::sleep(std::time::Duration::from_millis(500))
                                            .await;
                                        break di;
                                    }
                                }
                            }
                        }
                    };
                    if let Ok(devs) = nusb::list_devices().await {
                        let mut start_device = None;
                        for dev in devs {
                            if usb::is_android_device(&dev) {
                                start_device = Some(dev);
                            }
                        }
                        let d = if let Some(d) = start_device {
                            log::info!("Startup device {:?}", d);
                            d
                        } else {
                            looper(&mut watcher).await
                        };
                        let a = self.do_usb_iteration(d.clone(), config, setup).await;
                        if let Ok(a) = a {
                            let disconnect = make_disconnect_watcher(d);
                            let kill: AsyncFn = Box::new(move || Box::pin(async move {}));
                            return (a, disconnect, kill);
                        }
                        loop {
                            let b = looper(&mut watcher).await;
                            let a = self.do_usb_iteration(b.clone(), config, setup).await;
                            if let Ok(a) = a {
                                let disconnect = make_disconnect_watcher(b);
                                let kill: AsyncFn = Box::new(move || Box::pin(async move {}));
                                return (a, disconnect, kill);
                            }
                        }
                    } else {
                        Never::new().await
                    }
                } else {
                    Never::new().await
                }
            } else {
                Never::new().await
            }
        }
        #[cfg(not(feature = "usb"))]
        {
            Never::new().await
        }
    }

    /// does a wifi run
    async fn wifi_run(
        &self,
        config: &AndroidAutoConfiguration,
        setup: &AndroidAutoSetup,
    ) -> (ConnectionType, AsyncFn, AsyncFn) {
        #[cfg(feature = "wireless")]
        {
            if let Some(wireless) = self.supports_wireless() {
                let psettings = bluetooth_rust::BluetoothRfcommProfileSettings {
                    uuid: bluetooth_rust::BluetoothUuid::AndroidAuto
                        .as_str()
                        .to_string(),
                    name: Some("Android Auto Bluetooth Service".to_string()),
                    service_uuid: Some(
                        bluetooth_rust::BluetoothUuid::AndroidAuto
                            .as_str()
                            .to_string(),
                    ),
                    channel: Some(22),
                    psm: None,
                    authenticate: Some(true),
                    authorize: Some(true),
                    auto_connect: Some(true),
                    sdp_record: None,
                    sdp_version: None,
                    sdp_features: None,
                };

                if let Ok(profile) = wireless.setup_bluetooth_profile(&psettings).await {
                    log::info!("Setup bluetooth profile is ok?");
                    let wireless2 = wireless.clone();
                    let kill = tokio::sync::oneshot::channel::<()>();
                    tokio::spawn(async move {
                        tokio::select! {
                            e = bluetooth_service(profile, wireless2) => {
                                log::error!("Android auto bluetooth service stopped: {:?}", e);
                                e
                            }
                            _ = kill.1 => {
                                log::error!("Kill bluetooth service");
                                Ok(())
                            }
                        }
                    });
                    loop {
                        let e = wifi_service(wireless.clone()).await;
                        if let Ok(e) = e {
                            let disconnect: AsyncFn =
                                Box::new(move || Box::pin(async move { Never::new().await }));
                            let kill2: AsyncFn = Box::new(move || {
                                Box::pin(async move {
                                    kill.0.send(());
                                })
                            });
                            return (e, disconnect, kill2);
                        }
                    }
                } else {
                    Never::new().await
                }
            } else {
                Never::new().await
            }
        }
        #[cfg(not(feature = "wireless"))]
        {
            Never::new().await
        }
    }

    /// Runs the android auto server
    async fn run(
        self: Box<Self>,
        config: AndroidAutoConfiguration,
        js: &mut tokio::task::JoinSet<Result<(), String>>,
        setup: &AndroidAutoSetup,
    ) -> Result<(), String> {
        log::info!("Running android auto server");

        let (d, abort, kill) = tokio::select! {
            a = self.usb_run(&config, setup) => {
                log::error!("usb config finished");
                a
            }
            b = self.wifi_run(&config, setup) => {
                log::error!("wifi config finished");
                b
            }
        };

        self.connect().await;
        tokio::select! {
            a = d.run(config, &self) => {
                log::error!("Android auto finished {:?}", a);
            }
            b = abort() => {
                log::error!("Android auto aborted {:?}", b);
            }
        }
        kill().await;
        self.disconnect().await;

        Ok(())
    }
}

/// this trait is implemented by users that support wired (usb) android auto
#[async_trait::async_trait]
pub trait AndroidAutoWiredTrait: AndroidAutoMainTrait {}

/// this trait is implemented by users that support bluetooth and wifi (both are required for wireless android auto)
#[cfg(feature = "wireless")]
#[async_trait::async_trait]
pub trait AndroidAutoWirelessTrait: AndroidAutoMainTrait {
    /// The function to setup the android auto profile
    async fn setup_bluetooth_profile(
        &self,
        suggestions: &bluetooth_rust::BluetoothRfcommProfileSettings,
    ) -> Result<bluetooth_rust::BluetoothRfcommProfileAsync, String>;

    /// Returns wifi details
    fn get_wifi_details(&self) -> NetworkInformation;
}

/// This trait is implemented by users that support navigation indicators
#[async_trait::async_trait]
pub trait AndroidAutoSensorTrait {
    /// Returns the types of sensors supported
    fn get_supported_sensors(&self) -> &SensorInformation;
    /// Start the indicated sensor
    async fn start_sensor(&self, stype: Wifi::sensor_type::Enum) -> Result<(), ()>;
}

/// This trait is implemented by users that support navigation indicators
#[async_trait::async_trait]
pub trait AndroidAutoNavigationTrait: AndroidAutoMainTrait {
    /// A turn indication update
    async fn turn_indication(&self, m: Wifi::NavigationTurnEvent);
    /// A distance indication update
    async fn distance_indication(&self, m: Wifi::NavigationDistanceEvent);
    /// A status update
    async fn nagivation_status(&self, m: Wifi::NavigationStatus);
}

/// This trait is implemented by users wishing to display a video stream from an android auto (phone probably).
#[async_trait::async_trait]
pub trait AndroidAutoVideoChannelTrait {
    /// Parse a chunk of h264 video data
    async fn receive_video(&self, data: Vec<u8>, timestamp: Option<u64>);
    /// Setup the video device to receive h264 video, if anything is required. Return Ok(()) if setup was good, Err(()) if it was not good
    async fn setup_video(&self) -> Result<(), ()>;
    /// Tear down the video receiver, may be called without the setup having been called
    async fn teardown_video(&self);
    /// Wait for the video to be in focus
    async fn wait_for_focus(&self);
    /// Set the focus of the video stream to be as requested
    async fn set_focus(&self, focus: bool);
    /// Retrieve the video configuration for the channel
    fn retrieve_video_configuration(&self) -> &VideoConfiguration;
}

/// The types of audio channels that can exist
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub enum AudioChannelType {
    /// Media audio
    Media,
    /// System audio
    System,
    /// Speech audio
    Speech,
}

/// This trait is implemented by users that have audio output capabilities
#[async_trait::async_trait]
pub trait AndroidAutoAudioOutputTrait {
    /// Opens the specified channel
    async fn open_output_channel(&self, t: AudioChannelType) -> Result<(), ()>;
    /// Closes the specified channel
    async fn close_output_channel(&self, t: AudioChannelType) -> Result<(), ()>;
    /// Receive a chunk of audio data for the specified channel
    async fn receive_output_audio(&self, t: AudioChannelType, data: Vec<u8>);
    /// The specified audio channel will start
    async fn start_output_audio(&self, t: AudioChannelType);
    /// The specified audio channel will stop
    async fn stop_output_audio(&self, t: AudioChannelType);
}

/// This trait is implemented by users that have audio input capabilities
#[async_trait::async_trait]
pub trait AndroidAutoAudioInputTrait {
    /// Opens the channel
    async fn open_input_channel(&self) -> Result<(), ()>;
    /// Closes the channel
    async fn close_input_channel(&self) -> Result<(), ()>;
    /// The audio channel will start
    async fn start_input_audio(&self);
    /// The audio channel will stop
    async fn stop_input_audio(&self);
    /// The ack for the audio data
    async fn audio_input_ack(&self, chan: u8, ack: AVMediaAckIndication);
}

/// The configuration for an input channel
#[derive(Clone)]
pub struct InputConfiguration {
    /// The supported keycodes
    pub keycodes: Vec<u32>,
    /// The touchscreen width and height
    pub touchscreen: Option<(u16, u16)>,
}

/// This trait is implemented by users that have inputs for their head unit
#[async_trait::async_trait]
pub trait AndroidAutoInputChannelTrait {
    /// A binding request for the specified keycode, generally the same code reported in `AndroidAutoConfig::keycodes_supported`
    async fn binding_request(&self, code: u32) -> Result<(), ()>;
    /// Retrieve the input configuration
    fn retrieve_input_configuration(&self) -> &InputConfiguration;
}

/// A trait that is implemented for users that somehow support bluetooth for their hardware
#[async_trait::async_trait]
pub trait AndroidAutoBluetoothTrait: AndroidAutoMainTrait {
    /// Do something
    async fn do_stuff(&self);
    /// Get the configuration
    fn get_config(&self) -> &BluetoothInformation;
}

#[allow(missing_docs)]
#[allow(clippy::missing_docs_in_private_items)]
mod protobufmod {
    include!(concat!(env!("OUT_DIR"), "/protobuf/mod.rs"));
}
pub use protobufmod::*;

/// The android auto version supported
const VERSION: (u16, u16) = (1, 1);

/// The types of messages that can be sent over the android auto link
pub enum AndroidAutoMessage {
    /// An input message
    Input(Wifi::InputEventIndication),
    /// An audio packet message, optional timestamp (microseconds since UNIX_EPOCH) and data
    Audio(Option<u64>, Vec<u8>),
    /// A sensor event message
    Sensor(Wifi::SensorEventIndication),
    /// An other message
    Other,
}

/// The type of channel being sent in a sendable message
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum SendableChannelType {
    /// The input channel
    Input,
    /// The audio input channel
    AudioInput,
    /// The sensor channel
    Sensor,
    /// Other channel type
    Other,
}

/// The sendable form of an `AndroidAutoMessage`
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct SendableAndroidAutoMessage {
    /// The channel id to send the message to
    channel: SendableChannelType,
    /// The message body to send
    data: Vec<u8>,
}

impl SendableAndroidAutoMessage {
    /// Convert Self into an `AndroidAutoFrame``
    async fn into_frame(self) -> AndroidAutoFrame {
        let mut chan = None;
        let chans = CHANNEL_HANDLERS.read().await;
        for (i, c) in chans.iter().enumerate() {
            match self.channel {
                SendableChannelType::Sensor => {
                    if let ChannelHandler::Sensor(_) = c {
                        chan = Some(i as u8);
                        break;
                    }
                }
                SendableChannelType::AudioInput => {
                    if let ChannelHandler::AvInput(_) = c {
                        chan = Some(i as u8);
                        break;
                    }
                }
                SendableChannelType::Input => {
                    if let ChannelHandler::Input(_) = c {
                        chan = Some(i as u8);
                        break;
                    }
                }
                SendableChannelType::Other => {
                    todo!();
                }
            }
        }
        AndroidAutoFrame {
            header: FrameHeader {
                channel_id: chan.unwrap(),
                frame: FrameHeaderContents::new(true, FrameHeaderType::Single, false),
            },
            data: self.data,
        }
    }
}

/// A message sent from an app user to this crate
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum AndroidAutoChannelMessageFromApp {
    /// A message that needs to be forwarded to the android auto device
    MessageToPhone(SendableAndroidAutoMessage),
}

impl AndroidAutoMessage {
    /// Convert the message to something that can be sent, if possible
    pub fn sendable(self) -> SendableAndroidAutoMessage {
        match self {
            Self::Sensor(m) => {
                let mut data = m.write_to_bytes().unwrap();
                let t = Wifi::sensor_channel_message::Enum::SENSOR_EVENT_INDICATION as u16;
                let t = t.to_be_bytes();
                let mut m = Vec::new();
                m.push(t[0]);
                m.push(t[1]);
                m.append(&mut data);
                SendableAndroidAutoMessage {
                    channel: SendableChannelType::Sensor,
                    data: m,
                }
            }
            Self::Input(m) => {
                let mut data = m.write_to_bytes().unwrap();
                let t = Wifi::input_channel_message::Enum::INPUT_EVENT_INDICATION as u16;
                let t = t.to_be_bytes();
                let mut m = Vec::new();
                m.push(t[0]);
                m.push(t[1]);
                m.append(&mut data);
                SendableAndroidAutoMessage {
                    channel: SendableChannelType::Input,
                    data: m,
                }
            }
            Self::Audio(_timestamp, mut data) => {
                let t = Wifi::avchannel_message::Enum::AV_MEDIA_WITH_TIMESTAMP_INDICATION as u16;
                let t = t.to_be_bytes();
                let mut m = Vec::new();
                m.push(t[0]);
                m.push(t[1]);
                m.append(&mut data);
                SendableAndroidAutoMessage {
                    channel: SendableChannelType::AudioInput,
                    data: m,
                }
            }
            Self::Other => todo!(),
        }
    }
}

/// A message sent or received in the android auto protocol
#[cfg(feature = "wireless")]
struct AndroidAutoRawBluetoothMessage {
    /// The message type
    t: u16,
    /// The message contained in the message
    message: Vec<u8>,
}

/// The sensor information supported by the user for android auto
#[derive(Clone)]
pub struct SensorInformation {
    /// The sensor types supported
    pub sensors: HashSet<Wifi::sensor_type::Enum>,
}

/// The wireless network information to relay to the compatible android auto device
#[derive(Clone, Debug)]
pub struct NetworkInformation {
    /// The ssid of the wireless network
    pub ssid: String,
    /// The password for the wireless network
    pub psk: String,
    /// Unsure, probably the mac address of the android auto host
    pub mac_addr: String,
    /// The ip address of the android auto host
    pub ip: String,
    /// The port that the android auto host should listen on
    pub port: u16,
    /// The security mode for the wireless network
    pub security_mode: Bluetooth::SecurityMode,
    /// The access point type of the wireless network
    pub ap_type: Bluetooth::AccessPointType,
}

/// Information about the head unit that will be providing android auto services for compatible devices
#[derive(Clone)]
pub struct HeadUnitInfo {
    /// The name of the head unit
    pub name: String,
    /// The model of the vehicle
    pub car_model: String,
    /// The year of the vehicle
    pub car_year: String,
    /// The serial number of the vehicle
    pub car_serial: String,
    /// True when the vehicle is a left hand drive, false when a right hand drive
    pub left_hand: bool,
    /// The manufacturer of the head unit
    pub head_manufacturer: String,
    /// The model of the head unit
    pub head_model: String,
    /// The software build for the head unit
    pub sw_build: String,
    /// The software version for the head unit
    pub sw_version: String,
    /// Does the head unit support native media during vr
    pub native_media: bool,
    /// Should the clock be hidden?
    pub hide_clock: Option<bool>,
}

/// The required bluetooth information
#[derive(Clone)]
pub struct BluetoothInformation {
    /// The mac address of the bluetooth adapter
    pub address: String,
}

/// The configuration data for the video stream of android auto
#[derive(Clone)]
pub struct VideoConfiguration {
    /// Defines the desired resolution for the video stream
    pub resolution: Wifi::video_resolution::Enum,
    /// The fps for the video stream
    pub fps: Wifi::video_fps::Enum,
    /// The dots per inch of the display
    pub dpi: u16,
}

/// Provides basic configuration elements for setting up an android auto head unit
#[derive(Clone)]
pub struct AndroidAutoConfiguration {
    /// The head unit information
    pub unit: HeadUnitInfo,
    /// The android auto client certificate and private key in pem format (only if a custom one is desired)
    pub custom_certificate: Option<(Vec<u8>, Vec<u8>)>,
}

/// The channel identifier for channels in the android auto protocol
type ChannelId = u8;

/// Specifies the type of frame header, whether the data of a packet is contained in a single frame, or if it was too large and broken up into multiple frames for transmission.
#[derive(Debug, PartialEq)]
#[repr(u8)]
pub enum FrameHeaderType {
    /// This frame is neither the first or the last of a multi-frame packet
    Middle = 0,
    /// This is the first frame of a multi-frame packet
    First = 1,
    /// This is the last frame of a multi-frame packet
    Last = 2,
    /// The packet is contained in a single frame
    Single = 3,
}

impl From<u8> for FrameHeaderType {
    fn from(value: u8) -> Self {
        match value & 3 {
            0 => FrameHeaderType::Middle,
            1 => FrameHeaderType::First,
            2 => FrameHeaderType::Last,
            _ => FrameHeaderType::Single,
        }
    }
}

impl From<FrameHeaderType> for u8 {
    fn from(value: FrameHeaderType) -> Self {
        value as u8
    }
}

#[allow(missing_docs)]
/// The frame header module, because bitfield new does not make documentation yet.
mod frame_header {
    bitfield::bitfield! {
        #[derive(Copy, Clone)]
        pub struct FrameHeaderContents(u8);
        impl Debug;
        impl new;
        u8;
        /// True indicates the frame is encrypted
        pub get_encryption, set_encryption: 3;
        /// The frame header type
        pub from into super::FrameHeaderType, get_frame_type, set_frame_type: 1, 0;
        /// True when frame is for control, false when specific
        pub get_control, set_control: 2;
    }
}
use frame_header::FrameHeaderContents;

#[cfg(feature = "wireless")]
use crate::Bluetooth::Status;
use crate::protobufmod::Wifi::AVMediaAckIndication;

/// Represents the header of a frame sent to the android auto client
#[derive(Copy, Clone, Debug)]
struct FrameHeader {
    /// The channelid that this frame is intended for
    channel_id: ChannelId,
    /// The contents of the frame header
    frame: FrameHeaderContents,
}

impl FrameHeader {
    /// Add self to the given buffer to build part of a complete frame
    pub fn add_to(&self, buf: &mut Vec<u8>) {
        buf.push(self.channel_id);
        buf.push(self.frame.0);
    }
}

/// Responsible for receiving frame headers in the the android auto protocol.
struct FrameHeaderReceiver {
    /// The channel id received for a frame header, if one has been received.
    channel_id: Option<ChannelId>,
}

impl FrameHeaderReceiver {
    /// Construct a new self
    pub fn new() -> Self {
        Self { channel_id: None }
    }

    /// Read a frame header from the compatible android auto device
    /// Returns Ok(Some(p)) when a full frame header is actually received.
    pub async fn read<T: AsyncRead + Unpin>(
        &mut self,
        stream: &mut T,
    ) -> Result<Option<FrameHeader>, FrameReceiptError> {
        if self.channel_id.is_none() {
            let mut b = [0u8];
            stream
                .read_exact(&mut b)
                .await
                .map_err(|e| match e.kind() {
                    std::io::ErrorKind::TimedOut => FrameReceiptError::TimeoutHeader,
                    std::io::ErrorKind::UnexpectedEof => FrameReceiptError::Disconnected,
                    _ => FrameReceiptError::UnexpectedDuringFrameChannel(e),
                })?;
            self.channel_id = ChannelId::try_from(b[0]).ok();
        }
        if let Some(channel_id) = &self.channel_id {
            let mut b = [0u8];
            stream
                .read_exact(&mut b)
                .await
                .map_err(|e| match e.kind() {
                    std::io::ErrorKind::TimedOut => FrameReceiptError::TimeoutHeader,
                    std::io::ErrorKind::UnexpectedEof => FrameReceiptError::Disconnected,
                    _ => FrameReceiptError::UnexpectedDuringFrameHeader(e),
                })?;
            let mut a = FrameHeaderContents::new(false, FrameHeaderType::Single, false);
            a.0 = b[0];
            let fh = FrameHeader {
                channel_id: *channel_id,
                frame: a,
            };
            return Ok(Some(fh));
        }
        Ok(None)
    }
}

/// A frame of data for comunication in the android auto. When receiving frames, multi-frames are combined into a single frame.
#[derive(Debug)]
struct AndroidAutoFrame {
    /// The header of the frame
    header: FrameHeader,
    /// The data actually relayed in the frame
    data: Vec<u8>,
}

impl AndroidAutoFrame {
    /// The largest payload for a single frame
    const MAX_FRAME_DATA_SIZE: usize = 0x4000;
    #[allow(dead_code)]
    /// Currently unused function for building a set of frames for a large packet
    fn build_multi_frame(f: FrameHeader, d: Vec<u8>) -> Vec<Self> {
        let mut m = Vec::new();
        if d.len() < Self::MAX_FRAME_DATA_SIZE {
            let fr = AndroidAutoFrame { header: f, data: d };
            m.push(fr);
        } else {
            let packets = d.chunks(Self::MAX_FRAME_DATA_SIZE);
            let max = packets.len();
            for (i, p) in packets.enumerate() {
                let first = i == 0;
                let last = i == (max - 1);
                let mut h = f;
                if first {
                    h.frame.set_frame_type(FrameHeaderType::First);
                } else if last {
                    h.frame.set_frame_type(FrameHeaderType::Last);
                } else {
                    h.frame.set_frame_type(FrameHeaderType::Middle);
                }
                let fr = AndroidAutoFrame {
                    header: h,
                    data: p.to_vec(),
                };
                m.push(fr);
            }
        }
        m
    }

    async fn decrypt(
        &mut self,
        ssl_stream: &mut rustls::client::ClientConnection,
    ) -> Result<(), FrameReceiptError> {
        if self.header.frame.get_encryption() {
            let tls_len = u16::from_be_bytes([self.data[3], self.data[4]]);
            let mut plain_data = vec![0u8; self.data.len()];
            let mut cursor = Cursor::new(&self.data);
            let mut index = 0;
            loop {
                let n = ssl_stream
                    .read_tls(&mut cursor)
                    .map_err(FrameReceiptError::TlsReadError)?;
                if n == 0 {
                    break;
                }
                let pnp = ssl_stream
                    .process_new_packets()
                    .map_err(FrameReceiptError::TlsProcessingError)?;

                loop {
                    let amount = pnp.plaintext_bytes_to_read();
                    if amount > 0 {
                        match ssl_stream.reader().read(&mut plain_data[index..]) {
                            Ok(0) => break, // EOF for now
                            Ok(n) => index += n,
                            Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
                            Err(ref e) if e.kind() == std::io::ErrorKind::UnexpectedEof => break,
                            Err(e) => return Err(FrameReceiptError::TlsReadError(e)),
                        }
                    } else {
                        break;
                    }
                }
            }
            self.header.frame.set_encryption(false);
            self.data = plain_data[0..index].to_vec();
        }
        Ok(())
    }

    /// Build a vec with the frame that is ready to send out over the connection to the compatible android auto device.
    /// If necessary, the data will be encrypted.
    async fn build_vec(
        &self,
        stream: Option<&mut rustls::client::ClientConnection>,
    ) -> Result<Vec<u8>, SslError> {
        let mut buf = Vec::new();
        self.header.add_to(&mut buf);
        if self.header.frame.get_encryption() {
            if let Some(stream) = stream {
                let mut data = Vec::new();
                stream
                    .writer()
                    .write_all(&self.data)
                    .map_err(SslError::Write)?;
                stream.write_tls(&mut data).map_err(SslError::Tls)?;
                if data.is_empty() {
                    return Err(SslError::NoOutput);
                }
                let mut p = (data.len() as u16).to_be_bytes().to_vec();
                buf.append(&mut p);
                buf.append(&mut data);
            } else {
                return Err(SslError::MissingStream);
            }
        } else {
            let mut data = self.data.clone();
            let mut p = (data.len() as u16).to_be_bytes().to_vec();
            buf.append(&mut p);
            buf.append(&mut data);
        }
        Ok(buf)
    }
}

/// The errors that can occur in ssl communication
#[derive(Debug)]
pub enum SslError {
    /// An error writing ssl data
    Write(std::io::Error),
    /// A write tls error
    Tls(std::io::Error),
    /// An empty packet was received
    NoOutput,
    /// The ssl stream is missing
    MissingStream,
}

/// Responsible for receiving a full frame from the compatible android auto device
struct AndroidAutoFrameReceiver {
    /// Length received so far
    chunk_length: Vec<u8>,
    /// The length of the frame to receive, if it is known yet
    len: Option<u16>,
    /// The data for the current frame
    current_frame: Vec<u8>,
    /// The data received so far for a multi-frame packet
    rx_sofar: Vec<Vec<u8>>,
}

impl AndroidAutoFrameReceiver {
    /// Construct a new frame receiver
    fn new() -> Self {
        Self {
            chunk_length: Vec::new(),
            len: None,
            current_frame: Vec::new(),
            rx_sofar: Vec::new(),
        }
    }

    async fn read<T: tokio::io::AsyncRead + Unpin>(
        &mut self,
        header: &FrameHeader,
        stream: &mut T,
    ) -> Result<Option<AndroidAutoFrame>, FrameReceiptError> {
        if self.len.is_none() {
            if header.frame.get_frame_type() == FrameHeaderType::First {
                let mut p = [0u8; 6];
                stream
                    .read_exact(&mut p)
                    .await
                    .map_err(|e| match e.kind() {
                        std::io::ErrorKind::TimedOut => FrameReceiptError::TimeoutHeader,
                        std::io::ErrorKind::UnexpectedEof => FrameReceiptError::Disconnected,
                        _ => FrameReceiptError::UnexpectedDuringFrameLength(e),
                    })?;
                let len = u16::from_be_bytes([p[0], p[1]]);
                self.len.replace(len);
            } else {
                let mut p = [0u8; 2];
                stream
                    .read_exact(&mut p)
                    .await
                    .map_err(|e| match e.kind() {
                        std::io::ErrorKind::TimedOut => FrameReceiptError::TimeoutHeader,
                        std::io::ErrorKind::UnexpectedEof => FrameReceiptError::Disconnected,
                        _ => FrameReceiptError::UnexpectedDuringFrameLength(e),
                    })?;
                let len = u16::from_be_bytes(p);
                self.len.replace(len);
            }
        }

        if let Some(len) = &self.len {
            let mut data_frame = vec![0u8; *len as usize];
            stream
                .read_exact(&mut data_frame)
                .await
                .map_err(|e| match e.kind() {
                    std::io::ErrorKind::TimedOut => FrameReceiptError::TimeoutHeader,
                    std::io::ErrorKind::UnexpectedEof => FrameReceiptError::Disconnected,
                    _ => FrameReceiptError::UnexpectedDuringFrameContents(e),
                })?;
            let data = if header.frame.get_frame_type() == FrameHeaderType::Single {
                let d = data_frame.clone();
                self.len.take();
                Some(vec![d])
            } else {
                self.rx_sofar.push(data_frame);
                if header.frame.get_frame_type() == FrameHeaderType::Last {
                    let d = self.rx_sofar.clone();
                    self.rx_sofar.clear();
                    self.len.take();
                    Some(d)
                } else {
                    self.len.take();
                    None
                }
            };
            if let Some(data) = data {
                let data: Vec<u8> = data.into_iter().flatten().collect();
                let f = AndroidAutoFrame {
                    header: *header,
                    data,
                };
                let f = Some(f);
                return Ok(f);
            }
        }
        Ok(None)
    }
}

#[cfg(feature = "wireless")]
/// A message sent or received over the android auto bluetooth connection. Used for setting up wireless android auto.
enum AndroidAutoBluetoothMessage {
    /// A request for socket information
    SocketInfoRequest(Bluetooth::SocketInfoRequest),
    /// A message relaying network information to the other party
    NetworkInfoMessage(Bluetooth::NetworkInfo),
}

#[cfg(feature = "wireless")]
impl AndroidAutoBluetoothMessage {
    /// Build an `AndroidAutoMessage` from self
    fn as_message(&self) -> AndroidAutoRawBluetoothMessage {
        use protobuf::Message;
        match self {
            AndroidAutoBluetoothMessage::SocketInfoRequest(m) => AndroidAutoRawBluetoothMessage {
                t: Bluetooth::MessageId::BLUETOOTH_SOCKET_INFO_REQUEST as u16,
                message: m.write_to_bytes().unwrap(),
            },
            AndroidAutoBluetoothMessage::NetworkInfoMessage(m) => AndroidAutoRawBluetoothMessage {
                t: Bluetooth::MessageId::BLUETOOTH_NETWORK_INFO_MESSAGE as u16,
                message: m.write_to_bytes().unwrap(),
            },
        }
    }
}

#[cfg(feature = "wireless")]
impl From<AndroidAutoRawBluetoothMessage> for Vec<u8> {
    fn from(value: AndroidAutoRawBluetoothMessage) -> Self {
        let mut buf = Vec::new();
        let b = value.message.len() as u16;
        let a = b.to_be_bytes();
        buf.push(a[0]);
        buf.push(a[1]);
        let a = value.t.to_be_bytes();
        buf.push(a[0]);
        buf.push(a[1]);
        for b in value.message {
            buf.push(b);
        }
        buf
    }
}

/// The trait that all channel handlers must implement for android auto channels.
#[enum_dispatch::enum_dispatch]
trait ChannelHandlerTrait {
    /// Process data received that is specific to this channel. Return an error for any packets that were not handled that should cause communication to stop.
    async fn receive_data<T: AndroidAutoMainTrait + ?Sized>(
        &self,
        msg: AndroidAutoFrame,
        stream: &WriteHalf,
        _config: &AndroidAutoConfiguration,
        _main: &T,
    ) -> Result<(), FrameIoError>;

    /// Construct the channeldescriptor with the channel handler so it can be conveyed to the compatible android auto device
    fn build_channel<T: AndroidAutoMainTrait + ?Sized>(
        &self,
        config: &AndroidAutoConfiguration,
        chanid: ChannelId,
        main: &T,
    ) -> Option<ChannelDescriptor>;

    /// Set the list of all channels for the current channel. Only used for the control channel. This is because the control channel must be created first.
    fn set_channels(&self, _chans: Vec<ChannelDescriptor>) {}
}

/// A message sent for an av channel
#[derive(Debug)]
enum AvChannelMessage {
    /// A message to start setup of the av channel
    SetupRequest(ChannelId, Wifi::AVChannelSetupRequest),
    /// A message that responds to a setup request
    SetupResponse(ChannelId, Wifi::AVChannelSetupResponse),
    /// Message requesting the focus of the video channel to be set
    VideoFocusRequest(ChannelId, Wifi::VideoFocusRequest),
    /// Message requesting to open the channel
    AvChannelOpen(ChannelId, Wifi::AVInputOpenRequest),
    /// Message indication the focus status of the video stream on the head unit
    VideoIndicationResponse(ChannelId, Wifi::VideoFocusIndication),
    /// The stream is about to start
    StartIndication(ChannelId, Wifi::AVChannelStartIndication),
    /// The stream is about to stop
    StopIndication(ChannelId, Wifi::AVChannelStopIndication),
    /// A media indication message, optionally containing a timestamp
    MediaIndication(ChannelId, Option<u64>, Vec<u8>),
    /// An acknowledgement of receiving a media indication message
    MediaIndicationAck(ChannelId, Wifi::AVMediaAckIndication),
}

impl From<AvChannelMessage> for AndroidAutoFrame {
    fn from(value: AvChannelMessage) -> Self {
        match value {
            AvChannelMessage::AvChannelOpen(_, _) => unimplemented!(),
            AvChannelMessage::MediaIndicationAck(chan, m) => {
                let mut data = m.write_to_bytes().unwrap();
                let t = Wifi::avchannel_message::Enum::AV_MEDIA_ACK_INDICATION as u16;
                let t = t.to_be_bytes();
                let mut m = Vec::new();
                m.push(t[0]);
                m.push(t[1]);
                m.append(&mut data);
                AndroidAutoFrame {
                    header: FrameHeader {
                        channel_id: chan,
                        frame: FrameHeaderContents::new(true, FrameHeaderType::Single, false),
                    },
                    data: m,
                }
            }
            AvChannelMessage::SetupRequest(_, _) => unimplemented!(),
            AvChannelMessage::SetupResponse(chan, m) => {
                let mut data = m.write_to_bytes().unwrap();
                let t = Wifi::avchannel_message::Enum::SETUP_RESPONSE as u16;
                let t = t.to_be_bytes();
                let mut m = Vec::new();
                m.push(t[0]);
                m.push(t[1]);
                m.append(&mut data);
                AndroidAutoFrame {
                    header: FrameHeader {
                        channel_id: chan,
                        frame: FrameHeaderContents::new(true, FrameHeaderType::Single, false),
                    },
                    data: m,
                }
            }
            AvChannelMessage::MediaIndication(chan, timestamp, mut data) => {
                let (t, mut data) = if let Some(ts) = timestamp {
                    let mut m = Vec::new();
                    let mut tsb = ts.to_be_bytes().to_vec();
                    m.append(&mut tsb);
                    m.append(&mut data);
                    (
                        Wifi::avchannel_message::Enum::AV_MEDIA_WITH_TIMESTAMP_INDICATION as u16,
                        m,
                    )
                } else {
                    let mut m = Vec::new();
                    m.append(&mut data);
                    (Wifi::avchannel_message::Enum::AV_MEDIA_INDICATION as u16, m)
                };
                let t = t.to_be_bytes();
                let mut m = Vec::new();
                m.push(t[0]);
                m.push(t[1]);
                m.append(&mut data);
                AndroidAutoFrame {
                    header: FrameHeader {
                        channel_id: chan,
                        frame: FrameHeaderContents::new(true, FrameHeaderType::Single, false),
                    },
                    data: m,
                }
            }
            AvChannelMessage::VideoFocusRequest(_chan, _m) => unimplemented!(),
            AvChannelMessage::VideoIndicationResponse(chan, m) => {
                let mut data = m.write_to_bytes().unwrap();
                let t = Wifi::avchannel_message::Enum::VIDEO_FOCUS_INDICATION as u16;
                let t = t.to_be_bytes();
                let mut m = Vec::new();
                m.push(t[0]);
                m.push(t[1]);
                m.append(&mut data);
                AndroidAutoFrame {
                    header: FrameHeader {
                        channel_id: chan,
                        frame: FrameHeaderContents::new(true, FrameHeaderType::Single, false),
                    },
                    data: m,
                }
            }
            AvChannelMessage::StartIndication(_, _) => unimplemented!(),
            AvChannelMessage::StopIndication(_, _) => unimplemented!(),
        }
    }
}

impl TryFrom<&AndroidAutoFrame> for AvChannelMessage {
    type Error = String;
    fn try_from(value: &AndroidAutoFrame) -> Result<Self, Self::Error> {
        use protobuf::Enum;
        let mut ty = [0u8; 2];
        ty.copy_from_slice(&value.data[0..2]);
        let ty = u16::from_be_bytes(ty);
        if let Some(sys) = Wifi::avchannel_message::Enum::from_i32(ty as i32) {
            match sys {
                Wifi::avchannel_message::Enum::AV_MEDIA_WITH_TIMESTAMP_INDICATION => {
                    let mut b = [0u8; 8];
                    b.copy_from_slice(&value.data[2..10]);
                    let ts: u64 = u64::from_be_bytes(b);
                    Ok(Self::MediaIndication(
                        value.header.channel_id,
                        Some(ts),
                        value.data[10..].to_vec(),
                    ))
                }
                Wifi::avchannel_message::Enum::AV_MEDIA_INDICATION => Ok(Self::MediaIndication(
                    value.header.channel_id,
                    None,
                    value.data[2..].to_vec(),
                )),
                Wifi::avchannel_message::Enum::SETUP_REQUEST => {
                    let m = Wifi::AVChannelSetupRequest::parse_from_bytes(&value.data[2..]);
                    match m {
                        Ok(m) => Ok(Self::SetupRequest(value.header.channel_id, m)),
                        Err(e) => Err(format!("Invalid channel setup request: {}", e)),
                    }
                }
                Wifi::avchannel_message::Enum::START_INDICATION => {
                    let m = Wifi::AVChannelStartIndication::parse_from_bytes(&value.data[2..]);
                    match m {
                        Ok(m) => Ok(Self::StartIndication(value.header.channel_id, m)),
                        Err(e) => Err(format!("Invalid channel start request: {}", e)),
                    }
                }
                Wifi::avchannel_message::Enum::STOP_INDICATION => {
                    let m = Wifi::AVChannelStopIndication::parse_from_bytes(&value.data[2..]);
                    match m {
                        Ok(m) => Ok(Self::StopIndication(value.header.channel_id, m)),
                        Err(e) => Err(format!("Invalid channel stop request: {}", e)),
                    }
                }
                Wifi::avchannel_message::Enum::SETUP_RESPONSE => unimplemented!(),
                Wifi::avchannel_message::Enum::AV_MEDIA_ACK_INDICATION => {
                    let m = Wifi::AVMediaAckIndication::parse_from_bytes(&value.data[2..]);
                    match m {
                        Ok(m) => Ok(Self::MediaIndicationAck(value.header.channel_id, m)),
                        Err(e) => Err(format!("Invalid channel stop request: {}", e)),
                    }
                }
                Wifi::avchannel_message::Enum::AV_INPUT_OPEN_REQUEST => {
                    let m = Wifi::AVInputOpenRequest::parse_from_bytes(&value.data[2..]);
                    match m {
                        Ok(m) => Ok(Self::AvChannelOpen(value.header.channel_id, m)),
                        Err(e) => Err(format!("Invalid request: {}", e)),
                    }
                }
                Wifi::avchannel_message::Enum::AV_INPUT_OPEN_RESPONSE => todo!(),
                Wifi::avchannel_message::Enum::VIDEO_FOCUS_REQUEST => {
                    let m = Wifi::VideoFocusRequest::parse_from_bytes(&value.data[2..]);
                    match m {
                        Ok(m) => Ok(Self::VideoFocusRequest(value.header.channel_id, m)),
                        Err(e) => Err(format!("Invalid request: {}", e)),
                    }
                }
                Wifi::avchannel_message::Enum::VIDEO_FOCUS_INDICATION => unimplemented!(),
            }
        } else {
            Err(format!("Not converted message: {:x?}", value.data))
        }
    }
}

/// The server verifier for android auto head units. This verifies the certificate in the android auto compatible device (probably a phone)
#[derive(Debug)]
struct AndroidAutoServerVerifier {
    /// The object providing most of the functionality for server verification
    base: Arc<rustls::client::WebPkiServerVerifier>,
}

impl AndroidAutoServerVerifier {
    /// Build a new server verifier using the given root certificate store
    fn new(roots: Arc<rustls::RootCertStore>) -> Self {
        Self {
            base: rustls::client::WebPkiServerVerifier::builder(roots)
                .build()
                .unwrap(),
        }
    }
}

impl rustls::client::danger::ServerCertVerifier for AndroidAutoServerVerifier {
    fn verify_server_cert(
        &self,
        _end_entity: &rustls::pki_types::CertificateDer<'_>,
        _intermediates: &[rustls::pki_types::CertificateDer<'_>],
        _server_name: &rustls::pki_types::ServerName<'_>,
        _ocsp_response: &[u8],
        _now: rustls::pki_types::UnixTime,
    ) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
        Ok(rustls::client::danger::ServerCertVerified::assertion())
    }

    fn verify_tls12_signature(
        &self,
        message: &[u8],
        cert: &rustls::pki_types::CertificateDer<'_>,
        dss: &rustls::DigitallySignedStruct,
    ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
        self.base.verify_tls12_signature(message, cert, dss)
    }

    fn verify_tls13_signature(
        &self,
        message: &[u8],
        cert: &rustls::pki_types::CertificateDer<'_>,
        dss: &rustls::DigitallySignedStruct,
    ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
        self.base.verify_tls13_signature(message, cert, dss)
    }

    fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
        self.base.supported_verify_schemes()
    }
}

/// The channel handler type that covers all possible channel handlers
#[enum_dispatch::enum_dispatch(ChannelHandlerTrait)]
enum ChannelHandler {
    Control(ControlChannelHandler),
    Bluetooth(BluetoothChannelHandler),
    AvInput(AvInputChannelHandler),
    SystemAudio(SystemAudioChannelHandler),
    SpeechAudio(SpeechAudioChannelHandler),
    Sensor(SensorChannelHandler),
    Video(VideoChannelHandler),
    Navigation(NavigationChannelHandler),
    MediaStatus(MediaStatusChannelHandler),
    Input(InputChannelHandler),
    MediaAudio(MediaAudioChannelHandler),
}

/// This is a wrapper around a join handle, it aborts the handle when it is dropped.
struct DroppingJoinHandle<T> {
    /// The handle for the struct
    handle: tokio::task::JoinHandle<T>,
}

impl<T> Drop for DroppingJoinHandle<T> {
    fn drop(&mut self) {
        self.handle.abort();
    }
}

#[cfg(feature = "wireless")]
/// The handler function for a single bluetooth connection
async fn handle_bluetooth_client(
    stream: &mut BluetoothStream,
    network2: &NetworkInformation,
) -> Result<(), String> {
    let mut s = Bluetooth::SocketInfoRequest::new();
    s.set_ip_address(network2.ip.clone());
    s.set_port(network2.port as u32);
    log::info!("Got a bluetooth client");
    let m1 = AndroidAutoBluetoothMessage::SocketInfoRequest(s);
    let m: AndroidAutoRawBluetoothMessage = m1.as_message();
    let mdata: Vec<u8> = m.into();
    stream.write_all(&mdata).await.map_err(|e| e.to_string())?;
    loop {
        let mut ty = [0u8; 2];
        let mut len = [0u8; 2];
        stream
            .read_exact(&mut len)
            .await
            .map_err(|e| e.to_string())?;
        stream
            .read_exact(&mut ty)
            .await
            .map_err(|e| e.to_string())?;
        let len = u16::from_be_bytes(len);
        let ty = u16::from_be_bytes(ty);
        let mut message = vec![0; len as usize];
        stream
            .read_exact(&mut message)
            .await
            .map_err(|e| e.to_string())?;
        use protobuf::Enum;
        match Bluetooth::MessageId::from_i32(ty as i32) {
            Some(m) => match m {
                Bluetooth::MessageId::BLUETOOTH_SOCKET_INFO_REQUEST => {
                    log::error!("Got a socket info request {:x?}", message);
                    break;
                }
                Bluetooth::MessageId::BLUETOOTH_NETWORK_INFO_REQUEST => {
                    let mut response = Bluetooth::NetworkInfo::new();
                    log::debug!("Network info for bluetooth response: {:?}", network2);
                    response.set_ssid(network2.ssid.clone());
                    response.set_psk(network2.psk.clone());
                    response.set_mac_addr(network2.mac_addr.clone());
                    response.set_security_mode(network2.security_mode);
                    response.set_ap_type(network2.ap_type);
                    let response = AndroidAutoBluetoothMessage::NetworkInfoMessage(response);
                    let m: AndroidAutoRawBluetoothMessage = response.as_message();
                    let mdata: Vec<u8> = m.into();
                    let _ = stream.write_all(&mdata).await;
                }
                Bluetooth::MessageId::BLUETOOTH_SOCKET_INFO_RESPONSE => {
                    let message = Bluetooth::SocketInfoResponse::parse_from_bytes(&message);
                    log::info!("Message is now {:?}", message);
                    if let Ok(m) = message {
                        if m.status() == Status::STATUS_SUCCESS {
                            break;
                        }
                    }
                }
                _ => {}
            },
            _ => {
                log::error!("Unknown bluetooth packet {} {:x?}", ty, message);
            }
        }
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
    }
    log::info!("Ending bluetooth comms");
    Ok(())
}

#[cfg(feature = "wireless")]
/// Runs the bluetooth service that allows wireless android auto connections to start up
async fn bluetooth_service(
    mut profile: bluetooth_rust::BluetoothRfcommProfileAsync,
    wireless: Arc<dyn AndroidAutoWirelessTrait>,
) -> Result<(), String> {
    log::info!("Starting bluetooth service");
    loop {
        if let Ok(c) = profile.connectable().await {
            let network2 = wireless.get_wifi_details();
            use bluetooth_rust::BluetoothRfcommConnectableAsyncTrait;
            let mut stream =
                bluetooth_rust::BluetoothRfcommConnectableAsyncTrait::accept(c).await?;
            let e = handle_bluetooth_client(&mut stream.0, &network2).await;
            log::info!("Bluetooth client disconnected: {:?}", e);
        }
    }
}

#[cfg(feature = "wireless")]
/// Runs the wifi service for android auto
async fn wifi_service<T: AndroidAutoWirelessTrait + Send + ?Sized>(
    wireless: Arc<T>,
) -> Result<ConnectionType, String> {
    let network = wireless.get_wifi_details();

    log::info!(
        "Starting android auto wireless service on port {}",
        network.port
    );
    if let Ok(a) = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", network.port)).await {
        log::info!("Starting wifi listener");
        loop {
            if let Ok((stream, _addr)) = a.accept().await {
                let _ = stream.set_nodelay(true);
                return Ok(ConnectionType::Wireless(stream));
            }
        }
    } else {
        Err(format!("Failed to listen on port {} tcp", network.port))
    }
}

/// Handle a single android auto device for a head unit
async fn handle_client_generic<
    T: AndroidAutoMainTrait + ?Sized,
    R: AsyncRead + Send + Unpin + 'static,
    W: AsyncWrite + Send + Unpin + 'static,
>(
    reader: R,
    writer: W,
    config: AndroidAutoConfiguration,
    main: &Box<T>,
) -> Result<(), ClientError> {
    log::info!("Got android auto client");
    let mut root_store =
        rustls::RootCertStore::from_iter(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
    let aautocertder = {
        let mut br = std::io::Cursor::new(cert::AAUTO_CERT.to_string().as_bytes().to_vec());
        let aautocertpem = rustls::pki_types::pem::from_buf(&mut br)
            .map_err(|_| ClientError::InvalidRootCert)?
            .ok_or(ClientError::InvalidRootCert)?;
        CertificateDer::from_pem(aautocertpem.0, aautocertpem.1)
            .ok_or(ClientError::InvalidRootCert)?
    };

    let client_cert_data_pem = if let Some(custom) = &config.custom_certificate {
        custom
    } else {
        &(
            cert::CERTIFICATE.to_string().as_bytes().to_vec(),
            cert::PRIVATE_KEY.to_string().as_bytes().to_vec(),
        )
    };

    let cert = {
        let mut br = std::io::Cursor::new(&client_cert_data_pem.0);
        let aautocertpem = rustls::pki_types::pem::from_buf(&mut br)
            .map_err(|_| ClientError::InvalidClientCertificate)?
            .ok_or(ClientError::InvalidClientCertificate)?;
        CertificateDer::from_pem(aautocertpem.0, aautocertpem.1)
            .ok_or(ClientError::InvalidClientCertificate)?
    };
    let key = {
        let mut br = std::io::Cursor::new(&client_cert_data_pem.1);
        let aautocertpem = rustls::pki_types::pem::from_buf(&mut br)
            .map_err(|_| ClientError::InvalidClientPrivateKey)?
            .ok_or(ClientError::InvalidClientPrivateKey)?;
        rustls::pki_types::PrivateKeyDer::from_pem(aautocertpem.0, aautocertpem.1)
            .ok_or(ClientError::InvalidClientPrivateKey)?
    };
    let cert = vec![cert];
    root_store
        .add(aautocertder)
        .map_err(|_| ClientError::InvalidRootCert)?;
    let root_store = Arc::new(root_store);
    let mut ssl_client_config = rustls::ClientConfig::builder()
        .with_root_certificates(root_store.clone())
        .with_client_auth_cert(cert, key)
        .unwrap();
    let sver = Arc::new(AndroidAutoServerVerifier::new(root_store));
    ssl_client_config.dangerous().set_certificate_verifier(sver);
    let sslconfig = Arc::new(ssl_client_config);
    let server = "idontknow.com".try_into().unwrap();
    let ssl_client =
        rustls::ClientConnection::new(sslconfig, server).expect("Failed to build ssl client");
    let sm = StreamMux::new(ssl_client, writer, reader);
    let message_recv = main.get_receiver().await;
    let sm = sm.split();
    let sm2 = sm.1.clone();
    let kill = tokio::sync::oneshot::channel::<()>();
    let kill2 = tokio::sync::oneshot::channel::<()>();
    let _task2 = if let Some(mut msgr) = message_recv {
        let jh: tokio::task::JoinHandle<
            Result<(), tokio::sync::mpsc::error::SendError<SslThreadData>>,
        > = tokio::task::spawn(async move {
            while let Some(m) = msgr.recv().await {
                if let Err(e) = sm2.write_message(m).await {
                    log::error!("Error passing message: {:?}", e);
                    let _ = kill.0.send(());
                    return Err(e);
                }
            }
            Ok(())
        });
        Some(DroppingJoinHandle { handle: jh })
    } else {
        None
    };

    let sm3 = sm.1.clone();
    tokio::spawn(async move {
        tokio::select! {
            _ = async {
                loop {
                    tokio::time::sleep(std::time::Duration::from_secs(5)).await;
                    let mut m = Wifi::PingRequest::new();
                    let timestamp = std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap()
                        .as_micros() as i64;
                    m.set_timestamp(timestamp);
                    if let Err(e) = sm3
                        .write_frame(AndroidAutoControlMessage::PingRequest(m).into())
                        .await {
                            log::error!("Error sending ping request {:?}", e);
                        }
                }
            } => {}
            _ = kill2.1 => {
            }
        }
        log::info!("Exiting pinger");
    });

    log::info!("Sending channel handlers");
    {
        let mut channel_handlers: Vec<ChannelHandler> = Vec::new();
        channel_handlers.push(ControlChannelHandler::new().into());
        channel_handlers.push(InputChannelHandler {}.into());
        channel_handlers.push(SensorChannelHandler {}.into());
        channel_handlers.push(VideoChannelHandler::new().into());
        channel_handlers.push(MediaAudioChannelHandler {}.into());
        channel_handlers.push(SpeechAudioChannelHandler {}.into());
        channel_handlers.push(SystemAudioChannelHandler {}.into());
        channel_handlers.push(AvInputChannelHandler {}.into());
        if main.supports_bluetooth().is_some() {
            channel_handlers.push(BluetoothChannelHandler {}.into());
        }
        if main.supports_navigation().is_some() {
            channel_handlers.push(NavigationChannelHandler {}.into());
        }
        channel_handlers.push(MediaStatusChannelHandler {}.into());

        let mut chans = Vec::new();
        for (index, handler) in channel_handlers.iter().enumerate() {
            let chan: ChannelId = index as u8;
            if let Some(chan) = handler.build_channel(&config, chan, main.as_ref()) {
                chans.push(chan);
            }
        }
        channel_handlers.get_mut(0).unwrap().set_channels(chans);
        {
            let mut ch = CHANNEL_HANDLERS.write().await;
            ch.clear();
            log::error!(
                "Adding {} channels to CHANNEL_HANDLERS",
                channel_handlers.len()
            );
            ch.append(&mut channel_handlers);
        }
    }
    log::info!("Sending version request");
    sm.1.write_frame(AndroidAutoControlMessage::VersionRequest.into())
        .await
        .map_err(|e| {
            let e2: FrameIoError = e.into();
            e2
        })?;
    let channel_handlers = CHANNEL_HANDLERS.read().await;
    log::debug!("Waiting on first packet from android auto client");

    tokio::select! {
        a = do_android_auto_loop(channel_handlers, sm.0, &sm.1, config, main) => {

        }
        _ = kill.1 => {

        }
    }
    kill2.0.send(());
    Ok(())
}

async fn do_android_auto_loop<T: AndroidAutoMainTrait + ?Sized>(
    channel_handlers: RwLockReadGuard<'_, Vec<ChannelHandler>>,
    mut sm: ReadHalf,
    sr: &WriteHalf,
    config: AndroidAutoConfiguration,
    main: &Box<T>,
) -> Result<(), ClientError> {
    loop {
        if let Some(f) = sm.recv().await {
            match f {
                SslThreadResponse::Data(f) => {
                    if let Some(handler) = channel_handlers.get(f.header.channel_id as usize) {
                        handler.receive_data(f, sr, &config, main.as_ref()).await?;
                    } else {
                        panic!("Unknown channel id: {:?}", f.header.channel_id);
                    }
                }
                SslThreadResponse::HandshakeComplete => {
                    sr.write_frame(AndroidAutoControlMessage::SslAuthComplete(true).into())
                        .await?;
                    log::info!("SSL Handshake complete");
                }
                SslThreadResponse::ExitError(e) => {
                    log::error!("The error for exit is {}", e);
                    todo!();
                }
            }
        }
    }
}

#[cfg(feature = "usb")]
/// Watch for a usb disconnect message from nusb
async fn watch_for_disconnect(device_address: Arc<nusb::DeviceInfo>) {
    let mut watcher = nusb::watch_devices().unwrap();
    while let Some(event) = watcher.next().await {
        match event {
            nusb::hotplug::HotplugEvent::Disconnected(_info) => {
                let devs = nusb::list_devices().await;
                if let Ok(mut devs) = devs {
                    if devs
                        .find(|a| {
                            a.busnum() == device_address.busnum()
                                && a.device_address() == device_address.device_address()
                        })
                        .is_none()
                    {
                        log::info!("Android Auto USB device disconnected");
                        break;
                    }
                } else {
                    break;
                }
            }
            _ => {}
        }
    }
}

/// A helper type for operating the android auto code
type AsyncFn =
    Box<dyn FnOnce() -> std::pin::Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>;

#[cfg(feature = "usb")]
fn make_disconnect_watcher(device_address: nusb::DeviceInfo) -> AsyncFn {
    let device_address = std::sync::Arc::new(device_address);
    Box::new(move || {
        let device_address = device_address.clone();
        Box::pin(async move {
            watch_for_disconnect(device_address).await;
        })
    })
}

/// Token proving that [`setup`] has been called. Required to use the library's
/// main entry points so that initialisation cannot be forgotten.
///
/// Constructed exclusively by [`setup`]; users cannot build this type themselves.
/// The token is `Copy` so it can be stored and handed to multiple container
/// restarts without needing to call `setup` again.
#[derive(Clone, Copy)]
pub struct AndroidAutoSetup {
    _private: (),
}

/// Perform any setup required on startup of the library.
///
/// Returns an [`AndroidAutoSetup`] token that must be passed to [`AndroidAutoMainTrait::run`]
/// (and related methods). Requiring this token at the call site ensures that setup is
/// never accidentally skipped.
pub fn setup() -> AndroidAutoSetup {
    let cp = rustls::crypto::ring::default_provider();
    cp.install_default().expect("Failed to set ssl provider");
    AndroidAutoSetup { _private: () }
}