6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: server.c,v 1.9 2006/09/07 21:06:53 max Exp $
36 #include <sys/queue.h>
38 #define L2CAP_SOCKET_CHECKED
39 #include <bluetooth.h>
40 #include <dev/evdev/input.h>
41 #include <dev/vkbd/vkbd_var.h>
50 #include "bthid_config.h"
56 #define max(x, y) (((x) > (y))? (x) : (y))
58 static int32_t server_accept (bthid_server_p srv, int32_t fd);
59 static int32_t server_process(bthid_server_p srv, int32_t fd);
66 server_init(bthid_server_p srv)
68 struct sockaddr_l2cap l2addr;
72 srv->ctrl = srv->intr = -1;
73 FD_ZERO(&srv->rfdset);
74 FD_ZERO(&srv->wfdset);
75 LIST_INIT(&srv->sessions);
77 /* Open /dev/consolectl */
78 srv->cons = open("/dev/consolectl", O_RDWR);
80 syslog(LOG_ERR, "Could not open /dev/consolectl. %s (%d)",
81 strerror(errno), errno);
85 /* Create control socket */
86 srv->ctrl = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
88 syslog(LOG_ERR, "Could not create control L2CAP socket. " \
89 "%s (%d)", strerror(errno), errno);
94 l2addr.l2cap_len = sizeof(l2addr);
95 l2addr.l2cap_family = AF_BLUETOOTH;
96 memcpy(&l2addr.l2cap_bdaddr, &srv->bdaddr, sizeof(l2addr.l2cap_bdaddr));
97 l2addr.l2cap_psm = htole16(0x11);
98 l2addr.l2cap_bdaddr_type = BDADDR_BREDR;
101 if (bind(srv->ctrl, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
102 syslog(LOG_ERR, "Could not bind control L2CAP socket. " \
103 "%s (%d)", strerror(errno), errno);
109 if (listen(srv->ctrl, 10) < 0) {
110 syslog(LOG_ERR, "Could not listen on control L2CAP socket. " \
111 "%s (%d)", strerror(errno), errno);
117 /* Create intrrupt socket */
118 srv->intr = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
120 syslog(LOG_ERR, "Could not create interrupt L2CAP socket. " \
121 "%s (%d)", strerror(errno), errno);
127 l2addr.l2cap_psm = htole16(0x13);
129 if (bind(srv->intr, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
130 syslog(LOG_ERR, "Could not bind interrupt L2CAP socket. " \
131 "%s (%d)", strerror(errno), errno);
138 if (listen(srv->intr, 10) < 0) {
139 syslog(LOG_ERR, "Could not listen on interrupt L2CAP socket. "\
140 "%s (%d)", strerror(errno), errno);
147 FD_SET(srv->ctrl, &srv->rfdset);
148 FD_SET(srv->intr, &srv->rfdset);
149 srv->maxfd = max(srv->ctrl, srv->intr);
159 server_shutdown(bthid_server_p srv)
167 while (!LIST_EMPTY(&srv->sessions))
168 session_close(LIST_FIRST(&srv->sessions));
170 memset(srv, 0, sizeof(*srv));
174 * Do one server iteration
178 server_do(bthid_server_p srv)
181 fd_set rfdset, wfdset;
189 /* Copy cached version of the fd sets and call select */
190 memcpy(&rfdset, &srv->rfdset, sizeof(rfdset));
191 memcpy(&wfdset, &srv->wfdset, sizeof(wfdset));
193 n = select(srv->maxfd + 1, &rfdset, &wfdset, NULL, &tv);
198 syslog(LOG_ERR, "Could not select(%d, %p, %p). %s (%d)",
199 srv->maxfd + 1, &rfdset, &wfdset, strerror(errno), errno);
204 /* Process descriptors (if any) */
205 for (fd = 0; fd < srv->maxfd + 1 && n > 0; fd ++) {
206 if (FD_ISSET(fd, &rfdset)) {
209 if (fd == srv->ctrl || fd == srv->intr)
210 server_accept(srv, fd);
212 server_process(srv, fd);
213 } else if (FD_ISSET(fd, &wfdset)) {
216 client_connect(srv, fd);
224 * Accept new connection
228 server_accept(bthid_server_p srv, int32_t fd)
232 struct sockaddr_l2cap l2addr;
236 len = sizeof(l2addr);
237 if ((new_fd = accept(fd, (struct sockaddr *) &l2addr, &len)) < 0) {
238 syslog(LOG_ERR, "Could not accept %s connection. %s (%d)",
239 (fd == srv->ctrl)? "control" : "interrupt",
240 strerror(errno), errno);
244 /* Is device configured? */
245 if ((d = get_hid_device(&l2addr.l2cap_bdaddr)) == NULL) {
246 syslog(LOG_ERR, "Rejecting %s connection from %s. " \
247 "Device not configured",
248 (fd == srv->ctrl)? "control" : "interrupt",
249 bt_ntoa(&l2addr.l2cap_bdaddr, NULL));
254 /* Check if we have session for the device */
255 if ((s = session_by_bdaddr(srv, &l2addr.l2cap_bdaddr)) == NULL) {
256 d->new_device = 0; /* reset new device flag */
259 /* Create new inbound session */
260 if ((s = session_open(srv, d)) == NULL) {
261 syslog(LOG_CRIT, "Could not open inbound session "
262 "for %s", bt_ntoa(&l2addr.l2cap_bdaddr, NULL));
268 /* Update descriptors */
269 if (fd == srv->ctrl) {
270 assert(s->ctrl == -1);
272 s->state = (s->intr == -1)? W4INTR : OPEN;
274 assert(s->intr == -1);
276 s->state = (s->ctrl == -1)? W4CTRL : OPEN;
279 FD_SET(new_fd, &srv->rfdset);
280 if (new_fd > srv->maxfd)
283 syslog(LOG_NOTICE, "Accepted %s connection from %s",
284 (fd == srv->ctrl)? "control" : "interrupt",
285 bt_ntoa(&l2addr.l2cap_bdaddr, NULL));
287 /* Create virtual kbd/mouse after both channels are established */
288 if (s->state == OPEN && session_run(s) < 0) {
297 * Process data on the connection
301 server_process(bthid_server_p srv, int32_t fd)
303 bthid_session_p s = session_by_fd(srv, fd);
304 int32_t len, to_read;
305 int32_t (*cb)(bthid_session_p, uint8_t *, int32_t);
309 struct input_event ie;
313 return (0); /* can happen on device disconnect */
318 to_read = sizeof(data.b);
319 } else if (fd == s->intr) {
321 to_read = sizeof(data.b);
322 } else if (fd == s->ukbd) {
323 cb = uinput_kbd_status_changed;
324 to_read = sizeof(data.ie);
326 assert(fd == s->vkbd);
328 cb = kbd_status_changed;
329 to_read = sizeof(data.s);
333 len = read(fd, &data, to_read);
334 } while (len < 0 && errno == EINTR);
337 syslog(LOG_ERR, "Could not read data from %s (%s). %s (%d)",
338 bt_ntoa(&s->bdaddr, NULL),
339 (fd == s->ctrl)? "control" : "interrupt",
340 strerror(errno), errno);
346 syslog(LOG_NOTICE, "Remote device %s has closed %s connection",
347 bt_ntoa(&s->bdaddr, NULL),
348 (fd == s->ctrl)? "control" : "interrupt");
353 (*cb)(s, (uint8_t *) &data, len);