1/*
2 * client.c
3 *
4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
--- 7 unchanged lines hidden (view full) ---
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
26 * SUCH DAMAGE.
27 *
28 * $Id: client.c,v 1.6 2004/02/26 21:57:55 max Exp $
29 * $FreeBSD: head/usr.sbin/bluetooth/bthidd/client.c 156784 2006-03-16 18:26:54Z emax $
30 */
31
32#include <sys/queue.h>
33#include <assert.h>
34#include <bluetooth.h>
35#include <errno.h>
36#include <fcntl.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <syslog.h>
41#include <unistd.h>
42#include <usbhid.h>
43#include "bthidd.h"
44#include "bthid_config.h"
45
46static int client_socket(bdaddr_p bdaddr, int psm);
47
48/*
49 * Get next config entry and create outbound connection (if required)
50 *
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...
54 */
55
56static int connect_in_progress = 0;
57
58int
59client_rescan(bthid_server_p srv)
60{
61 static hid_device_p d = NULL;
62 bthid_session_p s = NULL;
63
64 assert(srv != NULL);
65
66 if (connect_in_progress)
67 return (0); /* another connect is still pending */
68
69 d = get_next_hid_device(d);
70 if (d == NULL)
--- 6 unchanged lines hidden (view full) ---
77 if (d->reconnect_initiate)
78 return (0); /* device will initiate reconnect */
79 }
80
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);
84
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));
88 return (-1);
89 }
90
91 /* Open control channel */
92 s->ctrl = client_socket(&s->bdaddr, d->control_psm);
93 if (s->ctrl < 0) {
94 syslog(LOG_ERR, "Could not open control channel to %s. %s (%d)",
95 bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno);
--- 11 unchanged lines hidden (view full) ---
107
108 return (0);
109}
110
111/*
112 * Process connect on the socket
113 */
114
115int
116client_connect(bthid_server_p srv, int fd)
117{
118 bthid_session_p s = NULL;
119 hid_device_p d = NULL;
120 int error, len;
121
122 assert(srv != NULL);
123 assert(fd >= 0);
124
125 s = session_by_fd(srv, fd);
126 assert(s != NULL);
127
128 d = get_hid_device(&s->bdaddr);
--- 47 unchanged lines hidden (view full) ---
176 break;
177
178 case W4INTR: /* Interrupt channel is open */
179 assert(s->ctrl != -1);
180 assert(s->intr == fd);
181
182 s->state = OPEN;
183 connect_in_progress = 0;
184 break;
185
186 default:
187 assert(0);
188 break;
189 }
190
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);
194
195 return (0);
196}
197
198/*
199 * Create bound non-blocking socket and initiate connect
200 */
201
202static int
203client_socket(bdaddr_p bdaddr, int psm)
204{
205 struct sockaddr_l2cap l2addr;
206 int s, m;
207
208 s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
209 if (s < 0)
210 return (-1);
211
212 m = fcntl(s, F_GETFL);
213 if (m < 0) {
214 close(s);
215 return (-1);
216 }
217
218 if (fcntl(s, F_SETFL, (m|O_NONBLOCK)) < 0) {
219 close(s);
220 return (-1);
221 }
222
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;
227
228 if (bind(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
229 close(s);
230 return (-1);
231 }
232
233 memcpy(&l2addr.l2cap_bdaddr, bdaddr, sizeof(l2addr.l2cap_bdaddr));
234 l2addr.l2cap_psm = htole16(psm);
235
236 if (connect(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0 &&
237 errno != EINPROGRESS) {
238 close(s);
239 return (-1);
240 }
241
242 return (s);
243}
244
2 * client.c
3 *
4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
--- 7 unchanged lines hidden (view full) ---
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
26 * SUCH DAMAGE.
27 *
28 * $Id: client.c,v 1.6 2004/02/26 21:57:55 max Exp $
29 * $FreeBSD: head/usr.sbin/bluetooth/bthidd/client.c 156784 2006-03-16 18:26:54Z emax $
30 */
31
32#include <sys/queue.h>
33#include <assert.h>
34#include <bluetooth.h>
35#include <errno.h>
36#include <fcntl.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <syslog.h>
41#include <unistd.h>
42#include <usbhid.h>
43#include "bthidd.h"
44#include "bthid_config.h"
45
46static int client_socket(bdaddr_p bdaddr, int psm);
47
48/*
49 * Get next config entry and create outbound connection (if required)
50 *
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...
54 */
55
56static int connect_in_progress = 0;
57
58int
59client_rescan(bthid_server_p srv)
60{
61 static hid_device_p d = NULL;
62 bthid_session_p s = NULL;
63
64 assert(srv != NULL);
65
66 if (connect_in_progress)
67 return (0); /* another connect is still pending */
68
69 d = get_next_hid_device(d);
70 if (d == NULL)
--- 6 unchanged lines hidden (view full) ---
77 if (d->reconnect_initiate)
78 return (0); /* device will initiate reconnect */
79 }
80
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);
84
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));
88 return (-1);
89 }
90
91 /* Open control channel */
92 s->ctrl = client_socket(&s->bdaddr, d->control_psm);
93 if (s->ctrl < 0) {
94 syslog(LOG_ERR, "Could not open control channel to %s. %s (%d)",
95 bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno);
--- 11 unchanged lines hidden (view full) ---
107
108 return (0);
109}
110
111/*
112 * Process connect on the socket
113 */
114
115int
116client_connect(bthid_server_p srv, int fd)
117{
118 bthid_session_p s = NULL;
119 hid_device_p d = NULL;
120 int error, len;
121
122 assert(srv != NULL);
123 assert(fd >= 0);
124
125 s = session_by_fd(srv, fd);
126 assert(s != NULL);
127
128 d = get_hid_device(&s->bdaddr);
--- 47 unchanged lines hidden (view full) ---
176 break;
177
178 case W4INTR: /* Interrupt channel is open */
179 assert(s->ctrl != -1);
180 assert(s->intr == fd);
181
182 s->state = OPEN;
183 connect_in_progress = 0;
184 break;
185
186 default:
187 assert(0);
188 break;
189 }
190
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);
194
195 return (0);
196}
197
198/*
199 * Create bound non-blocking socket and initiate connect
200 */
201
202static int
203client_socket(bdaddr_p bdaddr, int psm)
204{
205 struct sockaddr_l2cap l2addr;
206 int s, m;
207
208 s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
209 if (s < 0)
210 return (-1);
211
212 m = fcntl(s, F_GETFL);
213 if (m < 0) {
214 close(s);
215 return (-1);
216 }
217
218 if (fcntl(s, F_SETFL, (m|O_NONBLOCK)) < 0) {
219 close(s);
220 return (-1);
221 }
222
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;
227
228 if (bind(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
229 close(s);
230 return (-1);
231 }
232
233 memcpy(&l2addr.l2cap_bdaddr, bdaddr, sizeof(l2addr.l2cap_bdaddr));
234 l2addr.l2cap_psm = htole16(psm);
235
236 if (connect(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0 &&
237 errno != EINPROGRESS) {
238 close(s);
239 return (-1);
240 }
241
242 return (s);
243}
244