1/* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org> 6 * 7 * 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. 12 * 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. 17 * 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 21 * 22 */ 23 24#ifdef HAVE_CONFIG_H 25#include <config.h> 26#endif 27 28#include <unistd.h> 29#include <errno.h> 30 31#include <bluetooth/bluetooth.h> 32#include <bluetooth/sdp.h> 33 34#include <glib.h> 35#include <dbus/dbus.h> 36 37#include "log.h" 38 39#include "glib-helper.h" 40#include "btio.h" 41#include "adapter.h" 42#include "device.h" 43#include "server.h" 44 45static GSList *servers = NULL; 46struct input_server { 47 bdaddr_t src; 48 GIOChannel *ctrl; 49 GIOChannel *intr; 50 GIOChannel *confirm; 51}; 52 53static gint server_cmp(gconstpointer s, gconstpointer user_data) 54{ 55 const struct input_server *server = s; 56 const bdaddr_t *src = user_data; 57 58 return bacmp(&server->src, src); 59} 60 61static void connect_event_cb(GIOChannel *chan, GError *err, gpointer data) 62{ 63 uint16_t psm; 64 bdaddr_t src, dst; 65 GError *gerr = NULL; 66 int ret; 67 68 if (err) { 69 error("%s", err->message); 70 return; 71 } 72 73 bt_io_get(chan, BT_IO_L2CAP, &gerr, 74 BT_IO_OPT_SOURCE_BDADDR, &src, 75 BT_IO_OPT_DEST_BDADDR, &dst, 76 BT_IO_OPT_PSM, &psm, 77 BT_IO_OPT_INVALID); 78 if (gerr) { 79 error("%s", gerr->message); 80 g_error_free(gerr); 81 g_io_channel_shutdown(chan, TRUE, NULL); 82 return; 83 } 84 85 DBG("Incoming connection on PSM %d", psm); 86 87 ret = input_device_set_channel(&src, &dst, psm, chan); 88 if (ret == 0) 89 return; 90 91 /* Send unplug virtual cable to unknown devices */ 92 if (ret == -ENOENT && psm == L2CAP_PSM_HIDP_CTRL) { 93 unsigned char unplug = 0x15; 94 int sk = g_io_channel_unix_get_fd(chan); 95 if (write(sk, &unplug, sizeof(unplug)) < 0) 96 error("Unable to send virtual cable unplug"); 97 } 98 99 g_io_channel_shutdown(chan, TRUE, NULL); 100} 101 102static void auth_callback(DBusError *derr, void *user_data) 103{ 104 struct input_server *server = user_data; 105 bdaddr_t src, dst; 106 GError *err = NULL; 107 108 bt_io_get(server->confirm, BT_IO_L2CAP, &err, 109 BT_IO_OPT_SOURCE_BDADDR, &src, 110 BT_IO_OPT_DEST_BDADDR, &dst, 111 BT_IO_OPT_INVALID); 112 if (err) { 113 error("%s", err->message); 114 g_error_free(err); 115 goto reject; 116 } 117 118 if (derr) { 119 error("Access denied: %s", derr->message); 120 goto reject; 121 } 122 123 if (!bt_io_accept(server->confirm, connect_event_cb, server, 124 NULL, &err)) { 125 error("bt_io_accept: %s", err->message); 126 g_error_free(err); 127 goto reject; 128 } 129 130 g_io_channel_unref(server->confirm); 131 server->confirm = NULL; 132 133 return; 134 135reject: 136 g_io_channel_shutdown(server->confirm, TRUE, NULL); 137 g_io_channel_unref(server->confirm); 138 server->confirm = NULL; 139 input_device_close_channels(&src, &dst); 140} 141 142static void confirm_event_cb(GIOChannel *chan, gpointer user_data) 143{ 144 struct input_server *server = user_data; 145 bdaddr_t src, dst; 146 GError *err = NULL; 147 int ret; 148 149 bt_io_get(chan, BT_IO_L2CAP, &err, 150 BT_IO_OPT_SOURCE_BDADDR, &src, 151 BT_IO_OPT_DEST_BDADDR, &dst, 152 BT_IO_OPT_INVALID); 153 if (err) { 154 error("%s", err->message); 155 g_error_free(err); 156 goto drop; 157 } 158 159 if (server->confirm) { 160 error("Refusing connection: setup in progress"); 161 goto drop; 162 } 163 164 server->confirm = g_io_channel_ref(chan); 165 166 ret = btd_request_authorization(&src, &dst, HID_UUID, 167 auth_callback, server); 168 if (ret == 0) 169 return; 170 171 g_io_channel_unref(server->confirm); 172 server->confirm = NULL; 173 174drop: 175 input_device_close_channels(&src, &dst); 176 g_io_channel_shutdown(chan, TRUE, NULL); 177} 178 179int server_start(const bdaddr_t *src) 180{ 181 struct input_server *server; 182 GError *err = NULL; 183 184 server = g_new0(struct input_server, 1); 185 bacpy(&server->src, src); 186 187 server->ctrl = bt_io_listen(BT_IO_L2CAP, connect_event_cb, NULL, 188 server, NULL, &err, 189 BT_IO_OPT_SOURCE_BDADDR, src, 190 BT_IO_OPT_PSM, L2CAP_PSM_HIDP_CTRL, 191 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW, 192 BT_IO_OPT_POWER_ACTIVE, 0, 193 BT_IO_OPT_INVALID); 194 if (!server->ctrl) { 195 error("Failed to listen on control channel"); 196 g_error_free(err); 197 g_free(server); 198 return -1; 199 } 200 201 server->intr = bt_io_listen(BT_IO_L2CAP, NULL, confirm_event_cb, 202 server, NULL, &err, 203 BT_IO_OPT_SOURCE_BDADDR, src, 204 BT_IO_OPT_PSM, L2CAP_PSM_HIDP_INTR, 205 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW, 206 BT_IO_OPT_POWER_ACTIVE, 0, 207 BT_IO_OPT_INVALID); 208 if (!server->intr) { 209 error("Failed to listen on interrupt channel"); 210 g_io_channel_unref(server->ctrl); 211 g_error_free(err); 212 g_free(server); 213 return -1; 214 } 215 216 servers = g_slist_append(servers, server); 217 218 return 0; 219} 220 221void server_stop(const bdaddr_t *src) 222{ 223 struct input_server *server; 224 GSList *l; 225 226 l = g_slist_find_custom(servers, src, server_cmp); 227 if (!l) 228 return; 229 230 server = l->data; 231 232 g_io_channel_shutdown(server->intr, TRUE, NULL); 233 g_io_channel_unref(server->intr); 234 235 g_io_channel_shutdown(server->ctrl, TRUE, NULL); 236 g_io_channel_unref(server->ctrl); 237 238 servers = g_slist_remove(servers, server); 239 g_free(server); 240} 241