esubghz_chat.c 21 KB

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