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
32 #include <bluetooth/uuid.h>
33 #include <bluetooth/sdp.h>
34 #include <bluetooth/sdp_lib.h>
36 #include "glib-compat.h"
42 struct discover_primary {
50 struct discover_char {
54 GSList *characteristics;
59 static void discover_primary_free(struct discover_primary *dp)
61 g_slist_free(dp->primaries);
62 g_attrib_unref(dp->attrib);
66 static void discover_char_free(struct discover_char *dc)
68 g_slist_free_full(dc->characteristics, g_free);
69 g_attrib_unref(dc->attrib);
74 static guint16 encode_discover_primary(uint16_t start, uint16_t end,
75 bt_uuid_t *uuid, uint8_t *pdu, size_t len)
80 bt_uuid16_create(&prim, GATT_PRIM_SVC_UUID);
83 /* Discover all primary services */
84 plen = enc_read_by_grp_req(start, end, &prim, pdu, len);
91 /* Discover primary service by service UUID */
93 if (uuid->type == BT_UUID16) {
94 u16 = htobs(uuid->value.u16);
98 htob128(&uuid->value.u128, &u128);
103 plen = enc_find_by_type_req(start, end, &prim, value, vlen,
110 static void primary_by_uuid_cb(guint8 status, const guint8 *ipdu,
111 guint16 iplen, gpointer user_data)
114 struct discover_primary *dp = user_data;
115 GSList *ranges, *last;
116 struct att_range *range;
122 err = status == ATT_ECODE_ATTR_NOT_FOUND ? 0 : status;
126 ranges = dec_find_by_type_resp(ipdu, iplen);
130 dp->primaries = g_slist_concat(dp->primaries, ranges);
132 last = g_slist_last(ranges);
135 if (range->end == 0xffff)
138 buf = g_attrib_get_buffer(dp->attrib, &buflen);
139 oplen = encode_discover_primary(range->end + 1, 0xffff, &dp->uuid,
145 g_attrib_send(dp->attrib, 0, buf[0], buf, oplen, primary_by_uuid_cb,
150 dp->cb(dp->primaries, err, dp->user_data);
151 discover_primary_free(dp);
154 static void primary_all_cb(guint8 status, const guint8 *ipdu, guint16 iplen,
157 struct discover_primary *dp = user_data;
158 struct att_data_list *list;
163 err = status == ATT_ECODE_ATTR_NOT_FOUND ? 0 : status;
167 list = dec_read_by_grp_resp(ipdu, iplen);
173 for (i = 0, end = 0; i < list->num; i++) {
174 const uint8_t *data = list->data[i];
175 struct att_primary *primary;
178 start = att_get_u16(&data[0]);
179 end = att_get_u16(&data[2]);
181 if (list->len == 6) {
182 bt_uuid_t uuid16 = att_get_uuid16(&data[4]);
183 bt_uuid_to_uuid128(&uuid16, &uuid);
184 } else if (list->len == 20) {
185 uuid = att_get_uuid128(&data[4]);
187 /* Skipping invalid data */
191 primary = g_try_new0(struct att_primary, 1);
193 err = ATT_ECODE_INSUFF_RESOURCES;
196 primary->start = start;
198 bt_uuid_to_string(&uuid, primary->uuid, sizeof(primary->uuid));
199 dp->primaries = g_slist_append(dp->primaries, primary);
202 att_data_list_free(list);
207 uint8_t *buf = g_attrib_get_buffer(dp->attrib, &buflen);
208 guint16 oplen = encode_discover_primary(end + 1, 0xffff, NULL,
211 g_attrib_send(dp->attrib, 0, buf[0], buf, oplen, primary_all_cb,
218 dp->cb(dp->primaries, err, dp->user_data);
219 discover_primary_free(dp);
222 guint gatt_discover_primary(GAttrib *attrib, bt_uuid_t *uuid, gatt_cb_t func,
225 struct discover_primary *dp;
227 uint8_t *buf = g_attrib_get_buffer(attrib, &buflen);
228 GAttribResultFunc cb;
231 plen = encode_discover_primary(0x0001, 0xffff, uuid, buf, buflen);
235 dp = g_try_new0(struct discover_primary, 1);
239 dp->attrib = g_attrib_ref(attrib);
241 dp->user_data = user_data;
245 cb = primary_by_uuid_cb;
249 return g_attrib_send(attrib, 0, buf[0], buf, plen, cb, dp, NULL);
252 static void char_discovered_cb(guint8 status, const guint8 *ipdu, guint16 iplen,
255 struct discover_char *dc = user_data;
256 struct att_data_list *list;
265 err = status == ATT_ECODE_ATTR_NOT_FOUND ? 0 : status;
269 list = dec_read_by_type_resp(ipdu, iplen);
275 for (i = 0; i < list->num; i++) {
276 uint8_t *value = list->data[i];
277 struct att_char *chars;
280 last = att_get_u16(value);
282 if (list->len == 7) {
283 bt_uuid_t uuid16 = att_get_uuid16(&value[5]);
284 bt_uuid_to_uuid128(&uuid16, &uuid);
286 uuid = att_get_uuid128(&value[5]);
288 chars = g_try_new0(struct att_char, 1);
290 err = ATT_ECODE_INSUFF_RESOURCES;
294 if (dc->uuid && bt_uuid_cmp(dc->uuid, &uuid))
297 chars->handle = last;
298 chars->properties = value[2];
299 chars->value_handle = att_get_u16(&value[3]);
300 bt_uuid_to_string(&uuid, chars->uuid, sizeof(chars->uuid));
301 dc->characteristics = g_slist_append(dc->characteristics,
305 att_data_list_free(list);
309 buf = g_attrib_get_buffer(dc->attrib, &buflen);
311 bt_uuid16_create(&uuid, GATT_CHARAC_UUID);
313 oplen = enc_read_by_type_req(last + 1, dc->end, &uuid, buf,
319 g_attrib_send(dc->attrib, 0, buf[0], buf, oplen,
320 char_discovered_cb, dc, NULL);
326 dc->cb(dc->characteristics, err, dc->user_data);
327 discover_char_free(dc);
330 guint gatt_discover_char(GAttrib *attrib, uint16_t start, uint16_t end,
331 bt_uuid_t *uuid, gatt_cb_t func,
335 uint8_t *buf = g_attrib_get_buffer(attrib, &buflen);
336 struct discover_char *dc;
340 bt_uuid16_create(&type_uuid, GATT_CHARAC_UUID);
342 plen = enc_read_by_type_req(start, end, &type_uuid, buf, buflen);
346 dc = g_try_new0(struct discover_char, 1);
350 dc->attrib = g_attrib_ref(attrib);
352 dc->user_data = user_data;
354 dc->uuid = g_memdup(uuid, sizeof(bt_uuid_t));
356 return g_attrib_send(attrib, 0, buf[0], buf, plen, char_discovered_cb,
360 guint gatt_read_char_by_uuid(GAttrib *attrib, uint16_t start, uint16_t end,
361 bt_uuid_t *uuid, GAttribResultFunc func,
365 uint8_t *buf = g_attrib_get_buffer(attrib, &buflen);
368 plen = enc_read_by_type_req(start, end, uuid, buf, buflen);
372 return g_attrib_send(attrib, 0, ATT_OP_READ_BY_TYPE_REQ,
373 buf, plen, func, user_data, NULL);
376 struct read_long_data {
378 GAttribResultFunc func;
387 static void read_long_destroy(gpointer user_data)
389 struct read_long_data *long_read = user_data;
391 if (g_atomic_int_dec_and_test(&long_read->ref) == FALSE)
394 if (long_read->buffer != NULL)
395 g_free(long_read->buffer);
400 static void read_blob_helper(guint8 status, const guint8 *rpdu, guint16 rlen,
403 struct read_long_data *long_read = user_data;
410 if (status != 0 || rlen == 1) {
415 tmp = g_try_realloc(long_read->buffer, long_read->size + rlen - 1);
418 status = ATT_ECODE_INSUFF_RESOURCES;
422 memcpy(&tmp[long_read->size], &rpdu[1], rlen - 1);
423 long_read->buffer = tmp;
424 long_read->size += rlen - 1;
426 buf = g_attrib_get_buffer(long_read->attrib, &buflen);
430 plen = enc_read_blob_req(long_read->handle, long_read->size - 1,
432 id = g_attrib_send(long_read->attrib, long_read->id,
433 ATT_OP_READ_BLOB_REQ, buf, plen,
434 read_blob_helper, long_read, read_long_destroy);
437 g_atomic_int_inc(&long_read->ref);
441 status = ATT_ECODE_IO;
444 long_read->func(status, long_read->buffer, long_read->size,
445 long_read->user_data);
448 static void read_char_helper(guint8 status, const guint8 *rpdu,
449 guint16 rlen, gpointer user_data)
451 struct read_long_data *long_read = user_data;
453 uint8_t *buf = g_attrib_get_buffer(long_read->attrib, &buflen);
457 if (status != 0 || rlen < buflen)
460 long_read->buffer = g_malloc(rlen);
462 if (long_read->buffer == NULL)
465 memcpy(long_read->buffer, rpdu, rlen);
466 long_read->size = rlen;
468 plen = enc_read_blob_req(long_read->handle, rlen - 1, buf, buflen);
469 id = g_attrib_send(long_read->attrib, long_read->id,
470 ATT_OP_READ_BLOB_REQ, buf, plen, read_blob_helper,
471 long_read, read_long_destroy);
474 g_atomic_int_inc(&long_read->ref);
478 status = ATT_ECODE_IO;
481 long_read->func(status, rpdu, rlen, long_read->user_data);
484 guint gatt_read_char(GAttrib *attrib, uint16_t handle, uint16_t offset,
485 GAttribResultFunc func, gpointer user_data)
491 struct read_long_data *long_read;
493 long_read = g_try_new0(struct read_long_data, 1);
495 if (long_read == NULL)
498 long_read->attrib = attrib;
499 long_read->func = func;
500 long_read->user_data = user_data;
501 long_read->handle = handle;
503 buf = g_attrib_get_buffer(attrib, &buflen);
505 plen = enc_read_blob_req(long_read->handle, offset, buf,
507 id = g_attrib_send(attrib, 0, ATT_OP_READ_BLOB_REQ, buf, plen,
508 read_blob_helper, long_read, read_long_destroy);
510 plen = enc_read_req(handle, buf, buflen);
511 id = g_attrib_send(attrib, 0, ATT_OP_READ_REQ, buf, plen,
512 read_char_helper, long_read, read_long_destroy);
518 g_atomic_int_inc(&long_read->ref);
525 guint gatt_write_char(GAttrib *attrib, uint16_t handle, uint8_t *value,
526 int vlen, GAttribResultFunc func, gpointer user_data)
532 buf = g_attrib_get_buffer(attrib, &buflen);
534 plen = enc_write_req(handle, value, vlen, buf, buflen);
536 plen = enc_write_cmd(handle, value, vlen, buf, buflen);
538 return g_attrib_send(attrib, 0, buf[0], buf, plen, func,
542 guint gatt_exchange_mtu(GAttrib *attrib, uint16_t mtu, GAttribResultFunc func,
549 buf = g_attrib_get_buffer(attrib, &buflen);
550 plen = enc_mtu_req(mtu, buf, buflen);
551 return g_attrib_send(attrib, 0, ATT_OP_MTU_REQ, buf, plen, func,
555 guint gatt_find_info(GAttrib *attrib, uint16_t start, uint16_t end,
556 GAttribResultFunc func, gpointer user_data)
562 buf = g_attrib_get_buffer(attrib, &buflen);
563 plen = enc_find_info_req(start, end, buf, buflen);
567 return g_attrib_send(attrib, 0, ATT_OP_FIND_INFO_REQ, buf, plen, func,
571 guint gatt_write_cmd(GAttrib *attrib, uint16_t handle, uint8_t *value, int vlen,
572 GDestroyNotify notify, gpointer user_data)
578 buf = g_attrib_get_buffer(attrib, &buflen);
579 plen = enc_write_cmd(handle, value, vlen, buf, buflen);
580 return g_attrib_send(attrib, 0, ATT_OP_WRITE_CMD, buf, plen, NULL,
584 static sdp_data_t *proto_seq_find(sdp_list_t *proto_list)
589 sdp_uuid16_create(&proto, ATT_UUID);
591 for (list = proto_list; list; list = list->next) {
593 for (p = list->data; p; p = p->next) {
594 sdp_data_t *seq = p->data;
595 if (seq && seq->dtd == SDP_UUID16 &&
596 sdp_uuid16_cmp(&proto, &seq->val.uuid) == 0)
604 static gboolean parse_proto_params(sdp_list_t *proto_list, uint16_t *psm,
605 uint16_t *start, uint16_t *end)
607 sdp_data_t *seq1, *seq2;
610 *psm = sdp_get_proto_port(proto_list, L2CAP_UUID);
612 /* Getting start and end handle */
613 seq1 = proto_seq_find(proto_list);
614 if (!seq1 || seq1->dtd != SDP_UINT16)
618 if (!seq2 || seq2->dtd != SDP_UINT16)
622 *start = seq1->val.uint16;
625 *end = seq2->val.uint16;
630 gboolean gatt_parse_record(const sdp_record_t *rec,
631 uuid_t *prim_uuid, uint16_t *psm,
632 uint16_t *start, uint16_t *end)
638 if (sdp_get_service_classes(rec, &list) < 0)
641 memcpy(&uuid, list->data, sizeof(uuid));
642 sdp_list_free(list, free);
644 if (sdp_get_access_protos(rec, &list) < 0)
647 ret = parse_proto_params(list, psm, start, end);
649 sdp_list_foreach(list, (sdp_list_func_t) sdp_list_free, NULL);
650 sdp_list_free(list, NULL);
652 /* FIXME: replace by bt_uuid_t after uuid_t/sdp code cleanup */
653 if (ret && prim_uuid)
654 memcpy(prim_uuid, &uuid, sizeof(uuid_t));