wifi_marauder_app.c 7.4 KB

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