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
48
49
50
51
52
53
57
58
59
72
73
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
113
114
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
140
141
142
143
144
156
157
158
161
162
165
166
169
170
173
174
175
176
177
190
191
192
203
204
205
206
207
210
213
214
215
216
217
218
219
226
227
228
229
230
231
234
235
236
237
238
239
240
241
242
243
246
250
251
252
253
254
255
258
259
262
263
264
265
266
267
268
269
270
274
275
281
282
283
284
285
286
290
291
305
306
313
314
317
318
321
322
325
326
329
330
333
334
337
338
342
343
374
375
376
377
378
379
380
383
384
385
386
387
409
410
411
412
413
414
415
416
417
418
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
452
453
454
455
456
457
458
459
460
463
466
469
470
471
472
473
474
475
480
481
482
488
489
490
493
494
495
496
497
498
499
500
501
502
505
506
507
508
509
510
511
512
513
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
541
542
543
544
545
546
547
548
549
550
551
552
553
556
557
558
559
560
561
562
563
564
565
566
569
572
573
574
575
576
577
578
579
580
581
582
583
586
587
588
589
590
591
592
598
599
600
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
622
623
624
625
626
627
628
629
630
631
632
633
634
637
638
639
642
643
644
650
651
652
653
656
657
658
659
660
664
665
666
670
671
672
673
676
677
678
683
684
685
692
693
694
695
696
697
698
699
700
701
705
706
707
710
711
712
715
716
717
718
719
720
721
722
723
724
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
753
754
755
758
759
760
761
764
765
766
767
768
772
773
774
778
779
780
781
782
783
784
785
786
787
788
789
798
799
800
801
802
803
804
805
806
807
808
811
812
813
816
817
818
819
822
823
824
825
826
830
831
832
836
837
840
841
842
843
844
845
846
850
851
852
853
854
855
856
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
889
890
891
892
893
896
897
898
902
903
904
905
908
909
910
911
912
919
920
921
925
926
927
937
938
939
940
941
946
947
948
951
952
953
954
955
956
957
958
959
960
961
962
963
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
1001
1002
1003
1004
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1023
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1063
1064
1065
1066
1067
1072
1073
1074
1075
1076
1077
1078
1079
1082
1083
1084
1085
1086
1087
1088
1089
1090
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1116
1117
1118
1119
1120
1121
1122
1125
1128
1131
1134
1135
1136
1139
1140
1141
1142
1143
1147
1148
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1165
1166
1167
1168
1171
1172
1175
1178
1181
1182
1183
1186
1187
1188
1189
1191
1192
1197
1200
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1214
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1230
1231
1234
1235
1236
1237
1240
1241
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1269
1270
1271
1274
1275
1276
1277
1280
1283
1286
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1304
1305
1306
1307
1308
1309
1310
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1367
1370
1373
1376
1379
1382
1386
1390
1393
1396
1399
1402
1406
1410
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1444
1445
1446
1447
1448
1449
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1466
1469
1472
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1488
1491
1494
1498
1506
1509
1510
1511
1512
1513
1514
1517
1518
1519
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1544
1545
1546
1547
1548
1549
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1568
1571
1574
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1593
1596
1599
1603
1611
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1629
1632
1635
1643
1644
1645
1646
1647
1648
1656
1657
1674
1675
1676
1690
1691
1696
1697
1702
1703
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1730
1733
1736
1739
1742
1745
1746
1747
1750
1753
1756
1759
1762
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1784
1787
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1814
1817
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1855
1856
1857
1858
/* ... */
#define BTSTACK_FILE__ "att_db.c"
#include <string.h>
#include "ble/att_db.h"
#include "ble/core.h"
#include "bluetooth.h"
#include "btstack_debug.h"
#include "btstack_util.h"
6 includes
#ifdef ENABLE_ATT_DELAYED_READ_RESPONSE
#error "ENABLE_ATT_DELAYED_READ_RESPONSE was replaced by ENABLE_ATT_DELAYED_RESPONSE. Please update btstack_config.h"
#endif
typedef enum {
ATT_READ,
ATT_WRITE,
...} att_operation_t;
static bool is_Bluetooth_Base_UUID(uint8_t const *uuid){
static const uint8_t bluetooth_base_uuid[] = { 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
if (memcmp(&uuid[0], &bluetooth_base_uuid[0], 12) != 0){
return false;
}if (memcmp(&uuid[0], &bluetooth_base_uuid[0], 12) != 0) { ... }
if (memcmp(&uuid[14], &bluetooth_base_uuid[14], 2) != 0){
return false;
}if (memcmp(&uuid[14], &bluetooth_base_uuid[14], 2) != 0) { ... }
return true;
}{ ... }
static uint16_t uuid16_from_uuid(uint16_t uuid_len, uint8_t * uuid){
if (uuid_len == 2u){
return little_endian_read_16(uuid, 0u);
}if (uuid_len == 2u) { ... }
if (!is_Bluetooth_Base_UUID(uuid)){
return 0;
}if (!is_Bluetooth_Base_UUID(uuid)) { ... }
return little_endian_read_16(uuid, 12);
}{ ... }
typedef struct att_iterator {
uint8_t const * att_ptr;
uint16_t size;
uint16_t flags;
uint16_t handle;
uint8_t const * uuid;
uint16_t value_len;
uint8_t const * value;
...} att_iterator_t;
static void att_persistent_ccc_cache(att_iterator_t * it);
static uint8_t const * att_database = NULL;
static att_read_callback_t att_read_callback = NULL;
static att_write_callback_t att_write_callback = NULL;
static int att_prepare_write_error_code = 0;
static uint16_t att_prepare_write_error_handle = 0x0000;
static uint16_t att_persistent_ccc_handle;
static uint16_t att_persistent_ccc_uuid16;
static void att_iterator_init(att_iterator_t *it){
it->att_ptr = att_database;
}{ ... }
static bool att_iterator_has_next(att_iterator_t *it){
return it->att_ptr != NULL;
}{ ... }
static void att_iterator_fetch_next(att_iterator_t *it){
it->size = little_endian_read_16(it->att_ptr, 0);
if (it->size == 0u){
it->flags = 0;
it->handle = 0;
it->uuid = NULL;
it->value_len = 0;
it->value = NULL;
it->att_ptr = NULL;
return;
}if (it->size == 0u) { ... }
it->flags = little_endian_read_16(it->att_ptr, 2);
it->handle = little_endian_read_16(it->att_ptr, 4);
it->uuid = &it->att_ptr[6];
if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
it->value_len = it->size - 22u;
it->value = &it->att_ptr[22];
}if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u) { ... } else {
it->value_len = it->size - 8u;
it->value = &it->att_ptr[8];
}else { ... }
it->att_ptr += it->size;
}{ ... }
static bool att_iterator_match_uuid16(att_iterator_t *it, uint16_t uuid){
if (it->handle == 0u){
return false;
}if (it->handle == 0u) { ... }
if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
if (!is_Bluetooth_Base_UUID(it->uuid)){
return false;
}if (!is_Bluetooth_Base_UUID(it->uuid)) { ... }
return little_endian_read_16(it->uuid, 12) == uuid;
}if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u) { ... }
return little_endian_read_16(it->uuid, 0) == uuid;
}{ ... }
static bool att_iterator_match_uuid(att_iterator_t *it, uint8_t *uuid, uint16_t uuid_len){
if (it->handle == 0u){
return false;
}if (it->handle == 0u) { ... }
if (uuid_len == 2u) {
return att_iterator_match_uuid16(it, little_endian_read_16(uuid, 0));
}if (uuid_len == 2u) { ... }
if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
return memcmp(it->uuid, uuid, 16) == 0;
}if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u) { ... }
if (!is_Bluetooth_Base_UUID(uuid)){
return false;
}if (!is_Bluetooth_Base_UUID(uuid)) { ... }
return little_endian_read_16(uuid, 12) == little_endian_read_16(it->uuid, 0);
}{ ... }
static bool att_find_handle(att_iterator_t *it, uint16_t handle){
if (handle == 0u){
return false;
}if (handle == 0u) { ... }
att_iterator_init(it);
while (att_iterator_has_next(it)){
att_iterator_fetch_next(it);
if (it->handle == handle){
return true;
}if (it->handle == handle) { ... }
}while (att_iterator_has_next(it)) { ... }
return false;
}{ ... }
uint16_t att_uuid_for_handle(uint16_t attribute_handle){
att_iterator_t it;
bool ok = att_find_handle(&it, attribute_handle);
if (!ok){
return 0u;
}if (!ok) { ... }
if ((it.flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
return 0u;
}if ((it.flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u) { ... }
return little_endian_read_16(it.uuid, 0);
}{ ... }
const uint8_t * gatt_server_get_const_value_for_handle(uint16_t attribute_handle, uint16_t * out_value_len){
att_iterator_t it;
bool ok = att_find_handle(&it, attribute_handle);
if (!ok){
return NULL;
}if (!ok) { ... }
if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) != 0u){
return NULL;
}if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) != 0u) { ... }
*out_value_len = it.value_len;
return it.value;
}{ ... }
static void att_update_value_len(att_iterator_t *it, uint16_t offset, hci_con_handle_t con_handle) {
if ((it->flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u){
return;
}if ((it->flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u) { ... }
it->value_len = (*att_read_callback)(con_handle, it->handle, offset, NULL, 0);
return;
}{ ... }
static int att_copy_value(att_iterator_t *it, uint16_t offset, uint8_t * buffer, uint16_t buffer_size, hci_con_handle_t con_handle){
if ((it->flags & (uint16_t)ATT_PROPERTY_DYNAMIC) != 0u){
return (*att_read_callback)(con_handle, it->handle, offset, buffer, buffer_size);
}if ((it->flags & (uint16_t)ATT_PROPERTY_DYNAMIC) != 0u) { ... }
uint16_t bytes_to_copy = btstack_min(it->value_len - offset, buffer_size);
(void)memcpy(buffer, it->value, bytes_to_copy);
return bytes_to_copy;
}{ ... }
void att_set_db(uint8_t const * db){
if (db == NULL){
return;
}if (db == NULL) { ... }
if (*db != (uint8_t)ATT_DB_VERSION){
log_error("ATT DB version differs, please regenerate .h from .gatt file or update att_db_util.c");
return;
}if (*db != (uint8_t)ATT_DB_VERSION) { ... }
log_info("att_set_db %p", db);
att_database = &db[1];
}{ ... }
void att_set_read_callback(att_read_callback_t callback){
att_read_callback = callback;
}{ ... }
void att_set_write_callback(att_write_callback_t callback){
att_write_callback = callback;
}{ ... }
void att_dump_attributes(void){
att_iterator_t it;
att_iterator_init(&it);
uint8_t uuid128[16];
log_info("att_dump_attributes, table %p", att_database);
while (att_iterator_has_next(&it)){
att_iterator_fetch_next(&it);
if (it.handle == 0u) {
log_info("Handle: END");
return;
}if (it.handle == 0u) { ... }
log_info("Handle: 0x%04x, flags: 0x%04x, uuid: ", it.handle, it.flags);
if ((it.flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
reverse_128(it.uuid, uuid128);
log_info("%s", uuid128_to_str(uuid128));
}if ((it.flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u) { ... } else {
log_info("%04x", little_endian_read_16(it.uuid, 0));
}else { ... }
log_info(", value_len: %u, value: ", it.value_len);
log_info_hexdump(it.value, it.value_len);
}while (att_iterator_has_next(&it)) { ... }
}{ ... }
static void att_prepare_write_reset(void){
att_prepare_write_error_code = 0;
att_prepare_write_error_handle = 0x0000;
}{ ... }
static void att_prepare_write_update_errors(uint8_t error_code, uint16_t handle){
if ((error_code == (uint8_t)ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH) && (error_code != (uint8_t)att_prepare_write_error_code)){
att_prepare_write_error_code = error_code;
att_prepare_write_error_handle = handle;
return;
}if ((error_code == (uint8_t)ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH) && (error_code != (uint8_t)att_prepare_write_error_code)) { ... }
if ((error_code == (uint8_t)ATT_ERROR_INVALID_OFFSET) && (att_prepare_write_error_code == 0)){
att_prepare_write_error_code = error_code;
att_prepare_write_error_handle = handle;
return;
}if ((error_code == (uint8_t)ATT_ERROR_INVALID_OFFSET) && (att_prepare_write_error_code == 0)) { ... }
}{ ... }
static uint16_t setup_error(uint8_t * response_buffer, uint8_t request_opcode, uint16_t handle, uint8_t error_code){
response_buffer[0] = (uint8_t)ATT_ERROR_RESPONSE;
response_buffer[1] = request_opcode;
little_endian_store_16(response_buffer, 2, handle);
response_buffer[4] = error_code;
return 5;
}{ ... }
static inline uint16_t setup_error_read_not_permitted(uint8_t * response_buffer, uint8_t request_opcode, uint16_t start_handle){
return setup_error(response_buffer, request_opcode, start_handle, ATT_ERROR_READ_NOT_PERMITTED);
}{ ... }
static inline uint16_t setup_error_write_not_permitted(uint8_t * response_buffer, uint8_t request, uint16_t start_handle){
return setup_error(response_buffer, request, start_handle, ATT_ERROR_WRITE_NOT_PERMITTED);
}{ ... }
static inline uint16_t setup_error_atribute_not_found(uint8_t * response_buffer, uint8_t request_opcode, uint16_t start_handle){
return setup_error(response_buffer, request_opcode, start_handle, ATT_ERROR_ATTRIBUTE_NOT_FOUND);
}{ ... }
static inline uint16_t setup_error_invalid_handle(uint8_t * response_buffer, uint8_t request_opcode, uint16_t handle){
return setup_error(response_buffer, request_opcode, handle, ATT_ERROR_INVALID_HANDLE);
}{ ... }
static inline uint16_t setup_error_invalid_offset(uint8_t * response_buffer, uint8_t request_opcode, uint16_t handle){
return setup_error(response_buffer, request_opcode, handle, ATT_ERROR_INVALID_OFFSET);
}{ ... }
static inline uint16_t setup_error_invalid_pdu(uint8_t *response_buffer, uint8_t request_opcode) {
return setup_error(response_buffer, request_opcode, 0, ATT_ERROR_INVALID_PDU);
}{ ... }
struct att_security_settings {
uint8_t required_security_level;
bool requires_secure_connection;
...};
static void att_validate_security_get_settings(struct att_security_settings * security_settings, att_operation_t operation, att_iterator_t *it){
security_settings->required_security_level = 0u;
security_settings->requires_secure_connection = false;
switch (operation){
case ATT_READ:
if ((it->flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_0) != 0u){
security_settings->required_security_level |= 1u;
}if ((it->flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_0) != 0u) { ... }
if ((it->flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_1) != 0u){
security_settings->required_security_level |= 2u;
}if ((it->flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_1) != 0u) { ... }
if ((it->flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_SC) != 0u){
security_settings->requires_secure_connection = true;
}if ((it->flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_SC) != 0u) { ... }
break;case ATT_READ:
case ATT_WRITE:
if ((it->flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_0) != 0u){
security_settings->required_security_level |= 1u;
}if ((it->flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_0) != 0u) { ... }
if ((it->flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_1) != 0u){
security_settings->required_security_level |= 2u;
}if ((it->flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_1) != 0u) { ... }
if ((it->flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_SC) != 0u){
security_settings->requires_secure_connection = true;
}if ((it->flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_SC) != 0u) { ... }
break;case ATT_WRITE:
default:
btstack_assert(false);
break;default
}switch (operation) { ... }
}{ ... }
static uint8_t att_validate_security(att_connection_t * att_connection, att_operation_t operation, att_iterator_t * it){
struct att_security_settings security_settings;
att_validate_security_get_settings(&security_settings, operation, it);
uint8_t required_encryption_size = (uint8_t)(it->flags >> 12);
if (required_encryption_size != 0u){
required_encryption_size++;
}if (required_encryption_size != 0u) { ... }
log_debug("att_validate_security. flags 0x%04x (=> security level %u, key size %u) authorized %u, authenticated %u, encryption_key_size %u, secure connection %u",
it->flags, security_settings.required_security_level, required_encryption_size, att_connection->authorized, att_connection->authenticated, att_connection->encryption_key_size, att_connection->secure_connection);
bool sc_missing = security_settings.requires_secure_connection && (att_connection->secure_connection == 0u);
switch (security_settings.required_security_level){
case ATT_SECURITY_AUTHORIZED:
if ((att_connection->authorized == 0u) || sc_missing){
return ATT_ERROR_INSUFFICIENT_AUTHORIZATION;
}if ((att_connection->authorized == 0u) || sc_missing) { ... }
case ATT_SECURITY_AUTHORIZED:
case ATT_SECURITY_AUTHENTICATED:
if ((att_connection->authenticated == 0u) || sc_missing){
return ATT_ERROR_INSUFFICIENT_AUTHENTICATION;
}if ((att_connection->authenticated == 0u) || sc_missing) { ... }
case ATT_SECURITY_AUTHENTICATED:
case ATT_SECURITY_ENCRYPTED:
if ((required_encryption_size > 0u) && ((att_connection->encryption_key_size == 0u) || sc_missing)){
return ATT_ERROR_INSUFFICIENT_ENCRYPTION;
}if ((required_encryption_size > 0u) && ((att_connection->encryption_key_size == 0u) || sc_missing)) { ... }
if (required_encryption_size > att_connection->encryption_key_size){
return ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE;
}if (required_encryption_size > att_connection->encryption_key_size) { ... }
break;case ATT_SECURITY_ENCRYPTED:
default:
break;default
}switch (security_settings.required_security_level) { ... }
return ATT_ERROR_SUCCESS;
}{ ... }
static uint16_t handle_exchange_mtu_request(att_connection_t * att_connection, uint8_t * request_buffer, uint16_t request_len,
uint8_t * response_buffer){
if (request_len != 3u){
return setup_error_invalid_pdu(response_buffer, ATT_EXCHANGE_MTU_REQUEST);
}if (request_len != 3u) { ... }
uint16_t client_rx_mtu = little_endian_read_16(request_buffer, 1);
uint16_t min_mtu = btstack_min(client_rx_mtu, att_connection->max_mtu);
uint16_t new_mtu = btstack_max(ATT_DEFAULT_MTU, min_mtu);
att_connection->mtu_exchanged = true;
att_connection->mtu = new_mtu;
response_buffer[0] = ATT_EXCHANGE_MTU_RESPONSE;
little_endian_store_16(response_buffer, 1, att_connection->mtu);
return 3;
}{ ... }
static uint16_t handle_find_information_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size,
uint16_t start_handle, uint16_t end_handle){
UNUSED(att_connection);
log_info("ATT_FIND_INFORMATION_REQUEST: from %04X to %04X", start_handle, end_handle);
uint8_t request_type = ATT_FIND_INFORMATION_REQUEST;
if ((start_handle > end_handle) || (start_handle == 0u)){
return setup_error_invalid_handle(response_buffer, request_type, start_handle);
}if ((start_handle > end_handle) || (start_handle == 0u)) { ... }
uint16_t offset = 1;
uint16_t uuid_len = 0;
att_iterator_t it;
att_iterator_init(&it);
while (att_iterator_has_next(&it)){
att_iterator_fetch_next(&it);
if (!it.handle){
break;
}if (!it.handle) { ... }
if (it.handle > end_handle){
break;
}if (it.handle > end_handle) { ... }
if (it.handle < start_handle){
continue;
}if (it.handle < start_handle) { ... }
uint16_t this_uuid_len = (it.flags & (uint16_t)ATT_PROPERTY_UUID128) ? 16u : 2u;
if (offset > 1u){
if (this_uuid_len != uuid_len) {
break;
}if (this_uuid_len != uuid_len) { ... }
}if (offset > 1u) { ... }
if (offset == 1u) {
uuid_len = this_uuid_len;
response_buffer[offset] = (it.flags & (uint16_t)ATT_PROPERTY_UUID128) ? 0x02u : 0x01u;
offset++;
}if (offset == 1u) { ... }
if ((offset + 2u + uuid_len) > response_buffer_size){
break;
}if ((offset + 2u + uuid_len) > response_buffer_size) { ... }
little_endian_store_16(response_buffer, offset, it.handle);
offset += 2u;
(void)memcpy(response_buffer + offset, it.uuid, uuid_len);
offset += uuid_len;
}while (att_iterator_has_next(&it)) { ... }
if (offset == 1u){
return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
}if (offset == 1u) { ... }
response_buffer[0] = ATT_FIND_INFORMATION_REPLY;
return offset;
}{ ... }
static uint16_t handle_find_information_request(att_connection_t * att_connection, uint8_t * request_buffer, uint16_t request_len,
uint8_t * response_buffer, uint16_t response_buffer_size){
if (request_len != 5u){
return setup_error_invalid_pdu(response_buffer, ATT_FIND_INFORMATION_REQUEST);
}if (request_len != 5u) { ... }
uint16_t start_handle = little_endian_read_16(request_buffer, 1);
uint16_t end_handle = little_endian_read_16(request_buffer, 3);
return handle_find_information_request2(att_connection, response_buffer, response_buffer_size, start_handle, end_handle);
}{ ... }
static uint16_t handle_find_by_type_value_request(att_connection_t * att_connection, uint8_t * request_buffer, uint16_t request_len,
uint8_t * response_buffer, uint16_t response_buffer_size){
UNUSED(att_connection);
if (request_len < 7u){
return setup_error_invalid_pdu(response_buffer, ATT_FIND_BY_TYPE_VALUE_REQUEST);
}if (request_len < 7u) { ... }
uint16_t start_handle = little_endian_read_16(request_buffer, 1);
uint16_t end_handle = little_endian_read_16(request_buffer, 3);
uint16_t attribute_type = little_endian_read_16(request_buffer, 5);
const uint8_t *attribute_value = &request_buffer[7];
uint16_t attribute_len = request_len - 7u;
log_info("ATT_FIND_BY_TYPE_VALUE_REQUEST: from %04X to %04X, type %04X, value: ", start_handle, end_handle, attribute_type);
log_info_hexdump(attribute_value, attribute_len);
uint8_t request_type = ATT_FIND_BY_TYPE_VALUE_REQUEST;
if ((start_handle > end_handle) || (start_handle == 0u)){
return setup_error_invalid_handle(response_buffer, request_type, start_handle);
}if ((start_handle > end_handle) || (start_handle == 0u)) { ... }
uint16_t offset = 1;
bool in_group = false;
uint16_t prev_handle = 0;
att_iterator_t it;
att_iterator_init(&it);
while (att_iterator_has_next(&it)){
att_iterator_fetch_next(&it);
if ((it.handle != 0u) && (it.handle < start_handle)){
continue;
}if ((it.handle != 0u) && (it.handle < start_handle)) { ... }
if (it.handle > end_handle){
break;
}if (it.handle > end_handle) { ... }
if (in_group &&
((it.handle == 0u) || att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID))){
log_info("End of group, handle 0x%04x", prev_handle);
little_endian_store_16(response_buffer, offset, prev_handle);
offset += 2u;
in_group = false;
if ((offset + 4u) > response_buffer_size){
break;
}if ((offset + 4u) > response_buffer_size) { ... }
}if (in_group && ((it.handle == 0u) || att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID))) { ... }
prev_handle = it.handle;
if ((it.handle != 0u) && att_iterator_match_uuid16(&it, attribute_type) && (attribute_len == it.value_len) && (memcmp(attribute_value, it.value, it.value_len) == 0)){
log_info("Begin of group, handle 0x%04x", it.handle);
little_endian_store_16(response_buffer, offset, it.handle);
offset += 2u;
in_group = true;
}if ((it.handle != 0u) && att_iterator_match_uuid16(&it, attribute_type) && (attribute_len == it.value_len) && (memcmp(attribute_value, it.value, it.value_len) == 0)) { ... }
}while (att_iterator_has_next(&it)) { ... }
if (offset == 1u){
return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
}if (offset == 1u) { ... }
response_buffer[0] = ATT_FIND_BY_TYPE_VALUE_RESPONSE;
return offset;
}{ ... }
static uint16_t handle_read_by_type_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size,
uint16_t start_handle, uint16_t end_handle,
uint16_t attribute_type_len, uint8_t * attribute_type){
log_info("ATT_READ_BY_TYPE_REQUEST: from %04X to %04X, type: ", start_handle, end_handle);
log_info_hexdump(attribute_type, attribute_type_len);
uint8_t request_type = ATT_READ_BY_TYPE_REQUEST;
if ((start_handle > end_handle) || (start_handle == 0u)){
return setup_error_invalid_handle(response_buffer, request_type, start_handle);
}if ((start_handle > end_handle) || (start_handle == 0u)) { ... }
uint16_t offset = 1;
uint16_t pair_len = 0;
att_iterator_t it;
att_iterator_init(&it);
uint8_t error_code = 0;
uint16_t first_matching_but_unreadable_handle = 0;
while (att_iterator_has_next(&it)){
att_iterator_fetch_next(&it);
if ((it.handle == 0u ) || (it.handle > end_handle)){
break;
}if ((it.handle == 0u ) || (it.handle > end_handle)) { ... }
if ((it.handle < start_handle) || !att_iterator_match_uuid(&it, attribute_type, attribute_type_len)){
continue;
}if ((it.handle < start_handle) || !att_iterator_match_uuid(&it, attribute_type, attribute_type_len)) { ... }
if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) {
if (first_matching_but_unreadable_handle == 0u) {
first_matching_but_unreadable_handle = it.handle;
}if (first_matching_but_unreadable_handle == 0u) { ... }
continue;
}if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) { ... }
error_code = att_validate_security(att_connection, ATT_READ, &it);
if (error_code != 0u){
break;
}if (error_code != 0u) { ... }
att_update_value_len(&it, 0, att_connection->con_handle);
#ifdef ENABLE_ATT_DELAYED_RESPONSE
if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING){
return ATT_READ_RESPONSE_PENDING;
}if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING) { ... }
/* ... */#endif
if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET){
error_code = (uint8_t)(it.value_len - (uint16_t)ATT_READ_ERROR_CODE_OFFSET);
break;
}if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET) { ... }
uint16_t this_pair_len = 2u + it.value_len;
if ((offset > 1u) && (pair_len != this_pair_len)) {
break;
}if ((offset > 1u) && (pair_len != this_pair_len)) { ... }
if (offset == 1u) {
pair_len = this_pair_len;
response_buffer[offset] = (uint8_t) pair_len;
offset++;
}if (offset == 1u) { ... }
if ((offset + pair_len) > response_buffer_size) {
if (offset > 2u){
break;
}if (offset > 2u) { ... }
it.value_len = response_buffer_size - 4u;
response_buffer[1u] = 2u + it.value_len;
}if ((offset + pair_len) > response_buffer_size) { ... }
little_endian_store_16(response_buffer, offset, it.handle);
offset += 2u;
uint16_t bytes_copied = att_copy_value(&it, 0, response_buffer + offset, it.value_len, att_connection->con_handle);
offset += bytes_copied;
}while (att_iterator_has_next(&it)) { ... }
if (offset > 1u){
response_buffer[0] = ATT_READ_BY_TYPE_RESPONSE;
return offset;
}if (offset > 1u) { ... }
if (error_code != 0u){
return setup_error(response_buffer, request_type, start_handle, error_code);
}if (error_code != 0u) { ... }
if (first_matching_but_unreadable_handle != 0u){
return setup_error_read_not_permitted(response_buffer, request_type, first_matching_but_unreadable_handle);
}if (first_matching_but_unreadable_handle != 0u) { ... }
return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
}{ ... }
static uint16_t handle_read_by_type_request(att_connection_t * att_connection, uint8_t * request_buffer, uint16_t request_len,
uint8_t * response_buffer, uint16_t response_buffer_size){
uint16_t attribute_type_len;
switch (request_len){
case 7:
attribute_type_len = 2;
break;case 7:
case 21:
attribute_type_len = 16;
break;case 21:
default:
return setup_error_invalid_pdu(response_buffer, ATT_READ_BY_TYPE_REQUEST);default
}switch (request_len) { ... }
uint16_t start_handle = little_endian_read_16(request_buffer, 1);
uint16_t end_handle = little_endian_read_16(request_buffer, 3);
return handle_read_by_type_request2(att_connection, response_buffer, response_buffer_size, start_handle, end_handle, attribute_type_len, &request_buffer[5]);
}{ ... }
static uint16_t handle_read_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size, uint16_t handle){
log_info("ATT_READ_REQUEST: handle %04x", handle);
uint8_t request_type = ATT_READ_REQUEST;
att_iterator_t it;
bool ok = att_find_handle(&it, handle);
if (!ok){
return setup_error_invalid_handle(response_buffer, request_type, handle);
}if (!ok) { ... }
if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) {
return setup_error_read_not_permitted(response_buffer, request_type, handle);
}if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) { ... }
uint8_t error_code = att_validate_security(att_connection, ATT_READ, &it);
if (error_code != 0u) {
return setup_error(response_buffer, request_type, handle, error_code);
}if (error_code != 0u) { ... }
att_update_value_len(&it, 0, att_connection->con_handle);
#ifdef ENABLE_ATT_DELAYED_RESPONSE
if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING){
return ATT_READ_RESPONSE_PENDING;
}if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING) { ... }
/* ... */#endif
if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET){
error_code = (uint8_t)(it.value_len - (uint16_t)ATT_READ_ERROR_CODE_OFFSET);
return setup_error(response_buffer, request_type, handle, error_code);
}if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET) { ... }
uint16_t offset = 1;
uint16_t bytes_copied = att_copy_value(&it, 0, response_buffer + offset, response_buffer_size - offset, att_connection->con_handle);
offset += bytes_copied;
response_buffer[0] = ATT_READ_RESPONSE;
return offset;
}{ ... }
static uint16_t handle_read_request(att_connection_t * att_connection, uint8_t * request_buffer, uint16_t request_len,
uint8_t * response_buffer, uint16_t response_buffer_size){
if (request_len != 3u){
return setup_error_invalid_pdu(response_buffer, ATT_READ_REQUEST);
}if (request_len != 3u) { ... }
uint16_t handle = little_endian_read_16(request_buffer, 1);
return handle_read_request2(att_connection, response_buffer, response_buffer_size, handle);
}{ ... }
static uint16_t handle_read_blob_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size, uint16_t handle, uint16_t value_offset){
log_info("ATT_READ_BLOB_REQUEST: handle %04x, offset %u", handle, value_offset);
uint8_t request_type = ATT_READ_BLOB_REQUEST;
att_iterator_t it;
bool ok = att_find_handle(&it, handle);
if (!ok){
return setup_error_invalid_handle(response_buffer, request_type, handle);
}if (!ok) { ... }
if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) {
return setup_error_read_not_permitted(response_buffer, request_type, handle);
}if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) { ... }
uint8_t error_code = att_validate_security(att_connection, ATT_READ, &it);
if (error_code != 0u) {
return setup_error(response_buffer, request_type, handle, error_code);
}if (error_code != 0u) { ... }
att_update_value_len(&it, value_offset, att_connection->con_handle);
#ifdef ENABLE_ATT_DELAYED_RESPONSE
if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING){
return ATT_READ_RESPONSE_PENDING;
}if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING) { ... }
/* ... */#endif
if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET){
error_code = (uint8_t)(it.value_len - (uint16_t)ATT_READ_ERROR_CODE_OFFSET);
return setup_error(response_buffer, request_type, handle, error_code);
}if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET) { ... }
if (value_offset > it.value_len){
return setup_error_invalid_offset(response_buffer, request_type, handle);
}if (value_offset > it.value_len) { ... }
response_buffer[0] = ATT_READ_BLOB_RESPONSE;
uint16_t offset = 1;
if (value_offset < it.value_len){
uint16_t bytes_copied = att_copy_value(&it, value_offset, &response_buffer[offset], response_buffer_size - offset, att_connection->con_handle);
offset += bytes_copied;
}if (value_offset < it.value_len) { ... }
return offset;
}{ ... }
static uint16_t handle_read_blob_request(att_connection_t * att_connection, uint8_t * request_buffer, uint16_t request_len,
uint8_t * response_buffer, uint16_t response_buffer_size){
if (request_len != 5u){
return setup_error_invalid_pdu(response_buffer, ATT_READ_BLOB_REQUEST);
}if (request_len != 5u) { ... }
uint16_t handle = little_endian_read_16(request_buffer, 1);
uint16_t value_offset = little_endian_read_16(request_buffer, 3);
return handle_read_blob_request2(att_connection, response_buffer, response_buffer_size, handle, value_offset);
}{ ... }
static uint16_t handle_read_multiple_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size, uint16_t num_handles, uint8_t * handles, bool store_length){
log_info("ATT_READ_MULTIPLE_(VARIABLE_)REQUEST: num handles %u", num_handles);
uint8_t request_type = store_length ? ATT_READ_MULTIPLE_VARIABLE_REQ : ATT_READ_MULTIPLE_REQUEST;
uint8_t response_type = store_length ? ATT_READ_MULTIPLE_VARIABLE_RSP : ATT_READ_MULTIPLE_RESPONSE;
uint16_t offset = 1;
uint16_t i;
uint8_t error_code = 0;
uint16_t handle = 0;
#ifdef ENABLE_ATT_DELAYED_RESPONSE
bool read_request_pending = false;
#endif
for (i=0; i<num_handles; i++){
handle = little_endian_read_16(handles, i << 1);
if (handle == 0u){
return setup_error_invalid_handle(response_buffer, request_type, handle);
}if (handle == 0u) { ... }
att_iterator_t it;
bool ok = att_find_handle(&it, handle);
if (!ok){
return setup_error_invalid_handle(response_buffer, request_type, handle);
}if (!ok) { ... }
if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) {
error_code = (uint8_t)ATT_ERROR_READ_NOT_PERMITTED;
break;
}if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) { ... }
error_code = att_validate_security(att_connection, ATT_READ, &it);
if (error_code != 0u){
break;
}if (error_code != 0u) { ... }
att_update_value_len(&it, 0, att_connection->con_handle);
#ifdef ENABLE_ATT_DELAYED_RESPONSE
if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING) {
read_request_pending = true;
}if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING) { ... }
if (read_request_pending){
continue;
}if (read_request_pending) { ... }
/* ... */#endif
if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET){
error_code = (uint8_t)(it.value_len - (uint16_t)ATT_READ_ERROR_CODE_OFFSET);
break;
}if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET) { ... }
#ifdef ENABLE_GATT_OVER_EATT
if (store_length && ((offset + 2) >= response_buffer_size)){
break;
}if (store_length && ((offset + 2) >= response_buffer_size)) { ... }
uint16_t offset_value_length = offset;
if (store_length){
offset += 2;
}if (store_length) { ... }
/* ... */#endif
uint16_t bytes_copied = att_copy_value(&it, 0, response_buffer + offset, response_buffer_size - offset, att_connection->con_handle);
offset += bytes_copied;
#ifdef ENABLE_GATT_OVER_EATT
if (store_length) {
little_endian_store_16(response_buffer, offset_value_length, bytes_copied);
}if (store_length) { ... }
/* ... */#endif
}for (i=0; i
if (error_code != 0u){
return setup_error(response_buffer, request_type, handle, error_code);
}if (error_code != 0u) { ... }
response_buffer[0] = (uint8_t)response_type;
return offset;
}{ ... }
static uint16_t
handle_read_multiple_request(att_connection_t *att_connection, uint8_t *request_buffer, uint16_t request_len,
uint8_t *response_buffer, uint16_t response_buffer_size, bool store_length) {
uint8_t request_type = store_length ? ATT_READ_MULTIPLE_VARIABLE_REQ : ATT_READ_MULTIPLE_REQUEST;
if ( (request_len < 5u) || ((request_len & 1u) == 0u) ){
return setup_error_invalid_pdu(response_buffer, request_type);
}if ((request_len < 5u) || ((request_len & 1u) == 0u)) { ... }
uint8_t num_handles = (request_len - 1u) >> 1u;
return handle_read_multiple_request2(att_connection, response_buffer, response_buffer_size, num_handles,
&request_buffer[1], store_length);
}{ ... }
static uint16_t handle_read_by_group_type_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size,
uint16_t start_handle, uint16_t end_handle,
uint16_t attribute_type_len, uint8_t * attribute_type){
UNUSED(att_connection);
log_info("ATT_READ_BY_GROUP_TYPE_REQUEST: from %04X to %04X, buffer size %u, type: ", start_handle, end_handle, response_buffer_size);
log_info_hexdump(attribute_type, attribute_type_len);
uint8_t request_type = ATT_READ_BY_GROUP_TYPE_REQUEST;
if ((start_handle > end_handle) || (start_handle == 0u)){
return setup_error_invalid_handle(response_buffer, request_type, start_handle);
}if ((start_handle > end_handle) || (start_handle == 0u)) { ... }
uint16_t uuid16 = uuid16_from_uuid(attribute_type_len, attribute_type);
if ((uuid16 != (uint16_t)GATT_PRIMARY_SERVICE_UUID) && (uuid16 != (uint16_t)GATT_SECONDARY_SERVICE_UUID)){
return setup_error(response_buffer, request_type, start_handle, ATT_ERROR_UNSUPPORTED_GROUP_TYPE);
}if ((uuid16 != (uint16_t)GATT_PRIMARY_SERVICE_UUID) && (uuid16 != (uint16_t)GATT_SECONDARY_SERVICE_UUID)) { ... }
uint16_t offset = 1;
uint16_t pair_len = 0;
bool in_group = false;
uint16_t group_start_handle = 0;
uint8_t const * group_start_value = NULL;
uint16_t prev_handle = 0;
att_iterator_t it;
att_iterator_init(&it);
while (att_iterator_has_next(&it)){
att_iterator_fetch_next(&it);
if ((it.handle != 0u) && (it.handle < start_handle)){
continue;
}if ((it.handle != 0u) && (it.handle < start_handle)) { ... }
if (it.handle > end_handle){
break;
}if (it.handle > end_handle) { ... }
if (in_group &&
((it.handle == 0u) || att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID))){
little_endian_store_16(response_buffer, offset, group_start_handle);
offset += 2u;
little_endian_store_16(response_buffer, offset, prev_handle);
offset += 2u;
(void)memcpy(response_buffer + offset, group_start_value,
pair_len - 4u);
offset += pair_len - 4u;
in_group = false;
if ((offset + pair_len) > response_buffer_size){
break;
}if ((offset + pair_len) > response_buffer_size) { ... }
}if (in_group && ((it.handle == 0u) || att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID))) { ... }
prev_handle = it.handle;
if ((it.handle != 0u) && att_iterator_match_uuid(&it, attribute_type, attribute_type_len)) {
uint16_t this_pair_len = 4u + it.value_len;
if (offset > 1u){
if (this_pair_len != pair_len) {
break;
}if (this_pair_len != pair_len) { ... }
}if (offset > 1u) { ... }
if (offset == 1u) {
pair_len = this_pair_len;
response_buffer[offset] = (uint8_t) this_pair_len;
offset++;
}if (offset == 1u) { ... }
group_start_handle = it.handle;
group_start_value = it.value;
in_group = true;
}if ((it.handle != 0u) && att_iterator_match_uuid(&it, attribute_type, attribute_type_len)) { ... }
}while (att_iterator_has_next(&it)) { ... }
if (offset == 1u){
return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
}if (offset == 1u) { ... }
response_buffer[0] = ATT_READ_BY_GROUP_TYPE_RESPONSE;
return offset;
}{ ... }
static uint16_t handle_read_by_group_type_request(att_connection_t * att_connection, uint8_t * request_buffer, uint16_t request_len,
uint8_t * response_buffer, uint16_t response_buffer_size){
uint16_t attribute_type_len;
switch (request_len){
case 7:
attribute_type_len = 2;
break;case 7:
case 21:
attribute_type_len = 16;
break;case 21:
default:
return setup_error_invalid_pdu(response_buffer, ATT_READ_BY_GROUP_TYPE_REQUEST);default
}switch (request_len) { ... }
uint16_t start_handle = little_endian_read_16(request_buffer, 1);
uint16_t end_handle = little_endian_read_16(request_buffer, 3);
return handle_read_by_group_type_request2(att_connection, response_buffer, response_buffer_size, start_handle, end_handle, attribute_type_len, &request_buffer[5]);
}{ ... }
static uint16_t handle_write_request(att_connection_t * att_connection, uint8_t * request_buffer, uint16_t request_len,
uint8_t * response_buffer, uint16_t response_buffer_size){
UNUSED(response_buffer_size);
if (request_len < 3u){
return setup_error_invalid_pdu(response_buffer, ATT_WRITE_REQUEST);
}if (request_len < 3u) { ... }
uint8_t request_type = ATT_WRITE_REQUEST;
uint16_t handle = little_endian_read_16(request_buffer, 1);
att_iterator_t it;
bool ok = att_find_handle(&it, handle);
if (!ok) {
return setup_error_invalid_handle(response_buffer, request_type, handle);
}if (!ok) { ... }
if (att_write_callback == NULL) {
return setup_error_write_not_permitted(response_buffer, request_type, handle);
}if (att_write_callback == NULL) { ... }
if ((it.flags & (uint16_t)ATT_PROPERTY_WRITE) == 0u) {
return setup_error_write_not_permitted(response_buffer, request_type, handle);
}if ((it.flags & (uint16_t)ATT_PROPERTY_WRITE) == 0u) { ... }
if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u) {
return setup_error_write_not_permitted(response_buffer, request_type, handle);
}if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u) { ... }
int error_code = att_validate_security(att_connection, ATT_WRITE, &it);
if (error_code != 0) {
return setup_error(response_buffer, request_type, handle, error_code);
}if (error_code != 0) { ... }
att_persistent_ccc_cache(&it);
error_code = (*att_write_callback)(att_connection->con_handle, handle, ATT_TRANSACTION_MODE_NONE, 0u, request_buffer + 3u, request_len - 3u);
#ifdef ENABLE_ATT_DELAYED_RESPONSE
if (error_code == ATT_ERROR_WRITE_RESPONSE_PENDING){
return ATT_INTERNAL_WRITE_RESPONSE_PENDING;
}if (error_code == ATT_ERROR_WRITE_RESPONSE_PENDING) { ... }
/* ... */#endif
if (error_code != 0) {
return setup_error(response_buffer, request_type, handle, error_code);
}if (error_code != 0) { ... }
response_buffer[0] = (uint8_t)ATT_WRITE_RESPONSE;
return 1;
}{ ... }
static uint16_t handle_prepare_write_request(att_connection_t * att_connection, uint8_t * request_buffer, uint16_t request_len,
uint8_t * response_buffer, uint16_t response_buffer_size){
uint8_t request_type = ATT_PREPARE_WRITE_REQUEST;
if (request_len < 5u){
return setup_error_invalid_pdu(response_buffer, request_type);
}if (request_len < 5u) { ... }
uint16_t handle = little_endian_read_16(request_buffer, 1);
uint16_t offset = little_endian_read_16(request_buffer, 3);
if (att_write_callback == NULL) {
return setup_error_write_not_permitted(response_buffer, request_type, handle);
}if (att_write_callback == NULL) { ... }
att_iterator_t it;
if (att_find_handle(&it, handle) == false) {
return setup_error_invalid_handle(response_buffer, request_type, handle);
}if (att_find_handle(&it, handle) == false) { ... }
if ((it.flags & (uint16_t)ATT_PROPERTY_WRITE) == 0u) {
return setup_error_write_not_permitted(response_buffer, request_type, handle);
}if ((it.flags & (uint16_t)ATT_PROPERTY_WRITE) == 0u) { ... }
if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u) {
return setup_error_write_not_permitted(response_buffer, request_type, handle);
}if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u) { ... }
int error_code = att_validate_security(att_connection, ATT_WRITE, &it);
if (error_code != 0) {
return setup_error(response_buffer, request_type, handle, error_code);
}if (error_code != 0) { ... }
error_code = (*att_write_callback)(att_connection->con_handle, handle, ATT_TRANSACTION_MODE_ACTIVE, offset, request_buffer + 5u, request_len - 5u);
switch (error_code){
case 0:
break;case 0:
case ATT_ERROR_INVALID_OFFSET:
case ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH:
att_prepare_write_update_errors(error_code, handle);
break;
#ifdef ENABLE_ATT_DELAYED_RESPONSEcase ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH:
case ATT_ERROR_WRITE_RESPONSE_PENDING:
return ATT_INTERNAL_WRITE_RESPONSE_PENDING;/* ... */
#endifcase ATT_ERROR_WRITE_RESPONSE_PENDING:
default:
return setup_error(response_buffer, request_type, handle, error_code);default
}switch (error_code) { ... }
uint16_t bytes_to_echo = btstack_min(request_len, response_buffer_size);
(void)memcpy(response_buffer, request_buffer, bytes_to_echo);
response_buffer[0] = ATT_PREPARE_WRITE_RESPONSE;
return request_len;
}{ ... }
/* ... */
void att_clear_transaction_queue(att_connection_t * att_connection){
(*att_write_callback)(att_connection->con_handle, 0, ATT_TRANSACTION_MODE_CANCEL, 0, NULL, 0);
}{ ... }
static uint16_t handle_execute_write_request(att_connection_t * att_connection, uint8_t * request_buffer, uint16_t request_len,
uint8_t * response_buffer, uint16_t response_buffer_size){
UNUSED(response_buffer_size);
uint8_t request_type = ATT_EXECUTE_WRITE_REQUEST;
if (request_len < 2u){
return setup_error_invalid_pdu(response_buffer, request_type);
}if (request_len < 2u) { ... }
if (att_write_callback == NULL) {
return setup_error_write_not_permitted(response_buffer, request_type, 0);
}if (att_write_callback == NULL) { ... }
if (request_buffer[1] != 0) {
if (att_prepare_write_error_code == 0){
att_prepare_write_error_code = (*att_write_callback)(att_connection->con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
}if (att_prepare_write_error_code == 0) { ... }
#ifdef ENABLE_ATT_DELAYED_RESPONSE
if (att_prepare_write_error_code == ATT_ERROR_WRITE_RESPONSE_PENDING){
return ATT_INTERNAL_WRITE_RESPONSE_PENDING;
}if (att_prepare_write_error_code == ATT_ERROR_WRITE_RESPONSE_PENDING) { ... }
/* ... */#endif
if (att_prepare_write_error_code != 0){
att_clear_transaction_queue(att_connection);
uint8_t error_code = att_prepare_write_error_code;
uint16_t handle = att_prepare_write_error_handle;
att_prepare_write_reset();
return setup_error(response_buffer, request_type, handle, error_code);
}if (att_prepare_write_error_code != 0) { ... }
att_write_callback(att_connection->con_handle, 0, ATT_TRANSACTION_MODE_EXECUTE, 0, NULL, 0);
}if (request_buffer[1] != 0) { ... } else {
att_clear_transaction_queue(att_connection);
}else { ... }
response_buffer[0] = ATT_EXECUTE_WRITE_RESPONSE;
return 1;
}{ ... }
static void handle_write_command(att_connection_t * att_connection, uint8_t * request_buffer, uint16_t request_len, uint16_t required_flags){
if (request_len < 3u){
return;
}if (request_len < 3u) { ... }
uint16_t handle = little_endian_read_16(request_buffer, 1);
if (att_write_callback == NULL){
return;
}if (att_write_callback == NULL) { ... }
att_iterator_t it;
bool ok = att_find_handle(&it, handle);
if (!ok){
return;
}if (!ok) { ... }
if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u){
return;
}if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u) { ... }
if ((it.flags & required_flags) == 0u){
return;
}if ((it.flags & required_flags) == 0u) { ... }
if (att_validate_security(att_connection, ATT_WRITE, &it) != ATT_ERROR_SUCCESS){
return;
}if (att_validate_security(att_connection, ATT_WRITE, &it) != ATT_ERROR_SUCCESS) { ... }
att_persistent_ccc_cache(&it);
(*att_write_callback)(att_connection->con_handle, handle, ATT_TRANSACTION_MODE_NONE, 0u, request_buffer + 3u, request_len - 3u);
}{ ... }
static uint16_t prepare_handle_value(att_connection_t * att_connection,
uint16_t handle,
const uint8_t *value,
uint16_t value_len,
uint8_t * response_buffer){
little_endian_store_16(response_buffer, 1, handle);
uint16_t bytes_to_copy = btstack_min(value_len, att_connection->mtu - 3u);
(void)memcpy(&response_buffer[3], value, bytes_to_copy);
return value_len + 3u;
}{ ... }
uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
uint16_t attribute_handle,
const uint8_t *value,
uint16_t value_len,
uint8_t * response_buffer){
response_buffer[0] = ATT_HANDLE_VALUE_NOTIFICATION;
return prepare_handle_value(att_connection, attribute_handle, value, value_len, response_buffer);
}{ ... }
uint16_t att_prepare_handle_value_multiple_notification(att_connection_t * att_connection,
uint8_t num_attributes,
const uint16_t * attribute_handles,
const uint8_t ** values_data,
const uint16_t * values_len,
uint8_t * response_buffer){
response_buffer[0] = ATT_MULTIPLE_HANDLE_VALUE_NTF;
uint8_t i;
uint16_t offset = 1;
uint16_t response_buffer_size = att_connection->mtu - 3u;
for (i = 0; i < num_attributes; i++) {
uint16_t value_len = values_len[i];
if ((offset + 4 + value_len) > response_buffer_size){
break;
}if ((offset + 4 + value_len) > response_buffer_size) { ... }
little_endian_store_16(response_buffer, offset, attribute_handles[i]);
offset += 2;
little_endian_store_16(response_buffer, offset, value_len);
offset += 2;
(void) memcpy(&response_buffer[offset], values_data[i], value_len);
offset += value_len;
}for (i = 0; i < num_attributes; i++) { ... }
return offset;
}{ ... }
uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
uint16_t attribute_handle,
const uint8_t *value,
uint16_t value_len,
uint8_t * response_buffer){
response_buffer[0] = ATT_HANDLE_VALUE_INDICATION;
return prepare_handle_value(att_connection, attribute_handle, value, value_len, response_buffer);
}{ ... }
uint16_t att_handle_request(att_connection_t * att_connection,
uint8_t * request_buffer,
uint16_t request_len,
uint8_t * response_buffer){
uint16_t response_len = 0;
const uint16_t response_buffer_size = att_connection->mtu;
const uint8_t request_opcode = request_buffer[0];
switch (request_opcode){
case ATT_EXCHANGE_MTU_REQUEST:
response_len = handle_exchange_mtu_request(att_connection, request_buffer, request_len, response_buffer);
break;case ATT_EXCHANGE_MTU_REQUEST:
case ATT_FIND_INFORMATION_REQUEST:
response_len = handle_find_information_request(att_connection, request_buffer, request_len,response_buffer, response_buffer_size);
break;case ATT_FIND_INFORMATION_REQUEST:
case ATT_FIND_BY_TYPE_VALUE_REQUEST:
response_len = handle_find_by_type_value_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
break;case ATT_FIND_BY_TYPE_VALUE_REQUEST:
case ATT_READ_BY_TYPE_REQUEST:
response_len = handle_read_by_type_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
break;case ATT_READ_BY_TYPE_REQUEST:
case ATT_READ_REQUEST:
response_len = handle_read_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
break;case ATT_READ_REQUEST:
case ATT_READ_BLOB_REQUEST:
response_len = handle_read_blob_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
break;case ATT_READ_BLOB_REQUEST:
case ATT_READ_MULTIPLE_REQUEST:
response_len = handle_read_multiple_request(att_connection, request_buffer, request_len, response_buffer,
response_buffer_size, false);
break;case ATT_READ_MULTIPLE_REQUEST:
case ATT_READ_MULTIPLE_VARIABLE_REQ:
response_len = handle_read_multiple_request(att_connection, request_buffer, request_len, response_buffer,
response_buffer_size, true);
break;case ATT_READ_MULTIPLE_VARIABLE_REQ:
case ATT_READ_BY_GROUP_TYPE_REQUEST:
response_len = handle_read_by_group_type_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
break;case ATT_READ_BY_GROUP_TYPE_REQUEST:
case ATT_WRITE_REQUEST:
response_len = handle_write_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
break;case ATT_WRITE_REQUEST:
case ATT_PREPARE_WRITE_REQUEST:
response_len = handle_prepare_write_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
break;case ATT_PREPARE_WRITE_REQUEST:
case ATT_EXECUTE_WRITE_REQUEST:
response_len = handle_execute_write_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
break;case ATT_EXECUTE_WRITE_REQUEST:
case ATT_WRITE_COMMAND:
handle_write_command(att_connection, request_buffer, request_len, ATT_PROPERTY_WRITE_WITHOUT_RESPONSE);
break;
#ifdef ENABLE_LE_SIGNED_WRITEcase ATT_WRITE_COMMAND:
case ATT_SIGNED_WRITE_COMMAND:
handle_write_command(att_connection, request_buffer, request_len, ATT_PROPERTY_AUTHENTICATED_SIGNED_WRITE);
break;/* ... */
#endifcase ATT_SIGNED_WRITE_COMMAND:
default:
response_len = setup_error(response_buffer, request_opcode, 0, ATT_ERROR_REQUEST_NOT_SUPPORTED);
break;default
}switch (request_opcode) { ... }
return response_len;
}{ ... }
bool gatt_server_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle){
bool in_group = false;
uint16_t prev_handle = 0;
uint16_t service_start = 0;
uint8_t attribute_value[2];
int attribute_len = sizeof(attribute_value);
little_endian_store_16(attribute_value, 0, uuid16);
att_iterator_t it;
att_iterator_init(&it);
while (att_iterator_has_next(&it)){
att_iterator_fetch_next(&it);
int new_service_started = att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID);
if (in_group &&
((it.handle == 0u) || new_service_started)){
in_group = false;
if ((service_start >= *start_handle) && (prev_handle <= *end_handle)){
*start_handle = service_start;
*end_handle = prev_handle;
return true;
}if ((service_start >= *start_handle) && (prev_handle <= *end_handle)) { ... }
}if (in_group && ((it.handle == 0u) || new_service_started)) { ... }
prev_handle = it.handle;
if ( (it.handle != 0u) && new_service_started && (attribute_len == it.value_len) && (memcmp(attribute_value, it.value, it.value_len) == 0)){
service_start = it.handle;
in_group = true;
}if ((it.handle != 0u) && new_service_started && (attribute_len == it.value_len) && (memcmp(attribute_value, it.value, it.value_len) == 0)) { ... }
}while (att_iterator_has_next(&it)) { ... }
return false;
}{ ... }
uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){
att_iterator_t it;
att_iterator_init(&it);
while (att_iterator_has_next(&it)){
att_iterator_fetch_next(&it);
if ((it.handle != 0u) && (it.handle < start_handle)){
continue;
}if ((it.handle != 0u) && (it.handle < start_handle)) { ... }
if (it.handle > end_handle){
break;
}if (it.handle > end_handle) { ... }
if (it.handle == 0u){
break;
}if (it.handle == 0u) { ... }
if (att_iterator_match_uuid16(&it, uuid16)){
return it.handle;
}if (att_iterator_match_uuid16(&it, uuid16)) { ... }
}while (att_iterator_has_next(&it)) { ... }
return 0;
}{ ... }
uint16_t gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16, uint16_t descriptor_uuid16){
att_iterator_t it;
att_iterator_init(&it);
bool characteristic_found = false;
while (att_iterator_has_next(&it)){
att_iterator_fetch_next(&it);
if ((it.handle != 0u) && (it.handle < start_handle)){
continue;
}if ((it.handle != 0u) && (it.handle < start_handle)) { ... }
if (it.handle > end_handle){
break;
}if (it.handle > end_handle) { ... }
if (it.handle == 0u){
break;
}if (it.handle == 0u) { ... }
if (att_iterator_match_uuid16(&it, characteristic_uuid16)){
characteristic_found = true;
continue;
}if (att_iterator_match_uuid16(&it, characteristic_uuid16)) { ... }
if (att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID)
|| att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID)
|| att_iterator_match_uuid16(&it, GATT_CHARACTERISTICS_UUID)){
if (characteristic_found){
break;
}if (characteristic_found) { ... }
continue;
}if (att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_CHARACTERISTICS_UUID)) { ... }
if (characteristic_found && att_iterator_match_uuid16(&it, descriptor_uuid16)){
return it.handle;
}if (characteristic_found && att_iterator_match_uuid16(&it, descriptor_uuid16)) { ... }
}while (att_iterator_has_next(&it)) { ... }
return 0;
}{ ... }
uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16){
return gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(start_handle, end_handle, characteristic_uuid16, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION);
}{ ... }
uint16_t gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16){
return gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(start_handle, end_handle, characteristic_uuid16, GATT_SERVER_CHARACTERISTICS_CONFIGURATION);
}{ ... }
bool gatt_server_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle){
bool in_group = false;
uint16_t prev_handle = 0;
uint8_t attribute_value[16];
uint16_t attribute_len = (uint16_t)sizeof(attribute_value);
reverse_128(uuid128, attribute_value);
att_iterator_t it;
att_iterator_init(&it);
while (att_iterator_has_next(&it)){
att_iterator_fetch_next(&it);
int new_service_started = att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID);
if (in_group &&
((it.handle == 0u) || new_service_started)){
*end_handle = prev_handle;
return true;
}if (in_group && ((it.handle == 0u) || new_service_started)) { ... }
prev_handle = it.handle;
if ( (it.handle != 0u) && new_service_started && (attribute_len == it.value_len) && (memcmp(attribute_value, it.value, it.value_len) == 0)){
*start_handle = it.handle;
in_group = true;
}if ((it.handle != 0u) && new_service_started && (attribute_len == it.value_len) && (memcmp(attribute_value, it.value, it.value_len) == 0)) { ... }
}while (att_iterator_has_next(&it)) { ... }
return false;
}{ ... }
uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){
uint8_t attribute_value[16];
reverse_128(uuid128, attribute_value);
att_iterator_t it;
att_iterator_init(&it);
while (att_iterator_has_next(&it)){
att_iterator_fetch_next(&it);
if ((it.handle != 0u) && (it.handle < start_handle)){
continue;
}if ((it.handle != 0u) && (it.handle < start_handle)) { ... }
if (it.handle > end_handle){
break;
}if (it.handle > end_handle) { ... }
if (it.handle == 0u){
break;
}if (it.handle == 0u) { ... }
if (att_iterator_match_uuid(&it, attribute_value, 16)){
return it.handle;
}if (att_iterator_match_uuid(&it, attribute_value, 16)) { ... }
}while (att_iterator_has_next(&it)) { ... }
return 0;
}{ ... }
uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){
uint8_t attribute_value[16];
reverse_128(uuid128, attribute_value);
att_iterator_t it;
att_iterator_init(&it);
bool characteristic_found = false;
while (att_iterator_has_next(&it)){
att_iterator_fetch_next(&it);
if ((it.handle != 0u) && (it.handle < start_handle)){
continue;
}if ((it.handle != 0u) && (it.handle < start_handle)) { ... }
if (it.handle > end_handle){
break;
}if (it.handle > end_handle) { ... }
if (it.handle == 0u){
break;
}if (it.handle == 0u) { ... }
if (att_iterator_match_uuid(&it, attribute_value, 16)){
characteristic_found = true;
continue;
}if (att_iterator_match_uuid(&it, attribute_value, 16)) { ... }
if (att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID)
|| att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID)
|| att_iterator_match_uuid16(&it, GATT_CHARACTERISTICS_UUID)){
if (characteristic_found){
break;
}if (characteristic_found) { ... }
continue;
}if (att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_CHARACTERISTICS_UUID)) { ... }
if (characteristic_found && att_iterator_match_uuid16(&it, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION)){
return it.handle;
}if (characteristic_found && att_iterator_match_uuid16(&it, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION)) { ... }
}while (att_iterator_has_next(&it)) { ... }
return 0;
}{ ... }
bool gatt_server_get_included_service_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16,
uint16_t * out_included_service_handle, uint16_t * out_included_service_start_handle, uint16_t * out_included_service_end_handle){
att_iterator_t it;
att_iterator_init(&it);
while (att_iterator_has_next(&it)){
att_iterator_fetch_next(&it);
if ((it.handle != 0u) && (it.handle < start_handle)){
continue;
}if ((it.handle != 0u) && (it.handle < start_handle)) { ... }
if (it.handle > end_handle){
break;
}if (it.handle > end_handle) { ... }
if (it.handle == 0u){
break;
}if (it.handle == 0u) { ... }
if ((it.value_len == 6) && (att_iterator_match_uuid16(&it, GATT_INCLUDE_SERVICE_UUID))){
if (little_endian_read_16(it.value, 4) == uuid16){
*out_included_service_handle = it.handle;
*out_included_service_start_handle = little_endian_read_16(it.value, 0);
*out_included_service_end_handle = little_endian_read_16(it.value, 2);
return true;
}if (little_endian_read_16(it.value, 4) == uuid16) { ... }
}if ((it.value_len == 6) && (att_iterator_match_uuid16(&it, GATT_INCLUDE_SERVICE_UUID))) { ... }
}while (att_iterator_has_next(&it)) { ... }
return false;
}{ ... }
static void att_persistent_ccc_cache(att_iterator_t * it){
att_persistent_ccc_handle = it->handle;
if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
att_persistent_ccc_uuid16 = 0u;
}if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u) { ... } else {
att_persistent_ccc_uuid16 = little_endian_read_16(it->uuid, 0);
}else { ... }
}{ ... }
bool att_is_persistent_ccc(uint16_t handle){
if (handle != att_persistent_ccc_handle){
att_iterator_t it;
bool ok = att_find_handle(&it, handle);
if (!ok){
return false;
}if (!ok) { ... }
att_persistent_ccc_cache(&it);
}if (handle != att_persistent_ccc_handle) { ... }
switch (att_persistent_ccc_uuid16){
case GATT_CLIENT_CHARACTERISTICS_CONFIGURATION:
case GATT_CLIENT_SUPPORTED_FEATURES:
return true;case GATT_CLIENT_SUPPORTED_FEATURES:
default:
return false;default
}switch (att_persistent_ccc_uuid16) { ... }
}{ ... }
uint16_t att_read_callback_handle_blob(const uint8_t * blob, uint16_t blob_size, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
btstack_assert(blob != NULL);
if (buffer != NULL){
uint16_t bytes_to_copy = 0;
if (blob_size >= offset){
bytes_to_copy = btstack_min(blob_size - offset, buffer_size);
(void)memcpy(buffer, &blob[offset], bytes_to_copy);
}if (blob_size >= offset) { ... }
return bytes_to_copy;
}if (buffer != NULL) { ... } else {
return blob_size;
}else { ... }
}{ ... }
uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
uint8_t value_buffer[4];
little_endian_store_32(value_buffer, 0, value);
return att_read_callback_handle_blob(value_buffer, sizeof(value_buffer), offset, buffer, buffer_size);
}{ ... }
uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
uint8_t value_buffer[2];
little_endian_store_16(value_buffer, 0, value);
return att_read_callback_handle_blob(value_buffer, sizeof(value_buffer), offset, buffer, buffer_size);
}{ ... }
uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
uint8_t value_buffer[1];
value_buffer[0] = value;
return att_read_callback_handle_blob(value_buffer, sizeof(value_buffer), offset, buffer, buffer_size);
}{ ... }
#ifdef ENABLE_BTP
#include "btp.h"
static uint8_t btp_permissions_for_flags(uint16_t flags){
uint8_t permissions = 0;
uint8_t read_security_level = 0;
uint8_t write_security_level = 0;
if (flags & (uint16_t)ATT_PROPERTY_READ){
if (flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_0) {
read_security_level |= 1;
}if (flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_0) { ... }
if (flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_1) {
read_security_level |= 2;
}if (flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_1) { ... }
if (read_security_level == ATT_SECURITY_AUTHORIZED) {
permissions |= BTP_GATT_PERM_READ_AUTHZ;
}if (read_security_level == ATT_SECURITY_AUTHORIZED) { ... }
if (read_security_level == ATT_SECURITY_AUTHENTICATED) {
permissions |= BTP_GATT_PERM_READ_AUTHN;
}if (read_security_level == ATT_SECURITY_AUTHENTICATED) { ... }
if (read_security_level == ATT_SECURITY_ENCRYPTED) {
permissions |= BTP_GATT_PERM_READ_ENC;
}if (read_security_level == ATT_SECURITY_ENCRYPTED) { ... }
if (read_security_level == ATT_SECURITY_NONE) {
permissions |= BTP_GATT_PERM_READ;
}if (read_security_level == ATT_SECURITY_NONE) { ... }
}if (flags & (uint16_t)ATT_PROPERTY_READ) { ... }
if (flags & (ATT_PROPERTY_WRITE | ATT_PROPERTY_WRITE_WITHOUT_RESPONSE)){
if (flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_0) {
write_security_level |= 1;
}if (flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_0) { ... }
if (flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_1) {
write_security_level |= 2;
}if (flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_1) { ... }
if (write_security_level == ATT_SECURITY_AUTHORIZED) {
permissions |= BTP_GATT_PERM_WRITE_AUTHZ;
}if (write_security_level == ATT_SECURITY_AUTHORIZED) { ... }
if (write_security_level == ATT_SECURITY_AUTHENTICATED) {
permissions |= BTP_GATT_PERM_WRITE_AUTHN;
}if (write_security_level == ATT_SECURITY_AUTHENTICATED) { ... }
if (write_security_level == ATT_SECURITY_ENCRYPTED) {
permissions |= BTP_GATT_PERM_WRITE_ENC;
}if (write_security_level == ATT_SECURITY_ENCRYPTED) { ... }
if (write_security_level == ATT_SECURITY_NONE) {
permissions |= BTP_GATT_PERM_WRITE;
}if (write_security_level == ATT_SECURITY_NONE) { ... }
}if (flags & (ATT_PROPERTY_WRITE | ATT_PROPERTY_WRITE_WITHOUT_RESPONSE)) { ... }
return permissions;
}btp_permissions_for_flags (uint16_t flags) { ... }
uint16_t btp_att_get_attributes_by_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16, uint8_t * response_buffer, uint16_t response_buffer_size){
log_info("btp_att_get_attributes_by_uuid16 %04x from 0x%04x to 0x%04x, db %p", uuid16, start_handle, end_handle, att_database);
att_dump_attributes();
uint8_t num_attributes = 0;
uint16_t pos = 1;
att_iterator_t it;
att_iterator_init(&it);
while (att_iterator_has_next(&it) && ((pos + 6) < response_buffer_size)){
att_iterator_fetch_next(&it);
log_info("handle %04x", it.handle);
if (it.handle == 0){
break;
}if (it.handle == 0) { ... }
if (it.handle < start_handle){
continue;
}if (it.handle < start_handle) { ... }
if (it.handle > end_handle){
break;
}if (it.handle > end_handle) { ... }
if ((uuid16 == 0) || att_iterator_match_uuid16(&it, uuid16)){
little_endian_store_16(response_buffer, pos, it.handle);
pos += 2;
response_buffer[pos++] = btp_permissions_for_flags(it.flags);
response_buffer[pos++] = 2;
little_endian_store_16(response_buffer, pos, uuid16);
pos += 2;
num_attributes++;
}if ((uuid16 == 0) || att_iterator_match_uuid16(&it, uuid16)) { ... }
}while (att_iterator_has_next(&it) && ((pos + 6) < response_buffer_size)) { ... }
response_buffer[0] = num_attributes;
return pos;
}btp_att_get_attributes_by_uuid16 (uint16_t start_handle, uint16_t end_handle, uint16_t uuid16, uint8_t * response_buffer, uint16_t response_buffer_size) { ... }
uint16_t btp_att_get_attributes_by_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128, uint8_t * response_buffer, uint16_t response_buffer_size){
uint8_t num_attributes = 0;
uint16_t pos = 1;
att_iterator_t it;
att_iterator_init(&it);
while (att_iterator_has_next(&it) && ((pos + 20) < response_buffer_size)){
att_iterator_fetch_next(&it);
if (it.handle == 0){
break;
}if (it.handle == 0) { ... }
if (it.handle < start_handle){
continue;
}if (it.handle < start_handle) { ... }
if (it.handle > end_handle){
break;
}if (it.handle > end_handle) { ... }
if (att_iterator_match_uuid(&it, (uint8_t*) uuid128, 16)){
little_endian_store_16(response_buffer, pos, it.handle);
pos += 2;
response_buffer[pos++] = btp_permissions_for_flags(it.flags);
response_buffer[pos++] = 16;
reverse_128(uuid128, &response_buffer[pos]);
pos += 16;
num_attributes++;
}if (att_iterator_match_uuid(&it, (uint8_t*) uuid128, 16)) { ... }
}while (att_iterator_has_next(&it) && ((pos + 20) < response_buffer_size)) { ... }
response_buffer[0] = num_attributes;
return pos;
}btp_att_get_attributes_by_uuid128 (uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128, uint8_t * response_buffer, uint16_t response_buffer_size) { ... }
uint16_t btp_att_get_attribute_value(att_connection_t * att_connection, uint16_t attribute_handle, uint8_t * response_buffer, uint16_t response_buffer_size){
att_iterator_t it;
bool ok = att_find_handle(&it, attribute_handle);
if (!ok){
return 0;
}if (!ok) { ... }
uint16_t pos = 0;
response_buffer[pos++] = att_validate_security(att_connection, ATT_READ, &it);
uint16_t bytes_to_copy = btstack_min( response_buffer_size - 3, it.value_len);
little_endian_store_16(response_buffer, pos, bytes_to_copy);
pos += 2;
if (it.value){
memcpy(&response_buffer[pos], it.value, bytes_to_copy);
pos += bytes_to_copy;
}if (it.value) { ... }
return pos;
}btp_att_get_attribute_value (att_connection_t * att_connection, uint16_t attribute_handle, uint8_t * response_buffer, uint16_t response_buffer_size) { ... }
/* ... */
#endif