btattach.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2012 Intel Corporation
  7. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #define _GNU_SOURCE
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <getopt.h>
  22. #include <termios.h>
  23. #include <sys/ioctl.h>
  24. #include <poll.h>
  25. #include "lib/bluetooth.h"
  26. #include "lib/hci.h"
  27. #include "lib/hci_lib.h"
  28. #include "hciattach.h"
  29. #include "monitor/bt.h"
  30. #include "src/shared/mainloop.h"
  31. #include "src/shared/timeout.h"
  32. #include "src/shared/util.h"
  33. #include "src/shared/tty.h"
  34. #include "src/shared/hci.h"
  35. static int open_serial(const char *path, unsigned int speed, bool flowctl)
  36. {
  37. struct termios ti;
  38. int fd, saved_ldisc, ldisc = N_HCI;
  39. fd = open(path, O_RDWR | O_NOCTTY);
  40. if (fd < 0) {
  41. perror("Failed to open serial port");
  42. return -1;
  43. }
  44. if (tcflush(fd, TCIOFLUSH) < 0) {
  45. perror("Failed to flush serial port");
  46. close(fd);
  47. return -1;
  48. }
  49. if (ioctl(fd, TIOCGETD, &saved_ldisc) < 0) {
  50. perror("Failed get serial line discipline");
  51. close(fd);
  52. return -1;
  53. }
  54. /* Switch TTY to raw mode */
  55. memset(&ti, 0, sizeof(ti));
  56. cfmakeraw(&ti);
  57. ti.c_cflag |= (speed | CLOCAL | CREAD);
  58. if (flowctl) {
  59. /* Set flow control */
  60. ti.c_cflag |= CRTSCTS;
  61. }
  62. if (tcsetattr(fd, TCSANOW, &ti) < 0) {
  63. perror("Failed to set serial port settings");
  64. close(fd);
  65. return -1;
  66. }
  67. if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
  68. perror("Failed set serial line discipline");
  69. close(fd);
  70. return -1;
  71. }
  72. printf("Switched line discipline from %d to %d\n", saved_ldisc, ldisc);
  73. return fd;
  74. }
  75. static void local_version_callback(const void *data, uint8_t size,
  76. void *user_data)
  77. {
  78. const struct bt_hci_rsp_read_local_version *rsp = data;
  79. printf("Manufacturer: %u\n", le16_to_cpu(rsp->manufacturer));
  80. }
  81. static int attach_proto(const char *path, unsigned int proto,
  82. unsigned int speed, bool flowctl, unsigned int flags)
  83. {
  84. int fd, dev_id;
  85. fd = open_serial(path, speed, flowctl);
  86. if (fd < 0)
  87. return -1;
  88. if (ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
  89. perror("Failed to set flags");
  90. close(fd);
  91. return -1;
  92. }
  93. if (ioctl(fd, HCIUARTSETPROTO, proto) < 0) {
  94. perror("Failed to set protocol");
  95. close(fd);
  96. return -1;
  97. }
  98. dev_id = ioctl(fd, HCIUARTGETDEVICE);
  99. if (dev_id < 0) {
  100. perror("Failed to get device id");
  101. close(fd);
  102. return -1;
  103. }
  104. printf("Device index %d attached\n", dev_id);
  105. if (flags & (1 << HCI_UART_RAW_DEVICE)) {
  106. unsigned int attempts = 6;
  107. struct bt_hci *hci;
  108. while (attempts-- > 0) {
  109. hci = bt_hci_new_user_channel(dev_id);
  110. if (hci)
  111. break;
  112. usleep(250 * 1000);
  113. }
  114. if (!hci) {
  115. fprintf(stderr, "Failed to open HCI user channel\n");
  116. close(fd);
  117. return -1;
  118. }
  119. bt_hci_send(hci, BT_HCI_CMD_READ_LOCAL_VERSION, NULL, 0,
  120. local_version_callback, hci,
  121. (bt_hci_destroy_func_t) bt_hci_unref);
  122. }
  123. return fd;
  124. }
  125. static void uart_callback(int fd, uint32_t events, void *user_data)
  126. {
  127. printf("UART callback handling\n");
  128. }
  129. static void signal_callback(int signum, void *user_data)
  130. {
  131. static bool terminated = false;
  132. switch (signum) {
  133. case SIGINT:
  134. case SIGTERM:
  135. if (!terminated) {
  136. mainloop_quit();
  137. terminated = true;
  138. }
  139. break;
  140. }
  141. }
  142. static void usage(void)
  143. {
  144. printf("btattach - Bluetooth serial utility\n"
  145. "Usage:\n");
  146. printf("\tbtattach [options]\n");
  147. printf("options:\n"
  148. "\t-B, --bredr <device> Attach Primary controller\n"
  149. "\t-A, --amp <device> Attach AMP controller\n"
  150. "\t-P, --protocol <proto> Specify protocol type\n"
  151. "\t-S, --speed <baudrate> Specify which baudrate to use\n"
  152. "\t-N, --noflowctl Disable flow control\n"
  153. "\t-h, --help Show help options\n");
  154. }
  155. static const struct option main_options[] = {
  156. { "bredr", required_argument, NULL, 'B' },
  157. { "amp", required_argument, NULL, 'A' },
  158. { "protocol", required_argument, NULL, 'P' },
  159. { "speed", required_argument, NULL, 'S' },
  160. { "noflowctl",no_argument, NULL, 'N' },
  161. { "version", no_argument, NULL, 'v' },
  162. { "help", no_argument, NULL, 'h' },
  163. { }
  164. };
  165. static const struct {
  166. const char *name;
  167. unsigned int id;
  168. } proto_table[] = {
  169. { "h4", HCI_UART_H4 },
  170. { "bcsp", HCI_UART_BCSP },
  171. { "3wire", HCI_UART_3WIRE },
  172. { "h4ds", HCI_UART_H4DS },
  173. { "ll", HCI_UART_LL },
  174. { "ath3k", HCI_UART_ATH3K },
  175. { "intel", HCI_UART_INTEL },
  176. { "bcm", HCI_UART_BCM },
  177. { "qca", HCI_UART_QCA },
  178. { "ag6xx", HCI_UART_AG6XX },
  179. { "nokia", HCI_UART_NOKIA },
  180. { "mrvl", HCI_UART_MRVL },
  181. { }
  182. };
  183. int main(int argc, char *argv[])
  184. {
  185. const char *bredr_path = NULL, *amp_path = NULL, *proto = NULL;
  186. bool flowctl = true, raw_device = false;
  187. int exit_status, count = 0, proto_id = HCI_UART_H4;
  188. unsigned int speed = B115200;
  189. for (;;) {
  190. int opt;
  191. opt = getopt_long(argc, argv, "B:A:P:S:NRvh",
  192. main_options, NULL);
  193. if (opt < 0)
  194. break;
  195. switch (opt) {
  196. case 'B':
  197. bredr_path = optarg;
  198. break;
  199. case 'A':
  200. amp_path = optarg;
  201. break;
  202. case 'P':
  203. proto = optarg;
  204. break;
  205. case 'S':
  206. speed = tty_get_speed(atoi(optarg));
  207. if (!speed) {
  208. fprintf(stderr, "Invalid speed: %s\n", optarg);
  209. return EXIT_FAILURE;
  210. }
  211. break;
  212. case 'N':
  213. flowctl = false;
  214. break;
  215. case 'R':
  216. raw_device = true;
  217. break;
  218. case 'v':
  219. printf("%s\n", VERSION);
  220. return EXIT_SUCCESS;
  221. case 'h':
  222. usage();
  223. return EXIT_SUCCESS;
  224. default:
  225. return EXIT_FAILURE;
  226. }
  227. }
  228. if (argc - optind > 0) {
  229. fprintf(stderr, "Invalid command line parameters\n");
  230. return EXIT_FAILURE;
  231. }
  232. mainloop_init();
  233. if (proto) {
  234. unsigned int i;
  235. for (i = 0; proto_table[i].name; i++) {
  236. if (!strcmp(proto_table[i].name, proto)) {
  237. proto_id = proto_table[i].id;
  238. break;
  239. }
  240. }
  241. if (!proto_table[i].name) {
  242. fprintf(stderr, "Invalid protocol\n");
  243. return EXIT_FAILURE;
  244. }
  245. }
  246. if (bredr_path) {
  247. unsigned long flags;
  248. int fd;
  249. printf("Attaching Primary controller to %s\n", bredr_path);
  250. flags = (1 << HCI_UART_RESET_ON_INIT);
  251. if (raw_device)
  252. flags = (1 << HCI_UART_RAW_DEVICE);
  253. fd = attach_proto(bredr_path, proto_id, speed, flowctl, flags);
  254. if (fd >= 0) {
  255. mainloop_add_fd(fd, 0, uart_callback, NULL, NULL);
  256. count++;
  257. }
  258. }
  259. if (amp_path) {
  260. unsigned long flags;
  261. int fd;
  262. printf("Attaching AMP controller to %s\n", amp_path);
  263. flags = (1 << HCI_UART_RESET_ON_INIT) |
  264. (1 << HCI_UART_CREATE_AMP);
  265. if (raw_device)
  266. flags = (1 << HCI_UART_RAW_DEVICE);
  267. fd = attach_proto(amp_path, proto_id, speed, flowctl, flags);
  268. if (fd >= 0) {
  269. mainloop_add_fd(fd, 0, uart_callback, NULL, NULL);
  270. count++;
  271. }
  272. }
  273. if (count < 1) {
  274. fprintf(stderr, "No controller attached\n");
  275. return EXIT_FAILURE;
  276. }
  277. exit_status = mainloop_run_with_signal(signal_callback, NULL);
  278. return exit_status;
  279. }