Skip to content

Commit e7faebc

Browse files
Merge pull request #13 from jaylikesbunda/main
v1.0.9
2 parents fde1a15 + 22e54f5 commit e7faebc

6 files changed

+201
-278
lines changed

app_state.h

+1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ struct AppState {
5151
char* textBoxBuffer;
5252
size_t buffer_length;
5353
size_t buffer_capacity;
54+
size_t buffer_size;
5455
};

application.fam

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ App(
99
fap_category="GPIO/ESP",
1010
# Optional values
1111
icon="A_GhostESP_14",
12-
fap_version="1.0.7",
12+
fap_version="1.0.9",
1313
fap_icon="ghost_esp.png", # 10x10 1-bit PNG
1414
fap_icon_assets="images", # Image assets to compile for this application
1515
)

docs/changelog.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@
7676
- Filtering majorly improved
7777
- Improved stop on back to be much more reliable by added type-specific stop commands with delays between operations
7878

79+
80+
## v1.0.9
81+
- Fixed log file corruption when stopping captures
82+
- Added proper bounds checking for oversized messages
83+
- Improved text display buffer management
84+
- Added automatic prefix tagging for WiFi, BLE and system messages
85+
- Improved storage init speed
86+
7987
## TODO
8088
- Replaced select a utility text with prompt to show NEW Help Menu
81-
- IMPROVE optional filtering to UART output
89+
- FINALISE optional filtering to UART output
8290
- Improve directory organisation!!!!!!!!!!!!!!!!!!!!!!!!!!!!

