xref: /freebsd/usr.sbin/bluetooth/bthidd/server.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*
2  * server.c
3  *
4  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $Id: server.c,v 1.7 2004/11/17 21:59:42 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <sys/queue.h>
33 #include <assert.h>
34 #include <bluetooth.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <syslog.h>
41 #include <unistd.h>
42 #include <usbhid.h>
43 #include "bthidd.h"
44 #include "bthid_config.h"
45 #include "kbd.h"
46 
47 #undef	max
48 #define	max(x, y)	(((x) > (y))? (x) : (y))
49 
50 static int	server_accept (bthid_server_p srv, int fd);
51 static int	server_process(bthid_server_p srv, int fd);
52 
53 /*
54  * Initialize server
55  */
56 
57 int
58 server_init(bthid_server_p srv)
59 {
60 	struct sockaddr_l2cap	l2addr;
61 
62 	assert(srv != NULL);
63 
64 	srv->ctrl = srv->intr = -1;
65 	FD_ZERO(&srv->rfdset);
66 	FD_ZERO(&srv->wfdset);
67 	LIST_INIT(&srv->sessions);
68 
69 	/* Allocate HID keycodes buffer */
70 	srv->keys = bit_alloc(kbd_maxkey());
71 	if (srv->keys == NULL) {
72 		syslog(LOG_ERR, "Could not allocate HID keys buffer");
73 		return (-1);
74 	}
75 	memset(srv->keys, 0, bitstr_size(kbd_maxkey()));
76 
77 	/* Get wired keyboard index (if was not specified) */
78 	if (srv->windex == -1) {
79 		srv->windex = kbd_get_index("/dev/console");
80 		if (srv->windex < 0) {
81 			syslog(LOG_ERR, "Could not open get wired keyboard " \
82 				"index. %s (%d)", strerror(errno), errno);
83 			free(srv->keys);
84 			return (-1);
85 		}
86 	}
87 
88 	/* Open /dev/consolectl */
89 	srv->cons = open("/dev/consolectl", O_RDWR);
90 	if (srv->cons < 0) {
91 		syslog(LOG_ERR, "Could not open /dev/consolectl. %s (%d)",
92 			strerror(errno), errno);
93 		return (-1);
94 	}
95 
96 	/* Open /dev/vkbdctl */
97 	srv->vkbd = open("/dev/vkbdctl", O_RDWR);
98 	if (srv->vkbd < 0) {
99 		syslog(LOG_ERR, "Could not open /dev/vkbdctl. %s (%d)",
100 			strerror(errno), errno);
101 		close(srv->cons);
102 		free(srv->keys);
103 		return (-1);
104 	}
105 
106 	/* Create control socket */
107 	srv->ctrl = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
108 	if (srv->ctrl < 0) {
109 		syslog(LOG_ERR, "Could not create control L2CAP socket. " \
110 			"%s (%d)", strerror(errno), errno);
111 		close(srv->vkbd);
112 		close(srv->cons);
113 		free(srv->keys);
114 		return (-1);
115 	}
116 
117 	l2addr.l2cap_len = sizeof(l2addr);
118 	l2addr.l2cap_family = AF_BLUETOOTH;
119 	memcpy(&l2addr.l2cap_bdaddr, &srv->bdaddr, sizeof(l2addr.l2cap_bdaddr));
120 	l2addr.l2cap_psm =  0x11;
121 
122 	if (bind(srv->ctrl, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
123 		syslog(LOG_ERR, "Could not bind control L2CAP socket. " \
124 			"%s (%d)", strerror(errno), errno);
125 		close(srv->ctrl);
126 		close(srv->vkbd);
127 		close(srv->cons);
128 		free(srv->keys);
129 		return (-1);
130 	}
131 
132 	if (listen(srv->ctrl, 10) < 0) {
133 		syslog(LOG_ERR, "Could not listen on control L2CAP socket. " \
134 			"%s (%d)", strerror(errno), errno);
135 		close(srv->ctrl);
136 		close(srv->vkbd);
137 		close(srv->cons);
138 		free(srv->keys);
139 		return (-1);
140 	}
141 
142 	/* Create intrrupt socket */
143 	srv->intr = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
144 	if (srv->intr < 0) {
145 		syslog(LOG_ERR, "Could not create interrupt L2CAP socket. " \
146 			"%s (%d)", strerror(errno), errno);
147 		close(srv->ctrl);
148 		close(srv->vkbd);
149 		close(srv->cons);
150 		free(srv->keys);
151 		return (-1);
152 	}
153 
154 	l2addr.l2cap_psm = 0x13;
155 
156 	if (bind(srv->intr, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
157 		syslog(LOG_ERR, "Could not bind interrupt L2CAP socket. " \
158 			"%s (%d)", strerror(errno), errno);
159 		close(srv->intr);
160 		close(srv->ctrl);
161 		close(srv->vkbd);
162 		close(srv->cons);
163 		free(srv->keys);
164 		return (-1);
165 	}
166 
167 	if (listen(srv->intr, 10) < 0) {
168 		syslog(LOG_ERR, "Could not listen on interrupt L2CAP socket. "\
169 			"%s (%d)", strerror(errno), errno);
170 		close(srv->intr);
171 		close(srv->ctrl);
172 		close(srv->vkbd);
173 		close(srv->cons);
174 		free(srv->keys);
175 		return (-1);
176 	}
177 
178 	FD_SET(srv->ctrl, &srv->rfdset);
179 	FD_SET(srv->intr, &srv->rfdset);
180 	srv->maxfd = max(srv->ctrl, srv->intr);
181 
182 	return (0);
183 }
184 
185 /*
186  * Shutdown server
187  */
188 
189 void
190 server_shutdown(bthid_server_p srv)
191 {
192 	assert(srv != NULL);
193 
194 	close(srv->cons);
195 	close(srv->vkbd);
196 	close(srv->ctrl);
197 	close(srv->intr);
198 
199 	while (!LIST_EMPTY(&srv->sessions))
200 		session_close(LIST_FIRST(&srv->sessions));
201 
202 	free(srv->keys);
203 
204 	memset(srv, 0, sizeof(*srv));
205 }
206 
207 /*
208  * Do one server iteration
209  */
210 
211 int
212 server_do(bthid_server_p srv)
213 {
214 	struct timeval	tv;
215 	fd_set		rfdset, wfdset;
216 	int		n, fd;
217 
218 	assert(srv != NULL);
219 
220 	tv.tv_sec = 1;
221 	tv.tv_usec = 0;
222 
223 	/* Copy cached version of the fd sets and call select */
224 	memcpy(&rfdset, &srv->rfdset, sizeof(rfdset));
225 	memcpy(&wfdset, &srv->wfdset, sizeof(wfdset));
226 
227 	n = select(srv->maxfd + 1, &rfdset, &wfdset, NULL, &tv);
228 	if (n < 0) {
229 		if (errno == EINTR)
230 			return (0);
231 
232 		syslog(LOG_ERR, "Could not select(%d, %p, %p). %s (%d)",
233 			srv->maxfd + 1, &rfdset, &wfdset, strerror(errno), errno);
234 
235 		return (-1);
236 	}
237 
238 	/* Process descriptors (if any) */
239 	for (fd = 0; fd < srv->maxfd + 1 && n > 0; fd ++) {
240 		if (FD_ISSET(fd, &rfdset)) {
241 			n --;
242 
243 			if (fd == srv->ctrl || fd == srv->intr)
244 				server_accept(srv, fd);
245 			else
246 				server_process(srv, fd);
247 		} else if (FD_ISSET(fd, &wfdset)) {
248 			n --;
249 
250 			client_connect(srv, fd);
251 		}
252 	}
253 
254 	return (0);
255 }
256 
257 /*
258  * Accept new connection
259  */
260 
261 static int
262 server_accept(bthid_server_p srv, int fd)
263 {
264 	bthid_session_p		s = NULL;
265 	hid_device_p		d = NULL;
266 	struct sockaddr_l2cap	l2addr;
267 	int			len, new_fd;
268 
269 	len = sizeof(l2addr);
270 	if ((new_fd = accept(fd, (struct sockaddr *) &l2addr, &len)) < 0) {
271 		syslog(LOG_ERR, "Could not accept %s connection. %s (%d)",
272 			(fd == srv->ctrl)? "control" : "interrupt",
273 			strerror(errno), errno);
274 		return (-1);
275 	}
276 
277 	/* Check if we have session for the device */
278 	if ((s = session_by_bdaddr(srv, &l2addr.l2cap_bdaddr)) == NULL) {
279 		/* Is device configured? */
280 		if ((d = get_hid_device(&l2addr.l2cap_bdaddr)) == NULL) {
281 			syslog(LOG_ERR, "Rejecting %s connection from %s. " \
282 				"Device not configured",
283 				(fd == srv->ctrl)? "control" : "interrupt",
284 				bt_ntoa(&l2addr.l2cap_bdaddr, NULL));
285 			close(new_fd);
286 			return (-1);
287 		}
288 
289 		d->new_device = 0; /* reset new device flag */
290 		write_hids_file();
291 
292 		/* Create new inbound session */
293 		if ((s = session_open(srv, &l2addr.l2cap_bdaddr)) == NULL) {
294 			syslog(LOG_CRIT, "Could not open inbound session " \
295 				"for %s. Not enough memory",
296 				bt_ntoa(&l2addr.l2cap_bdaddr, NULL));
297 			close(new_fd);
298 			return (-1);
299 		}
300 	}
301 
302 	/* Update descriptors */
303 	if (fd == srv->ctrl) {
304 		assert(s->ctrl == -1);
305 		s->ctrl = new_fd;
306 		s->state = (s->intr == -1)? W4INTR : OPEN;
307 	} else {
308 		assert(s->intr == -1);
309 		s->intr = new_fd;
310 		s->state = (s->ctrl == -1)? W4CTRL : OPEN;
311 	}
312 
313 	FD_SET(new_fd, &srv->rfdset);
314 	if (new_fd > srv->maxfd)
315 		srv->maxfd = new_fd;
316 
317 	syslog(LOG_NOTICE, "Accepted %s connection from %s",
318 		(fd == srv->ctrl)? "control" : "interrupt",
319 		bt_ntoa(&l2addr.l2cap_bdaddr, NULL));
320 
321 	return (0);
322 }
323 
324 /*
325  * Process data on the connection
326  */
327 
328 static int
329 server_process(bthid_server_p srv, int fd)
330 {
331 	bthid_session_p	s = session_by_fd(srv, fd);
332 	char		data[1024];
333 	int		len;
334 
335 	assert(s != NULL);
336 
337 	do {
338 		len = read(fd, data, sizeof(data));
339 	} while (len < 0 && errno == EINTR);
340 
341 	if (len < 0) {
342 		syslog(LOG_ERR, "Could not read data from %s (%s). %s (%d)",
343 			bt_ntoa(&s->bdaddr, NULL),
344 			(fd == s->ctrl)? "control" : "interrupt",
345 			strerror(errno), errno);
346 		session_close(s);
347 		return (0);
348 	}
349 
350 	if (len == 0) {
351 		syslog(LOG_NOTICE, "Remote device %s has closed %s connection",
352 			bt_ntoa(&s->bdaddr, NULL),
353 			(fd == s->ctrl)? "control" : "interrupt");
354 		session_close(s);
355 		return (0);
356 	}
357 
358 	if (fd == s->ctrl)
359 		hid_control(s, data, len);
360 	else
361 		hid_interrupt(s, data, len);
362 
363 	return (0);
364 }
365 
366