File: hfp.py

package info (click to toggle)
python-bumble 0.0.225-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 9,464 kB
  • sloc: python: 75,258; java: 3,782; javascript: 823; xml: 203; sh: 172; makefile: 8
file content (2113 lines) | stat: -rw-r--r-- 76,696 bytes parent folder | download | duplicates (2)
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
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
from __future__ import annotations

import asyncio
import collections
import collections.abc
import dataclasses
import enum
import logging
import re
import traceback
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, ClassVar

from typing_extensions import Self

from bumble import at, device, rfcomm, sdp, utils
from bumble.colors import color
from bumble.core import (
    BT_GENERIC_AUDIO_SERVICE,
    BT_HANDSFREE_AUDIO_GATEWAY_SERVICE,
    BT_HANDSFREE_SERVICE,
    BT_L2CAP_PROTOCOL_ID,
    BT_RFCOMM_PROTOCOL_ID,
    ProtocolError,
)
from bumble.hci import (
    CodecID,
    CodingFormat,
    HCI_Enhanced_Setup_Synchronous_Connection_Command,
)

# -----------------------------------------------------------------------------
# Logging
# -----------------------------------------------------------------------------
logger = logging.getLogger(__name__)

# -----------------------------------------------------------------------------
# Error
# -----------------------------------------------------------------------------


class HfpProtocolError(ProtocolError):
    def __init__(self, error_name: str = '', details: str = ''):
        super().__init__(None, 'hfp', error_name, details)


# -----------------------------------------------------------------------------
# Protocol Support
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
class HfpProtocol:
    dlc: rfcomm.DLC
    buffer: str
    lines: collections.deque
    lines_available: asyncio.Event

    def __init__(self, dlc: rfcomm.DLC) -> None:
        self.dlc = dlc
        self.buffer = ''
        self.lines = collections.deque()
        self.lines_available = asyncio.Event()

        dlc.sink = self.feed

    def feed(self, data: bytes | str) -> None:
        # Convert the data to a string if needed
        if isinstance(data, bytes):
            data = data.decode('utf-8')

        logger.debug(f'<<< Data received: {data}')

        # Add to the buffer and look for lines
        self.buffer += data
        while (separator := self.buffer.find('\r')) >= 0:
            line = self.buffer[:separator].strip()
            self.buffer = self.buffer[separator + 1 :]
            if len(line) > 0:
                self.on_line(line)

    def on_line(self, line: str) -> None:
        self.lines.append(line)
        self.lines_available.set()

    def send_command_line(self, line: str) -> None:
        logger.debug(color(f'>>> {line}', 'yellow'))
        self.dlc.write(line + '\r')

    def send_response_line(self, line: str) -> None:
        logger.debug(color(f'>>> {line}', 'yellow'))
        self.dlc.write('\r\n' + line + '\r\n')

    async def next_line(self) -> str:
        await self.lines_available.wait()
        line = self.lines.popleft()
        if not self.lines:
            self.lines_available.clear()
        logger.debug(color(f'<<< {line}', 'green'))
        return line


# -----------------------------------------------------------------------------
# Normative protocol definitions
# -----------------------------------------------------------------------------


class HfFeature(enum.IntFlag):
    """
    HF supported features (AT+BRSF=) (normative).

    Hands-Free Profile v1.9, 4.34.2, AT Capabilities Re-Used from GSM 07.07 and 3GPP 27.007.
    """

    EC_NR = 0x001  # Echo Cancel & Noise reduction
    THREE_WAY_CALLING = 0x002
    CLI_PRESENTATION_CAPABILITY = 0x004
    VOICE_RECOGNITION_ACTIVATION = 0x008
    REMOTE_VOLUME_CONTROL = 0x010
    ENHANCED_CALL_STATUS = 0x020
    ENHANCED_CALL_CONTROL = 0x040
    CODEC_NEGOTIATION = 0x080
    HF_INDICATORS = 0x100
    ESCO_S4_SETTINGS_SUPPORTED = 0x200
    ENHANCED_VOICE_RECOGNITION_STATUS = 0x400
    VOICE_RECOGNITION_TEXT = 0x800


class AgFeature(enum.IntFlag):
    """
    AG supported features (+BRSF:) (normative).

    Hands-Free Profile v1.9, 4.34.2, AT Capabilities Re-Used from GSM 07.07 and 3GPP 27.007.
    """

    THREE_WAY_CALLING = 0x001
    EC_NR = 0x002  # Echo Cancel & Noise reduction
    VOICE_RECOGNITION_FUNCTION = 0x004
    IN_BAND_RING_TONE_CAPABILITY = 0x008
    VOICE_TAG = 0x010  # Attach a number to voice tag
    REJECT_CALL = 0x020  # Ability to reject a call
    ENHANCED_CALL_STATUS = 0x040
    ENHANCED_CALL_CONTROL = 0x080
    EXTENDED_ERROR_RESULT_CODES = 0x100
    CODEC_NEGOTIATION = 0x200
    HF_INDICATORS = 0x400
    ESCO_S4_SETTINGS_SUPPORTED = 0x800
    ENHANCED_VOICE_RECOGNITION_STATUS = 0x1000
    VOICE_RECOGNITION_TEXT = 0x2000


class AudioCodec(enum.IntEnum):
    """
    Audio Codec IDs (normative).

    Hands-Free Profile v1.9, 11 Appendix B
    """

    CVSD = 0x01  # Support for CVSD audio codec
    MSBC = 0x02  # Support for mSBC audio codec
    LC3_SWB = 0x03  # Support for LC3-SWB audio codec


class HfIndicator(enum.IntEnum):
    """
    HF Indicators (normative).

    Bluetooth Assigned Numbers, 6.10.1 HF Indicators.
    """

    ENHANCED_SAFETY = 0x01  # Enhanced safety feature
    BATTERY_LEVEL = 0x02  # Battery level feature


class CallHoldOperation(enum.Enum):
    """
    Call Hold supported operations (normative).

    AT Commands Reference Guide, 3.5.2.3.12 +CHLD - Call Holding Services.
    """

    RELEASE_ALL_HELD_CALLS = "0"  # Release all held calls
    RELEASE_ALL_ACTIVE_CALLS = "1"  # Release all active calls, accept other
    RELEASE_SPECIFIC_CALL = "1x"  # Release a specific call X
    HOLD_ALL_ACTIVE_CALLS = "2"  # Place all active calls on hold, accept other
    HOLD_ALL_CALLS_EXCEPT = "2x"  # Place all active calls except call X
    ADD_HELD_CALL = "3"  # Adds a held call to conversation
    CONNECT_TWO_CALLS = (
        "4"  # Connects the two calls and disconnects the subscriber from both calls
    )


class ResponseHoldStatus(enum.IntEnum):
    """
    Response Hold status (normative).

    Hands-Free Profile v1.8, 4.34.2, AT Capabilities Re-Used from GSM 07.07 and 3GPP 27.007.
    """

    INC_CALL_HELD = 0  # Put incoming call on hold
    HELD_CALL_ACC = 1  # Accept a held incoming call
    HELD_CALL_REJ = 2  # Reject a held incoming call


class AgIndicator(enum.Enum):
    """
    Values for the AG indicator (normative).

    Hands-Free Profile v1.8, 4.34.2, AT Capabilities Re-Used from GSM 07.07 and 3GPP 27.007.
    """

    SERVICE = 'service'
    CALL = 'call'
    CALL_SETUP = 'callsetup'
    CALL_HELD = 'callheld'
    SIGNAL = 'signal'
    ROAM = 'roam'
    BATTERY_CHARGE = 'battchg'


class CallSetupAgIndicator(enum.IntEnum):
    """
    Values for the Call Setup AG indicator (normative).

    Hands-Free Profile v1.8, 4.34.2, AT Capabilities Re-Used from GSM 07.07 and 3GPP 27.007.
    """

    NOT_IN_CALL_SETUP = 0
    INCOMING_CALL_PROCESS = 1
    OUTGOING_CALL_SETUP = 2
    REMOTE_ALERTED = 3  # Remote party alerted in an outgoing call


class CallHeldAgIndicator(enum.IntEnum):
    """
    Values for the Call Held AG indicator (normative).

    Hands-Free Profile v1.8, 4.34.2, AT Capabilities Re-Used from GSM 07.07 and 3GPP 27.007.
    """

    NO_CALLS_HELD = 0
    # Call is placed on hold or active/held calls swapped
    # (The AG has both an active AND a held call)
    CALL_ON_HOLD_AND_ACTIVE_CALL = 1
    CALL_ON_HOLD_NO_ACTIVE_CALL = 2  # Call on hold, no active call


class CallInfoDirection(enum.IntEnum):
    """
    Call Info direction (normative).

    AT Commands Reference Guide, 3.5.2.3.15 +CLCC - List Current Calls.
    """

    MOBILE_ORIGINATED_CALL = 0
    MOBILE_TERMINATED_CALL = 1


class CallInfoStatus(enum.IntEnum):
    """
    Call Info status (normative).

    AT Commands Reference Guide, 3.5.2.3.15 +CLCC - List Current Calls.
    """

    ACTIVE = 0
    HELD = 1
    DIALING = 2
    ALERTING = 3
    INCOMING = 4
    WAITING = 5


class CallInfoMode(enum.IntEnum):
    """
    Call Info mode (normative).

    AT Commands Reference Guide, 3.5.2.3.15 +CLCC - List Current Calls.
    """

    VOICE = 0
    DATA = 1
    FAX = 2
    UNKNOWN = 9


