esubghz_chat.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. #include "bgloader_api.h"
  7. #define CHAT_LEAVE_DELAY 10
  8. #define TICK_INTERVAL 50
  9. #define MESSAGE_COMPLETION_TIMEOUT 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. static void esubghz_chat_check_messages(ESubGhzChatState *state)
  211. {
  212. /* if the maximum message size was reached or the
  213. * MESSAGE_COMPLETION_TIMEOUT has expired, retrieve a message and call
  214. * post_rx() */
  215. size_t avail = 0;
  216. while ((avail = subghz_tx_rx_worker_available(state->subghz_worker)) >
  217. 0) {
  218. volatile uint32_t since_last_rx = furi_get_tick() -
  219. state->last_time_rx_data;
  220. if (avail < RX_TX_BUFFER_SIZE && since_last_rx <
  221. MESSAGE_COMPLETION_TIMEOUT) {
  222. break;
  223. }
  224. size_t rx_size = subghz_tx_rx_worker_read(state->subghz_worker,
  225. state->rx_buffer, RX_TX_BUFFER_SIZE);
  226. post_rx(state, rx_size);
  227. }
  228. }
  229. /* Tick event callback for view dispatcher. Called every TICK_INTERVAL. Resets
  230. * the locked message if necessary. Retrieves a received message from the
  231. * Sub-GHz worker and calls post_rx(). Then calls the scene manager. */
  232. static void esubghz_chat_tick_event_callback(void* context)
  233. {
  234. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_tick_event_callback");
  235. furi_assert(context);
  236. ESubGhzChatState* state = context;
  237. /* reset locked message if necessary */
  238. if (kbd_lock_msg_reset_timeout(state)) {
  239. kbd_lock_msg_reset(state, true);
  240. }
  241. esubghz_chat_check_messages(state);
  242. /* call scene manager */
  243. scene_manager_handle_tick_event(state->scene_manager);
  244. }
  245. /* Hooks into the view port's draw callback to overlay the keyboard locked
  246. * message. */
  247. static void esubghz_hooked_draw_callback(Canvas* canvas, void* context)
  248. {
  249. FURI_LOG_T(APPLICATION_NAME, "esubghz_hooked_draw_callback");
  250. furi_assert(canvas);
  251. furi_assert(context);
  252. ESubGhzChatState* state = context;
  253. /* call original callback */
  254. state->orig_draw_cb(canvas, state->view_dispatcher);
  255. /* display if the keyboard is locked */
  256. if (state->kbd_locked) {
  257. canvas_set_font(canvas, FontPrimary);
  258. elements_multiline_text_framed(canvas, 42, 30, "Locked");
  259. }
  260. /* display the unlock message if necessary */
  261. if (kbd_lock_msg_display(state)) {
  262. canvas_set_font(canvas, FontSecondary);
  263. elements_bold_rounded_frame(canvas, 14, 8, 99, 48);
  264. elements_multiline_text(canvas, 65, 26, "To unlock\npress:");
  265. canvas_draw_icon(canvas, 65, 42, &I_Pin_back_arrow_10x8);
  266. canvas_draw_icon(canvas, 80, 42, &I_Pin_back_arrow_10x8);
  267. canvas_draw_icon(canvas, 95, 42, &I_Pin_back_arrow_10x8);
  268. canvas_draw_icon(canvas, 16, 13, &I_WarningDolphin_45x42);
  269. }
  270. }
  271. /* Hooks into the view port's input callback to handle the user locking the
  272. * keyboard. */
  273. static void esubghz_hooked_input_callback(InputEvent* event, void* context)
  274. {
  275. FURI_LOG_T(APPLICATION_NAME, "esubghz_hooked_input_callback");
  276. furi_assert(event);
  277. furi_assert(context);
  278. ESubGhzChatState* state = context;
  279. /* if the keyboard is locked no key presses are forwarded */
  280. if (state->kbd_locked) {
  281. /* key has been pressed, display the unlock message and
  282. * initiate the timer */
  283. if (state->kbd_lock_count == 0) {
  284. state->kbd_lock_msg_ticks = furi_get_tick();
  285. }
  286. /* back button has been pressed, increase the lock counter */
  287. if (event->key == InputKeyBack && event->type ==
  288. InputTypeShort) {
  289. state->kbd_lock_count++;
  290. }
  291. /* unlock the keyboard */
  292. if (state->kbd_lock_count >= KBD_UNLOCK_CNT) {
  293. kbd_unlock(state);
  294. }
  295. /* do not handle the event */
  296. return;
  297. }
  298. /* handle long press of back key to exit for real */
  299. if (event->key == InputKeyBack) {
  300. if (state->view_dispatcher->current_view ==
  301. text_input_get_view(state->text_input)) {
  302. if (event->type == InputTypeLong) {
  303. state->exit_for_real = true;
  304. view_dispatcher_stop(state->view_dispatcher);
  305. return;
  306. }
  307. }
  308. }
  309. if (event->key == InputKeyOk) {
  310. /* if we are in the chat view and no input is ongoing, allow
  311. * locking */
  312. if (state->view_dispatcher->current_view ==
  313. text_box_get_view(state->chat_box) &&
  314. !(state->kbd_ok_input_ongoing)) {
  315. /* lock keyboard upon long press of Ok button */
  316. if (event->type == InputTypeLong) {
  317. kbd_lock(state);
  318. }
  319. /* do not handle any Ok key events to prevent blocking
  320. * of other keys */
  321. return;
  322. }
  323. /* handle ongoing inputs when changing to chat view */
  324. if (event->type == InputTypePress) {
  325. state->kbd_ok_input_ongoing = true;
  326. } else if (event->type == InputTypeRelease) {
  327. state->kbd_ok_input_ongoing = false;
  328. }
  329. }
  330. if (event->key == InputKeyLeft) {
  331. /* if we are in the chat view and no input is ongoing, allow
  332. * switching to msg input */
  333. if (state->view_dispatcher->current_view ==
  334. text_box_get_view(state->chat_box) &&
  335. !(state->kbd_left_input_ongoing)) {
  336. /* go to msg input upon short press of Left button */
  337. if (event->type == InputTypeShort) {
  338. view_dispatcher_send_custom_event(state->view_dispatcher,
  339. ESubGhzChatEvent_GotoMsgInput);
  340. }
  341. /* do not handle any Left key events to prevent
  342. * blocking of other keys */
  343. return;
  344. }
  345. /* handle ongoing inputs when changing to chat view */
  346. if (event->type == InputTypePress) {
  347. state->kbd_left_input_ongoing = true;
  348. } else if (event->type == InputTypeRelease) {
  349. state->kbd_left_input_ongoing = false;
  350. }
  351. }
  352. if (event->key == InputKeyRight) {
  353. /* if we are in the chat view and no input is ongoing, allow
  354. * switching to key display */
  355. if (state->view_dispatcher->current_view ==
  356. text_box_get_view(state->chat_box) &&
  357. !(state->kbd_right_input_ongoing)) {
  358. /* go to key display upon short press of Right button
  359. */
  360. if (event->type == InputTypeShort) {
  361. view_dispatcher_send_custom_event(state->view_dispatcher,
  362. ESubGhzChatEvent_GotoKeyDisplay);
  363. }
  364. /* do not handle any Right key events to prevent
  365. * blocking of other keys */
  366. return;
  367. }
  368. /* handle ongoing inputs when changing to chat view */
  369. if (event->type == InputTypePress) {
  370. state->kbd_right_input_ongoing = true;
  371. } else if (event->type == InputTypeRelease) {
  372. state->kbd_right_input_ongoing = false;
  373. }
  374. }
  375. /* call original callback */
  376. state->orig_input_cb(event, state->view_dispatcher);
  377. }
  378. static const char *esubghz_get_bgloader_app_path(const char *args)
  379. {
  380. size_t base_args_len = strlen(APP_BASE_ARGS);
  381. return (args + base_args_len + 1);
  382. }
  383. static bool esubghz_run_with_bgloader(const char *args)
  384. {
  385. size_t base_args_len = strlen(APP_BASE_ARGS);
  386. if (args == NULL) {
  387. return false;
  388. }
  389. if (strncmp(args, APP_BASE_ARGS, base_args_len) != 0) {
  390. return false;
  391. }
  392. if (strlen(args) < base_args_len + 2) {
  393. return false;
  394. }
  395. if (args[base_args_len] != ':') {
  396. return false;
  397. }
  398. const char *app_path = esubghz_get_bgloader_app_path(args);
  399. return furi_record_exists(app_path);
  400. }
  401. static void esubghz_attach_to_gui(ESubGhzChatState *state)
  402. {
  403. Gui *gui = furi_record_open(RECORD_GUI);
  404. view_dispatcher_attach_to_gui(state->view_dispatcher, gui,
  405. ViewDispatcherTypeFullscreen);
  406. }
  407. static void esubghz_detach_from_gui(ESubGhzChatState *state)
  408. {
  409. gui_remove_view_port(state->view_dispatcher->gui,
  410. state->view_dispatcher->view_port);
  411. state->view_dispatcher->gui = NULL;
  412. furi_record_close(RECORD_GUI);
  413. }
  414. static void esubghz_bgloader_loop(ESubGhzChatState *state, const char
  415. *bg_app_path)
  416. {
  417. while (true) {
  418. view_dispatcher_run(state->view_dispatcher);
  419. if (state->exit_for_real) {
  420. /* exit for real */
  421. break;
  422. }
  423. BGLoaderApp *bg_app = furi_record_open(bg_app_path);
  424. /* signal loader that we're ready to go to background */
  425. BGLoaderMessage msg;
  426. msg.type = BGLoaderMessageType_LoaderBackground;
  427. furi_check(furi_message_queue_put(bg_app->to_loader, &msg,
  428. FuriWaitForever) == FuriStatusOk);
  429. esubghz_detach_from_gui(state);
  430. while (true) {
  431. /* wait for loader to wake us up again */
  432. if (furi_message_queue_get(bg_app->to_app, &msg,
  433. TICK_INTERVAL) != FuriStatusOk)
  434. {
  435. /* check for messages on timeout */
  436. esubghz_chat_check_messages(state);
  437. continue;
  438. }
  439. if (msg.type == BGLoaderMessageType_AppReattached) {
  440. break;
  441. } else {
  442. furi_check(0);
  443. }
  444. }
  445. furi_record_close(bg_app_path);
  446. esubghz_attach_to_gui(state);
  447. }
  448. }
  449. static bool helper_strings_alloc(ESubGhzChatState *state)
  450. {
  451. furi_assert(state);
  452. state->name_prefix = furi_string_alloc();
  453. if (state->name_prefix == NULL) {
  454. return false;
  455. }
  456. state->msg_input = furi_string_alloc();
  457. if (state->msg_input == NULL) {
  458. furi_string_free(state->name_prefix);
  459. return false;
  460. }
  461. return true;
  462. }
  463. static void helper_strings_free(ESubGhzChatState *state)
  464. {
  465. furi_assert(state);
  466. furi_string_free(state->name_prefix);
  467. furi_string_free(state->msg_input);
  468. }
  469. static bool chat_box_alloc(ESubGhzChatState *state)
  470. {
  471. furi_assert(state);
  472. state->chat_box = text_box_alloc();
  473. if (state->chat_box == NULL) {
  474. return false;
  475. }
  476. state->chat_box_store = furi_string_alloc();
  477. if (state->chat_box_store == NULL) {
  478. text_box_free(state->chat_box);
  479. return false;
  480. }
  481. furi_string_reserve(state->chat_box_store, CHAT_BOX_STORE_SIZE);
  482. furi_string_set_char(state->chat_box_store, 0, 0);
  483. text_box_set_text(state->chat_box,
  484. furi_string_get_cstr(state->chat_box_store));
  485. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  486. return true;
  487. }
  488. static void chat_box_free(ESubGhzChatState *state)
  489. {
  490. furi_assert(state);
  491. text_box_free(state->chat_box);
  492. furi_string_free(state->chat_box_store);
  493. }
  494. int32_t esubghz_chat(const char *args)
  495. {
  496. /* init the crypto system */
  497. crypto_init();
  498. int32_t err = -1;
  499. FURI_LOG_I(APPLICATION_NAME, "Starting...");
  500. /* allocate necessary structs and buffers */
  501. ESubGhzChatState *state = malloc(sizeof(ESubGhzChatState));
  502. if (state == NULL) {
  503. goto err_alloc;
  504. }
  505. memset(state, 0, sizeof(*state));
  506. state->scene_manager = scene_manager_alloc(
  507. &esubghz_chat_scene_event_handlers, state);
  508. if (state->scene_manager == NULL) {
  509. goto err_alloc_sm;
  510. }
  511. state->view_dispatcher = view_dispatcher_alloc();
  512. if (state->view_dispatcher == NULL) {
  513. goto err_alloc_vd;
  514. }
  515. if (!helper_strings_alloc(state)) {
  516. goto err_alloc_hs;
  517. }
  518. state->menu = menu_alloc();
  519. if (state->menu == NULL) {
  520. goto err_alloc_menu;
  521. }
  522. state->text_input = text_input_alloc();
  523. if (state->text_input == NULL) {
  524. goto err_alloc_ti;
  525. }
  526. state->hex_key_input = byte_input_alloc();
  527. if (state->hex_key_input == NULL) {
  528. goto err_alloc_hki;
  529. }
  530. if (!chat_box_alloc(state)) {
  531. goto err_alloc_cb;
  532. }
  533. state->key_display = dialog_ex_alloc();
  534. if (state->key_display == NULL) {
  535. goto err_alloc_kd;
  536. }
  537. state->nfc_popup = popup_alloc();
  538. if (state->nfc_popup == NULL) {
  539. goto err_alloc_np;
  540. }
  541. state->subghz_worker = subghz_tx_rx_worker_alloc();
  542. if (state->subghz_worker == NULL) {
  543. goto err_alloc_worker;
  544. }
  545. state->nfc_worker = nfc_worker_alloc();
  546. if (state->nfc_worker == NULL) {
  547. goto err_alloc_nworker;
  548. }
  549. state->nfc_dev_data = malloc(sizeof(NfcDeviceData));
  550. if (state->nfc_dev_data == NULL) {
  551. goto err_alloc_ndevdata;
  552. }
  553. memset(state->nfc_dev_data, 0, sizeof(NfcDeviceData));
  554. state->crypto_ctx = crypto_ctx_alloc();
  555. if (state->crypto_ctx == NULL) {
  556. goto err_alloc_crypto;
  557. }
  558. /* set the default frequency */
  559. state->frequency = DEFAULT_FREQ;
  560. /* in the first few views there is no background support */
  561. state->exit_for_real = true;
  562. /* set the have_read callback of the Sub-GHz worker */
  563. subghz_tx_rx_worker_set_callback_have_read(state->subghz_worker,
  564. have_read_cb, state);
  565. /* enter suppress charge mode */
  566. furi_hal_power_suppress_charge_enter();
  567. /* init internal device */
  568. subghz_devices_init();
  569. state->subghz_device = radio_device_loader_set(state->subghz_device,
  570. SubGhzRadioDeviceTypeExternalCC1101);
  571. subghz_devices_reset(state->subghz_device);
  572. subghz_devices_idle(state->subghz_device);
  573. /* set chat name prefix */
  574. furi_string_printf(state->name_prefix, "%s",
  575. furi_hal_version_get_name_ptr());
  576. /* get notification record, we use this to make the flipper vibrate */
  577. /* no error handling here, don't know how */
  578. state->notification = furi_record_open(RECORD_NOTIFICATION);
  579. /* hook into the view port's draw and input callbacks */
  580. state->orig_draw_cb = state->view_dispatcher->view_port->draw_callback;
  581. state->orig_input_cb = state->view_dispatcher->view_port->input_callback;
  582. view_port_draw_callback_set(state->view_dispatcher->view_port,
  583. esubghz_hooked_draw_callback, state);
  584. view_port_input_callback_set(state->view_dispatcher->view_port,
  585. esubghz_hooked_input_callback, state);
  586. view_dispatcher_enable_queue(state->view_dispatcher);
  587. /* set callbacks for view dispatcher */
  588. view_dispatcher_set_event_callback_context(state->view_dispatcher, state);
  589. view_dispatcher_set_custom_event_callback(
  590. state->view_dispatcher,
  591. esubghz_chat_custom_event_callback);
  592. view_dispatcher_set_navigation_event_callback(
  593. state->view_dispatcher,
  594. esubghz_chat_navigation_event_callback);
  595. view_dispatcher_set_tick_event_callback(
  596. state->view_dispatcher,
  597. esubghz_chat_tick_event_callback,
  598. TICK_INTERVAL);
  599. /* add our two views to the view dispatcher */
  600. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Menu,
  601. menu_get_view(state->menu));
  602. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Input,
  603. text_input_get_view(state->text_input));
  604. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_HexKeyInput,
  605. byte_input_get_view(state->hex_key_input));
  606. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_ChatBox,
  607. text_box_get_view(state->chat_box));
  608. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_KeyDisplay,
  609. dialog_ex_get_view(state->key_display));
  610. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_NfcPopup,
  611. popup_get_view(state->nfc_popup));
  612. /* get the GUI record and attach the view dispatcher to the GUI */
  613. /* no error handling here, don't know how */
  614. Gui *gui = furi_record_open(RECORD_GUI);
  615. view_dispatcher_attach_to_gui(state->view_dispatcher, gui,
  616. ViewDispatcherTypeFullscreen);
  617. /* switch to the key menu scene */
  618. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_KeyMenu);
  619. /* run the view dispatcher, this call only returns when we close the
  620. * application */
  621. if (!esubghz_run_with_bgloader(args)) {
  622. view_dispatcher_run(state->view_dispatcher);
  623. } else {
  624. const char *bg_app_path = esubghz_get_bgloader_app_path(args);
  625. esubghz_bgloader_loop(state, bg_app_path);
  626. }
  627. /* if it is running, stop the Sub-GHz worker */
  628. if (subghz_tx_rx_worker_is_running(state->subghz_worker)) {
  629. exit_chat(state);
  630. subghz_tx_rx_worker_stop(state->subghz_worker);
  631. }
  632. /* if it is running, stop the NFC worker */
  633. nfc_worker_stop(state->nfc_worker);
  634. err = 0;
  635. /* close GUI record */
  636. furi_record_close(RECORD_GUI);
  637. /* remove our two views from the view dispatcher */
  638. view_dispatcher_remove_view(state->view_dispatcher,
  639. ESubGhzChatView_Menu);
  640. view_dispatcher_remove_view(state->view_dispatcher,
  641. ESubGhzChatView_Input);
  642. view_dispatcher_remove_view(state->view_dispatcher,
  643. ESubGhzChatView_HexKeyInput);
  644. view_dispatcher_remove_view(state->view_dispatcher,
  645. ESubGhzChatView_ChatBox);
  646. view_dispatcher_remove_view(state->view_dispatcher,
  647. ESubGhzChatView_KeyDisplay);
  648. view_dispatcher_remove_view(state->view_dispatcher,
  649. ESubGhzChatView_NfcPopup);
  650. /* close notification record */
  651. furi_record_close(RECORD_NOTIFICATION);
  652. /* clear the key and potential password */
  653. crypto_explicit_bzero(state->text_input_store,
  654. sizeof(state->text_input_store));
  655. crypto_explicit_bzero(state->hex_key_input_store,
  656. sizeof(state->hex_key_input_store));
  657. crypto_explicit_bzero(state->key_hex_str, sizeof(state->key_hex_str));
  658. crypto_ctx_clear(state->crypto_ctx);
  659. /* clear nfc data */
  660. if (state->nfc_dev_data->parsed_data != NULL) {
  661. furi_string_free(state->nfc_dev_data->parsed_data);
  662. }
  663. crypto_explicit_bzero(state->nfc_dev_data, sizeof(NfcDeviceData));
  664. /* deinit devices */
  665. radio_device_loader_end(state->subghz_device);
  666. subghz_devices_deinit();
  667. /* exit suppress charge mode */
  668. furi_hal_power_suppress_charge_exit();
  669. /* free everything we allocated */
  670. crypto_ctx_free(state->crypto_ctx);
  671. err_alloc_crypto:
  672. free(state->nfc_dev_data);
  673. err_alloc_ndevdata:
  674. nfc_worker_free(state->nfc_worker);
  675. err_alloc_nworker:
  676. subghz_tx_rx_worker_free(state->subghz_worker);
  677. err_alloc_worker:
  678. popup_free(state->nfc_popup);
  679. err_alloc_np:
  680. dialog_ex_free(state->key_display);
  681. err_alloc_kd:
  682. chat_box_free(state);
  683. err_alloc_cb:
  684. byte_input_free(state->hex_key_input);
  685. err_alloc_hki:
  686. text_input_free(state->text_input);
  687. err_alloc_ti:
  688. menu_free(state->menu);
  689. err_alloc_menu:
  690. helper_strings_free(state);
  691. err_alloc_hs:
  692. view_dispatcher_free(state->view_dispatcher);
  693. err_alloc_vd:
  694. scene_manager_free(state->scene_manager);
  695. err_alloc_sm:
  696. free(state);
  697. err_alloc:
  698. if (err != 0) {
  699. FURI_LOG_E(APPLICATION_NAME, "Failed to launch (alloc error)!");
  700. } else {
  701. FURI_LOG_I(APPLICATION_NAME, "Clean exit.");
  702. }
  703. return err;
  704. }