evil_portal_app.c 4.8 KB

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