subghz_cli.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "subghz_cli.h"
  2. #include <furi.h>
  3. #include <furi-hal.h>
  4. #include <stream_buffer.h>
  5. #include <lib/subghz/subghz_parser.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(FuriHalSubGhzPresetOok650Async);
  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_power_suppress_charge_enter();
  42. if(furi_hal_subghz_tx()) {
  43. printf("Transmitting at frequency %lu Hz\r\n", frequency);
  44. printf("Press CTRL+C to stop\r\n");
  45. while(!cli_cmd_interrupt_received(cli)) {
  46. osDelay(250);
  47. }
  48. } else {
  49. printf("This frequency can only be used for RX in your region\r\n");
  50. }
  51. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  52. furi_hal_subghz_sleep();
  53. furi_hal_power_suppress_charge_exit();
  54. }
  55. void subghz_cli_command_rx_carrier(Cli* cli, string_t args, void* context) {
  56. uint32_t frequency = 433920000;
  57. if(string_size(args)) {
  58. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  59. if(ret != 1) {
  60. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  61. cli_print_usage("subghz_tx_carrier", "<Frequency in HZ>", string_get_cstr(args));
  62. return;
  63. }
  64. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  65. printf(
  66. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  67. frequency);
  68. return;
  69. }
  70. }
  71. furi_hal_subghz_reset();
  72. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  73. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  74. printf("Receiving at frequency %lu Hz\r\n", frequency);
  75. printf("Press CTRL+C to stop\r\n");
  76. furi_hal_power_suppress_charge_enter();
  77. furi_hal_subghz_rx();
  78. while(!cli_cmd_interrupt_received(cli)) {
  79. osDelay(250);
  80. printf("RSSI: %03.1fdbm\r", furi_hal_subghz_get_rssi());
  81. fflush(stdout);
  82. }
  83. furi_hal_power_suppress_charge_exit();
  84. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  85. furi_hal_subghz_sleep();
  86. }
  87. void subghz_cli_command_tx(Cli* cli, string_t args, void* context) {
  88. uint32_t frequency = 433920000;
  89. uint32_t key = 0x0074BADE;
  90. size_t repeat = 10;
  91. if(string_size(args)) {
  92. int ret = sscanf(string_get_cstr(args), "%lx %lu %u", &key, &frequency, &repeat);
  93. if(ret != 3) {
  94. printf(
  95. "sscanf returned %d, key: %lx, frequency: %lu, repeat: %u\r\n",
  96. ret,
  97. key,
  98. frequency,
  99. repeat);
  100. cli_print_usage(
  101. "subghz_rx",
  102. "<3 Byte Key in hex> <Frequency in HZ> <Repeat count>",
  103. string_get_cstr(args));
  104. return;
  105. }
  106. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  107. printf(
  108. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  109. frequency);
  110. return;
  111. }
  112. }
  113. printf(
  114. "Transmitting at %lu, key %lx, repeat %u. Press CTRL+C to stop\r\n",
  115. frequency,
  116. key,
  117. repeat);
  118. SubGhzDecoderPrinceton* protocol = subghz_decoder_princeton_alloc();
  119. protocol->common.code_last_found = key;
  120. protocol->common.code_last_count_bit = 24;
  121. SubGhzProtocolCommonEncoder* encoder = subghz_protocol_encoder_common_alloc();
  122. encoder->repeat = repeat;
  123. subghz_protocol_princeton_send_key(protocol, encoder);
  124. furi_hal_subghz_reset();
  125. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  126. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  127. furi_hal_power_suppress_charge_enter();
  128. furi_hal_subghz_start_async_tx(subghz_protocol_encoder_common_yield, encoder);
  129. while(!(furi_hal_subghz_is_async_tx_complete() || cli_cmd_interrupt_received(cli))) {
  130. printf(".");
  131. fflush(stdout);
  132. osDelay(333);
  133. }
  134. furi_hal_subghz_stop_async_tx();
  135. furi_hal_subghz_sleep();
  136. furi_hal_power_suppress_charge_exit();
  137. subghz_decoder_princeton_free(protocol);
  138. subghz_protocol_encoder_common_free(encoder);
  139. }
  140. typedef struct {
  141. volatile bool overrun;
  142. StreamBufferHandle_t stream;
  143. size_t packet_count;
  144. } SubGhzCliCommandRx;
  145. static void subghz_cli_command_rx_callback(bool level, uint32_t duration, void* context) {
  146. SubGhzCliCommandRx* instance = context;
  147. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  148. LevelDuration level_duration = level_duration_make(level, duration);
  149. if(instance->overrun) {
  150. instance->overrun = false;
  151. level_duration = level_duration_reset();
  152. }
  153. size_t ret = xStreamBufferSendFromISR(
  154. instance->stream, &level_duration, sizeof(LevelDuration), &xHigherPriorityTaskWoken);
  155. if(sizeof(LevelDuration) != ret) instance->overrun = true;
  156. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  157. }
  158. static void subghz_cli_command_rx_text_callback(string_t text, void* context) {
  159. SubGhzCliCommandRx* instance = context;
  160. instance->packet_count++;
  161. printf("%s", string_get_cstr(text));
  162. }
  163. void subghz_cli_command_rx(Cli* cli, string_t args, void* context) {
  164. uint32_t frequency = 433920000;
  165. if(string_size(args)) {
  166. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  167. if(ret != 1) {
  168. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  169. cli_print_usage("subghz_rx", "<Frequency in HZ>", string_get_cstr(args));
  170. return;
  171. }
  172. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  173. printf(
  174. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  175. frequency);
  176. return;
  177. }
  178. }
  179. // Allocate context and buffers
  180. SubGhzCliCommandRx* instance = furi_alloc(sizeof(SubGhzCliCommandRx));
  181. instance->stream = xStreamBufferCreate(sizeof(LevelDuration) * 1024, sizeof(LevelDuration));
  182. furi_check(instance->stream);
  183. SubGhzParser* parser = subghz_parser_alloc();
  184. subghz_parser_load_keeloq_file(parser, "/ext/subghz/keeloq_mfcodes");
  185. subghz_parser_load_nice_flor_s_file(parser, "/ext/subghz/nice_floor_s_rx");
  186. subghz_parser_enable_dump_text(parser, subghz_cli_command_rx_text_callback, instance);
  187. // Configure radio
  188. furi_hal_subghz_reset();
  189. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  190. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  191. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  192. furi_hal_power_suppress_charge_enter();
  193. // Prepare and start RX
  194. furi_hal_subghz_start_async_rx(subghz_cli_command_rx_callback, instance);
  195. // Wait for packets to arrive
  196. printf("Listening at %lu. Press CTRL+C to stop\r\n", frequency);
  197. LevelDuration level_duration;
  198. while(!cli_cmd_interrupt_received(cli)) {
  199. int ret =
  200. xStreamBufferReceive(instance->stream, &level_duration, sizeof(LevelDuration), 10);
  201. if(ret == sizeof(LevelDuration)) {
  202. if(level_duration_is_reset(level_duration)) {
  203. printf(".");
  204. subghz_parser_reset(parser);
  205. } else {
  206. bool level = level_duration_get_level(level_duration);
  207. uint32_t duration = level_duration_get_duration(level_duration);
  208. subghz_parser_parse(parser, level, duration);
  209. }
  210. }
  211. }
  212. // Shutdown radio
  213. furi_hal_subghz_stop_async_rx();
  214. furi_hal_subghz_sleep();
  215. furi_hal_power_suppress_charge_exit();
  216. printf("\r\nPackets recieved %u\r\n", instance->packet_count);
  217. // Cleanup
  218. subghz_parser_free(parser);
  219. vStreamBufferDelete(instance->stream);
  220. free(instance);
  221. }