esubghz_chat.c 18 KB

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