4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * $Id: client.c,v 1.6 2004/02/26 21:57:55 max Exp $
32 #include <sys/queue.h>
34 #include <bluetooth.h>
44 #include "bthid_config.h"
46 static int client_socket(bdaddr_p bdaddr, int psm);
49 * Get next config entry and create outbound connection (if required)
51 * XXX Do only one device at a time. At least one of my devices (3COM
52 * Bluetooth PCCARD) rejects Create_Connection command if another
53 * Create_Connection command is still pending. Weird...
56 static int connect_in_progress = 0;
59 client_rescan(bthid_server_p srv)
61 static hid_device_p d = NULL;
62 bthid_session_p s = NULL;
66 if (connect_in_progress)
67 return (0); /* another connect is still pending */
69 d = get_next_hid_device(d);
71 return (0); /* XXX should not happen? empty config? */
73 if ((s = session_by_bdaddr(srv, &d->bdaddr)) != NULL)
74 return (0); /* session already active */
77 if (d->reconnect_initiate)
78 return (0); /* device will initiate reconnect */
81 syslog(LOG_NOTICE, "Opening outbound session for %s " \
82 "(new_device=%d, reconnect_initiate=%d)",
83 bt_ntoa(&d->bdaddr, NULL), d->new_device, d->reconnect_initiate);
85 if ((s = session_open(srv, &d->bdaddr)) == NULL) {
86 syslog(LOG_CRIT, "Could not open outbound session for %s. " \
87 "Not enough memory", bt_ntoa(&d->bdaddr, NULL));
91 /* Open control channel */
92 s->ctrl = client_socket(&s->bdaddr, d->control_psm);
94 syslog(LOG_ERR, "Could not open control channel to %s. %s (%d)",
95 bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno);
102 FD_SET(s->ctrl, &srv->wfdset);
103 if (s->ctrl > srv->maxfd)
104 srv->maxfd = s->ctrl;
106 connect_in_progress = 1;
112 * Process connect on the socket
116 client_connect(bthid_server_p srv, int fd)
118 bthid_session_p s = NULL;
119 hid_device_p d = NULL;
125 s = session_by_fd(srv, fd);
128 d = get_hid_device(&s->bdaddr);
133 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
134 syslog(LOG_ERR, "Could not get socket error for %s. %s (%d)",
135 bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno);
137 connect_in_progress = 0;
143 syslog(LOG_ERR, "Could not connect to %s. %s (%d)",
144 bt_ntoa(&s->bdaddr, NULL), strerror(error), error);
146 connect_in_progress = 0;
152 case W4CTRL: /* Control channel is open */
153 assert(s->ctrl == fd);
154 assert(s->intr == -1);
156 /* Open interrupt channel */
157 s->intr = client_socket(&s->bdaddr, d->interrupt_psm);
159 syslog(LOG_ERR, "Could not open interrupt channel " \
160 "to %s. %s (%d)", bt_ntoa(&s->bdaddr, NULL),
161 strerror(errno), errno);
163 connect_in_progress = 0;
170 FD_SET(s->intr, &srv->wfdset);
171 if (s->intr > srv->maxfd)
172 srv->maxfd = s->intr;
174 d->new_device = 0; /* reset new device flag */
178 case W4INTR: /* Interrupt channel is open */
179 assert(s->ctrl != -1);
180 assert(s->intr == fd);
183 connect_in_progress = 0;
191 /* Move fd to from the write fd set into read fd set */
192 FD_CLR(fd, &srv->wfdset);
193 FD_SET(fd, &srv->rfdset);
199 * Create bound non-blocking socket and initiate connect
203 client_socket(bdaddr_p bdaddr, int psm)
205 struct sockaddr_l2cap l2addr;
208 s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
212 m = fcntl(s, F_GETFL);
218 if (fcntl(s, F_SETFL, (m|O_NONBLOCK)) < 0) {
223 l2addr.l2cap_len = sizeof(l2addr);
224 l2addr.l2cap_family = AF_BLUETOOTH;
225 memcpy(&l2addr.l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(l2addr.l2cap_bdaddr));
226 l2addr.l2cap_psm = 0;
228 if (bind(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
233 memcpy(&l2addr.l2cap_bdaddr, bdaddr, sizeof(l2addr.l2cap_bdaddr));
234 l2addr.l2cap_psm = htole16(psm);
236 if (connect(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0 &&
237 errno != EINPROGRESS) {