6 * Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: server.c,v 1.9 2006/09/07 21:06:53 max Exp $
34 #include <sys/queue.h>
36 #include <bluetooth.h>
37 #include <dev/vkbd/vkbd_var.h>
46 #include "bthid_config.h"
51 #define max(x, y) (((x) > (y))? (x) : (y))
53 static int32_t server_accept (bthid_server_p srv, int32_t fd);
54 static int32_t server_process(bthid_server_p srv, int32_t fd);
61 server_init(bthid_server_p srv)
63 struct sockaddr_l2cap l2addr;
67 srv->ctrl = srv->intr = -1;
68 FD_ZERO(&srv->rfdset);
69 FD_ZERO(&srv->wfdset);
70 LIST_INIT(&srv->sessions);
72 /* Open /dev/consolectl */
73 srv->cons = open("/dev/consolectl", O_RDWR);
75 syslog(LOG_ERR, "Could not open /dev/consolectl. %s (%d)",
76 strerror(errno), errno);
80 /* Create control socket */
81 srv->ctrl = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
83 syslog(LOG_ERR, "Could not create control L2CAP socket. " \
84 "%s (%d)", strerror(errno), errno);
89 l2addr.l2cap_len = sizeof(l2addr);
90 l2addr.l2cap_family = AF_BLUETOOTH;
91 memcpy(&l2addr.l2cap_bdaddr, &srv->bdaddr, sizeof(l2addr.l2cap_bdaddr));
92 l2addr.l2cap_psm = htole16(0x11);
94 if (bind(srv->ctrl, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
95 syslog(LOG_ERR, "Could not bind control L2CAP socket. " \
96 "%s (%d)", strerror(errno), errno);
102 if (listen(srv->ctrl, 10) < 0) {
103 syslog(LOG_ERR, "Could not listen on control L2CAP socket. " \
104 "%s (%d)", strerror(errno), errno);
110 /* Create intrrupt socket */
111 srv->intr = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
113 syslog(LOG_ERR, "Could not create interrupt L2CAP socket. " \
114 "%s (%d)", strerror(errno), errno);
120 l2addr.l2cap_psm = htole16(0x13);
122 if (bind(srv->intr, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
123 syslog(LOG_ERR, "Could not bind interrupt L2CAP socket. " \
124 "%s (%d)", strerror(errno), errno);
131 if (listen(srv->intr, 10) < 0) {
132 syslog(LOG_ERR, "Could not listen on interrupt L2CAP socket. "\
133 "%s (%d)", strerror(errno), errno);
140 FD_SET(srv->ctrl, &srv->rfdset);
141 FD_SET(srv->intr, &srv->rfdset);
142 srv->maxfd = max(srv->ctrl, srv->intr);
152 server_shutdown(bthid_server_p srv)
160 while (!LIST_EMPTY(&srv->sessions))
161 session_close(LIST_FIRST(&srv->sessions));
163 memset(srv, 0, sizeof(*srv));
167 * Do one server iteration
171 server_do(bthid_server_p srv)
174 fd_set rfdset, wfdset;
182 /* Copy cached version of the fd sets and call select */
183 memcpy(&rfdset, &srv->rfdset, sizeof(rfdset));
184 memcpy(&wfdset, &srv->wfdset, sizeof(wfdset));
186 n = select(srv->maxfd + 1, &rfdset, &wfdset, NULL, &tv);
191 syslog(LOG_ERR, "Could not select(%d, %p, %p). %s (%d)",
192 srv->maxfd + 1, &rfdset, &wfdset, strerror(errno), errno);
197 /* Process descriptors (if any) */
198 for (fd = 0; fd < srv->maxfd + 1 && n > 0; fd ++) {
199 if (FD_ISSET(fd, &rfdset)) {
202 if (fd == srv->ctrl || fd == srv->intr)
203 server_accept(srv, fd);
205 server_process(srv, fd);
206 } else if (FD_ISSET(fd, &wfdset)) {
209 client_connect(srv, fd);
217 * Accept new connection
221 server_accept(bthid_server_p srv, int32_t fd)
225 struct sockaddr_l2cap l2addr;
229 len = sizeof(l2addr);
230 if ((new_fd = accept(fd, (struct sockaddr *) &l2addr, &len)) < 0) {
231 syslog(LOG_ERR, "Could not accept %s connection. %s (%d)",
232 (fd == srv->ctrl)? "control" : "interrupt",
233 strerror(errno), errno);
237 /* Is device configured? */
238 if ((d = get_hid_device(&l2addr.l2cap_bdaddr)) == NULL) {
239 syslog(LOG_ERR, "Rejecting %s connection from %s. " \
240 "Device not configured",
241 (fd == srv->ctrl)? "control" : "interrupt",
242 bt_ntoa(&l2addr.l2cap_bdaddr, NULL));
247 /* Check if we have session for the device */
248 if ((s = session_by_bdaddr(srv, &l2addr.l2cap_bdaddr)) == NULL) {
249 d->new_device = 0; /* reset new device flag */
252 /* Create new inbound session */
253 if ((s = session_open(srv, d)) == NULL) {
254 syslog(LOG_CRIT, "Could not open inbound session "
255 "for %s", bt_ntoa(&l2addr.l2cap_bdaddr, NULL));
261 /* Update descriptors */
262 if (fd == srv->ctrl) {
263 assert(s->ctrl == -1);
265 s->state = (s->intr == -1)? W4INTR : OPEN;
267 assert(s->intr == -1);
269 s->state = (s->ctrl == -1)? W4CTRL : OPEN;
272 FD_SET(new_fd, &srv->rfdset);
273 if (new_fd > srv->maxfd)
276 syslog(LOG_NOTICE, "Accepted %s connection from %s",
277 (fd == srv->ctrl)? "control" : "interrupt",
278 bt_ntoa(&l2addr.l2cap_bdaddr, NULL));
280 /* Register session's vkbd descriptor (if needed) for read */
281 if (s->state == OPEN && d->keyboard) {
282 assert(s->vkbd != -1);
284 FD_SET(s->vkbd, &srv->rfdset);
285 if (s->vkbd > srv->maxfd)
286 srv->maxfd = s->vkbd;
293 * Process data on the connection
297 server_process(bthid_server_p srv, int32_t fd)
299 bthid_session_p s = session_by_fd(srv, fd);
300 int32_t len, to_read;
301 int32_t (*cb)(bthid_session_p, uint8_t *, int32_t);
308 return (0); /* can happen on device disconnect */
313 to_read = sizeof(data.b);
314 } else if (fd == s->intr) {
316 to_read = sizeof(data.b);
318 assert(fd == s->vkbd);
320 cb = kbd_status_changed;
321 to_read = sizeof(data.s);
325 len = read(fd, &data, to_read);
326 } while (len < 0 && errno == EINTR);
329 syslog(LOG_ERR, "Could not read data from %s (%s). %s (%d)",
330 bt_ntoa(&s->bdaddr, NULL),
331 (fd == s->ctrl)? "control" : "interrupt",
332 strerror(errno), errno);
338 syslog(LOG_NOTICE, "Remote device %s has closed %s connection",
339 bt_ntoa(&s->bdaddr, NULL),
340 (fd == s->ctrl)? "control" : "interrupt");
345 (*cb)(s, (uint8_t *) &data, len);