bt_cli.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "bt_cli.h"
  2. #include <furi.h>
  3. #include <api-hal.h>
  4. void bt_cli_init() {
  5. Cli* cli = furi_record_open("cli");
  6. cli_add_command(cli, "bt_info", bt_cli_command_info, NULL);
  7. cli_add_command(cli, "bt_tx_carrier", bt_cli_command_carrier_tx, NULL);
  8. cli_add_command(cli, "bt_rx_carrier", bt_cli_command_carrier_rx, NULL);
  9. cli_add_command(cli, "bt_tx_pt", bt_cli_command_packet_tx, NULL);
  10. cli_add_command(cli, "bt_rx_pt", bt_cli_command_packet_rx, NULL);
  11. furi_record_close("cli");
  12. }
  13. void bt_cli_command_info(Cli* cli, string_t args, void* context) {
  14. string_t buffer;
  15. string_init(buffer);
  16. api_hal_bt_dump_state(buffer);
  17. printf(string_get_cstr(buffer));
  18. string_clear(buffer);
  19. }
  20. void bt_cli_command_carrier_tx(Cli* cli, string_t args, void* context) {
  21. uint16_t channel;
  22. uint16_t power;
  23. int ret = sscanf(string_get_cstr(args), "%hu %hu", &channel, &power);
  24. if(ret != 2) {
  25. printf("sscanf returned %d, channel: %hu, power: %hu\r\n", ret, channel, power);
  26. cli_print_usage("bt_tx_carrier", "<Channel number> <Power>", string_get_cstr(args));
  27. return;
  28. }
  29. if(channel > 39) {
  30. printf("Channel number must be in 0...39 range, not %hu\r\n", channel);
  31. return;
  32. }
  33. if(power > 6) {
  34. printf("Power must be in 0...6 dB range, not %hu\r\n", power);
  35. return;
  36. }
  37. printf("Transmitting carrier at %hu channel at %hu dB power\r\n", channel, power);
  38. printf("Press CTRL+C to stop\r\n");
  39. api_hal_bt_start_tone_tx(channel, 0x19 + power);
  40. while(!cli_cmd_interrupt_received(cli)) {
  41. osDelay(250);
  42. }
  43. api_hal_bt_stop_tone_tx();
  44. }
  45. void bt_cli_command_carrier_rx(Cli* cli, string_t args, void* context) {
  46. uint16_t channel;
  47. int ret = sscanf(string_get_cstr(args), "%hu", &channel);
  48. if(ret != 1) {
  49. printf("sscanf returned %d, channel: %hu\r\n", ret, channel);
  50. cli_print_usage("bt_rx_carrier", "<Channel number>", string_get_cstr(args));
  51. return;
  52. }
  53. if(channel > 39) {
  54. printf("Channel number must be in 0...39 range, not %hu\r\n", channel);
  55. return;
  56. }
  57. printf("Receiving carrier at %hu channel\r\n", channel);
  58. printf("Press CTRL+C to stop\r\n");
  59. api_hal_bt_start_packet_rx(channel, 1);
  60. float rssi_raw = 0;
  61. while(!cli_cmd_interrupt_received(cli)) {
  62. osDelay(250);
  63. rssi_raw = api_hal_bt_get_rssi();
  64. printf("RSSI: %03.1f dB\r", rssi_raw);
  65. fflush(stdout);
  66. }
  67. api_hal_bt_stop_packet_test();
  68. }
  69. void bt_cli_command_packet_tx(Cli* cli, string_t args, void* context) {
  70. uint16_t channel;
  71. uint16_t pattern;
  72. uint16_t datarate;
  73. int ret = sscanf(string_get_cstr(args), "%hu %hu %hu", &channel, &pattern, &datarate);
  74. if(ret != 3) {
  75. printf("sscanf returned %d, channel: %hu %hu %hu\r\n", ret, channel, pattern, datarate);
  76. cli_print_usage(
  77. "bt_tx_pt", "<Channel number> <Pattern> <Datarate>", string_get_cstr(args));
  78. return;
  79. }
  80. if(channel > 39) {
  81. printf("Channel number must be in 0...39 range, not %hu\r\n", channel);
  82. return;
  83. }
  84. if(pattern > 5) {
  85. printf("Pattern must be in 0...5 range, not %hu\r\n", pattern);
  86. printf("0 - Pseudo-Random bit sequence 9\r\n");
  87. printf("1 - Pattern of alternating bits '11110000'\r\n");
  88. printf("2 - Pattern of alternating bits '10101010'\r\n");
  89. printf("3 - Pseudo-Random bit sequence 15\r\n");
  90. printf("4 - Pattern of All '1' bits\r\n");
  91. printf("5 - Pattern of All '0' bits\r\n");
  92. return;
  93. }
  94. if(datarate < 1 || datarate > 2) {
  95. printf("Datarate must be in 1 or 2 Mb, not %hu\r\n", datarate);
  96. return;
  97. }
  98. printf(
  99. "Transmitting %hu pattern packet at %hu channel at %hu M datarate\r\n",
  100. pattern,
  101. channel,
  102. datarate);
  103. printf("Press CTRL+C to stop\r\n");
  104. api_hal_bt_start_packet_tx(channel, pattern, datarate);
  105. while(!cli_cmd_interrupt_received(cli)) {
  106. osDelay(250);
  107. }
  108. api_hal_bt_stop_packet_test();
  109. printf("Transmitted %lu packets", api_hal_bt_get_transmitted_packets());
  110. }
  111. void bt_cli_command_packet_rx(Cli* cli, string_t args, void* context) {
  112. uint16_t channel;
  113. uint16_t datarate;
  114. int ret = sscanf(string_get_cstr(args), "%hu %hu", &channel, &datarate);
  115. if(ret != 2) {
  116. printf("sscanf returned %d, channel: %hu datarate: %hu\r\n", ret, channel, datarate);
  117. cli_print_usage("bt_rx_pt", "<Channel number> <Datarate>", string_get_cstr(args));
  118. return;
  119. }
  120. if(channel > 39) {
  121. printf("Channel number must be in 0...39 range, not %hu\r\n", channel);
  122. return;
  123. }
  124. if(datarate < 1 || datarate > 2) {
  125. printf("Datarate must be in 1 or 2 Mb, not %hu\r\n", datarate);
  126. return;
  127. }
  128. printf("Receiving packets at %hu channel at %hu M datarate\r\n", channel, datarate);
  129. printf("Press CTRL+C to stop\r\n");
  130. api_hal_bt_start_packet_rx(channel, datarate);
  131. float rssi_raw = 0;
  132. while(!cli_cmd_interrupt_received(cli)) {
  133. osDelay(250);
  134. rssi_raw = api_hal_bt_get_rssi();
  135. printf("RSSI: %03.1f dB\r", rssi_raw);
  136. fflush(stdout);
  137. }
  138. uint16_t packets_received = api_hal_bt_stop_packet_test();
  139. printf("Received %hu packets", packets_received);
  140. }