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: client.c,v 1.7 2006/09/07 21:06:53 max Exp $
34 #include <sys/queue.h>
36 #define L2CAP_SOCKET_CHECKED
37 #include <bluetooth.h>
46 #include "bthid_config.h"
49 static int32_t client_socket(bdaddr_p bdaddr, uint16_t psm);
52 * Get next config entry and create outbound connection (if required)
54 * XXX Do only one device at a time. At least one of my devices (3COM
55 * Bluetooth PCCARD) rejects Create_Connection command if another
56 * Create_Connection command is still pending. Weird...
59 static int32_t connect_in_progress = 0;
62 client_rescan(bthid_server_p srv)
64 static hid_device_p d;
69 if (connect_in_progress)
70 return (0); /* another connect is still pending */
72 d = get_next_hid_device(d);
74 return (0); /* XXX should not happen? empty config? */
76 if ((s = session_by_bdaddr(srv, &d->bdaddr)) != NULL)
77 return (0); /* session already active */
80 if (d->reconnect_initiate)
81 return (0); /* device will initiate reconnect */
84 syslog(LOG_NOTICE, "Opening outbound session for %s " \
85 "(new_device=%d, reconnect_initiate=%d)",
86 bt_ntoa(&d->bdaddr, NULL), d->new_device, d->reconnect_initiate);
88 if ((s = session_open(srv, d)) == NULL) {
89 syslog(LOG_CRIT, "Could not create outbound session for %s",
90 bt_ntoa(&d->bdaddr, NULL));
94 /* Open control channel */
95 s->ctrl = client_socket(&s->bdaddr, d->control_psm);
97 syslog(LOG_ERR, "Could not open control channel to %s. %s (%d)",
98 bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno);
105 FD_SET(s->ctrl, &srv->wfdset);
106 if (s->ctrl > srv->maxfd)
107 srv->maxfd = s->ctrl;
109 connect_in_progress = 1;
115 * Process connect on the socket
119 client_connect(bthid_server_p srv, int32_t fd)
129 s = session_by_fd(srv, fd);
132 d = get_hid_device(&s->bdaddr);
137 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
138 syslog(LOG_ERR, "Could not get socket error for %s. %s (%d)",
139 bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno);
141 connect_in_progress = 0;
147 syslog(LOG_ERR, "Could not connect to %s. %s (%d)",
148 bt_ntoa(&s->bdaddr, NULL), strerror(error), error);
150 connect_in_progress = 0;
156 case W4CTRL: /* Control channel is open */
157 assert(s->ctrl == fd);
158 assert(s->intr == -1);
160 /* Open interrupt channel */
161 s->intr = client_socket(&s->bdaddr, d->interrupt_psm);
163 syslog(LOG_ERR, "Could not open interrupt channel " \
164 "to %s. %s (%d)", bt_ntoa(&s->bdaddr, NULL),
165 strerror(errno), errno);
167 connect_in_progress = 0;
174 FD_SET(s->intr, &srv->wfdset);
175 if (s->intr > srv->maxfd)
176 srv->maxfd = s->intr;
178 d->new_device = 0; /* reset new device flag */
182 case W4INTR: /* Interrupt channel is open */
183 assert(s->ctrl != -1);
184 assert(s->intr == fd);
187 connect_in_progress = 0;
189 /* Register session's vkbd descriptor (if any) for read */
190 if (s->state == OPEN && d->keyboard) {
191 assert(s->vkbd != -1);
193 FD_SET(s->vkbd, &srv->rfdset);
194 if (s->vkbd > srv->maxfd)
195 srv->maxfd = s->vkbd;
204 /* Move fd to from the write fd set into read fd set */
205 FD_CLR(fd, &srv->wfdset);
206 FD_SET(fd, &srv->rfdset);
212 * Create bound non-blocking socket and initiate connect
216 client_socket(bdaddr_p bdaddr, uint16_t psm)
218 struct sockaddr_l2cap l2addr;
221 s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
225 m = fcntl(s, F_GETFL);
231 if (fcntl(s, F_SETFL, (m|O_NONBLOCK)) < 0) {
236 l2addr.l2cap_len = sizeof(l2addr);
237 l2addr.l2cap_family = AF_BLUETOOTH;
238 memset(&l2addr.l2cap_bdaddr, 0, sizeof(l2addr.l2cap_bdaddr));
239 l2addr.l2cap_psm = 0;
240 l2addr.l2cap_bdaddr_type = BDADDR_BREDR;
241 l2addr.l2cap_cid = 0;
243 if (bind(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
248 memcpy(&l2addr.l2cap_bdaddr, bdaddr, sizeof(l2addr.l2cap_bdaddr));
249 l2addr.l2cap_psm = htole16(psm);
251 if (connect(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0 &&
252 errno != EINPROGRESS) {