esubghz_chat.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. #include <furi_hal.h>
  2. #include <gui/elements.h>
  3. #include <gui/gui.h>
  4. #include <lib/subghz/devices/cc1101_int/cc1101_int_interconnect.h>
  5. #include "esubghz_chat_icons.h"
  6. #include "esubghz_chat_i.h"
  7. #define CHAT_LEAVE_DELAY 10
  8. #define TICK_INTERVAL 50
  9. #define MESSAGE_COMPLETION_TIMEOUT 500
  10. #define TIMEOUT_BETWEEN_MESSAGES 500
  11. #define KBD_UNLOCK_CNT 3
  12. #define KBD_UNLOCK_TIMEOUT 1000
  13. /* Callback for RX events from the Sub-GHz worker. Records the current ticks as
  14. * the time of the last reception. */
  15. static void have_read_cb(void* context)
  16. {
  17. furi_assert(context);
  18. ESubGhzChatState* state = context;
  19. state->last_time_rx_data = furi_get_tick();
  20. }
  21. /* Decrypts a message for post_rx(). */
  22. static bool post_rx_decrypt(ESubGhzChatState *state, size_t rx_size)
  23. {
  24. bool ret = crypto_ctx_decrypt(state->crypto_ctx,
  25. state->rx_buffer, rx_size,
  26. (uint8_t*) state->rx_str_buffer);
  27. if (ret) {
  28. state->rx_str_buffer[rx_size - (MSG_OVERHEAD)] = 0;
  29. } else {
  30. state->rx_str_buffer[0] = 0;
  31. }
  32. return ret;
  33. }
  34. /* Post RX handler, decrypts received messages, displays them in the text box
  35. * and sends a notification. */
  36. static void post_rx(ESubGhzChatState *state, size_t rx_size)
  37. {
  38. furi_assert(state);
  39. if (rx_size == 0) {
  40. return;
  41. }
  42. furi_check(rx_size <= RX_TX_BUFFER_SIZE);
  43. /* decrypt if necessary */
  44. if (!state->encrypted) {
  45. memcpy(state->rx_str_buffer, state->rx_buffer, rx_size);
  46. state->rx_str_buffer[rx_size] = 0;
  47. /* remove trailing newline if it is there, for compat with CLI
  48. * Sub-GHz chat */
  49. if (state->rx_str_buffer[rx_size - 1] == '\n') {
  50. state->rx_str_buffer[rx_size - 1] = 0;
  51. }
  52. } else {
  53. /* if decryption fails output an error message */
  54. if (!post_rx_decrypt(state, rx_size)) {
  55. strcpy(state->rx_str_buffer, "ERR: Decryption failed!");
  56. }
  57. }
  58. /* append message to text box */
  59. furi_string_cat_printf(state->chat_box_store, "\n%s",
  60. state->rx_str_buffer);
  61. /* send notification (make the flipper vibrate) */
  62. notification_message(state->notification, &sequence_single_vibro);
  63. /* reset text box contents and focus */
  64. text_box_set_text(state->chat_box,
  65. furi_string_get_cstr(state->chat_box_store));
  66. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  67. }
  68. /* Reads the message from msg_input, encrypts it if necessary and then
  69. * transmits it. */
  70. void tx_msg_input(ESubGhzChatState *state)
  71. {
  72. /* encrypt message if necessary */
  73. size_t msg_len = strlen(furi_string_get_cstr(state->msg_input));
  74. size_t tx_size = msg_len;
  75. if (state->encrypted) {
  76. tx_size += MSG_OVERHEAD;
  77. furi_check(tx_size <= sizeof(state->tx_buffer));
  78. crypto_ctx_encrypt(state->crypto_ctx,
  79. (uint8_t *)
  80. furi_string_get_cstr(state->msg_input),
  81. msg_len,
  82. state->tx_buffer);
  83. } else {
  84. tx_size += 2;
  85. furi_check(tx_size <= sizeof(state->tx_buffer));
  86. memcpy(state->tx_buffer,
  87. furi_string_get_cstr(state->msg_input),
  88. msg_len);
  89. /* append \r\n for compat with Sub-GHz CLI chat */
  90. state->tx_buffer[msg_len] = '\r';
  91. state->tx_buffer[msg_len + 1] = '\n';
  92. }
  93. /* transmit */
  94. subghz_tx_rx_worker_write(state->subghz_worker, state->tx_buffer,
  95. tx_size);
  96. }
  97. /* Displays whether or not encryption has been enabled in the text box. Also
  98. * clears the text input buffer to remove the password and starts the Sub-GHz
  99. * worker. After starting the worker a join message is transmitted. */
  100. void enter_chat(ESubGhzChatState *state)
  101. {
  102. furi_string_cat_printf(state->chat_box_store, "\nEncrypted: %s",
  103. (state->encrypted ? "yes" : "no"));
  104. subghz_tx_rx_worker_start(state->subghz_worker, state->subghz_device,
  105. state->frequency);
  106. /* concatenate the name prefix and join message */
  107. furi_string_set(state->msg_input, state->name_prefix);
  108. furi_string_cat_str(state->msg_input, " joined chat.");
  109. /* encrypt and transmit message */
  110. tx_msg_input(state);
  111. /* clear message input buffer */
  112. furi_string_set_char(state->msg_input, 0, 0);
  113. }
  114. /* Sends a leave message */
  115. void exit_chat(ESubGhzChatState *state)
  116. {
  117. /* concatenate the name prefix and leave message */
  118. furi_string_set(state->msg_input, state->name_prefix);
  119. furi_string_cat_str(state->msg_input, " left chat.");
  120. /* encrypt and transmit message */
  121. tx_msg_input(state);
  122. /* clear message input buffer */
  123. furi_string_set_char(state->msg_input, 0, 0);
  124. /* wait for leave message to be delivered */
  125. furi_delay_ms(CHAT_LEAVE_DELAY);
  126. }
  127. /* Whether or not to display the locked message. */
  128. static bool kbd_lock_msg_display(ESubGhzChatState *state)
  129. {
  130. return (state->kbd_lock_msg_ticks != 0);
  131. }
  132. /* Whether or not to hide the locked message again. */
  133. static bool kbd_lock_msg_reset_timeout(ESubGhzChatState *state)
  134. {
  135. if (state->kbd_lock_msg_ticks == 0) {
  136. return false;
  137. }
  138. if (furi_get_tick() - state->kbd_lock_msg_ticks > KBD_UNLOCK_TIMEOUT) {
  139. return true;
  140. }
  141. return false;
  142. }
  143. /* Resets the timeout for the locked message and turns off the backlight if
  144. * specified. */
  145. static void kbd_lock_msg_reset(ESubGhzChatState *state, bool backlight_off)
  146. {
  147. state->kbd_lock_msg_ticks = 0;
  148. state->kbd_lock_count = 0;
  149. if (backlight_off) {
  150. notification_message(state->notification,
  151. &sequence_display_backlight_off);
  152. }
  153. }
  154. /* Locks the keyboard. */
  155. static void kbd_lock(ESubGhzChatState *state)
  156. {
  157. state->kbd_locked = true;
  158. kbd_lock_msg_reset(state, true);
  159. }
  160. /* Unlocks the keyboard. */
  161. static void kbd_unlock(ESubGhzChatState *state)
  162. {
  163. state->kbd_locked = false;
  164. kbd_lock_msg_reset(state, false);
  165. }
  166. /* Custom event callback for view dispatcher. Just calls scene manager. */
  167. static bool esubghz_chat_custom_event_callback(void* context, uint32_t event)
  168. {
  169. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_custom_event_callback");
  170. furi_assert(context);
  171. ESubGhzChatState* state = context;
  172. return scene_manager_handle_custom_event(state->scene_manager, event);
  173. }
  174. /* Navigation event callback for view dispatcher. Just calls scene manager. */
  175. static bool esubghz_chat_navigation_event_callback(void* context)
  176. {
  177. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_navigation_event_callback");
  178. furi_assert(context);
  179. ESubGhzChatState* state = context;
  180. return scene_manager_handle_back_event(state->scene_manager);
  181. }
  182. /* Tick event callback for view dispatcher. Called every TICK_INTERVAL. Resets
  183. * the locked message if necessary. Retrieves a received message from the
  184. * Sub-GHz worker and calls post_rx(). Then calls the scene manager. */
  185. static void esubghz_chat_tick_event_callback(void* context)
  186. {
  187. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_tick_event_callback");
  188. furi_assert(context);
  189. ESubGhzChatState* state = context;
  190. /* reset locked message if necessary */
  191. if (kbd_lock_msg_reset_timeout(state)) {
  192. kbd_lock_msg_reset(state, true);
  193. }
  194. /* if the maximum message size was reached or the
  195. * MESSAGE_COMPLETION_TIMEOUT has expired, retrieve a message and call
  196. * post_rx() */
  197. size_t avail = 0;
  198. while ((avail = subghz_tx_rx_worker_available(state->subghz_worker)) >
  199. 0) {
  200. volatile uint32_t since_last_rx = furi_get_tick() -
  201. state->last_time_rx_data;
  202. if (avail < RX_TX_BUFFER_SIZE && since_last_rx <
  203. MESSAGE_COMPLETION_TIMEOUT) {
  204. break;
  205. }
  206. size_t rx_size = subghz_tx_rx_worker_read(state->subghz_worker,
  207. state->rx_buffer, RX_TX_BUFFER_SIZE);
  208. post_rx(state, rx_size);
  209. }
  210. /* call scene manager */
  211. scene_manager_handle_tick_event(state->scene_manager);
  212. }
  213. /* Hooks into the view port's draw callback to overlay the keyboard locked
  214. * message. */
  215. static void esubghz_hooked_draw_callback(Canvas* canvas, void* context)
  216. {
  217. FURI_LOG_T(APPLICATION_NAME, "esubghz_hooked_draw_callback");
  218. furi_assert(canvas);
  219. furi_assert(context);
  220. ESubGhzChatState* state = context;
  221. /* call original callback */
  222. state->orig_draw_cb(canvas, state->view_dispatcher);
  223. /* display if the keyboard is locked */
  224. if (state->kbd_locked) {
  225. canvas_set_font(canvas, FontPrimary);
  226. elements_multiline_text_framed(canvas, 42, 30, "Locked");
  227. }
  228. /* display the unlock message if necessary */
  229. if (kbd_lock_msg_display(state)) {
  230. canvas_set_font(canvas, FontSecondary);
  231. elements_bold_rounded_frame(canvas, 14, 8, 99, 48);
  232. elements_multiline_text(canvas, 65, 26, "To unlock\npress:");
  233. canvas_draw_icon(canvas, 65, 42, &I_Pin_back_arrow_10x8);
  234. canvas_draw_icon(canvas, 80, 42, &I_Pin_back_arrow_10x8);
  235. canvas_draw_icon(canvas, 95, 42, &I_Pin_back_arrow_10x8);
  236. canvas_draw_icon(canvas, 16, 13, &I_WarningDolphin_45x42);
  237. }
  238. }
  239. /* Hooks into the view port's input callback to handle the user locking the
  240. * keyboard. */
  241. static void esubghz_hooked_input_callback(InputEvent* event, void* context)
  242. {
  243. FURI_LOG_T(APPLICATION_NAME, "esubghz_hooked_input_callback");
  244. furi_assert(event);
  245. furi_assert(context);
  246. ESubGhzChatState* state = context;
  247. /* if the keyboard is locked no key presses are forwarded */
  248. if (state->kbd_locked) {
  249. /* key has been pressed, display the unlock message and
  250. * initiate the timer */
  251. if (state->kbd_lock_count == 0) {
  252. state->kbd_lock_msg_ticks = furi_get_tick();
  253. }
  254. /* back button has been pressed, increase the lock counter */
  255. if (event->key == InputKeyBack && event->type ==
  256. InputTypeShort) {
  257. state->kbd_lock_count++;
  258. }
  259. /* unlock the keyboard */
  260. if (state->kbd_lock_count >= KBD_UNLOCK_CNT) {
  261. kbd_unlock(state);
  262. }
  263. /* do not handle the event */
  264. return;
  265. }
  266. if (event->key == InputKeyOk) {
  267. /* if we are in the chat view and no input is ongoing, allow
  268. * locking */
  269. if (state->view_dispatcher->current_view ==
  270. text_box_get_view(state->chat_box) &&
  271. !(state->kbd_ok_input_ongoing)) {
  272. /* lock keyboard upon long press of Ok button */
  273. if (event->type == InputTypeLong) {
  274. kbd_lock(state);
  275. }
  276. /* do not handle any Ok key events to prevent blocking
  277. * of other keys */
  278. return;
  279. }
  280. /* handle ongoing inputs when changing to chat view */
  281. if (event->type == InputTypePress) {
  282. state->kbd_ok_input_ongoing = true;
  283. } else if (event->type == InputTypeRelease) {
  284. state->kbd_ok_input_ongoing = false;
  285. }
  286. }
  287. if (event->key == InputKeyLeft) {
  288. /* if we are in the chat view and no input is ongoing, allow
  289. * switching to msg input */
  290. if (state->view_dispatcher->current_view ==
  291. text_box_get_view(state->chat_box) &&
  292. !(state->kbd_left_input_ongoing)) {
  293. /* go to msg input upon short press of Left button */
  294. if (event->type == InputTypeShort) {
  295. view_dispatcher_send_custom_event(state->view_dispatcher,
  296. ESubGhzChatEvent_GotoMsgInput);
  297. }
  298. /* do not handle any Left key events to prevent
  299. * blocking of other keys */
  300. return;
  301. }
  302. /* handle ongoing inputs when changing to chat view */
  303. if (event->type == InputTypePress) {
  304. state->kbd_left_input_ongoing = true;
  305. } else if (event->type == InputTypeRelease) {
  306. state->kbd_left_input_ongoing = false;
  307. }
  308. }
  309. if (event->key == InputKeyRight) {
  310. /* if we are in the chat view and no input is ongoing, allow
  311. * switching to key display */
  312. if (state->view_dispatcher->current_view ==
  313. text_box_get_view(state->chat_box) &&
  314. !(state->kbd_right_input_ongoing)) {
  315. /* go to key display upon short press of Right button
  316. */
  317. if (event->type == InputTypeShort) {
  318. view_dispatcher_send_custom_event(state->view_dispatcher,
  319. ESubGhzChatEvent_GotoKeyDisplay);
  320. }
  321. /* do not handle any Right key events to prevent
  322. * blocking of other keys */
  323. return;
  324. }
  325. /* handle ongoing inputs when changing to chat view */
  326. if (event->type == InputTypePress) {
  327. state->kbd_right_input_ongoing = true;
  328. } else if (event->type == InputTypeRelease) {
  329. state->kbd_right_input_ongoing = false;
  330. }
  331. }
  332. /* call original callback */
  333. state->orig_input_cb(event, state->view_dispatcher);
  334. }
  335. static bool helper_strings_alloc(ESubGhzChatState *state)
  336. {
  337. furi_assert(state);
  338. state->name_prefix = furi_string_alloc();
  339. if (state->name_prefix == NULL) {
  340. return false;
  341. }
  342. state->msg_input = furi_string_alloc();
  343. if (state->msg_input == NULL) {
  344. furi_string_free(state->name_prefix);
  345. return false;
  346. }
  347. return true;
  348. }
  349. static void helper_strings_free(ESubGhzChatState *state)
  350. {
  351. furi_assert(state);
  352. furi_string_free(state->name_prefix);
  353. furi_string_free(state->msg_input);
  354. }
  355. static bool chat_box_alloc(ESubGhzChatState *state)
  356. {
  357. furi_assert(state);
  358. state->chat_box = text_box_alloc();
  359. if (state->chat_box == NULL) {
  360. return false;
  361. }
  362. state->chat_box_store = furi_string_alloc();
  363. if (state->chat_box_store == NULL) {
  364. text_box_free(state->chat_box);
  365. return false;
  366. }
  367. furi_string_reserve(state->chat_box_store, CHAT_BOX_STORE_SIZE);
  368. furi_string_set_char(state->chat_box_store, 0, 0);
  369. text_box_set_text(state->chat_box,
  370. furi_string_get_cstr(state->chat_box_store));
  371. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  372. return true;
  373. }
  374. static void chat_box_free(ESubGhzChatState *state)
  375. {
  376. furi_assert(state);
  377. text_box_free(state->chat_box);
  378. furi_string_free(state->chat_box_store);
  379. }
  380. int32_t esubghz_chat(void)
  381. {
  382. /* init the crypto system */
  383. crypto_init();
  384. int32_t err = -1;
  385. FURI_LOG_I(APPLICATION_NAME, "Starting...");
  386. /* allocate necessary structs and buffers */
  387. ESubGhzChatState *state = malloc(sizeof(ESubGhzChatState));
  388. if (state == NULL) {
  389. goto err_alloc;
  390. }
  391. memset(state, 0, sizeof(*state));
  392. state->scene_manager = scene_manager_alloc(
  393. &esubghz_chat_scene_event_handlers, state);
  394. if (state->scene_manager == NULL) {
  395. goto err_alloc_sm;
  396. }
  397. state->view_dispatcher = view_dispatcher_alloc();
  398. if (state->view_dispatcher == NULL) {
  399. goto err_alloc_vd;
  400. }
  401. if (!helper_strings_alloc(state)) {
  402. goto err_alloc_hs;
  403. }
  404. state->menu = menu_alloc();
  405. if (state->menu == NULL) {
  406. goto err_alloc_menu;
  407. }
  408. state->text_input = text_input_alloc();
  409. if (state->text_input == NULL) {
  410. goto err_alloc_ti;
  411. }
  412. state->hex_key_input = byte_input_alloc();
  413. if (state->hex_key_input == NULL) {
  414. goto err_alloc_hki;
  415. }
  416. if (!chat_box_alloc(state)) {
  417. goto err_alloc_cb;
  418. }
  419. state->key_display = dialog_ex_alloc();
  420. if (state->key_display == NULL) {
  421. goto err_alloc_kd;
  422. }
  423. state->subghz_worker = subghz_tx_rx_worker_alloc();
  424. if (state->subghz_worker == NULL) {
  425. goto err_alloc_worker;
  426. }
  427. state->crypto_ctx = crypto_ctx_alloc();
  428. if (state->crypto_ctx == NULL) {
  429. goto err_alloc_crypto;
  430. }
  431. /* set the have_read callback of the Sub-GHz worker */
  432. subghz_tx_rx_worker_set_callback_have_read(state->subghz_worker,
  433. have_read_cb, state);
  434. /* enter suppress charge mode */
  435. furi_hal_power_suppress_charge_enter();
  436. /* init internal device */
  437. subghz_devices_init();
  438. state->subghz_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
  439. /* set chat name prefix */
  440. furi_string_printf(state->name_prefix, "%s",
  441. furi_hal_version_get_name_ptr());
  442. /* get notification record, we use this to make the flipper vibrate */
  443. /* no error handling here, don't know how */
  444. state->notification = furi_record_open(RECORD_NOTIFICATION);
  445. /* hook into the view port's draw and input callbacks */
  446. state->orig_draw_cb = state->view_dispatcher->view_port->draw_callback;
  447. state->orig_input_cb = state->view_dispatcher->view_port->input_callback;
  448. view_port_draw_callback_set(state->view_dispatcher->view_port,
  449. esubghz_hooked_draw_callback, state);
  450. view_port_input_callback_set(state->view_dispatcher->view_port,
  451. esubghz_hooked_input_callback, state);
  452. view_dispatcher_enable_queue(state->view_dispatcher);
  453. /* set callbacks for view dispatcher */
  454. view_dispatcher_set_event_callback_context(state->view_dispatcher, state);
  455. view_dispatcher_set_custom_event_callback(
  456. state->view_dispatcher,
  457. esubghz_chat_custom_event_callback);
  458. view_dispatcher_set_navigation_event_callback(
  459. state->view_dispatcher,
  460. esubghz_chat_navigation_event_callback);
  461. view_dispatcher_set_tick_event_callback(
  462. state->view_dispatcher,
  463. esubghz_chat_tick_event_callback,
  464. TICK_INTERVAL);
  465. /* add our two views to the view dispatcher */
  466. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Menu,
  467. menu_get_view(state->menu));
  468. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Input,
  469. text_input_get_view(state->text_input));
  470. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_HexKeyInput,
  471. byte_input_get_view(state->hex_key_input));
  472. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_ChatBox,
  473. text_box_get_view(state->chat_box));
  474. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_KeyDisplay,
  475. dialog_ex_get_view(state->key_display));
  476. /* get the GUI record and attach the view dispatcher to the GUI */
  477. /* no error handling here, don't know how */
  478. Gui *gui = furi_record_open(RECORD_GUI);
  479. view_dispatcher_attach_to_gui(state->view_dispatcher, gui,
  480. ViewDispatcherTypeFullscreen);
  481. /* switch to the frequency input scene */
  482. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_FreqInput);
  483. /* run the view dispatcher, this call only returns when we close the
  484. * application */
  485. view_dispatcher_run(state->view_dispatcher);
  486. /* if it is running, stop the Sub-GHz worker */
  487. if (subghz_tx_rx_worker_is_running(state->subghz_worker)) {
  488. exit_chat(state);
  489. subghz_tx_rx_worker_stop(state->subghz_worker);
  490. }
  491. err = 0;
  492. /* close GUI record */
  493. furi_record_close(RECORD_GUI);
  494. /* remove our two views from the view dispatcher */
  495. view_dispatcher_remove_view(state->view_dispatcher,
  496. ESubGhzChatView_Menu);
  497. view_dispatcher_remove_view(state->view_dispatcher,
  498. ESubGhzChatView_Input);
  499. view_dispatcher_remove_view(state->view_dispatcher,
  500. ESubGhzChatView_HexKeyInput);
  501. view_dispatcher_remove_view(state->view_dispatcher,
  502. ESubGhzChatView_ChatBox);
  503. view_dispatcher_remove_view(state->view_dispatcher,
  504. ESubGhzChatView_KeyDisplay);
  505. /* close notification record */
  506. furi_record_close(RECORD_NOTIFICATION);
  507. /* clear the key and potential password */
  508. crypto_explicit_bzero(state->text_input_store,
  509. sizeof(state->text_input_store));
  510. crypto_explicit_bzero(state->hex_key_input_store,
  511. sizeof(state->hex_key_input_store));
  512. crypto_explicit_bzero(state->key_hex_str, sizeof(state->key_hex_str));
  513. crypto_ctx_clear(state->crypto_ctx);
  514. /* deinit devices */
  515. subghz_devices_deinit();
  516. /* exit suppress charge mode */
  517. furi_hal_power_suppress_charge_exit();
  518. /* free everything we allocated */
  519. crypto_ctx_free(state->crypto_ctx);
  520. err_alloc_crypto:
  521. subghz_tx_rx_worker_free(state->subghz_worker);
  522. err_alloc_worker:
  523. dialog_ex_free(state->key_display);
  524. err_alloc_kd:
  525. chat_box_free(state);
  526. err_alloc_cb:
  527. byte_input_free(state->hex_key_input);
  528. err_alloc_hki:
  529. text_input_free(state->text_input);
  530. err_alloc_ti:
  531. menu_free(state->menu);
  532. err_alloc_menu:
  533. helper_strings_free(state);
  534. err_alloc_hs:
  535. view_dispatcher_free(state->view_dispatcher);
  536. err_alloc_vd:
  537. scene_manager_free(state->scene_manager);
  538. err_alloc_sm:
  539. free(state);
  540. err_alloc:
  541. if (err != 0) {
  542. FURI_LOG_E(APPLICATION_NAME, "Failed to launch (alloc error)!");
  543. } else {
  544. FURI_LOG_I(APPLICATION_NAME, "Clean exit.");
  545. }
  546. return err;
  547. }