evil_portal_app.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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,
  6. 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_html = false;
  24. app->sent_ap = false;
  25. app->has_command_queue = false;
  26. app->command_index = 0;
  27. app->portal_logs = malloc(5000);
  28. app->gui = furi_record_open(RECORD_GUI);
  29. app->view_dispatcher = view_dispatcher_alloc();
  30. app->scene_manager = scene_manager_alloc(&evil_portal_scene_handlers, app);
  31. view_dispatcher_enable_queue(app->view_dispatcher);
  32. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  33. view_dispatcher_set_custom_event_callback(
  34. app->view_dispatcher, evil_portal_app_custom_event_callback);
  35. view_dispatcher_set_navigation_event_callback(
  36. app->view_dispatcher, evil_portal_app_back_event_callback);
  37. view_dispatcher_set_tick_event_callback(
  38. app->view_dispatcher, evil_portal_app_tick_event_callback, 100);
  39. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui,
  40. ViewDispatcherTypeFullscreen);
  41. app->var_item_list = variable_item_list_alloc();
  42. view_dispatcher_add_view(app->view_dispatcher, Evil_PortalAppViewVarItemList,
  43. variable_item_list_get_view(app->var_item_list));
  44. for (int i = 0; i < NUM_MENU_ITEMS; ++i) {
  45. app->selected_option_index[i] = 0;
  46. }
  47. app->text_box = text_box_alloc();
  48. view_dispatcher_add_view(app->view_dispatcher,
  49. Evil_PortalAppViewConsoleOutput,
  50. text_box_get_view(app->text_box));
  51. app->text_box_store = furi_string_alloc();
  52. furi_string_reserve(app->text_box_store, EVIL_PORTAL_TEXT_BOX_STORE_SIZE);
  53. app->text_input = uart_text_input_alloc();
  54. view_dispatcher_add_view(app->view_dispatcher, Evil_PortalAppViewTextInput,
  55. uart_text_input_get_view(app->text_input));
  56. app->text_input = uart_text_input_alloc();
  57. view_dispatcher_add_view(app->view_dispatcher, Evil_PortalAppViewStartPortal,
  58. uart_text_input_get_view(app->text_input));
  59. scene_manager_next_scene(app->scene_manager, Evil_PortalSceneStart);
  60. return app;
  61. }
  62. void evil_portal_app_free(Evil_PortalApp *app) {
  63. // save latest logs
  64. if (strlen(app->portal_logs) > 0) {
  65. write_logs(app->portal_logs);
  66. }
  67. // Send reset event to dev board
  68. evil_portal_uart_tx((uint8_t *)("reset"), strlen("reset"));
  69. evil_portal_uart_tx((uint8_t *)("\n"), 1);
  70. furi_assert(app);
  71. // Views
  72. view_dispatcher_remove_view(app->view_dispatcher,
  73. Evil_PortalAppViewVarItemList);
  74. view_dispatcher_remove_view(app->view_dispatcher,
  75. Evil_PortalAppViewConsoleOutput);
  76. view_dispatcher_remove_view(app->view_dispatcher,
  77. Evil_PortalAppViewTextInput);
  78. view_dispatcher_remove_view(app->view_dispatcher,
  79. Evil_PortalAppViewStartPortal);
  80. text_box_free(app->text_box);
  81. furi_string_free(app->text_box_store);
  82. uart_text_input_free(app->text_input);
  83. // View dispatcher
  84. view_dispatcher_free(app->view_dispatcher);
  85. scene_manager_free(app->scene_manager);
  86. evil_portal_uart_free(app->uart);
  87. // Close records
  88. furi_record_close(RECORD_GUI);
  89. free(app);
  90. }
  91. int32_t evil_portal_app(void *p) {
  92. UNUSED(p);
  93. Evil_PortalApp *evil_portal_app = evil_portal_app_alloc();
  94. evil_portal_app->uart = evil_portal_uart_init(evil_portal_app);
  95. view_dispatcher_run(evil_portal_app->view_dispatcher);
  96. evil_portal_app_free(evil_portal_app);
  97. return 0;
  98. }