subghz_cli.c 8.8 KB

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