main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2014 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 <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdbool.h>
  17. #include <getopt.h>
  18. #include "src/shared/mainloop.h"
  19. #include "src/shared/util.h"
  20. #include "serial.h"
  21. #include "server.h"
  22. #include "vhci.h"
  23. #include "amp.h"
  24. #include "le.h"
  25. static void signal_callback(int signum, void *user_data)
  26. {
  27. switch (signum) {
  28. case SIGINT:
  29. case SIGTERM:
  30. mainloop_quit();
  31. break;
  32. }
  33. }
  34. static void usage(void)
  35. {
  36. printf("btvirt - Bluetooth emulator\n"
  37. "Usage:\n");
  38. printf("\tbtvirt [options]\n");
  39. printf("options:\n"
  40. "\t-d Enable debug\n"
  41. "\t-S Create local serial port\n"
  42. "\t-s Create local server sockets\n"
  43. "\t-l[num] Number of local controllers\n"
  44. "\t-L Create LE only controller\n"
  45. "\t-U[num] Number of test LE controllers\n"
  46. "\t-B Create BR/EDR only controller\n"
  47. "\t-A Create AMP controller\n"
  48. "\t-T[num] Number of test AMP controllers\n"
  49. "\t-h, --help Show help options\n");
  50. }
  51. static const struct option main_options[] = {
  52. { "debug", no_argument, NULL, 'd' },
  53. { "serial", no_argument, NULL, 'S' },
  54. { "server", no_argument, NULL, 's' },
  55. { "local", optional_argument, NULL, 'l' },
  56. { "le", no_argument, NULL, 'L' },
  57. { "bredr", no_argument, NULL, 'B' },
  58. { "amp", no_argument, NULL, 'A' },
  59. { "letest", optional_argument, NULL, 'U' },
  60. { "amptest", optional_argument, NULL, 'T' },
  61. { "version", no_argument, NULL, 'v' },
  62. { "help", no_argument, NULL, 'h' },
  63. { }
  64. };
  65. static void vhci_debug(const char *str, void *user_data)
  66. {
  67. int i = PTR_TO_UINT(user_data);
  68. printf("vhci%u: %s\n", i, str);
  69. }
  70. int main(int argc, char *argv[])
  71. {
  72. struct server *server1;
  73. struct server *server2;
  74. struct server *server3;
  75. struct server *server4;
  76. struct server *server5;
  77. bool debug_enabled = false;
  78. bool server_enabled = false;
  79. bool serial_enabled = false;
  80. int letest_count = 0;
  81. int amptest_count = 0;
  82. int vhci_count = 0;
  83. enum vhci_type vhci_type = VHCI_TYPE_BREDRLE;
  84. int i;
  85. mainloop_init();
  86. for (;;) {
  87. int opt;
  88. opt = getopt_long(argc, argv, "dSsl::LBAU::T::vh",
  89. main_options, NULL);
  90. if (opt < 0)
  91. break;
  92. switch (opt) {
  93. case 'd':
  94. debug_enabled = true;
  95. break;
  96. case 'S':
  97. serial_enabled = true;
  98. break;
  99. case 's':
  100. server_enabled = true;
  101. break;
  102. case 'l':
  103. if (optarg)
  104. vhci_count = atoi(optarg);
  105. else
  106. vhci_count = 1;
  107. break;
  108. case 'L':
  109. vhci_type = VHCI_TYPE_LE;
  110. break;
  111. case 'B':
  112. vhci_type = VHCI_TYPE_BREDR;
  113. break;
  114. case 'A':
  115. vhci_type = VHCI_TYPE_AMP;
  116. break;
  117. case 'U':
  118. if (optarg)
  119. letest_count = atoi(optarg);
  120. else
  121. letest_count = 1;
  122. break;
  123. case 'T':
  124. if (optarg)
  125. amptest_count = atoi(optarg);
  126. else
  127. amptest_count = 1;
  128. break;
  129. case 'v':
  130. printf("%s\n", VERSION);
  131. return EXIT_SUCCESS;
  132. case 'h':
  133. usage();
  134. return EXIT_SUCCESS;
  135. default:
  136. return EXIT_FAILURE;
  137. }
  138. }
  139. if (letest_count < 1 && amptest_count < 1 &&
  140. vhci_count < 1 && !server_enabled && !serial_enabled) {
  141. fprintf(stderr, "No emulator specified\n");
  142. return EXIT_FAILURE;
  143. }
  144. printf("Bluetooth emulator ver %s\n", VERSION);
  145. for (i = 0; i < letest_count; i++) {
  146. struct bt_le *le;
  147. le = bt_le_new();
  148. if (!le) {
  149. fprintf(stderr, "Failed to create LE controller\n");
  150. return EXIT_FAILURE;
  151. }
  152. }
  153. for (i = 0; i < amptest_count; i++) {
  154. struct bt_amp *amp;
  155. amp = bt_amp_new();
  156. if (!amp) {
  157. fprintf(stderr, "Failed to create AMP controller\n");
  158. return EXIT_FAILURE;
  159. }
  160. }
  161. for (i = 0; i < vhci_count; i++) {
  162. struct vhci *vhci;
  163. vhci = vhci_open(vhci_type);
  164. if (!vhci) {
  165. fprintf(stderr, "Failed to open Virtual HCI device\n");
  166. return EXIT_FAILURE;
  167. }
  168. if (debug_enabled)
  169. vhci_set_debug(vhci, vhci_debug, UINT_TO_PTR(i), NULL);
  170. }
  171. if (serial_enabled) {
  172. struct serial *serial;
  173. serial = serial_open(SERIAL_TYPE_BREDRLE);
  174. if (!serial)
  175. fprintf(stderr, "Failed to open serial emulation\n");
  176. }
  177. if (server_enabled) {
  178. server1 = server_open_unix(SERVER_TYPE_BREDRLE,
  179. "/tmp/bt-server-bredrle");
  180. if (!server1)
  181. fprintf(stderr, "Failed to open BR/EDR/LE server\n");
  182. server2 = server_open_unix(SERVER_TYPE_BREDR,
  183. "/tmp/bt-server-bredr");
  184. if (!server2)
  185. fprintf(stderr, "Failed to open BR/EDR server\n");
  186. server3 = server_open_unix(SERVER_TYPE_AMP,
  187. "/tmp/bt-server-amp");
  188. if (!server3)
  189. fprintf(stderr, "Failed to open AMP server\n");
  190. server4 = server_open_unix(SERVER_TYPE_LE,
  191. "/tmp/bt-server-le");
  192. if (!server4)
  193. fprintf(stderr, "Failed to open LE server\n");
  194. server5 = server_open_unix(SERVER_TYPE_MONITOR,
  195. "/tmp/bt-server-mon");
  196. if (!server5)
  197. fprintf(stderr, "Failed to open monitor server\n");
  198. }
  199. return mainloop_run_with_signal(signal_callback, NULL);
  200. }