blob: 35031482746b9e4e2f1e3d33ec23b1b43db42349 [file] [log] [blame]
Marcel Holtmann43177232013-12-30 20:48:31 -08001/*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2011-2012 Intel Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28
29#include <stdio.h>
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -080030#include <errno.h>
Marcel Holtmann43177232013-12-30 20:48:31 -080031#include <ctype.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <stdlib.h>
35#include <string.h>
36#include <getopt.h>
37#include <stdbool.h>
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -080038#include <termios.h>
Marcel Holtmann43177232013-12-30 20:48:31 -080039#include <sys/stat.h>
40#include <sys/socket.h>
41#include <sys/un.h>
42
Marcel Holtmann15097d32013-12-30 21:26:31 -080043#include <netdb.h>
44#include <arpa/inet.h>
45
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -080046#include "src/shared/util.h"
Marcel Holtmann43177232013-12-30 20:48:31 -080047#include "monitor/mainloop.h"
48#include "monitor/bt.h"
49
Marcel Holtmann43177232013-12-30 20:48:31 -080050#define BTPROTO_HCI 1
51struct sockaddr_hci {
52 sa_family_t hci_family;
53 unsigned short hci_dev;
54 unsigned short hci_channel;
55};
56#define HCI_CHANNEL_USER 1
57
58static uint16_t hci_index = 0;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -080059static bool client_active = false;
Marcel Holtmann81294e52014-01-16 00:27:30 -080060static bool debug_enabled = false;
Marcel Holtmann43177232013-12-30 20:48:31 -080061
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -080062static void hexdump_print(const char *str, void *user_data)
63{
Marcel Holtmann81294e52014-01-16 00:27:30 -080064 printf("%s%s\n", (char *) user_data, str);
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -080065}
66
Marcel Holtmann81294e52014-01-16 00:27:30 -080067struct proxy {
68 /* Receive commands, ACL and SCO data */
69 int host_fd;
70 uint8_t host_buf[4096];
71 uint16_t host_len;
72 bool host_shutdown;
73
74 /* Receive events, ACL and SCO data */
75 int dev_fd;
76 uint8_t dev_buf[4096];
77 uint16_t dev_len;
78 bool dev_shutdown;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -080079};
80
Marcel Holtmann81294e52014-01-16 00:27:30 -080081static bool write_packet(int fd, const void *data, size_t size,
82 void *user_data)
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -080083{
Marcel Holtmann81294e52014-01-16 00:27:30 -080084 while (size > 0) {
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -080085 ssize_t written;
86
Marcel Holtmann81294e52014-01-16 00:27:30 -080087 written = write(fd, data, size);
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -080088 if (written < 0) {
89 if (errno == EAGAIN || errno == EINTR)
90 continue;
Marcel Holtmann81294e52014-01-16 00:27:30 -080091 return false;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -080092 }
93
Marcel Holtmann81294e52014-01-16 00:27:30 -080094 if (debug_enabled)
95 util_hexdump('<', data, written, hexdump_print,
96 user_data);
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -080097
Marcel Holtmann81294e52014-01-16 00:27:30 -080098 data += written;
99 size -= written;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800100 }
101
102 return true;
103}
104
Marcel Holtmann81294e52014-01-16 00:27:30 -0800105static void host_read_destroy(void *user_data)
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800106{
Marcel Holtmann81294e52014-01-16 00:27:30 -0800107 struct proxy *proxy = user_data;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800108
Marcel Holtmann81294e52014-01-16 00:27:30 -0800109 printf("Closing host descriptor\n");
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800110
Marcel Holtmann81294e52014-01-16 00:27:30 -0800111 if (proxy->host_shutdown)
112 shutdown(proxy->host_fd, SHUT_RDWR);
113
114 close(proxy->host_fd);
115 proxy->host_fd = -1;
116
117 if (proxy->dev_fd < 0) {
118 client_active = false;
119 free(proxy);
120 } else
121 mainloop_remove_fd(proxy->dev_fd);
122}
123
124static void host_read_callback(int fd, uint32_t events, void *user_data)
125{
126 struct proxy *proxy = user_data;
127 struct bt_hci_cmd_hdr *cmd_hdr;
128 struct bt_hci_acl_hdr *acl_hdr;
129 struct bt_hci_sco_hdr *sco_hdr;
130 ssize_t len;
131 uint16_t pktlen;
132
133 if (events & (EPOLLERR | EPOLLHUP)) {
134 fprintf(stderr, "Error from host descriptor\n");
135 mainloop_remove_fd(proxy->host_fd);
136 return;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800137 }
138
Marcel Holtmann81294e52014-01-16 00:27:30 -0800139 if (events & EPOLLRDHUP) {
140 fprintf(stderr, "Remote hangup of host descriptor\n");
141 mainloop_remove_fd(proxy->host_fd);
142 return;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800143 }
144
Marcel Holtmann81294e52014-01-16 00:27:30 -0800145 len = read(proxy->host_fd, proxy->host_buf + proxy->host_len,
146 sizeof(proxy->host_buf) - proxy->host_len);
147 if (len < 0) {
148 if (errno == EAGAIN || errno == EINTR)
149 return;
150
151 fprintf(stderr, "Read from host descriptor failed\n");
152 mainloop_remove_fd(proxy->host_fd);
153 return;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800154 }
155
Marcel Holtmann81294e52014-01-16 00:27:30 -0800156 if (debug_enabled)
157 util_hexdump('>', proxy->host_buf + proxy->host_len, len,
158 hexdump_print, "H: ");
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800159
Marcel Holtmann81294e52014-01-16 00:27:30 -0800160 proxy->host_len += len;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800161
Marcel Holtmann81294e52014-01-16 00:27:30 -0800162process_packet:
163 if (proxy->host_len < 1)
164 return;
165
166 switch (proxy->host_buf[0]) {
167 case BT_H4_CMD_PKT:
168 if (proxy->host_len < 1 + sizeof(*cmd_hdr))
169 return;
170
171 cmd_hdr = (void *) (proxy->host_buf + 1);
172 pktlen = 1 + sizeof(*cmd_hdr) + cmd_hdr->plen;
173 break;
174 case BT_H4_ACL_PKT:
175 if (proxy->host_len < 1 + sizeof(*acl_hdr))
176 return;
177
178 acl_hdr = (void *) (proxy->host_buf + 1);
179 pktlen = 1 + sizeof(*acl_hdr) + cpu_to_le16(acl_hdr->dlen);
180 break;
181 case BT_H4_SCO_PKT:
182 if (proxy->host_len < 1 + sizeof(*sco_hdr))
183 return;
184
185 sco_hdr = (void *) (proxy->host_buf + 1);
186 pktlen = 1 + sizeof(*sco_hdr) + sco_hdr->dlen;
187 break;
188 case 0xff:
189 /* Notification packet from /dev/vhci - ignore */
190 proxy->host_len = 0;
191 return;
192 default:
193 fprintf(stderr, "Received unknown host packet type 0x%02x\n",
194 proxy->host_buf[0]);
195 mainloop_remove_fd(proxy->host_fd);
196 return;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800197 }
198
Marcel Holtmann81294e52014-01-16 00:27:30 -0800199 if (proxy->host_len < pktlen)
200 return;
201
202 if (!write_packet(proxy->dev_fd, proxy->host_buf, pktlen, "D: ")) {
203 fprintf(stderr, "Write to device descriptor failed\n");
204 mainloop_remove_fd(proxy->dev_fd);
205 return;
206 }
207
208 if (proxy->host_len > pktlen) {
209 memmove(proxy->host_buf, proxy->host_buf + pktlen,
210 proxy->host_len - pktlen);
211 proxy->host_len -= pktlen;
212 goto process_packet;
213 }
214
215 proxy->host_len = 0;
216}
217
218static void dev_read_destroy(void *user_data)
219{
220 struct proxy *proxy = user_data;
221
222 printf("Closing device descriptor\n");
223
224 if (proxy->dev_shutdown)
225 shutdown(proxy->dev_fd, SHUT_RDWR);
226
227 close(proxy->dev_fd);
228 proxy->dev_fd = -1;
229
230 if (proxy->host_fd < 0) {
231 client_active = false;
232 free(proxy);
233 } else
234 mainloop_remove_fd(proxy->host_fd);
235}
236
237static void dev_read_callback(int fd, uint32_t events, void *user_data)
238{
239 struct proxy *proxy = user_data;
240 struct bt_hci_evt_hdr *evt_hdr;
241 struct bt_hci_acl_hdr *acl_hdr;
242 struct bt_hci_sco_hdr *sco_hdr;
243 ssize_t len;
244 uint16_t pktlen;
245
246 if (events & (EPOLLERR | EPOLLHUP)) {
247 fprintf(stderr, "Error from device descriptor\n");
248 mainloop_remove_fd(proxy->dev_fd);
249 return;
250 }
251
252 if (events & EPOLLRDHUP) {
253 fprintf(stderr, "Remote hangup of device descriptor\n");
254 mainloop_remove_fd(proxy->host_fd);
255 return;
256 }
257
258 len = read(proxy->dev_fd, proxy->dev_buf + proxy->dev_len,
259 sizeof(proxy->dev_buf) - proxy->dev_len);
260 if (len < 0) {
261 if (errno == EAGAIN || errno == EINTR)
262 return;
263
264 fprintf(stderr, "Read from device descriptor failed\n");
265 mainloop_remove_fd(proxy->dev_fd);
266 return;
267 }
268
269 if (debug_enabled)
270 util_hexdump('>', proxy->dev_buf + proxy->dev_len, len,
271 hexdump_print, "D: ");
272
273 proxy->dev_len += len;
274
275process_packet:
276 if (proxy->dev_len < 1)
277 return;
278
279 switch (proxy->dev_buf[0]) {
280 case BT_H4_EVT_PKT:
281 if (proxy->dev_len < 1 + sizeof(*evt_hdr))
282 return;
283
284 evt_hdr = (void *) (proxy->dev_buf + 1);
285 pktlen = 1 + sizeof(*evt_hdr) + evt_hdr->plen;
286 break;
287 case BT_H4_ACL_PKT:
288 if (proxy->dev_len < 1 + sizeof(*acl_hdr))
289 return;
290
291 acl_hdr = (void *) (proxy->dev_buf + 1);
292 pktlen = 1 + sizeof(*acl_hdr) + cpu_to_le16(acl_hdr->dlen);
293 break;
294 case BT_H4_SCO_PKT:
295 if (proxy->dev_len < 1 + sizeof(*sco_hdr))
296 return;
297
298 sco_hdr = (void *) (proxy->dev_buf + 1);
299 pktlen = 1 + sizeof(*sco_hdr) + sco_hdr->dlen;
300 break;
301 default:
302 fprintf(stderr, "Received unknown device packet type 0x%02x\n",
303 proxy->dev_buf[0]);
304 mainloop_remove_fd(proxy->dev_fd);
305 return;
306 }
307
308 if (proxy->dev_len < pktlen)
309 return;
310
311 if (!write_packet(proxy->host_fd, proxy->dev_buf, pktlen, "H: ")) {
312 fprintf(stderr, "Write to host descriptor failed\n");
313 mainloop_remove_fd(proxy->host_fd);
314 return;
315 }
316
317 if (proxy->dev_len > pktlen) {
318 memmove(proxy->dev_buf, proxy->dev_buf + pktlen,
319 proxy->dev_len - pktlen);
320 proxy->dev_len -= pktlen;
321 goto process_packet;
322 }
323
324 proxy->dev_len = 0;
325}
326
327static bool setup_proxy(int host_fd, bool host_shutdown,
328 int dev_fd, bool dev_shutdown)
329{
330 struct proxy *proxy;
331
332 proxy = new0(struct proxy, 1);
Marcel Holtmann8b8a5312014-01-22 07:53:35 -0800333 if (!proxy)
Marcel Holtmann81294e52014-01-16 00:27:30 -0800334 return NULL;
335
336 proxy->host_fd = host_fd;
337 proxy->host_shutdown = host_shutdown;
338
339 proxy->dev_fd = dev_fd;
340 proxy->dev_shutdown = dev_shutdown;
341
342 mainloop_add_fd(proxy->host_fd, EPOLLIN | EPOLLRDHUP,
343 host_read_callback, proxy, host_read_destroy);
344
345 mainloop_add_fd(proxy->dev_fd, EPOLLIN | EPOLLRDHUP,
346 dev_read_callback, proxy, dev_read_destroy);
347
348 return true;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800349}
Marcel Holtmann43177232013-12-30 20:48:31 -0800350
351static int open_channel(uint16_t index)
352{
353 struct sockaddr_hci addr;
354 int fd;
355
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800356 printf("Opening user channel for hci%u\n", hci_index);
357
Marcel Holtmann43177232013-12-30 20:48:31 -0800358 fd = socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
359 if (fd < 0) {
360 perror("Failed to open Bluetooth socket");
361 return -1;
362 }
363
364 memset(&addr, 0, sizeof(addr));
365 addr.hci_family = AF_BLUETOOTH;
366 addr.hci_dev = index;
367 addr.hci_channel = HCI_CHANNEL_USER;
368
369 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
370 close(fd);
371 perror("Failed to bind Bluetooth socket");
372 return -1;
373 }
374
375 return fd;
376}
377
Marcel Holtmann43177232013-12-30 20:48:31 -0800378static void server_callback(int fd, uint32_t events, void *user_data)
379{
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800380 union {
Marcel Holtmann81294e52014-01-16 00:27:30 -0800381 struct sockaddr common;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800382 struct sockaddr_un sun;
383 struct sockaddr_in sin;
384 } addr;
Marcel Holtmann43177232013-12-30 20:48:31 -0800385 socklen_t len;
Marcel Holtmann81294e52014-01-16 00:27:30 -0800386 int host_fd, dev_fd;
Marcel Holtmann43177232013-12-30 20:48:31 -0800387
388 if (events & (EPOLLERR | EPOLLHUP)) {
389 mainloop_quit();
390 return;
391 }
392
393 memset(&addr, 0, sizeof(addr));
394 len = sizeof(addr);
395
Marcel Holtmann81294e52014-01-16 00:27:30 -0800396 if (getsockname(fd, &addr.common, &len) < 0) {
Marcel Holtmann43177232013-12-30 20:48:31 -0800397 perror("Failed to get socket name");
398 return;
399 }
400
Marcel Holtmann81294e52014-01-16 00:27:30 -0800401 host_fd = accept(fd, &addr.common, &len);
402 if (host_fd < 0) {
Marcel Holtmann43177232013-12-30 20:48:31 -0800403 perror("Failed to accept client socket");
404 return;
405 }
406
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800407 if (client_active) {
Marcel Holtmann43177232013-12-30 20:48:31 -0800408 fprintf(stderr, "Active client already present\n");
Marcel Holtmann81294e52014-01-16 00:27:30 -0800409 close(host_fd);
Marcel Holtmann43177232013-12-30 20:48:31 -0800410 return;
411 }
412
Marcel Holtmann81294e52014-01-16 00:27:30 -0800413 dev_fd = open_channel(hci_index);
414 if (dev_fd < 0) {
415 close(host_fd);
Marcel Holtmann43177232013-12-30 20:48:31 -0800416 return;
417 }
418
419 printf("New client connected\n");
420
Marcel Holtmann8b8a5312014-01-22 07:53:35 -0800421 if (!setup_proxy(host_fd, true, dev_fd, false)) {
422 close(dev_fd);
423 close(host_fd);
Marcel Holtmann43177232013-12-30 20:48:31 -0800424 return;
Marcel Holtmann8b8a5312014-01-22 07:53:35 -0800425 }
Marcel Holtmann43177232013-12-30 20:48:31 -0800426
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800427 client_active = true;
Marcel Holtmann43177232013-12-30 20:48:31 -0800428}
429
430static int open_unix(const char *path)
431{
432 struct sockaddr_un addr;
433 int fd;
434
435 unlink(path);
436
437 fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
438 if (fd < 0) {
Marcel Holtmann15097d32013-12-30 21:26:31 -0800439 perror("Failed to open Unix server socket");
Marcel Holtmann43177232013-12-30 20:48:31 -0800440 return -1;
441 }
442
443 memset(&addr, 0, sizeof(addr));
444 addr.sun_family = AF_UNIX;
445 strcpy(addr.sun_path, path);
446
447 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Marcel Holtmann15097d32013-12-30 21:26:31 -0800448 perror("Failed to bind Unix server socket");
Marcel Holtmann43177232013-12-30 20:48:31 -0800449 close(fd);
450 return -1;
451 }
452
453 if (listen(fd, 1) < 0) {
Marcel Holtmann15097d32013-12-30 21:26:31 -0800454 perror("Failed to listen Unix server socket");
Marcel Holtmann43177232013-12-30 20:48:31 -0800455 close(fd);
456 return -1;
457 }
458
459 if (chmod(path, 0666) < 0)
460 perror("Failed to change mode");
461
462 return fd;
463}
464
Marcel Holtmann98921e12013-12-30 23:34:18 -0800465static int open_tcp(const char *address, unsigned int port)
Marcel Holtmann15097d32013-12-30 21:26:31 -0800466{
467 struct sockaddr_in addr;
468 int fd, opt = 1;
469
470 fd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
471 if (fd < 0) {
472 perror("Failed to open TCP server socket");
473 return -1;
474 }
475
476 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
477
478 memset(&addr, 0, sizeof(addr));
479 addr.sin_family = AF_INET;
Marcel Holtmann98921e12013-12-30 23:34:18 -0800480 addr.sin_addr.s_addr = inet_addr(address);
Marcel Holtmann15097d32013-12-30 21:26:31 -0800481 addr.sin_port = htons(port);
482
483 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
484 perror("Failed to bind TCP server socket");
485 close(fd);
486 return -1;
487 }
488
489 if (listen(fd, 1) < 0) {
490 perror("Failed to listen TCP server socket");
491 close(fd);
492 return -1;
493 }
494
495 return fd;
496}
497
Marcel Holtmann98921e12013-12-30 23:34:18 -0800498static int connect_tcp(const char *address, unsigned int port)
499{
500 struct sockaddr_in addr;
501 int fd;
502
503 fd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
504 if (fd < 0) {
505 perror("Failed to open TCP client socket");
506 return -1;
507 }
508
509 memset(&addr, 0, sizeof(addr));
510 addr.sin_family = AF_INET;
511 addr.sin_addr.s_addr = inet_addr(address);
512 addr.sin_port = htons(port);
513
514 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
515 perror("Failed to connect TCP client socket");
516 close(fd);
517 return -1;
518 }
519
520 return fd;
521}
522
523static int open_vhci(uint8_t type)
524{
525 uint8_t create_req[2] = { 0xff, type };
526 ssize_t written;
527 int fd;
528
529 fd = open("/dev/vhci", O_RDWR | O_CLOEXEC);
530 if (fd < 0) {
531 perror("Failed to open /dev/vhci device");
532 return -1;
533 }
534
535 written = write(fd, create_req, sizeof(create_req));
536 if (written < 0) {
537 perror("Failed to set device type");
538 close(fd);
539 return -1;
540 }
541
542 return fd;
543}
544
Marcel Holtmann43177232013-12-30 20:48:31 -0800545static void signal_callback(int signum, void *user_data)
546{
547 switch (signum) {
548 case SIGINT:
549 case SIGTERM:
550 mainloop_quit();
551 break;
552 }
553}
554
555static void usage(void)
556{
557 printf("btproxy - Bluetooth controller proxy\n"
558 "Usage:\n");
559 printf("\tbtproxy [options]\n");
Marcel Holtmann81294e52014-01-16 00:27:30 -0800560 printf("Options:\n"
Marcel Holtmann98921e12013-12-30 23:34:18 -0800561 "\t-c, --connect <address> Connect to server\n"
562 "\t-l, --listen [address] Use TCP server\n"
563 "\t-u, --unix [path] Use Unix server\n"
564 "\t-p, --port <port> Use specified TCP port\n"
565 "\t-i, --index <num> Use specified controller\n"
Marcel Holtmann81294e52014-01-16 00:27:30 -0800566 "\t-d, --debug Enable debugging output\n"
Marcel Holtmann98921e12013-12-30 23:34:18 -0800567 "\t-h, --help Show help options\n");
Marcel Holtmann43177232013-12-30 20:48:31 -0800568}
569
570static const struct option main_options[] = {
Marcel Holtmann98921e12013-12-30 23:34:18 -0800571 { "connect", required_argument, NULL, 'c' },
572 { "listen", optional_argument, NULL, 'l' },
Marcel Holtmann15097d32013-12-30 21:26:31 -0800573 { "unix", optional_argument, NULL, 'u' },
Marcel Holtmann98921e12013-12-30 23:34:18 -0800574 { "port", required_argument, NULL, 'p' },
Marcel Holtmann43177232013-12-30 20:48:31 -0800575 { "index", required_argument, NULL, 'i' },
Marcel Holtmann81294e52014-01-16 00:27:30 -0800576 { "debug", no_argument, NULL, 'd' },
Marcel Holtmann43177232013-12-30 20:48:31 -0800577 { "version", no_argument, NULL, 'v' },
578 { "help", no_argument, NULL, 'h' },
579 { }
580};
581
582int main(int argc, char *argv[])
583{
Marcel Holtmann98921e12013-12-30 23:34:18 -0800584 const char *connect_address = NULL;
585 const char *server_address = NULL;
586 const char *unix_path = NULL;
Marcel Holtmann81294e52014-01-16 00:27:30 -0800587 unsigned short tcp_port = 0xb1ee; /* 45550 */
Marcel Holtmann43177232013-12-30 20:48:31 -0800588 const char *str;
589 sigset_t mask;
590
591 for (;;) {
592 int opt;
593
Marcel Holtmann81294e52014-01-16 00:27:30 -0800594 opt = getopt_long(argc, argv, "c:l::u::p:i:dvh",
Marcel Holtmann43177232013-12-30 20:48:31 -0800595 main_options, NULL);
596 if (opt < 0)
597 break;
598
599 switch (opt) {
Marcel Holtmann98921e12013-12-30 23:34:18 -0800600 case 'c':
601 connect_address = optarg;
602 break;
603 case 'l':
604 if (optarg)
605 server_address = optarg;
606 else
607 server_address = "0.0.0.0";
608 break;
Marcel Holtmann43177232013-12-30 20:48:31 -0800609 case 'u':
610 if (optarg)
Marcel Holtmann98921e12013-12-30 23:34:18 -0800611 unix_path = optarg;
Marcel Holtmann43177232013-12-30 20:48:31 -0800612 else
Marcel Holtmann98921e12013-12-30 23:34:18 -0800613 unix_path = "/tmp/bt-server-bredr";
Marcel Holtmann43177232013-12-30 20:48:31 -0800614 break;
Marcel Holtmann15097d32013-12-30 21:26:31 -0800615 case 'p':
Marcel Holtmann98921e12013-12-30 23:34:18 -0800616 tcp_port = atoi(optarg);
Marcel Holtmann15097d32013-12-30 21:26:31 -0800617 break;
Marcel Holtmann43177232013-12-30 20:48:31 -0800618 case 'i':
619 if (strlen(optarg) > 3 && !strncmp(optarg, "hci", 3))
620 str = optarg + 3;
621 else
622 str = optarg;
623 if (!isdigit(*str)) {
624 usage();
625 return EXIT_FAILURE;
626 }
627 hci_index = atoi(str);
628 break;
Marcel Holtmann81294e52014-01-16 00:27:30 -0800629 case 'd':
630 debug_enabled = true;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800631 break;
Marcel Holtmann43177232013-12-30 20:48:31 -0800632 case 'v':
633 printf("%s\n", VERSION);
634 return EXIT_SUCCESS;
635 case 'h':
636 usage();
637 return EXIT_SUCCESS;
638 default:
639 return EXIT_FAILURE;
640 }
641 }
642
643 if (argc - optind > 0) {
644 fprintf(stderr, "Invalid command line parameters\n");
645 return EXIT_FAILURE;
646 }
647
Marcel Holtmann81294e52014-01-16 00:27:30 -0800648 if (unix_path && server_address) {
649 fprintf(stderr, "Invalid to specify TCP and Unix servers\n");
650 return EXIT_FAILURE;
651 }
652
653 if (connect_address && (unix_path || server_address)) {
654 fprintf(stderr, "Invalid to specify client and server mode\n");
655 return EXIT_FAILURE;
656 }
657
Marcel Holtmann43177232013-12-30 20:48:31 -0800658 mainloop_init();
659
660 sigemptyset(&mask);
661 sigaddset(&mask, SIGINT);
662 sigaddset(&mask, SIGTERM);
663
664 mainloop_set_signal(&mask, signal_callback, NULL, NULL);
665
Marcel Holtmann98921e12013-12-30 23:34:18 -0800666 if (connect_address) {
Marcel Holtmann81294e52014-01-16 00:27:30 -0800667 int host_fd, dev_fd;
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800668
Marcel Holtmann98921e12013-12-30 23:34:18 -0800669 printf("Connecting to %s:%u\n", connect_address, tcp_port);
670
Marcel Holtmann81294e52014-01-16 00:27:30 -0800671 dev_fd = connect_tcp(connect_address, tcp_port);
672 if (dev_fd < 0)
Marcel Holtmann98921e12013-12-30 23:34:18 -0800673 return EXIT_FAILURE;
674
Marcel Holtmann98921e12013-12-30 23:34:18 -0800675 printf("Opening virtual device\n");
676
Marcel Holtmann81294e52014-01-16 00:27:30 -0800677 host_fd = open_vhci(0x00);
678 if (host_fd < 0) {
679 close(dev_fd);
Marcel Holtmann98921e12013-12-30 23:34:18 -0800680 return EXIT_FAILURE;
681 }
682
Andrei Emeltchenkoe27ca4c2014-01-23 11:07:28 +0200683 if (!setup_proxy(host_fd, false, dev_fd, true)) {
684 close(dev_fd);
685 close(host_fd);
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800686 return EXIT_FAILURE;
Andrei Emeltchenkoe27ca4c2014-01-23 11:07:28 +0200687 }
Marcel Holtmann98921e12013-12-30 23:34:18 -0800688 } else {
Marcel Holtmannf8c2b5e2013-12-31 23:16:16 -0800689 int server_fd;
690
Marcel Holtmann98921e12013-12-30 23:34:18 -0800691 if (unix_path) {
692 printf("Listening on %s\n", unix_path);
693
694 server_fd = open_unix(unix_path);
695 } else if (server_address) {
696 printf("Listening on %s:%u\n", server_address,
697 tcp_port);
698
699 server_fd = open_tcp(server_address, tcp_port);
700 } else {
701 fprintf(stderr, "Missing emulator device\n");
702 return EXIT_FAILURE;
703 }
704
705 if (server_fd < 0)
706 return EXIT_FAILURE;
707
708 mainloop_add_fd(server_fd, EPOLLIN, server_callback,
709 NULL, NULL);
Marcel Holtmann43177232013-12-30 20:48:31 -0800710 }
711
Marcel Holtmann43177232013-12-30 20:48:31 -0800712 return mainloop_run();
713}