Skip to content

Commit

Permalink
Merge pull request #11 from jaylikesbunda/main
Browse files Browse the repository at this point in the history
v1.0.8
  • Loading branch information
jaylikesbunda authored Nov 3, 2024
2 parents 9d3870d + 985867a commit f28e785
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 194 deletions.
6 changes: 1 addition & 5 deletions callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,7 @@ void show_app_info(void* context) {
FURI_LOG_D("AppInfo", "Show app info called, context: %p", app);

const char* info_text =
"Ghost ESP v1.0.5\n\n"
"Created by: Spooky\n"
"Special Thanks:\n"
"- Jay Candel\n"
"Built with <3";
"";

if(app && app->confirmation_view) {
// Create a new context for the confirmation dialog
Expand Down
13 changes: 13 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@
## v1.0.7a
- Disable the expansion interface before trying to use UART

## v1.0.8

### 🔴 CRITICAL FIX - PCAP capture
- Fixed PCAP file handling and storage system
- Resolved PCAP file stream corruption issues
- Added proper storage system initialization
- Removed the line buffering logic for PCAP data

### Improvements
- Added error checking for storage operations
- Filtering majorly improved
- Improved stop on back to be much more reliable by added type-specific stop commands with delays between operations

## TODO
- Replaced select a utility text with prompt to show NEW Help Menu
- IMPROVE optional filtering to UART output
Expand Down
61 changes: 45 additions & 16 deletions menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,18 +816,15 @@ void submenu_callback(void* context, uint32_t index) {

bool back_event_callback(void* context) {
AppState* state = (AppState*)context;

// Get the current view ID
uint32_t current_view = state->current_view;

// Do not consume the back button event if the current view is the confirmation view
if(current_view == 7) { // 7 is the ID for the confirmation view
// Allow the confirmation view's input callback to handle the back button
// Allow confirmation view to handle its own back button
if(current_view == 7) {
return false;
}

if(current_view == 5) { // Text box view
FURI_LOG_D("Ghost ESP", "Stopping Thread");
FURI_LOG_D("Ghost ESP", "Stopping Operations");

// Cleanup text buffer
if(state->textBoxBuffer) {
Expand All @@ -839,28 +836,61 @@ bool back_event_callback(void* context) {
state->buffer_length = 0;
}

// Only send stop commands if enabled in settings
// Send stop commands if enabled in settings
if(state->settings.stop_on_back_index) {
// Send all relevant stop commands
// First stop any packet captures to ensure proper file saving
send_uart_command("capture -stop\n", state);
furi_delay_ms(100); // Give time for file operation

// Reset PCAP state if needed
if(state->uart_context->pcap) {
state->uart_context->pcap = false;
furi_stream_buffer_reset(state->uart_context->pcap_stream);
furi_delay_ms(50); // Allow buffer cleanup
}

// Then stop various operations in a logical order
send_uart_command("stopscan\n", state);
furi_delay_ms(50);

send_uart_command("stopspam\n", state);
furi_delay_ms(50);

send_uart_command("stopdeauth\n", state);
furi_delay_ms(50);

send_uart_command("stopportal\n", state);
furi_delay_ms(50);

send_uart_command("blescan -s\n", state);
furi_delay_ms(50);

// Final general stop
send_uart_command("stop\n", state);
furi_delay_ms(50);
}

// Close any open files
if(state->uart_context->storageContext->current_file &&
storage_file_is_open(state->uart_context->storageContext->current_file)) {
state->uart_context->storageContext->HasOpenedFile = false;
FURI_LOG_D("DEBUG", "Storage File Closed");
// Close any open files with proper checking
if(state->uart_context->storageContext) {
if(state->uart_context->storageContext->current_file &&
storage_file_is_open(state->uart_context->storageContext->current_file)) {
// Give time for final writes
furi_delay_ms(100);
storage_file_close(state->uart_context->storageContext->current_file);
state->uart_context->storageContext->HasOpenedFile = false;
FURI_LOG_D("DEBUG", "Storage File Closed");
}
}

// Return to previous view
// Return to appropriate previous view
switch(state->previous_view) {
case 1: show_wifi_menu(state); break;
case 2: show_ble_menu(state); break;
case 3: show_gps_menu(state); break;
default: show_main_menu(state); break;
}
state->current_view = state->previous_view;
} else if(current_view == 8) { // Settings actions menu
} else if(current_view == 8) { // Settings menu
show_main_menu(state);
state->current_view = 0;
} else if(current_view != 0) {
Expand All @@ -870,7 +900,6 @@ bool back_event_callback(void* context) {
view_dispatcher_stop(state->view_dispatcher);
}

// Consume the back button event for all views except the confirmation view
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion settings_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ bool settings_custom_event_callback(void* context, uint32_t event_id) {
"Updated by: Jay Candel\n"
"Built with <3";

confirmation_view_set_header(app_state->confirmation_view, "Ghost ESP v1.0.7a");
confirmation_view_set_header(app_state->confirmation_view, "Ghost ESP v1.0.8");
confirmation_view_set_text(app_state->confirmation_view, info_text);

// Save current view before switching
Expand Down
115 changes: 78 additions & 37 deletions uart_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,64 @@
#include <storage/storage.h>
#include "sequential_file.h"


#define COMMAND_BUFFER_SIZE 128
#define PCAP_WRITE_CHUNK_SIZE 1024u

void uart_storage_rx_callback(uint8_t *buf, size_t len, void *context) {
UartContext *app = context;
if(app->storageContext->HasOpenedFile) {
storage_file_write(app->storageContext->current_file, buf, len);

if(!app || !app->storageContext || !app->storageContext->storage_api) {
FURI_LOG_E("Storage", "Invalid storage context");
return;
}

if(!app->storageContext->HasOpenedFile ||
!app->storageContext->current_file ||
!storage_file_is_open(app->storageContext->current_file)) {
FURI_LOG_W("Storage", "No file open for writing");
app->pcap = false; // Disable PCAP mode if file isn't ready
return;
}
}

UartStorageContext *uart_storage_init(UartContext *parentContext) {
// Write in chunks to avoid buffer overflow
size_t written = 0;
while(written < len) {
size_t remaining = len - written;
size_t chunk_size = (remaining < PCAP_WRITE_CHUNK_SIZE) ?
remaining : PCAP_WRITE_CHUNK_SIZE;

uint16_t bytes_written = storage_file_write(
app->storageContext->current_file,
buf + written,
chunk_size);

if(bytes_written != chunk_size) {
FURI_LOG_E("Storage", "Write failed: %u/%zu bytes", bytes_written, chunk_size);
app->pcap = false; // Disable PCAP mode on write failure
break;
}
written += bytes_written;
}
}
UartStorageContext* uart_storage_init(UartContext* parentContext) {
uint32_t start_time = furi_get_tick();
FURI_LOG_D("Storage", "Starting storage initialization");

UartStorageContext *ctx = malloc(sizeof(UartStorageContext));
UartStorageContext* ctx = malloc(sizeof(UartStorageContext));
if(!ctx) {
FURI_LOG_E("Storage", "Failed to allocate context");
return NULL;
}

memset(ctx, 0, sizeof(UartStorageContext));
ctx->parentContext = parentContext;

// Open storage API
uint32_t storage_open_start = furi_get_tick();
ctx->storage_api = furi_record_open(RECORD_STORAGE);
FURI_LOG_D("Storage", "Storage API open took %lums", furi_get_tick() - storage_open_start);

if(!ctx->storage_api) {
FURI_LOG_E("Storage", "Failed to open storage API");
free(ctx);
return NULL;
}

// Allocate file handles
ctx->current_file = storage_file_alloc(ctx->storage_api);
ctx->log_file = storage_file_alloc(ctx->storage_api);

Expand All @@ -53,69 +76,87 @@ UartStorageContext *uart_storage_init(UartContext *parentContext) {
return NULL;
}

// Create directories efficiently
uint32_t dir_start = furi_get_tick();
storage_simply_mkdir(ctx->storage_api, GHOST_ESP_APP_FOLDER);
storage_simply_mkdir(ctx->storage_api, GHOST_ESP_APP_FOLDER_LOGS);
storage_simply_mkdir(ctx->storage_api, GHOST_ESP_APP_FOLDER_PCAPS);
storage_simply_mkdir(ctx->storage_api, GHOST_ESP_APP_FOLDER_WARDRIVE);
FURI_LOG_D("Storage", "Directory creation took %lums", furi_get_tick() - dir_start);
// Create directories with verification
if(!storage_simply_mkdir(ctx->storage_api, GHOST_ESP_APP_FOLDER)) {
FURI_LOG_W("Storage", "Failed to create app folder");
}
if(!storage_simply_mkdir(ctx->storage_api, GHOST_ESP_APP_FOLDER_PCAPS)) {
FURI_LOG_W("Storage", "Failed to create PCAP folder");
}
if(!storage_simply_mkdir(ctx->storage_api, GHOST_ESP_APP_FOLDER_LOGS)) {
FURI_LOG_W("Storage", "Failed to create logs folder");
}
if(!storage_simply_mkdir(ctx->storage_api, GHOST_ESP_APP_FOLDER_WARDRIVE)) {
FURI_LOG_W("Storage", "Failed to create wardrive folder");
}

// Verify PCAP directory exists and is writable
File* test_dir = storage_file_alloc(ctx->storage_api);
if(test_dir) {
if(!storage_dir_open(test_dir, GHOST_ESP_APP_FOLDER_PCAPS)) {
FURI_LOG_E("Storage", "PCAP directory not accessible");
}
storage_dir_close(test_dir);
storage_file_free(test_dir);
}

// Initialize log file efficiently
uint32_t log_start = furi_get_tick();
// Initialize log file
bool log_ok = sequential_file_open(
ctx->storage_api,
ctx->log_file,
GHOST_ESP_APP_FOLDER_LOGS,
"ghost_logs",
"txt");

if(!log_ok) {
FURI_LOG_W("Storage", "Failed to create initial log file");
FURI_LOG_E("Storage", "Failed to open log file");
}
FURI_LOG_D("Storage", "Log file creation took %lums", furi_get_tick() - log_start);

ctx->HasOpenedFile = log_ok;
ctx->IsWritingToFile = false;

FURI_LOG_I("Storage", "Storage initialization completed in %lums",
furi_get_tick() - start_time);
return ctx;
}

void uart_storage_reset_logs(UartStorageContext *ctx) {
if(!ctx || !ctx->storage_api) return;

FURI_LOG_D("Storage", "Resetting log files");

// Close existing log file if open
if(ctx->log_file) {
storage_file_close(ctx->log_file);
storage_file_free(ctx->log_file);
ctx->log_file = storage_file_alloc(ctx->storage_api);
}

// Create new log file
ctx->HasOpenedFile = sequential_file_open(
ctx->storage_api,
ctx->log_file,
GHOST_ESP_APP_FOLDER_LOGS,
"ghost_logs",
"txt");

if(!ctx->HasOpenedFile) {
FURI_LOG_E("Storage", "Failed to create new log file during reset");
FURI_LOG_E("Storage", "Failed to reset log file");
}
}

void uart_storage_free(UartStorageContext *ctx) {
if(!ctx) return;

uint32_t start_time = furi_get_tick();
FURI_LOG_D("Storage", "Starting storage cleanup");
if(ctx->current_file) {
storage_file_close(ctx->current_file);
storage_file_free(ctx->current_file);
}

if(ctx->current_file) storage_file_free(ctx->current_file);
if(ctx->log_file) storage_file_free(ctx->log_file);
if(ctx->storage_api) furi_record_close(RECORD_STORAGE);
if(ctx->log_file) {
storage_file_close(ctx->log_file);
storage_file_free(ctx->log_file);
}

if(ctx->storage_api) {
furi_record_close(RECORD_STORAGE);
}

free(ctx);
FURI_LOG_D("Storage", "Storage cleanup completed in %lums",
furi_get_tick() - start_time);
}
1 change: 0 additions & 1 deletion uart_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <furi.h>
#include <storage/storage.h>


struct UartStorageContext {
Storage* storage_api;
File* current_file;
Expand Down
Loading

0 comments on commit f28e785

Please sign in to comment.