esubghz_chat.c 21 KB

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