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;
152
static GSList *context_list = NULL;
154
static void search_context_cleanup(struct search_context *ctxt)
156
context_list = g_slist_remove(context_list, ctxt);
159
ctxt->destroy(ctxt->user_data);
164
static void search_completed_cb(uint8_t type, uint16_t status,
165
uint8_t *rsp, size_t size, void *user_data)
167
struct search_context *ctxt = user_data;
168
sdp_list_t *recs = NULL;
169
int scanned, seqlen = 0, bytesleft = size;
173
if (status || type != SDP_SVC_SEARCH_ATTR_RSP) {
178
scanned = sdp_extract_seqtype(rsp, bytesleft, &dataType, &seqlen);
179
if (!scanned || !seqlen)
183
bytesleft -= scanned;
189
rec = sdp_extract_pdu(rsp, bytesleft, &recsize);
194
sdp_record_free(rec);
200
bytesleft -= recsize;
202
recs = sdp_list_append(recs, rec);
203
} while (scanned < (ssize_t) size && bytesleft > 0);
206
cache_sdp_session(&ctxt->src, &ctxt->dst, ctxt->session);
209
ctxt->cb(recs, err, ctxt->user_data);
212
sdp_list_free(recs, (sdp_free_func_t) sdp_record_free);
214
search_context_cleanup(ctxt);
217
static gboolean search_process_cb(GIOChannel *chan,
218
GIOCondition cond, void *user_data)
220
struct search_context *ctxt = user_data;
223
if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
228
if (sdp_process(ctxt->session) < 0)
235
sdp_close(ctxt->session);
238
ctxt->cb(NULL, err, ctxt->user_data);
240
search_context_cleanup(ctxt);
246
static gboolean connect_watch(GIOChannel *chan, GIOCondition cond, gpointer user_data)
248
struct search_context *ctxt = user_data;
249
sdp_list_t *search, *attrids;
250
uint32_t range = 0x0000ffff;
254
sk = g_io_channel_unix_get_fd(chan);
257
if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
265
if (sdp_set_notify(ctxt->session, search_completed_cb, ctxt) < 0) {
270
search = sdp_list_append(NULL, &ctxt->uuid);
271
attrids = sdp_list_append(NULL, &range);
272
if (sdp_service_search_attr_async(ctxt->session,
273
search, SDP_ATTR_REQ_RANGE, attrids) < 0) {
274
sdp_list_free(attrids, NULL);
275
sdp_list_free(search, NULL);
280
sdp_list_free(attrids, NULL);
281
sdp_list_free(search, NULL);
283
/* Set callback responsible for update the internal SDP transaction */
284
g_io_add_watch(chan, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
285
search_process_cb, ctxt);
289
sdp_close(ctxt->session);
292
ctxt->cb(NULL, -err, ctxt->user_data);
294
search_context_cleanup(ctxt);
299
static int create_search_context(struct search_context **ctxt,
300
const bdaddr_t *src, const bdaddr_t *dst,
309
s = get_sdp_session(src, dst);
313
*ctxt = g_try_malloc0(sizeof(struct search_context));
319
bacpy(&(*ctxt)->src, src);
320
bacpy(&(*ctxt)->dst, dst);
321
(*ctxt)->session = s;
322
(*ctxt)->uuid = *uuid;
324
chan = g_io_channel_unix_new(sdp_get_socket(s));
325
g_io_add_watch(chan, G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
326
connect_watch, *ctxt);
327
g_io_channel_unref(chan);
332
int bt_search_service(const bdaddr_t *src, const bdaddr_t *dst,
333
uuid_t *uuid, bt_callback_t cb, void *user_data,
334
bt_destroy_t destroy)
336
struct search_context *ctxt = NULL;
342
err = create_search_context(&ctxt, src, dst, uuid);
347
ctxt->destroy = destroy;
348
ctxt->user_data = user_data;
350
context_list = g_slist_append(context_list, ctxt);
355
int bt_discover_services(const bdaddr_t *src, const bdaddr_t *dst,
356
bt_callback_t cb, void *user_data, bt_destroy_t destroy)
360
sdp_uuid16_create(&uuid, PUBLIC_BROWSE_GROUP);
362
return bt_search_service(src, dst, &uuid, cb, user_data, destroy);
365
static int find_by_bdaddr(const void *data, const void *user_data)
367
const struct search_context *ctxt = data, *search = user_data;
369
return (bacmp(&ctxt->dst, &search->dst) &&
370
bacmp(&ctxt->src, &search->src));
373
int bt_cancel_discovery(const bdaddr_t *src, const bdaddr_t *dst)
375
struct search_context search, *ctxt;
378
memset(&search, 0, sizeof(search));
379
bacpy(&search.src, src);
380
bacpy(&search.dst, dst);
382
/* Ongoing SDP Discovery */
383
match = g_slist_find_custom(context_list, &search, find_by_bdaddr);
391
close(ctxt->session->sock);
395
char *bt_uuid2string(uuid_t *uuid)
400
unsigned short data1;
401
unsigned short data2;
402
unsigned short data3;
404
unsigned short data5;
409
switch (uuid->type) {
411
sdp_uuid16_to_uuid128(&uuid128, uuid);
414
sdp_uuid32_to_uuid128(&uuid128, uuid);
417
memcpy(&uuid128, uuid, sizeof(uuid_t));
420
/* Type of UUID unknown */
424
memcpy(&data0, &uuid128.value.uuid128.data[0], 4);
425
memcpy(&data1, &uuid128.value.uuid128.data[4], 2);
426
memcpy(&data2, &uuid128.value.uuid128.data[6], 2);
427
memcpy(&data3, &uuid128.value.uuid128.data[8], 2);
428
memcpy(&data4, &uuid128.value.uuid128.data[10], 4);
429
memcpy(&data5, &uuid128.value.uuid128.data[14], 2);
431
str = g_try_malloc0(MAX_LEN_UUID_STR);
435
sprintf(str, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
436
g_ntohl(data0), g_ntohs(data1),
437
g_ntohs(data2), g_ntohs(data3),
438
g_ntohl(data4), g_ntohs(data5));
447
{ "vcp", VIDEO_CONF_SVCLASS_ID },
448
{ "pbap", PBAP_SVCLASS_ID },
449
{ "sap", SAP_SVCLASS_ID },
450
{ "ftp", OBEX_FILETRANS_SVCLASS_ID },
451
{ "bpp", BASIC_PRINTING_SVCLASS_ID },
452
{ "bip", IMAGING_SVCLASS_ID },
453
{ "synch", IRMC_SYNC_SVCLASS_ID },
454
{ "dun", DIALUP_NET_SVCLASS_ID },
455
{ "opp", OBEX_OBJPUSH_SVCLASS_ID },
456
{ "fax", FAX_SVCLASS_ID },
457
{ "spp", SERIAL_PORT_SVCLASS_ID },
458
{ "hsp", HEADSET_SVCLASS_ID },
459
{ "hfp", HANDSFREE_SVCLASS_ID },
463
uint16_t bt_name2class(const char *pattern)
467
for (i = 0; bt_services[i].name; i++) {
468
if (strcasecmp(bt_services[i].name, pattern) == 0)
469
return bt_services[i].class;
475
static inline gboolean is_uuid128(const char *string)
477
return (strlen(string) == 36 &&
484
char *bt_name2string(const char *pattern)
490
/* UUID 128 string format */
491
if (is_uuid128(pattern))
492
return g_strdup(pattern);
494
/* Friendly service name format */
495
uuid16 = bt_name2class(pattern);
500
uuid16 = strtol(pattern, NULL, 16);
501
for (i = 0; bt_services[i].class; i++) {
502
if (bt_services[i].class == uuid16)
509
sdp_uuid16_create(&uuid, uuid16);
511
return bt_uuid2string(&uuid);
514
int bt_string2uuid(uuid_t *uuid, const char *string)
516
uint32_t data0, data4;
517
uint16_t data1, data2, data3, data5;
519
if (is_uuid128(string) &&
520
sscanf(string, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
521
&data0, &data1, &data2, &data3, &data4, &data5) == 6) {
524
data0 = g_htonl(data0);
525
data1 = g_htons(data1);
526
data2 = g_htons(data2);
527
data3 = g_htons(data3);
528
data4 = g_htonl(data4);
529
data5 = g_htons(data5);
531
memcpy(&val[0], &data0, 4);
532
memcpy(&val[4], &data1, 2);
533
memcpy(&val[6], &data2, 2);
534
memcpy(&val[8], &data3, 2);
535
memcpy(&val[10], &data4, 4);
536
memcpy(&val[14], &data5, 2);
538
sdp_uuid128_create(uuid, val);
542
uint16_t class = bt_name2class(string);
544
sdp_uuid16_create(uuid, class);
552
gchar *bt_list2string(GSList *list)
560
str = g_strdup((const gchar *) list->data);
562
for (l = list->next; l; l = l->next) {
563
tmp = g_strconcat(str, " " , (const gchar *) l->data, NULL);
571
GSList *bt_string2list(const gchar *str)
580
/* FIXME: eglib doesn't support g_strsplit */
581
uuids = g_strsplit(str, " ", 0);
586
l = g_slist_append(l, uuids[i]);
595
static gboolean hci_event_watch(GIOChannel *io,
596
GIOCondition cond, gpointer user_data)
598
unsigned char buf[HCI_MAX_EVENT_SIZE], *body;
599
struct hci_cmd_data *cmd = user_data;
600
evt_cmd_status *evt_status;
601
evt_auth_complete *evt_auth;
602
evt_encrypt_change *evt_enc;
604
set_conn_encrypt_cp cp;
607
uint8_t status = HCI_OE_POWER_OFF;
609
if (cond & G_IO_NVAL) {
610
cmd->cb(status, cmd->caller_data);
614
if (cond & (G_IO_ERR | G_IO_HUP))
617
dd = g_io_channel_unix_get_fd(io);
619
if (read(dd, buf, sizeof(buf)) < 0)
622
hdr = (hci_event_hdr *) (buf + 1);
623
body = buf + (1 + HCI_EVENT_HDR_SIZE);
627
evt_status = (evt_cmd_status *) body;
628
ocf = cmd_opcode_ocf(evt_status->opcode);
632
case OCF_AUTH_REQUESTED:
633
case OCF_SET_CONN_ENCRYPT:
634
if (evt_status->status != 0) {
635
/* Baseband rejected command */
636
status = evt_status->status;
643
/* Wait for the next event */
645
case EVT_AUTH_COMPLETE:
646
evt_auth = (evt_auth_complete *) body;
647
if (evt_auth->handle != cmd->handle) {
652
if (evt_auth->status != 0x00) {
653
status = evt_auth->status;
654
/* Abort encryption */
658
memset(&cp, 0, sizeof(cp));
659
cp.handle = cmd->handle;
662
cmd->ocf = OCF_SET_CONN_ENCRYPT;
664
if (hci_send_cmd(dd, OGF_LINK_CTL, OCF_SET_CONN_ENCRYPT,
665
SET_CONN_ENCRYPT_CP_SIZE, &cp) < 0) {
666
status = HCI_COMMAND_DISALLOWED;
669
/* Wait for encrypt change event */
671
case EVT_ENCRYPT_CHANGE:
672
evt_enc = (evt_encrypt_change *) body;
673
if (evt_enc->handle != cmd->handle)
676
/* Procedure finished: reporting status */
677
status = evt_enc->status;
685
cmd->cb(status, cmd->caller_data);
686
g_io_channel_shutdown(io, TRUE, NULL);
691
int bt_acl_encrypt(const bdaddr_t *src, const bdaddr_t *dst,
692
bt_hci_result_t cb, gpointer user_data)
695
struct hci_cmd_data *cmd;
696
struct hci_conn_info_req *cr;
697
auth_requested_cp cp;
698
struct hci_filter nf;
704
ba2str(src, src_addr);
705
dev_id = hci_devid(src_addr);
709
dd = hci_open_dev(dev_id);
713
cr = g_malloc0(sizeof(*cr) + sizeof(struct hci_conn_info));
715
bacpy(&cr->bdaddr, dst);
717
err = ioctl(dd, HCIGETCONNINFO, cr);
718
link_mode = cr->conn_info->link_mode;
719
handle = cr->conn_info->handle;
727
if (link_mode & HCI_LM_ENCRYPT) {
728
/* Already encrypted */
733
memset(&cp, 0, sizeof(cp));
734
cp.handle = htobs(handle);
736
if (hci_send_cmd(dd, OGF_LINK_CTL, OCF_AUTH_REQUESTED,
737
AUTH_REQUESTED_CP_SIZE, &cp) < 0) {
742
cmd = g_new0(struct hci_cmd_data, 1);
743
cmd->handle = handle;
744
cmd->ocf = OCF_AUTH_REQUESTED;
746
cmd->caller_data = user_data;
748
hci_filter_clear(&nf);
749
hci_filter_set_ptype(HCI_EVENT_PKT, &nf);
750
hci_filter_set_event(EVT_CMD_STATUS, &nf);
751
hci_filter_set_event(EVT_AUTH_COMPLETE, &nf);
752
hci_filter_set_event(EVT_ENCRYPT_CHANGE, &nf);
754
if (setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0) {
759
io = g_io_channel_unix_new(dd);
760
g_io_channel_set_close_on_unref(io, FALSE);
761
g_io_add_watch_full(io, G_PRIORITY_DEFAULT,
762
G_IO_HUP | G_IO_ERR | G_IO_NVAL | G_IO_IN,
763
hci_event_watch, cmd, g_free);
764
g_io_channel_unref(io);