3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2011-2012 Intel Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
37 #include <sys/ioctl.h>
40 #include "lib/bluetooth.h"
42 #include "lib/hci_lib.h"
44 #include "hciattach.h"
45 #include "monitor/bt.h"
46 #include "src/shared/mainloop.h"
47 #include "src/shared/timeout.h"
48 #include "src/shared/util.h"
49 #include "src/shared/hci.h"
51 static int open_serial(const char *path)
54 int fd, saved_ldisc, ldisc = N_HCI;
56 fd = open(path, O_RDWR | O_NOCTTY);
58 perror("Failed to open serial port");
62 if (tcflush(fd, TCIOFLUSH) < 0) {
63 perror("Failed to flush serial port");
68 if (ioctl(fd, TIOCGETD, &saved_ldisc) < 0) {
69 perror("Failed get serial line discipline");
74 /* Switch TTY to raw mode */
75 memset(&ti, 0, sizeof(ti));
78 ti.c_cflag |= (B115200 | CLOCAL | CREAD);
80 /* Set flow control */
81 ti.c_cflag |= CRTSCTS;
83 if (tcsetattr(fd, TCSANOW, &ti) < 0) {
84 perror("Failed to set serial port settings");
89 if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
90 perror("Failed set serial line discipline");
95 printf("Switched line discipline from %d to %d\n", saved_ldisc, ldisc);
100 static void local_version_callback(const void *data, uint8_t size,
103 const struct bt_hci_rsp_read_local_version *rsp = data;
105 printf("Manufacturer: %u\n", le16_to_cpu(rsp->manufacturer));
108 static int attach_proto(const char *path, unsigned int proto,
113 fd = open_serial(path);
117 if (ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
118 perror("Failed to set flags");
123 if (ioctl(fd, HCIUARTSETPROTO, proto) < 0) {
124 perror("Failed to set protocol");
129 dev_id = ioctl(fd, HCIUARTGETDEVICE);
131 perror("Failed to get device id");
136 printf("Device index %d attached\n", dev_id);
138 if (flags & (1 << HCI_UART_RAW_DEVICE)) {
139 unsigned int attempts = 6;
142 while (attempts-- > 0) {
143 hci = bt_hci_new_user_channel(dev_id);
151 fprintf(stderr, "Failed to open HCI user channel\n");
156 bt_hci_send(hci, BT_HCI_CMD_READ_LOCAL_VERSION, NULL, 0,
157 local_version_callback, hci,
158 (bt_hci_destroy_func_t) bt_hci_unref);
164 static void uart_callback(int fd, uint32_t events, void *user_data)
166 printf("UART callback handling\n");
169 static void signal_callback(int signum, void *user_data)
171 static bool terminated = false;
183 static void usage(void)
185 printf("btattach - Bluetooth serial utility\n"
187 printf("\tbtattach [options]\n");
189 "\t-B, --bredr <device> Attach BR/EDR controller\n"
190 "\t-A, --amp <device> Attach AMP controller\n"
191 "\t-P, --protocol <proto> Specify protocol type\n"
192 "\t-h, --help Show help options\n");
195 static const struct option main_options[] = {
196 { "bredr", required_argument, NULL, 'B' },
197 { "amp", required_argument, NULL, 'A' },
198 { "protocol", required_argument, NULL, 'P' },
199 { "version", no_argument, NULL, 'v' },
200 { "help", no_argument, NULL, 'h' },
204 static const struct {
208 { "h4", HCI_UART_H4 },
209 { "bcsp", HCI_UART_BCSP },
210 { "3wire", HCI_UART_3WIRE },
211 { "h4ds", HCI_UART_H4DS },
212 { "ll", HCI_UART_LL },
213 { "ath3k", HCI_UART_ATH3K },
214 { "intel", HCI_UART_INTEL },
215 { "bcm", HCI_UART_BCM },
216 { "qca", HCI_UART_QCA },
220 int main(int argc, char *argv[])
222 const char *bredr_path = NULL, *amp_path = NULL, *proto = NULL;
223 bool raw_device = false;
225 int exit_status, count = 0, proto_id = HCI_UART_H4;
230 opt = getopt_long(argc, argv, "B:A:P:Rvh",
249 printf("%s\n", VERSION);
259 if (argc - optind > 0) {
260 fprintf(stderr, "Invalid command line parameters\n");
267 sigaddset(&mask, SIGINT);
268 sigaddset(&mask, SIGTERM);
270 mainloop_set_signal(&mask, signal_callback, NULL, NULL);
275 for (i = 0; proto_table[i].name; i++) {
276 if (!strcmp(proto_table[i].name, proto)) {
277 proto_id = proto_table[i].id;
282 if (!proto_table[i].name) {
283 fprintf(stderr, "Invalid protocol\n");
292 printf("Attaching BR/EDR controller to %s\n", bredr_path);
294 flags = (1 << HCI_UART_RESET_ON_INIT);
297 flags = (1 << HCI_UART_RAW_DEVICE);
299 fd = attach_proto(bredr_path, proto_id, flags);
301 mainloop_add_fd(fd, 0, uart_callback, NULL, NULL);
310 printf("Attaching AMP controller to %s\n", amp_path);
312 flags = (1 << HCI_UART_RESET_ON_INIT) |
313 (1 << HCI_UART_CREATE_AMP);
316 flags = (1 << HCI_UART_RAW_DEVICE);
318 fd = attach_proto(amp_path, proto_id, flags);
320 mainloop_add_fd(fd, 0, uart_callback, NULL, NULL);
326 fprintf(stderr, "No controller attached\n");
330 exit_status = mainloop_run();