class CallInfoMultiParty(enum.IntEnum):
    """
    Call Info Multi-Party state (normative).

    AT Commands Reference Guide, 3.5.2.3.15 +CLCC - List Current Calls.
    """

    NOT_IN_CONFERENCE = 0
    IN_CONFERENCE = 1


@dataclasses.dataclass
class CallInfo:
    """
    Enhanced call status.

    AT Commands Reference Guide, 3.5.2.3.15 +CLCC - List Current Calls.
    """

    index: int
    direction: CallInfoDirection
    status: CallInfoStatus
    mode: CallInfoMode
    multi_party: CallInfoMultiParty
    number: str | None = None
    type: int | None = None


@dataclasses.dataclass
class CallLineIdentification:
    """
    Calling Line Identification notification.

    TS 127 007 - V6.8.0, 7.6 Calling line identification presentation +CLIP, but only
    number, type and alpha are meaningful in HFP.

    Attributes:
        number: String type phone number of format specified by `type`.
        type: Type of address octet in integer format (refer TS 24.008 [8] subclause
        10.5.4.7).
        subaddr: String type subaddress of format specified by `satype`.
        satype: Type of subaddress octet in integer format (refer TS 24.008 [8]
        subclause 10.5.4.8).
        alpha: Optional string type alphanumeric representation of number corresponding
        to the entry found in phonebook; used character set should be the one selected
        with command Select TE Character Set +CSCS.
        cli_validity: 0 CLI valid, 1 CLI has been withheld by the originator, 2 CLI is
        not available due to interworking problems or limitations of originating
        network.
    """

    number: str
    type: int
    subaddr: str | None = None
    satype: int | None = None
    alpha: str | None = None
    cli_validity: int | None = None

    @classmethod
    def parse_from(cls, parameters: list[bytes]) -> Self:
        return cls(
            number=parameters[0].decode(),
            type=int(parameters[1]),
            subaddr=parameters[2].decode() if len(parameters) >= 3 else None,
            satype=(
                int(parameters[3]) if len(parameters) >= 4 and parameters[3] else None
            ),
            alpha=parameters[4].decode() if len(parameters) >= 5 else None,
            cli_validity=(
                int(parameters[5]) if len(parameters) >= 6 and parameters[5] else None
            ),
        )

    def to_clip_string(self) -> str:
        return ','.join(
            str(arg) if arg else ''
            for arg in [
                self.number,
                self.type,
                self.subaddr,
                self.satype,
                self.alpha,
                self.cli_validity,
            ]
        )


class VoiceRecognitionState(enum.IntEnum):
    """
    vrec values provided in AT+BVRA command.

    Hands-Free Profile v1.8, 4.34.2, AT Capabilities Re-Used from GSM 07.07 and 3GPP 27.007.
    """

    DISABLE = 0
    ENABLE = 1
    # (Enhanced Voice Recognition Status only) HF is ready to accept audio.
    ENHANCED_READY = 2


class CmeError(enum.IntEnum):
    """
    CME ERROR codes (partial listed).

    TS 127 007 - V6.8.0, 9.2.1 General errors
    """

    PHONE_FAILURE = 0
    OPERATION_NOT_ALLOWED = 3
    OPERATION_NOT_SUPPORTED = 4
    MEMORY_FULL = 20
    INVALID_INDEX = 21
    NOT_FOUND = 22


# -----------------------------------------------------------------------------
# Hands-Free Control Interoperability Requirements
# -----------------------------------------------------------------------------

# Response codes.
RESPONSE_CODES = {
    "+APLSIRI",
    "+BAC",
    "+BCC",
    "+BCS",
    "+BIA",
    "+BIEV",
    "+BIND",
    "+BINP",
    "+BLDN",
    "+BRSF",
    "+BTRH",
    "+BVRA",
    "+CCWA",
    "+CHLD",
    "+CHUP",
    "+CIND",
    "+CLCC",
    "+CLIP",
    "+CMEE",
    "+CMER",
    "+CNUM",
    "+COPS",
    "+IPHONEACCEV",
    "+NREC",
    "+VGM",
    "+VGS",
    "+VTS",
    "+XAPL",
    "A",
    "D",
}

# Unsolicited responses and statuses.
UNSOLICITED_CODES = {
    "+APLSIRI",
    "+BCS",
    "+BIND",
    "+BSIR",
    "+BTRH",
    "+BVRA",
    "+CCWA",
    "+CIEV",
    "+CLIP",
    "+VGM",
    "+VGS",
    "BLACKLISTED",
    "BUSY",
    "DELAYED",
    "NO ANSWER",
    "NO CARRIER",
    "RING",
}

# Status codes
STATUS_CODES = {
    "+CME ERROR",
    "BLACKLISTED",
    "BUSY",
    "DELAYED",
    "ERROR",
    "NO ANSWER",
    "NO CARRIER",
    "OK",
}


@dataclasses.dataclass
class HfConfiguration:
    supported_hf_features: collections.abc.Sequence[HfFeature]
    supported_hf_indicators: collections.abc.Sequence[HfIndicator]
    supported_audio_codecs: collections.abc.Sequence[AudioCodec]


@dataclasses.dataclass
class AgConfiguration:
    supported_ag_features: Iterable[AgFeature]
    supported_ag_indicators: collections.abc.Sequence[AgIndicatorState]
    supported_hf_indicators: Iterable[HfIndicator]
    supported_ag_call_hold_operations: Iterable[CallHoldOperation]
    supported_audio_codecs: Iterable[AudioCodec]


class AtResponseType(enum.Enum):
    """
    Indicates if a response is expected from an AT command, and if multiple responses are accepted.
    """

    NONE = 0
    SINGLE = 1
    MULTIPLE = 2


@dataclasses.dataclass
class AtResponse:
    code: str
    parameters: list

    @classmethod
    def parse_from(cls: type[Self], buffer: bytearray) -> Self:
        code_and_parameters = buffer.split(b':')
        parameters = (
            code_and_parameters[1] if len(code_and_parameters) > 1 else bytearray()
        )
        return cls(
            code=code_and_parameters[0].decode(),
            parameters=at.parse_parameters(parameters),
        )


@dataclasses.dataclass
class AtCommand:
    class SubCode(str, enum.Enum):
        NONE = ''
        SET = '='
        TEST = '=?'
        READ = '?'

    code: str
    sub_code: SubCode
    parameters: list

    _PARSE_PATTERN: ClassVar[re.Pattern] = re.compile(
        r'AT\+(?P<code>[A-Z]+)(?P<sub_code>=\?|=|\?)?(?P<parameters>.*)'
    )

    @classmethod
    def parse_from(cls: type[Self], buffer: bytearray) -> Self:
        if not (match := cls._PARSE_PATTERN.fullmatch(buffer.decode())):
            if buffer.startswith(b'ATA'):
                return cls(code='A', sub_code=AtCommand.SubCode.NONE, parameters=[])
            if buffer.startswith(b'ATD'):
                return cls(
                    code='D', sub_code=AtCommand.SubCode.NONE, parameters=[buffer[3:]]
                )
            raise HfpProtocolError('Invalid command')

        parameters = []
        if parameters_text := match.group('parameters'):
            parameters = at.parse_parameters(parameters_text.encode())

        return cls(
            code=match.group('code'),
            sub_code=AtCommand.SubCode(match.group('sub_code') or ''),
            parameters=parameters,
        )


@dataclasses.dataclass
class AgIndicatorState:
    """State wrapper of AG indicator.

    Attributes:
        indicator: Indicator of this indicator state.
        supported_values: Supported values of this indicator.
        current_status: Current status of this indicator.
        index: (HF only) Index of this indicator.
        enabled: (AG only) Whether this indicator is enabled to report.
        on_test_text: Text message reported in AT+CIND=? of this indicator.
    """

    indicator: AgIndicator
    supported_values: set[int]
    current_status: int
    index: int | None = None
    enabled: bool = True

    @property
    def on_test_text(self) -> str:
        min_value = min(self.supported_values)
        max_value = max(self.supported_values)
        if len(self.supported_values) == (max_value - min_value + 1):
            supported_values_text = f'({min_value}-{max_value})'
        else:
            supported_values_text = (
                f'({",".join(str(v) for v in self.supported_values)})'
            )
        return f'("{self.indicator.value}",{supported_values_text})'

    @classmethod
    def call(cls: type[Self]) -> Self:
        """Default call indicator state."""
        return cls(
            indicator=AgIndicator.CALL, supported_values={0, 1}, current_status=0
        )

    @classmethod
    def callsetup(cls: type[Self]) -> Self:
        """Default callsetup indicator state."""
        return cls(
            indicator=AgIndicator.CALL_SETUP,
            supported_values={0, 1, 2, 3},
            current_status=0,
        )

    @classmethod
    def callheld(cls: type[Self]) -> Self:
        """Default call indicator state."""
        return cls(
            indicator=AgIndicator.CALL_HELD,
            supported_values={0, 1, 2},
            current_status=0,
        )

    @classmethod
    def service(cls: type[Self]) -> Self:
        """Default service indicator state."""
        return cls(
            indicator=AgIndicator.SERVICE, supported_values={0, 1}, current_status=0
        )

    @classmethod
    def signal(cls: type[Self]) -> Self:
        """Default signal indicator state."""
        return cls(
            indicator=AgIndicator.SIGNAL,
            supported_values={0, 1, 2, 3, 4, 5},
            current_status=0,
        )

    @classmethod
    def roam(cls: type[Self]) -> Self:
        """Default roam indicator state."""
        return cls(
            indicator=AgIndicator.CALL, supported_values={0, 1}, current_status=0
        )

    @classmethod
    def battchg(cls: type[Self]) -> Self:
        """Default battery charge indicator state."""
        return cls(
            indicator=AgIndicator.BATTERY_CHARGE,
            supported_values={0, 1, 2, 3, 4, 5},
            current_status=0,
        )


