6
6
#include <storage/storage.h>
7
7
#include "sequential_file.h"
8
8
9
-
10
9
#define COMMAND_BUFFER_SIZE 128
10
+ #define PCAP_WRITE_CHUNK_SIZE 1024u
11
11
12
12
void uart_storage_rx_callback (uint8_t * buf , size_t len , void * context ) {
13
13
UartContext * app = context ;
14
- if (app -> storageContext -> HasOpenedFile ) {
15
- storage_file_write (app -> storageContext -> current_file , buf , len );
14
+
15
+ if (!app || !app -> storageContext || !app -> storageContext -> storage_api ) {
16
+ FURI_LOG_E ("Storage" , "Invalid storage context" );
17
+ return ;
18
+ }
19
+
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 ;
16
26
}
17
- }
18
27
19
- UartStorageContext * uart_storage_init (UartContext * parentContext ) {
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
+ }
48
+ UartStorageContext * uart_storage_init (UartContext * parentContext ) {
20
49
uint32_t start_time = furi_get_tick ();
21
50
FURI_LOG_D ("Storage" , "Starting storage initialization" );
22
51
23
- UartStorageContext * ctx = malloc (sizeof (UartStorageContext ));
52
+ UartStorageContext * ctx = malloc (sizeof (UartStorageContext ));
24
53
if (!ctx ) {
25
54
FURI_LOG_E ("Storage" , "Failed to allocate context" );
26
55
return NULL ;
27
56
}
28
-
29
57
memset (ctx , 0 , sizeof (UartStorageContext ));
30
58
ctx -> parentContext = parentContext ;
31
59
32
- // Open storage API
33
- uint32_t storage_open_start = furi_get_tick ();
34
60
ctx -> storage_api = furi_record_open (RECORD_STORAGE );
35
- FURI_LOG_D ("Storage" , "Storage API open took %lums" , furi_get_tick () - storage_open_start );
36
-
37
61
if (!ctx -> storage_api ) {
38
62
FURI_LOG_E ("Storage" , "Failed to open storage API" );
39
63
free (ctx );
40
64
return NULL ;
41
65
}
42
66
43
- // Allocate file handles
44
67
ctx -> current_file = storage_file_alloc (ctx -> storage_api );
45
68
ctx -> log_file = storage_file_alloc (ctx -> storage_api );
46
69
@@ -53,69 +76,87 @@ UartStorageContext *uart_storage_init(UartContext *parentContext) {
53
76
return NULL ;
54
77
}
55
78
56
- // Create directories efficiently
57
- uint32_t dir_start = furi_get_tick ();
58
- storage_simply_mkdir (ctx -> storage_api , GHOST_ESP_APP_FOLDER );
59
- storage_simply_mkdir (ctx -> storage_api , GHOST_ESP_APP_FOLDER_LOGS );
60
- storage_simply_mkdir (ctx -> storage_api , GHOST_ESP_APP_FOLDER_PCAPS );
61
- storage_simply_mkdir (ctx -> storage_api , GHOST_ESP_APP_FOLDER_WARDRIVE );
62
- FURI_LOG_D ("Storage" , "Directory creation took %lums" , furi_get_tick () - dir_start );
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" );
91
+ }
92
+
93
+ // Verify PCAP directory exists and is writable
94
+ File * test_dir = storage_file_alloc (ctx -> storage_api );
95
+ if (test_dir ) {
96
+ if (!storage_dir_open (test_dir , GHOST_ESP_APP_FOLDER_PCAPS )) {
97
+ FURI_LOG_E ("Storage" , "PCAP directory not accessible" );
98
+ }
99
+ storage_dir_close (test_dir );
100
+ storage_file_free (test_dir );
101
+ }
63
102
64
- // Initialize log file efficiently
65
- uint32_t log_start = furi_get_tick ();
103
+ // Initialize log file
66
104
bool log_ok = sequential_file_open (
67
105
ctx -> storage_api ,
68
106
ctx -> log_file ,
69
107
GHOST_ESP_APP_FOLDER_LOGS ,
70
108
"ghost_logs" ,
71
109
"txt" );
72
-
110
+
73
111
if (!log_ok ) {
74
- FURI_LOG_W ("Storage" , "Failed to create initial log file" );
112
+ FURI_LOG_E ("Storage" , "Failed to open log file" );
75
113
}
76
- FURI_LOG_D ("Storage" , "Log file creation took %lums" , furi_get_tick () - log_start );
77
114
78
115
ctx -> HasOpenedFile = log_ok ;
116
+ ctx -> IsWritingToFile = false;
79
117
80
118
FURI_LOG_I ("Storage" , "Storage initialization completed in %lums" ,
81
119
furi_get_tick () - start_time );
82
120
return ctx ;
83
121
}
122
+
84
123
void uart_storage_reset_logs (UartStorageContext * ctx ) {
85
124
if (!ctx || !ctx -> storage_api ) return ;
86
125
87
- FURI_LOG_D ("Storage" , "Resetting log files" );
88
-
89
- // Close existing log file if open
90
126
if (ctx -> log_file ) {
91
127
storage_file_close (ctx -> log_file );
92
128
storage_file_free (ctx -> log_file );
93
129
ctx -> log_file = storage_file_alloc (ctx -> storage_api );
94
130
}
95
131
96
- // Create new log file
97
132
ctx -> HasOpenedFile = sequential_file_open (
98
133
ctx -> storage_api ,
99
134
ctx -> log_file ,
100
135
GHOST_ESP_APP_FOLDER_LOGS ,
101
136
"ghost_logs" ,
102
137
"txt" );
103
-
138
+
104
139
if (!ctx -> HasOpenedFile ) {
105
- FURI_LOG_E ("Storage" , "Failed to create new log file during reset " );
140
+ FURI_LOG_E ("Storage" , "Failed to reset log file" );
106
141
}
107
142
}
143
+
108
144
void uart_storage_free (UartStorageContext * ctx ) {
109
145
if (!ctx ) return ;
110
146
111
- uint32_t start_time = furi_get_tick ();
112
- FURI_LOG_D ("Storage" , "Starting storage cleanup" );
147
+ if (ctx -> current_file ) {
148
+ storage_file_close (ctx -> current_file );
149
+ storage_file_free (ctx -> current_file );
150
+ }
113
151
114
- if (ctx -> current_file ) storage_file_free (ctx -> current_file );
115
- if (ctx -> log_file ) storage_file_free (ctx -> log_file );
116
- if (ctx -> storage_api ) furi_record_close (RECORD_STORAGE );
152
+ if (ctx -> log_file ) {
153
+ storage_file_close (ctx -> log_file );
154
+ storage_file_free (ctx -> log_file );
155
+ }
156
+
157
+ if (ctx -> storage_api ) {
158
+ furi_record_close (RECORD_STORAGE );
159
+ }
117
160
118
161
free (ctx );
119
- FURI_LOG_D ("Storage" , "Storage cleanup completed in %lums" ,
120
- furi_get_tick () - start_time );
121
162
}
0 commit comments