subghz_cli.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "subghz_cli.h"
  2. #include <furi.h>
  3. #include <furi-hal.h>
  4. #include <stream_buffer.h>
  5. #include <lib/subghz/protocols/subghz_protocol.h>
  6. #include <lib/subghz/protocols/subghz_protocol_common.h>
  7. #include <lib/subghz/protocols/subghz_protocol_princeton.h>
  8. #define SUBGHZ_FREQUENCY_RANGE_STR \
  9. "299999755...348000000 or 386999938...464000000 or 778999847...928000000"
  10. void subghz_cli_init() {
  11. Cli* cli = furi_record_open("cli");
  12. cli_add_command(
  13. cli, "subghz_tx_carrier", CliCommandFlagDefault, subghz_cli_command_tx_carrier, NULL);
  14. cli_add_command(
  15. cli, "subghz_rx_carrier", CliCommandFlagDefault, subghz_cli_command_rx_carrier, NULL);
  16. cli_add_command(cli, "subghz_tx", CliCommandFlagDefault, subghz_cli_command_tx, NULL);
  17. cli_add_command(cli, "subghz_rx", CliCommandFlagDefault, subghz_cli_command_rx, NULL);
  18. furi_record_close("cli");
  19. }
  20. void subghz_cli_command_tx_carrier(Cli* cli, string_t args, void* context) {
  21. uint32_t frequency = 433920000;
  22. if(string_size(args)) {
  23. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  24. if(ret != 1) {
  25. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  26. cli_print_usage("subghz_tx_carrier", "<Frequency in HZ>", string_get_cstr(args));
  27. return;
  28. }
  29. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  30. printf(
  31. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  32. frequency);
  33. return;
  34. }
  35. }
  36. furi_hal_subghz_reset();
  37. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOokAsync);
  38. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  39. hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  40. hal_gpio_write(&gpio_cc1101_g0, true);
  41. furi_hal_subghz_tx();
  42. printf("Transmitting at frequency %lu Hz\r\n", frequency);
  43. printf("Press CTRL+C to stop\r\n");
  44. while(!cli_cmd_interrupt_received(cli)) {
  45. osDelay(250);
  46. }
  47. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  48. furi_hal_subghz_sleep();
  49. }
  50. void subghz_cli_command_rx_carrier(Cli* cli, string_t args, void* context) {
  51. uint32_t frequency = 433920000;
  52. if(string_size(args)) {
  53. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  54. if(ret != 1) {
  55. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  56. cli_print_usage("subghz_tx_carrier", "<Frequency in HZ>", string_get_cstr(args));
  57. return;
  58. }
  59. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  60. printf(
  61. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  62. frequency);
  63. return;
  64. }
  65. }
  66. furi_hal_subghz_reset();
  67. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOokAsync);
  68. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  69. printf("Receiving at frequency %lu Hz\r\n", frequency);
  70. printf("Press CTRL+C to stop\r\n");
  71. furi_hal_subghz_rx();
  72. while(!cli_cmd_interrupt_received(cli)) {
  73. osDelay(250);
  74. printf("RSSI: %03.1fdbm\r", furi_hal_subghz_get_rssi());
  75. fflush(stdout);
  76. }
  77. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  78. furi_hal_subghz_sleep();
  79. }
  80. void subghz_cli_command_tx(Cli* cli, string_t args, void* context) {
  81. uint32_t frequency = 433920000;
  82. uint32_t key = 0x0074BADE;
  83. size_t repeat = 10;
  84. if(string_size(args)) {
  85. int ret = sscanf(string_get_cstr(args), "%lx %lu %u", &key, &frequency, &repeat);
  86. if(ret != 3) {
  87. printf(
  88. "sscanf returned %d, key: %lx, frequency: %lu, repeat: %u\r\n",
  89. ret,
  90. key,
  91. frequency,
  92. repeat);
  93. cli_print_usage(
  94. "subghz_rx",
  95. "<3 Byte Key in hex> <Frequency in HZ> <Repeat count>",
  96. string_get_cstr(args));
  97. return;
  98. }
  99. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  100. printf(
  101. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  102. frequency);
  103. return;
  104. }
  105. }
  106. printf(
  107. "Transmitting at %lu, key %lx, repeat %u. Press CTRL+C to stop\r\n",
  108. frequency,
  109. key,
  110. repeat);
  111. SubGhzDecoderPrinceton* protocol = subghz_decoder_princeton_alloc();
  112. protocol->common.code_last_found = key;
  113. protocol->common.code_last_count_bit = 24;
  114. SubGhzProtocolEncoderCommon* encoder = subghz_protocol_encoder_common_alloc();
  115. encoder->repeat = repeat;
  116. subghz_protocol_princeton_send_key(protocol, encoder);
  117. furi_hal_subghz_reset();
  118. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOokAsync);
  119. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  120. furi_hal_subghz_start_async_tx(subghz_protocol_encoder_common_yield, encoder);
  121. while(!(furi_hal_subghz_is_async_tx_complete() || cli_cmd_interrupt_received(cli))) {
  122. printf(".");
  123. fflush(stdout);
  124. osDelay(333);
  125. }
  126. furi_hal_subghz_stop_async_tx();
  127. furi_hal_subghz_sleep();
  128. subghz_decoder_princeton_free(protocol);
  129. subghz_protocol_encoder_common_free(encoder);
  130. }
  131. typedef struct {
  132. volatile bool overrun;
  133. StreamBufferHandle_t stream;
  134. size_t packet_count;
  135. } SubGhzCliCommandRx;
  136. static void subghz_cli_command_rx_callback(bool level, uint32_t duration, void* context) {
  137. SubGhzCliCommandRx* instance = context;
  138. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  139. LevelDuration level_duration = level_duration_make(level, duration);
  140. if(instance->overrun) {
  141. instance->overrun = false;
  142. level_duration = level_duration_reset();
  143. }
  144. size_t ret = xStreamBufferSendFromISR(
  145. instance->stream, &level_duration, sizeof(LevelDuration), &xHigherPriorityTaskWoken);
  146. if(sizeof(LevelDuration) != ret) instance->overrun = true;
  147. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  148. }
  149. static void subghz_cli_command_rx_text_callback(string_t text, void* context) {
  150. SubGhzCliCommandRx* instance = context;
  151. instance->packet_count++;
  152. printf(string_get_cstr(text));
  153. }
  154. void subghz_cli_command_rx(Cli* cli, string_t args, void* context) {
  155. uint32_t frequency = 433920000;
  156. if(string_size(args)) {
  157. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  158. if(ret != 1) {
  159. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  160. cli_print_usage("subghz_rx", "<Frequency in HZ>", string_get_cstr(args));
  161. return;
  162. }
  163. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  164. printf(
  165. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  166. frequency);
  167. return;
  168. }
  169. }
  170. // Allocate context and buffers
  171. SubGhzCliCommandRx* instance = furi_alloc(sizeof(SubGhzCliCommandRx));
  172. instance->stream = xStreamBufferCreate(sizeof(LevelDuration) * 1024, sizeof(LevelDuration));
  173. furi_check(instance->stream);
  174. SubGhzProtocol* protocol = subghz_protocol_alloc();
  175. subghz_protocol_load_keeloq_file(protocol, "/ext/assets/subghz/keeloq_mfcodes");
  176. subghz_protocol_load_nice_flor_s_file(protocol, "/ext/assets/subghz/nice_floor_s_rx");
  177. subghz_protocol_enable_dump_text(protocol, subghz_cli_command_rx_text_callback, instance);
  178. // Configure radio
  179. furi_hal_subghz_reset();
  180. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOokAsync);
  181. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  182. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  183. // Prepare and start RX
  184. furi_hal_subghz_start_async_rx(subghz_cli_command_rx_callback, instance);
  185. // Wait for packets to arrive
  186. printf("Listening at %lu. Press CTRL+C to stop\r\n", frequency);
  187. LevelDuration level_duration;
  188. while(!cli_cmd_interrupt_received(cli)) {
  189. int ret =
  190. xStreamBufferReceive(instance->stream, &level_duration, sizeof(LevelDuration), 10);
  191. if(ret == sizeof(LevelDuration)) {
  192. if(level_duration_is_reset(level_duration)) {
  193. printf(".");
  194. subghz_protocol_reset(protocol);
  195. } else {
  196. bool level = level_duration_get_level(level_duration);
  197. uint32_t duration = level_duration_get_duration(level_duration);
  198. subghz_protocol_parse(protocol, level, duration);
  199. }
  200. }
  201. }
  202. // Shutdown radio
  203. furi_hal_subghz_stop_async_rx();
  204. furi_hal_subghz_sleep();
  205. printf("\r\nPackets recieved %u\r\n", instance->packet_count);
  206. // Cleanup
  207. subghz_protocol_free(protocol);
  208. vStreamBufferDelete(instance->stream);
  209. free(instance);
  210. }