btinfo.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #include <ctype.h>
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <getopt.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/socket.h>
  22. #include "monitor/bt.h"
  23. #include "src/shared/mainloop.h"
  24. #include "src/shared/timeout.h"
  25. #include "src/shared/util.h"
  26. #include "src/shared/hci.h"
  27. #define BTPROTO_HCI 1
  28. struct hci_dev_stats {
  29. uint32_t err_rx;
  30. uint32_t err_tx;
  31. uint32_t cmd_tx;
  32. uint32_t evt_rx;
  33. uint32_t acl_tx;
  34. uint32_t acl_rx;
  35. uint32_t sco_tx;
  36. uint32_t sco_rx;
  37. uint32_t byte_rx;
  38. uint32_t byte_tx;
  39. };
  40. struct hci_dev_info {
  41. uint16_t dev_id;
  42. char name[8];
  43. uint8_t bdaddr[6];
  44. uint32_t flags;
  45. uint8_t type;
  46. uint8_t features[8];
  47. uint32_t pkt_type;
  48. uint32_t link_policy;
  49. uint32_t link_mode;
  50. uint16_t acl_mtu;
  51. uint16_t acl_pkts;
  52. uint16_t sco_mtu;
  53. uint16_t sco_pkts;
  54. struct hci_dev_stats stat;
  55. };
  56. #define HCIDEVUP _IOW('H', 201, int)
  57. #define HCIDEVDOWN _IOW('H', 202, int)
  58. #define HCIGETDEVINFO _IOR('H', 211, int)
  59. #define HCI_UP (1 << 0)
  60. #define HCI_PRIMARY 0x00
  61. #define HCI_AMP 0x01
  62. static struct hci_dev_info hci_info;
  63. static uint8_t hci_type;
  64. static struct bt_hci *hci_dev;
  65. static bool reset_on_init = false;
  66. static bool reset_on_shutdown = false;
  67. static bool shutdown_timeout(void *user_data)
  68. {
  69. mainloop_quit();
  70. return false;
  71. }
  72. static void shutdown_complete(const void *data, uint8_t size, void *user_data)
  73. {
  74. unsigned int id = PTR_TO_UINT(user_data);
  75. timeout_remove(id);
  76. mainloop_quit();
  77. }
  78. static void shutdown_device(void)
  79. {
  80. unsigned int id;
  81. bt_hci_flush(hci_dev);
  82. if (reset_on_shutdown) {
  83. id = timeout_add(5000, shutdown_timeout, NULL, NULL);
  84. bt_hci_send(hci_dev, BT_HCI_CMD_RESET, NULL, 0,
  85. shutdown_complete, UINT_TO_PTR(id), NULL);
  86. } else
  87. mainloop_quit();
  88. }
  89. static void local_version_callback(const void *data, uint8_t size,
  90. void *user_data)
  91. {
  92. const struct bt_hci_rsp_read_local_version *rsp = data;
  93. printf("HCI version: %u\n", rsp->hci_ver);
  94. printf("HCI revision: %u\n", le16_to_cpu(rsp->hci_rev));
  95. switch (hci_type) {
  96. case HCI_PRIMARY:
  97. printf("LMP version: %u\n", rsp->lmp_ver);
  98. printf("LMP subversion: %u\n", le16_to_cpu(rsp->lmp_subver));
  99. break;
  100. case HCI_AMP:
  101. printf("PAL version: %u\n", rsp->lmp_ver);
  102. printf("PAL subversion: %u\n", le16_to_cpu(rsp->lmp_subver));
  103. break;
  104. }
  105. printf("Manufacturer: %u\n", le16_to_cpu(rsp->manufacturer));
  106. }
  107. static void local_commands_callback(const void *data, uint8_t size,
  108. void *user_data)
  109. {
  110. shutdown_device();
  111. }
  112. static void local_features_callback(const void *data, uint8_t size,
  113. void *user_data)
  114. {
  115. bt_hci_send(hci_dev, BT_HCI_CMD_READ_LOCAL_COMMANDS, NULL, 0,
  116. local_commands_callback, NULL, NULL);
  117. }
  118. static bool cmd_local(int argc, char *argv[])
  119. {
  120. if (reset_on_init)
  121. bt_hci_send(hci_dev, BT_HCI_CMD_RESET, NULL, 0,
  122. NULL, NULL, NULL);
  123. bt_hci_send(hci_dev, BT_HCI_CMD_READ_LOCAL_VERSION, NULL, 0,
  124. local_version_callback, NULL, NULL);
  125. bt_hci_send(hci_dev, BT_HCI_CMD_READ_LOCAL_FEATURES, NULL, 0,
  126. local_features_callback, NULL, NULL);
  127. return true;
  128. }
  129. typedef bool (*cmd_func_t)(int argc, char *argv[]);
  130. static const struct {
  131. const char *name;
  132. cmd_func_t func;
  133. const char *help;
  134. } cmd_table[] = {
  135. { "local", cmd_local, "Print local controller details" },
  136. { }
  137. };
  138. static void signal_callback(int signum, void *user_data)
  139. {
  140. static bool terminated = false;
  141. switch (signum) {
  142. case SIGINT:
  143. case SIGTERM:
  144. if (!terminated) {
  145. shutdown_device();
  146. terminated = true;
  147. }
  148. break;
  149. }
  150. }
  151. static void usage(void)
  152. {
  153. int i;
  154. printf("btinfo - Bluetooth device testing tool\n"
  155. "Usage:\n");
  156. printf("\tbtinfo [options] <command>\n");
  157. printf("options:\n"
  158. "\t-i, --index <num> Use specified controller\n"
  159. "\t-h, --help Show help options\n");
  160. printf("commands:\n");
  161. for (i = 0; cmd_table[i].name; i++)
  162. printf("\t%-25s%s\n", cmd_table[i].name, cmd_table[i].help);
  163. }
  164. static const struct option main_options[] = {
  165. { "index", required_argument, NULL, 'i' },
  166. { "reset", no_argument, NULL, 'r' },
  167. { "raw", no_argument, NULL, 'R' },
  168. { "version", no_argument, NULL, 'v' },
  169. { "help", no_argument, NULL, 'h' },
  170. { }
  171. };
  172. int main(int argc, char *argv[])
  173. {
  174. cmd_func_t func = NULL;
  175. uint16_t index = 0;
  176. const char *str;
  177. bool use_raw = false;
  178. bool power_down = false;
  179. int fd, i, exit_status;
  180. for (;;) {
  181. int opt;
  182. opt = getopt_long(argc, argv, "i:rRvh", main_options, NULL);
  183. if (opt < 0)
  184. break;
  185. switch (opt) {
  186. case 'i':
  187. if (strlen(optarg) > 3 && !strncmp(optarg, "hci", 3))
  188. str = optarg + 3;
  189. else
  190. str = optarg;
  191. if (!isdigit(*str)) {
  192. usage();
  193. return EXIT_FAILURE;
  194. }
  195. index = atoi(str);
  196. break;
  197. case 'r':
  198. reset_on_init = true;
  199. break;
  200. case 'R':
  201. use_raw = true;
  202. break;
  203. case 'v':
  204. printf("%s\n", VERSION);
  205. return EXIT_SUCCESS;
  206. case 'h':
  207. usage();
  208. return EXIT_SUCCESS;
  209. default:
  210. return EXIT_FAILURE;
  211. }
  212. }
  213. if (argc - optind < 1) {
  214. fprintf(stderr, "Missing command argument\n");
  215. return EXIT_FAILURE;
  216. }
  217. for (i = 0; cmd_table[i].name; i++) {
  218. if (!strcmp(cmd_table[i].name, argv[optind])) {
  219. func = cmd_table[i].func;
  220. break;
  221. }
  222. }
  223. if (!func) {
  224. fprintf(stderr, "Unsupported command specified\n");
  225. return EXIT_FAILURE;
  226. }
  227. mainloop_init();
  228. printf("Bluetooth information utility ver %s\n", VERSION);
  229. fd = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
  230. if (fd < 0) {
  231. perror("Failed to open HCI raw socket");
  232. return EXIT_FAILURE;
  233. }
  234. memset(&hci_info, 0, sizeof(hci_info));
  235. hci_info.dev_id = index;
  236. if (ioctl(fd, HCIGETDEVINFO, (void *) &hci_info) < 0) {
  237. perror("Failed to get HCI device information");
  238. close(fd);
  239. return EXIT_FAILURE;
  240. }
  241. if (use_raw && !(hci_info.flags & HCI_UP)) {
  242. printf("Powering on controller\n");
  243. if (ioctl(fd, HCIDEVUP, hci_info.dev_id) < 0) {
  244. perror("Failed to power on controller");
  245. close(fd);
  246. return EXIT_FAILURE;
  247. }
  248. power_down = true;
  249. }
  250. close(fd);
  251. hci_type = (hci_info.type & 0x30) >> 4;
  252. if (use_raw) {
  253. hci_dev = bt_hci_new_raw_device(index);
  254. if (!hci_dev) {
  255. fprintf(stderr, "Failed to open HCI raw device\n");
  256. return EXIT_FAILURE;
  257. }
  258. } else {
  259. hci_dev = bt_hci_new_user_channel(index);
  260. if (!hci_dev) {
  261. fprintf(stderr, "Failed to open HCI user channel\n");
  262. return EXIT_FAILURE;
  263. }
  264. reset_on_init = true;
  265. reset_on_shutdown = true;
  266. }
  267. if (!func(argc - optind - 1, argv + optind + 1)) {
  268. bt_hci_unref(hci_dev);
  269. return EXIT_FAILURE;
  270. }
  271. exit_status = mainloop_run_with_signal(signal_callback, NULL);
  272. bt_hci_unref(hci_dev);
  273. if (use_raw && power_down) {
  274. fd = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
  275. if (fd >= 0) {
  276. printf("Powering down controller\n");
  277. if (ioctl(fd, HCIDEVDOWN, hci_info.dev_id) < 0)
  278. perror("Failed to power down controller");
  279. close(fd);
  280. }
  281. }
  282. return exit_status;
  283. }