wifi_deauther_app.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "wifi_deauther_app_i.h"
  2. #include <furi_hal_power.h>
  3. #include <furi.h>
  4. #include <furi_hal.h>
  5. #include <expansion/expansion.h>
  6. static bool wifi_deauther_app_custom_event_callback(void* context, uint32_t event) {
  7. furi_assert(context);
  8. WifideautherApp* app = context;
  9. return scene_manager_handle_custom_event(app->scene_manager, event);
  10. }
  11. static bool wifi_deauther_app_back_event_callback(void* context) {
  12. furi_assert(context);
  13. WifideautherApp* app = context;
  14. return scene_manager_handle_back_event(app->scene_manager);
  15. }
  16. static void wifi_deauther_app_tick_event_callback(void* context) {
  17. furi_assert(context);
  18. WifideautherApp* app = context;
  19. scene_manager_handle_tick_event(app->scene_manager);
  20. }
  21. WifideautherApp* wifi_deauther_app_alloc() {
  22. WifideautherApp* app = malloc(sizeof(WifideautherApp));
  23. app->gui = furi_record_open(RECORD_GUI);
  24. app->view_dispatcher = view_dispatcher_alloc();
  25. app->scene_manager = scene_manager_alloc(&wifi_deauther_scene_handlers, app);
  26. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  27. view_dispatcher_set_custom_event_callback(
  28. app->view_dispatcher, wifi_deauther_app_custom_event_callback);
  29. view_dispatcher_set_navigation_event_callback(
  30. app->view_dispatcher, wifi_deauther_app_back_event_callback);
  31. view_dispatcher_set_tick_event_callback(
  32. app->view_dispatcher, wifi_deauther_app_tick_event_callback, 100);
  33. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  34. app->var_item_list = variable_item_list_alloc();
  35. view_dispatcher_add_view(
  36. app->view_dispatcher,
  37. WifideautherAppViewVarItemList,
  38. variable_item_list_get_view(app->var_item_list));
  39. for(int i = 0; i < NUM_MENU_ITEMS; ++i) {
  40. app->selected_option_index[i] = 0;
  41. }
  42. app->text_box = text_box_alloc();
  43. view_dispatcher_add_view(
  44. app->view_dispatcher, WifideautherAppViewConsoleOutput, text_box_get_view(app->text_box));
  45. app->text_box_store = furi_string_alloc();
  46. furi_string_reserve(app->text_box_store, WIFI_deauther_TEXT_BOX_STORE_SIZE);
  47. app->text_input = text_input_alloc();
  48. view_dispatcher_add_view(
  49. app->view_dispatcher, WifideautherAppViewTextInput, text_input_get_view(app->text_input));
  50. scene_manager_next_scene(app->scene_manager, WifideautherSceneStart);
  51. return app;
  52. }
  53. void wifi_deauther_app_free(WifideautherApp* app) {
  54. furi_assert(app);
  55. // Views
  56. view_dispatcher_remove_view(app->view_dispatcher, WifideautherAppViewVarItemList);
  57. view_dispatcher_remove_view(app->view_dispatcher, WifideautherAppViewConsoleOutput);
  58. view_dispatcher_remove_view(app->view_dispatcher, WifideautherAppViewTextInput);
  59. text_box_free(app->text_box);
  60. furi_string_free(app->text_box_store);
  61. text_input_free(app->text_input);
  62. // View dispatcher
  63. view_dispatcher_free(app->view_dispatcher);
  64. scene_manager_free(app->scene_manager);
  65. wifi_deauther_uart_free(app->uart);
  66. // Close records
  67. furi_record_close(RECORD_GUI);
  68. free(app);
  69. }
  70. int32_t wifi_deauther_app(void* p) {
  71. UNUSED(p);
  72. // Disable expansion protocol to avoid interference with UART Handle
  73. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  74. expansion_disable(expansion);
  75. // Enable 5v power, multiple attempts to avoid issues with power chip protection false triggering
  76. uint8_t attempts = 0;
  77. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  78. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  79. furi_hal_power_enable_otg();
  80. furi_delay_ms(10);
  81. }
  82. furi_delay_ms(600);
  83. WifideautherApp* wifi_deauther_app = wifi_deauther_app_alloc();
  84. wifi_deauther_app->uart = wifi_deauther_uart_init(wifi_deauther_app);
  85. view_dispatcher_run(wifi_deauther_app->view_dispatcher);
  86. wifi_deauther_app_free(wifi_deauther_app);
  87. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  88. furi_hal_power_disable_otg();
  89. }
  90. // Return previous state of expansion
  91. expansion_enable(expansion);
  92. furi_record_close(RECORD_EXPANSION);
  93. return 0;
  94. }