blackhat_app.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <expansion/expansion.h>
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include "blackhat_app_i.h"
  5. static bool blackhat_app_custom_event_callback(void* context, uint32_t event)
  6. {
  7. furi_assert(context);
  8. BlackhatApp* app = context;
  9. return scene_manager_handle_custom_event(app->scene_manager, event);
  10. }
  11. static bool blackhat_app_back_event_callback(void* context)
  12. {
  13. furi_assert(context);
  14. BlackhatApp* app = context;
  15. return scene_manager_handle_back_event(app->scene_manager);
  16. }
  17. static void blackhat_app_tick_event_callback(void* context)
  18. {
  19. furi_assert(context);
  20. BlackhatApp* app = context;
  21. scene_manager_handle_tick_event(app->scene_manager);
  22. }
  23. BlackhatApp* blackhat_app_alloc()
  24. {
  25. BlackhatApp* app = malloc(sizeof(BlackhatApp));
  26. app->dialogs = furi_record_open(RECORD_DIALOGS);
  27. app->gui = furi_record_open(RECORD_GUI);
  28. app->view_dispatcher = view_dispatcher_alloc();
  29. app->scene_manager = scene_manager_alloc(&blackhat_scene_handlers, app);
  30. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  31. view_dispatcher_set_custom_event_callback(
  32. app->view_dispatcher, blackhat_app_custom_event_callback
  33. );
  34. view_dispatcher_set_navigation_event_callback(
  35. app->view_dispatcher, blackhat_app_back_event_callback
  36. );
  37. view_dispatcher_set_tick_event_callback(
  38. app->view_dispatcher, blackhat_app_tick_event_callback, 100
  39. );
  40. view_dispatcher_attach_to_gui(
  41. app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen
  42. );
  43. app->var_item_list = variable_item_list_alloc();
  44. view_dispatcher_add_view(
  45. app->view_dispatcher,
  46. BlackhatAppViewVarItemList,
  47. variable_item_list_get_view(app->var_item_list)
  48. );
  49. app->text_input = text_input_alloc();
  50. view_dispatcher_add_view(
  51. app->view_dispatcher,
  52. BlackhatAppViewTextInput,
  53. text_input_get_view(app->text_input)
  54. );
  55. for (int i = 0; i < NUM_MENU_ITEMS; ++i) {
  56. app->selected_option_index[i] = 0;
  57. }
  58. app->text_box = text_box_alloc();
  59. view_dispatcher_add_view(
  60. app->view_dispatcher,
  61. BlackhatAppViewConsoleOutput,
  62. text_box_get_view(app->text_box)
  63. );
  64. app->text_box_store = furi_string_alloc();
  65. furi_string_reserve(app->text_box_store, BLACKHAT_TEXT_BOX_STORE_SIZE);
  66. scene_manager_next_scene(app->scene_manager, BlackhatSceneStart);
  67. return app;
  68. }
  69. void blackhat_app_free(BlackhatApp* app)
  70. {
  71. furi_assert(app);
  72. // Views
  73. view_dispatcher_remove_view(
  74. app->view_dispatcher, BlackhatAppViewVarItemList
  75. );
  76. variable_item_list_free(app->var_item_list);
  77. view_dispatcher_remove_view(app->view_dispatcher, BlackhatAppViewTextInput);
  78. text_input_free(app->text_input);
  79. view_dispatcher_remove_view(
  80. app->view_dispatcher, BlackhatAppViewConsoleOutput
  81. );
  82. text_box_free(app->text_box);
  83. furi_string_free(app->text_box_store);
  84. // View dispatcher
  85. view_dispatcher_free(app->view_dispatcher);
  86. scene_manager_free(app->scene_manager);
  87. blackhat_uart_free(app->uart);
  88. // Close records
  89. furi_record_close(RECORD_GUI);
  90. furi_record_close(RECORD_DIALOGS);
  91. free(app);
  92. }
  93. int32_t blackhat_app(void* p)
  94. {
  95. UNUSED(p);
  96. // Disable expansion protocol to avoid interference with UART Handle
  97. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  98. expansion_disable(expansion);
  99. BlackhatApp* blackhat_app = blackhat_app_alloc();
  100. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  101. // turn off 5v, so it gets reset on startup
  102. if (otg_was_enabled) {
  103. furi_hal_power_disable_otg();
  104. }
  105. uint8_t attempts = 0;
  106. while (!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  107. furi_hal_power_enable_otg();
  108. furi_delay_ms(10);
  109. }
  110. furi_delay_ms(200);
  111. blackhat_app->uart = blackhat_uart_init(blackhat_app);
  112. view_dispatcher_run(blackhat_app->view_dispatcher);
  113. blackhat_app_free(blackhat_app);
  114. if (furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  115. furi_hal_power_disable_otg();
  116. }
  117. // Return previous state of expansion
  118. expansion_enable(expansion);
  119. furi_record_close(RECORD_EXPANSION);
  120. return 0;
  121. }