9
9
#define COMMAND_BUFFER_SIZE 128
10
10
#define PCAP_WRITE_CHUNK_SIZE 1024u
11
11
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)
19
29
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
- }
48
30
UartStorageContext * uart_storage_init (UartContext * parentContext ) {
49
31
uint32_t start_time = furi_get_tick ();
50
32
FURI_LOG_D ("Storage" , "Starting storage initialization" );
51
33
34
+ // Allocate and initialize context
52
35
UartStorageContext * ctx = malloc (sizeof (UartStorageContext ));
53
36
if (!ctx ) {
54
37
FURI_LOG_E ("Storage" , "Failed to allocate context" );
@@ -57,69 +40,102 @@ UartStorageContext* uart_storage_init(UartContext* parentContext) {
57
40
memset (ctx , 0 , sizeof (UartStorageContext ));
58
41
ctx -> parentContext = parentContext ;
59
42
43
+ // Open storage API
60
44
ctx -> storage_api = furi_record_open (RECORD_STORAGE );
61
45
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 );
65
47
}
66
48
49
+ // Allocate file handles
67
50
ctx -> current_file = storage_file_alloc (ctx -> storage_api );
68
51
ctx -> log_file = storage_file_alloc (ctx -> storage_api );
69
-
70
52
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 );
77
54
}
78
55
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
+ }
91
63
}
92
64
93
65
// Verify PCAP directory exists and is writable
94
66
File * test_dir = storage_file_alloc (ctx -> storage_api );
95
67
if (test_dir ) {
96
68
if (!storage_dir_open (test_dir , GHOST_ESP_APP_FOLDER_PCAPS )) {
97
69
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
98
72
}
99
73
storage_dir_close (test_dir );
100
74
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
101
78
}
102
79
103
80
// Initialize log file
104
- bool log_ok = sequential_file_open (
81
+ ctx -> HasOpenedFile = sequential_file_open (
105
82
ctx -> storage_api ,
106
83
ctx -> log_file ,
107
84
GHOST_ESP_APP_FOLDER_LOGS ,
108
85
"ghost_logs" ,
109
- "txt" );
86
+ "txt"
87
+ );
110
88
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
113
92
}
114
93
115
- ctx -> HasOpenedFile = log_ok ;
116
94
ctx -> IsWritingToFile = false;
117
95
118
96
FURI_LOG_I ("Storage" , "Storage initialization completed in %lums" ,
119
97
furi_get_tick () - start_time );
120
98
return ctx ;
121
99
}
122
100
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
+
123
139
void uart_storage_reset_logs (UartStorageContext * ctx ) {
124
140
if (!ctx || !ctx -> storage_api ) return ;
125
141
@@ -134,7 +150,8 @@ void uart_storage_reset_logs(UartStorageContext *ctx) {
134
150
ctx -> log_file ,
135
151
GHOST_ESP_APP_FOLDER_LOGS ,
136
152
"ghost_logs" ,
137
- "txt" );
153
+ "txt"
154
+ );
138
155
139
156
if (!ctx -> HasOpenedFile ) {
140
157
FURI_LOG_E ("Storage" , "Failed to reset log file" );
@@ -159,4 +176,4 @@ void uart_storage_free(UartStorageContext *ctx) {
159
176
}
160
177
161
178
free (ctx );
162
- }
179
+ }
0 commit comments