evil_portal_app.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "evil_portal_app_i.h"
  2. #include "helpers/evil_portal_storage.h"
  3. #include <furi.h>
  4. #include <furi_hal.h>
  5. #include <expansion/expansion.h>
  6. static bool evil_portal_app_custom_event_callback(void* context, uint32_t event) {
  7. furi_assert(context);
  8. Evil_PortalApp* app = context;
  9. return scene_manager_handle_custom_event(app->scene_manager, event);
  10. }
  11. static bool evil_portal_app_back_event_callback(void* context) {
  12. furi_assert(context);
  13. Evil_PortalApp* app = context;
  14. return scene_manager_handle_back_event(app->scene_manager);
  15. }
  16. static void evil_portal_app_tick_event_callback(void* context) {
  17. furi_assert(context);
  18. Evil_PortalApp* app = context;
  19. scene_manager_handle_tick_event(app->scene_manager);
  20. }
  21. Evil_PortalApp* evil_portal_app_alloc() {
  22. Evil_PortalApp* app = malloc(sizeof(Evil_PortalApp));
  23. app->sent_reset = false;
  24. app->portal_logs = furi_string_alloc();
  25. app->portal_logs_mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
  26. app->capture_line = false;
  27. app->captured_line = furi_string_alloc();
  28. app->dialogs = furi_record_open(RECORD_DIALOGS);
  29. app->file_path = furi_string_alloc();
  30. app->gui = furi_record_open(RECORD_GUI);
  31. app->view_dispatcher = view_dispatcher_alloc();
  32. app->loading = loading_alloc();
  33. app->scene_manager = scene_manager_alloc(&evil_portal_scene_handlers, app);
  34. view_dispatcher_enable_queue(app->view_dispatcher);
  35. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  36. view_dispatcher_set_custom_event_callback(
  37. app->view_dispatcher, evil_portal_app_custom_event_callback);
  38. view_dispatcher_set_navigation_event_callback(
  39. app->view_dispatcher, evil_portal_app_back_event_callback);
  40. view_dispatcher_set_tick_event_callback(
  41. app->view_dispatcher, evil_portal_app_tick_event_callback, 100);
  42. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  43. app->view_stack = view_stack_alloc();
  44. app->var_item_list = variable_item_list_alloc();
  45. view_dispatcher_add_view(
  46. app->view_dispatcher,
  47. Evil_PortalAppViewVarItemList,
  48. variable_item_list_get_view(app->var_item_list));
  49. app->text_input = text_input_alloc();
  50. view_dispatcher_add_view(
  51. app->view_dispatcher, Evil_PortalAppViewTextInput, text_input_get_view(app->text_input));
  52. for(int i = 0; i < NUM_MENU_ITEMS; ++i) {
  53. app->selected_option_index[i] = 0;
  54. }
  55. app->text_box = text_box_alloc();
  56. view_dispatcher_add_view(
  57. app->view_dispatcher, Evil_PortalAppViewConsoleOutput, text_box_get_view(app->text_box));
  58. app->text_box_store = furi_string_alloc();
  59. furi_string_reserve(app->text_box_store, EVIL_PORTAL_TEXT_BOX_STORE_SIZE);
  60. scene_manager_next_scene(app->scene_manager, Evil_PortalSceneStart);
  61. return app;
  62. }
  63. void evil_portal_app_free(Evil_PortalApp* app) {
  64. // Send reset event to dev board
  65. evil_portal_uart_tx(
  66. app->uart, (uint8_t*)(RESET_CMD "\nstopscan\n"), strlen(RESET_CMD "\nstopscan\n"));
  67. furi_assert(app);
  68. // Views
  69. view_dispatcher_remove_view(app->view_dispatcher, Evil_PortalAppViewVarItemList);
  70. view_dispatcher_remove_view(app->view_dispatcher, Evil_PortalAppViewConsoleOutput);
  71. text_box_free(app->text_box);
  72. furi_string_free(app->text_box_store);
  73. text_input_free(app->text_input);
  74. view_stack_free(app->view_stack);
  75. loading_free(app->loading);
  76. // View dispatcher
  77. view_dispatcher_free(app->view_dispatcher);
  78. scene_manager_free(app->scene_manager);
  79. evil_portal_uart_free(app->uart);
  80. // save latest logs
  81. furi_mutex_acquire(app->portal_logs_mutex, FuriWaitForever);
  82. if(furi_string_size(app->portal_logs) > 0) {
  83. write_logs(app->portal_logs);
  84. furi_string_free(app->portal_logs);
  85. }
  86. furi_mutex_release(app->portal_logs_mutex);
  87. furi_mutex_free(app->portal_logs_mutex);
  88. furi_string_free(app->captured_line);
  89. // Close records
  90. furi_record_close(RECORD_GUI);
  91. furi_record_close(RECORD_DIALOGS);
  92. furi_string_free(app->file_path);
  93. free(app);
  94. }
  95. int32_t evil_portal_app(void* p) {
  96. UNUSED(p);
  97. // Disable expansion protocol to avoid interference with UART Handle
  98. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  99. expansion_disable(expansion);
  100. Evil_PortalApp* evil_portal_app = evil_portal_app_alloc();
  101. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  102. // turn off 5v, so it gets reset on startup
  103. if(otg_was_enabled) {
  104. furi_hal_power_disable_otg();
  105. }
  106. uint8_t attempts = 0;
  107. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  108. furi_hal_power_enable_otg();
  109. furi_delay_ms(10);
  110. }
  111. furi_delay_ms(200);
  112. evil_portal_app->uart = evil_portal_uart_init(evil_portal_app);
  113. view_dispatcher_run(evil_portal_app->view_dispatcher);
  114. evil_portal_app_free(evil_portal_app);
  115. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  116. furi_hal_power_disable_otg();
  117. }
  118. // Return previous state of expansion
  119. expansion_enable(expansion);
  120. furi_record_close(RECORD_EXPANSION);
  121. return 0;
  122. }