@dataclasses.dataclass
class HfIndicatorState:
    """State wrapper of HF indicator.

    Attributes:
        indicator: Indicator of this indicator state.
        supported: Whether this indicator is supported.
        enabled: Whether this indicator is enabled.
        current_status: Current (last-reported) status value of this indicaotr.
    """

    indicator: HfIndicator
    supported: bool = False
    enabled: bool = False
    current_status: int = 0


class HfProtocol(utils.EventEmitter):
    """
    Implementation for the Hands-Free side of the Hands-Free profile.

    Reference specification Hands-Free Profile v1.8.

    Emitted events:
        codec_negotiation: When codec is renegotiated, notify the new codec.
            Args:
                active_codec: AudioCodec
        ag_indicator: When AG update their indicators, notify the new state.
            Args:
                ag_indicator: AgIndicator
        speaker_volume: Emitted when AG update speaker volume autonomously.
            Args:
                volume: Int
        microphone_volume: Emitted when AG update microphone volume autonomously.
            Args:
                volume: Int
        microphone_volume: Emitted when AG sends a ringtone request.
            Args:
                None
        cli_notification: Emitted when notify the call metadata on line.
            Args:
                cli_notification: CallLineIdentification
        voice_recognition: Emitted when AG starts voice recognition autonomously.
            Args:
                vrec: VoiceRecognitionState
    """

    EVENT_CODEC_NEGOTIATION = "codec_negotiation"
    EVENT_AG_INDICATOR = "ag_indicator"
    EVENT_SPEAKER_VOLUME = "speaker_volume"
    EVENT_MICROPHONE_VOLUME = "microphone_volume"
    EVENT_RING = "ring"
    EVENT_CLI_NOTIFICATION = "cli_notification"
    EVENT_VOICE_RECOGNITION = "voice_recognition"

    class HfLoopTermination(HfpProtocolError):
        """Termination signal for run() loop."""

    supported_hf_features: int
    supported_audio_codecs: list[AudioCodec]

    supported_ag_features: int
    supported_ag_call_hold_operations: list[CallHoldOperation]

    ag_indicators: list[AgIndicatorState]
    hf_indicators: dict[HfIndicator, HfIndicatorState]

    dlc: rfcomm.DLC
    command_lock: asyncio.Lock
    if TYPE_CHECKING:
        response_queue: asyncio.Queue[AtResponse]
        unsolicited_queue: asyncio.Queue[AtResponse | None]
    else:
        response_queue: asyncio.Queue
        unsolicited_queue: asyncio.Queue
    read_buffer: bytearray
    active_codec: AudioCodec

    def __init__(
        self,
        dlc: rfcomm.DLC,
        configuration: HfConfiguration,
    ) -> None:
        super().__init__()

        # Configure internal state.
        self.dlc = dlc
        self.command_lock = asyncio.Lock()
        self.response_queue = asyncio.Queue()
        self.unsolicited_queue = asyncio.Queue()
        self.read_buffer = bytearray()
        self.active_codec = AudioCodec.CVSD
        self._slc_initialized = False

        # Build local features.
        self.supported_hf_features = sum(configuration.supported_hf_features)
        self.supported_audio_codecs = list(configuration.supported_audio_codecs)

        self.hf_indicators = {
            indicator: HfIndicatorState(indicator=indicator)
            for indicator in configuration.supported_hf_indicators
        }

        # Clear remote features.
        self.supported_ag_features = 0
        self.supported_ag_call_hold_operations = []
        self.ag_indicators = []

        # Bind the AT reader to the RFCOMM channel.
        self.dlc.sink = self._read_at
        # Stop the run() loop when L2CAP is closed.
        self.dlc.multiplexer.l2cap_channel.on(
            self.dlc.multiplexer.l2cap_channel.EVENT_CLOSE,
            lambda: self.unsolicited_queue.put_nowait(None),
        )

    def supports_hf_feature(self, feature: HfFeature) -> bool:
        return (self.supported_hf_features & feature) != 0

    def supports_ag_feature(self, feature: AgFeature) -> bool:
        return (self.supported_ag_features & feature) != 0

    def _read_at(self, data: bytes):
        """
        Reads AT messages from the RFCOMM channel.

        Enqueues AT commands, responses, unsolicited responses to their respective queues, and set the corresponding event.
        """
        # Append to the read buffer.
        self.read_buffer.extend(data)

        while self.read_buffer:
            # Locate header and trailer.
            header = self.read_buffer.find(b'\r\n')
            trailer = self.read_buffer.find(b'\r\n', header + 2)
            if header == -1 or trailer == -1:
                return

            # Isolate the AT response code and parameters.
            raw_response = self.read_buffer[header + 2 : trailer]
            response = AtResponse.parse_from(raw_response)
            logger.debug(f"<<< {raw_response.decode()}")

            # Consume the response bytes.
            self.read_buffer = self.read_buffer[trailer + 2 :]

            # Forward the received code to the correct queue.
            if self.command_lock.locked() and (
                response.code in STATUS_CODES or response.code in RESPONSE_CODES
            ):
                self.response_queue.put_nowait(response)
            elif response.code in UNSOLICITED_CODES:
                self.unsolicited_queue.put_nowait(response)
            else:
                logger.warning(
                    f"dropping unexpected response with code '{response.code}'"
                )

    async def execute_command(
        self,
        cmd: str,
        timeout: float = 1.0,
        response_type: AtResponseType = AtResponseType.NONE,
    ) -> None | AtResponse | list[AtResponse]:
        """
        Sends an AT command and wait for the peer response.
        Wait for the AT responses sent by the peer, to the status code.

        Args:
            cmd: the AT command in string to execute.
            timeout: timeout in float seconds.
            response_type: type of response.

        Raises:
            asyncio.TimeoutError: the status is not received after a timeout (default 1 second).
            ProtocolError: the status is not OK.
        """
        async with self.command_lock:
            logger.debug(f">>> {cmd}")
            self.dlc.write(cmd + '\r')
            responses: list[AtResponse] = []

            while True:
                result = await asyncio.wait_for(
                    self.response_queue.get(), timeout=timeout
                )
                if result.code == 'OK':
                    if response_type == AtResponseType.SINGLE and len(responses) != 1:
                        raise HfpProtocolError("NO ANSWER")

                    if response_type == AtResponseType.MULTIPLE:
                        return responses
                    if response_type == AtResponseType.SINGLE:
                        return responses[0]
                    return None
                if result.code in STATUS_CODES:
                    raise HfpProtocolError(result.code)
                responses.append(result)

    async def initiate_slc(self):
        """4.2.1 Service Level Connection Initialization."""

        # 4.2.1.1 Supported features exchange
        # First, in the initialization procedure, the HF shall send the
        # AT+BRSF=<HF supported features> command to the AG to both notify
        # the AG of the supported features in the HF, as well as to retrieve the
        # supported features in the AG using the +BRSF result code.
        response = await self.execute_command(
            f"AT+BRSF={self.supported_hf_features}", response_type=AtResponseType.SINGLE
        )

        self.supported_ag_features = int(response.parameters[0])
        logger.info(f"supported AG features: {self.supported_ag_features}")
        for feature in AgFeature:
            if self.supports_ag_feature(feature):
                logger.info(f"  - {feature.name}")

        # 4.2.1.2 Codec Negotiation
        # Secondly, in the initialization procedure, if the HF supports the
        # Codec Negotiation feature, it shall check if the AT+BRSF command
        # response from the AG has indicated that it supports the Codec
        # Negotiation feature.
        if self.supports_hf_feature(
            HfFeature.CODEC_NEGOTIATION
        ) and self.supports_ag_feature(AgFeature.CODEC_NEGOTIATION):
            # If both the HF and AG do support the Codec Negotiation feature
            # then the HF shall send the AT+BAC=<HF available codecs> command to
            # the AG to notify the AG of the available codecs in the HF.
            codecs = [str(c.value) for c in self.supported_audio_codecs]
            await self.execute_command(f"AT+BAC={','.join(codecs)}")

        # 4.2.1.3 AG Indicators
        # After having retrieved the supported features in the AG, the HF shall
        # determine which indicators are supported by the AG, as well as the
        # ordering of the supported indicators. This is because, according to
        # the 3GPP 27.007 specification [2], the AG may support additional
        # indicators not provided for by the Hands-Free Profile, and because the
        # ordering of the indicators is implementation specific. The HF uses
        # the AT+CIND=? Test command to retrieve information about the supported
        # indicators and their ordering.
        response = await self.execute_command(
            "AT+CIND=?", response_type=AtResponseType.SINGLE
        )

        self.ag_indicators = []
        for index, indicator in enumerate(response.parameters):
            description = AgIndicator(indicator[0].decode())
            supported_values = []
            for value in indicator[1]:
                value = value.split(b'-')
                value = [int(v) for v in value]
                value_min = value[0]
                value_max = value[1] if len(value) > 1 else value[0]
                supported_values.extend([v for v in range(value_min, value_max + 1)])

            self.ag_indicators.append(
                AgIndicatorState(description, index, set(supported_values), 0)
            )

        # Once the HF has the necessary supported indicator and ordering
        # information, it shall retrieve the current status of the indicators
        # in the AG using the AT+CIND? Read command.
        response = await self.execute_command(
            "AT+CIND?", response_type=AtResponseType.SINGLE
        )

        for index, indicator in enumerate(response.parameters):
            self.ag_indicators[index].current_status = int(indicator)

        # After having retrieved the status of the indicators in the AG, the HF
        # shall then enable the "Indicators status update" function in the AG by
        # issuing the AT+CMER command, to which the AG shall respond with OK.
        await self.execute_command("AT+CMER=3,,,1")

        if self.supports_hf_feature(
            HfFeature.THREE_WAY_CALLING
        ) and self.supports_ag_feature(AgFeature.THREE_WAY_CALLING):
            # After the HF has enabled the “Indicators status update” function in
            # the AG, and if the “Call waiting and 3-way calling” bit was set in the
            # supported features bitmap by both the HF and the AG, the HF shall
            # issue the AT+CHLD=? test command to retrieve the information about how
            # the call hold and multiparty services are supported in the AG. The HF
            # shall not issue the AT+CHLD=? test command in case either the HF or
            # the AG does not support the "Three-way calling" feature.
            response = await self.execute_command(
                "AT+CHLD=?", response_type=AtResponseType.SINGLE
            )

            self.supported_ag_call_hold_operations = [
                CallHoldOperation(operation.decode())
                for operation in response.parameters[0]
            ]

        # 4.2.1.4 HF Indicators
        # If the HF supports the HF indicator feature, it shall check the +BRSF
        # response to see if the AG also supports the HF Indicator feature.
        if self.supports_hf_feature(
            HfFeature.HF_INDICATORS
        ) and self.supports_ag_feature(AgFeature.HF_INDICATORS):
            # If both the HF and AG support the HF Indicator feature, then the HF
            # shall send the AT+BIND=<HF supported HF indicators> command to the AG
            # to notify the AG of the supported indicators’ assigned numbers in the
            # HF. The AG shall respond with OK
            indicators = [str(i.value) for i in self.hf_indicators]
            await self.execute_command(f"AT+BIND={','.join(indicators)}")

            # After having provided the AG with the HF indicators it supports,
            # the HF shall send the AT+BIND=? to request HF indicators supported
            # by the AG. The AG shall reply with the +BIND response listing all
            # HF indicators that it supports followed by an OK.
            response = await self.execute_command(
                "AT+BIND=?", response_type=AtResponseType.SINGLE
            )

            logger.info("supported HF indicators:")
            for indicator in response.parameters[0]:
                indicator = HfIndicator(int(indicator))
                logger.info(f"  - {indicator.name}")
                if indicator in self.hf_indicators:
                    self.hf_indicators[indicator].supported = True

            # Once the HF receives the supported HF indicators list from the AG,
            # the HF shall send the AT+BIND? command to determine which HF
            # indicators are enabled. The AG shall respond with one or more
            # +BIND responses. The AG shall terminate the list with OK.
            # (See Section 4.36.1.3).
            responses = await self.execute_command(
                "AT+BIND?", response_type=AtResponseType.MULTIPLE
            )

            logger.info("enabled HF indicators:")
            for response in responses:
                indicator = HfIndicator(int(response.parameters[0]))
                enabled = int(response.parameters[1]) != 0
                logger.info(f"  - {indicator.name}: {enabled}")
                if indicator in self.hf_indicators:
                    self.hf_indicators[indicator].enabled = True

        logger.info("SLC setup completed")
        self._slc_initialized = True

    async def setup_audio_connection(self):
        """4.11.2 Audio Connection Setup by HF."""

        # When the HF triggers the establishment of the Codec Connection it
        # shall send the AT command AT+BCC to the AG. The AG shall respond with
        # OK if it will start the Codec Connection procedure, and with ERROR
        # if it cannot start the Codec Connection procedure.
        await self.execute_command("AT+BCC")

    async def setup_codec_connection(self, codec_id: int):
        """4.11.3 Codec Connection Setup."""
        # The AG shall send a +BCS=<Codec ID> unsolicited response to the HF.
        # The HF shall then respond to the incoming unsolicited response with
        # the AT command AT+BCS=<Codec ID>. The ID shall be the same as in the
        # unsolicited response code as long as the ID is supported.
        # If the received ID is not available, the HF shall respond with
        # AT+BAC with its available codecs.
        if codec_id not in self.supported_audio_codecs:
            codecs = [str(c) for c in self.supported_audio_codecs]
            await self.execute_command(f"AT+BAC={','.join(codecs)}")
            return

        await self.execute_command(f"AT+BCS={codec_id}")

        # After sending the OK response, the AG shall open the
        # Synchronous Connection with the settings that are determined by the
        # ID. The HF shall be ready to accept the synchronous connection
        # establishment as soon as it has sent the AT commands AT+BCS=<Codec ID>.
        self.active_codec = AudioCodec(codec_id)
        self.emit(self.EVENT_CODEC_NEGOTIATION, self.active_codec)

        logger.info("codec connection setup completed")

    async def answer_incoming_call(self):
        """4.13.1 Answer Incoming Call from the HF - In-Band Ringing."""
        # The user accepts the incoming voice call by using the proper means
        # provided by the HF. The HF shall then send the ATA command
        # (see Section 4.34) to the AG. The AG shall then begin the procedure for
        # accepting the incoming call.
        await self.execute_command("ATA")

    async def reject_incoming_call(self):
        """4.14.1 Reject an Incoming Call from the HF."""
        # The user rejects the incoming call by using the User Interface on the
        # Hands-Free unit. The HF shall then send the AT+CHUP command
        # (see Section 4.34) to the AG. This may happen at any time during the
        # procedures described in Sections 4.13.1 and 4.13.2.
        await self.execute_command("AT+CHUP")

    async def terminate_call(self):
        """4.15.1 Terminate a Call Process from the HF."""
        # The user may abort the ongoing call process using whatever means
        # provided by the Hands-Free unit. The HF shall send AT+CHUP command
        # (see Section 4.34) to the AG, and the AG shall then start the
        # procedure to terminate or interrupt the current call procedure.
        # The AG shall then send the OK indication followed by the +CIEV result
        # code, with the value indicating (call=0).
        await self.execute_command("AT+CHUP")

    async def query_current_calls(self) -> list[CallInfo]:
        """4.32.1 Query List of Current Calls in AG.

        Return:
            List of current calls in AG.
        """
        responses = await self.execute_command(
            "AT+CLCC", response_type=AtResponseType.MULTIPLE
        )
        assert isinstance(responses, list)

        calls = []
        for response in responses:
            call_info = CallInfo(
                index=int(response.parameters[0]),
                direction=CallInfoDirection(int(response.parameters[1])),
                status=CallInfoStatus(int(response.parameters[2])),
                mode=CallInfoMode(int(response.parameters[3])),
                multi_party=CallInfoMultiParty(int(response.parameters[4])),
            )
            if len(response.parameters) >= 6:
                call_info.number = response.parameters[5].decode()
            if len(response.parameters) >= 7:
                call_info.type = int(response.parameters[6])
            calls.append(call_info)
        return calls

    async def update_ag_indicator(self, index: int, value: int):
        # CIEV is in 1-index, while ag_indicators is in 0-index.
        ag_indicator = self.ag_indicators[index - 1]
        ag_indicator.current_status = value
        self.emit(self.EVENT_AG_INDICATOR, ag_indicator)
        logger.info(f"AG indicator updated: {ag_indicator.indicator}, {value}")

    async def handle_unsolicited(self):
        """Handle unsolicited result codes sent by the audio gateway."""
        result = await self.unsolicited_queue.get()
        if not result:
            raise HfProtocol.HfLoopTermination()
        if result.code == "+BCS":
            await self.setup_codec_connection(int(result.parameters[0]))
        elif result.code == "+CIEV":
            await self.update_ag_indicator(
                int(result.parameters[0]), int(result.parameters[1])
            )
        elif result.code == "+VGS":
            self.emit(self.EVENT_SPEAKER_VOLUME, int(result.parameters[0]))
        elif result.code == "+VGM":
            self.emit(self.EVENT_MICROPHONE_VOLUME, int(result.parameters[0]))
        elif result.code == "RING":
            self.emit(self.EVENT_RING)
        elif result.code == "+CLIP":
            self.emit(
                self.EVENT_CLI_NOTIFICATION,
                CallLineIdentification.parse_from(result.parameters),
            )
        elif result.code == "+BVRA":
            # TODO: Support Enhanced Voice Recognition.
            self.emit(
                self.EVENT_VOICE_RECOGNITION,
                VoiceRecognitionState(int(result.parameters[0])),
            )
        else:
            logging.info(f"unhandled unsolicited response {result.code}")

    async def run(self):
        """
        Main routine for the Hands-Free side of the HFP protocol.

        Initiates the service level connection then loops handling unsolicited AG responses.
        """

        try:
            if not self._slc_initialized:
                await self.initiate_slc()
            while True:
                await self.handle_unsolicited()
        except HfProtocol.HfLoopTermination:
            logger.info('Loop terminated')
        except Exception:
            logger.error("HFP-HF protocol failed with the following error:")
            logger.error(traceback.format_exc())


