subghz_cli.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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_keystore.h>
  7. #include <lib/subghz/receiver.h>
  8. #include <lib/subghz/transmitter.h>
  9. #include <lib/subghz/subghz_file_encoder_worker.h>
  10. #include "helpers/subghz_chat.h"
  11. #include <notification/notification_messages.h>
  12. #include <flipper_format/flipper_format_i.h>
  13. #define SUBGHZ_FREQUENCY_RANGE_STR \
  14. "299999755...348000000 or 386999938...464000000 or 778999847...928000000"
  15. void subghz_cli_command_tx_carrier(Cli* cli, string_t args, void* context) {
  16. UNUSED(context);
  17. uint32_t frequency = 433920000;
  18. if(string_size(args)) {
  19. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  20. if(ret != 1) {
  21. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  22. cli_print_usage("subghz tx_carrier", "<Frequency: in Hz>", string_get_cstr(args));
  23. return;
  24. }
  25. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  26. printf(
  27. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  28. frequency);
  29. return;
  30. }
  31. }
  32. furi_hal_subghz_reset();
  33. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  34. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  35. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  36. furi_hal_gpio_write(&gpio_cc1101_g0, true);
  37. furi_hal_power_suppress_charge_enter();
  38. if(furi_hal_subghz_tx()) {
  39. printf("Transmitting at frequency %lu Hz\r\n", frequency);
  40. printf("Press CTRL+C to stop\r\n");
  41. while(!cli_cmd_interrupt_received(cli)) {
  42. osDelay(250);
  43. }
  44. } else {
  45. printf("This frequency can only be used for RX in your region\r\n");
  46. }
  47. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  48. furi_hal_subghz_sleep();
  49. furi_hal_power_suppress_charge_exit();
  50. }
  51. void subghz_cli_command_rx_carrier(Cli* cli, string_t args, void* context) {
  52. UNUSED(context);
  53. uint32_t frequency = 433920000;
  54. if(string_size(args)) {
  55. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  56. if(ret != 1) {
  57. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  58. cli_print_usage("subghz rx_carrier", "<Frequency: in Hz>", string_get_cstr(args));
  59. return;
  60. }
  61. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  62. printf(
  63. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  64. frequency);
  65. return;
  66. }
  67. }
  68. furi_hal_subghz_reset();
  69. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  70. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  71. printf("Receiving at frequency %lu Hz\r\n", frequency);
  72. printf("Press CTRL+C to stop\r\n");
  73. furi_hal_power_suppress_charge_enter();
  74. furi_hal_subghz_rx();
  75. while(!cli_cmd_interrupt_received(cli)) {
  76. osDelay(250);
  77. printf("RSSI: %03.1fdbm\r", (double)furi_hal_subghz_get_rssi());
  78. fflush(stdout);
  79. }
  80. furi_hal_power_suppress_charge_exit();
  81. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  82. furi_hal_subghz_sleep();
  83. }
  84. void subghz_cli_command_tx(Cli* cli, string_t args, void* context) {
  85. UNUSED(context);
  86. uint32_t frequency = 433920000;
  87. uint32_t key = 0x0074BADE;
  88. uint32_t repeat = 10;
  89. if(string_size(args)) {
  90. int ret = sscanf(string_get_cstr(args), "%lx %lu %lu", &key, &frequency, &repeat);
  91. if(ret != 3) {
  92. printf(
  93. "sscanf returned %d, key: %lx, frequency: %lu, repeat: %lu\r\n",
  94. ret,
  95. key,
  96. frequency,
  97. repeat);
  98. cli_print_usage(
  99. "subghz tx",
  100. "<3 Byte Key: in hex> <Frequency: in Hz> <Repeat count>",
  101. string_get_cstr(args));
  102. return;
  103. }
  104. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  105. printf(
  106. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  107. frequency);
  108. return;
  109. }
  110. }
  111. printf(
  112. "Transmitting at %lu, key %lx, repeat %lu. Press CTRL+C to stop\r\n",
  113. frequency,
  114. key,
  115. repeat);
  116. string_t flipper_format_string;
  117. string_init_printf(
  118. flipper_format_string,
  119. "Protocol: Princeton\n"
  120. "Bit: 24\n"
  121. "Key: 00 00 00 00 00 %X %X %X\n"
  122. "TE: 403\n"
  123. "Repeat: %d\n",
  124. (uint8_t)((key >> 16) & 0xFF),
  125. (uint8_t)((key >> 8) & 0xFF),
  126. (uint8_t)(key & 0xFF),
  127. repeat);
  128. FlipperFormat* flipper_format = flipper_format_string_alloc();
  129. Stream* stream = flipper_format_get_raw_stream(flipper_format);
  130. stream_clean(stream);
  131. stream_write_cstring(stream, string_get_cstr(flipper_format_string));
  132. SubGhzEnvironment* environment = subghz_environment_alloc();
  133. SubGhzTransmitter* transmitter = subghz_transmitter_alloc_init(environment, "Princeton");
  134. subghz_transmitter_deserialize(transmitter, flipper_format);
  135. furi_hal_subghz_reset();
  136. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  137. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  138. furi_hal_power_suppress_charge_enter();
  139. furi_hal_subghz_start_async_tx(subghz_transmitter_yield, transmitter);
  140. while(!(furi_hal_subghz_is_async_tx_complete() || cli_cmd_interrupt_received(cli))) {
  141. printf(".");
  142. fflush(stdout);
  143. osDelay(333);
  144. }
  145. furi_hal_subghz_stop_async_tx();
  146. furi_hal_subghz_sleep();
  147. furi_hal_power_suppress_charge_exit();
  148. flipper_format_free(flipper_format);
  149. subghz_transmitter_free(transmitter);
  150. subghz_environment_free(environment);
  151. }
  152. typedef struct {
  153. volatile bool overrun;
  154. StreamBufferHandle_t stream;
  155. size_t packet_count;
  156. } SubGhzCliCommandRx;
  157. static void subghz_cli_command_rx_capture_callback(bool level, uint32_t duration, void* context) {
  158. SubGhzCliCommandRx* instance = context;
  159. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  160. LevelDuration level_duration = level_duration_make(level, duration);
  161. if(instance->overrun) {
  162. instance->overrun = false;
  163. level_duration = level_duration_reset();
  164. }
  165. size_t ret = xStreamBufferSendFromISR(
  166. instance->stream, &level_duration, sizeof(LevelDuration), &xHigherPriorityTaskWoken);
  167. if(sizeof(LevelDuration) != ret) instance->overrun = true;
  168. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  169. }
  170. static void subghz_cli_command_rx_callback(
  171. SubGhzReceiver* receiver,
  172. SubGhzProtocolDecoderBase* decoder_base,
  173. void* context) {
  174. SubGhzCliCommandRx* instance = context;
  175. instance->packet_count++;
  176. string_t text;
  177. string_init(text);
  178. subghz_protocol_decoder_base_get_string(decoder_base, text);
  179. subghz_receiver_reset(receiver);
  180. printf("%s", string_get_cstr(text));
  181. string_clear(text);
  182. }
  183. void subghz_cli_command_rx(Cli* cli, string_t args, void* context) {
  184. UNUSED(context);
  185. uint32_t frequency = 433920000;
  186. if(string_size(args)) {
  187. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  188. if(ret != 1) {
  189. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  190. cli_print_usage("subghz rx", "<Frequency: in Hz>", string_get_cstr(args));
  191. return;
  192. }
  193. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  194. printf(
  195. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  196. frequency);
  197. return;
  198. }
  199. }
  200. // Allocate context and buffers
  201. SubGhzCliCommandRx* instance = malloc(sizeof(SubGhzCliCommandRx));
  202. instance->stream = xStreamBufferCreate(sizeof(LevelDuration) * 1024, sizeof(LevelDuration));
  203. furi_check(instance->stream);
  204. SubGhzEnvironment* environment = subghz_environment_alloc();
  205. subghz_environment_load_keystore(environment, "/ext/subghz/assets/keeloq_mfcodes");
  206. subghz_environment_set_came_atomo_rainbow_table_file_name(
  207. environment, "/ext/subghz/assets/came_atomo");
  208. subghz_environment_set_nice_flor_s_rainbow_table_file_name(
  209. environment, "/ext/subghz/assets/nice_flor_s");
  210. SubGhzReceiver* receiver = subghz_receiver_alloc_init(environment);
  211. subghz_receiver_set_filter(receiver, SubGhzProtocolFlag_Decodable);
  212. subghz_receiver_set_rx_callback(receiver, subghz_cli_command_rx_callback, instance);
  213. // Configure radio
  214. furi_hal_subghz_reset();
  215. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  216. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  217. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  218. furi_hal_power_suppress_charge_enter();
  219. // Prepare and start RX
  220. furi_hal_subghz_start_async_rx(subghz_cli_command_rx_capture_callback, instance);
  221. // Wait for packets to arrive
  222. printf("Listening at %lu. Press CTRL+C to stop\r\n", frequency);
  223. LevelDuration level_duration;
  224. while(!cli_cmd_interrupt_received(cli)) {
  225. int ret =
  226. xStreamBufferReceive(instance->stream, &level_duration, sizeof(LevelDuration), 10);
  227. if(ret == sizeof(LevelDuration)) {
  228. if(level_duration_is_reset(level_duration)) {
  229. printf(".");
  230. subghz_receiver_reset(receiver);
  231. } else {
  232. bool level = level_duration_get_level(level_duration);
  233. uint32_t duration = level_duration_get_duration(level_duration);
  234. subghz_receiver_decode(receiver, level, duration);
  235. }
  236. }
  237. }
  238. // Shutdown radio
  239. furi_hal_subghz_stop_async_rx();
  240. furi_hal_subghz_sleep();
  241. furi_hal_power_suppress_charge_exit();
  242. printf("\r\nPackets recieved %u\r\n", instance->packet_count);
  243. // Cleanup
  244. subghz_receiver_free(receiver);
  245. subghz_environment_free(environment);
  246. vStreamBufferDelete(instance->stream);
  247. free(instance);
  248. }
  249. void subghz_cli_command_decode_raw(Cli* cli, string_t args, void* context) {
  250. UNUSED(context);
  251. string_t file_name;
  252. string_init(file_name);
  253. string_set_str(file_name, "/any/subghz/test.sub");
  254. Storage* storage = furi_record_open("storage");
  255. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  256. string_t temp_str;
  257. string_init(temp_str);
  258. uint32_t temp_data32;
  259. bool check_file = false;
  260. do {
  261. if(string_size(args)) {
  262. if(!args_read_string_and_trim(args, file_name)) {
  263. cli_print_usage(
  264. "subghz decode_raw", "<file_name: path_RAW_file>", string_get_cstr(args));
  265. break;
  266. }
  267. }
  268. if(!flipper_format_file_open_existing(fff_data_file, string_get_cstr(file_name))) {
  269. printf(
  270. "subghz decode_raw \033[0;31mError open file\033[0m %s\r\n",
  271. string_get_cstr(file_name));
  272. break;
  273. }
  274. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  275. printf("subghz decode_raw \033[0;31mMissing or incorrect header\033[0m\r\n");
  276. break;
  277. }
  278. if(!strcmp(string_get_cstr(temp_str), SUBGHZ_RAW_FILE_TYPE) &&
  279. temp_data32 == SUBGHZ_KEY_FILE_VERSION) {
  280. } else {
  281. printf("subghz decode_raw \033[0;31mType or version mismatch\033[0m\r\n");
  282. break;
  283. }
  284. check_file = true;
  285. } while(false);
  286. string_clear(temp_str);
  287. flipper_format_free(fff_data_file);
  288. furi_record_close("storage");
  289. if(check_file) {
  290. // Allocate context
  291. SubGhzCliCommandRx* instance = malloc(sizeof(SubGhzCliCommandRx));
  292. SubGhzEnvironment* environment = subghz_environment_alloc();
  293. if(subghz_environment_load_keystore(environment, "/ext/subghz/assets/keeloq_mfcodes")) {
  294. printf("SubGhz test: Load_keystore \033[0;32mOK\033[0m\r\n");
  295. } else {
  296. printf("SubGhz test: Load_keystore \033[0;31mERROR\033[0m\r\n");
  297. }
  298. subghz_environment_set_came_atomo_rainbow_table_file_name(
  299. environment, "/ext/subghz/assets/came_atomo");
  300. subghz_environment_set_nice_flor_s_rainbow_table_file_name(
  301. environment, "/ext/subghz/assets/nice_flor_s");
  302. SubGhzReceiver* receiver = subghz_receiver_alloc_init(environment);
  303. subghz_receiver_set_filter(receiver, SubGhzProtocolFlag_Decodable);
  304. subghz_receiver_set_rx_callback(receiver, subghz_cli_command_rx_callback, instance);
  305. SubGhzFileEncoderWorker* file_worker_encoder = subghz_file_encoder_worker_alloc();
  306. if(subghz_file_encoder_worker_start(file_worker_encoder, string_get_cstr(file_name))) {
  307. //the worker needs a file in order to open and read part of the file
  308. osDelay(100);
  309. }
  310. printf(
  311. "Listening at \033[0;33m%s\033[0m.\r\n\r\nPress CTRL+C to stop\r\n\r\n",
  312. string_get_cstr(file_name));
  313. LevelDuration level_duration;
  314. while(!cli_cmd_interrupt_received(cli)) {
  315. furi_hal_delay_us(500); //you need to have time to read from the file from the SD card
  316. level_duration = subghz_file_encoder_worker_get_level_duration(file_worker_encoder);
  317. if(!level_duration_is_reset(level_duration)) {
  318. bool level = level_duration_get_level(level_duration);
  319. uint32_t duration = level_duration_get_duration(level_duration);
  320. subghz_receiver_decode(receiver, level, duration);
  321. } else {
  322. break;
  323. }
  324. }
  325. printf("\r\nPackets recieved \033[0;32m%u\033[0m\r\n", instance->packet_count);
  326. // Cleanup
  327. subghz_receiver_free(receiver);
  328. subghz_environment_free(environment);
  329. if(subghz_file_encoder_worker_is_running(file_worker_encoder)) {
  330. subghz_file_encoder_worker_stop(file_worker_encoder);
  331. }
  332. subghz_file_encoder_worker_free(file_worker_encoder);
  333. free(instance);
  334. }
  335. string_clear(file_name);
  336. }
  337. static void subghz_cli_command_print_usage() {
  338. printf("Usage:\r\n");
  339. printf("subghz <cmd> <args>\r\n");
  340. printf("Cmd list:\r\n");
  341. printf("\tchat <frequency:in Hz>\t - Chat with other Flippers\r\n");
  342. printf(
  343. "\ttx <3 byte Key: in hex> <frequency: in Hz> <repeat: count>\t - Transmitting key\r\n");
  344. printf("\trx <frequency:in Hz>\t - Reception key\r\n");
  345. printf("\tdecode_raw <file_name: path_RAW_file>\t - Testing\r\n");
  346. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  347. printf("\r\n");
  348. printf(" debug cmd:\r\n");
  349. printf("\ttx_carrier <frequency:in Hz>\t - Transmit carrier\r\n");
  350. printf("\trx_carrier <frequency:in Hz>\t - Receiv carrier\r\n");
  351. printf(
  352. "\tencrypt_keeloq <path_decrypted_file> <path_encrypted_file> <IV:16 bytes in hex>\t - Encrypt keeloq manufacture keys\r\n");
  353. printf(
  354. "\tencrypt_raw <path_decrypted_file> <path_encrypted_file> <IV:16 bytes in hex>\t - Encrypt RAW data\r\n");
  355. }
  356. }
  357. static void subghz_cli_command_encrypt_keeloq(Cli* cli, string_t args) {
  358. UNUSED(cli);
  359. uint8_t iv[16];
  360. string_t source;
  361. string_t destination;
  362. string_init(source);
  363. string_init(destination);
  364. SubGhzKeystore* keystore = subghz_keystore_alloc();
  365. do {
  366. if(!args_read_string_and_trim(args, source)) {
  367. subghz_cli_command_print_usage();
  368. break;
  369. }
  370. if(!args_read_string_and_trim(args, destination)) {
  371. subghz_cli_command_print_usage();
  372. break;
  373. }
  374. if(!args_read_hex_bytes(args, iv, 16)) {
  375. subghz_cli_command_print_usage();
  376. break;
  377. }
  378. if(!subghz_keystore_load(keystore, string_get_cstr(source))) {
  379. printf("Failed to load Keystore");
  380. break;
  381. }
  382. if(!subghz_keystore_save(keystore, string_get_cstr(destination), iv)) {
  383. printf("Failed to save Keystore");
  384. break;
  385. }
  386. } while(false);
  387. subghz_keystore_free(keystore);
  388. string_clear(destination);
  389. string_clear(source);
  390. }
  391. static void subghz_cli_command_encrypt_raw(Cli* cli, string_t args) {
  392. UNUSED(cli);
  393. uint8_t iv[16];
  394. string_t source;
  395. string_t destination;
  396. string_init(source);
  397. string_init(destination);
  398. do {
  399. if(!args_read_string_and_trim(args, source)) {
  400. subghz_cli_command_print_usage();
  401. break;
  402. }
  403. if(!args_read_string_and_trim(args, destination)) {
  404. subghz_cli_command_print_usage();
  405. break;
  406. }
  407. if(!args_read_hex_bytes(args, iv, 16)) {
  408. subghz_cli_command_print_usage();
  409. break;
  410. }
  411. if(!subghz_keystore_raw_encrypted_save(
  412. string_get_cstr(source), string_get_cstr(destination), iv)) {
  413. printf("Failed to save Keystore");
  414. break;
  415. }
  416. } while(false);
  417. string_clear(destination);
  418. string_clear(source);
  419. }
  420. static void subghz_cli_command_chat(Cli* cli, string_t args) {
  421. uint32_t frequency = 433920000;
  422. if(string_size(args)) {
  423. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  424. if(ret != 1) {
  425. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  426. cli_print_usage("subghz chat", "<Frequency: in Hz>", string_get_cstr(args));
  427. return;
  428. }
  429. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  430. printf(
  431. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  432. frequency);
  433. return;
  434. }
  435. }
  436. if(!furi_hal_subghz_is_tx_allowed(frequency)) {
  437. printf(
  438. "In your region, only reception on this frequency (%lu) is allowed,\r\n"
  439. "the actual operation of the application is not possible\r\n ",
  440. frequency);
  441. return;
  442. }
  443. SubGhzChatWorker* subghz_chat = subghz_chat_worker_alloc(cli);
  444. if(!subghz_chat_worker_start(subghz_chat, frequency)) {
  445. printf("Startup error SubGhzChatWorker\r\n");
  446. if(subghz_chat_worker_is_running(subghz_chat)) {
  447. subghz_chat_worker_stop(subghz_chat);
  448. subghz_chat_worker_free(subghz_chat);
  449. }
  450. return;
  451. }
  452. printf("Receiving at frequency %lu Hz\r\n", frequency);
  453. printf("Press CTRL+C to stop\r\n");
  454. furi_hal_power_suppress_charge_enter();
  455. size_t message_max_len = 64;
  456. uint8_t message[64] = {0};
  457. string_t input;
  458. string_init(input);
  459. string_t name;
  460. string_init(name);
  461. string_t output;
  462. string_init(output);
  463. string_t sysmsg;
  464. string_init(sysmsg);
  465. bool exit = false;
  466. SubGhzChatEvent chat_event;
  467. NotificationApp* notification = furi_record_open("notification");
  468. string_printf(name, "\033[0;33m%s\033[0m: ", furi_hal_version_get_name_ptr());
  469. string_set(input, name);
  470. printf("%s", string_get_cstr(input));
  471. fflush(stdout);
  472. while(!exit) {
  473. chat_event = subghz_chat_worker_get_event_chat(subghz_chat);
  474. switch(chat_event.event) {
  475. case SubGhzChatEventInputData:
  476. if(chat_event.c == CliSymbolAsciiETX) {
  477. printf("\r\n");
  478. chat_event.event = SubGhzChatEventUserExit;
  479. subghz_chat_worker_put_event_chat(subghz_chat, &chat_event);
  480. break;
  481. } else if(
  482. (chat_event.c == CliSymbolAsciiBackspace) || (chat_event.c == CliSymbolAsciiDel)) {
  483. size_t len = string_length_u(input);
  484. if(len > string_length_u(name)) {
  485. printf("%s", "\e[D\e[1P");
  486. fflush(stdout);
  487. //delete 1 char UTF
  488. const char* str = string_get_cstr(input);
  489. size_t size = 0;
  490. m_str1ng_utf8_state_e s = M_STRING_UTF8_STARTING;
  491. string_unicode_t u = 0;
  492. string_reset(sysmsg);
  493. while(*str) {
  494. m_str1ng_utf8_decode(*str, &s, &u);
  495. if((s == M_STRING_UTF8_ERROR) || s == M_STRING_UTF8_STARTING) {
  496. string_push_u(sysmsg, u);
  497. if(++size >= len - 1) break;
  498. s = M_STRING_UTF8_STARTING;
  499. }
  500. str++;
  501. }
  502. string_set(input, sysmsg);
  503. }
  504. } else if(chat_event.c == CliSymbolAsciiCR) {
  505. printf("\r\n");
  506. string_push_back(input, '\r');
  507. string_push_back(input, '\n');
  508. while(!subghz_chat_worker_write(
  509. subghz_chat,
  510. (uint8_t*)string_get_cstr(input),
  511. strlen(string_get_cstr(input)))) {
  512. furi_hal_delay_ms(10);
  513. }
  514. string_printf(input, "%s", string_get_cstr(name));
  515. printf("%s", string_get_cstr(input));
  516. fflush(stdout);
  517. } else if(chat_event.c == CliSymbolAsciiLF) {
  518. //cut out the symbol \n
  519. } else {
  520. putc(chat_event.c, stdout);
  521. fflush(stdout);
  522. string_push_back(input, chat_event.c);
  523. break;
  524. case SubGhzChatEventRXData:
  525. do {
  526. memset(message, 0x00, message_max_len);
  527. size_t len = subghz_chat_worker_read(subghz_chat, message, message_max_len);
  528. for(size_t i = 0; i < len; i++) {
  529. string_push_back(output, message[i]);
  530. if(message[i] == '\n') {
  531. printf("\r");
  532. for(uint8_t i = 0; i < 80; i++) {
  533. printf(" ");
  534. }
  535. printf("\r %s", string_get_cstr(output));
  536. printf("%s", string_get_cstr(input));
  537. fflush(stdout);
  538. string_reset(output);
  539. }
  540. }
  541. } while(subghz_chat_worker_available(subghz_chat));
  542. break;
  543. case SubGhzChatEventNewMessage:
  544. notification_message(notification, &sequence_single_vibro);
  545. break;
  546. case SubGhzChatEventUserEntrance:
  547. string_printf(
  548. sysmsg,
  549. "\033[0;34m%s joined chat.\033[0m\r\n",
  550. furi_hal_version_get_name_ptr());
  551. subghz_chat_worker_write(
  552. subghz_chat,
  553. (uint8_t*)string_get_cstr(sysmsg),
  554. strlen(string_get_cstr(sysmsg)));
  555. break;
  556. case SubGhzChatEventUserExit:
  557. string_printf(
  558. sysmsg, "\033[0;31m%s left chat.\033[0m\r\n", furi_hal_version_get_name_ptr());
  559. subghz_chat_worker_write(
  560. subghz_chat,
  561. (uint8_t*)string_get_cstr(sysmsg),
  562. strlen(string_get_cstr(sysmsg)));
  563. furi_hal_delay_ms(10);
  564. exit = true;
  565. break;
  566. default:
  567. FURI_LOG_W("SubGhzChat", "Error event");
  568. break;
  569. }
  570. }
  571. }
  572. string_clear(input);
  573. string_clear(name);
  574. string_clear(output);
  575. string_clear(sysmsg);
  576. furi_hal_power_suppress_charge_exit();
  577. furi_record_close("notification");
  578. if(subghz_chat_worker_is_running(subghz_chat)) {
  579. subghz_chat_worker_stop(subghz_chat);
  580. subghz_chat_worker_free(subghz_chat);
  581. }
  582. printf("\r\nExit chat\r\n");
  583. }
  584. static void subghz_cli_command(Cli* cli, string_t args, void* context) {
  585. string_t cmd;
  586. string_init(cmd);
  587. do {
  588. if(!args_read_string_and_trim(args, cmd)) {
  589. subghz_cli_command_print_usage();
  590. break;
  591. }
  592. if(string_cmp_str(cmd, "chat") == 0) {
  593. subghz_cli_command_chat(cli, args);
  594. break;
  595. }
  596. if(string_cmp_str(cmd, "tx") == 0) {
  597. subghz_cli_command_tx(cli, args, context);
  598. break;
  599. }
  600. if(string_cmp_str(cmd, "rx") == 0) {
  601. subghz_cli_command_rx(cli, args, context);
  602. break;
  603. }
  604. if(string_cmp_str(cmd, "decode_raw") == 0) {
  605. subghz_cli_command_decode_raw(cli, args, context);
  606. break;
  607. }
  608. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  609. if(string_cmp_str(cmd, "encrypt_keeloq") == 0) {
  610. subghz_cli_command_encrypt_keeloq(cli, args);
  611. break;
  612. }
  613. if(string_cmp_str(cmd, "encrypt_raw") == 0) {
  614. subghz_cli_command_encrypt_raw(cli, args);
  615. break;
  616. }
  617. if(string_cmp_str(cmd, "tx_carrier") == 0) {
  618. subghz_cli_command_tx_carrier(cli, args, context);
  619. break;
  620. }
  621. if(string_cmp_str(cmd, "rx_carrier") == 0) {
  622. subghz_cli_command_rx_carrier(cli, args, context);
  623. break;
  624. }
  625. }
  626. subghz_cli_command_print_usage();
  627. } while(false);
  628. string_clear(cmd);
  629. }
  630. void subghz_on_system_start() {
  631. #ifdef SRV_CLI
  632. Cli* cli = furi_record_open("cli");
  633. cli_add_command(cli, "subghz", CliCommandFlagDefault, subghz_cli_command, NULL);
  634. furi_record_close("cli");
  635. #else
  636. UNUSED(subghz_cli_command);
  637. #endif
  638. }