3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2010 Nokia Corporation
6 * Copyright (C) 2010 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include "lib/sdp_lib.h"
38 #include "src/shared/util.h"
43 struct discover_primary {
54 /* Used for the Included Services Discovery (ISD) procedure */
55 struct included_discovery {
60 uint16_t start_handle;
67 struct included_uuid_query {
68 struct included_discovery *isd;
69 struct gatt_included *included;
72 struct discover_char {
79 GSList *characteristics;
84 struct discover_desc {
96 static void discover_primary_unref(void *data)
98 struct discover_primary *dp = data;
105 g_slist_free_full(dp->primaries, g_free);
106 g_attrib_unref(dp->attrib);
110 static struct discover_primary *discover_primary_ref(
111 struct discover_primary *dp)
118 static struct included_discovery *isd_ref(struct included_discovery *isd)
120 __sync_fetch_and_add(&isd->refs, 1);
125 static void isd_unref(struct included_discovery *isd)
127 if (__sync_sub_and_fetch(&isd->refs, 1) > 0)
131 isd->cb(isd->err, NULL, isd->user_data);
133 isd->cb(isd->err, isd->includes, isd->user_data);
135 g_slist_free_full(isd->includes, g_free);
136 g_attrib_unref(isd->attrib);
140 static void discover_char_unref(void *data)
142 struct discover_char *dc = data;
149 g_slist_free_full(dc->characteristics, g_free);
150 g_attrib_unref(dc->attrib);
155 static struct discover_char *discover_char_ref(struct discover_char *dc)
162 static void discover_desc_unref(void *data)
164 struct discover_desc *dd = data;
171 g_slist_free_full(dd->descriptors, g_free);
172 g_attrib_unref(dd->attrib);
177 static struct discover_desc *discover_desc_ref(struct discover_desc *dd)
184 static void put_uuid_le(const bt_uuid_t *uuid, void *dst)
186 if (uuid->type == BT_UUID16)
187 put_le16(uuid->value.u16, dst);
188 #ifdef __TIZEN_PATCH__
189 else if (uuid->type == BT_UUID32)
190 put_le32(uuid->value.u32, dst);
193 /* Convert from 128-bit BE to LE */
194 bswap_128(&uuid->value.u128, dst);
197 static void get_uuid128(uint8_t type, const void *val, bt_uuid_t *uuid)
199 if (type == BT_UUID16) {
202 bt_uuid16_create(&uuid16, get_le16(val));
203 bt_uuid_to_uuid128(&uuid16, uuid);
204 #ifdef __TIZEN_PATCH__
205 } else if (type == BT_UUID32) {
208 bt_uuid32_create(&uuid32, get_le32(val));
209 bt_uuid_to_uuid128(&uuid32, uuid);
214 /* Convert from 128-bit LE to BE */
215 bswap_128(val, &u128);
216 bt_uuid128_create(uuid, u128);
220 static guint16 encode_discover_primary(uint16_t start, uint16_t end,
221 bt_uuid_t *uuid, uint8_t *pdu, size_t len)
226 bt_uuid16_create(&prim, GATT_PRIM_SVC_UUID);
229 /* Discover all primary services */
230 plen = enc_read_by_grp_req(start, end, &prim, pdu, len);
235 /* Discover primary service by service UUID */
236 put_uuid_le(uuid, value);
237 vlen = bt_uuid_len(uuid);
239 plen = enc_find_by_type_req(start, end, &prim, value, vlen,
246 static void primary_by_uuid_cb(guint8 status, const guint8 *ipdu,
247 guint16 iplen, gpointer user_data)
250 struct discover_primary *dp = user_data;
251 GSList *ranges, *last;
252 struct att_range *range;
259 err = status == ATT_ECODE_ATTR_NOT_FOUND ? 0 : status;
263 ranges = dec_find_by_type_resp(ipdu, iplen);
267 dp->primaries = g_slist_concat(dp->primaries, ranges);
269 last = g_slist_last(ranges);
272 if (range->end == 0xffff)
276 * If last handle is lower from previous start handle then it is smth
277 * wrong. Let's stop search, otherwise we might enter infinite loop.
279 if (range->end < dp->start) {
280 err = ATT_ECODE_UNLIKELY;
284 dp->start = range->end + 1;
286 buf = g_attrib_get_buffer(dp->attrib, &buflen);
287 oplen = encode_discover_primary(dp->start, 0xffff, &dp->uuid,
293 g_attrib_send(dp->attrib, dp->id, buf, oplen, primary_by_uuid_cb,
294 discover_primary_ref(dp), discover_primary_unref);
298 dp->cb(err, dp->primaries, dp->user_data);
301 static void primary_all_cb(guint8 status, const guint8 *ipdu, guint16 iplen,
304 struct discover_primary *dp = user_data;
305 struct att_data_list *list;
311 err = status == ATT_ECODE_ATTR_NOT_FOUND ? 0 : status;
315 list = dec_read_by_grp_resp(ipdu, iplen);
323 else if (list->len == 20)
326 att_data_list_free(list);
327 err = ATT_ECODE_INVALID_PDU;
331 for (i = 0, end = 0; i < list->num; i++) {
332 const uint8_t *data = list->data[i];
333 struct gatt_primary *primary;
336 start = get_le16(&data[0]);
337 end = get_le16(&data[2]);
339 get_uuid128(type, &data[4], &uuid128);
341 primary = g_try_new0(struct gatt_primary, 1);
343 att_data_list_free(list);
344 err = ATT_ECODE_INSUFF_RESOURCES;
347 primary->range.start = start;
348 primary->range.end = end;
349 bt_uuid_to_string(&uuid128, primary->uuid, sizeof(primary->uuid));
350 dp->primaries = g_slist_append(dp->primaries, primary);
353 att_data_list_free(list);
357 * If last handle is lower from previous start handle then it is smth
358 * wrong. Let's stop search, otherwise we might enter infinite loop.
360 if (end < dp->start) {
361 err = ATT_ECODE_UNLIKELY;
369 uint8_t *buf = g_attrib_get_buffer(dp->attrib, &buflen);
370 guint16 oplen = encode_discover_primary(dp->start, 0xffff, NULL,
374 g_attrib_send(dp->attrib, dp->id, buf, oplen, primary_all_cb,
375 discover_primary_ref(dp),
376 discover_primary_unref);
382 dp->cb(err, dp->primaries, dp->user_data);
385 guint gatt_discover_primary(GAttrib *attrib, bt_uuid_t *uuid, gatt_cb_t func,
388 struct discover_primary *dp;
390 uint8_t *buf = g_attrib_get_buffer(attrib, &buflen);
391 GAttribResultFunc cb;
394 plen = encode_discover_primary(0x0001, 0xffff, uuid, buf, buflen);
398 dp = g_try_new0(struct discover_primary, 1);
402 dp->attrib = g_attrib_ref(attrib);
404 dp->user_data = user_data;
409 cb = primary_by_uuid_cb;
413 dp->id = g_attrib_send(attrib, 0, buf, plen, cb,
414 discover_primary_ref(dp),
415 discover_primary_unref);
420 static void resolve_included_uuid_cb(uint8_t status, const uint8_t *pdu,
421 uint16_t len, gpointer user_data)
423 struct included_uuid_query *query = user_data;
424 struct included_discovery *isd = query->isd;
425 struct gatt_included *incl = query->included;
426 unsigned int err = status;
434 buf = g_attrib_get_buffer(isd->attrib, &buflen);
435 if (dec_read_resp(pdu, len, buf, buflen) != 16) {
440 get_uuid128(BT_UUID128, buf, &uuid128);
442 bt_uuid_to_string(&uuid128, incl->uuid, sizeof(incl->uuid));
443 isd->includes = g_slist_append(isd->includes, incl);
444 query->included = NULL;
451 static void inc_query_free(void *data)
453 struct included_uuid_query *query = data;
455 isd_unref(query->isd);
456 g_free(query->included);
460 static guint resolve_included_uuid(struct included_discovery *isd,
461 struct gatt_included *incl)
463 struct included_uuid_query *query;
465 uint8_t *buf = g_attrib_get_buffer(isd->attrib, &buflen);
466 guint16 oplen = enc_read_req(incl->range.start, buf, buflen);
468 query = g_new0(struct included_uuid_query, 1);
469 query->isd = isd_ref(isd);
470 query->included = incl;
472 return g_attrib_send(isd->attrib, query->isd->id, buf, oplen,
473 resolve_included_uuid_cb, query,
477 static struct gatt_included *included_from_buf(const uint8_t *buf, gsize len)
479 struct gatt_included *incl = g_new0(struct gatt_included, 1);
481 incl->handle = get_le16(&buf[0]);
482 incl->range.start = get_le16(&buf[2]);
483 incl->range.end = get_le16(&buf[4]);
488 get_uuid128(BT_UUID16, &buf[6], &uuid128);
489 bt_uuid_to_string(&uuid128, incl->uuid, sizeof(incl->uuid));
495 static void find_included_cb(uint8_t status, const uint8_t *pdu, uint16_t len,
498 static guint find_included(struct included_discovery *isd, uint16_t start)
502 uint8_t *buf = g_attrib_get_buffer(isd->attrib, &buflen);
505 bt_uuid16_create(&uuid, GATT_INCLUDE_UUID);
506 oplen = enc_read_by_type_req(start, isd->end_handle, &uuid,
509 /* If id != 0 it means we are in the middle of include search */
511 return g_attrib_send(isd->attrib, isd->id, buf, oplen,
512 find_included_cb, isd_ref(isd),
513 (GDestroyNotify) isd_unref);
515 /* This is first call from the gattrib user */
516 isd->id = g_attrib_send(isd->attrib, 0, buf, oplen, find_included_cb,
517 isd_ref(isd), (GDestroyNotify) isd_unref);
522 static void find_included_cb(uint8_t status, const uint8_t *pdu, uint16_t len,
525 struct included_discovery *isd = user_data;
526 uint16_t last_handle = isd->end_handle;
527 unsigned int err = status;
528 struct att_data_list *list;
531 if (err == ATT_ECODE_ATTR_NOT_FOUND)
537 list = dec_read_by_type_resp(pdu, len);
543 if (list->len != 6 && list->len != 8) {
545 att_data_list_free(list);
549 for (i = 0; i < list->num; i++) {
550 struct gatt_included *incl;
552 incl = included_from_buf(list->data[i], list->len);
553 last_handle = incl->handle;
555 /* 128 bit UUID, needs resolving */
556 if (list->len == 6) {
557 resolve_included_uuid(isd, incl);
561 isd->includes = g_slist_append(isd->includes, incl);
564 att_data_list_free(list);
567 * If last handle is lower from previous start handle then it is smth
568 * wrong. Let's stop search, otherwise we might enter infinite loop.
570 if (last_handle < isd->start_handle) {
571 isd->err = ATT_ECODE_UNLIKELY;
575 isd->start_handle = last_handle + 1;
577 if (last_handle < isd->end_handle)
578 find_included(isd, isd->start_handle);
585 unsigned int gatt_find_included(GAttrib *attrib, uint16_t start, uint16_t end,
586 gatt_cb_t func, gpointer user_data)
588 struct included_discovery *isd;
590 isd = g_new0(struct included_discovery, 1);
591 isd->attrib = g_attrib_ref(attrib);
592 isd->start_handle = start;
593 isd->end_handle = end;
595 isd->user_data = user_data;
597 return find_included(isd, start);
600 static void char_discovered_cb(guint8 status, const guint8 *ipdu, guint16 iplen,
603 struct discover_char *dc = user_data;
604 struct att_data_list *list;
605 unsigned int i, err = 0;
609 /* We have all the characteristic now, lets send it up */
610 if (status == ATT_ECODE_ATTR_NOT_FOUND) {
611 err = dc->characteristics ? 0 : status;
620 list = dec_read_by_type_resp(ipdu, iplen);
631 for (i = 0; i < list->num; i++) {
632 uint8_t *value = list->data[i];
633 struct gatt_char *chars;
636 last = get_le16(value);
638 get_uuid128(type, &value[5], &uuid128);
640 if (dc->uuid && bt_uuid_cmp(dc->uuid, &uuid128))
643 chars = g_try_new0(struct gatt_char, 1);
645 att_data_list_free(list);
646 err = ATT_ECODE_INSUFF_RESOURCES;
650 chars->handle = last;
651 chars->properties = value[2];
652 chars->value_handle = get_le16(&value[3]);
653 bt_uuid_to_string(&uuid128, chars->uuid, sizeof(chars->uuid));
654 dc->characteristics = g_slist_append(dc->characteristics,
658 att_data_list_free(list);
661 * If last handle is lower from previous start handle then it is smth
662 * wrong. Let's stop search, otherwise we might enter infinite loop.
664 if (last < dc->start) {
665 err = ATT_ECODE_UNLIKELY;
669 dc->start = last + 1;
671 if (last != 0 && (dc->start < dc->end)) {
677 buf = g_attrib_get_buffer(dc->attrib, &buflen);
679 bt_uuid16_create(&uuid, GATT_CHARAC_UUID);
681 oplen = enc_read_by_type_req(dc->start, dc->end, &uuid, buf,
687 g_attrib_send(dc->attrib, dc->id, buf, oplen,
688 char_discovered_cb, discover_char_ref(dc),
689 discover_char_unref);
695 dc->cb(err, dc->characteristics, dc->user_data);
698 guint gatt_discover_char(GAttrib *attrib, uint16_t start, uint16_t end,
699 bt_uuid_t *uuid, gatt_cb_t func,
703 uint8_t *buf = g_attrib_get_buffer(attrib, &buflen);
704 struct discover_char *dc;
708 bt_uuid16_create(&type_uuid, GATT_CHARAC_UUID);
710 plen = enc_read_by_type_req(start, end, &type_uuid, buf, buflen);
714 dc = g_try_new0(struct discover_char, 1);
718 dc->attrib = g_attrib_ref(attrib);
720 dc->user_data = user_data;
723 dc->uuid = g_memdup(uuid, sizeof(bt_uuid_t));
725 dc->id = g_attrib_send(attrib, 0, buf, plen, char_discovered_cb,
726 discover_char_ref(dc), discover_char_unref);
731 guint gatt_read_char_by_uuid(GAttrib *attrib, uint16_t start, uint16_t end,
732 bt_uuid_t *uuid, GAttribResultFunc func,
736 uint8_t *buf = g_attrib_get_buffer(attrib, &buflen);
739 plen = enc_read_by_type_req(start, end, uuid, buf, buflen);
743 return g_attrib_send(attrib, 0, buf, plen, func, user_data, NULL);
746 struct read_long_data {
748 GAttribResultFunc func;
757 static void read_long_destroy(gpointer user_data)
759 struct read_long_data *long_read = user_data;
761 if (__sync_sub_and_fetch(&long_read->ref, 1) > 0)
764 g_attrib_unref(long_read->attrib);
766 if (long_read->buffer != NULL)
767 g_free(long_read->buffer);
772 static void read_blob_helper(guint8 status, const guint8 *rpdu, guint16 rlen,
775 struct read_long_data *long_read = user_data;
782 if (status != 0 || rlen == 1) {
787 tmp = g_try_realloc(long_read->buffer, long_read->size + rlen - 1);
790 status = ATT_ECODE_INSUFF_RESOURCES;
794 memcpy(&tmp[long_read->size], &rpdu[1], rlen - 1);
795 long_read->buffer = tmp;
796 long_read->size += rlen - 1;
798 buf = g_attrib_get_buffer(long_read->attrib, &buflen);
802 plen = enc_read_blob_req(long_read->handle, long_read->size - 1,
804 id = g_attrib_send(long_read->attrib, long_read->id, buf, plen,
805 read_blob_helper, long_read, read_long_destroy);
808 __sync_fetch_and_add(&long_read->ref, 1);
812 status = ATT_ECODE_IO;
815 long_read->func(status, long_read->buffer, long_read->size,
816 long_read->user_data);
819 static void read_char_helper(guint8 status, const guint8 *rpdu,
820 guint16 rlen, gpointer user_data)
822 struct read_long_data *long_read = user_data;
824 uint8_t *buf = g_attrib_get_buffer(long_read->attrib, &buflen);
828 if (status != 0 || rlen < buflen)
831 long_read->buffer = g_malloc(rlen);
832 if (long_read->buffer == NULL) {
833 status = ATT_ECODE_INSUFF_RESOURCES;
837 memcpy(long_read->buffer, rpdu, rlen);
838 long_read->size = rlen;
840 plen = enc_read_blob_req(long_read->handle, rlen - 1, buf, buflen);
842 id = g_attrib_send(long_read->attrib, long_read->id, buf, plen,
843 read_blob_helper, long_read, read_long_destroy);
845 __sync_fetch_and_add(&long_read->ref, 1);
849 status = ATT_ECODE_IO;
852 long_read->func(status, rpdu, rlen, long_read->user_data);
855 #ifdef __TIZEN_PATCH__
856 guint gatt_read_char_by_offset(GAttrib *attrib, uint16_t handle, uint16_t offset,
857 GAttribResultFunc func, gpointer user_data)
863 struct read_long_data *long_read;
865 long_read = g_try_new0(struct read_long_data, 1);
867 if (long_read == NULL)
870 long_read->attrib = attrib;
871 long_read->func = func;
872 long_read->user_data = user_data;
873 long_read->handle = handle;
875 buf = g_attrib_get_buffer(attrib, &buflen);
877 plen = enc_read_blob_req(handle, offset, buf, buflen);
879 plen = enc_read_req(handle, buf, buflen);
881 id = g_attrib_send(attrib, 0, buf, plen, read_char_helper,
882 long_read, read_long_destroy);
886 __sync_fetch_and_add(&long_read->ref, 1);
894 guint gatt_read_char(GAttrib *attrib, uint16_t handle, GAttribResultFunc func,
901 struct read_long_data *long_read;
903 long_read = g_try_new0(struct read_long_data, 1);
905 if (long_read == NULL)
908 long_read->attrib = g_attrib_ref(attrib);
909 long_read->func = func;
910 long_read->user_data = user_data;
911 long_read->handle = handle;
913 buf = g_attrib_get_buffer(attrib, &buflen);
914 plen = enc_read_req(handle, buf, buflen);
915 id = g_attrib_send(attrib, 0, buf, plen, read_char_helper,
916 long_read, read_long_destroy);
918 g_attrib_unref(long_read->attrib);
921 __sync_fetch_and_add(&long_read->ref, 1);
928 struct write_long_data {
930 GAttribResultFunc func;
938 static guint execute_write(GAttrib *attrib, uint8_t flags,
939 GAttribResultFunc func, gpointer user_data)
945 buf = g_attrib_get_buffer(attrib, &buflen);
946 plen = enc_exec_write_req(flags, buf, buflen);
950 return g_attrib_send(attrib, 0, buf, plen, func, user_data, NULL);
953 static guint prepare_write(struct write_long_data *long_write);
955 static void prepare_write_cb(guint8 status, const guint8 *rpdu, guint16 rlen,
958 struct write_long_data *long_write = user_data;
961 long_write->func(status, rpdu, rlen, long_write->user_data);
965 /* Skip Prepare Write Response PDU header (5 bytes) */
966 long_write->offset += rlen - 5;
968 if (long_write->offset == long_write->vlen) {
969 execute_write(long_write->attrib, ATT_WRITE_ALL_PREP_WRITES,
970 long_write->func, long_write->user_data);
971 g_free(long_write->value);
977 prepare_write(long_write);
980 static guint prepare_write(struct write_long_data *long_write)
982 GAttrib *attrib = long_write->attrib;
983 uint16_t handle = long_write->handle;
984 uint16_t offset = long_write->offset;
985 uint8_t *buf, *value = long_write->value + offset;
986 size_t buflen, vlen = long_write->vlen - offset;
989 buf = g_attrib_get_buffer(attrib, &buflen);
991 plen = enc_prep_write_req(handle, offset, value, vlen, buf, buflen);
995 return g_attrib_send(attrib, 0, buf, plen, prepare_write_cb, long_write,
999 guint gatt_write_char(GAttrib *attrib, uint16_t handle, const uint8_t *value,
1000 size_t vlen, GAttribResultFunc func, gpointer user_data)
1004 struct write_long_data *long_write;
1006 buf = g_attrib_get_buffer(attrib, &buflen);
1008 /* Use Write Request if payload fits on a single transfer, including 3
1009 * bytes for the header. */
1010 if (vlen <= buflen - 3) {
1013 plen = enc_write_req(handle, value, vlen, buf, buflen);
1017 return g_attrib_send(attrib, 0, buf, plen, func, user_data,
1021 /* Write Long Characteristic Values */
1022 long_write = g_try_new0(struct write_long_data, 1);
1023 if (long_write == NULL)
1026 long_write->attrib = attrib;
1027 long_write->func = func;
1028 long_write->user_data = user_data;
1029 long_write->handle = handle;
1030 long_write->value = g_memdup(value, vlen);
1031 long_write->vlen = vlen;
1033 return prepare_write(long_write);
1036 guint gatt_execute_write(GAttrib *attrib, uint8_t flags,
1037 GAttribResultFunc func, gpointer user_data)
1039 return execute_write(attrib, flags, func, user_data);
1042 guint gatt_reliable_write_char(GAttrib *attrib, uint16_t handle,
1043 const uint8_t *value, size_t vlen,
1044 GAttribResultFunc func,
1051 buf = g_attrib_get_buffer(attrib, &buflen);
1053 plen = enc_prep_write_req(handle, 0, value, vlen, buf, buflen);
1057 return g_attrib_send(attrib, 0, buf, plen, func, user_data, NULL);
1060 guint gatt_exchange_mtu(GAttrib *attrib, uint16_t mtu, GAttribResultFunc func,
1067 buf = g_attrib_get_buffer(attrib, &buflen);
1068 plen = enc_mtu_req(mtu, buf, buflen);
1069 return g_attrib_send(attrib, 0, buf, plen, func, user_data, NULL);
1072 static void desc_discovered_cb(guint8 status, const guint8 *ipdu,
1073 guint16 iplen, gpointer user_data)
1075 struct discover_desc *dd = user_data;
1076 struct att_data_list *list;
1077 unsigned int i, err = 0;
1079 uint16_t last = 0xffff;
1081 gboolean uuid_found = FALSE;
1083 if (status == ATT_ECODE_ATTR_NOT_FOUND) {
1084 err = dd->descriptors ? 0 : status;
1093 list = dec_find_info_resp(ipdu, iplen, &format);
1099 if (format == ATT_FIND_INFO_RESP_FMT_16BIT)
1104 for (i = 0; i < list->num; i++) {
1105 uint8_t *value = list->data[i];
1106 struct gatt_desc *desc;
1109 last = get_le16(value);
1111 get_uuid128(type, &value[2], &uuid128);
1114 if (bt_uuid_cmp(dd->uuid, &uuid128))
1120 desc = g_try_new0(struct gatt_desc, 1);
1122 att_data_list_free(list);
1123 err = ATT_ECODE_INSUFF_RESOURCES;
1127 bt_uuid_to_string(&uuid128, desc->uuid, sizeof(desc->uuid));
1128 desc->handle = last;
1130 if (type == BT_UUID16)
1131 desc->uuid16 = get_le16(&value[2]);
1133 dd->descriptors = g_slist_append(dd->descriptors, desc);
1139 att_data_list_free(list);
1142 * If last handle is lower from previous start handle then it is smth
1143 * wrong. Let's stop search, otherwise we might enter infinite loop.
1145 if (last < dd->start) {
1146 err = ATT_ECODE_UNLIKELY;
1150 dd->start = last + 1;
1152 if (last < dd->end && !uuid_found) {
1157 buf = g_attrib_get_buffer(dd->attrib, &buflen);
1159 oplen = enc_find_info_req(dd->start, dd->end, buf, buflen);
1163 g_attrib_send(dd->attrib, dd->id, buf, oplen,
1164 desc_discovered_cb, discover_desc_ref(dd),
1165 discover_desc_unref);
1171 dd->cb(err, dd->descriptors, dd->user_data);
1174 guint gatt_discover_desc(GAttrib *attrib, uint16_t start, uint16_t end,
1175 bt_uuid_t *uuid, gatt_cb_t func,
1179 uint8_t *buf = g_attrib_get_buffer(attrib, &buflen);
1180 struct discover_desc *dd;
1183 plen = enc_find_info_req(start, end, buf, buflen);
1187 dd = g_try_new0(struct discover_desc, 1);
1191 dd->attrib = g_attrib_ref(attrib);
1193 dd->user_data = user_data;
1196 dd->uuid = g_memdup(uuid, sizeof(bt_uuid_t));
1198 dd->id = g_attrib_send(attrib, 0, buf, plen, desc_discovered_cb,
1199 discover_desc_ref(dd), discover_desc_unref);
1204 guint gatt_write_cmd(GAttrib *attrib, uint16_t handle, const uint8_t *value,
1205 int vlen, GDestroyNotify notify, gpointer user_data)
1211 buf = g_attrib_get_buffer(attrib, &buflen);
1212 plen = enc_write_cmd(handle, value, vlen, buf, buflen);
1213 return g_attrib_send(attrib, 0, buf, plen, NULL, user_data, notify);
1216 guint gatt_signed_write_cmd(GAttrib *attrib, uint16_t handle,
1217 const uint8_t *value, int vlen,
1218 struct bt_crypto *crypto,
1219 const uint8_t csrk[16],
1221 GDestroyNotify notify,
1228 buf = g_attrib_get_buffer(attrib, &buflen);
1229 plen = enc_signed_write_cmd(handle, value, vlen, crypto, csrk, sign_cnt,
1234 return g_attrib_send(attrib, 0, buf, plen, NULL, user_data, notify);
1237 static sdp_data_t *proto_seq_find(sdp_list_t *proto_list)
1242 sdp_uuid16_create(&proto, ATT_UUID);
1244 for (list = proto_list; list; list = list->next) {
1246 for (p = list->data; p; p = p->next) {
1247 sdp_data_t *seq = p->data;
1248 if (seq && seq->dtd == SDP_UUID16 &&
1249 sdp_uuid16_cmp(&proto, &seq->val.uuid) == 0)
1257 static gboolean parse_proto_params(sdp_list_t *proto_list, uint16_t *psm,
1258 uint16_t *start, uint16_t *end)
1260 sdp_data_t *seq1, *seq2;
1263 *psm = sdp_get_proto_port(proto_list, L2CAP_UUID);
1265 /* Getting start and end handle */
1266 seq1 = proto_seq_find(proto_list);
1267 if (!seq1 || seq1->dtd != SDP_UINT16)
1271 if (!seq2 || seq2->dtd != SDP_UINT16)
1275 *start = seq1->val.uint16;
1278 *end = seq2->val.uint16;
1283 gboolean gatt_parse_record(const sdp_record_t *rec,
1284 uuid_t *prim_uuid, uint16_t *psm,
1285 uint16_t *start, uint16_t *end)
1291 if (sdp_get_service_classes(rec, &list) < 0)
1294 memcpy(&uuid, list->data, sizeof(uuid));
1295 sdp_list_free(list, free);
1297 if (sdp_get_access_protos(rec, &list) < 0)
1300 ret = parse_proto_params(list, psm, start, end);
1302 sdp_list_foreach(list, (sdp_list_func_t) sdp_list_free, NULL);
1303 sdp_list_free(list, NULL);
1305 /* FIXME: replace by bt_uuid_t after uuid_t/sdp code cleanup */
1306 if (ret && prim_uuid)
1307 memcpy(prim_uuid, &uuid, sizeof(uuid_t));