class AgProtocol(utils.EventEmitter):
    """
    Implementation for the Audio-Gateway side of the Hands-Free profile.

    Reference specification Hands-Free Profile v1.8.

    Emitted events:
        slc_complete: Emit when SLC procedure is completed.
        codec_negotiation: When codec is renegotiated, notify the new codec.
            Args:
                active_codec: AudioCodec
        hf_indicator: When HF update their indicators, notify the new state.
            Args:
                hf_indicator: HfIndicatorState
        codec_connection_request: Emit when HF sends AT+BCC to request codec connection.
        answer: Emit when HF sends ATA to answer phone call.
        hang_up: Emit when HF sends AT+CHUP to hang up phone call.
        dial: Emit when HF sends ATD to dial phone call.
        voice_recognition: Emit when HF requests voice recognition state.
            Args:
                vrec: VoiceRecognitionState
        call_hold: Emit when HF requests call hold operation.
            Args:
                operation: CallHoldOperation
                call_index: Optional[int]
        speaker_volume: Emitted when AG update speaker volume autonomously.
            Args:
                volume: Int
        microphone_volume: Emitted when AG update microphone volume autonomously.
            Args:
                volume: Int
    """

    EVENT_SLC_COMPLETE = "slc_complete"
    EVENT_SUPPORTED_AUDIO_CODECS = "supported_audio_codecs"
    EVENT_CODEC_NEGOTIATION = "codec_negotiation"
    EVENT_VOICE_RECOGNITION = "voice_recognition"
    EVENT_CALL_HOLD = "call_hold"
    EVENT_HF_INDICATOR = "hf_indicator"
    EVENT_CODEC_CONNECTION_REQUEST = "codec_connection_request"
    EVENT_ANSWER = "answer"
    EVENT_DIAL = "dial"
    EVENT_HANG_UP = "hang_up"
    EVENT_SPEAKER_VOLUME = "speaker_volume"
    EVENT_MICROPHONE_VOLUME = "microphone_volume"

    supported_hf_features: int
    supported_hf_indicators: set[HfIndicator]
    supported_audio_codecs: list[AudioCodec]

    supported_ag_features: int
    supported_ag_call_hold_operations: list[CallHoldOperation]

    ag_indicators: list[AgIndicatorState]
    hf_indicators: collections.OrderedDict[HfIndicator, HfIndicatorState]

    dlc: rfcomm.DLC

    read_buffer: bytearray
    active_codec: AudioCodec
    calls: list[CallInfo]

    indicator_report_enabled: bool
    inband_ringtone_enabled: bool
    cme_error_enabled: bool
    cli_notification_enabled: bool
    call_waiting_enabled: bool
    _remained_slc_setup_features: set[HfFeature]

    def __init__(self, dlc: rfcomm.DLC, configuration: AgConfiguration) -> None:
        super().__init__()

        # Configure internal state.
        self.dlc = dlc
        self.read_buffer = bytearray()
        self.active_codec = AudioCodec.CVSD
        self.calls = []

        # Build local features.
        self.supported_ag_features = sum(configuration.supported_ag_features)
        self.supported_ag_call_hold_operations = list(
            configuration.supported_ag_call_hold_operations
        )
        self.ag_indicators = list(configuration.supported_ag_indicators)
        self.supported_hf_indicators = set(configuration.supported_hf_indicators)
        self.inband_ringtone_enabled = True
        self._remained_slc_setup_features = set()

        # Clear remote features.
        self.supported_hf_features = 0
        self.supported_audio_codecs = []
        self.indicator_report_enabled = False
        self.cme_error_enabled = False
        self.cli_notification_enabled = False
        self.call_waiting_enabled = False

        self.hf_indicators = collections.OrderedDict()

        # Bind the AT reader to the RFCOMM channel.
        self.dlc.sink = self._read_at

    def supports_hf_feature(self, feature: HfFeature) -> bool:
        return (self.supported_hf_features & feature) != 0

    def supports_ag_feature(self, feature: AgFeature) -> bool:
        return (self.supported_ag_features & feature) != 0

    def _read_at(self, data: bytes):
        """
        Reads AT messages from the RFCOMM channel.
        """
        # Append to the read buffer.
        self.read_buffer.extend(data)

        while self.read_buffer:
            # Locate the trailer.
            trailer = self.read_buffer.find(b'\r')
            if trailer == -1:
                return

            # Isolate the AT response code and parameters.
            raw_command = self.read_buffer[:trailer]
            command = AtCommand.parse_from(raw_command)
            logger.debug(f"<<< {raw_command.decode()}")

            # Consume the response bytes.
            self.read_buffer = self.read_buffer[trailer + 1 :]

            if command.sub_code == AtCommand.SubCode.TEST:
                handler_name = f'_on_{command.code.lower()}_test'
            elif command.sub_code == AtCommand.SubCode.READ:
                handler_name = f'_on_{command.code.lower()}_read'
            else:
                handler_name = f'_on_{command.code.lower()}'

            if handler := getattr(self, handler_name, None):
                handler(*command.parameters)
            else:
                logger.warning('Handler %s not found', handler_name)
                self.send_response('ERROR')

    def send_response(self, response: str) -> None:
        """Sends an AT response."""
        self.dlc.write(f'\r\n{response}\r\n')

    def send_cme_error(self, error_code: CmeError) -> None:
        """Sends an CME ERROR response.

        If CME Error is not enabled by HF, sends ERROR instead.
        """
        if self.cme_error_enabled:
            self.send_response(f'+CME ERROR: {error_code.value}')
        else:
            self.send_error()

    def send_ok(self) -> None:
        """Sends an OK response."""
        self.send_response('OK')

    def send_error(self) -> None:
        """Sends an ERROR response."""
        self.send_response('ERROR')

    def set_inband_ringtone_enabled(self, enabled: bool) -> None:
        """Enables or disables in-band ringtone."""

        self.inband_ringtone_enabled = enabled
        self.send_response(f'+BSIR: {1 if enabled else 0}')

    def set_speaker_volume(self, level: int) -> None:
        """Reports speaker volume."""

        self.send_response(f'+VGS: {level}')

    def set_microphone_volume(self, level: int) -> None:
        """Reports microphone volume."""

        self.send_response(f'+VGM: {level}')

    def send_ring(self) -> None:
        """Sends RING command to trigger ringtone on HF."""

        self.send_response('RING')

    def update_ag_indicator(self, indicator: AgIndicator, value: int) -> None:
        """Updates AG indicator.

        Args:
            indicator: Name of the indicator.
            value: new value of the indicator.
        """

        search_result = next(
            (
                (index, state)
                for index, state in enumerate(self.ag_indicators)
                if state.indicator == indicator
            ),
            None,
        )
        if not search_result:
            raise KeyError(f'{indicator} is not supported.')

        index, indicator_state = search_result
        if not self.indicator_report_enabled:
            logger.warning('AG indicator report is disabled')
        if not indicator_state.enabled:
            logger.warning(f'AG indicator {indicator} is disabled')

        indicator_state.current_status = value
        self.send_response(f'+CIEV: {index + 1},{value}')

    async def negotiate_codec(self, codec: AudioCodec) -> None:
        """Starts codec negotiation."""

        if not self.supports_ag_feature(AgFeature.CODEC_NEGOTIATION):
            logger.warning('Local does not support Codec Negotiation')
        if not self.supports_hf_feature(HfFeature.CODEC_NEGOTIATION):
            logger.warning('Peer does not support Codec Negotiation')
        if codec not in self.supported_audio_codecs:
            logger.warning(f'{codec} is not supported by peer')

        at_bcs_future = asyncio.get_running_loop().create_future()
        self.once('codec_negotiation', at_bcs_future.set_result)
        self.send_response(f'+BCS: {codec.value}')
        if (new_codec := await at_bcs_future) != codec:
            raise HfpProtocolError(f'Expect codec: {codec}, but get {new_codec}')

    def send_cli_notification(self, cli: CallLineIdentification) -> None:
        """Sends +CLIP CLI notification."""

        if not self.cli_notification_enabled:
            logger.warning('Try to send CLIP while CLI notification is not enabled')

        self.send_response(f'+CLIP: {cli.to_clip_string()}')

    def _check_remained_slc_commands(self) -> None:
        if not self._remained_slc_setup_features:
            self.emit(self.EVENT_SLC_COMPLETE)

    def _on_brsf(self, hf_features: bytes) -> None:
        self.supported_hf_features = int(hf_features)
        self.send_response(f'+BRSF: {self.supported_ag_features}')
        self.send_ok()

        if self.supports_hf_feature(
            HfFeature.HF_INDICATORS
        ) and self.supports_ag_feature(AgFeature.HF_INDICATORS):
            self._remained_slc_setup_features.add(HfFeature.HF_INDICATORS)

        if self.supports_hf_feature(
            HfFeature.THREE_WAY_CALLING
        ) and self.supports_ag_feature(AgFeature.THREE_WAY_CALLING):
            self._remained_slc_setup_features.add(HfFeature.THREE_WAY_CALLING)

    def _on_bac(self, *args) -> None:
        self.supported_audio_codecs = [AudioCodec(int(value)) for value in args]
        self.emit(self.EVENT_SUPPORTED_AUDIO_CODECS, self.supported_audio_codecs)
        self.send_ok()

    def _on_bcs(self, codec: bytes) -> None:
        self.active_codec = AudioCodec(int(codec))
        self.send_ok()
        self.emit(self.EVENT_CODEC_NEGOTIATION, self.active_codec)

    def _on_bvra(self, vrec: bytes) -> None:
        self.send_ok()
        self.emit(self.EVENT_VOICE_RECOGNITION, VoiceRecognitionState(int(vrec)))

    def _on_chld(self, operation_code: bytes) -> None:
        call_index: int | None = None
        if len(operation_code) > 1:
            call_index = int(operation_code[1:])
            operation_code = operation_code[:1] + b'x'
        try:
            operation = CallHoldOperation(operation_code.decode())
        except Exception:
            logger.error(f'Invalid operation: {operation_code.decode()}')
            self.send_cme_error(CmeError.OPERATION_NOT_SUPPORTED)
            return

        if operation not in self.supported_ag_call_hold_operations:
            logger.error(f'Unsupported operation: {operation_code.decode()}')
            self.send_cme_error(CmeError.OPERATION_NOT_SUPPORTED)

        if call_index is not None and not any(
            call.index == call_index for call in self.calls
        ):
            logger.error(f'No matching call {call_index}')
            self.send_cme_error(CmeError.INVALID_INDEX)

        # Real three-way calls have more complicated situations, but this is not a popular issue - let users to handle the remaining :)

        self.send_ok()
        self.emit(self.EVENT_CALL_HOLD, operation, call_index)

    def _on_chld_test(self) -> None:
        if not self.supports_ag_feature(AgFeature.THREE_WAY_CALLING):
            self.send_error()
            return

        self.send_response(
            '+CHLD: ({})'.format(
                ','.join(
                    operation.value
                    for operation in self.supported_ag_call_hold_operations
                )
            )
        )
        self.send_ok()
        self._remained_slc_setup_features.remove(HfFeature.THREE_WAY_CALLING)
        self._check_remained_slc_commands()

    def _on_cind_test(self) -> None:
        if not self.ag_indicators:
            self.send_cme_error(CmeError.NOT_FOUND)
            return

        indicator_list_str = ",".join(
            indicator.on_test_text for indicator in self.ag_indicators
        )
        self.send_response(f'+CIND: {indicator_list_str}')
        self.send_ok()

    def _on_cind_read(self) -> None:
        if not self.ag_indicators:
            self.send_cme_error(CmeError.NOT_FOUND)
            return

        indicator_list_str = ",".join(
            str(indicator.current_status) for indicator in self.ag_indicators
        )
        self.send_response(f'+CIND: {indicator_list_str}')
        self.send_ok()

        self._check_remained_slc_commands()

    def _on_cmer(
        self,
        mode: bytes,
        keypad: bytes | None = None,
        display: bytes | None = None,
        indicator: bytes = b'',
    ) -> None:
        if (
            int(mode) != 3
            or (keypad and int(keypad))
            or (display and int(display))
            or int(indicator) not in (0, 1)
        ):
            logger.error(
                f'Unexpected values: mode={mode!r}, keypad={keypad!r}, '
                f'display={display!r}, indicator={indicator!r}'
            )
            self.send_cme_error(CmeError.INVALID_INDEX)

        self.indicator_report_enabled = bool(int(indicator))
        self.send_ok()

    def _on_cmee(self, enabled: bytes) -> None:
        self.cme_error_enabled = bool(int(enabled))
        self.send_ok()

    def _on_ccwa(self, enabled: bytes) -> None:
        self.call_waiting_enabled = bool(int(enabled))
        self.send_ok()

    def _on_bind(self, *args) -> None:
        if not self.supports_ag_feature(AgFeature.HF_INDICATORS):
            self.send_error()
            return

        peer_supported_indicators = set(
            HfIndicator(int(indicator)) for indicator in args
        )
        self.hf_indicators = collections.OrderedDict(
            {
                indicator: HfIndicatorState(indicator=indicator)
                for indicator in self.supported_hf_indicators.intersection(
                    peer_supported_indicators
                )
            }
        )
        self.send_ok()

    def _on_bind_test(self) -> None:
        if not self.supports_ag_feature(AgFeature.HF_INDICATORS):
            self.send_error()
            return

        hf_indicator_list_str = ",".join(
            str(indicator.value) for indicator in self.supported_hf_indicators
        )
        self.send_response(f'+BIND: ({hf_indicator_list_str})')
        self.send_ok()

    def _on_bind_read(self) -> None:
        if not self.supports_ag_feature(AgFeature.HF_INDICATORS):
            self.send_error()
            return

        for indicator in self.hf_indicators:
            self.send_response(f'+BIND: {indicator.value},1')

        self.send_ok()

        self._remained_slc_setup_features.remove(HfFeature.HF_INDICATORS)
        self._check_remained_slc_commands()

    def _on_biev(self, index_bytes: bytes, value_bytes: bytes) -> None:
        if not self.supports_ag_feature(AgFeature.HF_INDICATORS):
            self.send_error()
            return

        index = HfIndicator(int(index_bytes))
        if index not in self.hf_indicators:
            self.send_error()
            return

        self.hf_indicators[index].current_status = int(value_bytes)
        self.emit(self.EVENT_HF_INDICATOR, self.hf_indicators[index])
        self.send_ok()

    def _on_bia(self, *args) -> None:
        for enabled, state in zip(args, self.ag_indicators):
            state.enabled = bool(int(enabled))
        self.send_ok()

    def _on_bcc(self) -> None:
        self.emit(self.EVENT_CODEC_CONNECTION_REQUEST)
        self.send_ok()

    def _on_a(self) -> None:
        """ATA handler."""
        self.emit(self.EVENT_ANSWER)
        self.send_ok()

    def _on_d(self, number: bytes) -> None:
        """ATD handler."""
        self.emit(self.EVENT_DIAL, number.decode())
        self.send_ok()

    def _on_chup(self) -> None:
        self.emit(self.EVENT_HANG_UP)
        self.send_ok()

    def _on_clcc(self) -> None:
        for call in self.calls:
            number_text = f',"{call.number}"' if call.number is not None else ''
            type_text = f',{call.type}' if call.type is not None else ''
            response = (
                f'+CLCC: {call.index}'
                f',{call.direction.value}'
                f',{call.status.value}'
                f',{call.mode.value}'
                f',{call.multi_party.value}'
                f'{number_text}'
                f'{type_text}'
            )
            self.send_response(response)
        self.send_ok()

    def _on_clip(self, enabled: bytes) -> None:
        if not self.supports_hf_feature(HfFeature.CLI_PRESENTATION_CAPABILITY):
            logger.error('Remote doesn not support CLI but sends AT+CLIP')
        self.cli_notification_enabled = True if enabled == b'1' else False
        self.send_ok()

    def _on_vgs(self, level: bytes) -> None:
        self.emit(self.EVENT_SPEAKER_VOLUME, int(level))
        self.send_ok()

    def _on_vgm(self, level: bytes) -> None:
        self.emit(self.EVENT_MICROPHONE_VOLUME, int(level))
        self.send_ok()


