subghz_cli.c 9.1 KB

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