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;
96 #ifdef __TIZEN_PATCH__
98 bdaddr_t unicast_addr;
101 gatt_db_read_t read_func;
102 gatt_db_write_t write_func;
105 unsigned int read_id;
106 struct queue *pending_reads;
108 unsigned int write_id;
109 struct queue *pending_writes;
112 struct gatt_db_service {
116 uint16_t num_handles;
117 struct gatt_db_attribute **attributes;
120 static void pending_read_result(struct pending_read *p, int err,
121 const uint8_t *data, size_t length)
123 if (p->timeout_id > 0)
124 timeout_remove(p->timeout_id);
126 p->func(p->attrib, err, data, length, p->user_data);
131 static void pending_read_free(void *data)
133 struct pending_read *p = data;
135 pending_read_result(p, -ECANCELED, NULL, 0);
138 static void pending_write_result(struct pending_write *p, int err)
140 if (p->timeout_id > 0)
141 timeout_remove(p->timeout_id);
143 p->func(p->attrib, err, p->user_data);
148 static void pending_write_free(void *data)
150 struct pending_write *p = data;
152 pending_write_result(p, -ECANCELED);
155 static void attribute_destroy(struct gatt_db_attribute *attribute)
157 /* Attribute was not initialized by user */
161 queue_destroy(attribute->pending_reads, pending_read_free);
162 queue_destroy(attribute->pending_writes, pending_write_free);
164 free(attribute->value);
168 static struct gatt_db_attribute *new_attribute(struct gatt_db_service *service,
170 const bt_uuid_t *type,
174 struct gatt_db_attribute *attribute;
176 attribute = new0(struct gatt_db_attribute, 1);
178 attribute->service = service;
179 attribute->handle = handle;
180 attribute->uuid = *type;
181 attribute->value_len = len;
183 attribute->value = malloc0(len);
184 if (!attribute->value)
187 memcpy(attribute->value, val, len);
190 attribute->pending_reads = queue_new();
191 attribute->pending_writes = queue_new();
196 attribute_destroy(attribute);
200 struct gatt_db *gatt_db_ref(struct gatt_db *db)
205 __sync_fetch_and_add(&db->ref_count, 1);
210 struct gatt_db *gatt_db_new(void)
214 db = new0(struct gatt_db, 1);
215 db->services = queue_new();
216 db->notify_list = queue_new();
217 db->next_handle = 0x0001;
219 return gatt_db_ref(db);
222 static void notify_destroy(void *data)
224 struct notify *notify = data;
227 notify->destroy(notify->user_data);
232 static bool match_notify_id(const void *a, const void *b)
234 const struct notify *notify = a;
235 unsigned int id = PTR_TO_UINT(b);
237 return notify->id == id;
241 struct gatt_db_attribute *attr;
245 static void handle_notify(void *data, void *user_data)
247 struct notify *notify = data;
248 struct notify_data *notify_data = user_data;
250 if (notify_data->added)
251 notify->service_added(notify_data->attr, notify->user_data);
253 notify->service_removed(notify_data->attr, notify->user_data);
256 static void notify_service_changed(struct gatt_db *db,
257 struct gatt_db_service *service,
260 struct notify_data data;
262 if (queue_isempty(db->notify_list))
265 data.attr = service->attributes[0];
270 queue_foreach(db->notify_list, handle_notify, &data);
275 static void gatt_db_service_destroy(void *data)
277 struct gatt_db_service *service = data;
281 notify_service_changed(service->db, service, false);
283 for (i = 0; i < service->num_handles; i++)
284 attribute_destroy(service->attributes[i]);
286 free(service->attributes);
290 static void gatt_db_destroy(struct gatt_db *db)
296 * Clear the notify list before clearing the services to prevent the
297 * latter from sending service_removed events.
299 queue_destroy(db->notify_list, notify_destroy);
300 db->notify_list = NULL;
302 queue_destroy(db->services, gatt_db_service_destroy);
306 void gatt_db_unref(struct gatt_db *db)
311 if (__sync_sub_and_fetch(&db->ref_count, 1))
317 bool gatt_db_isempty(struct gatt_db *db)
322 return queue_isempty(db->services);
325 static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst)
329 if (uuid->type == BT_UUID16) {
330 put_le16(uuid->value.u16, dst);
331 return bt_uuid_len(uuid);
334 bt_uuid_to_uuid128(uuid, &uuid128);
335 bswap_128(&uuid128.value.u128, dst);
336 return bt_uuid_len(&uuid128);
339 static bool le_to_uuid(const uint8_t *src, size_t len, bt_uuid_t *uuid)
344 bt_uuid16_create(uuid, get_le16(src));
349 bt_uuid32_create(uuid, get_le32(src));
356 bswap_128(src, &u128);
357 bt_uuid128_create(uuid, u128);
362 static struct gatt_db_service *gatt_db_service_create(const bt_uuid_t *uuid,
365 uint16_t num_handles)
367 struct gatt_db_service *service;
368 const bt_uuid_t *type;
375 service = new0(struct gatt_db_service, 1);
376 service->attributes = new0(struct gatt_db_attribute *, num_handles);
379 type = &primary_service_uuid;
381 type = &secondary_service_uuid;
383 len = uuid_to_le(uuid, value);
385 service->attributes[0] = new_attribute(service, handle, type, value,
387 if (!service->attributes[0]) {
388 gatt_db_service_destroy(service);
396 bool gatt_db_remove_service(struct gatt_db *db,
397 struct gatt_db_attribute *attrib)
399 struct gatt_db_service *service;
404 service = attrib->service;
406 queue_remove(db->services, service);
408 gatt_db_service_destroy(service);
413 bool gatt_db_clear(struct gatt_db *db)
418 queue_remove_all(db->services, NULL, NULL, gatt_db_service_destroy);
425 static void gatt_db_service_get_handles(const struct gatt_db_service *service,
426 uint16_t *start_handle,
427 uint16_t *end_handle)
430 *start_handle = service->attributes[0]->handle;
433 *end_handle = service->attributes[0]->handle +
434 service->num_handles - 1;
441 static bool match_range(const void *a, const void *b)
443 const struct gatt_db_service *service = a;
444 const struct clear_range *range = b;
445 uint16_t svc_start, svc_end;
447 gatt_db_service_get_handles(service, &svc_start, &svc_end);
449 return svc_start <= range->end && svc_end >= range->start;
452 bool gatt_db_clear_range(struct gatt_db *db, uint16_t start_handle,
455 struct clear_range range;
457 if (!db || start_handle > end_handle)
460 range.start = start_handle;
461 range.end = end_handle;
463 queue_remove_all(db->services, match_range, &range,
464 gatt_db_service_destroy);
469 static struct gatt_db_service *find_insert_loc(struct gatt_db *db,
470 uint16_t start, uint16_t end,
471 struct gatt_db_service **after)
473 const struct queue_entry *services_entry;
474 struct gatt_db_service *service;
475 uint16_t cur_start, cur_end;
479 services_entry = queue_get_entries(db->services);
481 while (services_entry) {
482 service = services_entry->data;
484 gatt_db_service_get_handles(service, &cur_start, &cur_end);
486 if (start >= cur_start && start <= cur_end)
489 if (end >= cur_start && end <= cur_end)
496 services_entry = services_entry->next;
502 struct gatt_db_attribute *gatt_db_insert_service(struct gatt_db *db,
504 const bt_uuid_t *uuid,
506 uint16_t num_handles)
508 struct gatt_db_service *service, *after;
512 if (!db || handle < 1)
515 if (num_handles < 1 || (handle + num_handles - 1) > UINT16_MAX)
518 service = find_insert_loc(db, handle, handle + num_handles - 1, &after);
520 const bt_uuid_t *type;
524 type = &primary_service_uuid;
526 type = &secondary_service_uuid;
528 gatt_db_attribute_get_service_uuid(service->attributes[0],
531 /* Check if service match */
532 if (!bt_uuid_cmp(&service->attributes[0]->uuid, type) &&
533 !bt_uuid_cmp(&value, uuid) &&
534 service->num_handles == num_handles &&
535 service->attributes[0]->handle == handle)
536 return service->attributes[0];
541 service = gatt_db_service_create(uuid, handle, primary, num_handles);
547 if (!queue_push_after(db->services, after, service))
549 } else if (!queue_push_head(db->services, service)) {
554 service->attributes[0]->handle = handle;
555 service->num_handles = num_handles;
557 /* Fast-forward next_handle if the new service was added to the end */
558 db->next_handle = MAX(handle + num_handles, db->next_handle);
560 return service->attributes[0];
563 gatt_db_service_destroy(service);
567 struct gatt_db_attribute *gatt_db_add_service(struct gatt_db *db,
568 const bt_uuid_t *uuid,
570 uint16_t num_handles)
572 return gatt_db_insert_service(db, db->next_handle, uuid, primary,
576 unsigned int gatt_db_register(struct gatt_db *db,
577 gatt_db_attribute_cb_t service_added,
578 gatt_db_attribute_cb_t service_removed,
580 gatt_db_destroy_func_t destroy)
582 struct notify *notify;
584 if (!db || !(service_added || service_removed))
587 notify = new0(struct notify, 1);
588 notify->service_added = service_added;
589 notify->service_removed = service_removed;
590 notify->destroy = destroy;
591 notify->user_data = user_data;
593 if (db->next_notify_id < 1)
594 db->next_notify_id = 1;
596 notify->id = db->next_notify_id++;
598 if (!queue_push_tail(db->notify_list, notify)) {
606 bool gatt_db_unregister(struct gatt_db *db, unsigned int id)
608 struct notify *notify;
613 notify = queue_find(db->notify_list, match_notify_id, UINT_TO_PTR(id));
617 queue_remove(db->notify_list, notify);
618 notify_destroy(notify);
623 static uint16_t get_attribute_index(struct gatt_db_service *service,
628 /* Here we look for first free attribute index with given offset */
629 while (i < (service->num_handles - end_offset) &&
630 service->attributes[i])
633 return i == (service->num_handles - end_offset) ? 0 : i;
636 static uint16_t get_handle_at_index(struct gatt_db_service *service,
639 return service->attributes[index]->handle;
642 static struct gatt_db_attribute *
643 attribute_update(struct gatt_db_service *service, int index)
645 uint16_t previous_handle;
647 /* We call this function with index > 0, because index 0 is reserved
648 * for service declaration, and is set in add_service()
650 previous_handle = service->attributes[index - 1]->handle;
651 service->attributes[index]->handle = previous_handle + 1;
653 return service->attributes[index];
656 static void set_attribute_data(struct gatt_db_attribute *attribute,
657 gatt_db_read_t read_func,
658 gatt_db_write_t write_func,
659 uint32_t permissions,
662 attribute->permissions = permissions;
663 attribute->read_func = read_func;
664 attribute->write_func = write_func;
665 attribute->user_data = user_data;
668 static struct gatt_db_attribute *
669 service_insert_characteristic(struct gatt_db_service *service,
671 const bt_uuid_t *uuid,
672 uint32_t permissions,
674 gatt_db_read_t read_func,
675 gatt_db_write_t write_func,
678 uint8_t value[MAX_CHAR_DECL_VALUE_LEN];
682 /* Check if handle is in within service range */
683 if (handle && handle <= service->attributes[0]->handle)
687 * It is not possible to allocate last handle for a Characteristic
688 * since it would not have space for its value:
689 * 3.3.2 Characteristic Value Declaration
690 * The Characteristic Value declaration contains the value of the
691 * characteristic. It is the first Attribute after the characteristic
692 * declaration. All characteristic definitions shall have a
693 * Characteristic Value declaration.
695 if (handle == UINT16_MAX)
698 i = get_attribute_index(service, 1);
703 handle = get_handle_at_index(service, i - 1) + 2;
705 value[0] = properties;
706 len += sizeof(properties);
708 /* We set handle of characteristic value, which will be added next */
709 put_le16(handle, &value[1]);
710 len += sizeof(uint16_t);
711 len += uuid_to_le(uuid, &value[3]);
713 service->attributes[i] = new_attribute(service, handle - 1,
714 &characteristic_uuid,
716 if (!service->attributes[i])
721 service->attributes[i] = new_attribute(service, handle, uuid, NULL, 0);
722 if (!service->attributes[i]) {
723 free(service->attributes[i - 1]);
724 #ifdef __TIZEN_PATCH__
725 service->attributes[i - 1] = NULL;
730 set_attribute_data(service->attributes[i], read_func, write_func,
731 permissions, user_data);
733 return service->attributes[i];
736 struct gatt_db_attribute *
737 gatt_db_service_insert_characteristic(struct gatt_db_attribute *attrib,
739 const bt_uuid_t *uuid,
740 uint32_t permissions,
742 gatt_db_read_t read_func,
743 gatt_db_write_t write_func,
746 if (!attrib || !handle)
749 return service_insert_characteristic(attrib->service, handle, uuid,
750 permissions, properties,
751 read_func, write_func,
755 struct gatt_db_attribute *
756 gatt_db_service_add_characteristic(struct gatt_db_attribute *attrib,
757 const bt_uuid_t *uuid,
758 uint32_t permissions,
760 gatt_db_read_t read_func,
761 gatt_db_write_t write_func,
767 return service_insert_characteristic(attrib->service, 0, uuid,
768 permissions, properties,
769 read_func, write_func,
773 static struct gatt_db_attribute *
774 service_insert_descriptor(struct gatt_db_service *service,
776 const bt_uuid_t *uuid,
777 uint32_t permissions,
778 gatt_db_read_t read_func,
779 gatt_db_write_t write_func,
784 i = get_attribute_index(service, 0);
788 /* Check if handle is in within service range */
789 if (handle && handle <= service->attributes[0]->handle)
793 handle = get_handle_at_index(service, i - 1) + 1;
795 service->attributes[i] = new_attribute(service, handle, uuid, NULL, 0);
796 if (!service->attributes[i])
799 set_attribute_data(service->attributes[i], read_func, write_func,
800 permissions, user_data);
802 return service->attributes[i];
805 struct gatt_db_attribute *
806 gatt_db_service_insert_descriptor(struct gatt_db_attribute *attrib,
808 const bt_uuid_t *uuid,
809 uint32_t permissions,
810 gatt_db_read_t read_func,
811 gatt_db_write_t write_func,
814 if (!attrib || !handle)
817 return service_insert_descriptor(attrib->service, handle, uuid,
818 permissions, read_func, write_func,
822 struct gatt_db_attribute *
823 gatt_db_service_add_descriptor(struct gatt_db_attribute *attrib,
824 const bt_uuid_t *uuid,
825 uint32_t permissions,
826 gatt_db_read_t read_func,
827 gatt_db_write_t write_func,
833 return service_insert_descriptor(attrib->service, 0, uuid,
834 permissions, read_func, write_func,
838 struct gatt_db_attribute *
839 gatt_db_service_add_included(struct gatt_db_attribute *attrib,
840 struct gatt_db_attribute *include)
842 struct gatt_db_service *service, *included;
843 uint8_t value[MAX_INCLUDED_VALUE_LEN];
844 uint16_t included_handle, len = 0;
847 if (!attrib || !include)
850 service = attrib->service;
851 included = include->service;
853 /* Adjust include to point to the first attribute */
854 if (include != included->attributes[0])
855 include = included->attributes[0];
857 included_handle = include->handle;
859 put_le16(included_handle, &value[len]);
860 len += sizeof(uint16_t);
862 put_le16(included_handle + included->num_handles - 1, &value[len]);
863 len += sizeof(uint16_t);
865 /* The Service UUID shall only be present when the UUID is a 16-bit
866 * Bluetooth UUID. Vol 2. Part G. 3.2
868 if (include->value_len == sizeof(uint16_t)) {
869 memcpy(&value[len], include->value, include->value_len);
870 len += include->value_len;
873 index = get_attribute_index(service, 0);
877 service->attributes[index] = new_attribute(service, 0,
878 &included_service_uuid,
880 if (!service->attributes[index])
883 /* The Attribute Permissions shall be read only and not require
884 * authentication or authorization. Vol 2. Part G. 3.2
886 * TODO handle permissions
888 set_attribute_data(service->attributes[index], NULL, NULL, 0, NULL);
890 return attribute_update(service, index);
893 bool gatt_db_service_set_active(struct gatt_db_attribute *attrib, bool active)
895 struct gatt_db_service *service;
900 service = attrib->service;
902 if (service->active == active)
905 service->active = active;
907 notify_service_changed(service->db, service, active);
912 bool gatt_db_service_get_active(struct gatt_db_attribute *attrib)
917 return attrib->service->active;
920 bool gatt_db_service_set_claimed(struct gatt_db_attribute *attrib,
926 attrib->service->claimed = claimed;
931 bool gatt_db_service_get_claimed(struct gatt_db_attribute *attrib)
936 return attrib->service->claimed;
939 void gatt_db_read_by_group_type(struct gatt_db *db, uint16_t start_handle,
941 const bt_uuid_t type,
944 const struct queue_entry *services_entry;
945 struct gatt_db_service *service;
946 uint16_t grp_start, grp_end, uuid_size;
950 services_entry = queue_get_entries(db->services);
952 while (services_entry) {
953 service = services_entry->data;
955 if (!service->active)
958 if (bt_uuid_cmp(&type, &service->attributes[0]->uuid))
961 grp_start = service->attributes[0]->handle;
962 grp_end = grp_start + service->num_handles - 1;
964 if (grp_end < start_handle || grp_start > end_handle)
967 if (grp_start < start_handle || grp_start > end_handle)
971 uuid_size = service->attributes[0]->value_len;
972 else if (uuid_size != service->attributes[0]->value_len)
975 queue_push_tail(queue, service->attributes[0]);
978 services_entry = services_entry->next;
982 struct find_by_type_value_data {
984 uint16_t start_handle;
986 gatt_db_attribute_cb_t func;
990 unsigned int num_of_res;
993 static void find_by_type(void *data, void *user_data)
995 struct find_by_type_value_data *search_data = user_data;
996 struct gatt_db_service *service = data;
997 struct gatt_db_attribute *attribute;
1000 if (!service->active)
1003 for (i = 0; i < service->num_handles; i++) {
1004 attribute = service->attributes[i];
1009 if ((attribute->handle < search_data->start_handle) ||
1010 (attribute->handle > search_data->end_handle))
1013 if (bt_uuid_cmp(&search_data->uuid, &attribute->uuid))
1016 /* TODO: fix for read-callback based attributes */
1017 if (search_data->value && memcmp(attribute->value,
1019 search_data->value_len))
1022 search_data->num_of_res++;
1023 search_data->func(attribute, search_data->user_data);
1027 unsigned int gatt_db_find_by_type(struct gatt_db *db, uint16_t start_handle,
1028 uint16_t end_handle,
1029 const bt_uuid_t *type,
1030 gatt_db_attribute_cb_t func,
1033 struct find_by_type_value_data data;
1035 memset(&data, 0, sizeof(data));
1038 data.start_handle = start_handle;
1039 data.end_handle = end_handle;
1041 data.user_data = user_data;
1043 queue_foreach(db->services, find_by_type, &data);
1045 return data.num_of_res;
1048 unsigned int gatt_db_find_by_type_value(struct gatt_db *db,
1049 uint16_t start_handle,
1050 uint16_t end_handle,
1051 const bt_uuid_t *type,
1054 gatt_db_attribute_cb_t func,
1057 struct find_by_type_value_data data;
1060 data.start_handle = start_handle;
1061 data.end_handle = end_handle;
1063 data.user_data = user_data;
1065 data.value_len = value_len;
1067 queue_foreach(db->services, find_by_type, &data);
1069 return data.num_of_res;
1072 struct read_by_type_data {
1073 struct queue *queue;
1075 uint16_t start_handle;
1076 uint16_t end_handle;
1079 static void read_by_type(void *data, void *user_data)
1081 struct read_by_type_data *search_data = user_data;
1082 struct gatt_db_service *service = data;
1083 struct gatt_db_attribute *attribute;
1086 if (!service->active)
1089 for (i = 0; i < service->num_handles; i++) {
1090 attribute = service->attributes[i];
1094 if (attribute->handle < search_data->start_handle)
1097 if (attribute->handle > search_data->end_handle)
1100 if (bt_uuid_cmp(&search_data->uuid, &attribute->uuid))
1103 queue_push_tail(search_data->queue, attribute);
1107 void gatt_db_read_by_type(struct gatt_db *db, uint16_t start_handle,
1108 uint16_t end_handle,
1109 const bt_uuid_t type,
1110 struct queue *queue)
1112 struct read_by_type_data data;
1114 data.start_handle = start_handle;
1115 data.end_handle = end_handle;
1118 queue_foreach(db->services, read_by_type, &data);
1122 struct find_information_data {
1123 struct queue *queue;
1124 uint16_t start_handle;
1125 uint16_t end_handle;
1128 static void find_information(void *data, void *user_data)
1130 struct find_information_data *search_data = user_data;
1131 struct gatt_db_service *service = data;
1132 struct gatt_db_attribute *attribute;
1135 if (!service->active)
1138 /* Check if service is in range */
1139 if ((service->attributes[0]->handle + service->num_handles - 1) <
1140 search_data->start_handle)
1143 for (i = 0; i < service->num_handles; i++) {
1144 attribute = service->attributes[i];
1148 if (attribute->handle < search_data->start_handle)
1151 if (attribute->handle > search_data->end_handle)
1154 queue_push_tail(search_data->queue, attribute);
1158 void gatt_db_find_information(struct gatt_db *db, uint16_t start_handle,
1159 uint16_t end_handle,
1160 struct queue *queue)
1162 struct find_information_data data;
1164 data.start_handle = start_handle;
1165 data.end_handle = end_handle;
1168 queue_foreach(db->services, find_information, &data);
1171 void gatt_db_foreach_service(struct gatt_db *db, const bt_uuid_t *uuid,
1172 gatt_db_attribute_cb_t func,
1175 gatt_db_foreach_service_in_range(db, uuid, func, user_data, 0x0001,
1179 struct foreach_data {
1180 gatt_db_attribute_cb_t func;
1181 const bt_uuid_t *uuid;
1183 uint16_t start, end;
1186 static void foreach_service_in_range(void *data, void *user_data)
1188 struct gatt_db_service *service = data;
1189 struct foreach_data *foreach_data = user_data;
1193 svc_start = get_handle_at_index(service, 0);
1195 if (svc_start > foreach_data->end || svc_start < foreach_data->start)
1198 if (foreach_data->uuid) {
1199 gatt_db_attribute_get_service_uuid(service->attributes[0],
1201 if (bt_uuid_cmp(&uuid, foreach_data->uuid))
1205 foreach_data->func(service->attributes[0], foreach_data->user_data);
1208 void gatt_db_foreach_service_in_range(struct gatt_db *db,
1209 const bt_uuid_t *uuid,
1210 gatt_db_attribute_cb_t func,
1212 uint16_t start_handle,
1213 uint16_t end_handle)
1215 struct foreach_data data;
1217 if (!db || !func || start_handle > end_handle)
1222 data.user_data = user_data;
1223 data.start = start_handle;
1224 data.end = end_handle;
1226 queue_foreach(db->services, foreach_service_in_range, &data);
1229 void gatt_db_service_foreach(struct gatt_db_attribute *attrib,
1230 const bt_uuid_t *uuid,
1231 gatt_db_attribute_cb_t func,
1234 struct gatt_db_service *service;
1235 struct gatt_db_attribute *attr;
1238 if (!attrib || !func)
1241 service = attrib->service;
1243 for (i = 0; i < service->num_handles; i++) {
1244 attr = service->attributes[i];
1248 if (uuid && bt_uuid_cmp(uuid, &attr->uuid))
1251 func(attr, user_data);
1255 void gatt_db_service_foreach_char(struct gatt_db_attribute *attrib,
1256 gatt_db_attribute_cb_t func,
1259 gatt_db_service_foreach(attrib, &characteristic_uuid, func, user_data);
1262 void gatt_db_service_foreach_desc(struct gatt_db_attribute *attrib,
1263 gatt_db_attribute_cb_t func,
1266 struct gatt_db_service *service;
1267 struct gatt_db_attribute *attr;
1270 if (!attrib || !func)
1273 /* Return if this attribute is not a characteristic declaration */
1274 if (bt_uuid_cmp(&characteristic_uuid, &attrib->uuid))
1277 service = attrib->service;
1279 /* Start from the attribute following the value handle */
1280 for (i = 0; i < service->num_handles; i++) {
1281 if (service->attributes[i] == attrib) {
1287 for (; i < service->num_handles; i++) {
1288 attr = service->attributes[i];
1292 /* Return if we reached the end of this characteristic */
1293 if (!bt_uuid_cmp(&characteristic_uuid, &attr->uuid) ||
1294 !bt_uuid_cmp(&included_service_uuid, &attr->uuid))
1297 func(attr, user_data);
1301 void gatt_db_service_foreach_incl(struct gatt_db_attribute *attrib,
1302 gatt_db_attribute_cb_t func,
1305 gatt_db_service_foreach(attrib, &included_service_uuid, func,
1309 static bool find_service_for_handle(const void *data, const void *user_data)
1311 const struct gatt_db_service *service = data;
1312 uint16_t handle = PTR_TO_UINT(user_data);
1313 uint16_t start, end;
1315 gatt_db_service_get_handles(service, &start, &end);
1317 return (start <= handle) && (handle <= end);
1320 struct gatt_db_attribute *gatt_db_get_attribute(struct gatt_db *db,
1323 struct gatt_db_service *service;
1329 service = queue_find(db->services, find_service_for_handle,
1330 UINT_TO_PTR(handle));
1334 for (i = 0; i < service->num_handles; i++) {
1335 if (!service->attributes[i])
1338 if (service->attributes[i]->handle == handle)
1339 return service->attributes[i];
1345 static bool find_service_with_uuid(const void *data, const void *user_data)
1347 const struct gatt_db_service *service = data;
1348 const bt_uuid_t *uuid = user_data;
1351 gatt_db_attribute_get_service_uuid(service->attributes[0], &svc_uuid);
1353 return bt_uuid_cmp(uuid, &svc_uuid) == 0;
1356 struct gatt_db_attribute *gatt_db_get_service_with_uuid(struct gatt_db *db,
1357 const bt_uuid_t *uuid)
1359 struct gatt_db_service *service;
1364 service = queue_find(db->services, find_service_with_uuid, uuid);
1368 return service->attributes[0];
1371 const bt_uuid_t *gatt_db_attribute_get_type(
1372 const struct gatt_db_attribute *attrib)
1377 return &attrib->uuid;
1380 uint16_t gatt_db_attribute_get_handle(const struct gatt_db_attribute *attrib)
1385 return attrib->handle;
1388 bool gatt_db_attribute_get_service_uuid(const struct gatt_db_attribute *attrib,
1391 struct gatt_db_service *service;
1393 if (!attrib || !uuid)
1396 service = attrib->service;
1398 if (service->attributes[0]->value_len == sizeof(uint16_t)) {
1401 value = get_le16(service->attributes[0]->value);
1402 bt_uuid16_create(uuid, value);
1407 if (service->attributes[0]->value_len == sizeof(uint128_t)) {
1410 bswap_128(service->attributes[0]->value, &value);
1411 bt_uuid128_create(uuid, value);
1419 bool gatt_db_attribute_get_service_handles(
1420 const struct gatt_db_attribute *attrib,
1421 uint16_t *start_handle,
1422 uint16_t *end_handle)
1424 struct gatt_db_service *service;
1429 service = attrib->service;
1431 gatt_db_service_get_handles(service, start_handle, end_handle);
1436 bool gatt_db_attribute_get_service_data(const struct gatt_db_attribute *attrib,
1437 uint16_t *start_handle,
1438 uint16_t *end_handle,
1442 struct gatt_db_service *service;
1443 struct gatt_db_attribute *decl;
1448 service = attrib->service;
1449 decl = service->attributes[0];
1451 gatt_db_service_get_handles(service, start_handle, end_handle);
1454 *primary = bt_uuid_cmp(&decl->uuid, &secondary_service_uuid);
1460 * The service declaration attribute value is the 16 or 128 bit service
1463 return le_to_uuid(decl->value, decl->value_len, uuid);
1466 bool gatt_db_attribute_get_char_data(const struct gatt_db_attribute *attrib,
1468 uint16_t *value_handle,
1469 uint8_t *properties,
1475 if (bt_uuid_cmp(&characteristic_uuid, &attrib->uuid))
1479 * Characteristic declaration value:
1480 * 1 octet: Characteristic properties
1481 * 2 octets: Characteristic value handle
1482 * 2 or 16 octets: characteristic UUID
1484 if (!attrib->value || (attrib->value_len != 5 &&
1485 attrib->value_len != 19))
1489 *handle = attrib->handle;
1492 *properties = attrib->value[0];
1495 *value_handle = get_le16(attrib->value + 1);
1500 return le_to_uuid(attrib->value + 3, attrib->value_len - 3, uuid);
1503 bool gatt_db_attribute_get_incl_data(const struct gatt_db_attribute *attrib,
1505 uint16_t *start_handle,
1506 uint16_t *end_handle)
1511 if (bt_uuid_cmp(&included_service_uuid, &attrib->uuid))
1515 * Include definition value:
1516 * 2 octets: start handle of included service
1517 * 2 octets: end handle of included service
1518 * optional 2 octets: 16-bit Bluetooth UUID
1520 if (!attrib->value || attrib->value_len < 4 || attrib->value_len > 6)
1524 * We only return the handles since the UUID can be easily obtained
1525 * from the corresponding attribute.
1528 *handle = attrib->handle;
1531 *start_handle = get_le16(attrib->value);
1534 *end_handle = get_le16(attrib->value + 2);
1540 gatt_db_attribute_get_permissions(const struct gatt_db_attribute *attrib)
1545 return attrib->permissions;
1548 static bool read_timeout(void *user_data)
1550 struct pending_read *p = user_data;
1554 queue_remove(p->attrib->pending_reads, p);
1556 pending_read_result(p, -ETIMEDOUT, NULL, 0);
1561 bool gatt_db_attribute_read(struct gatt_db_attribute *attrib, uint16_t offset,
1562 uint8_t opcode, struct bt_att *att,
1563 gatt_db_attribute_read_t func, void *user_data)
1567 if (!attrib || !func)
1570 if (attrib->read_func) {
1571 struct pending_read *p;
1573 p = new0(struct pending_read, 1);
1575 p->id = ++attrib->read_id;
1576 p->timeout_id = timeout_add(ATTRIBUTE_TIMEOUT, read_timeout,
1579 p->user_data = user_data;
1581 queue_push_tail(attrib->pending_reads, p);
1583 attrib->read_func(attrib, p->id, offset, opcode, att,
1588 /* Check boundary if value is stored in the db */
1589 if (offset > attrib->value_len) {
1590 func(attrib, BT_ATT_ERROR_INVALID_OFFSET, NULL, 0, user_data);
1594 /* Guard against invalid access if offset equals to value length */
1595 value = offset == attrib->value_len ? NULL : &attrib->value[offset];
1597 func(attrib, 0, value, attrib->value_len - offset, user_data);
1602 static bool find_pending(const void *a, const void *b)
1604 const struct pending_read *p = a;
1605 unsigned int id = PTR_TO_UINT(b);
1610 bool gatt_db_attribute_read_result(struct gatt_db_attribute *attrib,
1611 unsigned int id, int err,
1612 const uint8_t *value, size_t length)
1614 struct pending_read *p;
1619 p = queue_remove_if(attrib->pending_reads, find_pending,
1624 pending_read_result(p, err, value, length);
1629 static bool write_timeout(void *user_data)
1631 struct pending_write *p = user_data;
1635 queue_remove(p->attrib->pending_writes, p);
1637 pending_write_result(p, -ETIMEDOUT);
1642 bool gatt_db_attribute_write(struct gatt_db_attribute *attrib, uint16_t offset,
1643 const uint8_t *value, size_t len,
1644 uint8_t opcode, struct bt_att *att,
1645 gatt_db_attribute_write_t func,
1648 if (!attrib || !func)
1651 if (attrib->write_func) {
1652 struct pending_write *p;
1654 p = new0(struct pending_write, 1);
1656 p->id = ++attrib->write_id;
1657 p->timeout_id = timeout_add(ATTRIBUTE_TIMEOUT, write_timeout,
1660 p->user_data = user_data;
1662 queue_push_tail(attrib->pending_writes, p);
1664 attrib->write_func(attrib, p->id, offset, value, len, opcode,
1665 att, attrib->user_data);
1669 /* Nothing to write just skip */
1673 /* For values stored in db allocate on demand */
1674 if (!attrib->value || offset >= attrib->value_len ||
1675 len > (unsigned) (attrib->value_len - offset)) {
1678 buf = realloc(attrib->value, len + offset);
1682 attrib->value = buf;
1684 /* Init data in the first allocation */
1685 if (!attrib->value_len)
1686 memset(attrib->value, 0, offset);
1688 attrib->value_len = len + offset;
1691 memcpy(&attrib->value[offset], value, len);
1694 func(attrib, 0, user_data);
1699 bool gatt_db_attribute_write_result(struct gatt_db_attribute *attrib,
1700 unsigned int id, int err)
1702 struct pending_write *p;
1707 p = queue_remove_if(attrib->pending_writes, find_pending,
1712 pending_write_result(p, err);
1717 bool gatt_db_attribute_reset(struct gatt_db_attribute *attrib)
1722 if (!attrib->value || !attrib->value_len)
1725 free(attrib->value);
1726 attrib->value = NULL;
1727 attrib->value_len = 0;
1732 #ifdef __TIZEN_PATCH__
1733 void set_ccc_notify_indicate(struct gatt_db_attribute *ccc,
1737 ccc->notify_indicate = enable;
1740 bool get_ccc_notify_indicate(const struct gatt_db_attribute *ccc)
1743 return ccc->notify_indicate;
1748 void set_ccc_unicast_address(const struct gatt_db_attribute *ccc,
1749 const char *address)
1752 str2ba(address, (bdaddr_t *)&ccc->unicast_addr);
1755 bdaddr_t *get_ccc_unicast_address(const struct gatt_db_attribute *ccc)
1758 return &ccc->unicast_addr;