3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-2009 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program 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
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
35 #include <bluetooth/bluetooth.h>
36 #include <bluetooth/hci.h>
37 #include <bluetooth/hci_lib.h>
38 #include <bluetooth/rfcomm.h>
39 #include <bluetooth/l2cap.h>
40 #include <bluetooth/sco.h>
41 #include <bluetooth/sdp.h>
42 #include <bluetooth/sdp_lib.h>
46 #include "glib-helper.h"
48 /* Number of seconds to keep a sdp_session_t in the cache */
49 #define CACHE_TIMEOUT 2
51 struct cached_sdp_session {
54 sdp_session_t *session;
58 static GSList *cached_sdp_sessions = NULL;
67 static gboolean cached_session_expired(gpointer user_data)
69 struct cached_sdp_session *cached = user_data;
71 cached_sdp_sessions = g_slist_remove(cached_sdp_sessions, cached);
73 sdp_close(cached->session);
80 static sdp_session_t *get_sdp_session(const bdaddr_t *src, const bdaddr_t *dst)
84 for (l = cached_sdp_sessions; l != NULL; l = l->next) {
85 struct cached_sdp_session *c = l->data;
86 sdp_session_t *session;
88 if (bacmp(&c->src, src) || bacmp(&c->dst, dst))
91 g_source_remove(c->timer);
95 cached_sdp_sessions = g_slist_remove(cached_sdp_sessions, c);
101 return sdp_connect(src, dst, SDP_NON_BLOCKING);
104 static void cache_sdp_session(bdaddr_t *src, bdaddr_t *dst,
105 sdp_session_t *session)
107 struct cached_sdp_session *cached;
109 cached = g_new0(struct cached_sdp_session, 1);
111 bacpy(&cached->src, src);
112 bacpy(&cached->dst, dst);
114 cached->session = session;
116 cached_sdp_sessions = g_slist_append(cached_sdp_sessions, cached);
118 cached->timer = g_timeout_add_seconds(CACHE_TIMEOUT,
119 cached_session_expired,
123 int set_nonblocking(int fd)
127 arg = fcntl(fd, F_GETFL);
131 /* Return if already nonblocking */
132 if (arg & O_NONBLOCK)
136 if (fcntl(fd, F_SETFL, arg) < 0)
142 struct search_context {
145 sdp_session_t *session;
147 bt_destroy_t destroy;
153 static GSList *context_list = NULL;
155 static void search_context_cleanup(struct search_context *ctxt)
157 context_list = g_slist_remove(context_list, ctxt);
160 ctxt->destroy(ctxt->user_data);
165 static void search_completed_cb(uint8_t type, uint16_t status,
166 uint8_t *rsp, size_t size, void *user_data)
168 struct search_context *ctxt = user_data;
169 sdp_list_t *recs = NULL;
170 int scanned, seqlen = 0, bytesleft = size;
174 if (status || type != SDP_SVC_SEARCH_ATTR_RSP) {
179 scanned = sdp_extract_seqtype(rsp, bytesleft, &dataType, &seqlen);
180 if (!scanned || !seqlen)
184 bytesleft -= scanned;
190 rec = sdp_extract_pdu(rsp, bytesleft, &recsize);
195 sdp_record_free(rec);
201 bytesleft -= recsize;
203 recs = sdp_list_append(recs, rec);
204 } while (scanned < (ssize_t) size && bytesleft > 0);
207 cache_sdp_session(&ctxt->src, &ctxt->dst, ctxt->session);
210 ctxt->cb(recs, err, ctxt->user_data);
213 sdp_list_free(recs, (sdp_free_func_t) sdp_record_free);
215 search_context_cleanup(ctxt);
218 static gboolean search_process_cb(GIOChannel *chan,
219 GIOCondition cond, void *user_data)
221 struct search_context *ctxt = user_data;
224 if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
229 if (sdp_process(ctxt->session) < 0)
236 sdp_close(ctxt->session);
237 ctxt->session = NULL;
240 ctxt->cb(NULL, err, ctxt->user_data);
242 search_context_cleanup(ctxt);
248 static gboolean connect_watch(GIOChannel *chan, GIOCondition cond, gpointer user_data)
250 struct search_context *ctxt = user_data;
251 sdp_list_t *search, *attrids;
252 uint32_t range = 0x0000ffff;
256 sk = g_io_channel_unix_get_fd(chan);
260 if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
268 if (sdp_set_notify(ctxt->session, search_completed_cb, ctxt) < 0) {
273 search = sdp_list_append(NULL, &ctxt->uuid);
274 attrids = sdp_list_append(NULL, &range);
275 if (sdp_service_search_attr_async(ctxt->session,
276 search, SDP_ATTR_REQ_RANGE, attrids) < 0) {
277 sdp_list_free(attrids, NULL);
278 sdp_list_free(search, NULL);
283 sdp_list_free(attrids, NULL);
284 sdp_list_free(search, NULL);
286 /* Set callback responsible for update the internal SDP transaction */
287 ctxt->io_id = g_io_add_watch(chan,
288 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
289 search_process_cb, ctxt);
293 sdp_close(ctxt->session);
294 ctxt->session = NULL;
297 ctxt->cb(NULL, -err, ctxt->user_data);
299 search_context_cleanup(ctxt);
304 static int create_search_context(struct search_context **ctxt,
305 const bdaddr_t *src, const bdaddr_t *dst,
314 s = get_sdp_session(src, dst);
318 *ctxt = g_try_malloc0(sizeof(struct search_context));
324 bacpy(&(*ctxt)->src, src);
325 bacpy(&(*ctxt)->dst, dst);
326 (*ctxt)->session = s;
327 (*ctxt)->uuid = *uuid;
329 chan = g_io_channel_unix_new(sdp_get_socket(s));
330 (*ctxt)->io_id = g_io_add_watch(chan,
331 G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
332 connect_watch, *ctxt);
333 g_io_channel_unref(chan);
338 int bt_search_service(const bdaddr_t *src, const bdaddr_t *dst,
339 uuid_t *uuid, bt_callback_t cb, void *user_data,
340 bt_destroy_t destroy)
342 struct search_context *ctxt = NULL;
348 err = create_search_context(&ctxt, src, dst, uuid);
353 ctxt->destroy = destroy;
354 ctxt->user_data = user_data;
356 context_list = g_slist_append(context_list, ctxt);
361 int bt_discover_services(const bdaddr_t *src, const bdaddr_t *dst,
362 bt_callback_t cb, void *user_data, bt_destroy_t destroy)
366 sdp_uuid16_create(&uuid, PUBLIC_BROWSE_GROUP);
368 return bt_search_service(src, dst, &uuid, cb, user_data, destroy);
371 static int find_by_bdaddr(const void *data, const void *user_data)
373 const struct search_context *ctxt = data, *search = user_data;
375 return (bacmp(&ctxt->dst, &search->dst) &&
376 bacmp(&ctxt->src, &search->src));
379 int bt_cancel_discovery(const bdaddr_t *src, const bdaddr_t *dst)
381 struct search_context search, *ctxt;
384 memset(&search, 0, sizeof(search));
385 bacpy(&search.src, src);
386 bacpy(&search.dst, dst);
388 /* Ongoing SDP Discovery */
389 match = g_slist_find_custom(context_list, &search, find_by_bdaddr);
398 g_source_remove(ctxt->io_id);
401 sdp_close(ctxt->session);
403 search_context_cleanup(ctxt);
407 char *bt_uuid2string(uuid_t *uuid)
412 unsigned short data1;
413 unsigned short data2;
414 unsigned short data3;
416 unsigned short data5;
421 switch (uuid->type) {
423 sdp_uuid16_to_uuid128(&uuid128, uuid);
426 sdp_uuid32_to_uuid128(&uuid128, uuid);
429 memcpy(&uuid128, uuid, sizeof(uuid_t));
432 /* Type of UUID unknown */
436 memcpy(&data0, &uuid128.value.uuid128.data[0], 4);
437 memcpy(&data1, &uuid128.value.uuid128.data[4], 2);
438 memcpy(&data2, &uuid128.value.uuid128.data[6], 2);
439 memcpy(&data3, &uuid128.value.uuid128.data[8], 2);
440 memcpy(&data4, &uuid128.value.uuid128.data[10], 4);
441 memcpy(&data5, &uuid128.value.uuid128.data[14], 2);
443 str = g_try_malloc0(MAX_LEN_UUID_STR);
447 sprintf(str, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
448 g_ntohl(data0), g_ntohs(data1),
449 g_ntohs(data2), g_ntohs(data3),
450 g_ntohl(data4), g_ntohs(data5));
459 { "vcp", VIDEO_CONF_SVCLASS_ID },
460 { "pbap", PBAP_SVCLASS_ID },
461 { "sap", SAP_SVCLASS_ID },
462 { "ftp", OBEX_FILETRANS_SVCLASS_ID },
463 { "bpp", BASIC_PRINTING_SVCLASS_ID },
464 { "bip", IMAGING_SVCLASS_ID },
465 { "synch", IRMC_SYNC_SVCLASS_ID },
466 { "dun", DIALUP_NET_SVCLASS_ID },
467 { "opp", OBEX_OBJPUSH_SVCLASS_ID },
468 { "fax", FAX_SVCLASS_ID },
469 { "spp", SERIAL_PORT_SVCLASS_ID },
470 { "hsp", HEADSET_SVCLASS_ID },
471 { "hfp", HANDSFREE_SVCLASS_ID },
475 uint16_t bt_name2class(const char *pattern)
479 for (i = 0; bt_services[i].name; i++) {
480 if (strcasecmp(bt_services[i].name, pattern) == 0)
481 return bt_services[i].class;
487 static inline gboolean is_uuid128(const char *string)
489 return (strlen(string) == 36 &&
496 char *bt_name2string(const char *pattern)
502 /* UUID 128 string format */
503 if (is_uuid128(pattern))
504 return g_strdup(pattern);
506 /* Friendly service name format */
507 uuid16 = bt_name2class(pattern);
512 uuid16 = strtol(pattern, NULL, 16);
513 for (i = 0; bt_services[i].class; i++) {
514 if (bt_services[i].class == uuid16)
521 sdp_uuid16_create(&uuid, uuid16);
523 return bt_uuid2string(&uuid);
526 int bt_string2uuid(uuid_t *uuid, const char *string)
528 uint32_t data0, data4;
529 uint16_t data1, data2, data3, data5;
531 if (is_uuid128(string) &&
532 sscanf(string, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
533 &data0, &data1, &data2, &data3, &data4, &data5) == 6) {
536 data0 = g_htonl(data0);
537 data1 = g_htons(data1);
538 data2 = g_htons(data2);
539 data3 = g_htons(data3);
540 data4 = g_htonl(data4);
541 data5 = g_htons(data5);
543 memcpy(&val[0], &data0, 4);
544 memcpy(&val[4], &data1, 2);
545 memcpy(&val[6], &data2, 2);
546 memcpy(&val[8], &data3, 2);
547 memcpy(&val[10], &data4, 4);
548 memcpy(&val[14], &data5, 2);
550 sdp_uuid128_create(uuid, val);
554 uint16_t class = bt_name2class(string);
556 sdp_uuid16_create(uuid, class);
564 gchar *bt_list2string(GSList *list)
572 str = g_strdup((const gchar *) list->data);
574 for (l = list->next; l; l = l->next) {
575 tmp = g_strconcat(str, " " , (const gchar *) l->data, NULL);
583 GSList *bt_string2list(const gchar *str)
592 /* FIXME: eglib doesn't support g_strsplit */
593 uuids = g_strsplit(str, " ", 0);
598 l = g_slist_append(l, uuids[i]);
607 static gboolean hci_event_watch(GIOChannel *io,
608 GIOCondition cond, gpointer user_data)
610 unsigned char buf[HCI_MAX_EVENT_SIZE], *body;
611 struct hci_cmd_data *cmd = user_data;
612 evt_cmd_status *evt_status;
613 evt_auth_complete *evt_auth;
614 evt_encrypt_change *evt_enc;
616 set_conn_encrypt_cp cp;
619 uint8_t status = HCI_OE_POWER_OFF;
621 if (cond & G_IO_NVAL) {
622 cmd->cb(status, cmd->caller_data);
626 if (cond & (G_IO_ERR | G_IO_HUP))
629 dd = g_io_channel_unix_get_fd(io);
631 if (read(dd, buf, sizeof(buf)) < 0)
634 hdr = (hci_event_hdr *) (buf + 1);
635 body = buf + (1 + HCI_EVENT_HDR_SIZE);
639 evt_status = (evt_cmd_status *) body;
640 ocf = cmd_opcode_ocf(evt_status->opcode);
644 case OCF_AUTH_REQUESTED:
645 case OCF_SET_CONN_ENCRYPT:
646 if (evt_status->status != 0) {
647 /* Baseband rejected command */
648 status = evt_status->status;
655 /* Wait for the next event */
657 case EVT_AUTH_COMPLETE:
658 evt_auth = (evt_auth_complete *) body;
659 if (evt_auth->handle != cmd->handle) {
664 if (evt_auth->status != 0x00) {
665 status = evt_auth->status;
666 /* Abort encryption */
670 memset(&cp, 0, sizeof(cp));
671 cp.handle = cmd->handle;
674 cmd->ocf = OCF_SET_CONN_ENCRYPT;
676 if (hci_send_cmd(dd, OGF_LINK_CTL, OCF_SET_CONN_ENCRYPT,
677 SET_CONN_ENCRYPT_CP_SIZE, &cp) < 0) {
678 status = HCI_COMMAND_DISALLOWED;
681 /* Wait for encrypt change event */
683 case EVT_ENCRYPT_CHANGE:
684 evt_enc = (evt_encrypt_change *) body;
685 if (evt_enc->handle != cmd->handle)
688 /* Procedure finished: reporting status */
689 status = evt_enc->status;
697 cmd->cb(status, cmd->caller_data);
698 g_io_channel_shutdown(io, TRUE, NULL);
703 int bt_acl_encrypt(const bdaddr_t *src, const bdaddr_t *dst,
704 bt_hci_result_t cb, gpointer user_data)
707 struct hci_cmd_data *cmd;
708 struct hci_conn_info_req *cr;
709 auth_requested_cp cp;
710 struct hci_filter nf;
716 ba2str(src, src_addr);
717 dev_id = hci_devid(src_addr);
721 dd = hci_open_dev(dev_id);
725 cr = g_malloc0(sizeof(*cr) + sizeof(struct hci_conn_info));
727 bacpy(&cr->bdaddr, dst);
729 err = ioctl(dd, HCIGETCONNINFO, cr);
730 link_mode = cr->conn_info->link_mode;
731 handle = cr->conn_info->handle;
739 if (link_mode & HCI_LM_ENCRYPT) {
740 /* Already encrypted */
745 memset(&cp, 0, sizeof(cp));
746 cp.handle = htobs(handle);
748 if (hci_send_cmd(dd, OGF_LINK_CTL, OCF_AUTH_REQUESTED,
749 AUTH_REQUESTED_CP_SIZE, &cp) < 0) {
754 cmd = g_new0(struct hci_cmd_data, 1);
755 cmd->handle = handle;
756 cmd->ocf = OCF_AUTH_REQUESTED;
758 cmd->caller_data = user_data;
760 hci_filter_clear(&nf);
761 hci_filter_set_ptype(HCI_EVENT_PKT, &nf);
762 hci_filter_set_event(EVT_CMD_STATUS, &nf);
763 hci_filter_set_event(EVT_AUTH_COMPLETE, &nf);
764 hci_filter_set_event(EVT_ENCRYPT_CHANGE, &nf);
766 if (setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0) {
771 io = g_io_channel_unix_new(dd);
772 g_io_channel_set_close_on_unref(io, FALSE);
773 g_io_add_watch_full(io, G_PRIORITY_DEFAULT,
774 G_IO_HUP | G_IO_ERR | G_IO_NVAL | G_IO_IN,
775 hci_event_watch, cmd, g_free);
776 g_io_channel_unref(io);