esubghz_chat.c 20 KB

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