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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
95
96
97
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
144
145
146
156
157
158
159
160
161
180
181
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
220
221
222
223
224
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
330
331
332
333
334
335
336
337
338
339
340
342
343
344
345
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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
492
493
494
496
497
498
499
500
501
525
526
527
528
529
530
531
532
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
567
568
569
570
571
572
573
574
575
576
583
584
589
590
591
592
593
594
595
596
597
598
599
600
601
607
608
609
610
611
612
613
614
615
616
617
620
621
622
627
628
629
650
651
652
658
659
660
661
662
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
691
692
693
702
703
706
707
708
709
710
711
712
713
714
719
724
727
732
733
734
735
736
737
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
761
762
763
764
777
778
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
832
833
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
885
886
887
888
889
890
891
892
893
894
895
896
897
898
902
903
904
908
909
910
921
922
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
984
985
986
987
988
991
998
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1028
1030
1031
1032
1033
1034
1035
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
1072
1073
1076
1077
1081
1082
1083
1084
1085
1086
1087
1088
1092
1093
1094
1095
1096
1099
1104
1105
1106
1107
1108
1109
1113
1114
1115
1123
1124
1125
1126
1127
1128
1129
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
1202
1203
1208
1209
1214
1215
1216
1217
1218
1219
1224
1225
1226
1227
1228
1229
1230
1231
1232
1238
1239
1240
1241
1242
1243
1244
1245
1246
1258
1259
1260
1261
1262
1272
1273
1274
1277
1278
1279
1280
1281
1282
1283
1287
1288
1289
1292
1293
1296
1297
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1326
1327
1328
1329
1330
1331
1332
1333
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1350
1351
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1407
1408
1409
1410
1411
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1473
1474
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1526
1527
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
1602
1603
1604
1605
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1650
1651
1652
1655
1656
1657
1658
1659
1660
1661
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
/* ... */
#define BTSTACK_FILE__ "att_server.c"
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#include "btstack_config.h"
#include "ble/att_dispatch.h"
#include "ble/att_db.h"
#include "ble/att_server.h"
#include "ble/core.h"
#include "ble/le_device_db.h"
#include "ble/sm.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_memory.h"
#include "btstack_run_loop.h"
#include "gap.h"
#include "hci.h"
#include "hci_dump.h"
#include "l2cap.h"
#include "btstack_tlv.h"
19 includes#ifdef ENABLE_LE_SIGNED_WRITE
#include "ble/sm.h"
#include "bluetooth_psm.h"
/* ... */
#endif
#ifdef ENABLE_TESTING_SUPPORT
#include <stdio.h>
#endif
#ifndef NVN_NUM_GATT_SERVER_CCC
#define NVN_NUM_GATT_SERVER_CCC 20
#endif
#define ATT_SERVICE_FLAGS_DELAYED_RESPONSE (1<<0u)
static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection);
static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle);
static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle);
static void att_server_handle_can_send_now(void);
static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection);
static void att_server_persistent_ccc_clear(att_server_t * att_server);
static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size);
typedef enum {
ATT_SERVER_RUN_PHASE_1_REQUESTS = 0,
ATT_SERVER_RUN_PHASE_2_INDICATIONS,
ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS,
...} att_server_run_phase_t;
typedef struct {
uint32_t seq_nr;
uint16_t att_handle;
uint8_t value;
uint8_t device_index;
...} persistent_ccc_entry_t;
static btstack_packet_callback_registration_t hci_event_callback_registration;
static btstack_packet_callback_registration_t sm_event_callback_registration;
static btstack_packet_handler_t att_client_packet_handler;
static btstack_linked_list_t service_handlers;
static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration;
static att_read_callback_t att_server_client_read_callback;
static att_write_callback_t att_server_client_write_callback;
static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID;
static uint8_t att_server_flags;
#ifdef ENABLE_GATT_OVER_EATT
typedef struct {
btstack_linked_item_t item;
att_server_t att_server;
att_connection_t att_connection;
uint8_t * receive_buffer;
uint8_t * send_buffer;
...} att_server_eatt_bearer_t;
static att_server_eatt_bearer_t * att_server_eatt_bearer_for_con_handle(hci_con_handle_t con_handle);
static btstack_linked_list_t att_server_eatt_bearer_pool;
static btstack_linked_list_t att_server_eatt_bearer_active;/* ... */
#endif
#ifdef ENABLE_LE_SIGNED_WRITE
static bool att_server_connections_for_state(att_server_state_t state, att_server_t ** att_server_out, att_connection_t ** att_connection_out){
btstack_linked_list_iterator_t it;
hci_connections_get_iterator(&it);
while(btstack_linked_list_iterator_has_next(&it)){
hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
if (hci_connection->att_server.state == state) {
*att_server_out = &hci_connection->att_server;
*att_connection_out = &hci_connection->att_connection;
return true;
}if (hci_connection->att_server.state == state) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
#ifdef ENABLE_GATT_OVER_EATT
btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active);
while(btstack_linked_list_iterator_has_next(&it)){
att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it);
if (eatt_bearer->att_server.state == state) {
*att_server_out = &eatt_bearer->att_server;
*att_connection_out = &eatt_bearer->att_connection;
return true;
}if (eatt_bearer->att_server.state == state) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
/* ... */#endif
return false;
}att_server_connections_for_state (att_server_state_t state, att_server_t ** att_server_out, att_connection_t ** att_connection_out) { ... }
/* ... */#endif
static void att_server_request_can_send_now(att_server_t * att_server, att_connection_t * att_connection ){
switch (att_server->bearer_type){
case ATT_BEARER_UNENHANCED_LE:
#ifdef ENABLE_GATT_OVER_CLASSICcase ATT_BEARER_UNENHANCED_LE:
case ATT_BEARER_UNENHANCED_CLASSIC:
/* ... */
#endif
att_dispatch_server_request_can_send_now_event(att_connection->con_handle);
break;
#ifdef ENABLE_GATT_OVER_EATTcase ATT_BEARER_UNENHANCED_CLASSIC:
case ATT_BEARER_ENHANCED_LE:
l2cap_request_can_send_now_event(att_server->l2cap_cid);
break;/* ... */
#endifcase ATT_BEARER_ENHANCED_LE:
default:
btstack_unreachable();
break;default
}switch (att_server->bearer_type) { ... }
}{ ... }
static bool att_server_can_send_packet(att_server_t * att_server, att_connection_t * att_connection){
switch (att_server->bearer_type) {
case ATT_BEARER_UNENHANCED_LE:
#ifdef ENABLE_GATT_OVER_CLASSICcase ATT_BEARER_UNENHANCED_LE:
case ATT_BEARER_UNENHANCED_CLASSIC:
/* ... */
#endif
return att_dispatch_server_can_send_now(att_connection->con_handle);
#ifdef ENABLE_GATT_OVER_EATTcase ATT_BEARER_UNENHANCED_CLASSIC:
case ATT_BEARER_ENHANCED_LE:
return l2cap_can_send_packet_now(att_server->l2cap_cid);/* ... */
#endifcase ATT_BEARER_ENHANCED_LE:
default:
btstack_unreachable();
break;default
}switch (att_server->bearer_type) { ... }
return false;
}{ ... }
static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
if (!packet_handler) return;
uint8_t event[7];
int pos = 0;
event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
event[pos++] = sizeof(event) - 2u;
event[pos++] = status;
little_endian_store_16(event, pos, client_handle);
pos += 2;
little_endian_store_16(event, pos, attribute_handle);
(*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
}{ ... }
static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
if (att_client_packet_handler != NULL){
(*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
}if (att_client_packet_handler != NULL) { ... }
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &service_handlers);
while (btstack_linked_list_iterator_has_next(&it)){
att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
if (!handler->packet_handler) continue;
(*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}{ ... }
static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
uint8_t event[6];
int pos = 0;
event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
event[pos++] = sizeof(event) - 2u;
little_endian_store_16(event, pos, con_handle);
pos += 2;
little_endian_store_16(event, pos, mtu);
att_dispatch_server_mtu_exchanged(con_handle, mtu);
att_emit_event_to_all(&event[0], sizeof(event));
}{ ... }
static void att_emit_can_send_now_event(void * context){
UNUSED(context);
if (!att_client_packet_handler) return;
uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
(*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
}{ ... }
static void att_emit_connected_event(att_server_t * att_server, att_connection_t * att_connection){
uint8_t event[11];
int pos = 0;
event[pos++] = ATT_EVENT_CONNECTED;
event[pos++] = sizeof(event) - 2u;
event[pos++] = att_server->peer_addr_type;
reverse_bd_addr(att_server->peer_address, &event[pos]);
pos += 6;
little_endian_store_16(event, pos, att_connection->con_handle);
pos += 2;
att_emit_event_to_all(&event[0], sizeof(event));
}{ ... }
static void att_emit_disconnected_event(uint16_t con_handle){
uint8_t event[4];
int pos = 0;
event[pos++] = ATT_EVENT_DISCONNECTED;
event[pos++] = sizeof(event) - 2u;
little_endian_store_16(event, pos, con_handle);
pos += 2;
att_emit_event_to_all(&event[0], sizeof(event));
}{ ... }
static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
void * context = btstack_run_loop_get_timer_context(ts);
hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) return;
att_server_t * att_server = &hci_connection->att_server;
uint16_t att_handle = att_server->value_indication_handle;
att_connection_t * att_connection = &hci_connection->att_connection;
att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle);
}{ ... }
static void att_server_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
att_server_t * att_server;
att_connection_t * att_connection;
hci_con_handle_t con_handle;
hci_connection_t * hci_connection;
switch (packet_type) {
case HCI_EVENT_PACKET:
switch (hci_event_packet_get_type(packet)) {
case HCI_EVENT_META_GAP:
switch (hci_event_gap_meta_get_subevent_code(packet)) {
case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
con_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) break;
att_server = &hci_connection->att_server;
att_server->peer_addr_type = gap_subevent_le_connection_complete_get_peer_address_type(packet);
gap_subevent_le_connection_complete_get_peer_address(packet, att_server->peer_address);
att_connection = &hci_connection->att_connection;
att_connection->con_handle = con_handle;
att_server->state = ATT_SERVER_IDLE;
att_server->bearer_type = ATT_BEARER_UNENHANCED_LE;
att_connection->mtu = ATT_DEFAULT_MTU;
att_connection->max_mtu = l2cap_max_le_mtu();
if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
}if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE) { ... }
att_connection->encryption_key_size = 0u;
att_connection->authenticated = 0u;
att_connection->authorized = 0u;
att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
att_server->ir_lookup_active = false;
att_server->pairing_active = false;
att_emit_connected_event(att_server, att_connection);
break;case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
default:
break;default
}switch (hci_event_gap_meta_get_subevent_code(packet)) { ... }
break;
case HCI_EVENT_META_GAP:
case HCI_EVENT_LE_META:
switch (hci_event_le_meta_get_subevent_code(packet)) {
case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
att_emit_event_to_all(packet, size);
break;case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
default:
break;default
}switch (hci_event_le_meta_get_subevent_code(packet)) { ... }
break;
case HCI_EVENT_LE_META:
case HCI_EVENT_ENCRYPTION_CHANGE:
case HCI_EVENT_ENCRYPTION_CHANGE_V2:
case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
con_handle = little_endian_read_16(packet, 3);
hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) break;
if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break;
att_server = &hci_connection->att_server;
att_connection = &hci_connection->att_connection;
att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0;
att_connection->secure_connection = gap_secure_connection(con_handle) ? 1 : 0;
log_info("encrypted key size %u, authenticated %u, secure connection %u",
att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
if (hci_event_encryption_change_get_encryption_enabled(packet) != 0){
att_server_persistent_ccc_restore(att_server, att_connection);
}if (hci_event_encryption_change_get_encryption_enabled(packet) != 0) { ... }
}if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE) { ... }
att_run_for_context(att_server, att_connection);
break;
case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
case HCI_EVENT_DISCONNECTION_COMPLETE:
con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) break;
att_server = &hci_connection->att_server;
att_connection = &hci_connection->att_connection;
att_clear_transaction_queue(att_connection);
att_connection->con_handle = 0;
att_server->pairing_active = false;
att_server->state = ATT_SERVER_IDLE;
if (att_server->value_indication_handle != 0u){
btstack_run_loop_remove_timer(&att_server->value_indication_timer);
uint16_t att_handle = att_server->value_indication_handle;
att_server->value_indication_handle = 0u;
att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle);
}if (att_server->value_indication_handle != 0u) { ... }
att_emit_disconnected_event(con_handle);
att_emit_event_to_all(packet, size);
break;
case HCI_EVENT_DISCONNECTION_COMPLETE:
case SM_EVENT_IDENTITY_RESOLVING_STARTED:
con_handle = sm_event_identity_resolving_started_get_handle(packet);
hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) break;
att_server = &hci_connection->att_server;
log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
att_server->ir_lookup_active = true;
break;case SM_EVENT_IDENTITY_RESOLVING_STARTED:
case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
con_handle = sm_event_identity_created_get_handle(packet);
hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) return;
att_connection = &hci_connection->att_connection;
att_server = &hci_connection->att_server;
att_server->ir_lookup_active = false;
att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
att_run_for_context(att_server, att_connection);
break;case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
case SM_EVENT_IDENTITY_RESOLVING_FAILED:
con_handle = sm_event_identity_resolving_failed_get_handle(packet);
hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) break;
att_connection = &hci_connection->att_connection;
att_server = &hci_connection->att_server;
log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
att_server->ir_lookup_active = false;
att_server->ir_le_device_db_index = -1;
att_run_for_context(att_server, att_connection);
break;
case SM_EVENT_IDENTITY_RESOLVING_FAILED:
case SM_EVENT_JUST_WORKS_REQUEST:
case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
case SM_EVENT_PASSKEY_INPUT_NUMBER:
case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
con_handle = sm_event_just_works_request_get_handle(packet);
hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) break;
att_server = &hci_connection->att_server;
log_info("SM Pairing started");
att_server->pairing_active = true;
if (att_server->ir_le_device_db_index < 0) break;
att_server_persistent_ccc_clear(att_server);
att_server->ir_le_device_db_index = -1;
break;
case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
case SM_EVENT_IDENTITY_CREATED:
con_handle = sm_event_identity_created_get_handle(packet);
hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) return;
att_connection = &hci_connection->att_connection;
att_server = &hci_connection->att_server;
att_server->pairing_active = false;
att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
att_run_for_context(att_server, att_connection);
break;
case SM_EVENT_IDENTITY_CREATED:
case SM_EVENT_PAIRING_COMPLETE:
con_handle = sm_event_pairing_complete_get_handle(packet);
hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) return;
att_connection = &hci_connection->att_connection;
att_server = &hci_connection->att_server;
att_server->pairing_active = false;
att_run_for_context(att_server, att_connection);
break;
case SM_EVENT_PAIRING_COMPLETE:
case SM_EVENT_AUTHORIZATION_RESULT: {
con_handle = sm_event_authorization_result_get_handle(packet);
hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) break;
att_connection = &hci_connection->att_connection;
att_server = &hci_connection->att_server;
att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet);
att_server_request_can_send_now(att_server, att_connection);
break;
...}case SM_EVENT_AUTHORIZATION_RESULT:
default:
break;default
}switch (hci_event_packet_get_type(packet)) { ... }
break;case HCI_EVENT_PACKET:
default:
break;default
}switch (packet_type) { ... }
}{ ... }
static uint8_t
att_server_send_prepared(const att_server_t *att_server, const att_connection_t *att_connection, uint8_t *buffer,
uint16_t size) {
UNUSED(buffer);
uint8_t status = ERROR_CODE_SUCCESS;
switch (att_server->bearer_type) {
case ATT_BEARER_UNENHANCED_LE:
status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
break;
#ifdef ENABLE_GATT_OVER_CLASSICcase ATT_BEARER_UNENHANCED_LE:
case ATT_BEARER_UNENHANCED_CLASSIC:
status = l2cap_send_prepared(att_server->l2cap_cid, size);
break;/* ... */
#endif
#ifdef ENABLE_GATT_OVER_EATTcase ATT_BEARER_UNENHANCED_CLASSIC:
case ATT_BEARER_ENHANCED_LE:
btstack_assert(buffer != NULL);
status = l2cap_send(att_server->l2cap_cid, buffer, size);
break;/* ... */
#endifcase ATT_BEARER_ENHANCED_LE:
default:
btstack_unreachable();
break;default
}switch (att_server->bearer_type) { ... }
return status;
}{ ... }
#ifdef ENABLE_LE_SIGNED_WRITE
static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
att_server_t * att_server = NULL;
att_connection_t * att_connection = NULL;
bool found = att_server_connections_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION, &att_server, &att_connection);
if (found == false){
return;
}if (found == false) { ... }
uint8_t hash_flipped[8];
reverse_64(hash, hash_flipped);
if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8) != 0){
log_info("ATT Signed Write, invalid signature");
#ifdef ENABLE_TESTING_SUPPORT
printf("ATT Signed Write, invalid signature\n");
#endif
att_server->state = ATT_SERVER_IDLE;
return;
}if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8) != 0) { ... }
log_info("ATT Signed Write, valid signature");
#ifdef ENABLE_TESTING_SUPPORT
printf("ATT Signed Write, valid signature\n");
#endif
uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
att_server_request_can_send_now(att_server, att_connection);
}att_signed_write_handle_cmac_result (uint8_t hash[8]) { ... }
/* ... */#endif
#ifdef ENABLE_ATT_DELAYED_RESPONSE
static void att_server_handle_response_pending(att_server_t *att_server, const att_connection_t *att_connection,
const uint8_t *eatt_buffer,
uint16_t att_response_size) {
if (eatt_buffer == NULL){
l2cap_release_packet_buffer();
}if (eatt_buffer == NULL) { ... }
att_server->state = ATT_SERVER_RESPONSE_PENDING;
if (att_response_size == ATT_READ_RESPONSE_PENDING){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &service_handlers);
while (btstack_linked_list_iterator_has_next(&it)) {
att_service_handler_t *handler = (att_service_handler_t *) btstack_linked_list_iterator_next(&it);
if ((handler->flags & ATT_SERVICE_FLAGS_DELAYED_RESPONSE) != 0){
handler->flags &= ~ATT_SERVICE_FLAGS_DELAYED_RESPONSE;
handler->read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
}if ((handler->flags & ATT_SERVICE_FLAGS_DELAYED_RESPONSE) != 0) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
if ((att_server_flags & ATT_SERVICE_FLAGS_DELAYED_RESPONSE) != 0){
btstack_assert(att_server_client_read_callback != NULL);
(*att_server_client_read_callback)(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
}if ((att_server_flags & ATT_SERVICE_FLAGS_DELAYED_RESPONSE) != 0) { ... }
}if (att_response_size == ATT_READ_RESPONSE_PENDING) { ... }
}att_server_handle_response_pending (att_server_t *att_server, const att_connection_t *att_connection, const uint8_t *eatt_buffer, uint16_t att_response_size) { ... }
/* ... */#endif
static int
att_server_process_validated_request(att_server_t *att_server, att_connection_t *att_connection, uint8_t *eatt_buffer) {
uint8_t * att_response_buffer;
if (eatt_buffer != NULL){
att_response_buffer = eatt_buffer;
}if (eatt_buffer != NULL) { ... } else {
l2cap_reserve_packet_buffer();
att_response_buffer = l2cap_get_outgoing_buffer();
}else { ... }
uint16_t att_response_size = 0;
if ((att_server->bearer_type != ATT_BEARER_UNENHANCED_LE) && (att_server->request_buffer[0] == ATT_EXCHANGE_MTU_REQUEST)){
att_response_size = 5;
att_response_buffer[0] = ATT_ERROR_RESPONSE;
att_response_buffer[1] = ATT_EXCHANGE_MTU_REQUEST;
att_response_buffer[2] = 0;
att_response_buffer[3] = 0;
att_response_buffer[4] = ATT_ERROR_REQUEST_NOT_SUPPORTED;
}if ((att_server->bearer_type != ATT_BEARER_UNENHANCED_LE) && (att_server->request_buffer[0] == ATT_EXCHANGE_MTU_REQUEST)) { ... } else {
att_response_size = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
}else { ... }
#ifdef ENABLE_ATT_DELAYED_RESPONSE
if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){
att_server_handle_response_pending(att_server, att_connection, eatt_buffer, att_response_size);
return 0;
}if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)) { ... }
/* ... */#endif
if ((att_response_size >= 4u)
&& (att_response_buffer[0] == ATT_ERROR_RESPONSE)
&& (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
&& (att_connection->authenticated != 0u)){
switch (gap_authorization_state(att_connection->con_handle)){
case AUTHORIZATION_UNKNOWN:
if (eatt_buffer == NULL){
l2cap_release_packet_buffer();
}if (eatt_buffer == NULL) { ... }
sm_request_pairing(att_connection->con_handle);
return 0;case AUTHORIZATION_UNKNOWN:
case AUTHORIZATION_PENDING:
if (eatt_buffer == NULL){
l2cap_release_packet_buffer();
}if (eatt_buffer == NULL) { ... }
return 0;case AUTHORIZATION_PENDING:
default:
break;default
}switch (gap_authorization_state(att_connection->con_handle)) { ... }
}if ((att_response_size >= 4u) && (att_response_buffer[0] == ATT_ERROR_RESPONSE) && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) && (att_connection->authenticated != 0u)) { ... }
att_server->state = ATT_SERVER_IDLE;
if (att_response_size == 0u) {
if (eatt_buffer == NULL){
l2cap_release_packet_buffer();
}if (eatt_buffer == NULL) { ... }
return 0;
}if (att_response_size == 0u) { ... }
(void) att_server_send_prepared(att_server, att_connection, eatt_buffer, att_response_size);
if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
att_emit_mtu_event(att_connection->con_handle, att_connection->mtu);
}if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE) { ... }
return 1;
}{ ... }
#ifdef ENABLE_ATT_DELAYED_RESPONSE
uint8_t att_server_response_ready(hci_con_handle_t con_handle){
hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
att_server_t * att_server = &hci_connection->att_server;
att_connection_t * att_connection = &hci_connection->att_connection;
if (att_server->state != ATT_SERVER_RESPONSE_PENDING) return ERROR_CODE_COMMAND_DISALLOWED;
att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
att_server_request_can_send_now(att_server, att_connection);
return ERROR_CODE_SUCCESS;
}att_server_response_ready (hci_con_handle_t con_handle) { ... }
/* ... */#endif
static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection){
switch (att_server->state){
case ATT_SERVER_REQUEST_RECEIVED:
switch (att_server->bearer_type){
case ATT_BEARER_UNENHANCED_LE:
if (gap_reconnect_security_setup_active(att_connection->con_handle)) {
return;
}if (gap_reconnect_security_setup_active(att_connection->con_handle)) { ... };
break;
#if defined(ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT)
#ifdef ENABLE_GATT_OVER_CLASSIC
case ATT_BEARER_UNENHANCED_CLASSIC:
/* ... */
#endif
#ifdef ENABLE_GATT_OVER_EATTcase ATT_BEARER_UNENHANCED_CLASSIC:
case ATT_BEARER_ENHANCED_LE:
#endif
break;/* ... */
#endif
default:
btstack_unreachable();
break;default
}switch (att_server->bearer_type) { ... }
if (att_server->pairing_active) break;
#ifdef ENABLE_LE_SIGNED_WRITE
if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
log_info("ATT Signed Write!");
if (!sm_cmac_ready()) {
log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
att_server->state = ATT_SERVER_IDLE;
return;
}if (!sm_cmac_ready()) { ... }
if (att_server->request_size < (3 + 12)) {
log_info("ATT Signed Write, request to short. Abort.");
att_server->state = ATT_SERVER_IDLE;
return;
}if (att_server->request_size < (3 + 12)) { ... }
if (att_server->ir_lookup_active){
return;
}if (att_server->ir_lookup_active) { ... }
if (att_server->ir_le_device_db_index < 0){
log_info("ATT Signed Write, CSRK not available");
att_server->state = ATT_SERVER_IDLE;
return;
}if (att_server->ir_le_device_db_index < 0) { ... }
uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
if (counter_packet < counter_db){
log_info("ATT Signed Write, db reports higher counter, abort");
att_server->state = ATT_SERVER_IDLE;
return;
}if (counter_packet < counter_db) { ... }
sm_key_t csrk;
le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
log_info("Orig Signature: ");
log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
sm_cmac_signed_write_start(csrk, att_server->request_buffer[0], attribute_handle, att_server->request_size - 15, &att_server->request_buffer[3], counter_packet, att_signed_write_handle_cmac_result);
return;
}if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND) { ... } /* ... */
#endif
att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
att_server_request_can_send_now(att_server, att_connection);
break;
case ATT_SERVER_REQUEST_RECEIVED:
default:
break;default
}switch (att_server->state) { ... }
}{ ... }
static bool att_server_data_ready_for_phase(att_server_t * att_server, att_server_run_phase_t phase){
switch (phase){
case ATT_SERVER_RUN_PHASE_1_REQUESTS:
return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;case ATT_SERVER_RUN_PHASE_1_REQUESTS:
case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u));case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
return (!btstack_linked_list_empty(&att_server->notification_requests));case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
default:
btstack_assert(false);
return false;default
}switch (phase) { ... }
}{ ... }
static void att_server_trigger_send_for_phase(att_server_t * att_server, att_connection_t * att_connection, att_server_run_phase_t phase){
btstack_context_callback_registration_t * client;
switch (phase){
case ATT_SERVER_RUN_PHASE_1_REQUESTS:
att_server_process_validated_request(att_server, att_connection, NULL);
break;case ATT_SERVER_RUN_PHASE_1_REQUESTS:
case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
client = (btstack_context_callback_registration_t*) att_server->indication_requests;
btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
client->callback(client->context);
break;case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
client = (btstack_context_callback_registration_t*) att_server->notification_requests;
btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
client->callback(client->context);
break;case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
default:
btstack_assert(false);
break;default
}switch (phase) { ... }
}{ ... }
static void att_server_handle_can_send_now(void){
hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
hci_connection_t * request_hci_connection = NULL;
bool can_send_now = true;
uint8_t phase_index;
for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
while (true){
btstack_linked_list_iterator_t it;
hci_connections_get_iterator(&it);
while(btstack_linked_list_iterator_has_next(&it)){
hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
att_server_t * att_server = &connection->att_server;
att_connection_t * att_connection = &connection->att_connection;
bool data_ready = att_server_data_ready_for_phase(att_server, phase);
if (skip_connections_until != HCI_CON_HANDLE_INVALID){
if (data_ready && (request_hci_connection == NULL)){
request_hci_connection = connection;
}if (data_ready && (request_hci_connection == NULL)) { ... }
if (skip_connections_until == att_connection->con_handle){
skip_connections_until = HCI_CON_HANDLE_INVALID;
}if (skip_connections_until == att_connection->con_handle) { ... }
continue;
}if (skip_connections_until != HCI_CON_HANDLE_INVALID) { ... };
if (data_ready){
if (can_send_now){
att_server_trigger_send_for_phase(att_server, att_connection, phase);
last_send_con_handle = att_connection->con_handle;
can_send_now = att_server_can_send_packet(att_server, att_connection);
data_ready = att_server_data_ready_for_phase(att_server, phase);
if (data_ready && (request_hci_connection == NULL)){
request_hci_connection = connection;
}if (data_ready && (request_hci_connection == NULL)) { ... }
}if (can_send_now) { ... } else {
request_hci_connection = connection;
break;
}else { ... }
}if (data_ready) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
skip_connections_until = HCI_CON_HANDLE_INVALID;
if (!can_send_now) break;
if (request_hci_connection == NULL) break;
request_hci_connection = NULL;
}while (true) { ... }
if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
att_server_last_can_send_now = last_send_con_handle;
}if (last_send_con_handle != HCI_CON_HANDLE_INVALID) { ... }
}for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++) { ... }
if (request_hci_connection == NULL) return;
att_server_t * att_server = &request_hci_connection->att_server;
att_connection_t * att_connection = &request_hci_connection->att_connection;
att_server_request_can_send_now(att_server, att_connection);
}{ ... }
static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size){
uint8_t opcode = packet[0u];
uint8_t method = opcode & 0x03fu;
bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF;
bool command = (opcode & 0x40u) != 0u;
if (invalid && command){
return;
}if (invalid && command) { ... }
if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){
btstack_run_loop_remove_timer(&att_server->value_indication_timer);
uint16_t att_handle = att_server->value_indication_handle;
att_server->value_indication_handle = 0u;
att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle);
att_server_request_can_send_now(att_server, att_connection);
return;
}if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)) { ... }
if (opcode == ATT_WRITE_COMMAND){
att_handle_request(att_connection, packet, size, NULL);
return;
}if (opcode == ATT_WRITE_COMMAND) { ... }
if (size > sizeof(att_server->request_buffer)) {
log_info("drop att pdu 0x%02x as size %u > att_server->request_buffer %u", packet[0], size, (int) sizeof(att_server->request_buffer));
return;
}if (size > sizeof(att_server->request_buffer)) { ... }
#ifdef ENABLE_LE_SIGNED_WRITE
if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
log_info("skip new signed write request as previous is in validation");
return;
}if (packet[0] == ATT_SIGNED_WRITE_COMMAND) { ... } else {
log_info("abort signed write validation to process new request");
att_server->state = ATT_SERVER_IDLE;
}else { ... }
}if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION) { ... }
/* ... */#endif
if (att_server->state != ATT_SERVER_IDLE){
log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
return;
}if (att_server->state != ATT_SERVER_IDLE) { ... }
att_server->state = ATT_SERVER_REQUEST_RECEIVED;
att_server->request_size = size;
(void)memcpy(att_server->request_buffer, packet, size);
att_run_for_context(att_server, att_connection);
}{ ... }
static void att_server_dispatch_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
hci_connection_t * hci_connection;
att_connection_t * att_connection;
att_server_t * att_server;
#ifdef ENABLE_GATT_OVER_CLASSIC
hci_con_handle_t con_handle;
bd_addr_t address;
btstack_linked_list_iterator_t it;/* ... */
#endif
switch (packet_type){
case HCI_EVENT_PACKET:
switch (packet[0]){
#ifdef ENABLE_GATT_OVER_CLASSIC
case L2CAP_EVENT_CHANNEL_OPENED:
con_handle = l2cap_event_channel_opened_get_handle(packet);
hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) break;
att_server = &hci_connection->att_server;
att_server->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC;
att_server->peer_addr_type = BD_ADDR_TYPE_ACL;
l2cap_event_channel_opened_get_address(packet, att_server->peer_address);
att_connection = &hci_connection->att_connection;
att_connection->con_handle = con_handle;
att_server->state = ATT_SERVER_IDLE;
att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
att_connection->max_mtu = l2cap_max_mtu();
if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
}if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE) { ... }
log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu);
att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
att_connection->authenticated = gap_authenticated(con_handle);
att_connection->secure_connection = gap_secure_connection(con_handle);
log_info("encrypted key size %u, authenticated %u, secure connection %u",
att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
att_emit_connected_event(att_server, att_connection);
if ( gap_security_level(con_handle) >= LEVEL_2){
att_server_persistent_ccc_restore(att_server, att_connection);
}if (gap_security_level(con_handle) >= LEVEL_2) { ... }
att_server->pairing_active = 0;
break;/* ... */
#endifcase L2CAP_EVENT_CHANNEL_OPENED:
case L2CAP_EVENT_CAN_SEND_NOW:
att_server_handle_can_send_now();
break;case L2CAP_EVENT_CAN_SEND_NOW:
case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
hci_connection = hci_connection_for_handle(channel);
if (!hci_connection) break;
att_connection = &hci_connection->att_connection;
att_connection->mtu = little_endian_read_16(packet, 4);
break;case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
default:
break;default
}switch (packet[0]) { ... }
break;
case HCI_EVENT_PACKET:
case ATT_DATA_PACKET:
log_debug("ATT Packet, handle 0x%04x", channel);
hci_connection = hci_connection_for_handle(channel);
if (!hci_connection) break;
att_server = &hci_connection->att_server;
att_connection = &hci_connection->att_connection;
att_server_handle_att_pdu(att_server, att_connection, packet, size);
break;
#ifdef ENABLE_GATT_OVER_CLASSICcase ATT_DATA_PACKET:
case L2CAP_DATA_PACKET:
hci_connections_get_iterator(&it);
while(btstack_linked_list_iterator_has_next(&it)){
hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
att_server = &hci_connection->att_server;
att_connection = &hci_connection->att_connection;
if (att_server->l2cap_cid == channel) {
att_server_handle_att_pdu(att_server, att_connection, packet, size);
break;
}if (att_server->l2cap_cid == channel) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
break;/* ... */
#endif
case L2CAP_DATA_PACKET:
default:
break;default
}switch (packet_type) { ... }
}{ ... }
static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index;
}{ ... }
static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) return;
att_server_t * att_server = &hci_connection->att_server;
int le_device_index = att_server->ir_le_device_db_index;
log_info("Store CCC value 0x%04x for handle 0x%04x of remote %s, le device id %d", value, att_handle, bd_addr_to_str(att_server->peer_address), le_device_index);
if (le_device_index < 0) return;
const btstack_tlv_t * tlv_impl = NULL;
void * tlv_context;
btstack_tlv_get_instance(&tlv_impl, &tlv_context);
if (!tlv_impl) return;
int index;
uint32_t highest_seq_nr = 0;
uint32_t lowest_seq_nr = 0;
uint32_t tag_for_lowest_seq_nr = 0;
uint32_t tag_for_empty = 0;
persistent_ccc_entry_t entry;
for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){
uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
if (len != sizeof(persistent_ccc_entry_t)){
tag_for_empty = tag;
continue;
}if (len != sizeof(persistent_ccc_entry_t)) { ... }
if (entry.seq_nr > highest_seq_nr){
highest_seq_nr = entry.seq_nr;
}if (entry.seq_nr > highest_seq_nr) { ... }
if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){
tag_for_lowest_seq_nr = tag;
lowest_seq_nr = entry.seq_nr;
}if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)) { ... }
if (entry.device_index != le_device_index) continue;
if (entry.att_handle != att_handle) continue;
if (value != 0){
if (entry.value == value) {
log_info("CCC Index %u: Up-to-date", index);
return;
}if (entry.value == value) { ... }
entry.value = (uint8_t) value;
entry.seq_nr = highest_seq_nr + 1u;
log_info("CCC Index %u: Store", index);
int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
if (result != 0){
log_error("Store tag index %u failed", index);
}if (result != 0) { ... }
}if (value != 0) { ... } else {
log_info("CCC Index %u: Delete", index);
tlv_impl->delete_tag(tlv_context, tag);
}else { ... }
return;
}for (index=0; index
log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
if (value == 0u){
return;
}if (value == 0u) { ... }
uint32_t tag_to_use = 0u;
if (tag_for_empty != 0u){
tag_to_use = tag_for_empty;
}if (tag_for_empty != 0u) { ... } else if (tag_for_lowest_seq_nr != 0){
tag_to_use = tag_for_lowest_seq_nr;
}else if (tag_for_lowest_seq_nr != 0) { ... } else {
return;
}else { ... }
entry.seq_nr = highest_seq_nr + 1u;
entry.device_index = le_device_index;
entry.att_handle = att_handle;
entry.value = (uint8_t) value;
int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
if (result != 0){
log_error("Store tag index %u failed", index);
}if (result != 0) { ... }
}{ ... }
static void att_server_persistent_ccc_clear(att_server_t * att_server){
int le_device_index = att_server->ir_le_device_db_index;
log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
if (le_device_index < 0) return;
const btstack_tlv_t * tlv_impl = NULL;
void * tlv_context;
btstack_tlv_get_instance(&tlv_impl, &tlv_context);
if (!tlv_impl) return;
int index;
persistent_ccc_entry_t entry;
for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
if (len != sizeof(persistent_ccc_entry_t)) continue;
if (entry.device_index != le_device_index) continue;
log_info("CCC Index %u: Delete", index);
tlv_impl->delete_tag(tlv_context, tag);
}for (index=0;index
}{ ... }
static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection){
int le_device_index = att_server->ir_le_device_db_index;
log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
if (le_device_index < 0) return;
const btstack_tlv_t * tlv_impl = NULL;
void * tlv_context;
btstack_tlv_get_instance(&tlv_impl, &tlv_context);
if (!tlv_impl) return;
int index;
persistent_ccc_entry_t entry;
for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
if (len != sizeof(persistent_ccc_entry_t)) continue;
if (entry.device_index != le_device_index) continue;
uint16_t attribute_handle = entry.att_handle;
uint8_t value[2];
little_endian_store_16(value, 0, entry.value);
att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
if (!callback) continue;
log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
(*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
}for (index=0;index
}{ ... }
static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &service_handlers);
while (btstack_linked_list_iterator_has_next(&it)){
att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
if (handler->start_handle > handle) continue;
if (handler->end_handle < handle) continue;
return handler;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}{ ... }
static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
att_service_handler_t * handler = att_service_handler_for_handle(handle);
if (handler != NULL) return handler->write_callback;
return att_server_client_write_callback;
}{ ... }
static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
att_service_handler_t * handler = att_service_handler_for_handle(handle);
if (handler != NULL) return handler->packet_handler;
return att_client_packet_handler;
}{ ... }
static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &service_handlers);
while (btstack_linked_list_iterator_has_next(&it)){
att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
if (!handler->write_callback) continue;
(*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
if (!att_server_client_write_callback) return;
(*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
}{ ... }
static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &service_handlers);
while (btstack_linked_list_iterator_has_next(&it)){
att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
if (!handler->write_callback) continue;
uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
if (error_code != 0u) return error_code;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
if (!att_server_client_write_callback) return 0;
return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
}{ ... }
static uint16_t att_server_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
att_service_handler_t * service = att_service_handler_for_handle(attribute_handle);
att_read_callback_t read_callback = (service != NULL) ? service->read_callback : att_server_client_read_callback;
uint16_t result = 0;
if (read_callback != NULL){
result = (*read_callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
#ifdef ENABLE_ATT_DELAYED_RESPONSE
if (result == ATT_READ_RESPONSE_PENDING){
if (service == NULL){
att_server_flags |= ATT_SERVICE_FLAGS_DELAYED_RESPONSE;
}if (service == NULL) { ... } else {
service->flags |= ATT_SERVICE_FLAGS_DELAYED_RESPONSE;
}else { ... }
}if (result == ATT_READ_RESPONSE_PENDING) { ... }
/* ... */#endif
}if (read_callback != NULL) { ... }
return result;
}{ ... }
static int att_server_write_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
switch (transaction_mode){
case ATT_TRANSACTION_MODE_VALIDATE:
return att_validate_prepared_write(con_handle);case ATT_TRANSACTION_MODE_VALIDATE:
case ATT_TRANSACTION_MODE_EXECUTE:
case ATT_TRANSACTION_MODE_CANCEL:
att_notify_write_callbacks(con_handle, transaction_mode);
return 0;case ATT_TRANSACTION_MODE_CANCEL:
default:
break;default
}switch (transaction_mode) { ... }
if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){
att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
}if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)) { ... }
att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
if (!callback) return 0;
return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
}{ ... }
/* ... */
void att_server_register_service_handler(att_service_handler_t * handler){
bool att_server_registered = false;
if (att_service_handler_for_handle(handler->start_handle) != NULL){
att_server_registered = true;
}if (att_service_handler_for_handle(handler->start_handle) != NULL) { ... }
if (att_service_handler_for_handle(handler->end_handle) != NULL){
att_server_registered = true;
}if (att_service_handler_for_handle(handler->end_handle) != NULL) { ... }
if (att_server_registered){
log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
return;
}if (att_server_registered) { ... }
handler->flags = 0;
btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
}{ ... }
void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
att_server_client_read_callback = read_callback;
att_server_client_write_callback = write_callback;
hci_event_callback_registration.callback = &att_server_event_packet_handler;
hci_add_event_handler(&hci_event_callback_registration);
sm_event_callback_registration.callback = &att_server_event_packet_handler;
sm_add_event_handler(&sm_event_callback_registration);
att_dispatch_register_server(att_server_dispatch_packet_handler);
#ifdef ENABLE_GATT_OVER_CLASSIC
att_dispatch_classic_register_service();/* ... */
#endif
att_set_db(db);
att_set_read_callback(att_server_read_callback);
att_set_write_callback(att_server_write_callback);
}{ ... }
void att_server_register_packet_handler(btstack_packet_handler_t handler){
att_client_packet_handler = handler;
}{ ... }
int att_server_can_send_packet_now(hci_con_handle_t con_handle){
hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) return 0;
att_server_t * att_server = &hci_connection->att_server;
att_connection_t * att_connection = &hci_connection->att_connection;
return att_server_can_send_packet(att_server, att_connection);
}{ ... }
uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
return att_server_request_to_send_notification(callback_registration, con_handle);
}{ ... }
void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
}{ ... }
uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
att_server_t * att_server = &hci_connection->att_server;
att_connection_t * att_connection = &hci_connection->att_connection;
bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
att_server_request_can_send_now(att_server, att_connection);
if (added){
return ERROR_CODE_SUCCESS;
}if (added) { ... } else {
return ERROR_CODE_COMMAND_DISALLOWED;
}else { ... }
}{ ... }
uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
att_server_t * att_server = &hci_connection->att_server;
att_connection_t * att_connection = &hci_connection->att_connection;
bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
att_server_request_can_send_now(att_server, att_connection);
if (added){
return ERROR_CODE_SUCCESS;
}if (added) { ... } else {
return ERROR_CODE_COMMAND_DISALLOWED;
}else { ... }
}{ ... }
static uint8_t att_server_prepare_server_message(hci_con_handle_t con_handle, att_server_t ** out_att_server, att_connection_t ** out_att_connection, uint8_t ** out_packet_buffer){
att_server_t * att_server = NULL;
att_connection_t * att_connection = NULL;
uint8_t * packet_buffer = NULL;
#ifdef ENABLE_GATT_OVER_EATT
att_server_eatt_bearer_t * eatt_bearer = att_server_eatt_bearer_for_con_handle(con_handle);
if (eatt_bearer != NULL){
att_server = &eatt_bearer->att_server;
att_connection = &eatt_bearer->att_connection;
packet_buffer = eatt_bearer->send_buffer;
}if (eatt_bearer != NULL) { ... } else
#endif
{
hci_connection_t *hci_connection = hci_connection_for_handle(con_handle);
if (hci_connection != NULL) {
att_server = &hci_connection->att_server;
att_connection = &hci_connection->att_connection;
}if (hci_connection != NULL) { ... }
}else { ... }
if (att_server == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL;
if (packet_buffer == NULL){
l2cap_reserve_packet_buffer();
packet_buffer = l2cap_get_outgoing_buffer();
}if (packet_buffer == NULL) { ... }
*out_att_connection = att_connection;
*out_att_server = att_server;
*out_packet_buffer = packet_buffer;
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
att_server_t * att_server = NULL;
att_connection_t * att_connection = NULL;
uint8_t * packet_buffer = NULL;
uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer);
return att_server_send_prepared(att_server, att_connection, NULL, size);
}{ ... }
/* ... */
uint8_t att_server_multiple_notify(hci_con_handle_t con_handle, uint8_t num_attributes,
const uint16_t * attribute_handles, const uint8_t ** values_data, const uint16_t * values_len){
att_server_t * att_server = NULL;
att_connection_t * att_connection = NULL;
uint8_t * packet_buffer = NULL;
uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
uint16_t size = att_prepare_handle_value_multiple_notification(att_connection, num_attributes, attribute_handles, values_data, values_len, packet_buffer);
return att_server_send_prepared(att_server, att_connection, packet_buffer, size);
}{ ... }
uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
att_server_t * att_server = NULL;
att_connection_t * att_connection = NULL;
uint8_t * packet_buffer = NULL;
uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
if (att_server->value_indication_handle != 0u) {
if (att_server->bearer_type == ATT_BEARER_ENHANCED_LE){
l2cap_release_packet_buffer();
}if (att_server->bearer_type == ATT_BEARER_ENHANCED_LE) { ... }
return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
}if (att_server->value_indication_handle != 0u) { ... }
att_server->value_indication_handle = attribute_handle;
btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
btstack_run_loop_add_timer(&att_server->value_indication_timer);
uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer);
return att_server_send_prepared(att_server, att_connection, NULL, size);
}{ ... }
uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) return 0;
att_connection_t * att_connection = &hci_connection->att_connection;
return att_connection->mtu;
}{ ... }
void att_server_deinit(void){
att_server_client_read_callback = NULL;
att_server_client_write_callback = NULL;
att_client_packet_handler = NULL;
service_handlers = NULL;
att_server_flags = 0;
}{ ... }
#ifdef ENABLE_GATT_OVER_EATT
#define MAX_NR_EATT_CHANNELS 5
static uint16_t att_server_eatt_receive_buffer_size;
static uint16_t att_server_eatt_send_buffer_size;
static att_server_eatt_bearer_t * att_server_eatt_bearer_for_cid(uint16_t cid){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active);
while(btstack_linked_list_iterator_has_next(&it)){
att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it);
if (eatt_bearer->att_server.l2cap_cid == cid) {
return eatt_bearer;
}if (eatt_bearer->att_server.l2cap_cid == cid) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}att_server_eatt_bearer_for_cid (uint16_t cid) { ... }
static att_server_eatt_bearer_t * att_server_eatt_bearer_for_con_handle(hci_con_handle_t con_handle){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active);
while(btstack_linked_list_iterator_has_next(&it)){
att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it);
if (eatt_bearer->att_connection.con_handle == con_handle) {
return eatt_bearer;
}if (eatt_bearer->att_connection.con_handle == con_handle) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}att_server_eatt_bearer_for_con_handle (hci_con_handle_t con_handle) { ... }
static void att_server_eatt_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
uint16_t cid;
uint8_t status;
uint16_t remote_mtu;
uint8_t i;
uint8_t num_requested_bearers;
uint8_t num_accepted_bearers;
uint16_t initial_credits = L2CAP_LE_AUTOMATIC_CREDITS;
uint8_t * receive_buffers[MAX_NR_EATT_CHANNELS];
uint16_t cids[MAX_NR_EATT_CHANNELS];
att_server_eatt_bearer_t * eatt_bearers[MAX_NR_EATT_CHANNELS];
att_server_eatt_bearer_t * eatt_bearer;
att_server_t * att_server;
att_connection_t * att_connection;
hci_con_handle_t con_handle;
hci_connection_t * hci_connection;
switch (packet_type) {
case L2CAP_DATA_PACKET:
eatt_bearer = att_server_eatt_bearer_for_cid(channel);
btstack_assert(eatt_bearer != NULL);
att_server = &eatt_bearer->att_server;
att_connection = &eatt_bearer->att_connection;
att_server_handle_att_pdu(att_server, att_connection, packet, size);
break;
case L2CAP_DATA_PACKET:
case HCI_EVENT_PACKET:
switch (packet[0]) {
case L2CAP_EVENT_CAN_SEND_NOW:
cid = l2cap_event_packet_sent_get_local_cid(packet);
eatt_bearer = att_server_eatt_bearer_for_cid(cid);
btstack_assert(eatt_bearer != NULL);
att_server = &eatt_bearer->att_server;
att_connection = &eatt_bearer->att_connection;
btstack_assert(att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED);
att_server_process_validated_request(att_server, att_connection, eatt_bearer->send_buffer);
break;
case L2CAP_EVENT_CAN_SEND_NOW:
case L2CAP_EVENT_PACKET_SENT:
cid = l2cap_event_packet_sent_get_local_cid(packet);
eatt_bearer = att_server_eatt_bearer_for_cid(cid);
btstack_assert(eatt_bearer != NULL);
att_server = &eatt_bearer->att_server;
att_connection = &eatt_bearer->att_connection;
att_server_handle_att_pdu(att_server, att_connection, packet, size);
break;
case L2CAP_EVENT_PACKET_SENT:
case L2CAP_EVENT_ECBM_INCOMING_CONNECTION:
cid = l2cap_event_ecbm_incoming_connection_get_local_cid(packet);
con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet);
hci_connection = hci_connection_for_handle(con_handle);
btstack_assert(hci_connection != NULL);
if (hci_connection->att_server.eatt_outgoing_active) {
hci_connection->att_server.incoming_connection_request = true;
l2cap_ecbm_decline_channels(cid, L2CAP_ECBM_CONNECTION_RESULT_SOME_REFUSED_INSUFFICIENT_RESOURCES_AVAILABLE );
log_info("Decline incoming connection from %s", bd_addr_to_str(hci_connection->address));
}if (hci_connection->att_server.eatt_outgoing_active) { ... } else {
num_requested_bearers = l2cap_event_ecbm_incoming_connection_get_num_channels(packet);
for (i = 0; i < num_requested_bearers; i++){
eatt_bearers[i] = (att_server_eatt_bearer_t *) btstack_linked_list_pop(&att_server_eatt_bearer_pool);
if (eatt_bearers[i] == NULL) {
break;
}if (eatt_bearers[i] == NULL) { ... }
eatt_bearers[i]->att_connection.con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet);
eatt_bearers[i]->att_server.bearer_type = ATT_BEARER_ENHANCED_LE;
receive_buffers[i] = eatt_bearers[i]->receive_buffer;
btstack_linked_list_add(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearers[i]);
}for (i = 0; i < num_requested_bearers; i++) { ... }
num_accepted_bearers = i;
status = l2cap_ecbm_accept_channels(cid, num_accepted_bearers, initial_credits, att_server_eatt_receive_buffer_size, receive_buffers, cids);
btstack_assert(status == ERROR_CODE_SUCCESS);
log_info("requested %u, accepted %u", num_requested_bearers, num_accepted_bearers);
for (i=0;i<num_accepted_bearers;i++){
log_info("eatt l2cap cid: 0x%04x", cids[i]);
eatt_bearers[i]->att_server.l2cap_cid = cids[i];
}for (i=0;i
}else { ... }
break;
case L2CAP_EVENT_ECBM_INCOMING_CONNECTION:
case L2CAP_EVENT_ECBM_CHANNEL_OPENED:
cid = l2cap_event_ecbm_channel_opened_get_local_cid(packet);
status = l2cap_event_ecbm_channel_opened_get_status(packet);
remote_mtu = l2cap_event_ecbm_channel_opened_get_remote_mtu(packet);
eatt_bearer = att_server_eatt_bearer_for_cid(cid);
btstack_assert(eatt_bearer != NULL);
eatt_bearer->att_connection.mtu_exchanged = true;
eatt_bearer->att_connection.mtu = remote_mtu;
eatt_bearer->att_connection.max_mtu = remote_mtu;
log_info("L2CAP_EVENT_ECBM_CHANNEL_OPENED - cid 0x%04x mtu %u, status 0x%02x", cid, remote_mtu, status);
break;
case L2CAP_EVENT_ECBM_CHANNEL_OPENED:
case L2CAP_EVENT_ECBM_RECONFIGURED:
break;
case L2CAP_EVENT_ECBM_RECONFIGURED:
case L2CAP_EVENT_CHANNEL_CLOSED:
eatt_bearer = att_server_eatt_bearer_for_cid(l2cap_event_channel_closed_get_local_cid(packet));
btstack_assert(eatt_bearers != NULL);
btstack_linked_list_remove(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearer);
btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer);
break;case L2CAP_EVENT_CHANNEL_CLOSED:
default:
break;default
}switch (packet[0]) { ... }
break;case HCI_EVENT_PACKET:
default:
btstack_unreachable();
break;default
}switch (packet_type) { ... }
}att_server_eatt_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { ... }
uint8_t att_server_eatt_init(uint8_t num_eatt_bearers, uint8_t * storage_buffer, uint16_t storage_size){
uint16_t size_for_structs = num_eatt_bearers * sizeof(att_server_eatt_bearer_t);
if (storage_size < size_for_structs) {
return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
}if (storage_size < size_for_structs) { ... }
memset(storage_buffer, 0, storage_size);
uint16_t buffer_size_per_bearer = ((storage_size - size_for_structs) / num_eatt_bearers);
att_server_eatt_receive_buffer_size = buffer_size_per_bearer / 2;
att_server_eatt_send_buffer_size = buffer_size_per_bearer / 2;
uint8_t * bearer_buffer = &storage_buffer[size_for_structs];
uint8_t i;
att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) storage_buffer;
log_info("%u EATT bearers with receive buffer size %u",
num_eatt_bearers, att_server_eatt_receive_buffer_size);
for (i=0;i<num_eatt_bearers;i++){
eatt_bearer->att_connection.con_handle = HCI_CON_HANDLE_INVALID;
eatt_bearer->receive_buffer = bearer_buffer;
bearer_buffer += att_server_eatt_receive_buffer_size;
eatt_bearer->send_buffer = bearer_buffer;
bearer_buffer += att_server_eatt_send_buffer_size;
btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer);
eatt_bearer++;
}for (i=0;i
l2cap_ecbm_register_service(att_server_eatt_handler, BLUETOOTH_PSM_EATT, 64, 0, false);
return 0;
}att_server_eatt_init (uint8_t num_eatt_bearers, uint8_t * storage_buffer, uint16_t storage_size) { ... }
/* ... */#endif