1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2004-2009 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 "logging.h"
38
39 #include "glib-helper.h"
40 #include "btio.h"
41 #include "adapter.h"
42 #include "device.h"
43 #include "server.h"
44
45 static GSList *servers = NULL;
46 struct input_server {
47 bdaddr_t src;
48 GIOChannel *ctrl;
49 GIOChannel *intr;
50 GIOChannel *confirm;
51 };
52
server_cmp(gconstpointer s,gconstpointer user_data)53 static 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
connect_event_cb(GIOChannel * chan,GError * err,gpointer data)61 static 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 debug("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 err, sk = g_io_channel_unix_get_fd(chan);
95 err = write(sk, &unplug, sizeof(unplug));
96 }
97
98 g_io_channel_shutdown(chan, TRUE, NULL);
99 }
100
auth_callback(DBusError * derr,void * user_data)101 static void auth_callback(DBusError *derr, void *user_data)
102 {
103 struct input_server *server = user_data;
104 bdaddr_t src, dst;
105 GError *err = NULL;
106
107 bt_io_get(server->confirm, BT_IO_L2CAP, &err,
108 BT_IO_OPT_SOURCE_BDADDR, &src,
109 BT_IO_OPT_DEST_BDADDR, &dst,
110 BT_IO_OPT_INVALID);
111 if (err) {
112 error("%s", err->message);
113 g_error_free(err);
114 goto reject;
115 }
116
117 if (derr) {
118 error("Access denied: %s", derr->message);
119 goto reject;
120 }
121
122 if (!bt_io_accept(server->confirm, connect_event_cb, server,
123 NULL, &err)) {
124 error("bt_io_accept: %s", err->message);
125 g_error_free(err);
126 goto reject;
127 }
128
129 g_io_channel_unref(server->confirm);
130 server->confirm = NULL;
131
132 return;
133
134 reject:
135 g_io_channel_shutdown(server->confirm, TRUE, NULL);
136 g_io_channel_unref(server->confirm);
137 server->confirm = NULL;
138 input_device_close_channels(&src, &dst);
139 }
140
confirm_event_cb(GIOChannel * chan,gpointer user_data)141 static void confirm_event_cb(GIOChannel *chan, gpointer user_data)
142 {
143 struct input_server *server = user_data;
144 bdaddr_t src, dst;
145 GError *err = NULL;
146 int ret;
147
148 bt_io_get(chan, BT_IO_L2CAP, &err,
149 BT_IO_OPT_SOURCE_BDADDR, &src,
150 BT_IO_OPT_DEST_BDADDR, &dst,
151 BT_IO_OPT_INVALID);
152 if (err) {
153 error("%s", err->message);
154 g_error_free(err);
155 goto drop;
156 }
157
158 if (server->confirm) {
159 error("Refusing connection: setup in progress");
160 goto drop;
161 }
162
163 server->confirm = g_io_channel_ref(chan);
164
165 ret = btd_request_authorization(&src, &dst, HID_UUID,
166 auth_callback, server);
167 if (ret == 0)
168 return;
169
170 g_io_channel_unref(server->confirm);
171 server->confirm = NULL;
172
173 drop:
174 input_device_close_channels(&src, &dst);
175 g_io_channel_shutdown(chan, TRUE, NULL);
176 }
177
server_start(const bdaddr_t * src)178 int server_start(const bdaddr_t *src)
179 {
180 struct input_server *server;
181 GError *err = NULL;
182
183 server = g_new0(struct input_server, 1);
184 bacpy(&server->src, src);
185
186 server->ctrl = bt_io_listen(BT_IO_L2CAP, connect_event_cb, NULL,
187 server, NULL, &err,
188 BT_IO_OPT_SOURCE_BDADDR, src,
189 BT_IO_OPT_PSM, L2CAP_PSM_HIDP_CTRL,
190 BT_IO_OPT_INVALID);
191 if (!server->ctrl) {
192 error("Failed to listen on control channel");
193 g_error_free(err);
194 g_free(server);
195 return -1;
196 }
197
198 server->intr = bt_io_listen(BT_IO_L2CAP, NULL, confirm_event_cb,
199 server, NULL, &err,
200 BT_IO_OPT_SOURCE_BDADDR, src,
201 BT_IO_OPT_PSM, L2CAP_PSM_HIDP_INTR,
202 BT_IO_OPT_INVALID);
203 if (!server->intr) {
204 error("Failed to listen on interrupt channel");
205 g_io_channel_unref(server->ctrl);
206 g_error_free(err);
207 g_free(server);
208 return -1;
209 }
210
211 servers = g_slist_append(servers, server);
212
213 return 0;
214 }
215
server_stop(const bdaddr_t * src)216 void server_stop(const bdaddr_t *src)
217 {
218 struct input_server *server;
219 GSList *l;
220
221 l = g_slist_find_custom(servers, src, server_cmp);
222 if (!l)
223 return;
224
225 server = l->data;
226
227 g_io_channel_shutdown(server->intr, TRUE, NULL);
228 g_io_channel_unref(server->intr);
229
230 g_io_channel_shutdown(server->ctrl, TRUE, NULL);
231 g_io_channel_unref(server->ctrl);
232
233 servers = g_slist_remove(servers, server);
234 g_free(server);
235 }
236