evil_portal_app.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "evil_portal_app_i.h"
  2. #include "helpers/evil_portal_speaker.h"
  3. #include "helpers/evil_portal_storage.h"
  4. #include <furi.h>
  5. #include <furi_hal.h>
  6. static bool evil_portal_app_custom_event_callback(void *context,
  7. uint32_t event) {
  8. furi_assert(context);
  9. Evil_PortalApp *app = context;
  10. return scene_manager_handle_custom_event(app->scene_manager, event);
  11. }
  12. static bool evil_portal_app_back_event_callback(void *context) {
  13. furi_assert(context);
  14. Evil_PortalApp *app = context;
  15. return scene_manager_handle_back_event(app->scene_manager);
  16. }
  17. static void evil_portal_app_tick_event_callback(void *context) {
  18. furi_assert(context);
  19. Evil_PortalApp *app = context;
  20. scene_manager_handle_tick_event(app->scene_manager);
  21. }
  22. Evil_PortalApp *evil_portal_app_alloc() {
  23. Evil_PortalApp *app = malloc(sizeof(Evil_PortalApp));
  24. app->sent_html = false;
  25. app->sent_ap = false;
  26. app->has_command_queue = false;
  27. app->command_index = 0;
  28. app->portal_logs = malloc(5000);
  29. app->gui = furi_record_open(RECORD_GUI);
  30. app->view_dispatcher = view_dispatcher_alloc();
  31. app->scene_manager = scene_manager_alloc(&evil_portal_scene_handlers, app);
  32. view_dispatcher_enable_queue(app->view_dispatcher);
  33. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  34. view_dispatcher_set_custom_event_callback(
  35. app->view_dispatcher, evil_portal_app_custom_event_callback);
  36. view_dispatcher_set_navigation_event_callback(
  37. app->view_dispatcher, evil_portal_app_back_event_callback);
  38. view_dispatcher_set_tick_event_callback(
  39. app->view_dispatcher, evil_portal_app_tick_event_callback, 100);
  40. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui,
  41. ViewDispatcherTypeFullscreen);
  42. app->var_item_list = variable_item_list_alloc();
  43. view_dispatcher_add_view(app->view_dispatcher, Evil_PortalAppViewVarItemList,
  44. variable_item_list_get_view(app->var_item_list));
  45. for (int i = 0; i < NUM_MENU_ITEMS; ++i) {
  46. app->selected_option_index[i] = 0;
  47. }
  48. app->text_box = text_box_alloc();
  49. view_dispatcher_add_view(app->view_dispatcher,
  50. Evil_PortalAppViewConsoleOutput,
  51. text_box_get_view(app->text_box));
  52. app->text_box_store = furi_string_alloc();
  53. furi_string_reserve(app->text_box_store, EVIL_PORTAL_TEXT_BOX_STORE_SIZE);
  54. scene_manager_next_scene(app->scene_manager, Evil_PortalSceneStart);
  55. return app;
  56. }
  57. void evil_portal_app_free(Evil_PortalApp *app) {
  58. // save latest logs
  59. if (strlen(app->portal_logs) > 0) {
  60. write_logs(app->portal_logs);
  61. }
  62. // Send reset event to dev board
  63. evil_portal_uart_tx((uint8_t *)("reset"), strlen("reset"));
  64. evil_portal_uart_tx((uint8_t *)("\n"), 1);
  65. furi_assert(app);
  66. // Views
  67. view_dispatcher_remove_view(app->view_dispatcher,
  68. Evil_PortalAppViewVarItemList);
  69. view_dispatcher_remove_view(app->view_dispatcher,
  70. Evil_PortalAppViewConsoleOutput);
  71. text_box_free(app->text_box);
  72. furi_string_free(app->text_box_store);
  73. // View dispatcher
  74. view_dispatcher_free(app->view_dispatcher);
  75. scene_manager_free(app->scene_manager);
  76. evil_portal_uart_free(app->uart);
  77. // Close records
  78. furi_record_close(RECORD_GUI);
  79. free(app);
  80. }
  81. int32_t evil_portal_app(void *p) {
  82. UNUSED(p);
  83. Evil_PortalApp *evil_portal_app = evil_portal_app_alloc();
  84. evil_portal_app->uart = evil_portal_uart_init(evil_portal_app);
  85. view_dispatcher_run(evil_portal_app->view_dispatcher);
  86. // crashing here
  87. evil_portal_app_free(evil_portal_app);
  88. return 0;
  89. }