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
|
// Derived from ArduinoBLE.
// Copyright 2020 Dan Halbert for Adafruit Industries
// Some functions here are unused now, but may be used in the future.
// Don't warn or error about this, and depend on the compiler & linker to
// eliminate the associated code.
#pragma GCC diagnostic ignored "-Wunused"
#pragma GCC diagnostic ignored "-Wunused-function"
/*
This file is part of the ArduinoBLE library.
Copyright (c) 2018 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "hci.h"
#include "att.h"
// Zephyr include files to define HCI communication values and structs.
// #include "hci_include/hci.h"
// #include "hci_include/hci_err.h"
#include "hci_include/att_internal.h"
#include "hci_include/l2cap_internal.h"
#include "py/obj.h"
#include "common-hal/_bleio/Adapter.h"
#include "common-hal/_bleio/Attribute.h"
#include "shared-bindings/_bleio/__init__.h"
#include "shared-bindings/_bleio/Characteristic.h"
#include "shared-bindings/_bleio/Descriptor.h"
#include "shared-bindings/_bleio/Service.h"
#include "shared-bindings/_bleio/UUID.h"
#include "supervisor/shared/tick.h"
STATIC uint16_t max_mtu = BT_ATT_DEFAULT_LE_MTU; // 23
STATIC unsigned long timeout = 5000;
STATIC volatile bool confirm;
STATIC uint16_t long_write_handle = BLE_GATT_HANDLE_INVALID;
STATIC uint8_t *long_write_value = NULL;
STATIC uint16_t long_write_value_length = 0;
// When we send a request, fill this struct with info about the expected response.
// We check this against the actual received response.
STATIC struct {
uint16_t conn_handle; // Expected handle.
uint8_t opcode; // Expected RSP opcode.
uint8_t *buffer; // Pointer to response packet
uint8_t length; // Length of response packet.
} expected_rsp;
// A characteristic declaration has this data, in this order:
// See Bluetooth v5.1 spec, section 3.3.1 Characteristic declaration.
typedef struct __packed {
uint8_t properties;
uint16_t value_handle;
uint8_t uuid[]; // 2 or 16 bytes
} characteristic_declaration_t;
STATIC uint8_t bleio_properties_to_ble_spec_properties(uint8_t bleio_properties) {
uint8_t ble_spec_properties = 0;
if (bleio_properties & CHAR_PROP_BROADCAST) {
ble_spec_properties |= BT_GATT_CHRC_BROADCAST;
}
if (bleio_properties & CHAR_PROP_INDICATE) {
ble_spec_properties |= BT_GATT_CHRC_INDICATE;
}
if (bleio_properties & CHAR_PROP_NOTIFY) {
ble_spec_properties |= BT_GATT_CHRC_NOTIFY;
}
if (bleio_properties & CHAR_PROP_READ) {
ble_spec_properties |= BT_GATT_CHRC_READ;
}
if (bleio_properties & CHAR_PROP_WRITE) {
ble_spec_properties |= BT_GATT_CHRC_WRITE;
}
if (bleio_properties & CHAR_PROP_WRITE_NO_RESPONSE) {
ble_spec_properties |= BT_GATT_CHRC_WRITE_WITHOUT_RESP;
}
return ble_spec_properties;
}
// FIX not currently used; reenable when used.
#if 0
STATIC uint8_t ble_spec_properties_to_bleio_properties(uint8_t ble_spec_properties) {
uint8_t bleio_properties = 0;
if (ble_spec_properties & BT_GATT_CHRC_BROADCAST) {
bleio_properties |= CHAR_PROP_BROADCAST;
}
if (ble_spec_properties & BT_GATT_CHRC_INDICATE) {
bleio_properties |= CHAR_PROP_INDICATE;
}
if (ble_spec_properties & BT_GATT_CHRC_NOTIFY) {
bleio_properties |= CHAR_PROP_NOTIFY;
}
if (ble_spec_properties & BT_GATT_CHRC_READ) {
bleio_properties |= CHAR_PROP_READ;
}
if (ble_spec_properties & BT_GATT_CHRC_WRITE) {
bleio_properties |= CHAR_PROP_WRITE;
}
if (ble_spec_properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP) {
bleio_properties |= CHAR_PROP_WRITE_NO_RESPONSE;
}
return bleio_properties;
}
#endif // #if 0
STATIC void send_error(uint16_t conn_handle, uint8_t opcode, uint16_t handle, uint8_t code) {
struct __packed {
struct bt_att_hdr h;
struct bt_att_error_rsp r;
} rsp = { {
.code = BT_ATT_OP_ERROR_RSP,
}, {
.request = opcode,
}};
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, sizeof(rsp), (uint8_t *)&rsp);
}
STATIC void send_req(uint16_t conn_handle, size_t request_length, uint8_t *request_buffer) {
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, request_length, request_buffer);
}
STATIC int send_req_wait_for_rsp(uint16_t conn_handle, size_t request_length, uint8_t *request_buffer, uint8_t response_buffer[]) {
// We expect a particular kind of response after this request.
expected_rsp.conn_handle = conn_handle;
// The response opcode is the request opcode + 1.
expected_rsp.opcode = request_buffer[0] + 1;
expected_rsp.buffer = response_buffer;
expected_rsp.length = 0;
send_req(conn_handle, request_length, request_buffer);
if (response_buffer == NULL) {
// not expecting a response.
return 0;
}
for (uint64_t start = supervisor_ticks_ms64(); supervisor_ticks_ms64() - start < timeout;) {
// RUN_BACKGROUND_TASKS includes hci_poll_for_incoming_pkt();
RUN_BACKGROUND_TASKS;
if (!att_handle_is_connected(conn_handle)) {
break;
}
if (expected_rsp.length != 0) {
expected_rsp.conn_handle = 0xffff;
return expected_rsp.length;
}
}
expected_rsp.conn_handle = 0xffff;
return 0;
}
// If a response matches what is in expected_rsp, copy the rest of it into the buffer.
STATIC void check_and_save_expected_rsp(uint16_t conn_handle, uint8_t opcode, uint8_t dlen, uint8_t data[]) {
if (conn_handle == expected_rsp.conn_handle && expected_rsp.opcode == opcode) {
expected_rsp.buffer[0] = opcode;
memcpy(&expected_rsp.buffer[1], data, dlen);
expected_rsp.length = dlen + 1;
}
}
void bleio_att_reset(void) {
max_mtu = BT_ATT_DEFAULT_LE_MTU;
timeout = 5000;
long_write_handle = BLE_GATT_HANDLE_INVALID;
long_write_value = NULL;
long_write_value_length = 0;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
bleio_connections[i].conn_handle = BLE_CONN_HANDLE_INVALID;
bleio_connections[i].role = 0x00;
bleio_connections[i].addr.type = 0;
memset(bleio_connections[i].addr.a.val, 0, sizeof_field(bt_addr_t, val));
bleio_connections[i].mtu = BT_ATT_DEFAULT_LE_MTU;
}
}
bool att_connect_to_address(bt_addr_le_t *addr) {
// FIX
if (hci_le_create_conn(0x0060, 0x0030, 0x00, addr, 0x00,
0x0006, 0x000c, 0x0000, 0x00c8, 0x0004, 0x0006) != 0) {
return false;
}
bool is_connected = false;
for (uint64_t start = supervisor_ticks_ms64(); supervisor_ticks_ms64() - start < timeout;) {
// RUN_BACKGROUND_TASKS includes hci_poll_for_incoming_pkt();
RUN_BACKGROUND_TASKS;
is_connected = att_address_is_connected(addr);
if (is_connected) {
break;
}
}
if (!is_connected) {
hci_le_cancel_conn();
}
return is_connected;
}
bool att_disconnect(uint16_t conn_handle) {
if (conn_handle == BLE_CONN_HANDLE_INVALID) {
return false;
}
hci_disconnect(conn_handle);
// Confirm we're now disconnected.
return !att_handle_is_connected(conn_handle);
}
// FIX
// STATIC bool discover_services(uint16_t conn_handle, BLERemoteDevice* device, const char* serviceUuidFilter) {
// uint16_t reqStart_handle = 0x0001;
// uint16_t reqEnd_handle = 0xffff;
// uint8_t response_buffer[max_mtu];
// BLEUuid serviceUuid(serviceUuidFilter);
// while (reqEnd_handle == 0xffff) {
// int respLength = readByGroupReq(conn_handle, reqStart_handle, reqEnd_handle, BLE_UUID_SERVICE_PRIMARY, response_buffer);
// if (respLength == 0) {
// return false;
// }
// if (response_buffer[0] == BT_ATT_OP_READ_GROUP_RSP) {
// uint16_t lengthPerService = response_buffer[1];
// uint8_t uuidLen = lengthPerService - 4;
// for (size_t i = 2; i < respLength; i += lengthPerService) {
// struct __attribute__ ((packed)) RawService {
// uint16_t start_handle;
// uint16_t end_handle;
// uint8_t uuid[16];
// } *rawService = (RawService*)&response_buffer[i];
// if (serviceUuidFilter == NULL ||
// (uuidLen == serviceUuid.length() && memcmp(rawService->uuid, serviceUuid.data(), uuidLen) == 0)) {
// BLERemoteService* service = new BLERemoteService(rawService->uuid, uuidLen,
// rawService->start_handle,
// rawService->end_handle);
// if (service == NULL) {
// return false;
// }
// device->addService(service);
// }
// reqStart_handle = rawService->end_handle + 1;
// if (reqStart_handle == BLE_GATT_HANDLE_INVALID) {
// reqEnd_handle = BLE_GATT_HANDLE_INVALID;
// }
// }
// } else {
// reqEnd_handle = BLE_GATT_HANDLE_INVALID;
// }
// }
// return true;
// }
// STATIC bool discover_characteristics(uint16_t conn_handle, BLERemoteDevice* device) {
// uint16_t reqStart_handle = 0x0001;
// uint16_t reqEnd_handle = 0xffff;
// uint8_t response_buffer[max_mtu];
// int serviceCount = device->serviceCount();
// for (size_t i = 0; i < serviceCount; i++) {
// BLERemoteService* service = device->service(i);
// reqStart_handle = service->start_handle();
// reqEnd_handle = service->end_handle();
// while (1) {
// int respLength = readByTypeReq(conn_handle, reqStart_handle, reqEnd_handle, BLE_UUID_CHARACTERISTIC, response_buffer);
// if (respLength == 0) {
// return false;
// }
// if (response_buffer[0] == BT_ATT_OP_READ_TYPE_RSP) {
// uint16_t lengthPerCharacteristic = response_buffer[1];
// uint8_t uuidLen = lengthPerCharacteristic - 5;
// for (size_t i = 2; i < respLength; i += lengthPerCharacteristic) {
// struct __attribute__ ((packed)) RawCharacteristic {
// uint16_t start_handle;
// uint8_t properties;
// uint16_t value_handle;
// uint8_t uuid[16];
// } *rawCharacteristic = (RawCharacteristic*)&response_buffer[i];
// BLERemoteCharacteristic* characteristic = new BLERemoteCharacteristic(rawCharacteristic->uuid, uuidLen,
// conn_handle,
// rawCharacteristic->start_handle,
// rawCharacteristic->properties,
// rawCharacteristic->value_handle);
// if (characteristic == NULL) {
// return false;
// }
// service->addCharacteristic(characteristic);
// reqStart_handle = rawCharacteristic->value_handle + 1;
// }
// } else {
// break;
// }
// }
// }
// return true;
// }
// STATIC bool discover_descriptors(uint16_t conn_handle, BLERemoteDevice* device) {
// uint16_t reqStart_handle = 0x0001;
// uint16_t reqEnd_handle = 0xffff;
// uint8_t response_buffer[max_mtu];
// int serviceCount = device->serviceCount();
// for (size_t i = 0; i < serviceCount; i++) {
// BLERemoteService* service = device->service(i);
// uint16_t serviceEnd_handle = service->end_handle();
// int characteristicCount = service->characteristicCount();
// for (int j = 0; j < characteristicCount; j++) {
// BLERemoteCharacteristic* characteristic = service->characteristic(j);
// BLERemoteCharacteristic* nextCharacteristic = (j == (characteristicCount - 1)) ? NULL : service->characteristic(j);
// reqStart_handle = characteristic->value_handle() + 1;
// reqEnd_handle = nextCharacteristic ? nextCharacteristic->value_handle() : serviceEnd_handle;
// if (reqStart_handle > reqEnd_handle) {
// continue;
// }
// while (1) {
// int respLength = findInfoReq(conn_handle, reqStart_handle, reqEnd_handle, response_buffer);
// if (respLength == 0) {
// return false;
// }
// if (response_buffer[0] == BT_ATT_OP_FIND_INFO_RSP) {
// uint16_t lengthPerDescriptor = response_buffer[1] * 4;
// uint8_t uuidLen = 2;
// for (size_t i = 2; i < respLength; i += lengthPerDescriptor) {
// struct __attribute__ ((packed)) RawDescriptor {
// uint16_t handle;
// uint8_t uuid[16];
// } *rawDescriptor = (RawDescriptor*)&response_buffer[i];
// BLERemoteDescriptor* descriptor = new BLERemoteDescriptor(rawDescriptor->uuid, uuidLen,
// conn_handle,
// rawDescriptor->handle);
// if (descriptor == NULL) {
// return false;
// }
// characteristic->addDescriptor(descriptor);
// reqStart_handle = rawDescriptor->handle + 1;
// }
// } else {
// break;
// }
// }
// }
// }
// return true;
// }
bool att_discover_attributes(bt_addr_le_t *addr, const char *service_uuid_filter) {
uint16_t conn_handle = att_conn_handle(addr);
if (conn_handle == 0xffff) {
return false;
}
// send MTU request
if (!att_exchange_mtu(conn_handle)) {
return false;
}
// find the device entry for the peeer
// FIX BLERemoteDevice* device = NULL;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
// if (bleio_connections[i].conn_handle == conn_handle) {
// //FIX if (bleio_connections[i].device == NULL) {
// //FIX
// //bleio_connections[i].device = new BLERemoteDevice();
// //}
// //device = bleio_connections[i].device;
// break;
// }
// }
// //FIX if (device == NULL) {
// // return false;
// // }
// if (service_uuid_filter == NULL) {
// // clear existing services
// //FIX device->clear_services();
// } else {
// //FIX int service_count = device->service_count();
// for (size_t i = 0; i < service_count; i++) {
// //FIX BLERemoteService* service = device->service(i);
// if (strcasecmp(service->uuid(), service_uuid_filter) == 0) {
// // found an existing service with same UUID
// return true;
// }
// }
}
// discover services
// FIX
// if (!att_discover_services(conn_handle, device, service_uuid_filter)) {
// return false;
// }
// // discover characteristics
// if (!discover_characteristics(conn_handle, device)) {
// return false;
// }
// // discover descriptors396
// if (!discover_descriptors(conn_handle, device)) {
// return false;
// }
return true;
}
void att_set_max_mtu(uint16_t max_mtu_in) {
max_mtu = max_mtu_in;
}
void att_set_timeout(unsigned long timeout_in) {
timeout = timeout_in;
}
void att_add_connection(uint16_t handle, uint8_t role, bt_addr_le_t *peer_addr, uint16_t interval, uint16_t latency, uint16_t supervision_timeout, uint8_t master_clock_accuracy) {
(void)interval;
(void)latency;
(void)supervision_timeout;
(void)master_clock_accuracy;
int peer_index = -1;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == 0xffff) {
peer_index = i;
break;
}
}
if (peer_index == -1) {
// bail, no space
return;
}
bleio_connections[peer_index].conn_handle = handle;
bleio_connections[peer_index].role = role;
bleio_connections[peer_index].mtu = BT_ATT_DEFAULT_LE_MTU;
memcpy(&bleio_connections[peer_index].addr, peer_addr, sizeof(bleio_connections[peer_index].addr));
}
void att_remove_connection(uint16_t conn_handle, uint8_t reason) {
(void)reason;
int peer_index = -1;
int peer_count = 0;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == conn_handle) {
peer_index = i;
}
if (bleio_connections[i].conn_handle != 0xffff) {
peer_count++;
}
}
if (peer_index == -1) {
// Peer not found
return;
}
if (peer_count == 1) {
// Clear CCCD values on disconnect.
size_t max_attribute_handle = bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj);
for (size_t handle = 1; handle <= max_attribute_handle; handle++) {
mp_obj_t attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
uint16_t zero = 0;
mp_buffer_info_t zero_cccd_value = {
.buf = &zero,
.len = sizeof(zero),
};
if (mp_obj_is_type(attribute_obj, &bleio_descriptor_type)) {
bleio_descriptor_obj_t *descriptor = MP_OBJ_TO_PTR(attribute_obj);
if (bleio_uuid_get_uuid16_or_unknown(descriptor->uuid) == BLE_UUID_CCCD) {
common_hal_bleio_descriptor_set_value(descriptor, &zero_cccd_value);
}
}
}
long_write_handle = BLE_GATT_HANDLE_INVALID;
long_write_value_length = 0;
}
bleio_connections[peer_index].conn_handle = 0xffff;
bleio_connections[peer_index].role = 0x00;
memset(&bleio_connections[peer_index].addr, 0x00, sizeof(bleio_connections[peer_index].addr));
bleio_connections[peer_index].mtu = BT_ATT_DEFAULT_LE_MTU;
}
uint16_t att_conn_handle(bt_addr_le_t *addr) {
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].addr.type == addr->type &&
memcmp(&bleio_connections[i].addr.a.val, addr->a.val, sizeof(addr->a.val)) == 0) {
return bleio_connections[i].conn_handle;
}
}
return 0xffff;
}
bool att_is_connected(void) {
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle != 0xffff) {
return true;
}
}
return false;
}
bool att_address_is_connected(bt_addr_le_t *addr) {
return att_conn_handle(addr) != 0xffff;
}
bool att_handle_is_connected(uint16_t handle) {
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == handle) {
return true;
}
}
return false;
}
uint16_t att_mtu(uint16_t handle) {
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == handle) {
return bleio_connections[i].mtu;
}
}
return BT_ATT_DEFAULT_LE_MTU;
}
bool att_disconnect_all(void) {
int num_disconnects = 0;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == 0xffff) {
continue;
}
if (att_disconnect(bleio_connections[i].conn_handle) != 0) {
continue;
}
num_disconnects++;
bleio_connections[i].conn_handle = 0xffff;
bleio_connections[i].role = 0x00;
bleio_connections[i].addr.type = 0;
memset(bleio_connections[i].addr.a.val, 0, sizeof(bleio_connections[i].addr.a.val));
bleio_connections[i].mtu = BT_ATT_DEFAULT_LE_MTU;
}
return num_disconnects > 0;
}
bool att_notify(uint16_t handle, const uint8_t *value, int length) {
int num_notifications = 0;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == 0xffff) {
continue;
}
typedef struct __packed {
struct bt_att_hdr hdr;
struct bt_att_notify ntf;
} notify_t;
size_t allowed_length = MIN((uint16_t)(bleio_connections[i].mtu - sizeof(notify_t)), (uint16_t)length);
uint8_t notify_bytes[sizeof(notify_t) + allowed_length];
notify_t *notify = (notify_t *)notify_bytes;
notify->hdr.code = BT_ATT_OP_NOTIFY;
;
notify->ntf.handle = handle;
memcpy(notify->ntf.value, value, allowed_length);
hci_send_acl_pkt(bleio_connections[i].conn_handle, BT_L2CAP_CID_ATT,
sizeof(notify_bytes), notify_bytes);
num_notifications++;
}
return num_notifications > 0;
}
bool att_indicate(uint16_t handle, const uint8_t *value, int length) {
int num_indications = 0;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == 0xffff) {
continue;
}
typedef struct __packed {
struct bt_att_hdr hdr;
struct bt_att_indicate ind;
} indicate_t;
size_t allowed_length = MIN((uint16_t)(bleio_connections[i].mtu - sizeof(indicate_t)), (uint16_t)length);
uint8_t indicate_bytes[sizeof(indicate_t) + allowed_length];
indicate_t *indicate = (indicate_t *)indicate_bytes;
indicate->hdr.code = BT_ATT_OP_INDICATE;
;
indicate->ind.handle = handle;
memcpy(indicate->ind.value, value, allowed_length);
confirm = false;
hci_send_acl_pkt(bleio_connections[i].conn_handle, BT_L2CAP_CID_ATT,
sizeof(indicate_bytes), indicate_bytes);
while (!confirm) {
// RUN_BACKGROUND_TASKS includes hci_poll_for_incoming_pkt();
RUN_BACKGROUND_TASKS;
if (!att_address_is_connected(&bleio_connections[i].addr)) {
break;
}
}
num_indications++;
}
return num_indications > 0;
}
STATIC void process_error(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
struct bt_att_error_rsp *rsp = (struct bt_att_error_rsp *)data;
if (dlen != sizeof(struct bt_att_error_rsp)) {
// Incorrect size; ignore.
return;
}
// expected_rsp.opcode is an RSP opcode. Does it match the REQ opcode in this response?
if (expected_rsp.conn_handle == conn_handle && (expected_rsp.opcode - 1) == rsp->request) {
expected_rsp.buffer[0] = BT_ATT_OP_ERROR_RSP;
memcpy(&expected_rsp.buffer[1], data, dlen);
expected_rsp.length = dlen + 1;
}
}
STATIC void process_mtu_req(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
struct bt_att_exchange_mtu_req *req = (struct bt_att_exchange_mtu_req *)data;
if (dlen != sizeof(struct bt_att_exchange_mtu_req)) {
send_error(conn_handle, BT_ATT_OP_MTU_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU);
return;
}
uint16_t mtu = req->mtu;
if (mtu > max_mtu) {
mtu = max_mtu;
}
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == conn_handle) {
bleio_connections[i].mtu = mtu;
break;
}
}
struct __packed {
struct bt_att_hdr h;
struct bt_att_exchange_mtu_rsp r;
} rsp = { {
.code = BT_ATT_OP_MTU_RSP,
}, {
.mtu = mtu,
}};
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, sizeof(rsp), (uint8_t *)&rsp);
}
STATIC void process_mtu_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
struct bt_att_exchange_mtu_rsp *rsp = (struct bt_att_exchange_mtu_rsp *)data;
if (dlen != sizeof(struct bt_att_exchange_mtu_rsp)) {
return;
}
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == conn_handle) {
bleio_connections[i].mtu = rsp->mtu;
break;
}
}
check_and_save_expected_rsp(conn_handle, BT_ATT_OP_MTU_RSP, dlen, data);
}
STATIC void process_find_info_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) {
struct bt_att_find_info_req *req = (struct bt_att_find_info_req *)data;
if (dlen != sizeof(struct bt_att_find_info_req)) {
send_error(conn_handle, BT_ATT_OP_FIND_INFO_REQ, req->start_handle, BT_ATT_ERR_INVALID_PDU);
return;
}
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_find_info_rsp r;
} rsp_t;
uint8_t rsp_bytes[mtu];
rsp_t *rsp = (rsp_t *)rsp_bytes;
rsp->h.code = BT_ATT_OP_FIND_INFO_RSP;
// Keeps track of total length of the response.
size_t rsp_length = sizeof(rsp_t);
bool no_data = true;
// All the data chunks must have uuid's that are the same size.
// Keep track of the first one to make sure.
size_t sizeof_first_uuid = 0;
const uint16_t max_attribute_handle = bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj);
for (uint16_t handle = req->start_handle;
handle <= max_attribute_handle && handle <= req->end_handle;
handle++) {
mp_obj_t *attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
// Fetch the uuid for the given attribute, which might be a characteristic or a descriptor.
bleio_uuid_obj_t *uuid;
if (mp_obj_is_type(attribute_obj, &bleio_characteristic_type)) {
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute_obj);
if (characteristic->handle != handle) {
// If the handles don't match, this is the characteristic definition attribute.
// Skip it. We want the characteristic value attribute.
continue;
}
uuid = characteristic->uuid;
} else {
uuid = bleio_attribute_get_uuid(attribute_obj);
}
const uint32_t sizeof_uuid = common_hal_bleio_uuid_get_size(uuid) / 8;
if (sizeof_first_uuid == 0) {
sizeof_first_uuid = sizeof_uuid;
// All the uuids in the response will be the same size.
rsp->r.format = sizeof_uuid == 2 ? BT_ATT_INFO_16 : BT_ATT_INFO_128;
}
if (sizeof_uuid != sizeof_first_uuid) {
// Previous UUID was a different size. We can't mix sizes.
// Stop and send what we have so far.
break;
}
if (rsp_length + sizeof_uuid > mtu) {
// No remaining room in response for this uuid.
break;
}
if (sizeof_uuid == 2) {
struct bt_att_info_16 *info_16 = (struct bt_att_info_16 *)&rsp_bytes[rsp_length];
info_16->handle = handle;
info_16->uuid = common_hal_bleio_uuid_get_uuid16(uuid);
rsp_length += sizeof(struct bt_att_info_16);
} else {
struct bt_att_info_128 *info_128 = (struct bt_att_info_128 *)&rsp_bytes[rsp_length];
info_128->handle = handle;
common_hal_bleio_uuid_get_uuid128(uuid, info_128->uuid);
rsp_length += sizeof(struct bt_att_info_128);
}
no_data = false;
} // end for
if (no_data) {
send_error(conn_handle, BT_ATT_OP_FIND_INFO_REQ, req->start_handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
} else {
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, rsp_length, rsp_bytes);
}
}
static int att_find_info_req(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, uint8_t response_buffer[]) {
struct __packed req {
struct bt_att_hdr h;
struct bt_att_find_info_req r;
} req = { {
.code = BT_ATT_OP_FIND_INFO_REQ,
}, {
.start_handle = start_handle,
.end_handle = end_handle,
}};
return send_req_wait_for_rsp(conn_handle, sizeof(req), (uint8_t *)&req, response_buffer);
}
STATIC void process_find_info_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
if (dlen < 2) {
return; // invalid, drop
}
check_and_save_expected_rsp(conn_handle, BT_ATT_OP_FIND_INFO_RSP, dlen, data);
}
STATIC void process_find_type_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) {
struct bt_att_find_type_req *req = (struct bt_att_find_type_req *)data;
if (dlen < sizeof(struct bt_att_find_type_req)) {
send_error(conn_handle, BT_ATT_OP_FIND_TYPE_RSP, req->start_handle, BT_ATT_ERR_INVALID_PDU);
return;
}
uint8_t response[mtu];
uint16_t response_length;
response[0] = BT_ATT_OP_FIND_TYPE_RSP;
response_length = 1;
// FIX
// if (find_type_req->type == BLE_UUID_SERVICE_PRIMARY) {
// for (uint16_t i = (find_type_req->start_handle - 1); i < GATT.attributeCount() && i <= (find_type_req->end_handle - 1); i++) {
// BLELocalAttribute* attribute = GATT.attribute(i);
// if ((attribute->type() == find_type_req->type) && (attribute->uuidLength() == value_length) && memcmp(attribute->uuidData(), value, value_length) == 0) {
// BLELocalService* service = (BLELocalService*)attribute;
// // add the start handle
// uint16_t start_handle = service->start_handle();
// memcpy(&response[response_length], &start_handle, sizeof(start_handle));
// response_length += sizeof(start_handle);
// // add the end handle
// uint16_t end_handle = service->end_handle();
// memcpy(&response[response_length], &end_handle, sizeof(end_handle));
// response_length += sizeof(end_handle);
// }
// if ((response_length + 4) > mtu) {
// break;
// }
// }
// }
if (response_length == 1) {
send_error(conn_handle, BT_ATT_OP_FIND_TYPE_RSP, req->start_handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
} else {
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, response_length, response);
}
}
static void process_read_group_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) {
struct bt_att_read_group_req *req = (struct bt_att_read_group_req *)data;
uint16_t type_uuid = req->uuid[0] | (req->uuid[1] << 8);
// We only support returning services for BT_ATT_OP_READ_GROUP_REQ, which is typically used
// for service discovery.
if (dlen != sizeof(struct bt_att_read_group_req) + sizeof(type_uuid) ||
(type_uuid != BLE_UUID_SERVICE_PRIMARY &&
type_uuid != BLE_UUID_SERVICE_SECONDARY)) {
send_error(conn_handle, BT_ATT_OP_READ_GROUP_REQ, req->start_handle, BT_ATT_ERR_UNSUPPORTED_GROUP_TYPE);
return;
}
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_read_group_rsp r;
} rsp_t;
uint8_t rsp_bytes[mtu];
rsp_t *rsp = (rsp_t *)rsp_bytes;
rsp->h.code = BT_ATT_OP_READ_GROUP_RSP;
rsp->r.len = 0;
// Keeps track of total length of the response.
size_t rsp_length = sizeof(rsp_t);
bool no_data = true;
// All the data chunks must have uuid's that are the same size.
// Keep track of the first one to make sure.
size_t sizeof_first_service_uuid = 0;
// Size of a single bt_att_group_data chunk. Start with the intial size, and
// add the uuid size in the loop below.
size_t data_length = sizeof(struct bt_att_group_data);
const uint16_t max_attribute_handle = bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj);
for (uint16_t handle = req->start_handle;
handle <= max_attribute_handle && handle <= req->end_handle;
handle++) {
if (rsp_length + data_length > mtu) {
// The next possible bt_att_group_data chunk won't fit. The response is full.
break;
}
mp_obj_t *attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
if (mp_obj_is_type(attribute_obj, &bleio_service_type)) {
bleio_service_obj_t *service = MP_OBJ_TO_PTR(attribute_obj);
// Is this a 16-bit or a 128-bit uuid? It must match in size with any previous attribute
// in this transmission.
const uint32_t sizeof_service_uuid = common_hal_bleio_uuid_get_size(service->uuid) / 8;
if (sizeof_first_service_uuid == 0) {
sizeof_first_service_uuid = sizeof_service_uuid;
data_length += sizeof_service_uuid;
} else if (sizeof_first_service_uuid != sizeof_service_uuid) {
// Mismatched sizes, which can't be in the same batch.
// Transmit just what we have so far in this batch.
break;
}
// Pass the length of ONE bt_att_group_data chunk.
// There may be multiple chunks in this transmission.
rsp->r.len = data_length;
struct bt_att_group_data *group_data = (struct bt_att_group_data *)&rsp_bytes[rsp_length];
group_data->start_handle = service->start_handle;
group_data->end_handle = service->end_handle;
common_hal_bleio_uuid_pack_into(service->uuid, group_data->value);
rsp_length += data_length;
no_data = false;
}
}
if (no_data) {
send_error(conn_handle, BT_ATT_OP_READ_GROUP_REQ, req->start_handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
} else {
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, rsp_length, rsp_bytes);
}
}
static int att_read_group_req(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, uint16_t uuid, uint8_t response_buffer[]) {
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_read_group_req r;
} req_t;
uint8_t req_bytes[sizeof(req_t) + sizeof(uuid)];
req_t *req = (req_t *)req_bytes;
req->h.code = BT_ATT_OP_READ_GROUP_REQ;
req->r.start_handle = start_handle;
req->r.end_handle = end_handle;
req->r.uuid[0] = uuid & 0xff;
req->r.uuid[1] = uuid >> 8;
return send_req_wait_for_rsp(conn_handle, sizeof(req_bytes), req_bytes, response_buffer);
}
STATIC void process_read_group_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
if (dlen < 2) {
return; // invalid, drop
}
check_and_save_expected_rsp(conn_handle, BT_ATT_OP_READ_GROUP_RSP, dlen, data);
}
STATIC void process_read_or_read_blob_req(uint16_t conn_handle, uint16_t mtu, uint8_t opcode, uint8_t dlen, uint8_t data[]) {
uint16_t handle;
uint16_t offset = 0;
uint8_t response_opcode;
if (opcode == BT_ATT_OP_READ_REQ) {
if (dlen != sizeof(struct bt_att_read_req)) {
send_error(conn_handle, BT_ATT_OP_READ_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU);
return;
}
struct bt_att_read_req *req = (struct bt_att_read_req *)data;
handle = req->handle;
response_opcode = BT_ATT_OP_READ_RSP;
} else if (opcode == BT_ATT_OP_READ_BLOB_REQ) {
if (dlen != sizeof(struct bt_att_read_blob_req)) {
send_error(conn_handle, BT_ATT_OP_READ_BLOB_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU);
return;
}
struct bt_att_read_blob_req *req = (struct bt_att_read_blob_req *)data;
handle = req->handle;
offset = req->offset;
response_opcode = BT_ATT_OP_READ_BLOB_RSP;
} else {
return;
}
if (handle > bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj)) {
send_error(conn_handle, opcode, handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
return;
}
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_read_rsp r; // Same as bt_att_read_blob_rsp.
} rsp_t;
uint8_t rsp_bytes[mtu];
rsp_t *rsp = (rsp_t *)rsp_bytes;
rsp->h.code = response_opcode;
// Keeps track of total length of the response.
size_t rsp_length = sizeof(rsp_t);
mp_obj_t *attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
if (mp_obj_is_type(attribute_obj, &bleio_service_type)) {
if (offset) {
send_error(conn_handle, BT_ATT_ERR_ATTRIBUTE_NOT_LONG, handle, BT_ATT_ERR_INVALID_PDU);
return;
}
bleio_service_obj_t *service = MP_OBJ_TO_PTR(attribute_obj);
const uint32_t sizeof_service_uuid = common_hal_bleio_uuid_get_size(service->uuid) / 8;
common_hal_bleio_uuid_pack_into(service->uuid, rsp->r.value);
rsp_length += sizeof_service_uuid;
} else if (mp_obj_is_type(attribute_obj, &bleio_characteristic_type)) {
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute_obj);
if (characteristic->decl_handle == handle) {
// Read characteristic declaration. Return properties, value handle, and uuid.
if (offset) {
send_error(conn_handle, opcode, handle, BT_ATT_ERR_ATTRIBUTE_NOT_LONG);
return;
}
characteristic_declaration_t *char_decl = (characteristic_declaration_t *)rsp->r.value;
// Convert from the bleio properties bit values to the BLE spec properties bit values.
// They are not the same :(.
char_decl->properties = bleio_properties_to_ble_spec_properties(characteristic->props);
char_decl->value_handle = characteristic->handle;
const uint32_t sizeof_char_uuid = common_hal_bleio_uuid_get_size(characteristic->uuid) / 8;
common_hal_bleio_uuid_pack_into(characteristic->uuid, char_decl->uuid);
rsp_length += sizeof_char_uuid;
} else {
// Read characteristic value.
if ((characteristic->props & CHAR_PROP_READ) == 0) {
send_error(conn_handle, opcode, handle, BT_ATT_ERR_READ_NOT_PERMITTED);
return;
}
mp_buffer_info_t bufinfo;
mp_get_buffer(characteristic->value, &bufinfo, MP_BUFFER_READ);
if (offset >= bufinfo.len) {
send_error(conn_handle, opcode, handle, BT_ATT_ERR_INVALID_OFFSET);
return;
}
size_t value_length = MIN(mtu - rsp_length, bufinfo.len - offset);
memcpy(rsp->r.value, bufinfo.buf + offset, value_length);
rsp_length += value_length;
}
} else if (mp_obj_is_type(attribute_obj, &bleio_descriptor_type)) {
bleio_descriptor_obj_t *descriptor = MP_OBJ_TO_PTR(attribute_obj);
mp_buffer_info_t bufinfo;
mp_get_buffer(descriptor->value, &bufinfo, MP_BUFFER_READ);
if (offset >= bufinfo.len) {
send_error(conn_handle, opcode, handle, BT_ATT_ERR_INVALID_OFFSET);
return;
}
size_t value_length = MIN(mtu - rsp_length, bufinfo.len - offset);
memcpy(rsp->r.value, bufinfo.buf + offset, value_length);
rsp_length += value_length;
}
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, rsp_length, rsp_bytes);
}
STATIC void process_read_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
check_and_save_expected_rsp(conn_handle, BT_ATT_OP_READ_RSP, dlen, data);
}
STATIC void process_read_type_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) {
struct bt_att_read_type_req *req = (struct bt_att_read_type_req *)data;
uint16_t type_uuid = req->uuid[0] | (req->uuid[1] << 8);
if (dlen != sizeof(struct bt_att_read_type_req) + sizeof(type_uuid)) {
send_error(conn_handle, BT_ATT_OP_READ_TYPE_REQ, req->start_handle, BT_ATT_ERR_INVALID_PDU);
return;
}
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_read_type_rsp r;
} rsp_t;
uint8_t rsp_bytes[mtu];
rsp_t *rsp = (rsp_t *)rsp_bytes;
rsp->h.code = BT_ATT_OP_READ_TYPE_RSP;
rsp->r.len = 0;
// Keeps track of total length of the response.
size_t rsp_length = sizeof(rsp_t);
bool no_data = true;
// All the data chunks must have uuid's that are the same size.
// Keep track of the first one to make sure.
size_t sizeof_first_uuid = 0;
// Size of a single bt_att_data chunk. Start with the initial size, and
// add the uuid size and other data sizes in the loop below.
size_t data_length = sizeof(struct bt_att_data);
const uint16_t max_attribute_handle = bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj);
for (uint16_t handle = req->start_handle;
handle <= max_attribute_handle && handle <= req->end_handle;
handle++) {
if (rsp_length + data_length > mtu) {
// The next possible bt_att_data chunk won't fit. The response is full.
break;
}
mp_obj_t *attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
if (type_uuid == BLE_UUID_CHARACTERISTIC &&
mp_obj_is_type(attribute_obj, &bleio_characteristic_type)) {
// Request is for characteristic declarations.
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute_obj);
if (characteristic->handle == handle) {
// If the characteristic's handle is this attribute's handle, skip it:
// it's the attribute for characteristic value. We want to return the declaration
// handle attribute instead. (It will probably get skipped below, by the
// handle++).
continue;
}
// Is this a 16-bit or a 128-bit uuid? It must match in size with any previous attribute
// in this transmission.
const uint32_t sizeof_uuid = common_hal_bleio_uuid_get_size(characteristic->uuid) / 8;
if (sizeof_first_uuid == 0) {
sizeof_first_uuid = sizeof_uuid;
data_length += sizeof_uuid;
data_length += sizeof(characteristic_declaration_t);
} else if (sizeof_first_uuid != sizeof_uuid) {
// Mismatched sizes, which can't be in the same batch.
// Transmit just what we have so far in this batch.
break;
}
// Pass the length of ONE bt_att_data chunk.
// There may be multiple chunks in this transmission.
rsp->r.len = data_length;
struct bt_att_data *att_data = (struct bt_att_data *)&rsp_bytes[rsp_length];
att_data->handle = characteristic->decl_handle;
characteristic_declaration_t *char_decl = (characteristic_declaration_t *)att_data->value;
// Convert from the bleio properties bit values to the BLE spec properties bit values.
// They are not the same :(.
char_decl->properties = bleio_properties_to_ble_spec_properties(characteristic->props);
char_decl->value_handle = characteristic->handle;
common_hal_bleio_uuid_pack_into(characteristic->uuid, char_decl->uuid);
// We know the next handle will be the characteristic value handle, so skip it.
handle++;
rsp_length += data_length;
no_data = false;
} else if (mp_obj_is_type(attribute_obj, &bleio_descriptor_type)) {
// See if request is for a descriptor value with a 16-bit UUID, such as the CCCD.
bleio_descriptor_obj_t *descriptor = MP_OBJ_TO_PTR(attribute_obj);
if (bleio_uuid_get_uuid16_or_unknown(descriptor->uuid) == type_uuid) {
struct bt_att_data *att_data = (struct bt_att_data *)&rsp_bytes[rsp_length];
att_data->handle = handle;
mp_buffer_info_t bufinfo;
if (!mp_get_buffer(descriptor->value, &bufinfo, MP_BUFFER_READ)) {
break;
}
uint16_t value_size = MIN(mtu - rsp_length, bufinfo.len);
memcpy(att_data->value, bufinfo.buf, value_size);
rsp_length += value_size;
// Only return one descriptor value.
no_data = false;
break;
}
} else if (mp_obj_is_type(attribute_obj, &bleio_characteristic_type)) {
// See if request is for a characteristic value with a 16-bit UUID.
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute_obj);
if (bleio_uuid_get_uuid16_or_unknown(characteristic->uuid) == type_uuid) {
struct bt_att_data *att_data = (struct bt_att_data *)&rsp_bytes[rsp_length];
att_data->handle = handle;
mp_buffer_info_t bufinfo;
if (!mp_get_buffer(characteristic->value, &bufinfo, MP_BUFFER_READ)) {
// This shouldn't happen. There should be a buf in characteristic->value.
break;
}
uint16_t value_size = MIN(mtu - rsp_length, bufinfo.len);
memcpy(att_data->value, bufinfo.buf, value_size);
rsp_length += value_size;
// Only return one characteristic value.
no_data = false;
break;
}
}
} // end for loop
if (no_data) {
send_error(conn_handle, BT_ATT_OP_READ_TYPE_REQ,
req->start_handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
} else {
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, rsp_length, rsp_bytes);
}
}
static int att_read_type_req(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, uint16_t type, uint8_t response_buffer[]) {
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_read_type_req r;
} req_t;
uint8_t req_bytes[sizeof(req_t) + sizeof(type)];
req_t *req = (req_t *)req_bytes;
req->h.code = BT_ATT_OP_READ_TYPE_REQ;
req->r.start_handle = start_handle;
req->r.end_handle = end_handle;
req->r.uuid[0] = type & 0xff;
req->r.uuid[1] = type >> 8;
return send_req_wait_for_rsp(conn_handle, sizeof(req_bytes), req_bytes, response_buffer);
}
STATIC void process_read_type_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
if (dlen < 1) {
return; // invalid, drop
}
check_and_save_expected_rsp(conn_handle, BT_ATT_OP_READ_TYPE_RSP, dlen, data);
}
// Handles BT_ATT_OP_WRITE_REQ or BT_ATT_OP_WRITE_
STATIC void process_write_req_or_cmd(uint16_t conn_handle, uint16_t mtu, uint8_t op, uint8_t dlen, uint8_t data[]) {
// struct bt_att_write_cmd is identical, so don't bother to split code paths based on opcode.
struct bt_att_write_req *req = (struct bt_att_write_req *)data;
bool with_response = (op == BT_ATT_OP_WRITE_REQ);
if (dlen < sizeof(struct bt_att_write_req)) {
if (with_response) {
send_error(conn_handle, BT_ATT_OP_WRITE_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU);
}
return;
}
if (req->handle > bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj)) {
if (with_response) {
send_error(conn_handle, BT_ATT_OP_WRITE_REQ, req->handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
}
return;
}
size_t value_length = dlen - sizeof(struct bt_att_write_req);
mp_buffer_info_t bufinfo;
bufinfo.buf = req->value;
bufinfo.len = value_length;
mp_obj_t attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, req->handle);
if (mp_obj_is_type(attribute_obj, &bleio_characteristic_type)) {
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute_obj);
// Don't write the characteristic declaration.
// Also, this must be a writable characteristic.
if (req->handle != characteristic->handle ||
(with_response
? (characteristic->props & CHAR_PROP_WRITE) == 0
: (characteristic->props & CHAR_PROP_WRITE_NO_RESPONSE) == 0)) {
if (with_response) {
send_error(conn_handle, BT_ATT_OP_WRITE_REQ, req->handle, BT_ATT_ERR_WRITE_NOT_PERMITTED);
}
return;
}
// Just change the local value. Don't fire off notifications, etc.
bleio_characteristic_set_local_value(characteristic, &bufinfo);
} else if (mp_obj_is_type(attribute_obj, &bleio_descriptor_type)) {
bleio_descriptor_obj_t *descriptor = MP_OBJ_TO_PTR(attribute_obj);
// Only CCCD's are writable.
if (bleio_uuid_get_uuid16_or_unknown(descriptor->uuid) != BLE_UUID_CCCD) {
if (with_response) {
send_error(conn_handle, BT_ATT_OP_WRITE_REQ, req->handle, BT_ATT_ERR_WRITE_NOT_PERMITTED);
}
return;
}
common_hal_bleio_descriptor_set_value(descriptor, &bufinfo);
}
if (with_response) {
// There's no data in the response. We just indicate the write happened.
struct bt_att_hdr rsp = {
.code = BT_ATT_OP_WRITE_RSP,
};
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, sizeof(rsp), (uint8_t *)&rsp);
}
}
STATIC void process_write_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
if (dlen != 0) {
return; // drop
}
check_and_save_expected_rsp(conn_handle, BT_ATT_OP_WRITE_RSP, dlen, data);
}
STATIC void process_prepare_write_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) {
struct bt_att_prepare_write_req *req = (struct bt_att_prepare_write_req *)data;
if (dlen < sizeof(struct bt_att_prepare_write_req)) {
send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU);
return;
}
uint16_t handle = req->handle;
uint16_t offset = req->offset;
(void)offset;
if (handle > bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj)) {
send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
return;
}
mp_obj_t *attribute = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
if (!mp_obj_is_type(attribute, &bleio_characteristic_type)) {
send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_ATTRIBUTE_NOT_LONG);
return;
}
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute);
if (handle != characteristic->handle) {
send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_ATTRIBUTE_NOT_LONG);
return;
}
if ((characteristic->props & CHAR_PROP_WRITE) == 0) {
send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_WRITE_NOT_PERMITTED);
return;
}
// FIX if (long_write_handle == BLE_GATT_HANDLE_INVALID)
// int valueSize = characteristic->valueSize();
// long_write_value = (uint8_t*)realloc(long_write_value, valueSize);
// long_write_value_length = 0;
// long_write_handle = handle;
// memset(long_write_value, 0x00, valueSize);
// } else if (long_write_handle != handle) {
// send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_UNLIKELY);
// return;
// }
// uint8_t value_length = dlen - sizeof(struct bt_att_prepare_write_req);
// uint8_t* value = &data[sizeof(struct bt_att_prepare_write_req)];
// if ((offset != long_write_value_length) || ((offset + value_length) > (uint16_t)characteristic->valueSize())) {
// send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_INVALID_OFFSET);
// return;
// }
// memcpy(long_write_value + offset, value, value_length);
// long_write_value_length += value_length;
// uint8_t response[mtu];
// uint16_t response_length;
// response[0] = BT_ATT_OP_PREP_WRITE_RSP;
// memcpy(&response[1], data, dlen);
// response_length = dlen + 1;
// hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, response_length, response);
}
STATIC void process_exec_write_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) {
struct bt_att_exec_write_req *req = (struct bt_att_exec_write_req *)data;
if (dlen != sizeof(struct bt_att_exec_write_req)) {
send_error(conn_handle, BT_ATT_OP_EXEC_WRITE_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU);
return;
}
if (long_write_handle && (req->flags & 0x01)) {
// FIX BLELocalCharacteristic* characteristic = (BLELocalCharacteristic*)GATT.attribute(long_write_handle - 1);
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == conn_handle) {
// FIX characteristic->writeValue(BLEDevice(bleio_connections[i].address_type, bleio_connections[i].address), long_write_value, long_write_value_length);
break;
}
}
}
long_write_handle = BLE_GATT_HANDLE_INVALID;
long_write_value_length = 0;
uint8_t response[mtu];
uint16_t response_length;
response[0] = BT_ATT_OP_EXEC_WRITE_RSP;
response_length = 1;
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, response_length, response);
}
STATIC void process_notify_or_indicate(uint16_t conn_handle, uint8_t opcode, uint8_t dlen, uint8_t data[]) {
if (dlen < 2) {
return; // drop
}
// struct bt_att_notify and bt_att_indicate are identical.
// FIXunused struct bt_att_notify *req = (struct bt_att_notify *) data;
// FIXunused uint8_t handle = req->handle;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle != conn_handle) {
continue;
}
// FIX BLERemoteDevice* device = bleio_connections[i].device;
// if (!device) {
// break;
// }
// int serviceCount = device->serviceCount();
// for (size_t i = 0; i < serviceCount; i++) {
// BLERemoteService* s = device->service(i);
// if (s->start_handle() < handle && s->end_handle() >= handle) {
// int characteristicCount = s->characteristicCount();
// for (int j = 0; j < characteristicCount; j++) {
// BLERemoteCharacteristic* c = s->characteristic(j);
// if (c->value_handle() == handle) {
// //FIX c->writeValue(BLEDevice(bleio_connections[i].address_type, bleio_connections[i].address), &data[2], dlen - 2);
// }
// }
// break;
// }
// }
}
if (opcode == BT_ATT_OP_INDICATE) {
// send CONFIRM for INDICATE
uint8_t op_confirm = BT_ATT_OP_CONFIRM;
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, sizeof(op_confirm), &op_confirm);
}
}
STATIC void process_confirm(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
(void)conn_handle;
(void)dlen;
(void)data;
confirm = true;
}
bool att_exchange_mtu(uint16_t conn_handle) {
uint8_t response_buffer[max_mtu];
struct bt_att_exchange_mtu_req req = {
.mtu = max_mtu,
};
return send_req_wait_for_rsp(conn_handle, sizeof(req), (uint8_t *)&req, response_buffer);
}
int att_read_req(uint16_t conn_handle, uint16_t handle, uint8_t response_buffer[]) {
struct __packed {
struct bt_att_hdr h;
struct bt_att_read_req r;
} req = { {
.code = BT_ATT_OP_READ_REQ,
}, {
.handle = handle,
}};
return send_req_wait_for_rsp(conn_handle, sizeof(req), (uint8_t *)&req, response_buffer);
}
int att_write_req(uint16_t conn_handle, uint16_t handle, const uint8_t *data, uint8_t data_len, uint8_t response_buffer[]) {
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_write_req r;
} req_t;
uint8_t req_bytes[sizeof(req_t) + data_len];
req_t *req = (req_t *)req_bytes;
req->h.code = BT_ATT_OP_WRITE_REQ;
req->r.handle = handle;
memcpy(req->r.value, data, data_len);
return send_req_wait_for_rsp(conn_handle, sizeof(req_bytes), req_bytes, response_buffer);
}
void att_write_cmd(uint16_t conn_handle, uint16_t handle, const uint8_t *data, uint8_t data_len) {
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_write_cmd r;
} cmd_t;
uint8_t cmd_bytes[sizeof(cmd_t) + data_len];
cmd_t *cmd = (cmd_t *)cmd_bytes;
cmd->h.code = BT_ATT_OP_WRITE_CMD;
cmd->r.handle = handle;
memcpy(cmd->r.value, data, data_len);
send_req(conn_handle, sizeof(cmd_bytes), cmd_bytes);
}
void att_process_data(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
// Opcode is a single byte at the front of the data.
uint8_t opcode = data[0];
// Skip over opcode.
dlen--;
data++;
uint16_t mtu = att_mtu(conn_handle);
switch (opcode) {
case BT_ATT_OP_ERROR_RSP:
process_error(conn_handle, dlen, data);
break;
case BT_ATT_OP_MTU_REQ:
process_mtu_req(conn_handle, dlen, data);
break;
case BT_ATT_OP_MTU_RSP:
process_mtu_rsp(conn_handle, dlen, data);
break;
case BT_ATT_OP_FIND_INFO_REQ:
process_find_info_req(conn_handle, mtu, dlen, data);
break;
case BT_ATT_OP_FIND_INFO_RSP:
process_find_info_rsp(conn_handle, dlen, data);
break;
case BT_ATT_OP_FIND_TYPE_REQ:
process_find_type_req(conn_handle, mtu, dlen, data);
break;
case BT_ATT_OP_READ_TYPE_REQ:
process_read_type_req(conn_handle, mtu, dlen, data);
break;
case BT_ATT_OP_READ_TYPE_RSP:
process_read_type_rsp(conn_handle, dlen, data);
break;
case BT_ATT_OP_READ_GROUP_REQ:
process_read_group_req(conn_handle, mtu, dlen, data);
break;
case BT_ATT_OP_READ_GROUP_RSP:
process_read_group_rsp(conn_handle, dlen, data);
break;
case BT_ATT_OP_READ_REQ:
case BT_ATT_OP_READ_BLOB_REQ:
process_read_or_read_blob_req(conn_handle, mtu, opcode, dlen, data);
break;
case BT_ATT_OP_READ_RSP:
process_read_rsp(conn_handle, dlen, data);
break;
case BT_ATT_OP_WRITE_REQ:
case BT_ATT_OP_WRITE_CMD:
process_write_req_or_cmd(conn_handle, mtu, opcode, dlen, data);
break;
case BT_ATT_OP_WRITE_RSP:
process_write_rsp(conn_handle, dlen, data);
break;
case BT_ATT_OP_PREPARE_WRITE_REQ:
process_prepare_write_req(conn_handle, mtu, dlen, data);
break;
case BT_ATT_OP_EXEC_WRITE_REQ:
process_exec_write_req(conn_handle, mtu, dlen, data);
break;
case BT_ATT_OP_NOTIFY:
case BT_ATT_OP_INDICATE:
process_notify_or_indicate(conn_handle, opcode, dlen, data);
break;
case BT_ATT_OP_CONFIRM:
process_confirm(conn_handle, dlen, data);
break;
case BT_ATT_OP_READ_MULT_REQ:
case BT_ATT_OP_SIGNED_WRITE_CMD:
default:
send_error(conn_handle, opcode, 0x00, BT_ATT_ERR_NOT_SUPPORTED);
break;
}
}
// FIX Do we need all of these?
static void check_att_err(uint8_t err) {
const compressed_string_t *msg = NULL;
switch (err) {
case 0:
return;
case BT_ATT_ERR_INVALID_HANDLE:
msg = translate("Invalid handle");
break;
case BT_ATT_ERR_READ_NOT_PERMITTED:
msg = translate("Read not permitted");
break;
case BT_ATT_ERR_WRITE_NOT_PERMITTED:
msg = translate("Write not permitted");
break;
case BT_ATT_ERR_INVALID_PDU:
msg = translate("Invalid PDU");
break;
case BT_ATT_ERR_NOT_SUPPORTED:
msg = translate("Not supported");
break;
case BT_ATT_ERR_INVALID_OFFSET:
msg = translate("Invalid offset");
break;
case BT_ATT_ERR_PREPARE_QUEUE_FULL:
msg = translate("Prepare queue full");
break;
case BT_ATT_ERR_ATTRIBUTE_NOT_FOUND:
msg = translate("Attribute not found");
break;
case BT_ATT_ERR_ATTRIBUTE_NOT_LONG:
msg = translate("Attribute not long");
break;
case BT_ATT_ERR_ENCRYPTION_KEY_SIZE:
msg = translate("Encryption key size");
break;
case BT_ATT_ERR_INVALID_ATTRIBUTE_LEN:
msg = translate("Invalid attribute length");
break;
case BT_ATT_ERR_UNLIKELY:
msg = translate("Unlikely");
break;
case BT_ATT_ERR_UNSUPPORTED_GROUP_TYPE:
msg = translate("Unsupported group type");
break;
case BT_ATT_ERR_INSUFFICIENT_RESOURCES:
msg = translate("Insufficient resources");
break;
case BT_ATT_ERR_DB_OUT_OF_SYNC:
msg = translate("DB out of sync");
break;
case BT_ATT_ERR_VALUE_NOT_ALLOWED:
msg = translate("Value not allowed");
break;
}
if (msg) {
mp_raise_bleio_BluetoothError(msg);
}
switch (err) {
case BT_ATT_ERR_AUTHENTICATION:
msg = translate("Insufficient authentication");
break;
case BT_ATT_ERR_INSUFFICIENT_ENCRYPTION:
msg = translate("Insufficient encryption");
break;
}
if (msg) {
mp_raise_bleio_SecurityError(msg);
}
mp_raise_bleio_BluetoothError(translate("Unknown ATT error: 0x%02x"), err);
}
|