wifi_marauder_app.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "wifi_marauder_app_i.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <expansion/expansion.h>
  5. static bool wifi_marauder_app_custom_event_callback(void* context, uint32_t event) {
  6. furi_assert(context);
  7. WifiMarauderApp* app = context;
  8. return scene_manager_handle_custom_event(app->scene_manager, event);
  9. }
  10. static bool wifi_marauder_app_back_event_callback(void* context) {
  11. furi_assert(context);
  12. WifiMarauderApp* app = context;
  13. return scene_manager_handle_back_event(app->scene_manager);
  14. }
  15. static void wifi_marauder_app_tick_event_callback(void* context) {
  16. furi_assert(context);
  17. WifiMarauderApp* app = context;
  18. scene_manager_handle_tick_event(app->scene_manager);
  19. }
  20. WifiMarauderApp* wifi_marauder_app_alloc() {
  21. WifiMarauderApp* app = malloc(sizeof(WifiMarauderApp));
  22. app->gui = furi_record_open(RECORD_GUI);
  23. app->dialogs = furi_record_open(RECORD_DIALOGS);
  24. app->storage = furi_record_open(RECORD_STORAGE);
  25. app->capture_file = storage_file_alloc(app->storage);
  26. app->log_file = storage_file_alloc(app->storage);
  27. app->save_pcap_setting_file = storage_file_alloc(app->storage);
  28. app->save_logs_setting_file = storage_file_alloc(app->storage);
  29. app->view_dispatcher = view_dispatcher_alloc();
  30. app->scene_manager = scene_manager_alloc(&wifi_marauder_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, wifi_marauder_app_custom_event_callback);
  34. view_dispatcher_set_navigation_event_callback(
  35. app->view_dispatcher, wifi_marauder_app_back_event_callback);
  36. view_dispatcher_set_tick_event_callback(
  37. app->view_dispatcher, wifi_marauder_app_tick_event_callback, 100);
  38. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  39. app->var_item_list = variable_item_list_alloc();
  40. view_dispatcher_add_view(
  41. app->view_dispatcher,
  42. WifiMarauderAppViewVarItemList,
  43. variable_item_list_get_view(app->var_item_list));
  44. for(int i = 0; i < NUM_MENU_ITEMS; ++i) {
  45. app->selected_option_index[i] = 0;
  46. }
  47. app->special_case_input_step = 0;
  48. app->text_box = text_box_alloc();
  49. view_dispatcher_add_view(
  50. app->view_dispatcher, WifiMarauderAppViewConsoleOutput, text_box_get_view(app->text_box));
  51. app->text_box_store = furi_string_alloc();
  52. furi_string_reserve(app->text_box_store, WIFI_MARAUDER_TEXT_BOX_STORE_SIZE);
  53. app->text_input = text_input_alloc();
  54. view_dispatcher_add_view(
  55. app->view_dispatcher, WifiMarauderAppViewTextInput, text_input_get_view(app->text_input));
  56. app->widget = widget_alloc();
  57. view_dispatcher_add_view(
  58. app->view_dispatcher, WifiMarauderAppViewWidget, widget_get_view(app->widget));
  59. app->has_saved_logs_this_session = false;
  60. // if user hasn't confirmed whether to save pcaps and logs to sdcard, then prompt when scene starts
  61. app->need_to_prompt_settings_init =
  62. (!storage_file_exists(app->storage, SAVE_PCAP_SETTING_FILEPATH) ||
  63. !storage_file_exists(app->storage, SAVE_LOGS_SETTING_FILEPATH));
  64. // Submenu
  65. app->submenu = submenu_alloc();
  66. view_dispatcher_add_view(
  67. app->view_dispatcher, WifiMarauderAppViewSubmenu, submenu_get_view(app->submenu));
  68. scene_manager_next_scene(app->scene_manager, WifiMarauderSceneStart);
  69. return app;
  70. }
  71. void wifi_marauder_make_app_folder(WifiMarauderApp* app) {
  72. furi_assert(app);
  73. if(!storage_simply_mkdir(app->storage, MARAUDER_APP_FOLDER)) {
  74. dialog_message_show_storage_error(app->dialogs, "Cannot create\napp folder");
  75. }
  76. if(!storage_simply_mkdir(app->storage, MARAUDER_APP_FOLDER_PCAPS)) {
  77. dialog_message_show_storage_error(app->dialogs, "Cannot create\npcaps folder");
  78. }
  79. if(!storage_simply_mkdir(app->storage, MARAUDER_APP_FOLDER_DUMPS)) {
  80. dialog_message_show_storage_error(app->dialogs, "Cannot create\ndumps folder");
  81. }
  82. if(!storage_simply_mkdir(app->storage, MARAUDER_APP_FOLDER_LOGS)) {
  83. dialog_message_show_storage_error(app->dialogs, "Cannot create\nlogs folder");
  84. }
  85. if(!storage_simply_mkdir(app->storage, MARAUDER_APP_FOLDER_SCRIPTS)) {
  86. dialog_message_show_storage_error(app->dialogs, "Cannot create\nscripts folder");
  87. }
  88. if(!storage_simply_mkdir(app->storage, MARAUDER_APP_FOLDER_HTML)) {
  89. dialog_message_show_storage_error(app->dialogs, "Cannot create\nhtml folder");
  90. }
  91. }
  92. void wifi_marauder_load_settings(WifiMarauderApp* app) {
  93. if(storage_file_open(
  94. app->save_pcap_setting_file,
  95. SAVE_PCAP_SETTING_FILEPATH,
  96. FSAM_READ,
  97. FSOM_OPEN_EXISTING)) {
  98. char ok[1];
  99. storage_file_read(app->save_pcap_setting_file, ok, sizeof(ok));
  100. app->ok_to_save_pcaps = ok[0] == 'Y';
  101. }
  102. storage_file_close(app->save_pcap_setting_file);
  103. if(storage_file_open(
  104. app->save_logs_setting_file,
  105. SAVE_LOGS_SETTING_FILEPATH,
  106. FSAM_READ,
  107. FSOM_OPEN_EXISTING)) {
  108. char ok[1];
  109. storage_file_read(app->save_logs_setting_file, ok, sizeof(ok));
  110. app->ok_to_save_logs = ok[0] == 'Y';
  111. }
  112. storage_file_close(app->save_logs_setting_file);
  113. }
  114. void wifi_marauder_app_free(WifiMarauderApp* app) {
  115. furi_assert(app);
  116. // Views
  117. view_dispatcher_remove_view(app->view_dispatcher, WifiMarauderAppViewVarItemList);
  118. view_dispatcher_remove_view(app->view_dispatcher, WifiMarauderAppViewConsoleOutput);
  119. view_dispatcher_remove_view(app->view_dispatcher, WifiMarauderAppViewTextInput);
  120. view_dispatcher_remove_view(app->view_dispatcher, WifiMarauderAppViewWidget);
  121. view_dispatcher_remove_view(app->view_dispatcher, WifiMarauderAppViewSubmenu);
  122. widget_free(app->widget);
  123. text_box_free(app->text_box);
  124. furi_string_free(app->text_box_store);
  125. text_input_free(app->text_input);
  126. submenu_free(app->submenu);
  127. variable_item_list_free(app->var_item_list);
  128. storage_file_free(app->capture_file);
  129. storage_file_free(app->log_file);
  130. storage_file_free(app->save_pcap_setting_file);
  131. storage_file_free(app->save_logs_setting_file);
  132. // View dispatcher
  133. view_dispatcher_free(app->view_dispatcher);
  134. scene_manager_free(app->scene_manager);
  135. wifi_marauder_uart_free(app->uart);
  136. // Close records
  137. furi_record_close(RECORD_GUI);
  138. furi_record_close(RECORD_STORAGE);
  139. furi_record_close(RECORD_DIALOGS);
  140. free(app);
  141. }
  142. int32_t wifi_marauder_app(void* p) {
  143. UNUSED(p);
  144. // Disable expansion protocol to avoid interference with UART Handle
  145. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  146. expansion_disable(expansion);
  147. uint8_t attempts = 0;
  148. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  149. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  150. furi_hal_power_enable_otg();
  151. furi_delay_ms(10);
  152. }
  153. furi_delay_ms(200);
  154. WifiMarauderApp* wifi_marauder_app = wifi_marauder_app_alloc();
  155. wifi_marauder_make_app_folder(wifi_marauder_app);
  156. wifi_marauder_load_settings(wifi_marauder_app);
  157. wifi_marauder_app->uart = wifi_marauder_usart_init(wifi_marauder_app);
  158. for(int i = 0; i < 2; i++) {
  159. furi_delay_ms(500);
  160. wifi_marauder_uart_tx(wifi_marauder_app->uart, (uint8_t[1]){'w'}, 1);
  161. }
  162. furi_delay_ms(1);
  163. view_dispatcher_run(wifi_marauder_app->view_dispatcher);
  164. wifi_marauder_app_free(wifi_marauder_app);
  165. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  166. furi_hal_power_disable_otg();
  167. }
  168. // Return previous state of expansion
  169. expansion_enable(expansion);
  170. furi_record_close(RECORD_EXPANSION);
  171. return 0;
  172. }