subghz_cli.c 25 KB

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