subghz_cli.c 17 KB

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