3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2015 Intel Corporation. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <sys/ioctl.h>
32 #include "lib/bluetooth.h"
34 #include "lib/hci_lib.h"
35 #include "tools/hciattach.h"
36 #include "peripheral/attach.h"
38 static const char *serial_dev = "/dev/ttyS1";
39 static int serial_fd = -1;
41 static int open_serial(const char *path)
44 int fd, saved_ldisc, ldisc = N_HCI;
46 fd = open(path, O_RDWR | O_NOCTTY);
48 perror("Failed to open serial port");
52 if (tcflush(fd, TCIOFLUSH) < 0) {
53 perror("Failed to flush serial port");
58 if (ioctl(fd, TIOCGETD, &saved_ldisc) < 0) {
59 perror("Failed get serial line discipline");
64 /* Switch TTY to raw mode */
65 memset(&ti, 0, sizeof(ti));
68 ti.c_cflag |= (B115200 | CLOCAL | CREAD);
70 /* Set flow control */
71 ti.c_cflag |= CRTSCTS;
73 if (tcsetattr(fd, TCSANOW, &ti) < 0) {
74 perror("Failed to set serial port settings");
79 if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
80 perror("Failed set serial line discipline");
85 printf("Switched line discipline from %d to %d\n", saved_ldisc, ldisc);
90 static int attach_proto(const char *path, unsigned int proto,
95 fd = open_serial(path);
99 if (ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
100 perror("Failed to set flags");
105 if (ioctl(fd, HCIUARTSETPROTO, proto) < 0) {
106 perror("Failed to set protocol");
111 dev_id = ioctl(fd, HCIUARTGETDEVICE);
113 perror("Failed to get device id");
118 printf("Device index %d attached\n", dev_id);
123 void attach_start(void)
130 printf("Attaching BR/EDR controller to %s\n", serial_dev);
132 flags = (1 << HCI_UART_RESET_ON_INIT);
134 serial_fd = attach_proto(serial_dev, HCI_UART_H4, flags);
137 void attach_stop(void)