subghz_cli.c 26 KB

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