subghz_cli.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 "helpers/subghz_chat.h"
  11. #include <notification/notification-messages.h>
  12. #define SUBGHZ_FREQUENCY_RANGE_STR \
  13. "299999755...348000000 or 386999938...464000000 or 778999847...928000000"
  14. 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. 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. 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. 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. SubGhzChatWorker* subghz_chat = subghz_chat_worker_alloc();
  313. if(!subghz_chat_worker_start(subghz_chat, frequency)) {
  314. printf("Startup error SubGhzChatWorker\r\n");
  315. if(subghz_chat_worker_is_running(subghz_chat)) {
  316. subghz_chat_worker_stop(subghz_chat);
  317. subghz_chat_worker_free(subghz_chat);
  318. }
  319. return;
  320. }
  321. printf("Receiving at frequency %lu Hz\r\n", frequency);
  322. printf("Press CTRL+C to stop\r\n");
  323. furi_hal_power_suppress_charge_enter();
  324. size_t message_max_len = 64;
  325. uint8_t message[64] = {0};
  326. string_t input;
  327. string_init(input);
  328. string_t name;
  329. string_init(name);
  330. string_t output;
  331. string_init(output);
  332. string_t sysmsg;
  333. string_init(sysmsg);
  334. bool exit = false;
  335. SubghzChatEvent chat_event;
  336. NotificationApp* notification = furi_record_open("notification");
  337. string_printf(name, "\033[0;33m%s\033[0m: ", furi_hal_version_get_name_ptr());
  338. string_set(input, name);
  339. printf("%s", string_get_cstr(input));
  340. fflush(stdout);
  341. while(!exit) {
  342. chat_event = subghz_chat_worker_get_event_chat(subghz_chat);
  343. switch(chat_event.event) {
  344. case SubghzChatEventInputData:
  345. if(chat_event.c == CliSymbolAsciiETX) {
  346. printf("\r\n");
  347. chat_event.event = SubghzChatEventUserExit;
  348. subghz_chat_worker_put_event_chat(subghz_chat, &chat_event);
  349. break;
  350. } else if(
  351. (chat_event.c == CliSymbolAsciiBackspace) || (chat_event.c == CliSymbolAsciiDel)) {
  352. size_t len = string_length_u(input);
  353. if(len > string_length_u(name)) {
  354. printf("%s", "\e[D\e[1P");
  355. fflush(stdout);
  356. //delete 1 char UTF
  357. const char* str = string_get_cstr(input);
  358. size_t size = 0;
  359. m_str1ng_utf8_state_e s = M_STRING_UTF8_STARTING;
  360. string_unicode_t u = 0;
  361. string_reset(sysmsg);
  362. while(*str) {
  363. m_str1ng_utf8_decode(*str, &s, &u);
  364. if((s == M_STRING_UTF8_ERROR) || s == M_STRING_UTF8_STARTING) {
  365. string_push_u(sysmsg, u);
  366. if(++size >= len - 1) break;
  367. s = M_STRING_UTF8_STARTING;
  368. }
  369. str++;
  370. }
  371. string_set(input, sysmsg);
  372. }
  373. } else if(chat_event.c == CliSymbolAsciiCR) {
  374. printf("\r\n");
  375. string_push_back(input, '\r');
  376. string_push_back(input, '\n');
  377. while(!subghz_chat_worker_write(
  378. subghz_chat,
  379. (uint8_t*)string_get_cstr(input),
  380. strlen(string_get_cstr(input)))) {
  381. delay(10);
  382. }
  383. string_printf(input, "%s", string_get_cstr(name));
  384. printf("%s", string_get_cstr(input));
  385. fflush(stdout);
  386. } else if(chat_event.c == CliSymbolAsciiLF) {
  387. //cut out the symbol \n
  388. } else {
  389. putc(chat_event.c, stdout);
  390. fflush(stdout);
  391. string_push_back(input, chat_event.c);
  392. break;
  393. case SubghzChatEventRXData:
  394. do {
  395. memset(message, 0x00, message_max_len);
  396. size_t len = subghz_chat_worker_read(subghz_chat, message, message_max_len);
  397. for(size_t i = 0; i < len; i++) {
  398. string_push_back(output, message[i]);
  399. if(message[i] == '\n') {
  400. printf("\r");
  401. for(uint8_t i = 0; i < 80; i++) {
  402. printf(" ");
  403. }
  404. printf("\r %s", string_get_cstr(output));
  405. printf("%s", string_get_cstr(input));
  406. fflush(stdout);
  407. string_reset(output);
  408. }
  409. }
  410. } while(subghz_chat_worker_available(subghz_chat));
  411. break;
  412. case SubghzChatEventNewMessage:
  413. notification_message(notification, &sequence_single_vibro);
  414. break;
  415. case SubghzChatEventUserEntrance:
  416. string_printf(
  417. sysmsg,
  418. "\033[0;34m%s joined chat.\033[0m\r\n",
  419. furi_hal_version_get_name_ptr());
  420. subghz_chat_worker_write(
  421. subghz_chat,
  422. (uint8_t*)string_get_cstr(sysmsg),
  423. strlen(string_get_cstr(sysmsg)));
  424. break;
  425. case SubghzChatEventUserExit:
  426. string_printf(
  427. sysmsg, "\033[0;31m%s left chat.\033[0m\r\n", furi_hal_version_get_name_ptr());
  428. subghz_chat_worker_write(
  429. subghz_chat,
  430. (uint8_t*)string_get_cstr(sysmsg),
  431. strlen(string_get_cstr(sysmsg)));
  432. delay(10);
  433. exit = true;
  434. break;
  435. default:
  436. FURI_LOG_W("SubGhzChat", "Error event");
  437. break;
  438. }
  439. }
  440. }
  441. string_clear(input);
  442. string_clear(name);
  443. string_clear(output);
  444. string_clear(sysmsg);
  445. furi_hal_power_suppress_charge_exit();
  446. furi_record_close("notification");
  447. if(subghz_chat_worker_is_running(subghz_chat)) {
  448. subghz_chat_worker_stop(subghz_chat);
  449. subghz_chat_worker_free(subghz_chat);
  450. }
  451. printf("\r\nExit chat\r\n");
  452. }
  453. void subghz_cli_command(Cli* cli, string_t args, void* context) {
  454. string_t cmd;
  455. string_init(cmd);
  456. do {
  457. if(!args_read_string_and_trim(args, cmd)) {
  458. subghz_cli_command_print_usage();
  459. break;
  460. }
  461. if(string_cmp_str(cmd, "encrypt_keeloq") == 0) {
  462. subghz_cli_command_encrypt_keeloq(cli, args);
  463. break;
  464. }
  465. if(string_cmp_str(cmd, "encrypt_raw") == 0) {
  466. subghz_cli_command_encrypt_raw(cli, args);
  467. break;
  468. }
  469. if(string_cmp_str(cmd, "chat") == 0) {
  470. subghz_cli_command_chat(cli, args);
  471. break;
  472. }
  473. subghz_cli_command_print_usage();
  474. } while(false);
  475. string_clear(cmd);
  476. }
  477. void subghz_on_system_start() {
  478. #ifdef SRV_CLI
  479. Cli* cli = furi_record_open("cli");
  480. cli_add_command(
  481. cli, "subghz_tx_carrier", CliCommandFlagDefault, subghz_cli_command_tx_carrier, NULL);
  482. cli_add_command(
  483. cli, "subghz_rx_carrier", CliCommandFlagDefault, subghz_cli_command_rx_carrier, NULL);
  484. cli_add_command(cli, "subghz_tx", CliCommandFlagDefault, subghz_cli_command_tx, NULL);
  485. cli_add_command(cli, "subghz_rx", CliCommandFlagDefault, subghz_cli_command_rx, NULL);
  486. cli_add_command(cli, "subghz", CliCommandFlagDefault, subghz_cli_command, NULL);
  487. furi_record_close("cli");
  488. #endif
  489. }