3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2014 Intel Corporation. All rights reserved.
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this library; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31
#include "lib/bluetooth.h"
33
#include "src/shared/util.h"
34
#include "src/shared/queue.h"
35
#include "src/shared/timeout.h"
36
#include "src/shared/att.h"
37
#include "src/shared/gatt-db.h"
40
#define MAX(a, b) ((a) > (b) ? (a) : (b))
43
#define MAX_CHAR_DECL_VALUE_LEN 19
44
#define MAX_INCLUDED_VALUE_LEN 6
45
#define ATTRIBUTE_TIMEOUT 5000
47
static const bt_uuid_t primary_service_uuid = { .type = BT_UUID16,
48
.value.u16 = GATT_PRIM_SVC_UUID };
49
static const bt_uuid_t secondary_service_uuid = { .type = BT_UUID16,
50
.value.u16 = GATT_SND_SVC_UUID };
51
static const bt_uuid_t characteristic_uuid = { .type = BT_UUID16,
52
.value.u16 = GATT_CHARAC_UUID };
53
static const bt_uuid_t included_service_uuid = { .type = BT_UUID16,
54
.value.u16 = GATT_INCLUDE_UUID };
59
struct queue *services;
61
struct queue *notify_list;
62
unsigned int next_notify_id;
67
gatt_db_attribute_cb_t service_added;
68
gatt_db_attribute_cb_t service_removed;
69
gatt_db_destroy_func_t destroy;
74
struct gatt_db_attribute *attrib;
76
unsigned int timeout_id;
77
gatt_db_attribute_read_t func;
81
struct pending_write {
82
struct gatt_db_attribute *attrib;
84
unsigned int timeout_id;
85
gatt_db_attribute_write_t func;
89
struct gatt_db_attribute {
90
struct gatt_db_service *service;
97
gatt_db_read_t read_func;
98
gatt_db_write_t write_func;
101
unsigned int read_id;
102
struct queue *pending_reads;
104
unsigned int write_id;
105
struct queue *pending_writes;
108
struct gatt_db_service {
112
uint16_t num_handles;
113
struct gatt_db_attribute **attributes;
116
static void pending_read_result(struct pending_read *p, int err,
117
const uint8_t *data, size_t length)
119
if (p->timeout_id > 0)
120
timeout_remove(p->timeout_id);
122
p->func(p->attrib, err, data, length, p->user_data);
127
static void pending_read_free(void *data)
129
struct pending_read *p = data;
131
pending_read_result(p, -ECANCELED, NULL, 0);
134
static void pending_write_result(struct pending_write *p, int err)
136
if (p->timeout_id > 0)
137
timeout_remove(p->timeout_id);
139
p->func(p->attrib, err, p->user_data);
144
static void pending_write_free(void *data)
146
struct pending_write *p = data;
148
pending_write_result(p, -ECANCELED);
151
static void attribute_destroy(struct gatt_db_attribute *attribute)
153
/* Attribute was not initialized by user */
157
queue_destroy(attribute->pending_reads, pending_read_free);
158
queue_destroy(attribute->pending_writes, pending_write_free);
160
free(attribute->value);
164
static struct gatt_db_attribute *new_attribute(struct gatt_db_service *service,
166
const bt_uuid_t *type,
170
struct gatt_db_attribute *attribute;
172
attribute = new0(struct gatt_db_attribute, 1);
174
attribute->service = service;
175
attribute->handle = handle;
176
attribute->uuid = *type;
177
attribute->value_len = len;
179
attribute->value = malloc0(len);
180
if (!attribute->value)
183
memcpy(attribute->value, val, len);
186
attribute->pending_reads = queue_new();
187
attribute->pending_writes = queue_new();
192
attribute_destroy(attribute);
196
struct gatt_db *gatt_db_ref(struct gatt_db *db)
201
__sync_fetch_and_add(&db->ref_count, 1);
206
struct gatt_db *gatt_db_new(void)
210
db = new0(struct gatt_db, 1);
211
db->services = queue_new();
212
db->notify_list = queue_new();
213
db->next_handle = 0x0001;
215
return gatt_db_ref(db);
218
static void notify_destroy(void *data)
220
struct notify *notify = data;
223
notify->destroy(notify->user_data);
228
static bool match_notify_id(const void *a, const void *b)
230
const struct notify *notify = a;
231
unsigned int id = PTR_TO_UINT(b);
233
return notify->id == id;
237
struct gatt_db_attribute *attr;
241
static void handle_notify(void *data, void *user_data)
243
struct notify *notify = data;
244
struct notify_data *notify_data = user_data;
246
if (notify_data->added)
247
notify->service_added(notify_data->attr, notify->user_data);
249
notify->service_removed(notify_data->attr, notify->user_data);
252
static void notify_service_changed(struct gatt_db *db,
253
struct gatt_db_service *service,
256
struct notify_data data;
258
if (queue_isempty(db->notify_list))
261
data.attr = service->attributes[0];
266
queue_foreach(db->notify_list, handle_notify, &data);
271
static void gatt_db_service_destroy(void *data)
273
struct gatt_db_service *service = data;
277
notify_service_changed(service->db, service, false);
279
for (i = 0; i < service->num_handles; i++)
280
attribute_destroy(service->attributes[i]);
282
free(service->attributes);
286
static void gatt_db_destroy(struct gatt_db *db)
292
* Clear the notify list before clearing the services to prevent the
293
* latter from sending service_removed events.
295
queue_destroy(db->notify_list, notify_destroy);
296
db->notify_list = NULL;
298
queue_destroy(db->services, gatt_db_service_destroy);
302
void gatt_db_unref(struct gatt_db *db)
307
if (__sync_sub_and_fetch(&db->ref_count, 1))
313
bool gatt_db_isempty(struct gatt_db *db)
318
return queue_isempty(db->services);
321
static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst)
325
if (uuid->type == BT_UUID16) {
326
put_le16(uuid->value.u16, dst);
327
return bt_uuid_len(uuid);
330
bt_uuid_to_uuid128(uuid, &uuid128);
331
bswap_128(&uuid128.value.u128, dst);
332
return bt_uuid_len(&uuid128);
335
static bool le_to_uuid(const uint8_t *src, size_t len, bt_uuid_t *uuid)
340
bt_uuid16_create(uuid, get_le16(src));
345
bt_uuid32_create(uuid, get_le32(src));
352
bswap_128(src, &u128);
353
bt_uuid128_create(uuid, u128);
358
static struct gatt_db_service *gatt_db_service_create(const bt_uuid_t *uuid,
361
uint16_t num_handles)
363
struct gatt_db_service *service;
364
const bt_uuid_t *type;
371
service = new0(struct gatt_db_service, 1);
372
service->attributes = new0(struct gatt_db_attribute *, num_handles);
375
type = &primary_service_uuid;
377
type = &secondary_service_uuid;
379
len = uuid_to_le(uuid, value);
381
service->attributes[0] = new_attribute(service, handle, type, value,
383
if (!service->attributes[0]) {
384
gatt_db_service_destroy(service);
392
bool gatt_db_remove_service(struct gatt_db *db,
393
struct gatt_db_attribute *attrib)
395
struct gatt_db_service *service;
400
service = attrib->service;
402
queue_remove(db->services, service);
404
gatt_db_service_destroy(service);
409
bool gatt_db_clear(struct gatt_db *db)
414
queue_remove_all(db->services, NULL, NULL, gatt_db_service_destroy);
421
static void gatt_db_service_get_handles(const struct gatt_db_service *service,
422
uint16_t *start_handle,
423
uint16_t *end_handle)
426
*start_handle = service->attributes[0]->handle;
429
*end_handle = service->attributes[0]->handle +
430
service->num_handles - 1;
437
static bool match_range(const void *a, const void *b)
439
const struct gatt_db_service *service = a;
440
const struct clear_range *range = b;
441
uint16_t svc_start, svc_end;
443
gatt_db_service_get_handles(service, &svc_start, &svc_end);
445
return svc_start <= range->end && svc_end >= range->start;
448
bool gatt_db_clear_range(struct gatt_db *db, uint16_t start_handle,
451
struct clear_range range;
453
if (!db || start_handle > end_handle)
456
range.start = start_handle;
457
range.end = end_handle;
459
queue_remove_all(db->services, match_range, &range,
460
gatt_db_service_destroy);
465
static struct gatt_db_service *find_insert_loc(struct gatt_db *db,
466
uint16_t start, uint16_t end,
467
struct gatt_db_service **after)
469
const struct queue_entry *services_entry;
470
struct gatt_db_service *service;
471
uint16_t cur_start, cur_end;
475
services_entry = queue_get_entries(db->services);
477
while (services_entry) {
478
service = services_entry->data;
480
gatt_db_service_get_handles(service, &cur_start, &cur_end);
482
if (start >= cur_start && start <= cur_end)
485
if (end >= cur_start && end <= cur_end)
492
services_entry = services_entry->next;
498
struct gatt_db_attribute *gatt_db_insert_service(struct gatt_db *db,
500
const bt_uuid_t *uuid,
502
uint16_t num_handles)
504
struct gatt_db_service *service, *after;
508
if (!db || handle < 1)
511
if (num_handles < 1 || (handle + num_handles - 1) > UINT16_MAX)
514
service = find_insert_loc(db, handle, handle + num_handles - 1, &after);
516
const bt_uuid_t *type;
520
type = &primary_service_uuid;
522
type = &secondary_service_uuid;
524
gatt_db_attribute_get_service_uuid(service->attributes[0],
527
/* Check if service match */
528
if (!bt_uuid_cmp(&service->attributes[0]->uuid, type) &&
529
!bt_uuid_cmp(&value, uuid) &&
530
service->num_handles == num_handles &&
531
service->attributes[0]->handle == handle)
532
return service->attributes[0];
537
service = gatt_db_service_create(uuid, handle, primary, num_handles);
543
if (!queue_push_after(db->services, after, service))
545
} else if (!queue_push_head(db->services, service)) {
550
service->attributes[0]->handle = handle;
551
service->num_handles = num_handles;
553
/* Fast-forward next_handle if the new service was added to the end */
554
db->next_handle = MAX(handle + num_handles, db->next_handle);
556
return service->attributes[0];
559
gatt_db_service_destroy(service);
563
struct gatt_db_attribute *gatt_db_add_service(struct gatt_db *db,
564
const bt_uuid_t *uuid,
566
uint16_t num_handles)
568
return gatt_db_insert_service(db, db->next_handle, uuid, primary,
572
unsigned int gatt_db_register(struct gatt_db *db,
573
gatt_db_attribute_cb_t service_added,
574
gatt_db_attribute_cb_t service_removed,
576
gatt_db_destroy_func_t destroy)
578
struct notify *notify;
580
if (!db || !(service_added || service_removed))
583
notify = new0(struct notify, 1);
584
notify->service_added = service_added;
585
notify->service_removed = service_removed;
586
notify->destroy = destroy;
587
notify->user_data = user_data;
589
if (db->next_notify_id < 1)
590
db->next_notify_id = 1;
592
notify->id = db->next_notify_id++;
594
if (!queue_push_tail(db->notify_list, notify)) {
602
bool gatt_db_unregister(struct gatt_db *db, unsigned int id)
604
struct notify *notify;
609
notify = queue_find(db->notify_list, match_notify_id, UINT_TO_PTR(id));
613
queue_remove(db->notify_list, notify);
614
notify_destroy(notify);
619
static uint16_t get_attribute_index(struct gatt_db_service *service,
624
/* Here we look for first free attribute index with given offset */
625
while (i < (service->num_handles - end_offset) &&
626
service->attributes[i])
629
return i == (service->num_handles - end_offset) ? 0 : i;
632
static uint16_t get_handle_at_index(struct gatt_db_service *service,
635
return service->attributes[index]->handle;
638
static struct gatt_db_attribute *
639
attribute_update(struct gatt_db_service *service, int index)
641
uint16_t previous_handle;
643
/* We call this function with index > 0, because index 0 is reserved
644
* for service declaration, and is set in add_service()
646
previous_handle = service->attributes[index - 1]->handle;
647
service->attributes[index]->handle = previous_handle + 1;
649
return service->attributes[index];
652
static void set_attribute_data(struct gatt_db_attribute *attribute,
653
gatt_db_read_t read_func,
654
gatt_db_write_t write_func,
655
uint32_t permissions,
658
attribute->permissions = permissions;
659
attribute->read_func = read_func;
660
attribute->write_func = write_func;
661
attribute->user_data = user_data;
664
static struct gatt_db_attribute *
665
service_insert_characteristic(struct gatt_db_service *service,
667
const bt_uuid_t *uuid,
668
uint32_t permissions,
670
gatt_db_read_t read_func,
671
gatt_db_write_t write_func,
674
uint8_t value[MAX_CHAR_DECL_VALUE_LEN];
678
/* Check if handle is in within service range */
679
if (handle && handle <= service->attributes[0]->handle)
683
* It is not possible to allocate last handle for a Characteristic
684
* since it would not have space for its value:
685
* 3.3.2 Characteristic Value Declaration
686
* The Characteristic Value declaration contains the value of the
687
* characteristic. It is the first Attribute after the characteristic
688
* declaration. All characteristic definitions shall have a
689
* Characteristic Value declaration.
691
if (handle == UINT16_MAX)
694
i = get_attribute_index(service, 1);
699
handle = get_handle_at_index(service, i - 1) + 2;
701
value[0] = properties;
702
len += sizeof(properties);
704
/* We set handle of characteristic value, which will be added next */
705
put_le16(handle, &value[1]);
706
len += sizeof(uint16_t);
707
len += uuid_to_le(uuid, &value[3]);
709
service->attributes[i] = new_attribute(service, handle - 1,
710
&characteristic_uuid,
712
if (!service->attributes[i])
717
service->attributes[i] = new_attribute(service, handle, uuid, NULL, 0);
718
if (!service->attributes[i]) {
719
free(service->attributes[i - 1]);
723
set_attribute_data(service->attributes[i], read_func, write_func,
724
permissions, user_data);
726
return service->attributes[i];
729
struct gatt_db_attribute *
730
gatt_db_service_insert_characteristic(struct gatt_db_attribute *attrib,
732
const bt_uuid_t *uuid,
733
uint32_t permissions,
735
gatt_db_read_t read_func,
736
gatt_db_write_t write_func,
739
if (!attrib || !handle)
742
return service_insert_characteristic(attrib->service, handle, uuid,
743
permissions, properties,
744
read_func, write_func,
748
struct gatt_db_attribute *
749
gatt_db_service_add_characteristic(struct gatt_db_attribute *attrib,
750
const bt_uuid_t *uuid,
751
uint32_t permissions,
753
gatt_db_read_t read_func,
754
gatt_db_write_t write_func,
760
return service_insert_characteristic(attrib->service, 0, uuid,
761
permissions, properties,
762
read_func, write_func,
766
static struct gatt_db_attribute *
767
service_insert_descriptor(struct gatt_db_service *service,
769
const bt_uuid_t *uuid,
770
uint32_t permissions,
771
gatt_db_read_t read_func,
772
gatt_db_write_t write_func,
777
i = get_attribute_index(service, 0);
781
/* Check if handle is in within service range */
782
if (handle && handle <= service->attributes[0]->handle)
786
handle = get_handle_at_index(service, i - 1) + 1;
788
service->attributes[i] = new_attribute(service, handle, uuid, NULL, 0);
789
if (!service->attributes[i])
792
set_attribute_data(service->attributes[i], read_func, write_func,
793
permissions, user_data);
795
return service->attributes[i];
798
struct gatt_db_attribute *
799
gatt_db_service_insert_descriptor(struct gatt_db_attribute *attrib,
801
const bt_uuid_t *uuid,
802
uint32_t permissions,
803
gatt_db_read_t read_func,
804
gatt_db_write_t write_func,
807
if (!attrib || !handle)
810
return service_insert_descriptor(attrib->service, handle, uuid,
811
permissions, read_func, write_func,
815
struct gatt_db_attribute *
816
gatt_db_service_add_descriptor(struct gatt_db_attribute *attrib,
817
const bt_uuid_t *uuid,
818
uint32_t permissions,
819
gatt_db_read_t read_func,
820
gatt_db_write_t write_func,
826
return service_insert_descriptor(attrib->service, 0, uuid,
827
permissions, read_func, write_func,
831
struct gatt_db_attribute *
832
gatt_db_service_add_included(struct gatt_db_attribute *attrib,
833
struct gatt_db_attribute *include)
835
struct gatt_db_service *service, *included;
836
uint8_t value[MAX_INCLUDED_VALUE_LEN];
837
uint16_t included_handle, len = 0;
840
if (!attrib || !include)
843
service = attrib->service;
844
included = include->service;
846
/* Adjust include to point to the first attribute */
847
if (include != included->attributes[0])
848
include = included->attributes[0];
850
included_handle = include->handle;
852
put_le16(included_handle, &value[len]);
853
len += sizeof(uint16_t);
855
put_le16(included_handle + included->num_handles - 1, &value[len]);
856
len += sizeof(uint16_t);
858
/* The Service UUID shall only be present when the UUID is a 16-bit
859
* Bluetooth UUID. Vol 2. Part G. 3.2
861
if (include->value_len == sizeof(uint16_t)) {
862
memcpy(&value[len], include->value, include->value_len);
863
len += include->value_len;
866
index = get_attribute_index(service, 0);
870
service->attributes[index] = new_attribute(service, 0,
871
&included_service_uuid,
873
if (!service->attributes[index])
876
/* The Attribute Permissions shall be read only and not require
877
* authentication or authorization. Vol 2. Part G. 3.2
879
* TODO handle permissions
881
set_attribute_data(service->attributes[index], NULL, NULL, 0, NULL);
883
return attribute_update(service, index);
886
bool gatt_db_service_set_active(struct gatt_db_attribute *attrib, bool active)
888
struct gatt_db_service *service;
893
service = attrib->service;
895
if (service->active == active)
898
service->active = active;
900
notify_service_changed(service->db, service, active);
905
bool gatt_db_service_get_active(struct gatt_db_attribute *attrib)
910
return attrib->service->active;
913
bool gatt_db_service_set_claimed(struct gatt_db_attribute *attrib,
919
attrib->service->claimed = claimed;
924
bool gatt_db_service_get_claimed(struct gatt_db_attribute *attrib)
929
return attrib->service->claimed;
932
void gatt_db_read_by_group_type(struct gatt_db *db, uint16_t start_handle,
934
const bt_uuid_t type,
937
const struct queue_entry *services_entry;
938
struct gatt_db_service *service;
939
uint16_t grp_start, grp_end, uuid_size;
943
services_entry = queue_get_entries(db->services);
945
while (services_entry) {
946
service = services_entry->data;
948
if (!service->active)
951
if (bt_uuid_cmp(&type, &service->attributes[0]->uuid))
954
grp_start = service->attributes[0]->handle;
955
grp_end = grp_start + service->num_handles - 1;
957
if (grp_end < start_handle || grp_start > end_handle)
960
if (grp_start < start_handle || grp_start > end_handle)
964
uuid_size = service->attributes[0]->value_len;
965
else if (uuid_size != service->attributes[0]->value_len)
968
queue_push_tail(queue, service->attributes[0]);
971
services_entry = services_entry->next;
975
struct find_by_type_value_data {
977
uint16_t start_handle;
979
gatt_db_attribute_cb_t func;
983
unsigned int num_of_res;
986
static void find_by_type(void *data, void *user_data)
988
struct find_by_type_value_data *search_data = user_data;
989
struct gatt_db_service *service = data;
990
struct gatt_db_attribute *attribute;
993
if (!service->active)
996
for (i = 0; i < service->num_handles; i++) {
997
attribute = service->attributes[i];
1002
if ((attribute->handle < search_data->start_handle) ||
1003
(attribute->handle > search_data->end_handle))
1006
if (bt_uuid_cmp(&search_data->uuid, &attribute->uuid))
1009
/* TODO: fix for read-callback based attributes */
1010
if (search_data->value && memcmp(attribute->value,
1012
search_data->value_len))
1015
search_data->num_of_res++;
1016
search_data->func(attribute, search_data->user_data);
1020
unsigned int gatt_db_find_by_type(struct gatt_db *db, uint16_t start_handle,
1021
uint16_t end_handle,
1022
const bt_uuid_t *type,
1023
gatt_db_attribute_cb_t func,
1026
struct find_by_type_value_data data;
1028
memset(&data, 0, sizeof(data));
1031
data.start_handle = start_handle;
1032
data.end_handle = end_handle;
1034
data.user_data = user_data;
1036
queue_foreach(db->services, find_by_type, &data);
1038
return data.num_of_res;
1041
unsigned int gatt_db_find_by_type_value(struct gatt_db *db,
1042
uint16_t start_handle,
1043
uint16_t end_handle,
1044
const bt_uuid_t *type,
1047
gatt_db_attribute_cb_t func,
1050
struct find_by_type_value_data data;
1053
data.start_handle = start_handle;
1054
data.end_handle = end_handle;
1056
data.user_data = user_data;
1058
data.value_len = value_len;
1060
queue_foreach(db->services, find_by_type, &data);
1062
return data.num_of_res;
1065
struct read_by_type_data {
1066
struct queue *queue;
1068
uint16_t start_handle;
1069
uint16_t end_handle;
1072
static void read_by_type(void *data, void *user_data)
1074
struct read_by_type_data *search_data = user_data;
1075
struct gatt_db_service *service = data;
1076
struct gatt_db_attribute *attribute;
1079
if (!service->active)
1082
for (i = 0; i < service->num_handles; i++) {
1083
attribute = service->attributes[i];
1087
if (attribute->handle < search_data->start_handle)
1090
if (attribute->handle > search_data->end_handle)
1093
if (bt_uuid_cmp(&search_data->uuid, &attribute->uuid))
1096
queue_push_tail(search_data->queue, attribute);
1100
void gatt_db_read_by_type(struct gatt_db *db, uint16_t start_handle,
1101
uint16_t end_handle,
1102
const bt_uuid_t type,
1103
struct queue *queue)
1105
struct read_by_type_data data;
1107
data.start_handle = start_handle;
1108
data.end_handle = end_handle;
1111
queue_foreach(db->services, read_by_type, &data);
1115
struct find_information_data {
1116
struct queue *queue;
1117
uint16_t start_handle;
1118
uint16_t end_handle;
1121
static void find_information(void *data, void *user_data)
1123
struct find_information_data *search_data = user_data;
1124
struct gatt_db_service *service = data;
1125
struct gatt_db_attribute *attribute;
1128
if (!service->active)
1131
/* Check if service is in range */
1132
if ((service->attributes[0]->handle + service->num_handles - 1) <
1133
search_data->start_handle)
1136
for (i = 0; i < service->num_handles; i++) {
1137
attribute = service->attributes[i];
1141
if (attribute->handle < search_data->start_handle)
1144
if (attribute->handle > search_data->end_handle)
1147
queue_push_tail(search_data->queue, attribute);
1151
void gatt_db_find_information(struct gatt_db *db, uint16_t start_handle,
1152
uint16_t end_handle,
1153
struct queue *queue)
1155
struct find_information_data data;
1157
data.start_handle = start_handle;
1158
data.end_handle = end_handle;
1161
queue_foreach(db->services, find_information, &data);
1164
void gatt_db_foreach_service(struct gatt_db *db, const bt_uuid_t *uuid,
1165
gatt_db_attribute_cb_t func,
1168
gatt_db_foreach_service_in_range(db, uuid, func, user_data, 0x0001,
1172
struct foreach_data {
1173
gatt_db_attribute_cb_t func;
1174
const bt_uuid_t *uuid;
1176
uint16_t start, end;
1179
static void foreach_service_in_range(void *data, void *user_data)
1181
struct gatt_db_service *service = data;
1182
struct foreach_data *foreach_data = user_data;
1186
svc_start = get_handle_at_index(service, 0);
1188
if (svc_start > foreach_data->end || svc_start < foreach_data->start)
1191
if (foreach_data->uuid) {
1192
gatt_db_attribute_get_service_uuid(service->attributes[0],
1194
if (bt_uuid_cmp(&uuid, foreach_data->uuid))
1198
foreach_data->func(service->attributes[0], foreach_data->user_data);
1201
void gatt_db_foreach_service_in_range(struct gatt_db *db,
1202
const bt_uuid_t *uuid,
1203
gatt_db_attribute_cb_t func,
1205
uint16_t start_handle,
1206
uint16_t end_handle)
1208
struct foreach_data data;
1210
if (!db || !func || start_handle > end_handle)
1215
data.user_data = user_data;
1216
data.start = start_handle;
1217
data.end = end_handle;
1219
queue_foreach(db->services, foreach_service_in_range, &data);
1222
void gatt_db_service_foreach(struct gatt_db_attribute *attrib,
1223
const bt_uuid_t *uuid,
1224
gatt_db_attribute_cb_t func,
1227
struct gatt_db_service *service;
1228
struct gatt_db_attribute *attr;
1231
if (!attrib || !func)
1234
service = attrib->service;
1236
for (i = 0; i < service->num_handles; i++) {
1237
attr = service->attributes[i];
1241
if (uuid && bt_uuid_cmp(uuid, &attr->uuid))
1244
func(attr, user_data);
1248
void gatt_db_service_foreach_char(struct gatt_db_attribute *attrib,
1249
gatt_db_attribute_cb_t func,
1252
gatt_db_service_foreach(attrib, &characteristic_uuid, func, user_data);
1255
void gatt_db_service_foreach_desc(struct gatt_db_attribute *attrib,
1256
gatt_db_attribute_cb_t func,
1259
struct gatt_db_service *service;
1260
struct gatt_db_attribute *attr;
1263
if (!attrib || !func)
1266
/* Return if this attribute is not a characteristic declaration */
1267
if (bt_uuid_cmp(&characteristic_uuid, &attrib->uuid))
1270
service = attrib->service;
1272
/* Start from the attribute following the value handle */
1273
for (i = 0; i < service->num_handles; i++) {
1274
if (service->attributes[i] == attrib) {
1280
for (; i < service->num_handles; i++) {
1281
attr = service->attributes[i];
1285
/* Return if we reached the end of this characteristic */
1286
if (!bt_uuid_cmp(&characteristic_uuid, &attr->uuid) ||
1287
!bt_uuid_cmp(&included_service_uuid, &attr->uuid))
1290
func(attr, user_data);
1294
void gatt_db_service_foreach_incl(struct gatt_db_attribute *attrib,
1295
gatt_db_attribute_cb_t func,
1298
gatt_db_service_foreach(attrib, &included_service_uuid, func,
1302
static bool find_service_for_handle(const void *data, const void *user_data)
1304
const struct gatt_db_service *service = data;
1305
uint16_t handle = PTR_TO_UINT(user_data);
1306
uint16_t start, end;
1308
gatt_db_service_get_handles(service, &start, &end);
1310
return (start <= handle) && (handle <= end);
1313
struct gatt_db_attribute *gatt_db_get_attribute(struct gatt_db *db,
1316
struct gatt_db_service *service;
1322
service = queue_find(db->services, find_service_for_handle,
1323
UINT_TO_PTR(handle));
1327
for (i = 0; i < service->num_handles; i++) {
1328
if (!service->attributes[i])
1331
if (service->attributes[i]->handle == handle)
1332
return service->attributes[i];
1338
static bool find_service_with_uuid(const void *data, const void *user_data)
1340
const struct gatt_db_service *service = data;
1341
const bt_uuid_t *uuid = user_data;
1344
gatt_db_attribute_get_service_uuid(service->attributes[0], &svc_uuid);
1346
return bt_uuid_cmp(uuid, &svc_uuid) == 0;
1349
struct gatt_db_attribute *gatt_db_get_service_with_uuid(struct gatt_db *db,
1350
const bt_uuid_t *uuid)
1352
struct gatt_db_service *service;
1357
service = queue_find(db->services, find_service_with_uuid, uuid);
1361
return service->attributes[0];
1364
const bt_uuid_t *gatt_db_attribute_get_type(
1365
const struct gatt_db_attribute *attrib)
1370
return &attrib->uuid;
1373
uint16_t gatt_db_attribute_get_handle(const struct gatt_db_attribute *attrib)
1378
return attrib->handle;
1381
bool gatt_db_attribute_get_service_uuid(const struct gatt_db_attribute *attrib,
1384
struct gatt_db_service *service;
1386
if (!attrib || !uuid)
1389
service = attrib->service;
1391
if (service->attributes[0]->value_len == sizeof(uint16_t)) {
1394
value = get_le16(service->attributes[0]->value);
1395
bt_uuid16_create(uuid, value);
1400
if (service->attributes[0]->value_len == sizeof(uint128_t)) {
1403
bswap_128(service->attributes[0]->value, &value);
1404
bt_uuid128_create(uuid, value);
1412
bool gatt_db_attribute_get_service_handles(
1413
const struct gatt_db_attribute *attrib,
1414
uint16_t *start_handle,
1415
uint16_t *end_handle)
1417
struct gatt_db_service *service;
1422
service = attrib->service;
1424
gatt_db_service_get_handles(service, start_handle, end_handle);
1429
bool gatt_db_attribute_get_service_data(const struct gatt_db_attribute *attrib,
1430
uint16_t *start_handle,
1431
uint16_t *end_handle,
1435
struct gatt_db_service *service;
1436
struct gatt_db_attribute *decl;
1441
service = attrib->service;
1442
decl = service->attributes[0];
1444
gatt_db_service_get_handles(service, start_handle, end_handle);
1447
*primary = bt_uuid_cmp(&decl->uuid, &secondary_service_uuid);
1453
* The service declaration attribute value is the 16 or 128 bit service
1456
return le_to_uuid(decl->value, decl->value_len, uuid);
1459
bool gatt_db_attribute_get_char_data(const struct gatt_db_attribute *attrib,
1461
uint16_t *value_handle,
1462
uint8_t *properties,
1468
if (bt_uuid_cmp(&characteristic_uuid, &attrib->uuid))
1472
* Characteristic declaration value:
1473
* 1 octet: Characteristic properties
1474
* 2 octets: Characteristic value handle
1475
* 2 or 16 octets: characteristic UUID
1477
if (!attrib->value || (attrib->value_len != 5 &&
1478
attrib->value_len != 19))
1482
*handle = attrib->handle;
1485
*properties = attrib->value[0];
1488
*value_handle = get_le16(attrib->value + 1);
1493
return le_to_uuid(attrib->value + 3, attrib->value_len - 3, uuid);
1496
bool gatt_db_attribute_get_incl_data(const struct gatt_db_attribute *attrib,
1498
uint16_t *start_handle,
1499
uint16_t *end_handle)
1504
if (bt_uuid_cmp(&included_service_uuid, &attrib->uuid))
1508
* Include definition value:
1509
* 2 octets: start handle of included service
1510
* 2 octets: end handle of included service
1511
* optional 2 octets: 16-bit Bluetooth UUID
1513
if (!attrib->value || attrib->value_len < 4 || attrib->value_len > 6)
1517
* We only return the handles since the UUID can be easily obtained
1518
* from the corresponding attribute.
1521
*handle = attrib->handle;
1524
*start_handle = get_le16(attrib->value);
1527
*end_handle = get_le16(attrib->value + 2);
1533
gatt_db_attribute_get_permissions(const struct gatt_db_attribute *attrib)
1538
return attrib->permissions;
1541
static bool read_timeout(void *user_data)
1543
struct pending_read *p = user_data;
1547
queue_remove(p->attrib->pending_reads, p);
1549
pending_read_result(p, -ETIMEDOUT, NULL, 0);
1554
bool gatt_db_attribute_read(struct gatt_db_attribute *attrib, uint16_t offset,
1555
uint8_t opcode, struct bt_att *att,
1556
gatt_db_attribute_read_t func, void *user_data)
1560
if (!attrib || !func)
1563
if (attrib->read_func) {
1564
struct pending_read *p;
1566
p = new0(struct pending_read, 1);
1568
p->id = ++attrib->read_id;
1569
p->timeout_id = timeout_add(ATTRIBUTE_TIMEOUT, read_timeout,
1572
p->user_data = user_data;
1574
queue_push_tail(attrib->pending_reads, p);
1576
attrib->read_func(attrib, p->id, offset, opcode, att,
1581
/* Check boundary if value is stored in the db */
1582
if (offset > attrib->value_len) {
1583
func(attrib, BT_ATT_ERROR_INVALID_OFFSET, NULL, 0, user_data);
1587
/* Guard against invalid access if offset equals to value length */
1588
value = offset == attrib->value_len ? NULL : &attrib->value[offset];
1590
func(attrib, 0, value, attrib->value_len - offset, user_data);
1595
static bool find_pending(const void *a, const void *b)
1597
const struct pending_read *p = a;
1598
unsigned int id = PTR_TO_UINT(b);
1603
bool gatt_db_attribute_read_result(struct gatt_db_attribute *attrib,
1604
unsigned int id, int err,
1605
const uint8_t *value, size_t length)
1607
struct pending_read *p;
1612
p = queue_remove_if(attrib->pending_reads, find_pending,
1617
pending_read_result(p, err, value, length);
1622
static bool write_timeout(void *user_data)
1624
struct pending_write *p = user_data;
1628
queue_remove(p->attrib->pending_writes, p);
1630
pending_write_result(p, -ETIMEDOUT);
1635
bool gatt_db_attribute_write(struct gatt_db_attribute *attrib, uint16_t offset,
1636
const uint8_t *value, size_t len,
1637
uint8_t opcode, struct bt_att *att,
1638
gatt_db_attribute_write_t func,
1641
if (!attrib || !func)
1644
if (attrib->write_func) {
1645
struct pending_write *p;
1647
p = new0(struct pending_write, 1);
1649
p->id = ++attrib->write_id;
1650
p->timeout_id = timeout_add(ATTRIBUTE_TIMEOUT, write_timeout,
1653
p->user_data = user_data;
1655
queue_push_tail(attrib->pending_writes, p);
1657
attrib->write_func(attrib, p->id, offset, value, len, opcode,
1658
att, attrib->user_data);
1662
/* Nothing to write just skip */
1666
/* For values stored in db allocate on demand */
1667
if (!attrib->value || offset >= attrib->value_len ||
1668
len > (unsigned) (attrib->value_len - offset)) {
1671
buf = realloc(attrib->value, len + offset);
1675
attrib->value = buf;
1677
/* Init data in the first allocation */
1678
if (!attrib->value_len)
1679
memset(attrib->value, 0, offset);
1681
attrib->value_len = len + offset;
1684
memcpy(&attrib->value[offset], value, len);
1687
func(attrib, 0, user_data);
1692
bool gatt_db_attribute_write_result(struct gatt_db_attribute *attrib,
1693
unsigned int id, int err)
1695
struct pending_write *p;
1700
p = queue_remove_if(attrib->pending_writes, find_pending,
1705
pending_write_result(p, err);
1710
bool gatt_db_attribute_reset(struct gatt_db_attribute *attrib)
1715
if (!attrib->value || !attrib->value_len)
1718
free(attrib->value);
1719
attrib->value = NULL;
1720
attrib->value_len = 0;