Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / src / shared / gatt-db.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2014  Intel Corporation. All rights reserved.
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdbool.h>
29 #include <errno.h>
30
31 #include "lib/bluetooth.h"
32 #include "lib/uuid.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"
38
39 #ifndef MAX
40 #define MAX(a, b) ((a) > (b) ? (a) : (b))
41 #endif
42
43 #define MAX_CHAR_DECL_VALUE_LEN 19
44 #define MAX_INCLUDED_VALUE_LEN 6
45 #define ATTRIBUTE_TIMEOUT 5000
46
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 };
55
56 struct gatt_db {
57         int ref_count;
58         uint16_t next_handle;
59         struct queue *services;
60
61         struct queue *notify_list;
62         unsigned int next_notify_id;
63 };
64
65 struct notify {
66         unsigned int id;
67         gatt_db_attribute_cb_t service_added;
68         gatt_db_attribute_cb_t service_removed;
69         gatt_db_destroy_func_t destroy;
70         void *user_data;
71 };
72
73 struct pending_read {
74         struct gatt_db_attribute *attrib;
75         unsigned int id;
76         unsigned int timeout_id;
77         gatt_db_attribute_read_t func;
78         void *user_data;
79 };
80
81 struct pending_write {
82         struct gatt_db_attribute *attrib;
83         unsigned int id;
84         unsigned int timeout_id;
85         gatt_db_attribute_write_t func;
86         void *user_data;
87 };
88
89 struct gatt_db_attribute {
90         struct gatt_db_service *service;
91         uint16_t handle;
92         bt_uuid_t uuid;
93         uint32_t permissions;
94         uint16_t value_len;
95         uint8_t *value;
96 #ifdef __TIZEN_PATCH__
97         bool notify_indicate;
98         bdaddr_t unicast_addr;
99 #endif
100
101         gatt_db_read_t read_func;
102         gatt_db_write_t write_func;
103         void *user_data;
104
105         unsigned int read_id;
106         struct queue *pending_reads;
107
108         unsigned int write_id;
109         struct queue *pending_writes;
110 };
111
112 struct gatt_db_service {
113         struct gatt_db *db;
114         bool active;
115         bool claimed;
116         uint16_t num_handles;
117         struct gatt_db_attribute **attributes;
118 };
119
120 static void pending_read_result(struct pending_read *p, int err,
121                                         const uint8_t *data, size_t length)
122 {
123         if (p->timeout_id > 0)
124                 timeout_remove(p->timeout_id);
125
126         p->func(p->attrib, err, data, length, p->user_data);
127
128         free(p);
129 }
130
131 static void pending_read_free(void *data)
132 {
133         struct pending_read *p = data;
134
135         pending_read_result(p, -ECANCELED, NULL, 0);
136 }
137
138 static void pending_write_result(struct pending_write *p, int err)
139 {
140         if (p->timeout_id > 0)
141                 timeout_remove(p->timeout_id);
142
143         p->func(p->attrib, err, p->user_data);
144
145         free(p);
146 }
147
148 static void pending_write_free(void *data)
149 {
150         struct pending_write *p = data;
151
152         pending_write_result(p, -ECANCELED);
153 }
154
155 static void attribute_destroy(struct gatt_db_attribute *attribute)
156 {
157         /* Attribute was not initialized by user */
158         if (!attribute)
159                 return;
160
161         queue_destroy(attribute->pending_reads, pending_read_free);
162         queue_destroy(attribute->pending_writes, pending_write_free);
163
164         free(attribute->value);
165         free(attribute);
166 }
167
168 static struct gatt_db_attribute *new_attribute(struct gatt_db_service *service,
169                                                         uint16_t handle,
170                                                         const bt_uuid_t *type,
171                                                         const uint8_t *val,
172                                                         uint16_t len)
173 {
174         struct gatt_db_attribute *attribute;
175
176         attribute = new0(struct gatt_db_attribute, 1);
177
178         attribute->service = service;
179         attribute->handle = handle;
180         attribute->uuid = *type;
181         attribute->value_len = len;
182         if (len) {
183                 attribute->value = malloc0(len);
184                 if (!attribute->value)
185                         goto failed;
186
187                 memcpy(attribute->value, val, len);
188         }
189
190         attribute->pending_reads = queue_new();
191         attribute->pending_writes = queue_new();
192
193         return attribute;
194
195 failed:
196         attribute_destroy(attribute);
197         return NULL;
198 }
199
200 struct gatt_db *gatt_db_ref(struct gatt_db *db)
201 {
202         if (!db)
203                 return NULL;
204
205         __sync_fetch_and_add(&db->ref_count, 1);
206
207         return db;
208 }
209
210 struct gatt_db *gatt_db_new(void)
211 {
212         struct gatt_db *db;
213
214         db = new0(struct gatt_db, 1);
215         db->services = queue_new();
216         db->notify_list = queue_new();
217         db->next_handle = 0x0001;
218
219         return gatt_db_ref(db);
220 }
221
222 static void notify_destroy(void *data)
223 {
224         struct notify *notify = data;
225
226         if (notify->destroy)
227                 notify->destroy(notify->user_data);
228
229         free(notify);
230 }
231
232 static bool match_notify_id(const void *a, const void *b)
233 {
234         const struct notify *notify = a;
235         unsigned int id = PTR_TO_UINT(b);
236
237         return notify->id == id;
238 }
239
240 struct notify_data {
241         struct gatt_db_attribute *attr;
242         bool added;
243 };
244
245 static void handle_notify(void *data, void *user_data)
246 {
247         struct notify *notify = data;
248         struct notify_data *notify_data = user_data;
249
250         if (notify_data->added)
251                 notify->service_added(notify_data->attr, notify->user_data);
252         else
253                 notify->service_removed(notify_data->attr, notify->user_data);
254 }
255
256 static void notify_service_changed(struct gatt_db *db,
257                                                 struct gatt_db_service *service,
258                                                 bool added)
259 {
260         struct notify_data data;
261
262         if (queue_isempty(db->notify_list))
263                 return;
264
265         data.attr = service->attributes[0];
266         data.added = added;
267
268         gatt_db_ref(db);
269
270         queue_foreach(db->notify_list, handle_notify, &data);
271
272         gatt_db_unref(db);
273 }
274
275 static void gatt_db_service_destroy(void *data)
276 {
277         struct gatt_db_service *service = data;
278         int i;
279
280         if (service->active)
281                 notify_service_changed(service->db, service, false);
282
283         for (i = 0; i < service->num_handles; i++)
284                 attribute_destroy(service->attributes[i]);
285
286         free(service->attributes);
287         free(service);
288 }
289
290 static void gatt_db_destroy(struct gatt_db *db)
291 {
292         if (!db)
293                 return;
294
295         /*
296          * Clear the notify list before clearing the services to prevent the
297          * latter from sending service_removed events.
298          */
299         queue_destroy(db->notify_list, notify_destroy);
300         db->notify_list = NULL;
301
302         queue_destroy(db->services, gatt_db_service_destroy);
303         free(db);
304 }
305
306 void gatt_db_unref(struct gatt_db *db)
307 {
308         if (!db)
309                 return;
310
311         if (__sync_sub_and_fetch(&db->ref_count, 1))
312                 return;
313
314         gatt_db_destroy(db);
315 }
316
317 bool gatt_db_isempty(struct gatt_db *db)
318 {
319         if (!db)
320                 return true;
321
322         return queue_isempty(db->services);
323 }
324
325 static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst)
326 {
327         bt_uuid_t uuid128;
328
329         if (uuid->type == BT_UUID16) {
330                 put_le16(uuid->value.u16, dst);
331                 return bt_uuid_len(uuid);
332         }
333
334         bt_uuid_to_uuid128(uuid, &uuid128);
335         bswap_128(&uuid128.value.u128, dst);
336         return bt_uuid_len(&uuid128);
337 }
338
339 static bool le_to_uuid(const uint8_t *src, size_t len, bt_uuid_t *uuid)
340 {
341         uint128_t u128;
342
343         if (len == 2) {
344                 bt_uuid16_create(uuid, get_le16(src));
345                 return true;
346         }
347
348         if (len == 4) {
349                 bt_uuid32_create(uuid, get_le32(src));
350                 return true;
351         }
352
353         if (len != 16)
354                 return false;
355
356         bswap_128(src, &u128);
357         bt_uuid128_create(uuid, u128);
358
359         return true;
360 }
361
362 static struct gatt_db_service *gatt_db_service_create(const bt_uuid_t *uuid,
363                                                         uint16_t handle,
364                                                         bool primary,
365                                                         uint16_t num_handles)
366 {
367         struct gatt_db_service *service;
368         const bt_uuid_t *type;
369         uint8_t value[16];
370         uint16_t len;
371
372         if (num_handles < 1)
373                 return NULL;
374
375         service = new0(struct gatt_db_service, 1);
376         service->attributes = new0(struct gatt_db_attribute *, num_handles);
377
378         if (primary)
379                 type = &primary_service_uuid;
380         else
381                 type = &secondary_service_uuid;
382
383         len = uuid_to_le(uuid, value);
384
385         service->attributes[0] = new_attribute(service, handle, type, value,
386                                                                         len);
387         if (!service->attributes[0]) {
388                 gatt_db_service_destroy(service);
389                 return NULL;
390         }
391
392         return service;
393 }
394
395
396 bool gatt_db_remove_service(struct gatt_db *db,
397                                         struct gatt_db_attribute *attrib)
398 {
399         struct gatt_db_service *service;
400
401         if (!db || !attrib)
402                 return false;
403
404         service = attrib->service;
405
406         queue_remove(db->services, service);
407
408         gatt_db_service_destroy(service);
409
410         return true;
411 }
412
413 bool gatt_db_clear(struct gatt_db *db)
414 {
415         if (!db)
416                 return false;
417
418         queue_remove_all(db->services, NULL, NULL, gatt_db_service_destroy);
419
420         db->next_handle = 0;
421
422         return true;
423 }
424
425 static void gatt_db_service_get_handles(const struct gatt_db_service *service,
426                                                         uint16_t *start_handle,
427                                                         uint16_t *end_handle)
428 {
429         if (start_handle)
430                 *start_handle = service->attributes[0]->handle;
431
432         if (end_handle)
433                 *end_handle = service->attributes[0]->handle +
434                                                 service->num_handles - 1;
435 }
436
437 struct clear_range {
438         uint16_t start, end;
439 };
440
441 static bool match_range(const void *a, const void *b)
442 {
443         const struct gatt_db_service *service = a;
444         const struct clear_range *range = b;
445         uint16_t svc_start, svc_end;
446
447         gatt_db_service_get_handles(service, &svc_start, &svc_end);
448
449         return svc_start <= range->end && svc_end >= range->start;
450 }
451
452 bool gatt_db_clear_range(struct gatt_db *db, uint16_t start_handle,
453                                                         uint16_t end_handle)
454 {
455         struct clear_range range;
456
457         if (!db || start_handle > end_handle)
458                 return false;
459
460         range.start = start_handle;
461         range.end = end_handle;
462
463         queue_remove_all(db->services, match_range, &range,
464                                                 gatt_db_service_destroy);
465
466         return true;
467 }
468
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)
472 {
473         const struct queue_entry *services_entry;
474         struct gatt_db_service *service;
475         uint16_t cur_start, cur_end;
476
477         *after = NULL;
478
479         services_entry = queue_get_entries(db->services);
480
481         while (services_entry) {
482                 service = services_entry->data;
483
484                 gatt_db_service_get_handles(service, &cur_start, &cur_end);
485
486                 if (start >= cur_start && start <= cur_end)
487                         return service;
488
489                 if (end >= cur_start && end <= cur_end)
490                         return service;
491
492                 if (end < cur_start)
493                         return NULL;
494
495                 *after = service;
496                 services_entry = services_entry->next;
497         }
498
499         return NULL;
500 }
501
502 struct gatt_db_attribute *gatt_db_insert_service(struct gatt_db *db,
503                                                         uint16_t handle,
504                                                         const bt_uuid_t *uuid,
505                                                         bool primary,
506                                                         uint16_t num_handles)
507 {
508         struct gatt_db_service *service, *after;
509
510         after = NULL;
511
512         if (!db || handle < 1)
513                 return NULL;
514
515         if (num_handles < 1 || (handle + num_handles - 1) > UINT16_MAX)
516                 return NULL;
517
518         service = find_insert_loc(db, handle, handle + num_handles - 1, &after);
519         if (service) {
520                 const bt_uuid_t *type;
521                 bt_uuid_t value;
522
523                 if (primary)
524                         type = &primary_service_uuid;
525                 else
526                         type = &secondary_service_uuid;
527
528                 gatt_db_attribute_get_service_uuid(service->attributes[0],
529                                                                         &value);
530
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];
537
538                 return NULL;
539         }
540
541         service = gatt_db_service_create(uuid, handle, primary, num_handles);
542
543         if (!service)
544                 return NULL;
545
546         if (after) {
547                 if (!queue_push_after(db->services, after, service))
548                         goto fail;
549         } else if (!queue_push_head(db->services, service)) {
550                 goto fail;
551         }
552
553         service->db = db;
554         service->attributes[0]->handle = handle;
555         service->num_handles = num_handles;
556
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);
559
560         return service->attributes[0];
561
562 fail:
563         gatt_db_service_destroy(service);
564         return NULL;
565 }
566
567 struct gatt_db_attribute *gatt_db_add_service(struct gatt_db *db,
568                                                 const bt_uuid_t *uuid,
569                                                 bool primary,
570                                                 uint16_t num_handles)
571 {
572         return gatt_db_insert_service(db, db->next_handle, uuid, primary,
573                                                                 num_handles);
574 }
575
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,
579                                         void *user_data,
580                                         gatt_db_destroy_func_t destroy)
581 {
582         struct notify *notify;
583
584         if (!db || !(service_added || service_removed))
585                 return 0;
586
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;
592
593         if (db->next_notify_id < 1)
594                 db->next_notify_id = 1;
595
596         notify->id = db->next_notify_id++;
597
598         if (!queue_push_tail(db->notify_list, notify)) {
599                 free(notify);
600                 return 0;
601         }
602
603         return notify->id;
604 }
605
606 bool gatt_db_unregister(struct gatt_db *db, unsigned int id)
607 {
608         struct notify *notify;
609
610         if (!db || !id)
611                 return false;
612
613         notify = queue_find(db->notify_list, match_notify_id, UINT_TO_PTR(id));
614         if (!notify)
615                 return false;
616
617         queue_remove(db->notify_list, notify);
618         notify_destroy(notify);
619
620         return true;
621 }
622
623 static uint16_t get_attribute_index(struct gatt_db_service *service,
624                                                         int end_offset)
625 {
626         int i = 0;
627
628         /* Here we look for first free attribute index with given offset */
629         while (i < (service->num_handles - end_offset) &&
630                                                 service->attributes[i])
631                 i++;
632
633         return i == (service->num_handles - end_offset) ? 0 : i;
634 }
635
636 static uint16_t get_handle_at_index(struct gatt_db_service *service,
637                                                                 int index)
638 {
639         return service->attributes[index]->handle;
640 }
641
642 static struct gatt_db_attribute *
643 attribute_update(struct gatt_db_service *service, int index)
644 {
645         uint16_t previous_handle;
646
647         /* We call this function with index > 0, because index 0 is reserved
648          * for service declaration, and is set in add_service()
649          */
650         previous_handle = service->attributes[index - 1]->handle;
651         service->attributes[index]->handle = previous_handle + 1;
652
653         return service->attributes[index];
654 }
655
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,
660                                                 void *user_data)
661 {
662         attribute->permissions = permissions;
663         attribute->read_func = read_func;
664         attribute->write_func = write_func;
665         attribute->user_data = user_data;
666 }
667
668 static struct gatt_db_attribute *
669 service_insert_characteristic(struct gatt_db_service *service,
670                                         uint16_t handle,
671                                         const bt_uuid_t *uuid,
672                                         uint32_t permissions,
673                                         uint8_t properties,
674                                         gatt_db_read_t read_func,
675                                         gatt_db_write_t write_func,
676                                         void *user_data)
677 {
678         uint8_t value[MAX_CHAR_DECL_VALUE_LEN];
679         uint16_t len = 0;
680         int i;
681
682         /* Check if handle is in within service range */
683         if (handle && handle <= service->attributes[0]->handle)
684                 return NULL;
685
686         /*
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.
694          */
695         if (handle == UINT16_MAX)
696                 return NULL;
697
698         i = get_attribute_index(service, 1);
699         if (!i)
700                 return NULL;
701
702         if (!handle)
703                 handle = get_handle_at_index(service, i - 1) + 2;
704
705         value[0] = properties;
706         len += sizeof(properties);
707
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]);
712
713         service->attributes[i] = new_attribute(service, handle - 1,
714                                                         &characteristic_uuid,
715                                                         value, len);
716         if (!service->attributes[i])
717                 return NULL;
718
719         i++;
720
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;
726 #endif
727                 return NULL;
728         }
729
730         set_attribute_data(service->attributes[i], read_func, write_func,
731                                                         permissions, user_data);
732
733         return service->attributes[i];
734 }
735
736 struct gatt_db_attribute *
737 gatt_db_service_insert_characteristic(struct gatt_db_attribute *attrib,
738                                         uint16_t handle,
739                                         const bt_uuid_t *uuid,
740                                         uint32_t permissions,
741                                         uint8_t properties,
742                                         gatt_db_read_t read_func,
743                                         gatt_db_write_t write_func,
744                                         void *user_data)
745 {
746         if (!attrib || !handle)
747                 return NULL;
748
749         return service_insert_characteristic(attrib->service, handle, uuid,
750                                                 permissions, properties,
751                                                 read_func, write_func,
752                                                 user_data);
753 }
754
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,
759                                         uint8_t properties,
760                                         gatt_db_read_t read_func,
761                                         gatt_db_write_t write_func,
762                                         void *user_data)
763 {
764         if (!attrib)
765                 return NULL;
766
767         return service_insert_characteristic(attrib->service, 0, uuid,
768                                                 permissions, properties,
769                                                 read_func, write_func,
770                                                 user_data);
771 }
772
773 static struct gatt_db_attribute *
774 service_insert_descriptor(struct gatt_db_service *service,
775                                         uint16_t handle,
776                                         const bt_uuid_t *uuid,
777                                         uint32_t permissions,
778                                         gatt_db_read_t read_func,
779                                         gatt_db_write_t write_func,
780                                         void *user_data)
781 {
782         int i;
783
784         i = get_attribute_index(service, 0);
785         if (!i)
786                 return NULL;
787
788         /* Check if handle is in within service range */
789         if (handle && handle <= service->attributes[0]->handle)
790                 return NULL;
791
792         if (!handle)
793                 handle = get_handle_at_index(service, i - 1) + 1;
794
795         service->attributes[i] = new_attribute(service, handle, uuid, NULL, 0);
796         if (!service->attributes[i])
797                 return NULL;
798
799         set_attribute_data(service->attributes[i], read_func, write_func,
800                                                         permissions, user_data);
801
802         return service->attributes[i];
803 }
804
805 struct gatt_db_attribute *
806 gatt_db_service_insert_descriptor(struct gatt_db_attribute *attrib,
807                                         uint16_t handle,
808                                         const bt_uuid_t *uuid,
809                                         uint32_t permissions,
810                                         gatt_db_read_t read_func,
811                                         gatt_db_write_t write_func,
812                                         void *user_data)
813 {
814         if (!attrib || !handle)
815                 return NULL;
816
817         return service_insert_descriptor(attrib->service, handle, uuid,
818                                         permissions, read_func, write_func,
819                                         user_data);
820 }
821
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,
828                                         void *user_data)
829 {
830         if (!attrib)
831                 return NULL;
832
833         return service_insert_descriptor(attrib->service, 0, uuid,
834                                         permissions, read_func, write_func,
835                                         user_data);
836 }
837
838 struct gatt_db_attribute *
839 gatt_db_service_add_included(struct gatt_db_attribute *attrib,
840                                         struct gatt_db_attribute *include)
841 {
842         struct gatt_db_service *service, *included;
843         uint8_t value[MAX_INCLUDED_VALUE_LEN];
844         uint16_t included_handle, len = 0;
845         int index;
846
847         if (!attrib || !include)
848                 return NULL;
849
850         service = attrib->service;
851         included = include->service;
852
853         /* Adjust include to point to the first attribute */
854         if (include != included->attributes[0])
855                 include = included->attributes[0];
856
857         included_handle = include->handle;
858
859         put_le16(included_handle, &value[len]);
860         len += sizeof(uint16_t);
861
862         put_le16(included_handle + included->num_handles - 1, &value[len]);
863         len += sizeof(uint16_t);
864
865         /* The Service UUID shall only be present when the UUID is a 16-bit
866          * Bluetooth UUID. Vol 2. Part G. 3.2
867          */
868         if (include->value_len == sizeof(uint16_t)) {
869                 memcpy(&value[len], include->value, include->value_len);
870                 len += include->value_len;
871         }
872
873         index = get_attribute_index(service, 0);
874         if (!index)
875                 return NULL;
876
877         service->attributes[index] = new_attribute(service, 0,
878                                                         &included_service_uuid,
879                                                         value, len);
880         if (!service->attributes[index])
881                 return NULL;
882
883         /* The Attribute Permissions shall be read only and not require
884          * authentication or authorization. Vol 2. Part G. 3.2
885          *
886          * TODO handle permissions
887          */
888         set_attribute_data(service->attributes[index], NULL, NULL, 0, NULL);
889
890         return attribute_update(service, index);
891 }
892
893 bool gatt_db_service_set_active(struct gatt_db_attribute *attrib, bool active)
894 {
895         struct gatt_db_service *service;
896
897         if (!attrib)
898                 return false;
899
900         service = attrib->service;
901
902         if (service->active == active)
903                 return true;
904
905         service->active = active;
906
907         notify_service_changed(service->db, service, active);
908
909         return true;
910 }
911
912 bool gatt_db_service_get_active(struct gatt_db_attribute *attrib)
913 {
914         if (!attrib)
915                 return false;
916
917         return attrib->service->active;
918 }
919
920 bool gatt_db_service_set_claimed(struct gatt_db_attribute *attrib,
921                                                                 bool claimed)
922 {
923         if (!attrib)
924                 return false;
925
926         attrib->service->claimed = claimed;
927
928         return true;
929 }
930
931 bool gatt_db_service_get_claimed(struct gatt_db_attribute *attrib)
932 {
933         if (!attrib)
934                 return false;
935
936         return attrib->service->claimed;
937 }
938
939 void gatt_db_read_by_group_type(struct gatt_db *db, uint16_t start_handle,
940                                                         uint16_t end_handle,
941                                                         const bt_uuid_t type,
942                                                         struct queue *queue)
943 {
944         const struct queue_entry *services_entry;
945         struct gatt_db_service *service;
946         uint16_t grp_start, grp_end, uuid_size;
947
948         uuid_size = 0;
949
950         services_entry = queue_get_entries(db->services);
951
952         while (services_entry) {
953                 service = services_entry->data;
954
955                 if (!service->active)
956                         goto next_service;
957
958                 if (bt_uuid_cmp(&type, &service->attributes[0]->uuid))
959                         goto next_service;
960
961                 grp_start = service->attributes[0]->handle;
962                 grp_end = grp_start + service->num_handles - 1;
963
964                 if (grp_end < start_handle || grp_start > end_handle)
965                         goto next_service;
966
967                 if (grp_start < start_handle || grp_start > end_handle)
968                         goto next_service;
969
970                 if (!uuid_size)
971                         uuid_size = service->attributes[0]->value_len;
972                 else if (uuid_size != service->attributes[0]->value_len)
973                         return;
974
975                 queue_push_tail(queue, service->attributes[0]);
976
977 next_service:
978                 services_entry = services_entry->next;
979         }
980 }
981
982 struct find_by_type_value_data {
983         bt_uuid_t uuid;
984         uint16_t start_handle;
985         uint16_t end_handle;
986         gatt_db_attribute_cb_t func;
987         void *user_data;
988         const void *value;
989         size_t value_len;
990         unsigned int num_of_res;
991 };
992
993 static void find_by_type(void *data, void *user_data)
994 {
995         struct find_by_type_value_data *search_data = user_data;
996         struct gatt_db_service *service = data;
997         struct gatt_db_attribute *attribute;
998         int i;
999
1000         if (!service->active)
1001                 return;
1002
1003         for (i = 0; i < service->num_handles; i++) {
1004                 attribute = service->attributes[i];
1005
1006                 if (!attribute)
1007                         continue;
1008
1009                 if ((attribute->handle < search_data->start_handle) ||
1010                                 (attribute->handle > search_data->end_handle))
1011                         continue;
1012
1013                 if (bt_uuid_cmp(&search_data->uuid, &attribute->uuid))
1014                         continue;
1015
1016                 /* TODO: fix for read-callback based attributes */
1017                 if (search_data->value && memcmp(attribute->value,
1018                                                         search_data->value,
1019                                                         search_data->value_len))
1020                         continue;
1021
1022                 search_data->num_of_res++;
1023                 search_data->func(attribute, search_data->user_data);
1024         }
1025 }
1026
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,
1031                                                 void *user_data)
1032 {
1033         struct find_by_type_value_data data;
1034
1035         memset(&data, 0, sizeof(data));
1036
1037         data.uuid = *type;
1038         data.start_handle = start_handle;
1039         data.end_handle = end_handle;
1040         data.func = func;
1041         data.user_data = user_data;
1042
1043         queue_foreach(db->services, find_by_type, &data);
1044
1045         return data.num_of_res;
1046 }
1047
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,
1052                                                 const void *value,
1053                                                 size_t value_len,
1054                                                 gatt_db_attribute_cb_t func,
1055                                                 void *user_data)
1056 {
1057         struct find_by_type_value_data data;
1058
1059         data.uuid = *type;
1060         data.start_handle = start_handle;
1061         data.end_handle = end_handle;
1062         data.func = func;
1063         data.user_data = user_data;
1064         data.value = value;
1065         data.value_len = value_len;
1066
1067         queue_foreach(db->services, find_by_type, &data);
1068
1069         return data.num_of_res;
1070 }
1071
1072 struct read_by_type_data {
1073         struct queue *queue;
1074         bt_uuid_t uuid;
1075         uint16_t start_handle;
1076         uint16_t end_handle;
1077 };
1078
1079 static void read_by_type(void *data, void *user_data)
1080 {
1081         struct read_by_type_data *search_data = user_data;
1082         struct gatt_db_service *service = data;
1083         struct gatt_db_attribute *attribute;
1084         int i;
1085
1086         if (!service->active)
1087                 return;
1088
1089         for (i = 0; i < service->num_handles; i++) {
1090                 attribute = service->attributes[i];
1091                 if (!attribute)
1092                         continue;
1093
1094                 if (attribute->handle < search_data->start_handle)
1095                         continue;
1096
1097                 if (attribute->handle > search_data->end_handle)
1098                         return;
1099
1100                 if (bt_uuid_cmp(&search_data->uuid, &attribute->uuid))
1101                         continue;
1102
1103                 queue_push_tail(search_data->queue, attribute);
1104         }
1105 }
1106
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)
1111 {
1112         struct read_by_type_data data;
1113         data.uuid = type;
1114         data.start_handle = start_handle;
1115         data.end_handle = end_handle;
1116         data.queue = queue;
1117
1118         queue_foreach(db->services, read_by_type, &data);
1119 }
1120
1121
1122 struct find_information_data {
1123         struct queue *queue;
1124         uint16_t start_handle;
1125         uint16_t end_handle;
1126 };
1127
1128 static void find_information(void *data, void *user_data)
1129 {
1130         struct find_information_data *search_data = user_data;
1131         struct gatt_db_service *service = data;
1132         struct gatt_db_attribute *attribute;
1133         int i;
1134
1135         if (!service->active)
1136                 return;
1137
1138         /* Check if service is in range */
1139         if ((service->attributes[0]->handle + service->num_handles - 1) <
1140                                                 search_data->start_handle)
1141                 return;
1142
1143         for (i = 0; i < service->num_handles; i++) {
1144                 attribute = service->attributes[i];
1145                 if (!attribute)
1146                         continue;
1147
1148                 if (attribute->handle < search_data->start_handle)
1149                         continue;
1150
1151                 if (attribute->handle > search_data->end_handle)
1152                         return;
1153
1154                 queue_push_tail(search_data->queue, attribute);
1155         }
1156 }
1157
1158 void gatt_db_find_information(struct gatt_db *db, uint16_t start_handle,
1159                                                         uint16_t end_handle,
1160                                                         struct queue *queue)
1161 {
1162         struct find_information_data data;
1163
1164         data.start_handle = start_handle;
1165         data.end_handle = end_handle;
1166         data.queue = queue;
1167
1168         queue_foreach(db->services, find_information, &data);
1169 }
1170
1171 void gatt_db_foreach_service(struct gatt_db *db, const bt_uuid_t *uuid,
1172                                                 gatt_db_attribute_cb_t func,
1173                                                 void *user_data)
1174 {
1175         gatt_db_foreach_service_in_range(db, uuid, func, user_data, 0x0001,
1176                                                                         0xffff);
1177 }
1178
1179 struct foreach_data {
1180         gatt_db_attribute_cb_t func;
1181         const bt_uuid_t *uuid;
1182         void *user_data;
1183         uint16_t start, end;
1184 };
1185
1186 static void foreach_service_in_range(void *data, void *user_data)
1187 {
1188         struct gatt_db_service *service = data;
1189         struct foreach_data *foreach_data = user_data;
1190         uint16_t svc_start;
1191         bt_uuid_t uuid;
1192
1193         svc_start = get_handle_at_index(service, 0);
1194
1195         if (svc_start > foreach_data->end || svc_start < foreach_data->start)
1196                 return;
1197
1198         if (foreach_data->uuid) {
1199                 gatt_db_attribute_get_service_uuid(service->attributes[0],
1200                                                                         &uuid);
1201                 if (bt_uuid_cmp(&uuid, foreach_data->uuid))
1202                         return;
1203         }
1204
1205         foreach_data->func(service->attributes[0], foreach_data->user_data);
1206 }
1207
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,
1211                                                 void *user_data,
1212                                                 uint16_t start_handle,
1213                                                 uint16_t end_handle)
1214 {
1215         struct foreach_data data;
1216
1217         if (!db || !func || start_handle > end_handle)
1218                 return;
1219
1220         data.func = func;
1221         data.uuid = uuid;
1222         data.user_data = user_data;
1223         data.start = start_handle;
1224         data.end = end_handle;
1225
1226         queue_foreach(db->services, foreach_service_in_range, &data);
1227 }
1228
1229 void gatt_db_service_foreach(struct gatt_db_attribute *attrib,
1230                                                 const bt_uuid_t *uuid,
1231                                                 gatt_db_attribute_cb_t func,
1232                                                 void *user_data)
1233 {
1234         struct gatt_db_service *service;
1235         struct gatt_db_attribute *attr;
1236         uint16_t i;
1237
1238         if (!attrib || !func)
1239                 return;
1240
1241         service = attrib->service;
1242
1243         for (i = 0; i < service->num_handles; i++) {
1244                 attr = service->attributes[i];
1245                 if (!attr)
1246                         continue;
1247
1248                 if (uuid && bt_uuid_cmp(uuid, &attr->uuid))
1249                         continue;
1250
1251                 func(attr, user_data);
1252         }
1253 }
1254
1255 void gatt_db_service_foreach_char(struct gatt_db_attribute *attrib,
1256                                                 gatt_db_attribute_cb_t func,
1257                                                 void *user_data)
1258 {
1259         gatt_db_service_foreach(attrib, &characteristic_uuid, func, user_data);
1260 }
1261
1262 void gatt_db_service_foreach_desc(struct gatt_db_attribute *attrib,
1263                                                 gatt_db_attribute_cb_t func,
1264                                                 void *user_data)
1265 {
1266         struct gatt_db_service *service;
1267         struct gatt_db_attribute *attr;
1268         uint16_t i;
1269
1270         if (!attrib || !func)
1271                 return;
1272
1273         /* Return if this attribute is not a characteristic declaration */
1274         if (bt_uuid_cmp(&characteristic_uuid, &attrib->uuid))
1275                 return;
1276
1277         service = attrib->service;
1278
1279         /* Start from the attribute following the value handle */
1280         for (i = 0; i < service->num_handles; i++) {
1281                 if (service->attributes[i] == attrib) {
1282                         i += 2;
1283                         break;
1284                 }
1285         }
1286
1287         for (; i < service->num_handles; i++) {
1288                 attr = service->attributes[i];
1289                 if (!attr)
1290                         continue;
1291
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))
1295                         return;
1296
1297                 func(attr, user_data);
1298         }
1299 }
1300
1301 void gatt_db_service_foreach_incl(struct gatt_db_attribute *attrib,
1302                                                 gatt_db_attribute_cb_t func,
1303                                                 void *user_data)
1304 {
1305         gatt_db_service_foreach(attrib, &included_service_uuid, func,
1306                                                                 user_data);
1307 }
1308
1309 static bool find_service_for_handle(const void *data, const void *user_data)
1310 {
1311         const struct gatt_db_service *service = data;
1312         uint16_t handle = PTR_TO_UINT(user_data);
1313         uint16_t start, end;
1314
1315         gatt_db_service_get_handles(service, &start, &end);
1316
1317         return (start <= handle) && (handle <= end);
1318 }
1319
1320 struct gatt_db_attribute *gatt_db_get_attribute(struct gatt_db *db,
1321                                                         uint16_t handle)
1322 {
1323         struct gatt_db_service *service;
1324         int i;
1325
1326         if (!db || !handle)
1327                 return NULL;
1328
1329         service = queue_find(db->services, find_service_for_handle,
1330                                                         UINT_TO_PTR(handle));
1331         if (!service)
1332                 return NULL;
1333
1334         for (i = 0; i < service->num_handles; i++) {
1335                 if (!service->attributes[i])
1336                         continue;
1337
1338                 if (service->attributes[i]->handle == handle)
1339                         return service->attributes[i];
1340         }
1341
1342         return NULL;
1343 }
1344
1345 static bool find_service_with_uuid(const void *data, const void *user_data)
1346 {
1347         const struct gatt_db_service *service = data;
1348         const bt_uuid_t *uuid = user_data;
1349         bt_uuid_t svc_uuid;
1350
1351         gatt_db_attribute_get_service_uuid(service->attributes[0], &svc_uuid);
1352
1353         return bt_uuid_cmp(uuid, &svc_uuid) == 0;
1354 }
1355
1356 struct gatt_db_attribute *gatt_db_get_service_with_uuid(struct gatt_db *db,
1357                                                         const bt_uuid_t *uuid)
1358 {
1359         struct gatt_db_service *service;
1360
1361         if (!db || !uuid)
1362                 return NULL;
1363
1364         service = queue_find(db->services, find_service_with_uuid, uuid);
1365         if (!service)
1366                 return NULL;
1367
1368         return service->attributes[0];
1369 }
1370
1371 const bt_uuid_t *gatt_db_attribute_get_type(
1372                                         const struct gatt_db_attribute *attrib)
1373 {
1374         if (!attrib)
1375                 return NULL;
1376
1377         return &attrib->uuid;
1378 }
1379
1380 uint16_t gatt_db_attribute_get_handle(const struct gatt_db_attribute *attrib)
1381 {
1382         if (!attrib)
1383                 return 0;
1384
1385         return attrib->handle;
1386 }
1387
1388 bool gatt_db_attribute_get_service_uuid(const struct gatt_db_attribute *attrib,
1389                                                         bt_uuid_t *uuid)
1390 {
1391         struct gatt_db_service *service;
1392
1393         if (!attrib || !uuid)
1394                 return false;
1395
1396         service = attrib->service;
1397
1398         if (service->attributes[0]->value_len == sizeof(uint16_t)) {
1399                 uint16_t value;
1400
1401                 value = get_le16(service->attributes[0]->value);
1402                 bt_uuid16_create(uuid, value);
1403
1404                 return true;
1405         }
1406
1407         if (service->attributes[0]->value_len == sizeof(uint128_t)) {
1408                 uint128_t value;
1409
1410                 bswap_128(service->attributes[0]->value, &value);
1411                 bt_uuid128_create(uuid, value);
1412
1413                 return true;
1414         }
1415
1416         return false;
1417 }
1418
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)
1423 {
1424         struct gatt_db_service *service;
1425
1426         if (!attrib)
1427                 return false;
1428
1429         service = attrib->service;
1430
1431         gatt_db_service_get_handles(service, start_handle, end_handle);
1432
1433         return true;
1434 }
1435
1436 bool gatt_db_attribute_get_service_data(const struct gatt_db_attribute *attrib,
1437                                                         uint16_t *start_handle,
1438                                                         uint16_t *end_handle,
1439                                                         bool *primary,
1440                                                         bt_uuid_t *uuid)
1441 {
1442         struct gatt_db_service *service;
1443         struct gatt_db_attribute *decl;
1444
1445         if (!attrib)
1446                 return false;
1447
1448         service = attrib->service;
1449         decl = service->attributes[0];
1450
1451         gatt_db_service_get_handles(service, start_handle, end_handle);
1452
1453         if (primary)
1454                 *primary = bt_uuid_cmp(&decl->uuid, &secondary_service_uuid);
1455
1456         if (!uuid)
1457                 return true;
1458
1459         /*
1460          * The service declaration attribute value is the 16 or 128 bit service
1461          * UUID.
1462          */
1463         return le_to_uuid(decl->value, decl->value_len, uuid);
1464 }
1465
1466 bool gatt_db_attribute_get_char_data(const struct gatt_db_attribute *attrib,
1467                                                         uint16_t *handle,
1468                                                         uint16_t *value_handle,
1469                                                         uint8_t *properties,
1470                                                         bt_uuid_t *uuid)
1471 {
1472         if (!attrib)
1473                 return false;
1474
1475         if (bt_uuid_cmp(&characteristic_uuid, &attrib->uuid))
1476                 return false;
1477
1478         /*
1479          * Characteristic declaration value:
1480          * 1 octet: Characteristic properties
1481          * 2 octets: Characteristic value handle
1482          * 2 or 16 octets: characteristic UUID
1483          */
1484         if (!attrib->value || (attrib->value_len != 5 &&
1485                                                 attrib->value_len != 19))
1486                 return false;
1487
1488         if (handle)
1489                 *handle = attrib->handle;
1490
1491         if (properties)
1492                 *properties = attrib->value[0];
1493
1494         if (value_handle)
1495                 *value_handle = get_le16(attrib->value + 1);
1496
1497         if (!uuid)
1498                 return true;
1499
1500         return le_to_uuid(attrib->value + 3, attrib->value_len - 3, uuid);
1501 }
1502
1503 bool gatt_db_attribute_get_incl_data(const struct gatt_db_attribute *attrib,
1504                                                         uint16_t *handle,
1505                                                         uint16_t *start_handle,
1506                                                         uint16_t *end_handle)
1507 {
1508         if (!attrib)
1509                 return false;
1510
1511         if (bt_uuid_cmp(&included_service_uuid, &attrib->uuid))
1512                 return false;
1513
1514         /*
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
1519          */
1520         if (!attrib->value || attrib->value_len < 4 || attrib->value_len > 6)
1521                 return false;
1522
1523         /*
1524          * We only return the handles since the UUID can be easily obtained
1525          * from the corresponding attribute.
1526          */
1527         if (handle)
1528                 *handle = attrib->handle;
1529
1530         if (start_handle)
1531                 *start_handle = get_le16(attrib->value);
1532
1533         if (end_handle)
1534                 *end_handle = get_le16(attrib->value + 2);
1535
1536         return true;
1537 }
1538
1539 uint32_t
1540 gatt_db_attribute_get_permissions(const struct gatt_db_attribute *attrib)
1541 {
1542         if (!attrib)
1543                 return 0;
1544
1545         return attrib->permissions;
1546 }
1547
1548 static bool read_timeout(void *user_data)
1549 {
1550         struct pending_read *p = user_data;
1551
1552         p->timeout_id = 0;
1553
1554         queue_remove(p->attrib->pending_reads, p);
1555
1556         pending_read_result(p, -ETIMEDOUT, NULL, 0);
1557
1558         return false;
1559 }
1560
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)
1564 {
1565         uint8_t *value;
1566
1567         if (!attrib || !func)
1568                 return false;
1569
1570         if (attrib->read_func) {
1571                 struct pending_read *p;
1572
1573                 p = new0(struct pending_read, 1);
1574                 p->attrib = attrib;
1575                 p->id = ++attrib->read_id;
1576                 p->timeout_id = timeout_add(ATTRIBUTE_TIMEOUT, read_timeout,
1577                                                                 p, NULL);
1578                 p->func = func;
1579                 p->user_data = user_data;
1580
1581                 queue_push_tail(attrib->pending_reads, p);
1582
1583                 attrib->read_func(attrib, p->id, offset, opcode, att,
1584                                                         attrib->user_data);
1585                 return true;
1586         }
1587
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);
1591                 return true;
1592         }
1593
1594         /* Guard against invalid access if offset equals to value length */
1595         value = offset == attrib->value_len ? NULL : &attrib->value[offset];
1596
1597         func(attrib, 0, value, attrib->value_len - offset, user_data);
1598
1599         return true;
1600 }
1601
1602 static bool find_pending(const void *a, const void *b)
1603 {
1604         const struct pending_read *p = a;
1605         unsigned int id = PTR_TO_UINT(b);
1606
1607         return p->id == id;
1608 }
1609
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)
1613 {
1614         struct pending_read *p;
1615
1616         if (!attrib || !id)
1617                 return false;
1618
1619         p = queue_remove_if(attrib->pending_reads, find_pending,
1620                                                         UINT_TO_PTR(id));
1621         if (!p)
1622                 return false;
1623
1624         pending_read_result(p, err, value, length);
1625
1626         return true;
1627 }
1628
1629 static bool write_timeout(void *user_data)
1630 {
1631         struct pending_write *p = user_data;
1632
1633         p->timeout_id = 0;
1634
1635         queue_remove(p->attrib->pending_writes, p);
1636
1637         pending_write_result(p, -ETIMEDOUT);
1638
1639         return false;
1640 }
1641
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,
1646                                         void *user_data)
1647 {
1648         if (!attrib || !func)
1649                 return false;
1650
1651         if (attrib->write_func) {
1652                 struct pending_write *p;
1653
1654                 p = new0(struct pending_write, 1);
1655                 p->attrib = attrib;
1656                 p->id = ++attrib->write_id;
1657                 p->timeout_id = timeout_add(ATTRIBUTE_TIMEOUT, write_timeout,
1658                                                                 p, NULL);
1659                 p->func = func;
1660                 p->user_data = user_data;
1661
1662                 queue_push_tail(attrib->pending_writes, p);
1663
1664                 attrib->write_func(attrib, p->id, offset, value, len, opcode,
1665                                                         att, attrib->user_data);
1666                 return true;
1667         }
1668
1669         /* Nothing to write just skip */
1670         if (len == 0)
1671                 goto done;
1672
1673         /* For values stored in db allocate on demand */
1674         if (!attrib->value || offset >= attrib->value_len ||
1675                                 len > (unsigned) (attrib->value_len - offset)) {
1676                 void *buf;
1677
1678                 buf = realloc(attrib->value, len + offset);
1679                 if (!buf)
1680                         return false;
1681
1682                 attrib->value = buf;
1683
1684                 /* Init data in the first allocation */
1685                 if (!attrib->value_len)
1686                         memset(attrib->value, 0, offset);
1687
1688                 attrib->value_len = len + offset;
1689         }
1690
1691         memcpy(&attrib->value[offset], value, len);
1692
1693 done:
1694         func(attrib, 0, user_data);
1695
1696         return true;
1697 }
1698
1699 bool gatt_db_attribute_write_result(struct gatt_db_attribute *attrib,
1700                                                 unsigned int id, int err)
1701 {
1702         struct pending_write *p;
1703
1704         if (!attrib || !id)
1705                 return false;
1706
1707         p = queue_remove_if(attrib->pending_writes, find_pending,
1708                                                         UINT_TO_PTR(id));
1709         if (!p)
1710                 return false;
1711
1712         pending_write_result(p, err);
1713
1714         return true;
1715 }
1716
1717 bool gatt_db_attribute_reset(struct gatt_db_attribute *attrib)
1718 {
1719         if (!attrib)
1720                 return false;
1721
1722         if (!attrib->value || !attrib->value_len)
1723                 return true;
1724
1725         free(attrib->value);
1726         attrib->value = NULL;
1727         attrib->value_len = 0;
1728
1729         return true;
1730 }
1731
1732 #ifdef __TIZEN_PATCH__
1733 void set_ccc_notify_indicate(struct gatt_db_attribute *ccc,
1734                                                         bool enable)
1735 {
1736         if (ccc)
1737                 ccc->notify_indicate = enable;
1738 }
1739
1740 bool get_ccc_notify_indicate(const struct gatt_db_attribute *ccc)
1741 {
1742         if (ccc)
1743                 return ccc->notify_indicate;
1744
1745         return false;
1746 }
1747
1748 void set_ccc_unicast_address(const struct gatt_db_attribute *ccc,
1749                                                         const char *address)
1750 {
1751         if (ccc)
1752                 str2ba(address, (bdaddr_t *)&ccc->unicast_addr);
1753 }
1754
1755 bdaddr_t *get_ccc_unicast_address(const struct gatt_db_attribute *ccc)
1756 {
1757         if (ccc)
1758                 return &ccc->unicast_addr;
1759         return NULL;
1760 }
1761 #endif