xref: /freebsd/usr.sbin/bluetooth/bthidd/client.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*
2  * client.c
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7  *
8  * Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  * $Id: client.c,v 1.7 2006/09/07 21:06:53 max Exp $
33  * $FreeBSD$
34  */
35 
36 #include <sys/queue.h>
37 #include <assert.h>
38 #define L2CAP_SOCKET_CHECKED
39 #include <bluetooth.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <syslog.h>
46 #include <unistd.h>
47 #include <usbhid.h>
48 #include "bthid_config.h"
49 #include "bthidd.h"
50 
51 static int32_t	client_socket(bdaddr_p bdaddr, uint16_t psm);
52 
53 /*
54  * Get next config entry and create outbound connection (if required)
55  *
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...
59  */
60 
61 static int32_t	connect_in_progress = 0;
62 
63 int32_t
64 client_rescan(bthid_server_p srv)
65 {
66 	static hid_device_p	d;
67 	bthid_session_p		s;
68 
69 	assert(srv != NULL);
70 
71 	if (connect_in_progress)
72 		return (0); /* another connect is still pending */
73 
74 	d = get_next_hid_device(d);
75 	if (d == NULL)
76 		return (0); /* XXX should not happen? empty config? */
77 
78 	if ((s = session_by_bdaddr(srv, &d->bdaddr)) != NULL)
79 		return (0); /* session already active */
80 
81 	if (!d->new_device) {
82 		if (d->reconnect_initiate)
83 			return (0); /* device will initiate reconnect */
84 	}
85 
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);
89 
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));
93 		return (-1);
94 	}
95 
96 	/* Open control channel */
97 	s->ctrl = client_socket(&s->bdaddr, d->control_psm);
98 	if (s->ctrl < 0) {
99 		syslog(LOG_ERR, "Could not open control channel to %s. %s (%d)",
100 			bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno);
101 		session_close(s);
102 		return (-1);
103 	}
104 
105 	s->state = W4CTRL;
106 
107 	FD_SET(s->ctrl, &srv->wfdset);
108 	if (s->ctrl > srv->maxfd)
109 		srv->maxfd = s->ctrl;
110 
111 	connect_in_progress = 1;
112 
113 	return (0);
114 }
115 
116 /*
117  * Process connect on the socket
118  */
119 
120 int32_t
121 client_connect(bthid_server_p srv, int32_t fd)
122 {
123 	bthid_session_p	s;
124 	hid_device_p	d;
125 	int32_t		error;
126 	socklen_t	len;
127 
128 	assert(srv != NULL);
129 	assert(fd >= 0);
130 
131 	s = session_by_fd(srv, fd);
132 	assert(s != NULL);
133 
134 	d = get_hid_device(&s->bdaddr);
135 	assert(d != NULL);
136 
137 	error = 0;
138 	len = sizeof(error);
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);
142 		session_close(s);
143 		connect_in_progress = 0;
144 
145 		return (-1);
146 	}
147 
148 	if (error != 0) {
149 		syslog(LOG_ERR, "Could not connect to %s. %s (%d)",
150 			bt_ntoa(&s->bdaddr, NULL), strerror(error), error);
151 		session_close(s);
152 		connect_in_progress = 0;
153 
154 		return (0);
155 	}
156 
157 	switch (s->state) {
158 	case W4CTRL: /* Control channel is open */
159 		assert(s->ctrl == fd);
160 		assert(s->intr == -1);
161 
162 		/* Open interrupt channel */
163 		s->intr = client_socket(&s->bdaddr, d->interrupt_psm);
164 		if (s->intr < 0) {
165 			syslog(LOG_ERR, "Could not open interrupt channel " \
166 				"to %s. %s (%d)", bt_ntoa(&s->bdaddr, NULL),
167 				strerror(errno), errno);
168 			session_close(s);
169 			connect_in_progress = 0;
170 
171 			return (-1);
172 		}
173 
174 		s->state = W4INTR;
175 
176 		FD_SET(s->intr, &srv->wfdset);
177 		if (s->intr > srv->maxfd)
178 			srv->maxfd = s->intr;
179 
180 		d->new_device = 0; /* reset new device flag */
181 		write_hids_file();
182 		break;
183 
184 	case W4INTR: /* Interrupt channel is open */
185 		assert(s->ctrl != -1);
186 		assert(s->intr == fd);
187 
188 		s->state = OPEN;
189 		connect_in_progress = 0;
190 
191 		/* Create kbd/mouse after both channels are established */
192 		if (session_run(s) < 0) {
193 			session_close(s);
194 			return (-1);
195 		}
196 		break;
197 
198 	default:
199 		assert(0);
200 		break;
201 	}
202 
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);
206 
207 	return (0);
208 }
209 
210 /*
211  * Create bound non-blocking socket and initiate connect
212  */
213 
214 static int
215 client_socket(bdaddr_p bdaddr, uint16_t psm)
216 {
217 	struct sockaddr_l2cap	l2addr;
218 	int32_t			s, m;
219 
220 	s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
221 	if (s < 0)
222 		return (-1);
223 
224 	m = fcntl(s, F_GETFL);
225 	if (m < 0) {
226 		close(s);
227 		return (-1);
228 	}
229 
230 	if (fcntl(s, F_SETFL, (m|O_NONBLOCK)) < 0) {
231 		close(s);
232 		return (-1);
233 	}
234 
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;
241 
242 	if (bind(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
243 		close(s);
244 		return (-1);
245 	}
246 
247 	memcpy(&l2addr.l2cap_bdaddr, bdaddr, sizeof(l2addr.l2cap_bdaddr));
248 	l2addr.l2cap_psm = htole16(psm);
249 
250 	if (connect(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0 &&
251 	    errno != EINPROGRESS) {
252 		close(s);
253 		return (-1);
254 	}
255 
256 	return (s);
257 }
258 
259