bt_cli.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <applications/cli/cli.h>
  4. #include <lib/toolbox/args.h>
  5. #include "bt_settings.h"
  6. static const char* bt_cli_address_types[] = {
  7. "Public Device Address",
  8. "Random Device Address",
  9. "Public Identity Address",
  10. "Random (Static) Identity Address",
  11. };
  12. static void bt_cli_command_hci_info(Cli* cli, string_t args, void* context) {
  13. string_t buffer;
  14. string_init(buffer);
  15. furi_hal_bt_dump_state(buffer);
  16. printf("%s", string_get_cstr(buffer));
  17. string_clear(buffer);
  18. }
  19. static void bt_cli_command_carrier_tx(Cli* cli, string_t args, void* context) {
  20. int channel = 0;
  21. int power = 0;
  22. do {
  23. if(!args_read_int_and_trim(args, &channel) && (channel < 0 || channel > 39)) {
  24. printf("Incorrect or missing channel, expected int 0-39");
  25. break;
  26. }
  27. if(!args_read_int_and_trim(args, &power) && (power < 0 || power > 6)) {
  28. printf("Incorrect or missing power, expected int 0-6");
  29. break;
  30. }
  31. furi_hal_bt_stop_advertising();
  32. printf("Transmitting carrier at %d channel at %d dB power\r\n", channel, power);
  33. printf("Press CTRL+C to stop\r\n");
  34. furi_hal_bt_start_tone_tx(channel, 0x19 + power);
  35. while(!cli_cmd_interrupt_received(cli)) {
  36. osDelay(250);
  37. }
  38. furi_hal_bt_stop_tone_tx();
  39. } while(false);
  40. }
  41. static void bt_cli_command_carrier_rx(Cli* cli, string_t args, void* context) {
  42. int channel = 0;
  43. do {
  44. if(!args_read_int_and_trim(args, &channel) && (channel < 0 || channel > 39)) {
  45. printf("Incorrect or missing channel, expected int 0-39");
  46. break;
  47. }
  48. furi_hal_bt_stop_advertising();
  49. printf("Receiving carrier at %d channel\r\n", channel);
  50. printf("Press CTRL+C to stop\r\n");
  51. furi_hal_bt_start_packet_rx(channel, 1);
  52. while(!cli_cmd_interrupt_received(cli)) {
  53. osDelay(250);
  54. printf("RSSI: %6.1f dB\r", furi_hal_bt_get_rssi());
  55. fflush(stdout);
  56. }
  57. furi_hal_bt_stop_packet_test();
  58. } while(false);
  59. }
  60. static void bt_cli_command_packet_tx(Cli* cli, string_t args, void* context) {
  61. int channel = 0;
  62. int pattern = 0;
  63. int datarate = 1;
  64. do {
  65. if(!args_read_int_and_trim(args, &channel) && (channel < 0 || channel > 39)) {
  66. printf("Incorrect or missing channel, expected int 0-39");
  67. break;
  68. }
  69. if(!args_read_int_and_trim(args, &pattern) && (pattern < 0 || pattern > 5)) {
  70. printf("Incorrect or missing pattern, expected int 0-5 \r\n");
  71. printf("0 - Pseudo-Random bit sequence 9\r\n");
  72. printf("1 - Pattern of alternating bits '11110000'\r\n");
  73. printf("2 - Pattern of alternating bits '10101010'\r\n");
  74. printf("3 - Pseudo-Random bit sequence 15\r\n");
  75. printf("4 - Pattern of All '1' bits\r\n");
  76. printf("5 - Pattern of All '0' bits\r\n");
  77. break;
  78. }
  79. if(!args_read_int_and_trim(args, &datarate) && (datarate < 1 || datarate > 2)) {
  80. printf("Incorrect or missing datarate, expected int 1-2");
  81. break;
  82. }
  83. furi_hal_bt_stop_advertising();
  84. printf(
  85. "Transmitting %d pattern packet at %d channel at %d M datarate\r\n",
  86. pattern,
  87. channel,
  88. datarate);
  89. printf("Press CTRL+C to stop\r\n");
  90. furi_hal_bt_start_packet_tx(channel, pattern, datarate);
  91. while(!cli_cmd_interrupt_received(cli)) {
  92. osDelay(250);
  93. }
  94. furi_hal_bt_stop_packet_test();
  95. printf("Transmitted %lu packets", furi_hal_bt_get_transmitted_packets());
  96. } while(false);
  97. }
  98. static void bt_cli_command_packet_rx(Cli* cli, string_t args, void* context) {
  99. int channel = 0;
  100. int datarate = 1;
  101. do {
  102. if(!args_read_int_and_trim(args, &channel) && (channel < 0 || channel > 39)) {
  103. printf("Incorrect or missing channel, expected int 0-39");
  104. break;
  105. }
  106. if(!args_read_int_and_trim(args, &datarate) && (datarate < 1 || datarate > 2)) {
  107. printf("Incorrect or missing datarate, expected int 1-2");
  108. break;
  109. }
  110. furi_hal_bt_stop_advertising();
  111. printf("Receiving packets at %d channel at %d M datarate\r\n", channel, datarate);
  112. printf("Press CTRL+C to stop\r\n");
  113. furi_hal_bt_start_packet_rx(channel, datarate);
  114. float rssi_raw = 0;
  115. while(!cli_cmd_interrupt_received(cli)) {
  116. osDelay(250);
  117. rssi_raw = furi_hal_bt_get_rssi();
  118. printf("RSSI: %03.1f dB\r", rssi_raw);
  119. fflush(stdout);
  120. }
  121. uint16_t packets_received = furi_hal_bt_stop_packet_test();
  122. printf("Received %hu packets", packets_received);
  123. } while(false);
  124. }
  125. static void bt_cli_scan_callback(GapAddress address, void* context) {
  126. furi_assert(context);
  127. osMessageQueueId_t queue = context;
  128. osMessageQueuePut(queue, &address, 0, 250);
  129. }
  130. static void bt_cli_command_scan(Cli* cli, string_t args, void* context) {
  131. osMessageQueueId_t queue = osMessageQueueNew(20, sizeof(GapAddress), NULL);
  132. furi_hal_bt_start_scan(bt_cli_scan_callback, queue);
  133. GapAddress address = {};
  134. bool exit = false;
  135. while(!exit) {
  136. if(osMessageQueueGet(queue, &address, NULL, 250) == osOK) {
  137. if(address.type < sizeof(bt_cli_address_types)) {
  138. printf("Found new device. Type: %s, MAC: ", bt_cli_address_types[address.type]);
  139. for(uint8_t i = 0; i < sizeof(address.mac) - 1; i++) {
  140. printf("%02X:", address.mac[i]);
  141. }
  142. printf("%02X\r\n", address.mac[sizeof(address.mac) - 1]);
  143. }
  144. }
  145. exit = cli_cmd_interrupt_received(cli);
  146. }
  147. furi_hal_bt_stop_scan();
  148. osMessageQueueDelete(queue);
  149. }
  150. static void bt_cli_print_usage() {
  151. printf("Usage:\r\n");
  152. printf("bt <cmd> <args>\r\n");
  153. printf("Cmd list:\r\n");
  154. printf("\thci_info\t - HCI info\r\n");
  155. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug) &&
  156. furi_hal_bt_get_radio_stack() == FuriHalBtStackHciLayer) {
  157. printf("\ttx_carrier <channel:0-39> <power:0-6>\t - start tx carrier test\r\n");
  158. printf("\trx_carrier <channel:0-39>\t - start rx carrier test\r\n");
  159. printf("\ttx_pt <channel:0-39> <pattern:0-5> <datarate:1-2>\t - start tx packet test\r\n");
  160. printf("\trx_pt <channel:0-39> <datarate:1-2>\t - start rx packer test\r\n");
  161. printf("\tscan\t - start scanner\r\n");
  162. }
  163. }
  164. static void bt_cli(Cli* cli, string_t args, void* context) {
  165. furi_record_open("bt");
  166. string_t cmd;
  167. string_init(cmd);
  168. BtSettings bt_settings;
  169. bt_settings_load(&bt_settings);
  170. do {
  171. if(!args_read_string_and_trim(args, cmd)) {
  172. bt_cli_print_usage();
  173. break;
  174. }
  175. if(string_cmp_str(cmd, "hci_info") == 0) {
  176. bt_cli_command_hci_info(cli, args, NULL);
  177. break;
  178. }
  179. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug) &&
  180. furi_hal_bt_get_radio_stack() == FuriHalBtStackHciLayer) {
  181. if(string_cmp_str(cmd, "carrier_tx") == 0) {
  182. bt_cli_command_carrier_tx(cli, args, NULL);
  183. break;
  184. }
  185. if(string_cmp_str(cmd, "carrier_rx") == 0) {
  186. bt_cli_command_carrier_rx(cli, args, NULL);
  187. break;
  188. }
  189. if(string_cmp_str(cmd, "packet_tx") == 0) {
  190. bt_cli_command_packet_tx(cli, args, NULL);
  191. break;
  192. }
  193. if(string_cmp_str(cmd, "packet_rx") == 0) {
  194. bt_cli_command_packet_rx(cli, args, NULL);
  195. break;
  196. }
  197. if(string_cmp_str(cmd, "scan") == 0) {
  198. bt_cli_command_scan(cli, args, NULL);
  199. break;
  200. }
  201. }
  202. bt_cli_print_usage();
  203. } while(false);
  204. if(bt_settings.enabled) {
  205. furi_hal_bt_start_advertising();
  206. }
  207. string_clear(cmd);
  208. furi_record_close("bt");
  209. }
  210. void bt_on_system_start() {
  211. #ifdef SRV_CLI
  212. Cli* cli = furi_record_open("cli");
  213. cli_add_command(cli, "bt", CliCommandFlagDefault, bt_cli, NULL);
  214. furi_record_close("cli");
  215. #else
  216. UNUSED(bt_cli);
  217. #endif
  218. }