subghz_cli.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include "subghz_cli.h"
  2. #include <furi.h>
  3. #include <furi-hal.h>
  4. #include <stream_buffer.h>
  5. #include <lib/toolbox/args.h>
  6. #include <lib/subghz/subghz_parser.h>
  7. #include <lib/subghz/subghz_keystore.h>
  8. #include <lib/subghz/protocols/subghz_protocol_common.h>
  9. #include <lib/subghz/protocols/subghz_protocol_princeton.h>
  10. #define SUBGHZ_FREQUENCY_RANGE_STR \
  11. "299999755...348000000 or 386999938...464000000 or 778999847...928000000"
  12. void subghz_cli_command_tx_carrier(Cli* cli, string_t args, void* context) {
  13. uint32_t frequency = 433920000;
  14. if(string_size(args)) {
  15. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  16. if(ret != 1) {
  17. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  18. cli_print_usage("subghz_tx_carrier", "<Frequency in HZ>", string_get_cstr(args));
  19. return;
  20. }
  21. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  22. printf(
  23. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  24. frequency);
  25. return;
  26. }
  27. }
  28. furi_hal_subghz_reset();
  29. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  30. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  31. hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  32. hal_gpio_write(&gpio_cc1101_g0, true);
  33. furi_hal_power_suppress_charge_enter();
  34. if(furi_hal_subghz_tx()) {
  35. printf("Transmitting at frequency %lu Hz\r\n", frequency);
  36. printf("Press CTRL+C to stop\r\n");
  37. while(!cli_cmd_interrupt_received(cli)) {
  38. osDelay(250);
  39. }
  40. } else {
  41. printf("This frequency can only be used for RX in your region\r\n");
  42. }
  43. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  44. furi_hal_subghz_sleep();
  45. furi_hal_power_suppress_charge_exit();
  46. }
  47. void subghz_cli_command_rx_carrier(Cli* cli, string_t args, void* context) {
  48. uint32_t frequency = 433920000;
  49. if(string_size(args)) {
  50. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  51. if(ret != 1) {
  52. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  53. cli_print_usage("subghz_tx_carrier", "<Frequency in HZ>", string_get_cstr(args));
  54. return;
  55. }
  56. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  57. printf(
  58. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  59. frequency);
  60. return;
  61. }
  62. }
  63. furi_hal_subghz_reset();
  64. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  65. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  66. printf("Receiving at frequency %lu Hz\r\n", frequency);
  67. printf("Press CTRL+C to stop\r\n");
  68. furi_hal_power_suppress_charge_enter();
  69. furi_hal_subghz_rx();
  70. while(!cli_cmd_interrupt_received(cli)) {
  71. osDelay(250);
  72. printf("RSSI: %03.1fdbm\r", furi_hal_subghz_get_rssi());
  73. fflush(stdout);
  74. }
  75. furi_hal_power_suppress_charge_exit();
  76. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  77. furi_hal_subghz_sleep();
  78. }
  79. void subghz_cli_command_tx(Cli* cli, string_t args, void* context) {
  80. uint32_t frequency = 433920000;
  81. uint32_t key = 0x0074BADE;
  82. size_t repeat = 10;
  83. if(string_size(args)) {
  84. int ret = sscanf(string_get_cstr(args), "%lx %lu %u", &key, &frequency, &repeat);
  85. if(ret != 3) {
  86. printf(
  87. "sscanf returned %d, key: %lx, frequency: %lu, repeat: %u\r\n",
  88. ret,
  89. key,
  90. frequency,
  91. repeat);
  92. cli_print_usage(
  93. "subghz_rx",
  94. "<3 Byte Key in hex> <Frequency in HZ> <Repeat count>",
  95. string_get_cstr(args));
  96. return;
  97. }
  98. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  99. printf(
  100. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  101. frequency);
  102. return;
  103. }
  104. }
  105. printf(
  106. "Transmitting at %lu, key %lx, repeat %u. Press CTRL+C to stop\r\n",
  107. frequency,
  108. key,
  109. repeat);
  110. SubGhzDecoderPrinceton* protocol = subghz_decoder_princeton_alloc();
  111. protocol->common.code_last_found = key;
  112. protocol->common.code_last_count_bit = 24;
  113. SubGhzProtocolCommonEncoder* encoder = subghz_protocol_encoder_common_alloc();
  114. encoder->repeat = repeat;
  115. subghz_protocol_princeton_send_key(protocol, encoder);
  116. furi_hal_subghz_reset();
  117. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  118. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  119. furi_hal_power_suppress_charge_enter();
  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. furi_hal_power_suppress_charge_exit();
  129. subghz_decoder_princeton_free(protocol);
  130. subghz_protocol_encoder_common_free(encoder);
  131. }
  132. typedef struct {
  133. volatile bool overrun;
  134. StreamBufferHandle_t stream;
  135. size_t packet_count;
  136. } SubGhzCliCommandRx;
  137. static void subghz_cli_command_rx_callback(bool level, uint32_t duration, void* context) {
  138. SubGhzCliCommandRx* instance = context;
  139. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  140. LevelDuration level_duration = level_duration_make(level, duration);
  141. if(instance->overrun) {
  142. instance->overrun = false;
  143. level_duration = level_duration_reset();
  144. }
  145. size_t ret = xStreamBufferSendFromISR(
  146. instance->stream, &level_duration, sizeof(LevelDuration), &xHigherPriorityTaskWoken);
  147. if(sizeof(LevelDuration) != ret) instance->overrun = true;
  148. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  149. }
  150. static void subghz_cli_command_rx_text_callback(string_t text, void* context) {
  151. SubGhzCliCommandRx* instance = context;
  152. instance->packet_count++;
  153. printf("%s", string_get_cstr(text));
  154. }
  155. void subghz_cli_command_rx(Cli* cli, string_t args, void* context) {
  156. uint32_t frequency = 433920000;
  157. if(string_size(args)) {
  158. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  159. if(ret != 1) {
  160. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  161. cli_print_usage("subghz_rx", "<Frequency in HZ>", string_get_cstr(args));
  162. return;
  163. }
  164. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  165. printf(
  166. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  167. frequency);
  168. return;
  169. }
  170. }
  171. // Allocate context and buffers
  172. SubGhzCliCommandRx* instance = furi_alloc(sizeof(SubGhzCliCommandRx));
  173. instance->stream = xStreamBufferCreate(sizeof(LevelDuration) * 1024, sizeof(LevelDuration));
  174. furi_check(instance->stream);
  175. SubGhzParser* parser = subghz_parser_alloc();
  176. subghz_parser_load_keeloq_file(parser, "/ext/subghz/keeloq_mfcodes");
  177. subghz_parser_load_nice_flor_s_file(parser, "/ext/subghz/nice_floor_s_rx");
  178. subghz_parser_enable_dump_text(parser, subghz_cli_command_rx_text_callback, instance);
  179. // Configure radio
  180. furi_hal_subghz_reset();
  181. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  182. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  183. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  184. furi_hal_power_suppress_charge_enter();
  185. // Prepare and start RX
  186. furi_hal_subghz_start_async_rx(subghz_cli_command_rx_callback, instance);
  187. // Wait for packets to arrive
  188. printf("Listening at %lu. Press CTRL+C to stop\r\n", frequency);
  189. LevelDuration level_duration;
  190. while(!cli_cmd_interrupt_received(cli)) {
  191. int ret =
  192. xStreamBufferReceive(instance->stream, &level_duration, sizeof(LevelDuration), 10);
  193. if(ret == sizeof(LevelDuration)) {
  194. if(level_duration_is_reset(level_duration)) {
  195. printf(".");
  196. subghz_parser_reset(parser);
  197. } else {
  198. bool level = level_duration_get_level(level_duration);
  199. uint32_t duration = level_duration_get_duration(level_duration);
  200. subghz_parser_parse(parser, level, duration);
  201. }
  202. }
  203. }
  204. // Shutdown radio
  205. furi_hal_subghz_stop_async_rx();
  206. furi_hal_subghz_sleep();
  207. furi_hal_power_suppress_charge_exit();
  208. printf("\r\nPackets recieved %u\r\n", instance->packet_count);
  209. // Cleanup
  210. subghz_parser_free(parser);
  211. vStreamBufferDelete(instance->stream);
  212. free(instance);
  213. }
  214. void subghz_cli_command_print_usage() {
  215. printf("Usage:\r\n");
  216. printf("subghz_crypto <cmd> <args>\r\n");
  217. printf("Cmd list:\r\n");
  218. printf(
  219. "\tkeeloq <plain_text_file> <encrypted_file> <IV:16 bytes in hex>\t - Encrypt keeloq manufacture keys\r\n");
  220. }
  221. void subghz_cli_command_encrypt_keeloq(Cli* cli, string_t args) {
  222. uint8_t iv[16];
  223. string_t source;
  224. string_t destination;
  225. string_init(source);
  226. string_init(destination);
  227. SubGhzKeystore* keystore = subghz_keystore_alloc();
  228. do {
  229. if(!args_read_string_and_trim(args, source)) {
  230. subghz_cli_command_print_usage();
  231. break;
  232. }
  233. if(!args_read_string_and_trim(args, destination)) {
  234. subghz_cli_command_print_usage();
  235. break;
  236. }
  237. if(!args_read_hex_bytes(args, iv, 16)) {
  238. subghz_cli_command_print_usage();
  239. break;
  240. }
  241. if(!subghz_keystore_load(keystore, string_get_cstr(source))) {
  242. printf("Failed to load Keystore");
  243. break;
  244. }
  245. if(!subghz_keystore_save(keystore, string_get_cstr(destination), iv)) {
  246. printf("Failed to save Keystore");
  247. break;
  248. }
  249. } while(false);
  250. subghz_keystore_free(keystore);
  251. string_clear(destination);
  252. string_clear(source);
  253. }
  254. void subghz_cli_command(Cli* cli, string_t args, void* context) {
  255. string_t cmd;
  256. string_init(cmd);
  257. do {
  258. if(!args_read_string_and_trim(args, cmd)) {
  259. subghz_cli_command_print_usage();
  260. break;
  261. }
  262. if(string_cmp_str(cmd, "encrypt_keeloq") == 0) {
  263. subghz_cli_command_encrypt_keeloq(cli, args);
  264. break;
  265. }
  266. subghz_cli_command_print_usage();
  267. } while(false);
  268. string_clear(cmd);
  269. }
  270. void subghz_cli_init() {
  271. Cli* cli = furi_record_open("cli");
  272. cli_add_command(
  273. cli, "subghz_tx_carrier", CliCommandFlagDefault, subghz_cli_command_tx_carrier, NULL);
  274. cli_add_command(
  275. cli, "subghz_rx_carrier", CliCommandFlagDefault, subghz_cli_command_rx_carrier, NULL);
  276. cli_add_command(cli, "subghz_tx", CliCommandFlagDefault, subghz_cli_command_tx, NULL);
  277. cli_add_command(cli, "subghz_rx", CliCommandFlagDefault, subghz_cli_command_rx, NULL);
  278. cli_add_command(cli, "subghz", CliCommandFlagDefault, subghz_cli_command, NULL);
  279. furi_record_close("cli");
  280. }