blackhat_app.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. // Var item list for script
  50. app->script_item_list = variable_item_list_alloc();
  51. view_dispatcher_add_view(
  52. app->view_dispatcher,
  53. BlackhatAppViewScriptItemList,
  54. variable_item_list_get_view(app->script_item_list)
  55. );
  56. app->text_input = text_input_alloc();
  57. view_dispatcher_add_view(
  58. app->view_dispatcher,
  59. BlackhatAppViewTextInput,
  60. text_input_get_view(app->text_input)
  61. );
  62. for (int i = 0; i < NUM_MENU_ITEMS; ++i) {
  63. app->selected_option_index[i] = 0;
  64. }
  65. app->text_box = text_box_alloc();
  66. view_dispatcher_add_view(
  67. app->view_dispatcher,
  68. BlackhatAppViewConsoleOutput,
  69. text_box_get_view(app->text_box)
  70. );
  71. app->script_text = malloc(BLACKHAT_TEXT_BOX_STORE_SIZE);
  72. app->script_text_ptr = 0;
  73. app->text_box_store = furi_string_alloc();
  74. furi_string_reserve(app->text_box_store, BLACKHAT_TEXT_BOX_STORE_SIZE);
  75. scene_manager_next_scene(app->scene_manager, BlackhatSceneStart);
  76. return app;
  77. }
  78. void blackhat_app_free(BlackhatApp* app)
  79. {
  80. furi_assert(app);
  81. // Views
  82. view_dispatcher_remove_view(
  83. app->view_dispatcher, BlackhatAppViewVarItemList
  84. );
  85. view_dispatcher_remove_view(
  86. app->view_dispatcher, BlackhatAppViewScriptItemList
  87. );
  88. variable_item_list_free(app->script_item_list);
  89. variable_item_list_free(app->var_item_list);
  90. view_dispatcher_remove_view(app->view_dispatcher, BlackhatAppViewTextInput);
  91. text_input_free(app->text_input);
  92. view_dispatcher_remove_view(
  93. app->view_dispatcher, BlackhatAppViewConsoleOutput
  94. );
  95. text_box_free(app->text_box);
  96. furi_string_free(app->text_box_store);
  97. // View dispatcher
  98. view_dispatcher_free(app->view_dispatcher);
  99. scene_manager_free(app->scene_manager);
  100. blackhat_uart_free(app->uart);
  101. // Close records
  102. furi_record_close(RECORD_GUI);
  103. furi_record_close(RECORD_DIALOGS);
  104. free(app);
  105. }
  106. int32_t blackhat_app(void* p)
  107. {
  108. UNUSED(p);
  109. // Disable expansion protocol to avoid interference with UART Handle
  110. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  111. expansion_disable(expansion);
  112. BlackhatApp* blackhat_app = blackhat_app_alloc();
  113. blackhat_app->scanned = false;
  114. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  115. // turn off 5v, so it gets reset on startup
  116. if (otg_was_enabled) {
  117. furi_hal_power_disable_otg();
  118. }
  119. uint8_t attempts = 0;
  120. while (!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  121. furi_hal_power_enable_otg();
  122. furi_delay_ms(10);
  123. }
  124. furi_delay_ms(200);
  125. blackhat_app->uart = blackhat_uart_init(blackhat_app);
  126. view_dispatcher_run(blackhat_app->view_dispatcher);
  127. blackhat_app_free(blackhat_app);
  128. if (furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  129. furi_hal_power_disable_otg();
  130. }
  131. // Return previous state of expansion
  132. expansion_enable(expansion);
  133. furi_record_close(RECORD_EXPANSION);
  134. return 0;
  135. }