settings_ui.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ bool settings_custom_event_callback(void* context, uint32_t event_id) {
391391
"Updated by: Jay Candel\n"
392392
"Built with <3";
393393

394-
confirmation_view_set_header(app_state->confirmation_view, "Ghost ESP v1.0.8");
394+
confirmation_view_set_header(app_state->confirmation_view, "Ghost ESP v1.0.9");
395395
confirmation_view_set_text(app_state->confirmation_view, info_text);
396396

397397
// Save current view before switching

uart_storage.c

+81-64
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,29 @@
99
#define COMMAND_BUFFER_SIZE 128
1010
#define PCAP_WRITE_CHUNK_SIZE 1024u
1111

12-
void uart_storage_rx_callback(uint8_t *buf, size_t len, void *context) {
13-
UartContext *app = context;
14-
15-
if(!app || !app->storageContext || !app->storageContext->storage_api) {
16-
FURI_LOG_E("Storage", "Invalid storage context");
17-
return;
18-
}
12+
// Define directories in an array for loop-based creation
13+
static const char* GHOST_DIRECTORIES[] = {
14+
GHOST_ESP_APP_FOLDER,
15+
GHOST_ESP_APP_FOLDER_PCAPS,
16+
GHOST_ESP_APP_FOLDER_LOGS,
17+
GHOST_ESP_APP_FOLDER_WARDRIVE
18+
};
19+
20+
// Helper macro for error handling and cleanup
21+
#define CLEANUP_AND_RETURN(ctx, msg, retval) \
22+
do { \
23+
FURI_LOG_E("Storage", msg); \
24+
if (ctx) { \
25+
uart_storage_free(ctx); \
26+
} \
27+
return retval; \
28+
} while(0)
1929

20-
if(!app->storageContext->HasOpenedFile ||
21-
!app->storageContext->current_file ||
22-
!storage_file_is_open(app->storageContext->current_file)) {
23-
FURI_LOG_W("Storage", "No file open for writing");
24-
app->pcap = false; // Disable PCAP mode if file isn't ready
25-
return;
26-
}
27-
28-
// Write in chunks to avoid buffer overflow
29-
size_t written = 0;
30-
while(written < len) {
31-
size_t remaining = len - written;
32-
size_t chunk_size = (remaining < PCAP_WRITE_CHUNK_SIZE) ?
33-
remaining : PCAP_WRITE_CHUNK_SIZE;
34-
35-
uint16_t bytes_written = storage_file_write(
36-
app->storageContext->current_file,
37-
buf + written,
38-
chunk_size);
39-
40-
if(bytes_written != chunk_size) {
41-
FURI_LOG_E("Storage", "Write failed: %u/%zu bytes", bytes_written, chunk_size);
42-
app->pcap = false; // Disable PCAP mode on write failure
43-
break;
44-
}
45-
written += bytes_written;
46-
}
47-
}
4830
UartStorageContext* uart_storage_init(UartContext* parentContext) {
4931
uint32_t start_time = furi_get_tick();
5032
FURI_LOG_D("Storage", "Starting storage initialization");
5133

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

43+
// Open storage API
6044
ctx->storage_api = furi_record_open(RECORD_STORAGE);
6145
if(!ctx->storage_api) {
62-
FURI_LOG_E("Storage", "Failed to open storage API");
63-
free(ctx);
64-
return NULL;
46+
CLEANUP_AND_RETURN(ctx, "Failed to open storage API", NULL);
6547
}
6648

49+
// Allocate file handles
6750
ctx->current_file = storage_file_alloc(ctx->storage_api);
6851
ctx->log_file = storage_file_alloc(ctx->storage_api);
69-
7052
if(!ctx->current_file || !ctx->log_file) {
71-
FURI_LOG_E("Storage", "Failed to allocate file handles");
72-
if(ctx->current_file) storage_file_free(ctx->current_file);
73-
if(ctx->log_file) storage_file_free(ctx->log_file);
74-
furi_record_close(RECORD_STORAGE);
75-
free(ctx);
76-
return NULL;
53+
CLEANUP_AND_RETURN(ctx, "Failed to allocate file handles", NULL);
7754
}
7855

79-
// Create directories with verification
80-
if(!storage_simply_mkdir(ctx->storage_api, GHOST_ESP_APP_FOLDER)) {
81-
FURI_LOG_W("Storage", "Failed to create app folder");
82-
}
83-
if(!storage_simply_mkdir(ctx->storage_api, GHOST_ESP_APP_FOLDER_PCAPS)) {
84-
FURI_LOG_W("Storage", "Failed to create PCAP folder");
85-
}
86-
if(!storage_simply_mkdir(ctx->storage_api, GHOST_ESP_APP_FOLDER_LOGS)) {
87-
FURI_LOG_W("Storage", "Failed to create logs folder");
88-
}
89-
if(!storage_simply_mkdir(ctx->storage_api, GHOST_ESP_APP_FOLDER_WARDRIVE)) {
90-
FURI_LOG_W("Storage", "Failed to create wardrive folder");
56+
// Create directories using a loop
57+
size_t num_directories = sizeof(GHOST_DIRECTORIES) / sizeof(GHOST_DIRECTORIES[0]);
58+
for(size_t i = 0; i < num_directories; i++) {
59+
if(!storage_simply_mkdir(ctx->storage_api, GHOST_DIRECTORIES[i])) {
60+
// Log warnings instead of errors to minimize critical failure states
61+
FURI_LOG_W("Storage", "Failed to create directory: %s", GHOST_DIRECTORIES[i]);
62+
}
9163
}
9264

9365
// Verify PCAP directory exists and is writable
9466
File* test_dir = storage_file_alloc(ctx->storage_api);
9567
if(test_dir) {
9668
if(!storage_dir_open(test_dir, GHOST_ESP_APP_FOLDER_PCAPS)) {
9769
FURI_LOG_E("Storage", "PCAP directory not accessible");
70+
// Optionally, decide whether to continue or cleanup
71+
// Here, we choose to disable PCAP mode but continue initialization
9872
}
9973
storage_dir_close(test_dir);
10074
storage_file_free(test_dir);
75+
} else {
76+
FURI_LOG_W("Storage", "Failed to allocate test directory handle");
77+
// Decide whether to treat this as critical or not
10178
}
10279

10380
// Initialize log file
104-
bool log_ok = sequential_file_open(
81+
ctx->HasOpenedFile = sequential_file_open(
10582
ctx->storage_api,
10683
ctx->log_file,
10784
GHOST_ESP_APP_FOLDER_LOGS,
10885
"ghost_logs",
109-
"txt");
86+
"txt"
87+
);
11088

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

115-
ctx->HasOpenedFile = log_ok;
11694
ctx->IsWritingToFile = false;
11795

11896
FURI_LOG_I("Storage", "Storage initialization completed in %lums",
11997
furi_get_tick() - start_time);
12098
return ctx;
12199
}
122100

101+
void uart_storage_rx_callback(uint8_t *buf, size_t len, void *context) {
102+
UartContext *app = context;
103+
104+
if(!app || !app->storageContext || !app->storageContext->storage_api) {
105+
FURI_LOG_E("Storage", "Invalid storage context");
106+
return;
107+
}
108+
109+
if(!app->storageContext->HasOpenedFile ||
110+
!app->storageContext->current_file ||
111+
!storage_file_is_open(app->storageContext->current_file)) {
112+
FURI_LOG_W("Storage", "No file open for writing");
113+
app->pcap = false; // Disable PCAP mode if file isn't ready
114+
return;
115+
}
116+
117+
// Write in chunks to avoid buffer overflow
118+
size_t written = 0;
119+
while(written < len) {
120+
size_t remaining = len - written;
121+
size_t chunk_size = (remaining < PCAP_WRITE_CHUNK_SIZE) ?
122+
remaining : PCAP_WRITE_CHUNK_SIZE;
123+
124+
uint16_t bytes_written = storage_file_write(
125+
app->storageContext->current_file,
126+
buf + written,
127+
chunk_size
128+
);
129+
130+
if(bytes_written != chunk_size) {
131+
FURI_LOG_E("Storage", "Write failed: %u/%zu bytes", bytes_written, chunk_size);
132+
app->pcap = false; // Disable PCAP mode on write failure
133+
break;
134+
}
135+
written += bytes_written;
136+
}
137+
}
138+
123139
void uart_storage_reset_logs(UartStorageContext *ctx) {
124140
if(!ctx || !ctx->storage_api) return;
125141

@@ -134,7 +150,8 @@ void uart_storage_reset_logs(UartStorageContext *ctx) {
134150
ctx->log_file,
135151
GHOST_ESP_APP_FOLDER_LOGS,
136152
"ghost_logs",
137-
"txt");
153+
"txt"
154+
);
138155

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

161178
free(ctx);
162-
}
179+
}

0 commit comments

Comments
 (0)