# -----------------------------------------------------------------------------
# Normative SDP definitions
# -----------------------------------------------------------------------------


class ProfileVersion(enum.IntEnum):
    """
    Profile version (normative).

    Hands-Free Profile v1.8, 6.3 SDP Interoperability Requirements.
    """

    V1_5 = 0x0105
    V1_6 = 0x0106
    V1_7 = 0x0107
    V1_8 = 0x0108
    V1_9 = 0x0109


class HfSdpFeature(enum.IntFlag):
    """
    HF supported features (normative).

    Hands-Free Profile v1.9, 6.3 SDP Interoperability Requirements.
    """

    EC_NR = 0x01  # Echo Cancel & Noise reduction
    THREE_WAY_CALLING = 0x02
    CLI_PRESENTATION_CAPABILITY = 0x04
    VOICE_RECOGNITION_ACTIVATION = 0x08
    REMOTE_VOLUME_CONTROL = 0x10
    WIDE_BAND_SPEECH = 0x20
    ENHANCED_VOICE_RECOGNITION_STATUS = 0x40
    VOICE_RECOGNITION_TEXT = 0x80
    SUPER_WIDE_BAND = 0x100


class AgSdpFeature(enum.IntFlag):
    """
    AG supported features (normative).

    Hands-Free Profile v1.9, 6.3 SDP Interoperability Requirements.
    """

    THREE_WAY_CALLING = 0x01
    EC_NR = 0x02  # Echo Cancel & Noise reduction
    VOICE_RECOGNITION_FUNCTION = 0x04
    IN_BAND_RING_TONE_CAPABILITY = 0x08
    VOICE_TAG = 0x10  # Attach a number to voice tag
    WIDE_BAND_SPEECH = 0x20
    ENHANCED_VOICE_RECOGNITION_STATUS = 0x40
    VOICE_RECOGNITION_TEXT = 0x80
    SUPER_WIDE_BAND_SPEED_SPEECH = 0x100


