esubghz_chat.c 22 KB

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