3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2009-2010 Marcel Holtmann <marcel@holtmann.org>
6 * Copyright (C) 2009-2010 Nokia Corporation
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
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include <bluetooth/bluetooth.h>
33 #include <bluetooth/l2cap.h>
34 #include <bluetooth/rfcomm.h>
35 #include <bluetooth/sco.h>
36 #include <bluetooth/hci.h>
37 #include <bluetooth/hci_lib.h>
43 #define ERROR_FAILED(gerr, str, err) \
44 g_set_error(gerr, BT_IO_ERROR, BT_IO_ERROR_FAILED, \
45 str ": %s (%d)", strerror(err), err)
47 #define DEFAULT_DEFER_TIMEOUT 30
68 GDestroyNotify destroy;
74 GDestroyNotify destroy;
81 GDestroyNotify destroy;
84 static void server_remove(struct server *server)
87 server->destroy(server->user_data);
91 static void connect_remove(struct connect *conn)
94 conn->destroy(conn->user_data);
98 static void accept_remove(struct accept *accept)
101 accept->destroy(accept->user_data);
105 static gboolean check_nval(GIOChannel *io)
109 memset(&fds, 0, sizeof(fds));
110 fds.fd = g_io_channel_unix_get_fd(io);
111 fds.events = POLLNVAL;
113 if (poll(&fds, 1, 0) > 0 && (fds.revents & POLLNVAL))
119 static gboolean accept_cb(GIOChannel *io, GIOCondition cond,
122 struct accept *accept = user_data;
125 /* If the user aborted this accept attempt */
126 if ((cond & G_IO_NVAL) || check_nval(io))
129 if (cond & (G_IO_HUP | G_IO_ERR))
130 g_set_error(&err, BT_IO_ERROR, BT_IO_ERROR_DISCONNECTED,
131 "HUP or ERR on socket");
133 accept->connect(io, err, accept->user_data);
140 static gboolean connect_cb(GIOChannel *io, GIOCondition cond,
143 struct connect *conn = user_data;
146 /* If the user aborted this connect attempt */
147 if ((cond & G_IO_NVAL) || check_nval(io))
150 if (cond & G_IO_OUT) {
151 int err = 0, sock = g_io_channel_unix_get_fd(io);
152 socklen_t len = sizeof(err);
154 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
158 g_set_error(&gerr, BT_IO_ERROR,
159 BT_IO_ERROR_CONNECT_FAILED, "%s (%d)",
161 } else if (cond & (G_IO_HUP | G_IO_ERR))
162 g_set_error(&gerr, BT_IO_ERROR, BT_IO_ERROR_CONNECT_FAILED,
163 "HUP or ERR on socket");
165 conn->connect(io, gerr, conn->user_data);
173 static gboolean server_cb(GIOChannel *io, GIOCondition cond,
176 struct server *server = user_data;
177 int srv_sock, cli_sock;
180 /* If the user closed the server */
181 if ((cond & G_IO_NVAL) || check_nval(io))
184 srv_sock = g_io_channel_unix_get_fd(io);
186 cli_sock = accept(srv_sock, NULL, NULL);
190 cli_io = g_io_channel_unix_new(cli_sock);
192 g_io_channel_set_close_on_unref(cli_io, TRUE);
193 g_io_channel_set_flags(cli_io, G_IO_FLAG_NONBLOCK, NULL);
196 server->confirm(cli_io, server->user_data);
198 server->connect(cli_io, NULL, server->user_data);
200 g_io_channel_unref(cli_io);
205 static void server_add(GIOChannel *io, BtIOConnect connect,
206 BtIOConfirm confirm, gpointer user_data,
207 GDestroyNotify destroy)
209 struct server *server;
212 server = g_new0(struct server, 1);
213 server->connect = connect;
214 server->confirm = confirm;
215 server->user_data = user_data;
216 server->destroy = destroy;
218 cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
219 g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, server_cb, server,
220 (GDestroyNotify) server_remove);
223 static void connect_add(GIOChannel *io, BtIOConnect connect,
224 gpointer user_data, GDestroyNotify destroy)
226 struct connect *conn;
229 conn = g_new0(struct connect, 1);
230 conn->connect = connect;
231 conn->user_data = user_data;
232 conn->destroy = destroy;
234 cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
235 g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, connect_cb, conn,
236 (GDestroyNotify) connect_remove);
239 static void accept_add(GIOChannel *io, BtIOConnect connect, gpointer user_data,
240 GDestroyNotify destroy)
242 struct accept *accept;
245 accept = g_new0(struct accept, 1);
246 accept->connect = connect;
247 accept->user_data = user_data;
248 accept->destroy = destroy;
250 cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
251 g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, accept_cb, accept,
252 (GDestroyNotify) accept_remove);
255 static int l2cap_bind(int sock, const bdaddr_t *src, uint16_t psm,
256 uint16_t cid, GError **err)
258 struct sockaddr_l2 addr;
260 memset(&addr, 0, sizeof(addr));
261 addr.l2_family = AF_BLUETOOTH;
262 bacpy(&addr.l2_bdaddr, src);
265 addr.l2_cid = htobs(cid);
267 addr.l2_psm = htobs(psm);
269 if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
270 ERROR_FAILED(err, "l2cap_bind", errno);
277 static int l2cap_connect(int sock, const bdaddr_t *dst,
278 uint16_t psm, uint16_t cid)
281 struct sockaddr_l2 addr;
283 memset(&addr, 0, sizeof(addr));
284 addr.l2_family = AF_BLUETOOTH;
285 bacpy(&addr.l2_bdaddr, dst);
287 addr.l2_cid = htobs(cid);
289 addr.l2_psm = htobs(psm);
291 err = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
292 if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS))
298 static int l2cap_set_master(int sock, int master)
304 if (getsockopt(sock, SOL_L2CAP, L2CAP_LM, &flags, &len) < 0)
308 if (flags & L2CAP_LM_MASTER)
310 flags |= L2CAP_LM_MASTER;
312 if (!(flags & L2CAP_LM_MASTER))
314 flags &= ~L2CAP_LM_MASTER;
317 if (setsockopt(sock, SOL_L2CAP, L2CAP_LM, &flags, sizeof(flags)) < 0)
323 static int rfcomm_set_master(int sock, int master)
329 if (getsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &flags, &len) < 0)
333 if (flags & RFCOMM_LM_MASTER)
335 flags |= RFCOMM_LM_MASTER;
337 if (!(flags & RFCOMM_LM_MASTER))
339 flags &= ~RFCOMM_LM_MASTER;
342 if (setsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &flags, sizeof(flags)) < 0)
348 static int l2cap_set_lm(int sock, int level)
353 L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT,
354 L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT | L2CAP_LM_SECURE,
355 }, opt = lm_map[level];
357 if (setsockopt(sock, SOL_L2CAP, L2CAP_LM, &opt, sizeof(opt)) < 0)
363 static int rfcomm_set_lm(int sock, int level)
368 RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT,
369 RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT | RFCOMM_LM_SECURE,
370 }, opt = lm_map[level];
372 if (setsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0)
378 static gboolean set_sec_level(int sock, BtIOType type, int level, GError **err)
380 struct bt_security sec;
383 if (level < BT_SECURITY_LOW || level > BT_SECURITY_HIGH) {
384 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
385 "Valid security level range is %d-%d",
386 BT_SECURITY_LOW, BT_SECURITY_HIGH);
390 memset(&sec, 0, sizeof(sec));
393 if (setsockopt(sock, SOL_BLUETOOTH, BT_SECURITY, &sec,
397 if (errno != ENOPROTOOPT) {
398 ERROR_FAILED(err, "setsockopt(BT_SECURITY)", errno);
402 if (type == BT_IO_L2CAP)
403 ret = l2cap_set_lm(sock, level);
405 ret = rfcomm_set_lm(sock, level);
408 ERROR_FAILED(err, "setsockopt(LM)", -ret);
415 static int l2cap_get_lm(int sock, int *sec_level)
421 if (getsockopt(sock, SOL_L2CAP, L2CAP_LM, &opt, &len) < 0)
426 if (opt & L2CAP_LM_AUTH)
427 *sec_level = BT_SECURITY_LOW;
428 if (opt & L2CAP_LM_ENCRYPT)
429 *sec_level = BT_SECURITY_MEDIUM;
430 if (opt & L2CAP_LM_SECURE)
431 *sec_level = BT_SECURITY_HIGH;
436 static int rfcomm_get_lm(int sock, int *sec_level)
442 if (getsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &opt, &len) < 0)
447 if (opt & RFCOMM_LM_AUTH)
448 *sec_level = BT_SECURITY_LOW;
449 if (opt & RFCOMM_LM_ENCRYPT)
450 *sec_level = BT_SECURITY_MEDIUM;
451 if (opt & RFCOMM_LM_SECURE)
452 *sec_level = BT_SECURITY_HIGH;
457 static gboolean get_sec_level(int sock, BtIOType type, int *level,
460 struct bt_security sec;
464 memset(&sec, 0, sizeof(sec));
466 if (getsockopt(sock, SOL_BLUETOOTH, BT_SECURITY, &sec, &len) == 0) {
471 if (errno != ENOPROTOOPT) {
472 ERROR_FAILED(err, "getsockopt(BT_SECURITY)", errno);
476 if (type == BT_IO_L2CAP)
477 ret = l2cap_get_lm(sock, level);
479 ret = rfcomm_get_lm(sock, level);
482 ERROR_FAILED(err, "getsockopt(LM)", -ret);
489 static int l2cap_set_flushable(int sock, gboolean flushable)
494 if (setsockopt(sock, SOL_BLUETOOTH, BT_FLUSHABLE, &f, sizeof(f)) < 0)
500 static gboolean l2cap_set(int sock, int sec_level, uint16_t imtu,
501 uint16_t omtu, uint8_t mode, int master,
502 int flushable, GError **err)
504 if (imtu || omtu || mode) {
505 struct l2cap_options l2o;
508 memset(&l2o, 0, sizeof(l2o));
510 if (getsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o,
512 ERROR_FAILED(err, "getsockopt(L2CAP_OPTIONS)", errno);
523 if (setsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o,
525 ERROR_FAILED(err, "setsockopt(L2CAP_OPTIONS)", errno);
530 if (master >= 0 && l2cap_set_master(sock, master) < 0) {
531 ERROR_FAILED(err, "l2cap_set_master", errno);
535 if (flushable >= 0 && l2cap_set_flushable(sock, flushable) < 0) {
536 ERROR_FAILED(err, "l2cap_set_flushable", errno);
540 if (sec_level && !set_sec_level(sock, BT_IO_L2CAP, sec_level, err))
546 static int rfcomm_bind(int sock,
547 const bdaddr_t *src, uint8_t channel, GError **err)
549 struct sockaddr_rc addr;
551 memset(&addr, 0, sizeof(addr));
552 addr.rc_family = AF_BLUETOOTH;
553 bacpy(&addr.rc_bdaddr, src);
554 addr.rc_channel = channel;
556 if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
557 ERROR_FAILED(err, "rfcomm_bind", errno);
564 static int rfcomm_connect(int sock, const bdaddr_t *dst, uint8_t channel)
567 struct sockaddr_rc addr;
569 memset(&addr, 0, sizeof(addr));
570 addr.rc_family = AF_BLUETOOTH;
571 bacpy(&addr.rc_bdaddr, dst);
572 addr.rc_channel = channel;
574 err = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
575 if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS))
581 static gboolean rfcomm_set(int sock, int sec_level, int master, GError **err)
583 if (sec_level && !set_sec_level(sock, BT_IO_RFCOMM, sec_level, err))
586 if (master >= 0 && rfcomm_set_master(sock, master) < 0) {
587 ERROR_FAILED(err, "rfcomm_set_master", errno);
594 static int sco_bind(int sock, const bdaddr_t *src, GError **err)
596 struct sockaddr_sco addr;
598 memset(&addr, 0, sizeof(addr));
599 addr.sco_family = AF_BLUETOOTH;
600 bacpy(&addr.sco_bdaddr, src);
602 if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
603 ERROR_FAILED(err, "sco_bind", errno);
610 static int sco_connect(int sock, const bdaddr_t *dst)
612 struct sockaddr_sco addr;
615 memset(&addr, 0, sizeof(addr));
616 addr.sco_family = AF_BLUETOOTH;
617 bacpy(&addr.sco_bdaddr, dst);
619 err = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
620 if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS))
626 static gboolean sco_set(int sock, uint16_t mtu, GError **err)
628 struct sco_options sco_opt;
634 len = sizeof(sco_opt);
635 memset(&sco_opt, 0, len);
636 if (getsockopt(sock, SOL_SCO, SCO_OPTIONS, &sco_opt, &len) < 0) {
637 ERROR_FAILED(err, "getsockopt(SCO_OPTIONS)", errno);
642 if (setsockopt(sock, SOL_SCO, SCO_OPTIONS, &sco_opt,
643 sizeof(sco_opt)) < 0) {
644 ERROR_FAILED(err, "setsockopt(SCO_OPTIONS)", errno);
651 static gboolean parse_set_opts(struct set_opts *opts, GError **err,
652 BtIOOption opt1, va_list args)
654 BtIOOption opt = opt1;
657 memset(opts, 0, sizeof(*opts));
660 opts->defer = DEFAULT_DEFER_TIMEOUT;
662 opts->sec_level = BT_IO_SEC_MEDIUM;
663 opts->mode = L2CAP_MODE_BASIC;
664 opts->flushable = -1;
666 while (opt != BT_IO_OPT_INVALID) {
668 case BT_IO_OPT_SOURCE:
669 str = va_arg(args, const char *);
670 if (strncasecmp(str, "hci", 3) == 0)
671 hci_devba(atoi(str + 3), &opts->src);
673 str2ba(str, &opts->src);
675 case BT_IO_OPT_SOURCE_BDADDR:
676 bacpy(&opts->src, va_arg(args, const bdaddr_t *));
679 str2ba(va_arg(args, const char *), &opts->dst);
681 case BT_IO_OPT_DEST_BDADDR:
682 bacpy(&opts->dst, va_arg(args, const bdaddr_t *));
684 case BT_IO_OPT_DEFER_TIMEOUT:
685 opts->defer = va_arg(args, int);
687 case BT_IO_OPT_SEC_LEVEL:
688 opts->sec_level = va_arg(args, int);
690 case BT_IO_OPT_CHANNEL:
691 opts->channel = va_arg(args, int);
694 opts->psm = va_arg(args, int);
697 opts->cid = va_arg(args, int);
700 opts->mtu = va_arg(args, int);
701 opts->imtu = opts->mtu;
702 opts->omtu = opts->mtu;
705 opts->omtu = va_arg(args, int);
707 opts->mtu = opts->omtu;
710 opts->imtu = va_arg(args, int);
712 opts->mtu = opts->imtu;
714 case BT_IO_OPT_MASTER:
715 opts->master = va_arg(args, gboolean);
718 opts->mode = va_arg(args, int);
720 case BT_IO_OPT_FLUSHABLE:
721 opts->flushable = va_arg(args, gboolean);
724 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
725 "Unknown option %d", opt);
729 opt = va_arg(args, int);
735 static gboolean get_peers(int sock, struct sockaddr *src, struct sockaddr *dst,
736 socklen_t len, GError **err)
742 if (getsockname(sock, src, &olen) < 0) {
743 ERROR_FAILED(err, "getsockname", errno);
749 if (getpeername(sock, dst, &olen) < 0) {
750 ERROR_FAILED(err, "getpeername", errno);
757 static int l2cap_get_info(int sock, uint16_t *handle, uint8_t *dev_class)
759 struct l2cap_conninfo info;
763 if (getsockopt(sock, SOL_L2CAP, L2CAP_CONNINFO, &info, &len) < 0)
767 *handle = info.hci_handle;
770 memcpy(dev_class, info.dev_class, 3);
775 static int l2cap_get_flushable(int sock, gboolean *flushable)
782 if (getsockopt(sock, SOL_BLUETOOTH, BT_FLUSHABLE, &f, &len) < 0)
793 static gboolean l2cap_get(int sock, GError **err, BtIOOption opt1,
796 BtIOOption opt = opt1;
797 struct sockaddr_l2 src, dst;
798 struct l2cap_options l2o;
800 uint8_t dev_class[3];
803 gboolean flushable = FALSE;
806 memset(&l2o, 0, len);
807 if (getsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len) < 0) {
808 ERROR_FAILED(err, "getsockopt(L2CAP_OPTIONS)", errno);
812 if (!get_peers(sock, (struct sockaddr *) &src,
813 (struct sockaddr *) &dst, sizeof(src), err))
816 while (opt != BT_IO_OPT_INVALID) {
818 case BT_IO_OPT_SOURCE:
819 ba2str(&src.l2_bdaddr, va_arg(args, char *));
821 case BT_IO_OPT_SOURCE_BDADDR:
822 bacpy(va_arg(args, bdaddr_t *), &src.l2_bdaddr);
825 ba2str(&dst.l2_bdaddr, va_arg(args, char *));
827 case BT_IO_OPT_DEST_BDADDR:
828 bacpy(va_arg(args, bdaddr_t *), &dst.l2_bdaddr);
830 case BT_IO_OPT_DEFER_TIMEOUT:
832 if (getsockopt(sock, SOL_BLUETOOTH, BT_DEFER_SETUP,
833 va_arg(args, int *), &len) < 0) {
834 ERROR_FAILED(err, "getsockopt(DEFER_SETUP)",
839 case BT_IO_OPT_SEC_LEVEL:
840 if (!get_sec_level(sock, BT_IO_L2CAP,
841 va_arg(args, int *), err))
845 *(va_arg(args, uint16_t *)) = src.l2_psm ?
846 src.l2_psm : dst.l2_psm;
849 *(va_arg(args, uint16_t *)) = src.l2_cid ?
850 src.l2_cid : dst.l2_cid;
853 *(va_arg(args, uint16_t *)) = l2o.omtu;
856 *(va_arg(args, uint16_t *)) = l2o.imtu;
858 case BT_IO_OPT_MASTER:
860 if (getsockopt(sock, SOL_L2CAP, L2CAP_LM, &flags,
862 ERROR_FAILED(err, "getsockopt(L2CAP_LM)",
866 *(va_arg(args, gboolean *)) =
867 (flags & L2CAP_LM_MASTER) ? TRUE : FALSE;
869 case BT_IO_OPT_HANDLE:
870 if (l2cap_get_info(sock, &handle, dev_class) < 0) {
871 ERROR_FAILED(err, "L2CAP_CONNINFO", errno);
874 *(va_arg(args, uint16_t *)) = handle;
876 case BT_IO_OPT_CLASS:
877 if (l2cap_get_info(sock, &handle, dev_class) < 0) {
878 ERROR_FAILED(err, "L2CAP_CONNINFO", errno);
881 memcpy(va_arg(args, uint8_t *), dev_class, 3);
884 *(va_arg(args, uint8_t *)) = l2o.mode;
886 case BT_IO_OPT_FLUSHABLE:
887 if (l2cap_get_flushable(sock, &flushable) < 0) {
888 ERROR_FAILED(err, "get_flushable", errno);
891 *(va_arg(args, gboolean *)) = flushable;
894 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
895 "Unknown option %d", opt);
899 opt = va_arg(args, int);
905 static int rfcomm_get_info(int sock, uint16_t *handle, uint8_t *dev_class)
907 struct rfcomm_conninfo info;
911 if (getsockopt(sock, SOL_RFCOMM, RFCOMM_CONNINFO, &info, &len) < 0)
915 *handle = info.hci_handle;
918 memcpy(dev_class, info.dev_class, 3);
923 static gboolean rfcomm_get(int sock, GError **err, BtIOOption opt1,
926 BtIOOption opt = opt1;
927 struct sockaddr_rc src, dst;
930 uint8_t dev_class[3];
933 if (!get_peers(sock, (struct sockaddr *) &src,
934 (struct sockaddr *) &dst, sizeof(src), err))
937 while (opt != BT_IO_OPT_INVALID) {
939 case BT_IO_OPT_SOURCE:
940 ba2str(&src.rc_bdaddr, va_arg(args, char *));
942 case BT_IO_OPT_SOURCE_BDADDR:
943 bacpy(va_arg(args, bdaddr_t *), &src.rc_bdaddr);
946 ba2str(&dst.rc_bdaddr, va_arg(args, char *));
948 case BT_IO_OPT_DEST_BDADDR:
949 bacpy(va_arg(args, bdaddr_t *), &dst.rc_bdaddr);
951 case BT_IO_OPT_DEFER_TIMEOUT:
953 if (getsockopt(sock, SOL_BLUETOOTH, BT_DEFER_SETUP,
954 va_arg(args, int *), &len) < 0) {
955 ERROR_FAILED(err, "getsockopt(DEFER_SETUP)",
960 case BT_IO_OPT_SEC_LEVEL:
961 if (!get_sec_level(sock, BT_IO_RFCOMM,
962 va_arg(args, int *), err))
965 case BT_IO_OPT_CHANNEL:
966 *(va_arg(args, uint8_t *)) = src.rc_channel ?
967 src.rc_channel : dst.rc_channel;
969 case BT_IO_OPT_SOURCE_CHANNEL:
970 *(va_arg(args, uint8_t *)) = src.rc_channel;
972 case BT_IO_OPT_DEST_CHANNEL:
973 *(va_arg(args, uint8_t *)) = dst.rc_channel;
975 case BT_IO_OPT_MASTER:
977 if (getsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &flags,
979 ERROR_FAILED(err, "getsockopt(RFCOMM_LM)",
983 *(va_arg(args, gboolean *)) =
984 (flags & RFCOMM_LM_MASTER) ? TRUE : FALSE;
986 case BT_IO_OPT_HANDLE:
987 if (rfcomm_get_info(sock, &handle, dev_class) < 0) {
988 ERROR_FAILED(err, "RFCOMM_CONNINFO", errno);
991 *(va_arg(args, uint16_t *)) = handle;
993 case BT_IO_OPT_CLASS:
994 if (rfcomm_get_info(sock, &handle, dev_class) < 0) {
995 ERROR_FAILED(err, "RFCOMM_CONNINFO", errno);
998 memcpy(va_arg(args, uint8_t *), dev_class, 3);
1001 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
1002 "Unknown option %d", opt);
1006 opt = va_arg(args, int);
1012 static int sco_get_info(int sock, uint16_t *handle, uint8_t *dev_class)
1014 struct sco_conninfo info;
1018 if (getsockopt(sock, SOL_SCO, SCO_CONNINFO, &info, &len) < 0)
1022 *handle = info.hci_handle;
1025 memcpy(dev_class, info.dev_class, 3);
1030 static gboolean sco_get(int sock, GError **err, BtIOOption opt1, va_list args)
1032 BtIOOption opt = opt1;
1033 struct sockaddr_sco src, dst;
1034 struct sco_options sco_opt;
1036 uint8_t dev_class[3];
1039 len = sizeof(sco_opt);
1040 memset(&sco_opt, 0, len);
1041 if (getsockopt(sock, SOL_SCO, SCO_OPTIONS, &sco_opt, &len) < 0) {
1042 ERROR_FAILED(err, "getsockopt(SCO_OPTIONS)", errno);
1046 if (!get_peers(sock, (struct sockaddr *) &src,
1047 (struct sockaddr *) &dst, sizeof(src), err))
1050 while (opt != BT_IO_OPT_INVALID) {
1052 case BT_IO_OPT_SOURCE:
1053 ba2str(&src.sco_bdaddr, va_arg(args, char *));
1055 case BT_IO_OPT_SOURCE_BDADDR:
1056 bacpy(va_arg(args, bdaddr_t *), &src.sco_bdaddr);
1058 case BT_IO_OPT_DEST:
1059 ba2str(&dst.sco_bdaddr, va_arg(args, char *));
1061 case BT_IO_OPT_DEST_BDADDR:
1062 bacpy(va_arg(args, bdaddr_t *), &dst.sco_bdaddr);
1065 case BT_IO_OPT_IMTU:
1066 case BT_IO_OPT_OMTU:
1067 *(va_arg(args, uint16_t *)) = sco_opt.mtu;
1069 case BT_IO_OPT_HANDLE:
1070 if (sco_get_info(sock, &handle, dev_class) < 0) {
1071 ERROR_FAILED(err, "SCO_CONNINFO", errno);
1074 *(va_arg(args, uint16_t *)) = handle;
1076 case BT_IO_OPT_CLASS:
1077 if (sco_get_info(sock, &handle, dev_class) < 0) {
1078 ERROR_FAILED(err, "SCO_CONNINFO", errno);
1081 memcpy(va_arg(args, uint8_t *), dev_class, 3);
1084 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
1085 "Unknown option %d", opt);
1089 opt = va_arg(args, int);
1095 static gboolean get_valist(GIOChannel *io, BtIOType type, GError **err,
1096 BtIOOption opt1, va_list args)
1100 sock = g_io_channel_unix_get_fd(io);
1105 return l2cap_get(sock, err, opt1, args);
1107 return rfcomm_get(sock, err, opt1, args);
1109 return sco_get(sock, err, opt1, args);
1112 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
1113 "Unknown BtIO type %d", type);
1117 gboolean bt_io_accept(GIOChannel *io, BtIOConnect connect, gpointer user_data,
1118 GDestroyNotify destroy, GError **err)
1124 sock = g_io_channel_unix_get_fd(io);
1126 memset(&pfd, 0, sizeof(pfd));
1128 pfd.events = POLLOUT;
1130 if (poll(&pfd, 1, 0) < 0) {
1131 ERROR_FAILED(err, "poll", errno);
1135 if (!(pfd.revents & POLLOUT)) {
1137 ret = read(sock, &c, 1);
1140 accept_add(io, connect, user_data, destroy);
1145 gboolean bt_io_set(GIOChannel *io, BtIOType type, GError **err,
1146 BtIOOption opt1, ...)
1150 struct set_opts opts;
1153 va_start(args, opt1);
1154 ret = parse_set_opts(&opts, err, opt1, args);
1160 sock = g_io_channel_unix_get_fd(io);
1165 return l2cap_set(sock, opts.sec_level, opts.imtu, opts.omtu,
1166 opts.mode, opts.master, opts.flushable, err);
1168 return rfcomm_set(sock, opts.sec_level, opts.master, err);
1170 return sco_set(sock, opts.mtu, err);
1173 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
1174 "Unknown BtIO type %d", type);
1178 gboolean bt_io_get(GIOChannel *io, BtIOType type, GError **err,
1179 BtIOOption opt1, ...)
1184 va_start(args, opt1);
1185 ret = get_valist(io, type, err, opt1, args);
1191 static GIOChannel *create_io(BtIOType type, gboolean server,
1192 struct set_opts *opts, GError **err)
1199 sock = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP);
1201 ERROR_FAILED(err, "socket(RAW, L2CAP)", errno);
1204 if (l2cap_bind(sock, &opts->src, server ? opts->psm : 0,
1205 opts->cid, err) < 0)
1207 if (!l2cap_set(sock, opts->sec_level, 0, 0, 0, -1, -1, err))
1211 sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
1213 ERROR_FAILED(err, "socket(SEQPACKET, L2CAP)", errno);
1216 if (l2cap_bind(sock, &opts->src, server ? opts->psm : 0,
1217 opts->cid, err) < 0)
1219 if (!l2cap_set(sock, opts->sec_level, opts->imtu, opts->omtu,
1220 opts->mode, opts->master, opts->flushable, err))
1224 sock = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
1226 ERROR_FAILED(err, "socket(STREAM, RFCOMM)", errno);
1229 if (rfcomm_bind(sock, &opts->src,
1230 server ? opts->channel : 0, err) < 0)
1232 if (!rfcomm_set(sock, opts->sec_level, opts->master, err))
1236 sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
1238 ERROR_FAILED(err, "socket(SEQPACKET, SCO)", errno);
1241 if (sco_bind(sock, &opts->src, err) < 0)
1243 if (!sco_set(sock, opts->mtu, err))
1247 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
1248 "Unknown BtIO type %d", type);
1252 io = g_io_channel_unix_new(sock);
1254 g_io_channel_set_close_on_unref(io, TRUE);
1255 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
1265 GIOChannel *bt_io_connect(BtIOType type, BtIOConnect connect,
1266 gpointer user_data, GDestroyNotify destroy,
1267 GError **gerr, BtIOOption opt1, ...)
1271 struct set_opts opts;
1275 va_start(args, opt1);
1276 ret = parse_set_opts(&opts, gerr, opt1, args);
1282 io = create_io(type, FALSE, &opts, gerr);
1286 sock = g_io_channel_unix_get_fd(io);
1290 err = l2cap_connect(sock, &opts.dst, 0, opts.cid);
1293 err = l2cap_connect(sock, &opts.dst, opts.psm, opts.cid);
1296 err = rfcomm_connect(sock, &opts.dst, opts.channel);
1299 err = sco_connect(sock, &opts.dst);
1302 g_set_error(gerr, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
1303 "Unknown BtIO type %d", type);
1308 g_set_error(gerr, BT_IO_ERROR, BT_IO_ERROR_CONNECT_FAILED,
1309 "connect: %s (%d)", strerror(-err), -err);
1310 g_io_channel_unref(io);
1314 connect_add(io, connect, user_data, destroy);
1319 GIOChannel *bt_io_listen(BtIOType type, BtIOConnect connect,
1320 BtIOConfirm confirm, gpointer user_data,
1321 GDestroyNotify destroy, GError **err,
1322 BtIOOption opt1, ...)
1326 struct set_opts opts;
1330 if (type == BT_IO_L2RAW) {
1331 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
1332 "Server L2CAP RAW sockets not supported");
1336 va_start(args, opt1);
1337 ret = parse_set_opts(&opts, err, opt1, args);
1343 io = create_io(type, TRUE, &opts, err);
1347 sock = g_io_channel_unix_get_fd(io);
1350 setsockopt(sock, SOL_BLUETOOTH, BT_DEFER_SETUP, &opts.defer,
1351 sizeof(opts.defer));
1353 if (listen(sock, 5) < 0) {
1354 ERROR_FAILED(err, "listen", errno);
1355 g_io_channel_unref(io);
1359 server_add(io, connect, confirm, user_data, destroy);
1364 GQuark bt_io_error_quark(void)
1366 return g_quark_from_static_string("bt-io-error-quark");