/
att_db.c
1599 lines (1356 loc) · 62 KB
/
att_db.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#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"
// check for ENABLE_ATT_DELAYED_READ_RESPONSE -> ENABLE_ATT_DELAYED_RESPONSE,
#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 int is_Bluetooth_Base_UUID(uint8_t const *uuid){
// Bluetooth Base UUID 00000000-0000-1000-8000-00805F9B34FB in little endian
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[14], &bluetooth_base_uuid[14], 2) != 0) return false;
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 (!is_Bluetooth_Base_UUID(uuid)) return 0;
return little_endian_read_16(uuid, 12);
}
// ATT Database
// new java-style iterator
typedef struct att_iterator {
// private
uint8_t const * att_ptr;
// public
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;
// single cache for att_is_persistent_ccc - stores flags before write callback
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;
}
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];
// handle 128 bit UUIDs
if ((it->flags & ATT_PROPERTY_UUID128) != 0u){
it->value_len = it->size - 22u;
it->value = &it->att_ptr[22];
} else {
it->value_len = it->size - 8u;
it->value = &it->att_ptr[8];
}
// advance AFTER setting values
it->att_ptr += it->size;
}
static int att_iterator_match_uuid16(att_iterator_t *it, uint16_t uuid){
if (it->handle == 0u) return 0u;
if (it->flags & ATT_PROPERTY_UUID128){
if (!is_Bluetooth_Base_UUID(it->uuid)) return 0;
return little_endian_read_16(it->uuid, 12) == uuid;
}
return little_endian_read_16(it->uuid, 0) == uuid;
}
static int att_iterator_match_uuid(att_iterator_t *it, uint8_t *uuid, uint16_t uuid_len){
if (it->handle == 0u) return 0u;
// input: UUID16
if (uuid_len == 2u) {
return att_iterator_match_uuid16(it, little_endian_read_16(uuid, 0));
}
// input and db: UUID128
if ((it->flags & ATT_PROPERTY_UUID128) != 0u){
return memcmp(it->uuid, uuid, 16) == 0;
}
// input: UUID128, db: UUID16
if (!is_Bluetooth_Base_UUID(uuid)) return 0;
return little_endian_read_16(uuid, 12) == little_endian_read_16(it->uuid, 0);
}
static int att_find_handle(att_iterator_t *it, uint16_t handle){
if (handle == 0u) return 0u;
att_iterator_init(it);
while (att_iterator_has_next(it)){
att_iterator_fetch_next(it);
if (it->handle != handle) continue;
return 1;
}
return 0;
}
// experimental client API
uint16_t att_uuid_for_handle(uint16_t attribute_handle){
att_iterator_t it;
int ok = att_find_handle(&it, attribute_handle);
if (!ok) return 0;
if ((it.flags & ATT_PROPERTY_UUID128) != 0u) return 0u;
return little_endian_read_16(it.uuid, 0);
}
// end of client API
static void att_update_value_len(att_iterator_t *it, hci_con_handle_t con_handle){
if ((it->flags & ATT_PROPERTY_DYNAMIC) == 0u) return;
it->value_len = (*att_read_callback)(con_handle, it->handle, 0, NULL, 0);
return;
}
// copy attribute value from offset into buffer with given size
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){
// DYNAMIC
if ((it->flags & ATT_PROPERTY_DYNAMIC) != 0u){
return (*att_read_callback)(con_handle, it->handle, offset, buffer, buffer_size);
}
// STATIC
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){
// validate db version
if (db == NULL) return;
if (*db != ATT_DB_VERSION){
log_error("ATT DB version differs, please regenerate .h from .gatt file or update att_db_util.c");
return;
}
log_info("att_set_db %p", db);
// ignore db version
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;
}
log_info("Handle: 0x%04x, flags: 0x%04x, uuid: ", it.handle, it.flags);
if ((it.flags & ATT_PROPERTY_UUID128) != 0u){
reverse_128(it.uuid, uuid128);
log_info("%s", uuid128_to_str(uuid128));
} else {
log_info("%04x", little_endian_read_16(it.uuid, 0));
}
log_info(", value_len: %u, value: ", it.value_len);
log_info_hexdump(it.value, it.value_len);
}
}
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){
// first ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH has highest priority
if ((error_code == ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH) && (error_code != att_prepare_write_error_code)){
att_prepare_write_error_code = error_code;
att_prepare_write_error_handle = handle;
return;
}
// first ATT_ERROR_INVALID_OFFSET is next
if ((error_code == ATT_ERROR_INVALID_OFFSET) && (att_prepare_write_error_code == 0)){
att_prepare_write_error_code = error_code;
att_prepare_write_error_handle = handle;
return;
}
}
static uint16_t setup_error(uint8_t * response_buffer, uint16_t request, uint16_t handle, uint8_t error_code){
response_buffer[0] = ATT_ERROR_RESPONSE;
response_buffer[1] = request;
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, uint16_t request, uint16_t start_handle){
return setup_error(response_buffer, request, start_handle, ATT_ERROR_READ_NOT_PERMITTED);
}
static inline uint16_t setup_error_write_not_permitted(uint8_t * response_buffer, uint16_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, uint16_t request, uint16_t start_handle){
return setup_error(response_buffer, request, start_handle, ATT_ERROR_ATTRIBUTE_NOT_FOUND);
}
static inline uint16_t setup_error_invalid_handle(uint8_t * response_buffer, uint16_t request, uint16_t handle){
return setup_error(response_buffer, request, handle, ATT_ERROR_INVALID_HANDLE);
}
static inline uint16_t setup_error_invalid_offset(uint8_t * response_buffer, uint16_t request, uint16_t handle){
return setup_error(response_buffer, request, handle, ATT_ERROR_INVALID_OFFSET);
}
static inline uint16_t setup_error_invalid_pdu(uint8_t *response_buffer, uint16_t request) {
return setup_error(response_buffer, request, 0, ATT_ERROR_INVALID_PDU);
}
static uint8_t att_validate_security(att_connection_t * att_connection, att_operation_t operation, att_iterator_t * it){
int required_security_level = 0;
bool requires_secure_connection = false;
switch (operation){
case ATT_READ:
if ((it->flags & ATT_PROPERTY_READ_PERMISSION_BIT_0) != 0u){
required_security_level |= 1;
}
if ((it->flags & ATT_PROPERTY_READ_PERMISSION_BIT_1) != 0u){
required_security_level |= 2;
}
if ((it->flags & ATT_PROPERTY_READ_PERMISSION_SC) != 0u){
requires_secure_connection = true;
}
break;
case ATT_WRITE:
if ((it->flags & ATT_PROPERTY_WRITE_PERMISSION_BIT_0) != 0u){
required_security_level |= 1;
}
if ((it->flags & ATT_PROPERTY_WRITE_PERMISSION_BIT_1) != 0u){
required_security_level |= 2;
}
if ((it->flags & ATT_PROPERTY_WRITE_PERMISSION_SC) != 0u){
requires_secure_connection = true;
}
break;
default:
btstack_assert(false);
break;
}
uint8_t required_encryption_size = it->flags >> 12;
if (required_encryption_size != 0) required_encryption_size++; // store -1 to fit into 4 bit
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, required_security_level, required_encryption_size, att_connection->authorized, att_connection->authenticated, att_connection->encryption_key_size, att_connection->secure_connection);
bool sc_missing = requires_secure_connection && (att_connection->secure_connection == 0u);
switch (required_security_level){
case ATT_SECURITY_AUTHORIZED:
if ((att_connection->authorized == 0u) || sc_missing){
return ATT_ERROR_INSUFFICIENT_AUTHORIZATION;
}
/* fall through */
case ATT_SECURITY_AUTHENTICATED:
if ((att_connection->authenticated == 0u) || sc_missing){
return ATT_ERROR_INSUFFICIENT_AUTHENTICATION;
}
/* fall through */
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 > att_connection->encryption_key_size){
return ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE;
}
break;
default:
break;
}
return ATT_ERROR_SUCCESS;
}
//
// MARK: ATT_EXCHANGE_MTU_REQUEST
//
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);
uint16_t client_rx_mtu = little_endian_read_16(request_buffer, 1);
// find min(local max mtu, remote mtu) >= ATT_DEFAULT_MTU and use as mtu for this connection
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;
}
//
// MARK: ATT_FIND_INFORMATION_REQUEST
//
// TODO: handle other types then GATT_PRIMARY_SERVICE_UUID and GATT_SECONDARY_SERVICE_UUID
//
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);
}
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 > end_handle) break;
if (it.handle < start_handle) continue;
// log_info("Handle 0x%04x", it.handle);
uint16_t this_uuid_len = (it.flags & ATT_PROPERTY_UUID128) ? 16 : 2;
// check if value has same len as last one if not first result
if (offset > 1u){
if (this_uuid_len != uuid_len) {
break;
}
}
// first
if (offset == 1u) {
uuid_len = this_uuid_len;
// set format field
response_buffer[offset] = (it.flags & ATT_PROPERTY_UUID128) ? 0x02 : 0x01;
offset++;
}
// space?
if ((offset + 2u + uuid_len) > response_buffer_size) break;
// store
little_endian_store_16(response_buffer, offset, it.handle);
offset += 2u;
(void)memcpy(response_buffer + offset, it.uuid, uuid_len);
offset += uuid_len;
}
if (offset == 1u){
return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
}
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);
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);
}
//
// MARK: ATT_FIND_BY_TYPE_VALUE
//
// "Only attributes with attribute handles between and including the Starting Handle parameter
// and the Ending Handle parameter that match the requested attri- bute type and the attribute
// value that have sufficient permissions to allow reading will be returned" -> (1)
//
// TODO: handle other types then GATT_PRIMARY_SERVICE_UUID and GATT_SECONDARY_SERVICE_UUID
//
// NOTE: doesn't handle DYNAMIC values
// NOTE: only supports 16 bit UUIDs
//
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);
// parse request
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);
}
uint16_t offset = 1;
uint16_t in_group = 0;
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 && (it.handle < start_handle)) continue;
if (it.handle > end_handle) break; // (1)
// close current tag, if within a group and a new service definition starts or we reach end of att db
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 = 0;
// check if space for another handle pair available
if ((offset + 4u) > response_buffer_size){
break;
}
}
// keep track of previous handle
prev_handle = it.handle;
// does current attribute match
if (it.handle && 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 = 1;
}
}
if (offset == 1u){
return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
}
response_buffer[0] = ATT_FIND_BY_TYPE_VALUE_RESPONSE;
return offset;
}
//
// MARK: ATT_READ_BY_TYPE_REQUEST
//
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);
}
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 < start_handle) continue;
// does current attribute match
if (!att_iterator_match_uuid(&it, attribute_type, attribute_type_len)) continue;
// skip handles that cannot be read but remember that there has been at least one
if ((it.flags & ATT_PROPERTY_READ) == 0u) {
if (first_matching_but_unreadable_handle == 0u) {
first_matching_but_unreadable_handle = it.handle;
}
continue;
}
// check security requirements
error_code = att_validate_security(att_connection, ATT_READ, &it);
if (error_code != 0u) break;
att_update_value_len(&it, att_connection->con_handle);
#ifdef ENABLE_ATT_DELAYED_RESPONSE
if (it.value_len == ATT_READ_RESPONSE_PENDING){
return ATT_READ_RESPONSE_PENDING;
}
#endif
// allow to return ATT Error Code in ATT Read Callback
if (it.value_len > ATT_READ_ERROR_CODE_OFFSET){
error_code = it.value_len - ATT_READ_ERROR_CODE_OFFSET;
break;
}
// check if value has same len as last one
uint16_t this_pair_len = 2u + it.value_len;
if ((offset > 1u) && (pair_len != this_pair_len)) {
break;
}
// first
if (offset == 1u) {
pair_len = this_pair_len;
response_buffer[offset] = pair_len;
offset++;
}
// space?
if ((offset + pair_len) > response_buffer_size) {
if (offset > 2u) break;
it.value_len = response_buffer_size - 4u;
response_buffer[1u] = 2u + it.value_len;
}
// store
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;
}
// at least one attribute could be read
if (offset > 1u){
response_buffer[0] = ATT_READ_BY_TYPE_RESPONSE;
return offset;
}
// first attribute had an error
if (error_code != 0u){
return setup_error(response_buffer, request_type, start_handle, error_code);
}
// no other errors, but all found attributes had been non-readable
if (first_matching_but_unreadable_handle != 0u){
return setup_error_read_not_permitted(response_buffer, request_type, first_matching_but_unreadable_handle);
}
// attribute not found
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 21:
attribute_type_len = 16;
break;
default:
return setup_error_invalid_pdu(response_buffer, ATT_READ_BY_TYPE_REQUEST);
}
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]);
}
//
// MARK: ATT_READ_BY_TYPE_REQUEST
//
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;
int ok = att_find_handle(&it, handle);
if (!ok){
return setup_error_invalid_handle(response_buffer, request_type, handle);
}
// check if handle can be read
if ((it.flags & ATT_PROPERTY_READ) == 0u) {
return setup_error_read_not_permitted(response_buffer, request_type, handle);
}
// check security requirements
uint8_t error_code = att_validate_security(att_connection, ATT_READ, &it);
if (error_code != 0) {
return setup_error(response_buffer, request_type, handle, error_code);
}
att_update_value_len(&it, att_connection->con_handle);
#ifdef ENABLE_ATT_DELAYED_RESPONSE
if (it.value_len == ATT_READ_RESPONSE_PENDING) return ATT_READ_RESPONSE_PENDING;
#endif
// allow to return ATT Error Code in ATT Read Callback
if (it.value_len > ATT_READ_ERROR_CODE_OFFSET){
error_code = it.value_len - ATT_READ_ERROR_CODE_OFFSET;
return setup_error(response_buffer, request_type, handle, error_code);
}
// store
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);
uint16_t handle = little_endian_read_16(request_buffer, 1);
return handle_read_request2(att_connection, response_buffer, response_buffer_size, handle);
}
//s
// MARK: ATT_READ_BLOB_REQUEST 0x0c
//
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;
int ok = att_find_handle(&it, handle);
if (!ok){
return setup_error_invalid_handle(response_buffer, request_type, handle);
}
// check if handle can be read
if ((it.flags & ATT_PROPERTY_READ) == 0u) {
return setup_error_read_not_permitted(response_buffer, request_type, handle);
}
// check security requirements
uint8_t error_code = att_validate_security(att_connection, ATT_READ, &it);
if (error_code != 0) {
return setup_error(response_buffer, request_type, handle, error_code);
}
att_update_value_len(&it, att_connection->con_handle);
#ifdef ENABLE_ATT_DELAYED_RESPONSE
if (it.value_len == ATT_READ_RESPONSE_PENDING) return ATT_READ_RESPONSE_PENDING;
#endif
// allow to return ATT Error Code in ATT Read Callback
if (it.value_len > ATT_READ_ERROR_CODE_OFFSET){
error_code = it.value_len - ATT_READ_ERROR_CODE_OFFSET;
return setup_error(response_buffer, request_type, handle, error_code);
}
if (value_offset > it.value_len){
return setup_error_invalid_offset(response_buffer, request_type, handle);
}
// prepare response
response_buffer[0] = ATT_READ_BLOB_RESPONSE;
uint16_t offset = 1;
// fetch more data if available
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;
}
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);
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);
}
//
// MARK: ATT_READ_MULTIPLE_REQUEST 0x0e
//
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){
log_info("ATT_READ_MULTIPLE_REQUEST: num handles %u", num_handles);
uint8_t request_type = ATT_READ_MULTIPLE_REQUEST;
uint16_t offset = 1;
int 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);
}
att_iterator_t it;
int ok = att_find_handle(&it, handle);
if (!ok){
return setup_error_invalid_handle(response_buffer, request_type, handle);
}
// check if handle can be read
if ((it.flags & ATT_PROPERTY_READ) == 0u) {
error_code = ATT_ERROR_READ_NOT_PERMITTED;
break;
}
// check security requirements
error_code = att_validate_security(att_connection, ATT_READ, &it);
if (error_code != 0) break;
att_update_value_len(&it, att_connection->con_handle);
#ifdef ENABLE_ATT_DELAYED_RESPONSE
if (it.value_len == ATT_READ_RESPONSE_PENDING) {
read_request_pending = true;
}
if (read_request_pending) continue;
#endif
// allow to return ATT Error Code in ATT Read Callback
if (it.value_len > ATT_READ_ERROR_CODE_OFFSET){
error_code = it.value_len -ATT_READ_ERROR_CODE_OFFSET;
break;
}
// store
uint16_t bytes_copied = att_copy_value(&it, 0, response_buffer + offset, response_buffer_size - offset, att_connection->con_handle);
offset += bytes_copied;
}
if (error_code != 0){
return setup_error(response_buffer, request_type, handle, error_code);
}
response_buffer[0] = ATT_READ_MULTIPLE_RESPONSE;
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){
// 1 byte opcode + two or more attribute handles (2 bytes each)
if ( (request_len < 5u) || ((request_len & 1u) == 0u) ) return setup_error_invalid_pdu(response_buffer,
ATT_READ_MULTIPLE_REQUEST);
int num_handles = (request_len - 1u) >> 1u;
return handle_read_multiple_request2(att_connection, response_buffer, response_buffer_size, num_handles, &request_buffer[1]);
}
//
// MARK: ATT_READ_BY_GROUP_TYPE_REQUEST 0x10
//
// Only handles GATT_PRIMARY_SERVICE_UUID and GATT_SECONDARY_SERVICE_UUID
// Core v4.0, vol 3, part g, 2.5.3
// "The «Primary Service» and «Secondary Service» grouping types may be used in the Read By Group Type Request.
// The «Characteristic» grouping type shall not be used in the ATT Read By Group Type Request."
//
// NOTE: doesn't handle DYNAMIC values
//
// NOTE: we don't check for security as PRIMARY and SECONDAY SERVICE definition shouldn't be protected
// Core 4.0, vol 3, part g, 8.1
// "The list of services and characteristics that a device supports is not considered private or
// confidential information, and therefore the Service and Characteristic Discovery procedures
// shall always be permitted. "
//
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);
}
// assert UUID is primary or secondary service uuid
uint16_t uuid16 = uuid16_from_uuid(attribute_type_len, attribute_type);
if ((uuid16 != GATT_PRIMARY_SERVICE_UUID) && (uuid16 != GATT_SECONDARY_SERVICE_UUID)){
return setup_error(response_buffer, request_type, start_handle, ATT_ERROR_UNSUPPORTED_GROUP_TYPE);
}
uint16_t offset = 1;
uint16_t pair_len = 0;
uint16_t in_group = 0;
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 && (it.handle < start_handle)) continue;
if (it.handle > end_handle) break; // (1)
// log_info("Handle 0x%04x", it.handle);
// close current tag, if within a group and a new service definition starts or we reach end of att db
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, val_len: %u", prev_handle, pair_len - 4);
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 = 0;
// check if space for another handle pair available
if ((offset + pair_len) > response_buffer_size){
break;
}
}
// keep track of previous handle
prev_handle = it.handle;
// does current attribute match
// log_info("compare: %04x == %04x", *(uint16_t*) context->attribute_type, *(uint16_t*) uuid);
if (it.handle && att_iterator_match_uuid(&it, attribute_type, attribute_type_len)) {
// check if value has same len as last one
uint16_t this_pair_len = 4u + it.value_len;
if (offset > 1u){
if (this_pair_len != pair_len) {
break;
}
}
// log_info("Begin of group, handle 0x%04x", it.handle);
// first
if (offset == 1u) {
pair_len = this_pair_len;
response_buffer[offset] = this_pair_len;
offset++;
}
group_start_handle = it.handle;
group_start_value = it.value;
in_group = 1;
}
}
if (offset == 1u){
return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
}
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 21:
attribute_type_len = 16;
break;
default:
return setup_error_invalid_pdu(response_buffer, ATT_READ_BY_GROUP_TYPE_REQUEST);
}
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]);
}
//
// MARK: ATT_WRITE_REQUEST 0x12
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);
uint8_t request_type = ATT_WRITE_REQUEST;
uint16_t handle = little_endian_read_16(request_buffer, 1);