def make_hf_sdp_records(
    service_record_handle: int,
    rfcomm_channel: int,
    configuration: HfConfiguration,
    version: ProfileVersion = ProfileVersion.V1_8,
) -> list[sdp.ServiceAttribute]:
    """
    Generates the SDP record for HFP Hands-Free support.

    The record exposes the features supported in the input configuration,
    and the allocated RFCOMM channel.
    """

    hf_supported_features = 0

    if HfFeature.EC_NR in configuration.supported_hf_features:
        hf_supported_features |= HfSdpFeature.EC_NR
    if HfFeature.THREE_WAY_CALLING in configuration.supported_hf_features:
        hf_supported_features |= HfSdpFeature.THREE_WAY_CALLING
    if HfFeature.CLI_PRESENTATION_CAPABILITY in configuration.supported_hf_features:
        hf_supported_features |= HfSdpFeature.CLI_PRESENTATION_CAPABILITY
    if HfFeature.VOICE_RECOGNITION_ACTIVATION in configuration.supported_hf_features:
        hf_supported_features |= HfSdpFeature.VOICE_RECOGNITION_ACTIVATION
    if HfFeature.REMOTE_VOLUME_CONTROL in configuration.supported_hf_features:
        hf_supported_features |= HfSdpFeature.REMOTE_VOLUME_CONTROL
    if (
        HfFeature.ENHANCED_VOICE_RECOGNITION_STATUS
        in configuration.supported_hf_features
    ):
        hf_supported_features |= HfSdpFeature.ENHANCED_VOICE_RECOGNITION_STATUS
    if HfFeature.VOICE_RECOGNITION_TEXT in configuration.supported_hf_features:
        hf_supported_features |= HfSdpFeature.VOICE_RECOGNITION_TEXT

    if AudioCodec.MSBC in configuration.supported_audio_codecs:
        hf_supported_features |= HfSdpFeature.WIDE_BAND_SPEECH

    return [
        sdp.ServiceAttribute(
            sdp.SDP_SERVICE_RECORD_HANDLE_ATTRIBUTE_ID,
            sdp.DataElement.unsigned_integer_32(service_record_handle),
        ),
        sdp.ServiceAttribute(
            sdp.SDP_SERVICE_CLASS_ID_LIST_ATTRIBUTE_ID,
            sdp.DataElement.sequence(
                [
                    sdp.DataElement.uuid(BT_HANDSFREE_SERVICE),
                    sdp.DataElement.uuid(BT_GENERIC_AUDIO_SERVICE),
                ]
            ),
        ),
        sdp.ServiceAttribute(
            sdp.SDP_PROTOCOL_DESCRIPTOR_LIST_ATTRIBUTE_ID,
            sdp.DataElement.sequence(
                [
                    sdp.DataElement.sequence(
                        [sdp.DataElement.uuid(BT_L2CAP_PROTOCOL_ID)]
                    ),
                    sdp.DataElement.sequence(
                        [
                            sdp.DataElement.uuid(BT_RFCOMM_PROTOCOL_ID),
                            sdp.DataElement.unsigned_integer_8(rfcomm_channel),
                        ]
                    ),
                ]
            ),
        ),
        sdp.ServiceAttribute(
            sdp.SDP_BLUETOOTH_PROFILE_DESCRIPTOR_LIST_ATTRIBUTE_ID,
            sdp.DataElement.sequence(
                [
                    sdp.DataElement.sequence(
                        [
                            sdp.DataElement.uuid(BT_HANDSFREE_SERVICE),
                            sdp.DataElement.unsigned_integer_16(version),
                        ]
                    )
                ]
            ),
        ),
        sdp.ServiceAttribute(
            sdp.SDP_SUPPORTED_FEATURES_ATTRIBUTE_ID,
            sdp.DataElement.unsigned_integer_16(hf_supported_features),
        ),
    ]


