blackhat_app.c 4.9 KB

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