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
31 #include <bluetooth/bluetooth.h>
32 #include <bluetooth/uuid.h>
39 #define GATT_TIMEOUT 30
53 GDestroyNotify destroy;
54 gpointer destroy_user_data;
65 GAttribResultFunc func;
67 GDestroyNotify notify;
73 GAttribNotifyFunc func;
75 GDestroyNotify notify;
78 static guint8 opcode2expected(guint8 opcode)
82 return ATT_OP_MTU_RESP;
84 case ATT_OP_FIND_INFO_REQ:
85 return ATT_OP_FIND_INFO_RESP;
87 case ATT_OP_FIND_BY_TYPE_REQ:
88 return ATT_OP_FIND_BY_TYPE_RESP;
90 case ATT_OP_READ_BY_TYPE_REQ:
91 return ATT_OP_READ_BY_TYPE_RESP;
94 return ATT_OP_READ_RESP;
96 case ATT_OP_READ_BLOB_REQ:
97 return ATT_OP_READ_BLOB_RESP;
99 case ATT_OP_READ_MULTI_REQ:
100 return ATT_OP_READ_MULTI_RESP;
102 case ATT_OP_READ_BY_GROUP_REQ:
103 return ATT_OP_READ_BY_GROUP_RESP;
105 case ATT_OP_WRITE_REQ:
106 return ATT_OP_WRITE_RESP;
108 case ATT_OP_PREP_WRITE_REQ:
109 return ATT_OP_PREP_WRITE_RESP;
111 case ATT_OP_EXEC_WRITE_REQ:
112 return ATT_OP_EXEC_WRITE_RESP;
114 case ATT_OP_HANDLE_IND:
115 return ATT_OP_HANDLE_CNF;
121 static gboolean is_response(guint8 opcode)
125 case ATT_OP_MTU_RESP:
126 case ATT_OP_FIND_INFO_RESP:
127 case ATT_OP_FIND_BY_TYPE_RESP:
128 case ATT_OP_READ_BY_TYPE_RESP:
129 case ATT_OP_READ_RESP:
130 case ATT_OP_READ_BLOB_RESP:
131 case ATT_OP_READ_MULTI_RESP:
132 case ATT_OP_READ_BY_GROUP_RESP:
133 case ATT_OP_WRITE_RESP:
134 case ATT_OP_PREP_WRITE_RESP:
135 case ATT_OP_EXEC_WRITE_RESP:
136 case ATT_OP_HANDLE_CNF:
143 GAttrib *g_attrib_ref(GAttrib *attrib)
148 g_atomic_int_inc(&attrib->refs);
150 DBG("%p: ref=%d", attrib, attrib->refs);
155 static void command_destroy(struct command *cmd)
158 cmd->notify(cmd->user_data);
164 static void event_destroy(struct event *evt)
167 evt->notify(evt->user_data);
172 static void attrib_destroy(GAttrib *attrib)
177 while ((c = g_queue_pop_head(attrib->requests)))
180 while ((c = g_queue_pop_head(attrib->responses)))
183 g_queue_free(attrib->requests);
184 attrib->requests = NULL;
186 g_queue_free(attrib->responses);
187 attrib->responses = NULL;
189 for (l = attrib->events; l; l = l->next)
190 event_destroy(l->data);
192 g_slist_free(attrib->events);
193 attrib->events = NULL;
195 if (attrib->timeout_watch > 0)
196 g_source_remove(attrib->timeout_watch);
198 if (attrib->write_watch > 0)
199 g_source_remove(attrib->write_watch);
201 if (attrib->read_watch > 0)
202 g_source_remove(attrib->read_watch);
205 g_io_channel_unref(attrib->io);
210 attrib->destroy(attrib->destroy_user_data);
215 void g_attrib_unref(GAttrib *attrib)
222 ret = g_atomic_int_dec_and_test(&attrib->refs);
224 DBG("%p: ref=%d", attrib, attrib->refs);
229 attrib_destroy(attrib);
232 GIOChannel *g_attrib_get_channel(GAttrib *attrib)
240 gboolean g_attrib_set_destroy_function(GAttrib *attrib,
241 GDestroyNotify destroy, gpointer user_data)
246 attrib->destroy = destroy;
247 attrib->destroy_user_data = user_data;
252 static gboolean disconnect_timeout(gpointer data)
254 struct _GAttrib *attrib = data;
257 g_attrib_ref(attrib);
259 c = g_queue_pop_head(attrib->requests);
264 c->func(ATT_ECODE_TIMEOUT, NULL, 0, c->user_data);
268 while ((c = g_queue_pop_head(attrib->requests))) {
270 c->func(ATT_ECODE_ABORTED, NULL, 0, c->user_data);
275 attrib->stale = TRUE;
277 g_attrib_unref(attrib);
282 static gboolean can_write_data(GIOChannel *io, GIOCondition cond,
285 struct _GAttrib *attrib = data;
295 if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
298 queue = attrib->responses;
299 cmd = g_queue_peek_head(queue);
301 queue = attrib->requests;
302 cmd = g_queue_peek_head(queue);
308 * Verify that we didn't already send this command. This can only
309 * happen with elementes from attrib->requests.
314 iostat = g_io_channel_write_chars(io, (gchar *) cmd->pdu, cmd->len,
316 if (iostat != G_IO_STATUS_NORMAL)
319 if (cmd->expected == 0) {
320 g_queue_pop_head(queue);
321 command_destroy(cmd);
328 if (attrib->timeout_watch == 0)
329 attrib->timeout_watch = g_timeout_add_seconds(GATT_TIMEOUT,
330 disconnect_timeout, attrib);
335 static void destroy_sender(gpointer data)
337 struct _GAttrib *attrib = data;
339 attrib->write_watch = 0;
340 g_attrib_unref(attrib);
343 static void wake_up_sender(struct _GAttrib *attrib)
345 if (attrib->write_watch > 0)
348 attrib = g_attrib_ref(attrib);
349 attrib->write_watch = g_io_add_watch_full(attrib->io,
350 G_PRIORITY_DEFAULT, G_IO_OUT,
351 can_write_data, attrib, destroy_sender);
354 static gboolean received_data(GIOChannel *io, GIOCondition cond, gpointer data)
356 struct _GAttrib *attrib = data;
357 struct command *cmd = NULL;
359 uint8_t buf[512], status;
362 gboolean norequests, noresponses;
367 if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
368 attrib->read_watch = 0;
372 memset(buf, 0, sizeof(buf));
374 iostat = g_io_channel_read_chars(io, (gchar *) buf, sizeof(buf),
376 if (iostat != G_IO_STATUS_NORMAL) {
377 status = ATT_ECODE_IO;
381 for (l = attrib->events; l; l = l->next) {
382 struct event *evt = l->data;
384 if (evt->expected == buf[0] ||
385 evt->expected == GATTRIB_ALL_EVENTS ||
386 (is_response(buf[0]) == FALSE &&
387 evt->expected == GATTRIB_ALL_REQS))
388 evt->func(buf, len, evt->user_data);
391 if (is_response(buf[0]) == FALSE)
394 if (attrib->timeout_watch > 0) {
395 g_source_remove(attrib->timeout_watch);
396 attrib->timeout_watch = 0;
399 cmd = g_queue_pop_head(attrib->requests);
401 /* Keep the watch if we have events to report */
402 return attrib->events != NULL;
405 if (buf[0] == ATT_OP_ERROR) {
410 if (cmd->expected != buf[0]) {
411 status = ATT_ECODE_IO;
418 norequests = attrib->requests == NULL ||
419 g_queue_is_empty(attrib->requests);
420 noresponses = attrib->responses == NULL ||
421 g_queue_is_empty(attrib->responses);
425 cmd->func(status, buf, len, cmd->user_data);
427 command_destroy(cmd);
430 if (!norequests || !noresponses)
431 wake_up_sender(attrib);
436 GAttrib *g_attrib_new(GIOChannel *io)
438 struct _GAttrib *attrib;
444 g_io_channel_set_encoding(io, NULL, NULL);
445 g_io_channel_set_buffered(io, FALSE);
447 bt_io_get(io, BT_IO_L2CAP, &gerr,
448 BT_IO_OPT_IMTU, &imtu,
453 error("%s", gerr->message);
458 attrib = g_try_new0(struct _GAttrib, 1);
462 att_mtu = (cid == ATT_CID) ? ATT_DEFAULT_LE_MTU : imtu;
464 attrib->buf = g_malloc0(att_mtu);
465 attrib->buflen = att_mtu;
467 attrib->io = g_io_channel_ref(io);
468 attrib->requests = g_queue_new();
469 attrib->responses = g_queue_new();
471 attrib->read_watch = g_io_add_watch(attrib->io,
472 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
473 received_data, attrib);
475 return g_attrib_ref(attrib);
478 guint g_attrib_send(GAttrib *attrib, guint id, guint8 opcode,
479 const guint8 *pdu, guint16 len, GAttribResultFunc func,
480 gpointer user_data, GDestroyNotify notify)
488 c = g_try_new0(struct command, 1);
493 c->expected = opcode2expected(opcode);
494 c->pdu = g_malloc(len);
495 memcpy(c->pdu, pdu, len);
498 c->user_data = user_data;
501 if (is_response(opcode))
502 queue = attrib->responses;
504 queue = attrib->requests;
508 if (!is_response(opcode))
509 g_queue_push_head(queue, c);
511 /* Don't re-order responses even if an ID is given */
512 g_queue_push_tail(queue, c);
514 c->id = ++attrib->next_cmd_id;
515 g_queue_push_tail(queue, c);
519 * If a command was added to the queue and it was empty before, wake up
520 * the sender. If the sender was already woken up by the second queue,
521 * wake_up_sender will just return.
523 if (g_queue_get_length(queue) == 1)
524 wake_up_sender(attrib);
529 static gint command_cmp_by_id(gconstpointer a, gconstpointer b)
531 const struct command *cmd = a;
532 guint id = GPOINTER_TO_UINT(b);
537 gboolean g_attrib_cancel(GAttrib *attrib, guint id)
546 queue = attrib->requests;
548 l = g_queue_find_custom(queue, GUINT_TO_POINTER(id),
551 queue = attrib->responses;
554 l = g_queue_find_custom(queue, GUINT_TO_POINTER(id),
563 if (cmd == g_queue_peek_head(queue) && cmd->sent)
566 g_queue_remove(queue, cmd);
567 command_destroy(cmd);
573 static gboolean cancel_all_per_queue(GQueue *queue)
575 struct command *c, *head = NULL;
576 gboolean first = TRUE;
581 while ((c = g_queue_pop_head(queue))) {
582 if (first && c->sent) {
583 /* If the command was sent ignore its callback ... */
594 /* ... and put it back in the queue */
595 g_queue_push_head(queue, head);
601 gboolean g_attrib_cancel_all(GAttrib *attrib)
608 ret = cancel_all_per_queue(attrib->requests);
609 ret = cancel_all_per_queue(attrib->responses) && ret;
614 gboolean g_attrib_set_debug(GAttrib *attrib,
615 GAttribDebugFunc func, gpointer user_data)
620 uint8_t *g_attrib_get_buffer(GAttrib *attrib, int *len)
625 *len = attrib->buflen;
630 gboolean g_attrib_set_mtu(GAttrib *attrib, int mtu)
632 if (mtu < ATT_DEFAULT_LE_MTU)
635 attrib->buf = g_realloc(attrib->buf, mtu);
637 attrib->buflen = mtu;
642 guint g_attrib_register(GAttrib *attrib, guint8 opcode,
643 GAttribNotifyFunc func, gpointer user_data,
644 GDestroyNotify notify)
646 static guint next_evt_id = 0;
649 event = g_try_new0(struct event, 1);
653 event->expected = opcode;
655 event->user_data = user_data;
656 event->notify = notify;
657 event->id = ++next_evt_id;
659 attrib->events = g_slist_append(attrib->events, event);
664 static gint event_cmp_by_id(gconstpointer a, gconstpointer b)
666 const struct event *evt = a;
667 guint id = GPOINTER_TO_UINT(b);
672 gboolean g_attrib_is_encrypted(GAttrib *attrib)
674 BtIOSecLevel sec_level;
676 if (!bt_io_get(attrib->io, BT_IO_L2CAP, NULL,
677 BT_IO_OPT_SEC_LEVEL, &sec_level,
681 return sec_level > BT_IO_SEC_LOW;
684 gboolean g_attrib_unregister(GAttrib *attrib, guint id)
689 l = g_slist_find_custom(attrib->events, GUINT_TO_POINTER(id),
696 attrib->events = g_slist_remove(attrib->events, evt);
699 evt->notify(evt->user_data);
706 gboolean g_attrib_unregister_all(GAttrib *attrib)
710 if (attrib->events == NULL)
713 for (l = attrib->events; l; l = l->next) {
714 struct event *evt = l->data;
717 evt->notify(evt->user_data);
722 g_slist_free(attrib->events);
723 attrib->events = NULL;