def make_ag_sdp_records(
    service_record_handle: int,
    rfcomm_channel: int,
    configuration: AgConfiguration,
    version: ProfileVersion = ProfileVersion.V1_8,
) -> list[sdp.ServiceAttribute]:
    """
    Generates the SDP record for HFP Audio-Gateway support.

    The record exposes the features supported in the input configuration,
    and the allocated RFCOMM channel.
    """

    ag_supported_features = 0

    if AgFeature.EC_NR in configuration.supported_ag_features:
        ag_supported_features |= AgSdpFeature.EC_NR
    if AgFeature.THREE_WAY_CALLING in configuration.supported_ag_features:
        ag_supported_features |= AgSdpFeature.THREE_WAY_CALLING
    if (
        AgFeature.ENHANCED_VOICE_RECOGNITION_STATUS
        in configuration.supported_ag_features
    ):
        ag_supported_features |= AgSdpFeature.ENHANCED_VOICE_RECOGNITION_STATUS
    if AgFeature.VOICE_RECOGNITION_TEXT in configuration.supported_ag_features:
        ag_supported_features |= AgSdpFeature.VOICE_RECOGNITION_TEXT
    if AgFeature.IN_BAND_RING_TONE_CAPABILITY in configuration.supported_ag_features:
        ag_supported_features |= AgSdpFeature.IN_BAND_RING_TONE_CAPABILITY
    if AgFeature.VOICE_RECOGNITION_FUNCTION in configuration.supported_ag_features:
        ag_supported_features |= AgSdpFeature.VOICE_RECOGNITION_FUNCTION
    if AudioCodec.MSBC in configuration.supported_audio_codecs:
        ag_supported_features |= AgSdpFeature.WIDE_BAND_SPEECH

    return [
        sdp.ServiceAttribute(
            sdp.SDP_SERVICE_RECORD_HANDLE_ATTRIBUTE_ID,
            sdp.DataElement.unsigned_integer_32(service_record_handle),
        ),
        sdp.ServiceAttribute(
            sdp.SDP_SERVICE_CLASS_ID_LIST_ATTRIBUTE_ID,
            sdp.DataElement.sequence(
                [
                    sdp.DataElement.uuid(BT_HANDSFREE_AUDIO_GATEWAY_SERVICE),
                    sdp.DataElement.uuid(BT_GENERIC_AUDIO_SERVICE),
                ]
            ),
        ),
        sdp.ServiceAttribute(
            sdp.SDP_PROTOCOL_DESCRIPTOR_LIST_ATTRIBUTE_ID,
            sdp.DataElement.sequence(
                [
                    sdp.DataElement.sequence(
                        [sdp.DataElement.uuid(BT_L2CAP_PROTOCOL_ID)]
                    ),
                    sdp.DataElement.sequence(
                        [
                            sdp.DataElement.uuid(BT_RFCOMM_PROTOCOL_ID),
                            sdp.DataElement.unsigned_integer_8(rfcomm_channel),
                        ]
                    ),
                ]
            ),
        ),
        sdp.ServiceAttribute(
            sdp.SDP_BLUETOOTH_PROFILE_DESCRIPTOR_LIST_ATTRIBUTE_ID,
            sdp.DataElement.sequence(
                [
                    sdp.DataElement.sequence(
                        [
                            sdp.DataElement.uuid(BT_HANDSFREE_AUDIO_GATEWAY_SERVICE),
                            sdp.DataElement.unsigned_integer_16(version),
                        ]
                    )
                ]
            ),
        ),
        sdp.ServiceAttribute(
            sdp.SDP_SUPPORTED_FEATURES_ATTRIBUTE_ID,
            sdp.DataElement.unsigned_integer_16(ag_supported_features),
        ),
    ]


async def find_hf_sdp_record(
    connection: device.Connection,
) -> tuple[int, ProfileVersion, HfSdpFeature] | None:
    """Searches a Hands-Free SDP record from remote device.

    Args:
        connection: ACL connection to make SDP search.

    Returns:
        Tuple of (<RFCOMM channel>, <Profile Version>, <HF SDP features>)
    """
    async with sdp.Client(connection) as sdp_client:
        search_result = await sdp_client.search_attributes(
            uuids=[BT_HANDSFREE_SERVICE],
            attribute_ids=[
                sdp.SDP_PROTOCOL_DESCRIPTOR_LIST_ATTRIBUTE_ID,
                sdp.SDP_BLUETOOTH_PROFILE_DESCRIPTOR_LIST_ATTRIBUTE_ID,
                sdp.SDP_SUPPORTED_FEATURES_ATTRIBUTE_ID,
                sdp.SDP_SERVICE_CLASS_ID_LIST_ATTRIBUTE_ID,
            ],
        )
        for attribute_lists in search_result:
            channel: int | None = None
            version: ProfileVersion | None = None
            features: HfSdpFeature | None = None
            for attribute in attribute_lists:
                # The layout is [[L2CAP_PROTOCOL], [RFCOMM_PROTOCOL, RFCOMM_CHANNEL]].
                if attribute.id == sdp.SDP_PROTOCOL_DESCRIPTOR_LIST_ATTRIBUTE_ID:
                    protocol_descriptor_list = attribute.value.value
                    channel = protocol_descriptor_list[1].value[1].value
                elif (
                    attribute.id
                    == sdp.SDP_BLUETOOTH_PROFILE_DESCRIPTOR_LIST_ATTRIBUTE_ID
                ):
                    profile_descriptor_list = attribute.value.value
                    version = ProfileVersion(profile_descriptor_list[0].value[1].value)
                elif attribute.id == sdp.SDP_SUPPORTED_FEATURES_ATTRIBUTE_ID:
                    features = HfSdpFeature(attribute.value.value)
                elif attribute.id == sdp.SDP_SERVICE_CLASS_ID_LIST_ATTRIBUTE_ID:
                    class_id_list = attribute.value.value
                    uuid = class_id_list[0].value
                    # AG record may also contain HF UUID in its profile descriptor list.
                    # If found, skip this record.
                    if uuid == BT_HANDSFREE_AUDIO_GATEWAY_SERVICE:
                        channel, version, features = (None, None, None)
                        break

            if channel is not None and version is not None and features is not None:
                return (channel, version, features)
    return None


