-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstorage.c
304 lines (284 loc) · 11.2 KB
/
storage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "storage.h"
#include <lib/print/wrappers.h>
#define TAG "Gravity"
#define FILENAME_SETTINGS "gravity.settings"
#define FILENAME_DATA "data.gravity"
#define DEFAULT_SSID_MIN 0
#define DEFAULT_SSID_MAX 32
#define DEFAULT_SSID_COUNT 20
#define DEFAULT_ATTACK_MILLIS 50
#define DEFAULT_PKT_EXPIRY 0 /* Don't expire */
#define FILEBUFFER_SIZE 128
#define DATABUFFER_SIZE 4096
/* Essentially the reverse of do_sync() from sync.c - Take all settings
from Flipper-Gravity and write them to a file on the Flipper, using
the same format as sync.
YAGNI: We should be able to ignore the case of empty strings because
these settings all have default values.
Except for 'Get' menu options, which may still be "Get" if
the sync on startup failed. Default values are #define'd
*/
bool writeSettingsToFile(GravityApp* app, File* file) {
int bufLen = 0;
char fBuffer[FILEBUFFER_SIZE] = "";
char strBuffer[32] = "";
memset(fBuffer, '\0', FILEBUFFER_SIZE);
for(GravitySyncItem i = 0; i < GRAVITY_SYNC_ITEM_COUNT; ++i) {
snprintf(strBuffer, FILEBUFFER_SIZE, "(%d:", i);
strcpy(&(fBuffer[bufLen]), strBuffer);
bufLen += strlen(strBuffer);
switch(i) {
case GRAVITY_SYNC_HOP_ON:
snprintf(
strBuffer,
FILEBUFFER_SIZE,
"%d)",
app->selected_menu_options[GRAVITY_MENU_SETTINGS][SETTINGS_MENU_HOP_STATUS]);
break;
case GRAVITY_SYNC_SSID_MIN:
/* If menu item's 'Get' option is "Get" then we have no value - Use default */
if(strcmp(
settings[SETTINGS_MENU_SSID_MIN].options_menu[OPTIONS_SSID_MIN_GET],
STRINGS_GET)) {
snprintf(
strBuffer,
FILEBUFFER_SIZE,
"%s)",
settings[SETTINGS_MENU_SSID_MIN].options_menu[OPTIONS_SSID_MIN_GET]);
} else {
snprintf(strBuffer, FILEBUFFER_SIZE, "%d)", DEFAULT_SSID_MIN);
}
break;
case GRAVITY_SYNC_SSID_MAX:
if(strcmp(
settings[SETTINGS_MENU_SSID_MAX].options_menu[OPTIONS_SSID_MAX_GET],
STRINGS_GET)) {
snprintf(
strBuffer,
FILEBUFFER_SIZE,
"%s)",
settings[SETTINGS_MENU_SSID_MAX].options_menu[OPTIONS_SSID_MAX_GET]);
} else {
snprintf(strBuffer, FILEBUFFER_SIZE, "%d)", DEFAULT_SSID_MAX);
}
break;
case GRAVITY_SYNC_SSID_COUNT:
if(strcmp(
settings[SETTINGS_MENU_SSID_DEFAULT].options_menu[OPTIONS_SSID_DEFAULT_GET],
STRINGS_GET)) {
snprintf(
strBuffer,
FILEBUFFER_SIZE,
"%s)",
settings[SETTINGS_MENU_SSID_DEFAULT].options_menu[OPTIONS_SSID_DEFAULT_GET]);
} else {
snprintf(strBuffer, FILEBUFFER_SIZE, "%d)", DEFAULT_SSID_COUNT);
}
break;
case GRAVITY_SYNC_CHANNEL:
/* Happily, the channel number is the same as the channel index */
// TODO: If the "Get" option is removed this won't be true anymore and all of sync
// will need to be refactored
snprintf(strBuffer, FILEBUFFER_SIZE, "%d)", app->channel);
break;
case GRAVITY_SYNC_MAC:;
char strTemp[NUM_MAC_BYTES * 3];
if(!mac_bytes_to_string(app->mac_bytes, strTemp)) {
FURI_LOG_E(TAG, "Failed to stringify MAC to save settings, abandoning save");
return false;
}
snprintf(strBuffer, FILEBUFFER_SIZE, "%s)", strTemp);
break;
case GRAVITY_SYNC_ATTACK_MILLIS:
if(strcmp(
settings[SETTINGS_MENU_ATTACK_MILLIS].options_menu[OPTIONS_ATTACK_MILLIS_GET],
STRINGS_GET)) {
snprintf(
strBuffer,
FILEBUFFER_SIZE,
"%s)",
settings[SETTINGS_MENU_ATTACK_MILLIS].options_menu[OPTIONS_ATTACK_MILLIS_GET]);
} else {
snprintf(strBuffer, FILEBUFFER_SIZE, "%d)", DEFAULT_ATTACK_MILLIS);
}
break;
case GRAVITY_SYNC_MAC_RAND:
snprintf(strBuffer, FILEBUFFER_SIZE, "%d)", app->mac_rand);
break;
case GRAVITY_SYNC_PKT_EXPIRY:
if(strcmp(
settings[SETTINGS_MENU_PKT_EXPIRY].options_menu[OPTIONS_PKT_EXPIRY_GET],
STRINGS_GET)) {
snprintf(
strBuffer,
FILEBUFFER_SIZE,
"%s)",
settings[SETTINGS_MENU_PKT_EXPIRY].options_menu[OPTIONS_PKT_EXPIRY_GET]);
} else {
snprintf(strBuffer, FILEBUFFER_SIZE, "%d)", DEFAULT_PKT_EXPIRY);
}
break;
case GRAVITY_SYNC_HOP_MODE:
snprintf(strBuffer, FILEBUFFER_SIZE, "%d)", app->hopMode);
break;
case GRAVITY_SYNC_DICT_DISABLED:
snprintf(strBuffer, FILEBUFFER_SIZE, "%d)", app->dict_disabled);
break;
case GRAVITY_SYNC_PURGE_STRAT:
snprintf(strBuffer, FILEBUFFER_SIZE, "%d)", app->purgeStrategy);
break;
case GRAVITY_SYNC_PURGE_RSSI_MAX:
snprintf(strBuffer, FILEBUFFER_SIZE, "%d)", app->purgeRSSI);
break;
case GRAVITY_SYNC_PURGE_AGE_MIN:
snprintf(strBuffer, FILEBUFFER_SIZE, "%d)", app->purgeAge);
break;
default:
//
break;
}
strcpy(&(fBuffer[bufLen]), strBuffer);
bufLen += strlen(strBuffer);
}
return storage_file_write(file, fBuffer, bufLen);
}
void close_file(File* file) {
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
}
/* So much goddamn repeated code! The APP_DATA_PATH macro, as far as I can figure, can't be
called with a variable, even a const char[] :( */
bool save_settings(GravityApp* app) {
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
bool retVal = true;
if(!storage_file_open(file, APP_DATA_PATH(FILENAME_SETTINGS), FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
FURI_LOG_E(TAG, "Failed to open %s for writing", FILENAME_SETTINGS);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
retVal = false;
}
if(retVal && !storage_file_truncate(file)) {
FURI_LOG_E(TAG, "Unable to truncate settings file for writing");
close_file(file);
retVal = false;
}
if(retVal && !writeSettingsToFile(app, file)) {
FURI_LOG_E(TAG, "Failed to write settings");
close_file(file);
retVal = false;
}
if (!retVal) {
popup_set_header(app->popup, "Save Failed", 64, 10, AlignCenter, AlignTop);
popup_set_text(app->popup, "Save Settings Failed", 10, 40, AlignLeft, AlignTop);
view_dispatcher_switch_to_view(app->view_dispatcher, Gravity_AppViewPopup);
return false;
}
/* Keeping this comment block as an example of writing arbitrary text to a file
if (!storage_file_write(file, "foo", strlen("foo"))) {
FURI_LOG_E(TAG, "Failed to write to file");
close_file(file);
return false;
} */
/* Flush write cache */
storage_file_sync(file);
close_file(file);
popup_set_header(app->popup, "Save Succeeded", 64, 10, AlignCenter, AlignTop);
popup_set_text(app->popup, "Saved Settings", 10, 40, AlignLeft, AlignTop);
view_dispatcher_switch_to_view(app->view_dispatcher, Gravity_AppViewPopup);
return true;
}
bool load_settings(GravityApp* app) {
uint16_t bufferSize = 1024;
char buffer[bufferSize];
uint16_t bytesRead;
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
if(!storage_file_open(file, APP_DATA_PATH(FILENAME_SETTINGS), FSAM_READ, FSOM_OPEN_EXISTING)) {
FURI_LOG_E(TAG, "Failed to open %s for reading", FILENAME_SETTINGS);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
popup_set_header(app->popup, "Load Failed", 64, 10, AlignCenter, AlignTop);
popup_set_text(app->popup, "Failed to load settings", 10, 40, AlignLeft, AlignTop);
view_dispatcher_switch_to_view(app->view_dispatcher, Gravity_AppViewPopup);
return false;
}
bytesRead = storage_file_read(file, buffer, bufferSize);
/* It would be a minor miracle if this worked */
memset(app->syncBuffer, '\0', SYNC_BUFFER_SIZE);
app->syncBufLen = bytesRead;
strncpy((char*)app->syncBuffer, buffer, SYNC_BUFFER_SIZE);
syncProcessResponse(app, true);
close_file(file);
popup_set_header(app->popup, "Load Successful", 64, 10, AlignCenter, AlignTop);
popup_set_text(app->popup, "Settings Loaded", 10, 40, AlignLeft, AlignTop);
view_dispatcher_switch_to_view(app->view_dispatcher, Gravity_AppViewPopup);
/* Another nod to the person with the insane compiler */
if(app->is_command) {
return true;
} else {
return true;
}
}
bool writeDataToFile(GravityApp* app, File* file) {
int bufLen = 0;
uint8_t dataBuffer[DATABUFFER_SIZE];
memset(dataBuffer, 0x00, DATABUFFER_SIZE);
(void)(bufLen);
return true;
}
bool save_data(GravityApp* app) {
// (void)(app);
// Storage* storage = furi_record_open(RECORD_STORAGE);
// File* file = storage_file_alloc(storage);
// if(!storage_file_open(file, APP_DATA_PATH(FILENAME_DATA), FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
// FURI_LOG_E(TAG, "Failed to open %s for writing", FILENAME_DATA);
// storage_file_free(file);
// furi_record_close(RECORD_STORAGE);
// return false;
// }
// if(!storage_file_truncate(file)) {
// FURI_LOG_E(TAG, "Unable to truncate data file for writing");
// close_file(file);
// return false;
// }
// if(!writeDataToFile(app, file)) {
// FURI_LOG_E(TAG, "Failed to write data");
// close_file(file);
// return false;
// }
// // if (!storage_file_write(file, "bar", strlen("bar"))) {
// // FURI_LOG_E(TAG, "Failed to write data");
// // close_file(file);
// // return false;
// // }
// close_file(file);
if(app->is_command) {
return true;
} else {
return true;
}
}
bool load_data(GravityApp* app) {
// uint16_t bufferSize = 1024;
// char buffer[bufferSize];
// uint16_t bytesRead;
// UNUSED(bytesRead);
// Storage* storage = furi_record_open(RECORD_STORAGE);
// File* file = storage_file_alloc(storage);
// if(!storage_file_open(file, APP_DATA_PATH(FILENAME_DATA), FSAM_READ, FSOM_OPEN_EXISTING)) {
// FURI_LOG_E(TAG, "Failed to open %s - Have you saved data?", FILENAME_DATA);
// storage_file_close(file);
// furi_record_close(RECORD_STORAGE);
// return false;
// }
// //bytesRead = storage_file_read(file, buffer, bufferSize);
// // Parse file
// close_file(file);
if(app->is_command) {
return true;
} else {
return true;
}
}