Skip to content

Commit

Permalink
Merge pull request #13 from jaylikesbunda/main
Browse files Browse the repository at this point in the history
v1.0.9
  • Loading branch information
jaylikesbunda authored Nov 3, 2024
2 parents fde1a15 + 22e54f5 commit e7faebc
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 278 deletions.
1 change: 1 addition & 0 deletions app_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ struct AppState {
char* textBoxBuffer;
size_t buffer_length;
size_t buffer_capacity;
size_t buffer_size;
};
2 changes: 1 addition & 1 deletion application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ App(
fap_category="GPIO/ESP",
# Optional values
icon="A_GhostESP_14",
fap_version="1.0.7",
fap_version="1.0.9",
fap_icon="ghost_esp.png", # 10x10 1-bit PNG
fap_icon_assets="images", # Image assets to compile for this application
)
10 changes: 9 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@
- Filtering majorly improved
- Improved stop on back to be much more reliable by added type-specific stop commands with delays between operations


## v1.0.9
- Fixed log file corruption when stopping captures
- Added proper bounds checking for oversized messages
- Improved text display buffer management
- Added automatic prefix tagging for WiFi, BLE and system messages
- Improved storage init speed

## TODO
- Replaced select a utility text with prompt to show NEW Help Menu
- IMPROVE optional filtering to UART output
- FINALISE optional filtering to UART output
- Improve directory organisation!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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.8");
confirmation_view_set_header(app_state->confirmation_view, "Ghost ESP v1.0.9");
confirmation_view_set_text(app_state->confirmation_view, info_text);

// Save current view before switching
Expand Down
145 changes: 81 additions & 64 deletions uart_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,29 @@
#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 || !app->storageContext || !app->storageContext->storage_api) {
FURI_LOG_E("Storage", "Invalid storage context");
return;
}
// Define directories in an array for loop-based creation
static const char* GHOST_DIRECTORIES[] = {
GHOST_ESP_APP_FOLDER,
GHOST_ESP_APP_FOLDER_PCAPS,
GHOST_ESP_APP_FOLDER_LOGS,
GHOST_ESP_APP_FOLDER_WARDRIVE
};

// Helper macro for error handling and cleanup
#define CLEANUP_AND_RETURN(ctx, msg, retval) \
do { \
FURI_LOG_E("Storage", msg); \
if (ctx) { \
uart_storage_free(ctx); \
} \
return retval; \
} while(0)

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;
}

// 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");

// Allocate and initialize context
UartStorageContext* ctx = malloc(sizeof(UartStorageContext));
if(!ctx) {
FURI_LOG_E("Storage", "Failed to allocate context");
Expand All @@ -57,69 +40,102 @@ UartStorageContext* uart_storage_init(UartContext* parentContext) {
memset(ctx, 0, sizeof(UartStorageContext));
ctx->parentContext = parentContext;

// Open storage API
ctx->storage_api = furi_record_open(RECORD_STORAGE);
if(!ctx->storage_api) {
FURI_LOG_E("Storage", "Failed to open storage API");
free(ctx);
return NULL;
CLEANUP_AND_RETURN(ctx, "Failed to open storage API", NULL);
}

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

if(!ctx->current_file || !ctx->log_file) {
FURI_LOG_E("Storage", "Failed to allocate file handles");
if(ctx->current_file) storage_file_free(ctx->current_file);
if(ctx->log_file) storage_file_free(ctx->log_file);
furi_record_close(RECORD_STORAGE);
free(ctx);
return NULL;
CLEANUP_AND_RETURN(ctx, "Failed to allocate file handles", NULL);
}

// 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");
// Create directories using a loop
size_t num_directories = sizeof(GHOST_DIRECTORIES) / sizeof(GHOST_DIRECTORIES[0]);
for(size_t i = 0; i < num_directories; i++) {
if(!storage_simply_mkdir(ctx->storage_api, GHOST_DIRECTORIES[i])) {
// Log warnings instead of errors to minimize critical failure states
FURI_LOG_W("Storage", "Failed to create directory: %s", GHOST_DIRECTORIES[i]);
}
}

// 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");
// Optionally, decide whether to continue or cleanup
// Here, we choose to disable PCAP mode but continue initialization
}
storage_dir_close(test_dir);
storage_file_free(test_dir);
} else {
FURI_LOG_W("Storage", "Failed to allocate test directory handle");
// Decide whether to treat this as critical or not
}

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

if(!log_ok) {
FURI_LOG_E("Storage", "Failed to open log file");
if(!ctx->HasOpenedFile) {
FURI_LOG_W("Storage", "Failed to open log file");
// Depending on requirements, decide to disable logging or treat as critical
}

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_rx_callback(uint8_t *buf, size_t len, void *context) {
UartContext *app = context;

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;
}

// 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;
}
}

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

Expand All @@ -134,7 +150,8 @@ void uart_storage_reset_logs(UartStorageContext *ctx) {
ctx->log_file,
GHOST_ESP_APP_FOLDER_LOGS,
"ghost_logs",
"txt");
"txt"
);

if(!ctx->HasOpenedFile) {
FURI_LOG_E("Storage", "Failed to reset log file");
Expand All @@ -159,4 +176,4 @@ void uart_storage_free(UartStorageContext *ctx) {
}

free(ctx);
}
}
Loading

0 comments on commit e7faebc

Please sign in to comment.