3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2004-2008 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
31
#include <sys/ioctl.h>
32
#include <sys/socket.h>
34
#include <bluetooth/bluetooth.h>
35
#include <bluetooth/hci.h>
36
#include <bluetooth/hci_lib.h>
37
#include <bluetooth/rfcomm.h>
38
#include <bluetooth/l2cap.h>
39
#include <bluetooth/sco.h>
40
#include <bluetooth/sdp.h>
41
#include <bluetooth/sdp_lib.h>
45
#include "glib-helper.h"
47
typedef int (*resolver_t) (int fd, char *src, char *dst);
48
typedef BtIOError (*connect_t) (BtIO *io, BtIOFunc func);
49
typedef BtIOError (*listen_t) (BtIO *io, BtIOFunc func);
58
int set_nonblocking(int fd)
62
arg = fcntl(fd, F_GETFL);
66
/* Return if already nonblocking */
71
if (fcntl(fd, F_SETFL, arg) < 0)
96
struct io_context *io_ctxt;
100
struct search_context {
103
sdp_session_t *session;
105
bt_destroy_t destroy;
110
static GSList *context_list = NULL;
112
static void search_context_cleanup(struct search_context *ctxt)
114
context_list = g_slist_remove(context_list, ctxt);
117
ctxt->destroy(ctxt->user_data);
122
static void search_completed_cb(uint8_t type, uint16_t status,
123
uint8_t *rsp, size_t size, void *user_data)
125
struct search_context *ctxt = user_data;
126
sdp_list_t *recs = NULL;
127
int scanned, seqlen = 0, bytesleft = size;
131
if (status || type != SDP_SVC_SEARCH_ATTR_RSP) {
136
scanned = sdp_extract_seqtype(rsp, bytesleft, &dataType, &seqlen);
137
if (!scanned || !seqlen)
141
bytesleft -= scanned;
147
rec = sdp_extract_pdu(rsp, bytesleft, &recsize);
152
sdp_record_free(rec);
158
bytesleft -= recsize;
160
recs = sdp_list_append(recs, rec);
161
} while (scanned < size && bytesleft > 0);
164
sdp_close(ctxt->session);
167
ctxt->cb(recs, err, ctxt->user_data);
170
sdp_list_free(recs, (sdp_free_func_t) sdp_record_free);
172
search_context_cleanup(ctxt);
175
static gboolean search_process_cb(GIOChannel *chan,
176
GIOCondition cond, void *user_data)
178
struct search_context *ctxt = user_data;
181
if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
186
if (sdp_process(ctxt->session) < 0)
193
sdp_close(ctxt->session);
196
ctxt->cb(NULL, err, ctxt->user_data);
198
search_context_cleanup(ctxt);
204
static gboolean connect_watch(GIOChannel *chan, GIOCondition cond, gpointer user_data)
206
struct search_context *ctxt = user_data;
207
sdp_list_t *search, *attrids;
208
uint32_t range = 0x0000ffff;
212
sk = g_io_channel_unix_get_fd(chan);
215
if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
223
if (sdp_set_notify(ctxt->session, search_completed_cb, ctxt) < 0) {
228
search = sdp_list_append(NULL, &ctxt->uuid);
229
attrids = sdp_list_append(NULL, &range);
230
if (sdp_service_search_attr_async(ctxt->session,
231
search, SDP_ATTR_REQ_RANGE, attrids) < 0) {
232
sdp_list_free(attrids, NULL);
233
sdp_list_free(search, NULL);
238
sdp_list_free(attrids, NULL);
239
sdp_list_free(search, NULL);
241
/* Set callback responsible for update the internal SDP transaction */
242
g_io_add_watch(chan, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
243
search_process_cb, ctxt);
247
sdp_close(ctxt->session);
250
ctxt->cb(NULL, -err, ctxt->user_data);
252
search_context_cleanup(ctxt);
257
static int create_search_context(struct search_context **ctxt,
258
const bdaddr_t *src, const bdaddr_t *dst,
267
s = sdp_connect(src, dst, SDP_NON_BLOCKING);
271
*ctxt = g_try_malloc0(sizeof(struct search_context));
277
bacpy(&(*ctxt)->src, src);
278
bacpy(&(*ctxt)->dst, dst);
279
(*ctxt)->session = s;
280
(*ctxt)->uuid = *uuid;
282
chan = g_io_channel_unix_new(sdp_get_socket(s));
283
g_io_add_watch(chan, G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
284
connect_watch, *ctxt);
285
g_io_channel_unref(chan);
290
int bt_search_service(const bdaddr_t *src, const bdaddr_t *dst,
291
uuid_t *uuid, bt_callback_t cb, void *user_data,
292
bt_destroy_t destroy)
294
struct search_context *ctxt = NULL;
300
err = create_search_context(&ctxt, src, dst, uuid);
305
ctxt->destroy = destroy;
306
ctxt->user_data = user_data;
308
context_list = g_slist_append(context_list, ctxt);
313
int bt_discover_services(const bdaddr_t *src, const bdaddr_t *dst,
314
bt_callback_t cb, void *user_data, bt_destroy_t destroy)
318
sdp_uuid16_create(&uuid, PUBLIC_BROWSE_GROUP);
320
return bt_search_service(src, dst, &uuid, cb, user_data, destroy);
323
static int find_by_bdaddr(const void *data, const void *user_data)
325
const struct search_context *ctxt = data, *search = user_data;
327
return (bacmp(&ctxt->dst, &search->dst) &&
328
bacmp(&ctxt->src, &search->src));
331
int bt_cancel_discovery(const bdaddr_t *src, const bdaddr_t *dst)
333
struct search_context search, *ctxt;
336
memset(&search, 0, sizeof(search));
337
bacpy(&search.src, src);
338
bacpy(&search.dst, dst);
340
/* Ongoing SDP Discovery */
341
match = g_slist_find_custom(context_list, &search, find_by_bdaddr);
349
close(ctxt->session->sock);
353
char *bt_uuid2string(uuid_t *uuid)
358
unsigned short data1;
359
unsigned short data2;
360
unsigned short data3;
362
unsigned short data5;
367
switch (uuid->type) {
369
sdp_uuid16_to_uuid128(&uuid128, uuid);
372
sdp_uuid32_to_uuid128(&uuid128, uuid);
375
memcpy(&uuid128, uuid, sizeof(uuid_t));
378
/* Type of UUID unknown */
382
memcpy(&data0, &uuid128.value.uuid128.data[0], 4);
383
memcpy(&data1, &uuid128.value.uuid128.data[4], 2);
384
memcpy(&data2, &uuid128.value.uuid128.data[6], 2);
385
memcpy(&data3, &uuid128.value.uuid128.data[8], 2);
386
memcpy(&data4, &uuid128.value.uuid128.data[10], 4);
387
memcpy(&data5, &uuid128.value.uuid128.data[14], 2);
389
str = g_try_malloc0(MAX_LEN_UUID_STR);
393
sprintf(str, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
394
g_ntohl(data0), g_ntohs(data1),
395
g_ntohs(data2), g_ntohs(data3),
396
g_ntohl(data4), g_ntohs(data5));
405
{ "vcp", VIDEO_CONF_SVCLASS_ID },
406
{ "pbap", PBAP_SVCLASS_ID },
407
{ "sap", SAP_SVCLASS_ID },
408
{ "ftp", OBEX_FILETRANS_SVCLASS_ID },
409
{ "bpp", BASIC_PRINTING_SVCLASS_ID },
410
{ "bip", IMAGING_SVCLASS_ID },
411
{ "synch", IRMC_SYNC_SVCLASS_ID },
412
{ "dun", DIALUP_NET_SVCLASS_ID },
413
{ "opp", OBEX_OBJPUSH_SVCLASS_ID },
414
{ "fax", FAX_SVCLASS_ID },
415
{ "spp", SERIAL_PORT_SVCLASS_ID },
416
{ "hsp", HEADSET_SVCLASS_ID },
417
{ "hfp", HANDSFREE_SVCLASS_ID },
421
uint16_t bt_string2class(const char *pattern)
425
for (i = 0; bt_services[i].name; i++) {
426
if (strcasecmp(bt_services[i].name, pattern) == 0)
427
return bt_services[i].class;
433
int bt_string2uuid(uuid_t *uuid, const char *string)
435
uint32_t data0, data4;
436
uint16_t data1, data2, data3, data5;
438
if (strlen(string) == 36 &&
443
sscanf(string, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
444
&data0, &data1, &data2, &data3, &data4, &data5) == 6) {
447
data0 = g_htonl(data0);
448
data1 = g_htons(data1);
449
data2 = g_htons(data2);
450
data3 = g_htons(data3);
451
data4 = g_htonl(data4);
452
data5 = g_htons(data5);
454
memcpy(&val[0], &data0, 4);
455
memcpy(&val[4], &data1, 2);
456
memcpy(&val[6], &data2, 2);
457
memcpy(&val[8], &data3, 2);
458
memcpy(&val[10], &data4, 4);
459
memcpy(&val[14], &data5, 2);
461
sdp_uuid128_create(uuid, val);
465
uint16_t class = bt_string2class(string);
467
sdp_uuid16_create(uuid, class);
475
gchar *bt_list2string(GSList *list)
483
str = g_strdup((const gchar *) list->data);
485
for (l = list->next; l; l = l->next) {
486
tmp = g_strconcat(str, " " , (const gchar *) l->data, NULL);
494
GSList *bt_string2list(const gchar *str)
503
/* FIXME: eglib doesn't support g_strsplit */
504
uuids = g_strsplit(str, " ", 0);
509
l = g_slist_append(l, uuids[i]);
518
static inline int resolve_names(int fd, struct sockaddr *host,
519
struct sockaddr *peer, socklen_t len)
525
memset(host, 0, len);
526
err = getsockname(fd, host, &namelen);
531
memset(peer, 0, len);
532
err = getpeername(fd, peer, &namelen);
539
static int rfcomm_resolver(int fd, char *src, char *dst)
541
struct sockaddr_rc host, peer;
546
err = resolve_names(fd, (struct sockaddr *) &host,
547
(struct sockaddr *) &peer, len);
551
ba2str(&host.rc_bdaddr, src);
552
ba2str(&peer.rc_bdaddr, dst);
557
static int l2cap_resolver(int fd, char *src, char *dst)
559
struct sockaddr_l2 host, peer;
564
err = resolve_names(fd, (struct sockaddr *) &host,
565
(struct sockaddr *) &peer, len);
569
ba2str(&host.l2_bdaddr, src);
570
ba2str(&peer.l2_bdaddr, dst);
575
static int sco_resolver(int fd, char *src, char *dst)
577
struct sockaddr_sco host, peer;
582
err = resolve_names(fd, (struct sockaddr *) &host,
583
(struct sockaddr *) &peer, len);
587
ba2str(&host.sco_bdaddr, src);
588
ba2str(&peer.sco_bdaddr, dst);
593
static gboolean listen_cb(GIOChannel *chan, GIOCondition cond,
596
BtIO *io = user_data;
597
struct io_context *io_ctxt = io->io_ctxt;
600
struct sockaddr addr;
604
if (cond & G_IO_NVAL)
607
if (cond & (G_IO_HUP | G_IO_ERR)) {
608
g_io_channel_close(chan);
609
g_io_channel_unref(chan);
615
memset(&addr, 0, len);
616
fd = accept(io_ctxt->fd, &addr, &len);
620
if (io_ctxt->resolver) {
621
err = io_ctxt->resolver(fd, io->src, io->dst);
628
gio = g_io_channel_unix_new(fd);
633
io_ctxt->func(io, err, gio, io_ctxt->user_data);
635
str2ba(io->src, &src);
636
str2ba(io->dst, &dst);
637
io_ctxt->cb(gio, err, &src, &dst, io_ctxt->user_data);
644
io_ctxt->func(io, -errno, NULL, io_ctxt->user_data);
646
io_ctxt->cb(NULL, err, NULL, NULL, io_ctxt->user_data);
651
static int transport_listen(BtIO *io)
653
struct io_context *io_ctxt = io->io_ctxt;
656
err = listen(io_ctxt->fd, 5);
660
io_ctxt->io = g_io_channel_unix_new(io_ctxt->fd);
664
g_io_add_watch(io_ctxt->io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
665
(GIOFunc) listen_cb, io);
670
static gboolean connect_cb(GIOChannel *gio, GIOCondition cond,
673
BtIO *io = user_data;
674
struct io_context *io_ctxt = io->io_ctxt;
679
if (cond & G_IO_NVAL)
683
if (getsockopt(io_ctxt->fd, SOL_SOCKET, SO_ERROR, &ret, &len) < 0) {
693
if (io_ctxt->resolver) {
694
err = io_ctxt->resolver(io_ctxt->fd, io->src, io->dst);
703
io_ctxt->func(io, err, gio, io_ctxt->user_data);
705
str2ba(io->src, &src);
706
str2ba(io->dst, &dst);
707
io_ctxt->cb(gio, err, &src, &dst, io_ctxt->user_data);
710
g_io_channel_close(io_ctxt->io);
711
g_io_channel_unref(io_ctxt->io);
718
static int transport_connect(BtIO *io, struct sockaddr *addr,
721
struct io_context *io_ctxt = io->io_ctxt;
724
io_ctxt->io = g_io_channel_unix_new(io_ctxt->fd);
728
err = g_io_channel_set_flags(io_ctxt->io, G_IO_FLAG_NONBLOCK, NULL);
729
if (err != G_IO_STATUS_NORMAL)
732
err = connect(io_ctxt->fd, addr, addrlen);
733
if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS))
736
g_io_add_watch(io_ctxt->io, G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
737
(GIOFunc) connect_cb, io);
742
static int sco_bind(struct io_context *io_ctxt, const char *address,
743
uint16_t mtu, struct sockaddr_sco *addr)
746
struct sco_options sco_opt;
748
io_ctxt->fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
752
memset(addr, 0, sizeof(*addr));
753
addr->sco_family = AF_BLUETOOTH;
754
str2ba(address, &addr->sco_bdaddr);
756
err = bind(io_ctxt->fd, (struct sockaddr *) addr, sizeof(*addr));
763
socklen_t olen = sizeof(sco_opt);
764
memset(&sco_opt, 0, olen);
765
getsockopt(io_ctxt->fd, SOL_SCO, SCO_OPTIONS, &sco_opt, &olen);
767
setsockopt(io_ctxt->fd, SOL_SCO, SCO_OPTIONS, &sco_opt,
774
static BtIOError sco_connect(BtIO *io, BtIOFunc func)
776
struct io_context *io_ctxt = io->io_ctxt;
777
struct sockaddr_sco addr;
780
io_ctxt->func = func;
782
err = sco_bind(io_ctxt, io->src, 0, &addr);
786
memset(&addr, 0, sizeof(addr));
787
addr.sco_family = AF_BLUETOOTH;
788
str2ba(io->dst, &addr.sco_bdaddr);
790
err = transport_connect(io, (struct sockaddr *) &addr,
797
return BT_IO_SUCCESS;
800
static int l2cap_bind(struct io_context *io_ctxt, const char *address,
801
uint16_t psm, uint16_t mtu, uint32_t flags,
802
struct sockaddr_l2 *addr)
805
struct l2cap_options l2o;
807
io_ctxt->fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
811
memset(addr, 0, sizeof(*addr));
812
addr->l2_family = AF_BLUETOOTH;
813
str2ba(address, &addr->l2_bdaddr);
814
addr->l2_psm = htobs(psm);
816
err = bind(io_ctxt->fd, (struct sockaddr *) addr, sizeof(*addr));
823
socklen_t olen = sizeof(l2o);
824
memset(&l2o, 0, olen);
825
getsockopt(io_ctxt->fd, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &olen);
826
l2o.imtu = l2o.omtu = mtu;
827
setsockopt(io_ctxt->fd, SOL_L2CAP, L2CAP_OPTIONS, &l2o, sizeof(l2o));
832
err = setsockopt(io_ctxt->fd, SOL_L2CAP, L2CAP_LM, &opt,
843
static BtIOError l2cap_listen(BtIO *io, BtIOFunc func)
845
struct io_context *io_ctxt = io->io_ctxt;
846
struct sockaddr_l2 addr;
849
io_ctxt->func = func;
851
err = l2cap_bind(io_ctxt, io->src, io->psm, io->mtu, io->flags, &addr);
855
err = transport_listen(io);
861
return BT_IO_SUCCESS;
864
static BtIOError l2cap_connect(BtIO *io, BtIOFunc func)
866
struct io_context *io_ctxt = io->io_ctxt;
867
struct sockaddr_l2 l2a;
870
io_ctxt->func = func;
872
err = l2cap_bind(io_ctxt, io->src, 0, io->mtu, 0, &l2a);
876
memset(&l2a, 0, sizeof(l2a));
877
l2a.l2_family = AF_BLUETOOTH;
878
str2ba(io->dst, &l2a.l2_bdaddr);
879
l2a.l2_psm = htobs(io->psm);
881
err = transport_connect(io, (struct sockaddr *) &l2a,
888
return BT_IO_SUCCESS;
891
static BtIOError rfcomm_bind(struct io_context *io_ctxt, const char *address,
892
uint8_t channel, uint32_t flags,
893
struct sockaddr_rc *addr)
897
io_ctxt->fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
902
memset(addr, 0, sizeof(*addr));
903
addr->rc_family = AF_BLUETOOTH;
904
str2ba(address, &addr->rc_bdaddr);
905
addr->rc_channel = channel;
907
err = bind(io_ctxt->fd, (struct sockaddr *) addr, sizeof(*addr));
915
err = setsockopt(io_ctxt->fd, SOL_RFCOMM, RFCOMM_LM, &opt,
923
return BT_IO_SUCCESS;
926
static BtIOError rfcomm_listen(BtIO *io, BtIOFunc func)
928
struct io_context *io_ctxt = io->io_ctxt;
929
struct sockaddr_rc addr;
933
io_ctxt->func = func;
935
err = rfcomm_bind(io_ctxt, io->src, io->channel, io->flags, &addr);
939
err = transport_listen(io);
945
sa_len = sizeof(struct sockaddr_rc);
946
memset(&addr, 0, sizeof(addr));
947
if (getsockname(io_ctxt->fd, (struct sockaddr *) &addr, &sa_len) < 0) {
953
io->channel = addr.rc_channel;
955
return BT_IO_SUCCESS;
958
static BtIOError rfcomm_connect(BtIO *io, BtIOFunc func)
960
struct io_context *io_ctxt = io->io_ctxt;
961
struct sockaddr_rc addr;
964
io_ctxt->func = func;
966
err = rfcomm_bind(io_ctxt, io->src, 0, 0, &addr);
970
memset(&addr, 0, sizeof(addr));
971
addr.rc_family = AF_BLUETOOTH;
972
str2ba(io->dst, &addr.rc_bdaddr);
973
addr.rc_channel = io->channel;
975
err = transport_connect(io, (struct sockaddr *) &addr,
982
return BT_IO_SUCCESS;
985
static BtIOError sco_listen(BtIO *io, BtIOFunc func)
987
struct io_context *io_ctxt = io->io_ctxt;
988
struct sockaddr_sco addr;
991
io_ctxt->func = func;
993
err = sco_bind(io_ctxt, io->src, io->mtu, &addr);
997
err = transport_listen(io);
1003
return BT_IO_SUCCESS;
1006
static gboolean hci_event_watch(GIOChannel *io,
1007
GIOCondition cond, gpointer user_data)
1009
unsigned char buf[HCI_MAX_EVENT_SIZE], *body;
1010
struct hci_cmd_data *cmd = user_data;
1011
evt_cmd_status *evt_status;
1012
evt_auth_complete *evt_auth;
1013
evt_encrypt_change *evt_enc;
1015
set_conn_encrypt_cp cp;
1018
uint8_t status = HCI_OE_POWER_OFF;
1020
if (cond & G_IO_NVAL) {
1021
cmd->cb(status, cmd->caller_data);
1025
if (cond & (G_IO_ERR | G_IO_HUP))
1028
dd = g_io_channel_unix_get_fd(io);
1030
if (read(dd, buf, sizeof(buf)) < 0)
1033
hdr = (hci_event_hdr *) (buf + 1);
1034
body = buf + (1 + HCI_EVENT_HDR_SIZE);
1037
case EVT_CMD_STATUS:
1038
evt_status = (evt_cmd_status *) body;
1039
ocf = cmd_opcode_ocf(evt_status->opcode);
1040
if (ocf != cmd->ocf)
1043
case OCF_AUTH_REQUESTED:
1044
case OCF_SET_CONN_ENCRYPT:
1045
if (evt_status->status != 0) {
1046
/* Baseband rejected command */
1047
status = evt_status->status;
1054
/* Wait for the next event */
1056
case EVT_AUTH_COMPLETE:
1057
evt_auth = (evt_auth_complete *) body;
1058
if (evt_auth->handle != cmd->handle) {
1063
if (evt_auth->status != 0x00) {
1064
status = evt_auth->status;
1065
/* Abort encryption */
1069
memset(&cp, 0, sizeof(cp));
1070
cp.handle = cmd->handle;
1073
cmd->ocf = OCF_SET_CONN_ENCRYPT;
1075
if (hci_send_cmd(dd, OGF_LINK_CTL, OCF_SET_CONN_ENCRYPT,
1076
SET_CONN_ENCRYPT_CP_SIZE, &cp) < 0) {
1077
status = HCI_COMMAND_DISALLOWED;
1080
/* Wait for encrypt change event */
1082
case EVT_ENCRYPT_CHANGE:
1083
evt_enc = (evt_encrypt_change *) body;
1084
if (evt_enc->handle != cmd->handle)
1087
/* Procedure finished: reporting status */
1088
status = evt_enc->status;
1096
cmd->cb(status, cmd->caller_data);
1097
g_io_channel_close(io);
1102
int bt_acl_encrypt(const bdaddr_t *src, const bdaddr_t *dst,
1103
bt_hci_result_t cb, gpointer user_data)
1106
struct hci_cmd_data *cmd;
1107
struct hci_conn_info_req *cr;
1108
auth_requested_cp cp;
1109
struct hci_filter nf;
1110
int dd, dev_id, err;
1115
ba2str(src, src_addr);
1116
dev_id = hci_devid(src_addr);
1120
dd = hci_open_dev(dev_id);
1124
cr = g_malloc0(sizeof(*cr) + sizeof(struct hci_conn_info));
1125
cr->type = ACL_LINK;
1126
bacpy(&cr->bdaddr, dst);
1128
err = ioctl(dd, HCIGETCONNINFO, cr);
1129
link_mode = cr->conn_info->link_mode;
1130
handle = cr->conn_info->handle;
1138
if (link_mode & HCI_LM_ENCRYPT) {
1139
/* Already encrypted */
1144
memset(&cp, 0, sizeof(cp));
1145
cp.handle = htobs(handle);
1147
if (hci_send_cmd(dd, OGF_LINK_CTL, OCF_AUTH_REQUESTED,
1148
AUTH_REQUESTED_CP_SIZE, &cp) < 0) {
1153
cmd = g_new0(struct hci_cmd_data, 1);
1154
cmd->handle = handle;
1155
cmd->ocf = OCF_AUTH_REQUESTED;
1157
cmd->caller_data = user_data;
1159
hci_filter_clear(&nf);
1160
hci_filter_set_ptype(HCI_EVENT_PKT, &nf);
1161
hci_filter_set_event(EVT_CMD_STATUS, &nf);
1162
hci_filter_set_event(EVT_AUTH_COMPLETE, &nf);
1163
hci_filter_set_event(EVT_ENCRYPT_CHANGE, &nf);
1165
if (setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0) {
1170
io = g_io_channel_unix_new(dd);
1171
g_io_channel_set_close_on_unref(io, FALSE);
1172
g_io_add_watch_full(io, G_PRIORITY_DEFAULT,
1173
G_IO_HUP | G_IO_ERR | G_IO_NVAL | G_IO_IN,
1174
hci_event_watch, cmd, g_free);
1175
g_io_channel_unref(io);
1185
static int create_io_context(struct io_context **io_ctxt, BtIOFunc func,
1186
gpointer cb, gpointer resolver, gpointer user_data)
1188
*io_ctxt = g_try_malloc0(sizeof(struct search_context));
1192
(*io_ctxt)->cb = cb;
1193
(*io_ctxt)->func = func;
1194
(*io_ctxt)->resolver = resolver;
1195
(*io_ctxt)->user_data = user_data;
1200
static void io_context_cleanup(struct io_context *io_ctxt)
1203
g_io_channel_close(io_ctxt->io);
1204
g_io_channel_unref(io_ctxt->io);
1209
GIOChannel *rfcomm_listen_internal(const bdaddr_t *src, uint8_t *channel,
1210
uint32_t flags, bt_io_callback_t cb, void *user_data)
1215
io = bt_io_create(BT_IO_RFCOMM, user_data, NULL);
1219
ba2str(src, io->src);
1220
io->channel = *channel;
1222
io->io_ctxt->cb = cb;
1223
err = bt_io_listen(io, NULL, NULL);
1224
if (err != BT_IO_SUCCESS) {
1229
*channel = io->channel;
1231
return io->io_ctxt->io;
1234
GIOChannel *bt_rfcomm_listen_allocate(const bdaddr_t *src, uint8_t *channel,
1235
uint32_t flags, bt_io_callback_t cb, void *user_data)
1242
return rfcomm_listen_internal(src, channel, flags, cb, user_data);
1245
GIOChannel *bt_rfcomm_listen(const bdaddr_t *src, uint8_t channel,
1246
uint32_t flags, bt_io_callback_t cb, void *user_data)
1248
if (channel < 1 || channel > 30)
1251
return rfcomm_listen_internal(src, &channel, flags, cb, user_data);
1255
int bt_rfcomm_connect(const bdaddr_t *src, const bdaddr_t *dst,
1256
uint8_t channel, bt_io_callback_t cb, void *user_data)
1261
io = bt_io_create(BT_IO_RFCOMM, user_data, NULL);
1265
ba2str(src, io->src);
1266
ba2str(dst, io->dst);
1267
io->channel = channel;
1268
io->io_ctxt->cb = cb;
1269
err = bt_io_connect(io, NULL, NULL);
1270
if (err != BT_IO_SUCCESS) {
1278
GIOChannel *bt_l2cap_listen(const bdaddr_t *src, uint16_t psm, uint16_t mtu,
1279
uint32_t flags, bt_io_callback_t cb, void *user_data)
1284
io = bt_io_create(BT_IO_L2CAP, user_data, NULL);
1288
ba2str(src, io->src);
1292
io->io_ctxt->cb = cb;
1293
err = bt_io_listen(io, NULL, NULL);
1294
if (err != BT_IO_SUCCESS) {
1299
return io->io_ctxt->io;
1302
int bt_l2cap_connect(const bdaddr_t *src, const bdaddr_t *dst,
1303
uint16_t psm, uint16_t mtu, bt_io_callback_t cb,
1309
io = bt_io_create(BT_IO_L2CAP, user_data, NULL);
1313
ba2str(src, io->src);
1314
ba2str(dst, io->dst);
1317
io->io_ctxt->cb = cb;
1318
err = bt_io_connect(io, NULL, NULL);
1319
if (err != BT_IO_SUCCESS) {
1327
GIOChannel *bt_sco_listen(const bdaddr_t *src, uint16_t mtu,
1328
bt_io_callback_t cb, void *user_data)
1333
io = bt_io_create(BT_IO_SCO, user_data, NULL);
1337
ba2str(src, io->src);
1338
io->io_ctxt->cb = cb;
1339
err = bt_io_listen(io, NULL, NULL);
1340
if (err != BT_IO_SUCCESS) {
1345
return io->io_ctxt->io;
1349
int bt_sco_connect(const bdaddr_t *src, const bdaddr_t *dst,
1350
bt_io_callback_t cb, void *user_data)
1355
io = bt_io_create(BT_IO_SCO, user_data, NULL);
1359
ba2str(src, io->src);
1360
ba2str(dst, io->dst);
1361
io->io_ctxt->cb = cb;
1362
err = bt_io_connect(io, NULL, NULL);
1363
if (err != BT_IO_SUCCESS) {
1371
/* Experiemental bt_io API */
1373
BtIO *bt_io_create(BtIOTransport type, gpointer user_data, GDestroyNotify notify)
1378
io = g_new0(BtIO, 1);
1386
err = create_io_context(&io->io_ctxt, NULL, NULL,
1387
l2cap_resolver, user_data);
1388
io->connect = l2cap_connect;
1389
io->listen = l2cap_listen;
1392
err = create_io_context(&io->io_ctxt, NULL, NULL,
1393
rfcomm_resolver, user_data);
1394
io->connect = rfcomm_connect;
1395
io->listen = rfcomm_listen;
1398
err = create_io_context(&io->io_ctxt, NULL, NULL,
1399
sco_resolver, user_data);
1400
io->connect = sco_connect;
1401
io->listen = sco_listen;
1415
BtIO *bt_io_ref(BtIO *io)
1422
void bt_io_unref(BtIO *io)
1429
io_context_cleanup(io->io_ctxt);
1433
gboolean bt_io_set_source(BtIO *io, const char *address)
1435
if (strlen(address) != sizeof(io->src))
1438
memcpy(io->src, address, sizeof(io->src));
1443
const char *bt_io_get_source(BtIO *io)
1448
gboolean bt_io_set_destination(BtIO *io, const char *address)
1450
if (strlen(address) != sizeof(io->dst))
1453
memcpy(io->dst, address, sizeof(io->dst));
1458
const char *bt_io_get_destination(BtIO *io)
1463
gboolean bt_io_set_flags(BtIO *io, guint32 flags)
1470
guint32 bt_io_get_flags(BtIO *io)
1475
gboolean bt_io_set_channel(BtIO *io, guint8 channel)
1477
if (io->type != BT_IO_RFCOMM)
1480
io->channel = channel;
1485
guint8 bt_io_get_channel(BtIO *io)
1490
gboolean bt_io_set_psm(BtIO *io, guint16 psm)
1492
if (io->type != BT_IO_L2CAP)
1500
guint16 bt_io_get_psm(BtIO *io)
1505
gboolean bt_io_set_mtu(BtIO *io, guint16 mtu)
1512
guint16 bt_io_get_mtu(BtIO *io)
1517
BtIOError bt_io_connect(BtIO *io, const char *uuid, BtIOFunc func)
1520
return BT_IO_FAILED;
1522
return io->connect(io, func);
1525
BtIOError bt_io_listen(BtIO *io, const char *uuid, BtIOFunc func)
1528
return BT_IO_FAILED;
1530
return io->listen(io, func);
1533
BtIOError bt_io_shutdown(BtIO *io)
1535
io_context_cleanup(io->io_ctxt);
1537
return BT_IO_SUCCESS;