wifi_deauther_app.c 4.2 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_enable_queue(app->view_dispatcher);
  27. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  28. view_dispatcher_set_custom_event_callback(
  29. app->view_dispatcher, wifi_deauther_app_custom_event_callback);
  30. view_dispatcher_set_navigation_event_callback(
  31. app->view_dispatcher, wifi_deauther_app_back_event_callback);
  32. view_dispatcher_set_tick_event_callback(
  33. app->view_dispatcher, wifi_deauther_app_tick_event_callback, 100);
  34. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  35. app->var_item_list = variable_item_list_alloc();
  36. view_dispatcher_add_view(
  37. app->view_dispatcher,
  38. WifideautherAppViewVarItemList,
  39. variable_item_list_get_view(app->var_item_list));
  40. for(int i = 0; i < NUM_MENU_ITEMS; ++i) {
  41. app->selected_option_index[i] = 0;
  42. }
  43. app->text_box = text_box_alloc();
  44. view_dispatcher_add_view(
  45. app->view_dispatcher, WifideautherAppViewConsoleOutput, text_box_get_view(app->text_box));
  46. app->text_box_store = furi_string_alloc();
  47. furi_string_reserve(app->text_box_store, WIFI_deauther_TEXT_BOX_STORE_SIZE);
  48. app->text_input = text_input_alloc();
  49. view_dispatcher_add_view(
  50. app->view_dispatcher, WifideautherAppViewTextInput, text_input_get_view(app->text_input));
  51. scene_manager_next_scene(app->scene_manager, WifideautherSceneStart);
  52. return app;
  53. }
  54. void wifi_deauther_app_free(WifideautherApp* app) {
  55. furi_assert(app);
  56. // Views
  57. view_dispatcher_remove_view(app->view_dispatcher, WifideautherAppViewVarItemList);
  58. view_dispatcher_remove_view(app->view_dispatcher, WifideautherAppViewConsoleOutput);
  59. view_dispatcher_remove_view(app->view_dispatcher, WifideautherAppViewTextInput);
  60. text_box_free(app->text_box);
  61. furi_string_free(app->text_box_store);
  62. text_input_free(app->text_input);
  63. // View dispatcher
  64. view_dispatcher_free(app->view_dispatcher);
  65. scene_manager_free(app->scene_manager);
  66. wifi_deauther_uart_free(app->uart);
  67. // Close records
  68. furi_record_close(RECORD_GUI);
  69. free(app);
  70. }
  71. int32_t wifi_deauther_app(void* p) {
  72. UNUSED(p);
  73. // Disable expansion protocol to avoid interference with UART Handle
  74. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  75. expansion_disable(expansion);
  76. // Enable 5v power, multiple attempts to avoid issues with power chip protection false triggering
  77. uint8_t attempts = 0;
  78. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  79. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  80. furi_hal_power_enable_otg();
  81. furi_delay_ms(10);
  82. }
  83. furi_delay_ms(600);
  84. WifideautherApp* wifi_deauther_app = wifi_deauther_app_alloc();
  85. wifi_deauther_app->uart = wifi_deauther_uart_init(wifi_deauther_app);
  86. view_dispatcher_run(wifi_deauther_app->view_dispatcher);
  87. wifi_deauther_app_free(wifi_deauther_app);
  88. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  89. furi_hal_power_disable_otg();
  90. }
  91. // Return previous state of expansion
  92. expansion_enable(expansion);
  93. furi_record_close(RECORD_EXPANSION);
  94. return 0;
  95. }