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: client.c,v 1.7 2006/09/07 21:06:53 max Exp $
36 #include <sys/queue.h>
38 #define L2CAP_SOCKET_CHECKED
39 #include <bluetooth.h>
48 #include "bthid_config.h"
51 static int32_t client_socket(bdaddr_p bdaddr, uint16_t psm);
54 * Get next config entry and create outbound connection (if required)
56 * XXX Do only one device at a time. At least one of my devices (3COM
57 * Bluetooth PCCARD) rejects Create_Connection command if another
58 * Create_Connection command is still pending. Weird...
61 static int32_t connect_in_progress = 0;
64 client_rescan(bthid_server_p srv)
66 static hid_device_p d;
71 if (connect_in_progress)
72 return (0); /* another connect is still pending */
74 d = get_next_hid_device(d);
76 return (0); /* XXX should not happen? empty config? */
78 if ((s = session_by_bdaddr(srv, &d->bdaddr)) != NULL)
79 return (0); /* session already active */
82 if (d->reconnect_initiate)
83 return (0); /* device will initiate reconnect */
86 syslog(LOG_NOTICE, "Opening outbound session for %s " \
87 "(new_device=%d, reconnect_initiate=%d)",
88 bt_ntoa(&d->bdaddr, NULL), d->new_device, d->reconnect_initiate);
90 if ((s = session_open(srv, d)) == NULL) {
91 syslog(LOG_CRIT, "Could not create outbound session for %s",
92 bt_ntoa(&d->bdaddr, NULL));
96 /* Open control channel */
97 s->ctrl = client_socket(&s->bdaddr, d->control_psm);
99 syslog(LOG_ERR, "Could not open control channel to %s. %s (%d)",
100 bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno);
107 FD_SET(s->ctrl, &srv->wfdset);
108 if (s->ctrl > srv->maxfd)
109 srv->maxfd = s->ctrl;
111 connect_in_progress = 1;
117 * Process connect on the socket
121 client_connect(bthid_server_p srv, int32_t fd)
131 s = session_by_fd(srv, fd);
134 d = get_hid_device(&s->bdaddr);
139 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
140 syslog(LOG_ERR, "Could not get socket error for %s. %s (%d)",
141 bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno);
143 connect_in_progress = 0;
149 syslog(LOG_ERR, "Could not connect to %s. %s (%d)",
150 bt_ntoa(&s->bdaddr, NULL), strerror(error), error);
152 connect_in_progress = 0;
158 case W4CTRL: /* Control channel is open */
159 assert(s->ctrl == fd);
160 assert(s->intr == -1);
162 /* Open interrupt channel */
163 s->intr = client_socket(&s->bdaddr, d->interrupt_psm);
165 syslog(LOG_ERR, "Could not open interrupt channel " \
166 "to %s. %s (%d)", bt_ntoa(&s->bdaddr, NULL),
167 strerror(errno), errno);
169 connect_in_progress = 0;
176 FD_SET(s->intr, &srv->wfdset);
177 if (s->intr > srv->maxfd)
178 srv->maxfd = s->intr;
180 d->new_device = 0; /* reset new device flag */
184 case W4INTR: /* Interrupt channel is open */
185 assert(s->ctrl != -1);
186 assert(s->intr == fd);
189 connect_in_progress = 0;
191 /* Create kbd/mouse after both channels are established */
192 if (session_run(s) < 0) {
203 /* Move fd to from the write fd set into read fd set */
204 FD_CLR(fd, &srv->wfdset);
205 FD_SET(fd, &srv->rfdset);
211 * Create bound non-blocking socket and initiate connect
215 client_socket(bdaddr_p bdaddr, uint16_t psm)
217 struct sockaddr_l2cap l2addr;
220 s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
224 m = fcntl(s, F_GETFL);
230 if (fcntl(s, F_SETFL, (m|O_NONBLOCK)) < 0) {
235 l2addr.l2cap_len = sizeof(l2addr);
236 l2addr.l2cap_family = AF_BLUETOOTH;
237 memset(&l2addr.l2cap_bdaddr, 0, sizeof(l2addr.l2cap_bdaddr));
238 l2addr.l2cap_psm = 0;
239 l2addr.l2cap_bdaddr_type = BDADDR_BREDR;
240 l2addr.l2cap_cid = 0;
242 if (bind(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
247 memcpy(&l2addr.l2cap_bdaddr, bdaddr, sizeof(l2addr.l2cap_bdaddr));
248 l2addr.l2cap_psm = htole16(psm);
250 if (connect(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0 &&
251 errno != EINPROGRESS) {