subghz_cli.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. #include <lib/subghz/subghz_tx_rx_worker.h>
  11. #define SUBGHZ_FREQUENCY_RANGE_STR \
  12. "299999755...348000000 or 386999938...464000000 or 778999847...928000000"
  13. static void subghz_cli_command_tx_carrier(Cli* cli, string_t args, void* context) {
  14. uint32_t frequency = 433920000;
  15. if(string_size(args)) {
  16. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  17. if(ret != 1) {
  18. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  19. cli_print_usage("subghz_tx_carrier", "<Frequency in HZ>", string_get_cstr(args));
  20. return;
  21. }
  22. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  23. printf(
  24. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  25. frequency);
  26. return;
  27. }
  28. }
  29. furi_hal_subghz_reset();
  30. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  31. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  32. hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  33. hal_gpio_write(&gpio_cc1101_g0, true);
  34. furi_hal_power_suppress_charge_enter();
  35. if(furi_hal_subghz_tx()) {
  36. printf("Transmitting at frequency %lu Hz\r\n", frequency);
  37. printf("Press CTRL+C to stop\r\n");
  38. while(!cli_cmd_interrupt_received(cli)) {
  39. osDelay(250);
  40. }
  41. } else {
  42. printf("This frequency can only be used for RX in your region\r\n");
  43. }
  44. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  45. furi_hal_subghz_sleep();
  46. furi_hal_power_suppress_charge_exit();
  47. }
  48. static 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(!furi_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. furi_hal_subghz_reset();
  65. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  66. frequency = furi_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. furi_hal_power_suppress_charge_enter();
  70. furi_hal_subghz_rx();
  71. while(!cli_cmd_interrupt_received(cli)) {
  72. osDelay(250);
  73. printf("RSSI: %03.1fdbm\r", furi_hal_subghz_get_rssi());
  74. fflush(stdout);
  75. }
  76. furi_hal_power_suppress_charge_exit();
  77. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  78. furi_hal_subghz_sleep();
  79. }
  80. static 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. SubGhzProtocolCommonEncoder* 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(FuriHalSubGhzPresetOok650Async);
  119. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  120. furi_hal_power_suppress_charge_enter();
  121. furi_hal_subghz_start_async_tx(subghz_protocol_encoder_common_yield, encoder);
  122. while(!(furi_hal_subghz_is_async_tx_complete() || cli_cmd_interrupt_received(cli))) {
  123. printf(".");
  124. fflush(stdout);
  125. osDelay(333);
  126. }
  127. furi_hal_subghz_stop_async_tx();
  128. furi_hal_subghz_sleep();
  129. furi_hal_power_suppress_charge_exit();
  130. subghz_decoder_princeton_free(protocol);
  131. subghz_protocol_encoder_common_free(encoder);
  132. }
  133. typedef struct {
  134. volatile bool overrun;
  135. StreamBufferHandle_t stream;
  136. size_t packet_count;
  137. } SubGhzCliCommandRx;
  138. static void subghz_cli_command_rx_callback(bool level, uint32_t duration, void* context) {
  139. SubGhzCliCommandRx* instance = context;
  140. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  141. LevelDuration level_duration = level_duration_make(level, duration);
  142. if(instance->overrun) {
  143. instance->overrun = false;
  144. level_duration = level_duration_reset();
  145. }
  146. size_t ret = xStreamBufferSendFromISR(
  147. instance->stream, &level_duration, sizeof(LevelDuration), &xHigherPriorityTaskWoken);
  148. if(sizeof(LevelDuration) != ret) instance->overrun = true;
  149. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  150. }
  151. static void subghz_cli_command_rx_text_callback(string_t text, void* context) {
  152. SubGhzCliCommandRx* instance = context;
  153. instance->packet_count++;
  154. printf("%s", string_get_cstr(text));
  155. }
  156. static void subghz_cli_command_rx(Cli* cli, string_t args, void* context) {
  157. uint32_t frequency = 433920000;
  158. if(string_size(args)) {
  159. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  160. if(ret != 1) {
  161. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  162. cli_print_usage("subghz_rx", "<Frequency in HZ>", string_get_cstr(args));
  163. return;
  164. }
  165. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  166. printf(
  167. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  168. frequency);
  169. return;
  170. }
  171. }
  172. // Allocate context and buffers
  173. SubGhzCliCommandRx* instance = furi_alloc(sizeof(SubGhzCliCommandRx));
  174. instance->stream = xStreamBufferCreate(sizeof(LevelDuration) * 1024, sizeof(LevelDuration));
  175. furi_check(instance->stream);
  176. SubGhzParser* parser = subghz_parser_alloc();
  177. subghz_parser_load_keeloq_file(parser, "/ext/subghz/keeloq_mfcodes");
  178. subghz_parser_load_keeloq_file(parser, "/ext/subghz/keeloq_mfcodes_user");
  179. subghz_parser_load_nice_flor_s_file(parser, "/ext/subghz/nice_flor_s_rx");
  180. subghz_parser_load_came_atomo_file(parser, "/ext/subghz/came_atomo");
  181. subghz_parser_enable_dump_text(parser, subghz_cli_command_rx_text_callback, instance);
  182. // Configure radio
  183. furi_hal_subghz_reset();
  184. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  185. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  186. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  187. furi_hal_power_suppress_charge_enter();
  188. // Prepare and start RX
  189. furi_hal_subghz_start_async_rx(subghz_cli_command_rx_callback, instance);
  190. // Wait for packets to arrive
  191. printf("Listening at %lu. Press CTRL+C to stop\r\n", frequency);
  192. LevelDuration level_duration;
  193. while(!cli_cmd_interrupt_received(cli)) {
  194. int ret =
  195. xStreamBufferReceive(instance->stream, &level_duration, sizeof(LevelDuration), 10);
  196. if(ret == sizeof(LevelDuration)) {
  197. if(level_duration_is_reset(level_duration)) {
  198. printf(".");
  199. subghz_parser_reset(parser);
  200. } else {
  201. bool level = level_duration_get_level(level_duration);
  202. uint32_t duration = level_duration_get_duration(level_duration);
  203. subghz_parser_parse(parser, level, duration);
  204. }
  205. }
  206. }
  207. // Shutdown radio
  208. furi_hal_subghz_stop_async_rx();
  209. furi_hal_subghz_sleep();
  210. furi_hal_power_suppress_charge_exit();
  211. printf("\r\nPackets recieved %u\r\n", instance->packet_count);
  212. // Cleanup
  213. subghz_parser_free(parser);
  214. vStreamBufferDelete(instance->stream);
  215. free(instance);
  216. }
  217. static void subghz_cli_command_print_usage() {
  218. printf("Usage:\r\n");
  219. printf("subghz <cmd> <args>\r\n");
  220. printf("Cmd list:\r\n");
  221. printf(
  222. "\tencrypt_keeloq <path_decrypted_file> <path_encrypted_file> <IV:16 bytes in hex>\t - Encrypt keeloq manufacture keys\r\n");
  223. printf(
  224. "\tencrypt_raw <path_decrypted_file> <path_encrypted_file> <IV:16 bytes in hex>\t - Encrypt RAW data\r\n");
  225. printf("\tchat <frequency:in Herz>\t - Chat with other Flippers\r\n");
  226. }
  227. static void subghz_cli_command_encrypt_keeloq(Cli* cli, string_t args) {
  228. uint8_t iv[16];
  229. string_t source;
  230. string_t destination;
  231. string_init(source);
  232. string_init(destination);
  233. SubGhzKeystore* keystore = subghz_keystore_alloc();
  234. do {
  235. if(!args_read_string_and_trim(args, source)) {
  236. subghz_cli_command_print_usage();
  237. break;
  238. }
  239. if(!args_read_string_and_trim(args, destination)) {
  240. subghz_cli_command_print_usage();
  241. break;
  242. }
  243. if(!args_read_hex_bytes(args, iv, 16)) {
  244. subghz_cli_command_print_usage();
  245. break;
  246. }
  247. if(!subghz_keystore_load(keystore, string_get_cstr(source))) {
  248. printf("Failed to load Keystore");
  249. break;
  250. }
  251. if(!subghz_keystore_save(keystore, string_get_cstr(destination), iv)) {
  252. printf("Failed to save Keystore");
  253. break;
  254. }
  255. } while(false);
  256. subghz_keystore_free(keystore);
  257. string_clear(destination);
  258. string_clear(source);
  259. }
  260. static void subghz_cli_command_encrypt_raw(Cli* cli, string_t args) {
  261. uint8_t iv[16];
  262. string_t source;
  263. string_t destination;
  264. string_init(source);
  265. string_init(destination);
  266. do {
  267. if(!args_read_string_and_trim(args, source)) {
  268. subghz_cli_command_print_usage();
  269. break;
  270. }
  271. if(!args_read_string_and_trim(args, destination)) {
  272. subghz_cli_command_print_usage();
  273. break;
  274. }
  275. if(!args_read_hex_bytes(args, iv, 16)) {
  276. subghz_cli_command_print_usage();
  277. break;
  278. }
  279. if(!subghz_keystore_raw_encrypted_save(
  280. string_get_cstr(source), string_get_cstr(destination), iv)) {
  281. printf("Failed to save Keystore");
  282. break;
  283. }
  284. } while(false);
  285. string_clear(destination);
  286. string_clear(source);
  287. }
  288. static void subghz_cli_command_chat(Cli* cli, string_t args) {
  289. uint32_t frequency = 433920000;
  290. if(string_size(args)) {
  291. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  292. if(ret != 1) {
  293. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  294. cli_print_usage("subghz_txrx", "<Frequency in HZ>", string_get_cstr(args));
  295. return;
  296. }
  297. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  298. printf(
  299. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  300. frequency);
  301. return;
  302. }
  303. }
  304. if(!furi_hal_subghz_is_tx_allowed(frequency)) {
  305. printf(
  306. "In your region, only reception on this frequency (%lu) is allowed,\r\n"
  307. "the actual operation of the application is not possible\r\n ",
  308. frequency);
  309. return;
  310. }
  311. SubGhzTxRxWorker* subghz_txrx = subghz_tx_rx_worker_alloc();
  312. subghz_tx_rx_worker_start(subghz_txrx, frequency);
  313. printf("Receiving at frequency %lu Hz\r\n", frequency);
  314. printf("Press CTRL+C to stop\r\n");
  315. furi_hal_power_suppress_charge_enter();
  316. size_t message_max_len = 64;
  317. uint8_t message[64] = {0};
  318. string_t input;
  319. string_init(input);
  320. string_t name;
  321. string_init(name);
  322. char c;
  323. bool exit = false;
  324. string_printf(name, "\033[0;33m%s\033[0m: ", furi_hal_version_get_name_ptr());
  325. string_set(input, name);
  326. printf("%s", string_get_cstr(input));
  327. fflush(stdout);
  328. while(!exit) {
  329. if(furi_hal_vcp_rx_with_timeout((uint8_t*)&c, 1, 0) == 1) {
  330. if(c == CliSymbolAsciiETX) {
  331. printf("\r\n");
  332. exit = true;
  333. break;
  334. } else if((c >= 0x20 && c < 0x7F) || (c >= 0x80 && c < 0xF0)) {
  335. putc(c, stdout);
  336. fflush(stdout);
  337. string_push_back(input, c);
  338. } else if(c == CliSymbolAsciiBackspace) {
  339. size_t len = string_size(input);
  340. if(len > string_size(name)) {
  341. string_set_strn(input, string_get_cstr(input), len - 1);
  342. printf("\r");
  343. for(uint8_t i = 0; i < len; i++) {
  344. printf(" ");
  345. }
  346. printf("\r%s", string_get_cstr(input));
  347. fflush(stdout);
  348. }
  349. } else if(c == CliSymbolAsciiCR) {
  350. printf("\r\n");
  351. subghz_tx_rx_worker_write(
  352. subghz_txrx, (uint8_t*)string_get_cstr(input), strlen(string_get_cstr(input)));
  353. string_printf(input, "%s", string_get_cstr(name));
  354. printf("%s", string_get_cstr(input));
  355. fflush(stdout);
  356. }
  357. }
  358. if(subghz_tx_rx_worker_available(subghz_txrx)) {
  359. memset(message, 0x00, message_max_len);
  360. subghz_tx_rx_worker_read(subghz_txrx, message, message_max_len);
  361. printf("\r");
  362. for(uint8_t i = 0; i < 80; i++) {
  363. printf(" ");
  364. }
  365. printf("\r %s\r\n", message);
  366. printf("%s", string_get_cstr(input));
  367. fflush(stdout);
  368. }
  369. }
  370. printf("\r\nExit chat\r\n");
  371. string_clear(input);
  372. string_clear(name);
  373. furi_hal_power_suppress_charge_exit();
  374. if(subghz_tx_rx_worker_is_running(subghz_txrx)) {
  375. subghz_tx_rx_worker_stop(subghz_txrx);
  376. subghz_tx_rx_worker_free(subghz_txrx);
  377. }
  378. }
  379. static void subghz_cli_command(Cli* cli, string_t args, void* context) {
  380. string_t cmd;
  381. string_init(cmd);
  382. do {
  383. if(!args_read_string_and_trim(args, cmd)) {
  384. subghz_cli_command_print_usage();
  385. break;
  386. }
  387. if(string_cmp_str(cmd, "encrypt_keeloq") == 0) {
  388. subghz_cli_command_encrypt_keeloq(cli, args);
  389. break;
  390. }
  391. if(string_cmp_str(cmd, "encrypt_raw") == 0) {
  392. subghz_cli_command_encrypt_raw(cli, args);
  393. break;
  394. }
  395. if(string_cmp_str(cmd, "chat") == 0) {
  396. subghz_cli_command_chat(cli, args);
  397. break;
  398. }
  399. subghz_cli_command_print_usage();
  400. } while(false);
  401. string_clear(cmd);
  402. }
  403. void subghz_cli_init() {
  404. Cli* cli = furi_record_open("cli");
  405. cli_add_command(
  406. cli, "subghz_tx_carrier", CliCommandFlagDefault, subghz_cli_command_tx_carrier, NULL);
  407. cli_add_command(
  408. cli, "subghz_rx_carrier", CliCommandFlagDefault, subghz_cli_command_rx_carrier, NULL);
  409. cli_add_command(cli, "subghz_tx", CliCommandFlagDefault, subghz_cli_command_tx, NULL);
  410. cli_add_command(cli, "subghz_rx", CliCommandFlagDefault, subghz_cli_command_rx, NULL);
  411. cli_add_command(cli, "subghz", CliCommandFlagDefault, subghz_cli_command, NULL);
  412. furi_record_close("cli");
  413. }