async def find_ag_sdp_record(
    connection: device.Connection,
) -> tuple[int, ProfileVersion, AgSdpFeature] | None:
    """Searches an Audio-Gateway SDP record from remote device.

    Args:
        connection: ACL connection to make SDP search.

    Returns:
        Tuple of (<RFCOMM channel>, <Profile Version>, <AG SDP features>)
    """
    async with sdp.Client(connection) as sdp_client:
        search_result = await sdp_client.search_attributes(
            uuids=[BT_HANDSFREE_AUDIO_GATEWAY_SERVICE],
            attribute_ids=[
                sdp.SDP_PROTOCOL_DESCRIPTOR_LIST_ATTRIBUTE_ID,
                sdp.SDP_BLUETOOTH_PROFILE_DESCRIPTOR_LIST_ATTRIBUTE_ID,
                sdp.SDP_SUPPORTED_FEATURES_ATTRIBUTE_ID,
            ],
        )
        for attribute_lists in search_result:
            channel: int | None = None
            version: ProfileVersion | None = None
            features: AgSdpFeature | None = None
            for attribute in attribute_lists:
                # The layout is [[L2CAP_PROTOCOL], [RFCOMM_PROTOCOL, RFCOMM_CHANNEL]].
                if attribute.id == sdp.SDP_PROTOCOL_DESCRIPTOR_LIST_ATTRIBUTE_ID:
                    protocol_descriptor_list = attribute.value.value
                    channel = protocol_descriptor_list[1].value[1].value
                elif (
                    attribute.id
                    == sdp.SDP_BLUETOOTH_PROFILE_DESCRIPTOR_LIST_ATTRIBUTE_ID
                ):
                    profile_descriptor_list = attribute.value.value
                    version = ProfileVersion(profile_descriptor_list[0].value[1].value)
                elif attribute.id == sdp.SDP_SUPPORTED_FEATURES_ATTRIBUTE_ID:
                    features = AgSdpFeature(attribute.value.value)
            if not channel or not version or features is None:
                logger.warning(f"Bad result {attribute_lists}.")
                return None
            return (channel, version, features)
    return None


# -----------------------------------------------------------------------------
# ESCO Codec Default Parameters
# -----------------------------------------------------------------------------


# Hands-Free Profile v1.8, 5.7 Codec Interoperability Requirements
class DefaultCodecParameters(enum.IntEnum):
    SCO_CVSD_D0 = enum.auto()
    SCO_CVSD_D1 = enum.auto()
    ESCO_CVSD_S1 = enum.auto()
    ESCO_CVSD_S2 = enum.auto()
    ESCO_CVSD_S3 = enum.auto()
    ESCO_CVSD_S4 = enum.auto()
    ESCO_MSBC_T1 = enum.auto()
    ESCO_MSBC_T2 = enum.auto()


@dataclasses.dataclass
class EscoParameters:
    # Codec specific
    transmit_coding_format: CodingFormat
    receive_coding_format: CodingFormat
    packet_type: HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType
    retransmission_effort: (
        HCI_Enhanced_Setup_Synchronous_Connection_Command.RetransmissionEffort
    )
    max_latency: int

    # Common
    input_coding_format: CodingFormat = CodingFormat(CodecID.LINEAR_PCM)
    output_coding_format: CodingFormat = CodingFormat(CodecID.LINEAR_PCM)
    input_coded_data_size: int = 16
    output_coded_data_size: int = 16
    input_pcm_data_format: (
        HCI_Enhanced_Setup_Synchronous_Connection_Command.PcmDataFormat
    ) = HCI_Enhanced_Setup_Synchronous_Connection_Command.PcmDataFormat.TWOS_COMPLEMENT
    output_pcm_data_format: (
        HCI_Enhanced_Setup_Synchronous_Connection_Command.PcmDataFormat
    ) = HCI_Enhanced_Setup_Synchronous_Connection_Command.PcmDataFormat.TWOS_COMPLEMENT
    input_pcm_sample_payload_msb_position: int = 0
    output_pcm_sample_payload_msb_position: int = 0
    input_data_path: HCI_Enhanced_Setup_Synchronous_Connection_Command.DataPath = (
        HCI_Enhanced_Setup_Synchronous_Connection_Command.DataPath.HCI
    )
    output_data_path: HCI_Enhanced_Setup_Synchronous_Connection_Command.DataPath = (
        HCI_Enhanced_Setup_Synchronous_Connection_Command.DataPath.HCI
    )
    input_transport_unit_size: int = 0
    output_transport_unit_size: int = 0
    input_bandwidth: int = 16000
    output_bandwidth: int = 16000
    transmit_bandwidth: int = 8000
    receive_bandwidth: int = 8000
    transmit_codec_frame_size: int = 60
    receive_codec_frame_size: int = 60

    def asdict(self) -> dict[str, Any]:
        # dataclasses.asdict() will recursively deep-copy the entire object,
        # which is expensive and breaks CodingFormat object, so let it simply copy here.
        return self.__dict__


_ESCO_PARAMETERS_CVSD_D0 = EscoParameters(
    transmit_coding_format=CodingFormat(CodecID.CVSD),
    receive_coding_format=CodingFormat(CodecID.CVSD),
    max_latency=0xFFFF,
    packet_type=HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.HV1,
    retransmission_effort=HCI_Enhanced_Setup_Synchronous_Connection_Command.RetransmissionEffort.NO_RETRANSMISSION,
)

_ESCO_PARAMETERS_CVSD_D1 = EscoParameters(
    transmit_coding_format=CodingFormat(CodecID.CVSD),
    receive_coding_format=CodingFormat(CodecID.CVSD),
    max_latency=0xFFFF,
    packet_type=HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.HV3,
    retransmission_effort=HCI_Enhanced_Setup_Synchronous_Connection_Command.RetransmissionEffort.NO_RETRANSMISSION,
)

_ESCO_PARAMETERS_CVSD_S1 = EscoParameters(
    transmit_coding_format=CodingFormat(CodecID.CVSD),
    receive_coding_format=CodingFormat(CodecID.CVSD),
    max_latency=0x0007,
    packet_type=(
        HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_2_EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_3_EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_2_EV5
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_3_EV5
    ),
    retransmission_effort=HCI_Enhanced_Setup_Synchronous_Connection_Command.RetransmissionEffort.OPTIMIZE_FOR_POWER,
)

_ESCO_PARAMETERS_CVSD_S2 = EscoParameters(
    transmit_coding_format=CodingFormat(CodecID.CVSD),
    receive_coding_format=CodingFormat(CodecID.CVSD),
    max_latency=0x0007,
    packet_type=(
        HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_3_EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_2_EV5
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_3_EV5
    ),
    retransmission_effort=HCI_Enhanced_Setup_Synchronous_Connection_Command.RetransmissionEffort.OPTIMIZE_FOR_POWER,
)

_ESCO_PARAMETERS_CVSD_S3 = EscoParameters(
    transmit_coding_format=CodingFormat(CodecID.CVSD),
    receive_coding_format=CodingFormat(CodecID.CVSD),
    max_latency=0x000A,
    packet_type=(
        HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_3_EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_2_EV5
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_3_EV5
    ),
    retransmission_effort=HCI_Enhanced_Setup_Synchronous_Connection_Command.RetransmissionEffort.OPTIMIZE_FOR_POWER,
)

_ESCO_PARAMETERS_CVSD_S4 = EscoParameters(
    transmit_coding_format=CodingFormat(CodecID.CVSD),
    receive_coding_format=CodingFormat(CodecID.CVSD),
    max_latency=0x000C,
    packet_type=(
        HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_3_EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_2_EV5
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_3_EV5
    ),
    retransmission_effort=HCI_Enhanced_Setup_Synchronous_Connection_Command.RetransmissionEffort.OPTIMIZE_FOR_QUALITY,
)

_ESCO_PARAMETERS_MSBC_T1 = EscoParameters(
    transmit_coding_format=CodingFormat(CodecID.MSBC),
    receive_coding_format=CodingFormat(CodecID.MSBC),
    max_latency=0x0008,
    packet_type=(
        HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_3_EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_2_EV5
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_3_EV5
    ),
    input_bandwidth=32000,
    output_bandwidth=32000,
    retransmission_effort=HCI_Enhanced_Setup_Synchronous_Connection_Command.RetransmissionEffort.OPTIMIZE_FOR_QUALITY,
)

_ESCO_PARAMETERS_MSBC_T2 = EscoParameters(
    transmit_coding_format=CodingFormat(CodecID.MSBC),
    receive_coding_format=CodingFormat(CodecID.MSBC),
    max_latency=0x000D,
    packet_type=(
        HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_2_EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_3_EV3
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_2_EV5
        | HCI_Enhanced_Setup_Synchronous_Connection_Command.PacketType.NO_3_EV5
    ),
    input_bandwidth=32000,
    output_bandwidth=32000,
    retransmission_effort=HCI_Enhanced_Setup_Synchronous_Connection_Command.RetransmissionEffort.OPTIMIZE_FOR_QUALITY,
)

ESCO_PARAMETERS = {
    DefaultCodecParameters.SCO_CVSD_D0: _ESCO_PARAMETERS_CVSD_D0,
    DefaultCodecParameters.SCO_CVSD_D1: _ESCO_PARAMETERS_CVSD_D1,
    DefaultCodecParameters.ESCO_CVSD_S1: _ESCO_PARAMETERS_CVSD_S1,
    DefaultCodecParameters.ESCO_CVSD_S2: _ESCO_PARAMETERS_CVSD_S2,
    DefaultCodecParameters.ESCO_CVSD_S3: _ESCO_PARAMETERS_CVSD_S3,
    DefaultCodecParameters.ESCO_CVSD_S4: _ESCO_PARAMETERS_CVSD_S4,
    DefaultCodecParameters.ESCO_MSBC_T1: _ESCO_PARAMETERS_MSBC_T1,
    DefaultCodecParameters.ESCO_MSBC_T2: _ESCO_PARAMETERS_MSBC_T2,
}