3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2004-2010 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 <bluetooth/bluetooth.h>
32
#include <bluetooth/sdp.h>
33
#include <bluetooth/uuid.h>
36
#include <dbus/dbus.h>
40
#include "glib-helper.h"
46
static GSList *servers = NULL;
54
static gint server_cmp(gconstpointer s, gconstpointer user_data)
56
const struct input_server *server = s;
57
const bdaddr_t *src = user_data;
59
return bacmp(&server->src, src);
62
static void connect_event_cb(GIOChannel *chan, GError *err, gpointer data)
71
error("%s", err->message);
75
bt_io_get(chan, BT_IO_L2CAP, &gerr,
76
BT_IO_OPT_SOURCE_BDADDR, &src,
77
BT_IO_OPT_DEST_BDADDR, &dst,
81
error("%s", gerr->message);
83
g_io_channel_shutdown(chan, TRUE, NULL);
87
ba2str(&dst, address);
88
DBG("Incoming connection from %s on PSM %d", address, psm);
90
ret = input_device_set_channel(&src, &dst, psm, chan);
94
error("Refusing input device connect: %s (%d)", strerror(-ret), -ret);
96
/* Send unplug virtual cable to unknown devices */
97
if (ret == -ENOENT && psm == L2CAP_PSM_HIDP_CTRL) {
98
unsigned char unplug = 0x15;
99
int sk = g_io_channel_unix_get_fd(chan);
100
if (write(sk, &unplug, sizeof(unplug)) < 0)
101
error("Unable to send virtual cable unplug");
104
g_io_channel_shutdown(chan, TRUE, NULL);
107
static void auth_callback(DBusError *derr, void *user_data)
109
struct input_server *server = user_data;
113
bt_io_get(server->confirm, BT_IO_L2CAP, &err,
114
BT_IO_OPT_SOURCE_BDADDR, &src,
115
BT_IO_OPT_DEST_BDADDR, &dst,
118
error("%s", err->message);
124
error("Access denied: %s", derr->message);
128
if (!bt_io_accept(server->confirm, connect_event_cb, server,
130
error("bt_io_accept: %s", err->message);
135
g_io_channel_unref(server->confirm);
136
server->confirm = NULL;
141
g_io_channel_shutdown(server->confirm, TRUE, NULL);
142
g_io_channel_unref(server->confirm);
143
server->confirm = NULL;
144
input_device_close_channels(&src, &dst);
147
static void confirm_event_cb(GIOChannel *chan, gpointer user_data)
149
struct input_server *server = user_data;
155
bt_io_get(chan, BT_IO_L2CAP, &err,
156
BT_IO_OPT_SOURCE_BDADDR, &src,
157
BT_IO_OPT_DEST_BDADDR, &dst,
160
error("%s", err->message);
165
if (server->confirm) {
168
ba2str(&dst, address);
169
error("Refusing connection from %s: setup in progress",
174
server->confirm = g_io_channel_ref(chan);
176
ret = btd_request_authorization(&src, &dst, HID_UUID,
177
auth_callback, server);
182
error("input: authorization for %s failed: %s (%d)",
183
addr, strerror(-ret), ret);
185
g_io_channel_unref(server->confirm);
186
server->confirm = NULL;
189
input_device_close_channels(&src, &dst);
190
g_io_channel_shutdown(chan, TRUE, NULL);
193
int server_start(const bdaddr_t *src)
195
struct input_server *server;
198
server = g_new0(struct input_server, 1);
199
bacpy(&server->src, src);
201
server->ctrl = bt_io_listen(BT_IO_L2CAP, connect_event_cb, NULL,
203
BT_IO_OPT_SOURCE_BDADDR, src,
204
BT_IO_OPT_PSM, L2CAP_PSM_HIDP_CTRL,
205
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
208
error("Failed to listen on control channel");
214
server->intr = bt_io_listen(BT_IO_L2CAP, NULL, confirm_event_cb,
216
BT_IO_OPT_SOURCE_BDADDR, src,
217
BT_IO_OPT_PSM, L2CAP_PSM_HIDP_INTR,
218
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
221
error("Failed to listen on interrupt channel");
222
g_io_channel_unref(server->ctrl);
228
servers = g_slist_append(servers, server);
233
void server_stop(const bdaddr_t *src)
235
struct input_server *server;
238
l = g_slist_find_custom(servers, src, server_cmp);
244
g_io_channel_shutdown(server->intr, TRUE, NULL);
245
g_io_channel_unref(server->intr);
247
g_io_channel_shutdown(server->ctrl, TRUE, NULL);
248
g_io_channel_unref(server->ctrl);
250
servers = g_slist_remove(servers, server);