Просмотр исходного кода

Create files before sending command so that buffer doesn't fill up too fast, show last saved log when starting up log viewer

0xchocolate 2 лет назад
Родитель
Сommit
9dc7e04270

+ 18 - 34
applications/external/wifi_marauder_companion/scenes/wifi_marauder_scene_console_output.c

@@ -4,15 +4,8 @@ void wifi_marauder_console_output_handle_rx_data_cb(uint8_t* buf, size_t len, vo
     furi_assert(context);
     WifiMarauderApp* app = context;
 
-    if(app->ok_to_save_logs) {
-        if(!app->is_writing_log) {
-            app->is_writing_log = true;
-            app->has_saved_logs_this_session = true;
-            if(!app->log_file || !storage_file_is_open(app->log_file)) {
-                wifi_marauder_create_log_file(app);
-            }
-        }
-
+    if(app->is_writing_log) {
+        app->has_saved_logs_this_session = true;
         storage_file_write(app->log_file, buf, len);
     }
 
@@ -33,19 +26,6 @@ void wifi_marauder_console_output_handle_rx_packets_cb(uint8_t* buf, size_t len,
     furi_assert(context);
     WifiMarauderApp* app = context;
 
-    if(!app->ok_to_save_pcaps) {
-        // user has declined this feature
-        return;
-    }
-
-    // If it is a sniff function, open the pcap file for recording
-    if(strncmp("sniff", app->selected_tx_string, strlen("sniff")) == 0 && !app->is_writing_pcap) {
-        app->is_writing_pcap = true;
-        if(!app->capture_file || !storage_file_is_open(app->capture_file)) {
-            wifi_marauder_create_pcap_file(app);
-        }
-    }
-
     if(app->is_writing_pcap) {
         storage_file_write(app->capture_file, buf, len);
     }
@@ -78,18 +58,8 @@ void wifi_marauder_scene_console_output_on_enter(void* context) {
         }
     }
 
-    // Set starting text - for "View Log", this will just be what was already in the text box store
+    // Set starting text - for "View Log from end", this will just be what was already in the text box store
     text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
-    if(furi_string_empty(app->text_box_store)) {
-        char help_msg[256];
-        snprintf(
-            help_msg,
-            sizeof(help_msg),
-            "The log is empty! :(\nTry sending a command?\n\nSaving pcaps to flipper sdcard: %s\nSaving logs to flipper sdcard: %s",
-            app->ok_to_save_pcaps ? "ON" : "OFF",
-            app->ok_to_save_logs ? "ON" : "OFF");
-        text_box_set_text(app->text_box, help_msg);
-    }
 
     scene_manager_set_scene_state(app->scene_manager, WifiMarauderSceneConsoleOutput, 0);
     view_dispatcher_switch_to_view(app->view_dispatcher, WifiMarauderAppViewConsoleOutput);
@@ -102,8 +72,22 @@ void wifi_marauder_scene_console_output_on_enter(void* context) {
         app->lp_uart,
         wifi_marauder_console_output_handle_rx_packets_cb); // setup callback for packets rx thread
 
-    // Send command with newline '\n'
+    // Get ready to send command
     if(app->is_command && app->selected_tx_string) {
+        // Create files *before* sending command
+        // (it takes time to iterate through the directory)
+        if(app->ok_to_save_logs) {
+            app->is_writing_log = true;
+            wifi_marauder_create_log_file(app);
+        }
+
+        // If it is a sniff function, open the pcap file for recording
+        if(app->ok_to_save_pcaps && strncmp("sniff", app->selected_tx_string, strlen("sniff")) == 0) {
+            app->is_writing_pcap = true;
+            wifi_marauder_create_pcap_file(app);
+        }
+
+        // Send command with newline '\n'
         wifi_marauder_uart_tx(
             (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
         wifi_marauder_uart_tx((uint8_t*)("\n"), 1);

+ 19 - 1
applications/external/wifi_marauder_companion/scenes/wifi_marauder_scene_log_viewer.c

@@ -111,7 +111,25 @@ void wifi_marauder_scene_log_viewer_on_enter(void* context) {
 
     app->open_log_file_page = 0;
     app->open_log_file_num_pages = 0;
-    wifi_marauder_scene_log_viewer_setup_widget(app, false);
+    bool saved_logs_exist = false;
+    if (!app->has_saved_logs_this_session && furi_string_empty(app->text_box_store)) {
+        // no commands sent yet this session, find last saved log
+        if (storage_dir_open(app->log_file, MARAUDER_APP_FOLDER_LOGS)) {
+            char name[70];
+            char lastname[70];
+            while (storage_dir_read(app->log_file, NULL, name, sizeof(name))) {
+                // keep reading directory until last file is reached
+                strlcpy(lastname, name, sizeof(lastname));
+                saved_logs_exist = true;
+            }
+            if (saved_logs_exist) {
+                snprintf(app->log_file_path, sizeof(app->log_file_path), "%s/%s", MARAUDER_APP_FOLDER_LOGS, lastname);
+            }
+        }
+        storage_dir_close(app->log_file);
+    }
+
+    wifi_marauder_scene_log_viewer_setup_widget(app, saved_logs_exist);
 
     view_dispatcher_switch_to_view(app->view_dispatcher, WifiMarauderAppViewWidget);
 }