subghz_cli.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. furi_delay_ms(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. furi_delay_ms(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. furi_delay_ms(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_load_keystore(environment, "/ext/subghz/assets/keeloq_mfcodes_user");
  207. subghz_environment_set_came_atomo_rainbow_table_file_name(
  208. environment, "/ext/subghz/assets/came_atomo");
  209. subghz_environment_set_nice_flor_s_rainbow_table_file_name(
  210. environment, "/ext/subghz/assets/nice_flor_s");
  211. SubGhzReceiver* receiver = subghz_receiver_alloc_init(environment);
  212. subghz_receiver_set_filter(receiver, SubGhzProtocolFlag_Decodable);
  213. subghz_receiver_set_rx_callback(receiver, subghz_cli_command_rx_callback, instance);
  214. // Configure radio
  215. furi_hal_subghz_reset();
  216. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  217. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  218. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  219. furi_hal_power_suppress_charge_enter();
  220. // Prepare and start RX
  221. furi_hal_subghz_start_async_rx(subghz_cli_command_rx_capture_callback, instance);
  222. // Wait for packets to arrive
  223. printf("Listening at %lu. Press CTRL+C to stop\r\n", frequency);
  224. LevelDuration level_duration;
  225. while(!cli_cmd_interrupt_received(cli)) {
  226. int ret =
  227. xStreamBufferReceive(instance->stream, &level_duration, sizeof(LevelDuration), 10);
  228. if(ret == sizeof(LevelDuration)) {
  229. if(level_duration_is_reset(level_duration)) {
  230. printf(".");
  231. subghz_receiver_reset(receiver);
  232. } else {
  233. bool level = level_duration_get_level(level_duration);
  234. uint32_t duration = level_duration_get_duration(level_duration);
  235. subghz_receiver_decode(receiver, level, duration);
  236. }
  237. }
  238. }
  239. // Shutdown radio
  240. furi_hal_subghz_stop_async_rx();
  241. furi_hal_subghz_sleep();
  242. furi_hal_power_suppress_charge_exit();
  243. printf("\r\nPackets recieved %u\r\n", instance->packet_count);
  244. // Cleanup
  245. subghz_receiver_free(receiver);
  246. subghz_environment_free(environment);
  247. vStreamBufferDelete(instance->stream);
  248. free(instance);
  249. }
  250. void subghz_cli_command_decode_raw(Cli* cli, string_t args, void* context) {
  251. UNUSED(context);
  252. string_t file_name;
  253. string_init(file_name);
  254. string_set_str(file_name, "/any/subghz/test.sub");
  255. Storage* storage = furi_record_open("storage");
  256. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  257. string_t temp_str;
  258. string_init(temp_str);
  259. uint32_t temp_data32;
  260. bool check_file = false;
  261. do {
  262. if(string_size(args)) {
  263. if(!args_read_string_and_trim(args, file_name)) {
  264. cli_print_usage(
  265. "subghz decode_raw", "<file_name: path_RAW_file>", string_get_cstr(args));
  266. break;
  267. }
  268. }
  269. if(!flipper_format_file_open_existing(fff_data_file, string_get_cstr(file_name))) {
  270. printf(
  271. "subghz decode_raw \033[0;31mError open file\033[0m %s\r\n",
  272. string_get_cstr(file_name));
  273. break;
  274. }
  275. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  276. printf("subghz decode_raw \033[0;31mMissing or incorrect header\033[0m\r\n");
  277. break;
  278. }
  279. if(!strcmp(string_get_cstr(temp_str), SUBGHZ_RAW_FILE_TYPE) &&
  280. temp_data32 == SUBGHZ_KEY_FILE_VERSION) {
  281. } else {
  282. printf("subghz decode_raw \033[0;31mType or version mismatch\033[0m\r\n");
  283. break;
  284. }
  285. check_file = true;
  286. } while(false);
  287. string_clear(temp_str);
  288. flipper_format_free(fff_data_file);
  289. furi_record_close("storage");
  290. if(check_file) {
  291. // Allocate context
  292. SubGhzCliCommandRx* instance = malloc(sizeof(SubGhzCliCommandRx));
  293. SubGhzEnvironment* environment = subghz_environment_alloc();
  294. if(subghz_environment_load_keystore(environment, "/ext/subghz/assets/keeloq_mfcodes")) {
  295. printf("SubGhz decode_raw: Load_keystore keeloq_mfcodes \033[0;32mOK\033[0m\r\n");
  296. } else {
  297. printf("SubGhz decode_raw: Load_keystore keeloq_mfcodes \033[0;31mERROR\033[0m\r\n");
  298. }
  299. if(subghz_environment_load_keystore(
  300. environment, "/ext/subghz/assets/keeloq_mfcodes_user")) {
  301. printf("SubGhz decode_raw: Load_keystore keeloq_mfcodes_user \033[0;32mOK\033[0m\r\n");
  302. } else {
  303. printf(
  304. "SubGhz decode_raw: Load_keystore keeloq_mfcodes_user \033[0;31mERROR\033[0m\r\n");
  305. }
  306. subghz_environment_set_came_atomo_rainbow_table_file_name(
  307. environment, "/ext/subghz/assets/came_atomo");
  308. subghz_environment_set_nice_flor_s_rainbow_table_file_name(
  309. environment, "/ext/subghz/assets/nice_flor_s");
  310. SubGhzReceiver* receiver = subghz_receiver_alloc_init(environment);
  311. subghz_receiver_set_filter(receiver, SubGhzProtocolFlag_Decodable);
  312. subghz_receiver_set_rx_callback(receiver, subghz_cli_command_rx_callback, instance);
  313. SubGhzFileEncoderWorker* file_worker_encoder = subghz_file_encoder_worker_alloc();
  314. if(subghz_file_encoder_worker_start(file_worker_encoder, string_get_cstr(file_name))) {
  315. //the worker needs a file in order to open and read part of the file
  316. furi_delay_ms(100);
  317. }
  318. printf(
  319. "Listening at \033[0;33m%s\033[0m.\r\n\r\nPress CTRL+C to stop\r\n\r\n",
  320. string_get_cstr(file_name));
  321. LevelDuration level_duration;
  322. while(!cli_cmd_interrupt_received(cli)) {
  323. furi_delay_us(500); //you need to have time to read from the file from the SD card
  324. level_duration = subghz_file_encoder_worker_get_level_duration(file_worker_encoder);
  325. if(!level_duration_is_reset(level_duration)) {
  326. bool level = level_duration_get_level(level_duration);
  327. uint32_t duration = level_duration_get_duration(level_duration);
  328. subghz_receiver_decode(receiver, level, duration);
  329. } else {
  330. break;
  331. }
  332. }
  333. printf("\r\nPackets recieved \033[0;32m%u\033[0m\r\n", instance->packet_count);
  334. // Cleanup
  335. subghz_receiver_free(receiver);
  336. subghz_environment_free(environment);
  337. if(subghz_file_encoder_worker_is_running(file_worker_encoder)) {
  338. subghz_file_encoder_worker_stop(file_worker_encoder);
  339. }
  340. subghz_file_encoder_worker_free(file_worker_encoder);
  341. free(instance);
  342. }
  343. string_clear(file_name);
  344. }
  345. static void subghz_cli_command_print_usage() {
  346. printf("Usage:\r\n");
  347. printf("subghz <cmd> <args>\r\n");
  348. printf("Cmd list:\r\n");
  349. printf("\tchat <frequency:in Hz>\t - Chat with other Flippers\r\n");
  350. printf(
  351. "\ttx <3 byte Key: in hex> <frequency: in Hz> <repeat: count>\t - Transmitting key\r\n");
  352. printf("\trx <frequency:in Hz>\t - Reception key\r\n");
  353. printf("\tdecode_raw <file_name: path_RAW_file>\t - Testing\r\n");
  354. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  355. printf("\r\n");
  356. printf(" debug cmd:\r\n");
  357. printf("\ttx_carrier <frequency:in Hz>\t - Transmit carrier\r\n");
  358. printf("\trx_carrier <frequency:in Hz>\t - Receiv carrier\r\n");
  359. printf(
  360. "\tencrypt_keeloq <path_decrypted_file> <path_encrypted_file> <IV:16 bytes in hex>\t - Encrypt keeloq manufacture keys\r\n");
  361. printf(
  362. "\tencrypt_raw <path_decrypted_file> <path_encrypted_file> <IV:16 bytes in hex>\t - Encrypt RAW data\r\n");
  363. }
  364. }
  365. static void subghz_cli_command_encrypt_keeloq(Cli* cli, string_t args) {
  366. UNUSED(cli);
  367. uint8_t iv[16];
  368. string_t source;
  369. string_t destination;
  370. string_init(source);
  371. string_init(destination);
  372. SubGhzKeystore* keystore = subghz_keystore_alloc();
  373. do {
  374. if(!args_read_string_and_trim(args, source)) {
  375. subghz_cli_command_print_usage();
  376. break;
  377. }
  378. if(!args_read_string_and_trim(args, destination)) {
  379. subghz_cli_command_print_usage();
  380. break;
  381. }
  382. if(!args_read_hex_bytes(args, iv, 16)) {
  383. subghz_cli_command_print_usage();
  384. break;
  385. }
  386. if(!subghz_keystore_load(keystore, string_get_cstr(source))) {
  387. printf("Failed to load Keystore");
  388. break;
  389. }
  390. if(!subghz_keystore_save(keystore, string_get_cstr(destination), iv)) {
  391. printf("Failed to save Keystore");
  392. break;
  393. }
  394. } while(false);
  395. subghz_keystore_free(keystore);
  396. string_clear(destination);
  397. string_clear(source);
  398. }
  399. static void subghz_cli_command_encrypt_raw(Cli* cli, string_t args) {
  400. UNUSED(cli);
  401. uint8_t iv[16];
  402. string_t source;
  403. string_t destination;
  404. string_init(source);
  405. string_init(destination);
  406. do {
  407. if(!args_read_string_and_trim(args, source)) {
  408. subghz_cli_command_print_usage();
  409. break;
  410. }
  411. if(!args_read_string_and_trim(args, destination)) {
  412. subghz_cli_command_print_usage();
  413. break;
  414. }
  415. if(!args_read_hex_bytes(args, iv, 16)) {
  416. subghz_cli_command_print_usage();
  417. break;
  418. }
  419. if(!subghz_keystore_raw_encrypted_save(
  420. string_get_cstr(source), string_get_cstr(destination), iv)) {
  421. printf("Failed to save Keystore");
  422. break;
  423. }
  424. } while(false);
  425. string_clear(destination);
  426. string_clear(source);
  427. }
  428. static void subghz_cli_command_chat(Cli* cli, string_t args) {
  429. uint32_t frequency = 433920000;
  430. if(string_size(args)) {
  431. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  432. if(ret != 1) {
  433. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  434. cli_print_usage("subghz chat", "<Frequency: in Hz>", string_get_cstr(args));
  435. return;
  436. }
  437. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  438. printf(
  439. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  440. frequency);
  441. return;
  442. }
  443. }
  444. if(!furi_hal_subghz_is_tx_allowed(frequency)) {
  445. printf(
  446. "In your region, only reception on this frequency (%lu) is allowed,\r\n"
  447. "the actual operation of the application is not possible\r\n ",
  448. frequency);
  449. return;
  450. }
  451. SubGhzChatWorker* subghz_chat = subghz_chat_worker_alloc(cli);
  452. if(!subghz_chat_worker_start(subghz_chat, frequency)) {
  453. printf("Startup error SubGhzChatWorker\r\n");
  454. if(subghz_chat_worker_is_running(subghz_chat)) {
  455. subghz_chat_worker_stop(subghz_chat);
  456. subghz_chat_worker_free(subghz_chat);
  457. }
  458. return;
  459. }
  460. printf("Receiving at frequency %lu Hz\r\n", frequency);
  461. printf("Press CTRL+C to stop\r\n");
  462. furi_hal_power_suppress_charge_enter();
  463. size_t message_max_len = 64;
  464. uint8_t message[64] = {0};
  465. string_t input;
  466. string_init(input);
  467. string_t name;
  468. string_init(name);
  469. string_t output;
  470. string_init(output);
  471. string_t sysmsg;
  472. string_init(sysmsg);
  473. bool exit = false;
  474. SubGhzChatEvent chat_event;
  475. NotificationApp* notification = furi_record_open("notification");
  476. string_printf(name, "\033[0;33m%s\033[0m: ", furi_hal_version_get_name_ptr());
  477. string_set(input, name);
  478. printf("%s", string_get_cstr(input));
  479. fflush(stdout);
  480. while(!exit) {
  481. chat_event = subghz_chat_worker_get_event_chat(subghz_chat);
  482. switch(chat_event.event) {
  483. case SubGhzChatEventInputData:
  484. if(chat_event.c == CliSymbolAsciiETX) {
  485. printf("\r\n");
  486. chat_event.event = SubGhzChatEventUserExit;
  487. subghz_chat_worker_put_event_chat(subghz_chat, &chat_event);
  488. break;
  489. } else if(
  490. (chat_event.c == CliSymbolAsciiBackspace) || (chat_event.c == CliSymbolAsciiDel)) {
  491. size_t len = string_length_u(input);
  492. if(len > string_length_u(name)) {
  493. printf("%s", "\e[D\e[1P");
  494. fflush(stdout);
  495. //delete 1 char UTF
  496. const char* str = string_get_cstr(input);
  497. size_t size = 0;
  498. m_str1ng_utf8_state_e s = M_STRING_UTF8_STARTING;
  499. string_unicode_t u = 0;
  500. string_reset(sysmsg);
  501. while(*str) {
  502. m_str1ng_utf8_decode(*str, &s, &u);
  503. if((s == M_STRING_UTF8_ERROR) || s == M_STRING_UTF8_STARTING) {
  504. string_push_u(sysmsg, u);
  505. if(++size >= len - 1) break;
  506. s = M_STRING_UTF8_STARTING;
  507. }
  508. str++;
  509. }
  510. string_set(input, sysmsg);
  511. }
  512. } else if(chat_event.c == CliSymbolAsciiCR) {
  513. printf("\r\n");
  514. string_push_back(input, '\r');
  515. string_push_back(input, '\n');
  516. while(!subghz_chat_worker_write(
  517. subghz_chat,
  518. (uint8_t*)string_get_cstr(input),
  519. strlen(string_get_cstr(input)))) {
  520. furi_delay_ms(10);
  521. }
  522. string_printf(input, "%s", string_get_cstr(name));
  523. printf("%s", string_get_cstr(input));
  524. fflush(stdout);
  525. } else if(chat_event.c == CliSymbolAsciiLF) {
  526. //cut out the symbol \n
  527. } else {
  528. putc(chat_event.c, stdout);
  529. fflush(stdout);
  530. string_push_back(input, chat_event.c);
  531. break;
  532. case SubGhzChatEventRXData:
  533. do {
  534. memset(message, 0x00, message_max_len);
  535. size_t len = subghz_chat_worker_read(subghz_chat, message, message_max_len);
  536. for(size_t i = 0; i < len; i++) {
  537. string_push_back(output, message[i]);
  538. if(message[i] == '\n') {
  539. printf("\r");
  540. for(uint8_t i = 0; i < 80; i++) {
  541. printf(" ");
  542. }
  543. printf("\r %s", string_get_cstr(output));
  544. printf("%s", string_get_cstr(input));
  545. fflush(stdout);
  546. string_reset(output);
  547. }
  548. }
  549. } while(subghz_chat_worker_available(subghz_chat));
  550. break;
  551. case SubGhzChatEventNewMessage:
  552. notification_message(notification, &sequence_single_vibro);
  553. break;
  554. case SubGhzChatEventUserEntrance:
  555. string_printf(
  556. sysmsg,
  557. "\033[0;34m%s joined chat.\033[0m\r\n",
  558. 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. break;
  564. case SubGhzChatEventUserExit:
  565. string_printf(
  566. sysmsg, "\033[0;31m%s left chat.\033[0m\r\n", furi_hal_version_get_name_ptr());
  567. subghz_chat_worker_write(
  568. subghz_chat,
  569. (uint8_t*)string_get_cstr(sysmsg),
  570. strlen(string_get_cstr(sysmsg)));
  571. furi_delay_ms(10);
  572. exit = true;
  573. break;
  574. default:
  575. FURI_LOG_W("SubGhzChat", "Error event");
  576. break;
  577. }
  578. }
  579. if(cli_is_connected(cli)) {
  580. printf("\r\n");
  581. chat_event.event = SubGhzChatEventUserExit;
  582. subghz_chat_worker_put_event_chat(subghz_chat, &chat_event);
  583. }
  584. }
  585. string_clear(input);
  586. string_clear(name);
  587. string_clear(output);
  588. string_clear(sysmsg);
  589. furi_hal_power_suppress_charge_exit();
  590. furi_record_close("notification");
  591. if(subghz_chat_worker_is_running(subghz_chat)) {
  592. subghz_chat_worker_stop(subghz_chat);
  593. subghz_chat_worker_free(subghz_chat);
  594. }
  595. printf("\r\nExit chat\r\n");
  596. }
  597. static void subghz_cli_command(Cli* cli, string_t args, void* context) {
  598. string_t cmd;
  599. string_init(cmd);
  600. do {
  601. if(!args_read_string_and_trim(args, cmd)) {
  602. subghz_cli_command_print_usage();
  603. break;
  604. }
  605. if(string_cmp_str(cmd, "chat") == 0) {
  606. subghz_cli_command_chat(cli, args);
  607. break;
  608. }
  609. if(string_cmp_str(cmd, "tx") == 0) {
  610. subghz_cli_command_tx(cli, args, context);
  611. break;
  612. }
  613. if(string_cmp_str(cmd, "rx") == 0) {
  614. subghz_cli_command_rx(cli, args, context);
  615. break;
  616. }
  617. if(string_cmp_str(cmd, "decode_raw") == 0) {
  618. subghz_cli_command_decode_raw(cli, args, context);
  619. break;
  620. }
  621. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  622. if(string_cmp_str(cmd, "encrypt_keeloq") == 0) {
  623. subghz_cli_command_encrypt_keeloq(cli, args);
  624. break;
  625. }
  626. if(string_cmp_str(cmd, "encrypt_raw") == 0) {
  627. subghz_cli_command_encrypt_raw(cli, args);
  628. break;
  629. }
  630. if(string_cmp_str(cmd, "tx_carrier") == 0) {
  631. subghz_cli_command_tx_carrier(cli, args, context);
  632. break;
  633. }
  634. if(string_cmp_str(cmd, "rx_carrier") == 0) {
  635. subghz_cli_command_rx_carrier(cli, args, context);
  636. break;
  637. }
  638. }
  639. subghz_cli_command_print_usage();
  640. } while(false);
  641. string_clear(cmd);
  642. }
  643. void subghz_on_system_start() {
  644. #ifdef SRV_CLI
  645. Cli* cli = furi_record_open("cli");
  646. cli_add_command(cli, "subghz", CliCommandFlagDefault, subghz_cli_command, NULL);
  647. furi_record_close("cli");
  648. #else
  649. UNUSED(subghz_cli_command);
  650. #endif
  651. }