esubghz_chat.c 19 KB

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