-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
mag_device.c
333 lines (266 loc) · 10.5 KB
/
mag_device.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "mag_device.h"
#include <toolbox/path.h>
#include <flipper_format/flipper_format.h>
#define TAG "MagDevice"
static const char* mag_file_header = "Flipper Mag device";
static const uint32_t mag_file_version = 1;
MagDevice* mag_device_alloc() {
MagDevice* mag_dev = malloc(sizeof(MagDevice));
mag_dev->dev_data.track[0].str = furi_string_alloc();
mag_dev->dev_data.track[1].str = furi_string_alloc();
mag_dev->dev_data.track[2].str = furi_string_alloc();
mag_dev->storage = furi_record_open(RECORD_STORAGE);
mag_dev->dialogs = furi_record_open(RECORD_DIALOGS);
mag_dev->load_path = furi_string_alloc();
return mag_dev;
}
void mag_device_data_clear(MagDeviceData* dev_data) {
furi_string_reset(dev_data->track[0].str);
furi_string_reset(dev_data->track[1].str);
furi_string_reset(dev_data->track[2].str);
}
void mag_device_clear(MagDevice* mag_dev) {
furi_assert(mag_dev);
mag_device_data_clear(&mag_dev->dev_data);
memset(&mag_dev->dev_data, 0, sizeof(mag_dev->dev_data));
furi_string_reset(mag_dev->load_path);
}
void mag_device_free(MagDevice* mag_dev) {
furi_assert(mag_dev);
mag_device_clear(mag_dev);
furi_record_close(RECORD_STORAGE);
furi_record_close(RECORD_DIALOGS);
furi_string_free(mag_dev->load_path);
//furi_string_free(mag_dev->dev_data.track[0].str);
//furi_string_free(mag_dev->dev_data.track[1].str);
//furi_string_free(mag_dev->dev_data.track[2].str);
free(mag_dev);
}
void mag_device_set_name(MagDevice* mag_dev, const char* name) {
furi_assert(mag_dev);
strlcpy(mag_dev->dev_name, name, MAG_DEV_NAME_MAX_LEN);
}
static bool mag_device_save_file(
MagDevice* mag_dev,
const char* dev_name,
const char* folder,
const char* extension,
bool use_load_path) {
furi_assert(mag_dev);
bool saved = false;
FlipperFormat* file = flipper_format_file_alloc(mag_dev->storage);
FuriString* temp_str;
temp_str = furi_string_alloc();
do {
if(use_load_path && !furi_string_empty(mag_dev->load_path)) {
// Get dir name
path_extract_dirname(furi_string_get_cstr(mag_dev->load_path), temp_str);
// Create mag directory if necessary
if(!storage_simply_mkdir((mag_dev->storage), furi_string_get_cstr(temp_str))) break;
// Make path to file to be saved
furi_string_cat_printf(temp_str, "/%s%s", dev_name, extension);
} else {
// Create mag directory if necessary
if(!storage_simply_mkdir((mag_dev->storage), MAG_APP_FOLDER)) break;
// First remove mag device file if it was saved
furi_string_printf(temp_str, "%s/%s%s", folder, dev_name, extension);
}
// Open file
if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
// Write header
if(!flipper_format_write_header_cstr(file, mag_file_header, mag_file_version)) break;
// Write comment
if(!flipper_format_write_comment_cstr(file, "Mag device track data")) break;
// Write data
for(uint8_t i = 0; i < MAG_DEV_TRACKS; i++) {
furi_string_printf(temp_str, "Track %d", i + 1);
if(!flipper_format_write_string_cstr(
file,
furi_string_get_cstr(temp_str),
furi_string_get_cstr(mag_dev->dev_data.track[i].str)))
break;
}
saved = true;
} while(0);
if(!saved) {
dialog_message_show_storage_error(mag_dev->dialogs, "Cannot save\nfile");
}
furi_string_free(temp_str);
flipper_format_free(file);
return saved;
}
bool mag_device_save(MagDevice* mag_dev, const char* dev_name) {
// wrapping function in the event we have multiple formats
return mag_device_save_file(mag_dev, dev_name, MAG_APP_FOLDER, MAG_APP_EXTENSION, true);
}
bool mag_device_load_data(MagDevice* mag_dev, FuriString* path, bool show_dialog) {
bool parsed = false;
FuriString* filename;
filename = furi_string_alloc();
path_extract_filename(path, filename, true);
strncpy(mag_dev->dev_name, furi_string_get_cstr(filename), MAG_DEV_NAME_MAX_LEN);
FlipperFormat* file = flipper_format_file_alloc(mag_dev->storage);
FuriString* temp_str;
temp_str = furi_string_alloc();
bool deprecated_version = false;
bool data_read = true;
if(mag_dev->loading_cb) {
mag_dev->loading_cb(mag_dev->loading_cb_ctx, true);
}
do {
if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
// Read and verify header, check file version
uint32_t version;
if(!flipper_format_read_header(file, temp_str, &version)) break;
if(furi_string_cmp_str(temp_str, mag_file_header) || (version != mag_file_version)) {
deprecated_version = true;
break;
}
// Parse data
for(uint8_t i = 0; i < MAG_DEV_TRACKS; i++) {
furi_string_printf(temp_str, "Track %d", i + 1);
if(!flipper_format_read_string(
file, furi_string_get_cstr(temp_str), mag_dev->dev_data.track[i].str)) {
FURI_LOG_D(TAG, "Could not read track %d data", i + 1);
// TODO: smarter load handling now that it is acceptible for some tracks to be empty
data_read = false;
}
}
parsed = true;
} while(false);
if((!parsed) && (show_dialog)) {
if(deprecated_version) {
dialog_message_show_storage_error(mag_dev->dialogs, "File format\ndeprecated");
} else if(!data_read) {
dialog_message_show_storage_error(mag_dev->dialogs, "Cannot read\ndata");
} else {
dialog_message_show_storage_error(mag_dev->dialogs, "Cannot parse\nfile");
}
}
furi_string_free(temp_str);
furi_string_free(filename);
flipper_format_free(file);
return parsed;
}
bool mag_file_select(MagDevice* mag_dev) {
furi_assert(mag_dev);
// Input events and views are managed by file_browser
FuriString* mag_app_folder;
mag_app_folder = furi_string_alloc_set(MAG_APP_FOLDER);
DialogsFileBrowserOptions browser_options;
dialog_file_browser_set_basic_options(&browser_options, MAG_APP_EXTENSION, &I_mag_file_10px);
browser_options.base_path = MAG_APP_FOLDER;
bool res = dialog_file_browser_show(
mag_dev->dialogs, mag_dev->load_path, mag_app_folder, &browser_options);
furi_string_free(mag_app_folder);
if(res) {
res = mag_device_load_data(mag_dev, mag_dev->load_path, true);
if(res) {
mag_device_set_name(mag_dev, mag_dev->dev_name);
}
}
return res;
}
bool mag_device_delete(MagDevice* mag_dev, bool use_load_path) {
furi_assert(mag_dev);
bool deleted = false;
FuriString* file_path;
file_path = furi_string_alloc();
do {
// Delete original file
if(use_load_path && !furi_string_empty(mag_dev->load_path)) {
furi_string_set(file_path, mag_dev->load_path);
} else {
furi_string_printf(
file_path, "%s/%s%s", MAG_APP_FOLDER, mag_dev->dev_name, MAG_APP_EXTENSION);
}
if(!storage_simply_remove(mag_dev->storage, furi_string_get_cstr(file_path))) break;
deleted = true;
} while(false);
if(!deleted) {
dialog_message_show_storage_error(mag_dev->dialogs, "Cannot remove\nfile");
}
furi_string_free(file_path);
return deleted;
}
bool mag_device_parse_card_string(MagDevice* mag_dev, FuriString* f_card_str) {
furi_assert(mag_dev);
FURI_LOG_D(TAG, "mag_device_parse_card_string");
const char* card_str = furi_string_get_cstr(f_card_str);
FURI_LOG_D(TAG, "Parsing card string: %s", card_str);
// Track 1
const char* track1_start = strchr(card_str, '%');
if(!track1_start) {
FURI_LOG_D(TAG, "Could not find track 1 start");
return false;
}
track1_start++;
const char* track1_end = strchr(track1_start, '?');
if(!track1_end) {
FURI_LOG_D(TAG, "Could not find track 1 end");
return false;
}
size_t track1_len = track1_end - track1_start;
FURI_LOG_D(TAG, "Track 1: %.*s", track1_len, track1_start);
mag_dev->dev_data.track[0].len = track1_len;
furi_string_printf(mag_dev->dev_data.track[0].str, "%%%.*s?", track1_len, track1_start);
// Track 2
const char* track2_start = strchr(track1_end, ';');
if(!track2_start) {
FURI_LOG_D(TAG, "Could not find track 2 start");
return true;
}
track2_start++;
const char* track2_end = strchr(track2_start, '?');
if(!track2_end) {
FURI_LOG_D(TAG, "Could not find track 2 end");
return true;
}
size_t track2_len = track2_end - track2_start;
FURI_LOG_D(TAG, "Track 2: %.*s", track2_len, track2_start);
mag_dev->dev_data.track[1].len = track2_len;
furi_string_printf(mag_dev->dev_data.track[1].str, "%%%.*s?", track2_len, track2_start);
// Track 3
const char* track3_start = strchr(track2_end, ';');
if(!track3_start) {
FURI_LOG_D(TAG, "Could not find track 3 start");
return true;
}
track3_start++;
const char* track3_end = strchr(track3_start, '?');
if(!track3_end) {
FURI_LOG_D(TAG, "Could not find track 3 end");
return true;
}
size_t track3_len = track3_end - track3_start;
FURI_LOG_D(TAG, "Track 3: %.*s", track3_len, track3_start);
mag_dev->dev_data.track[2].len = track3_len;
furi_string_printf(mag_dev->dev_data.track[2].str, "%%%.*s?", track3_len, track3_start);
return true;
}
MagTrackState mag_device_autoselect_track_state(MagDevice* mag_dev) {
// messy code to quickly check which tracks are available for emulation/display
bool is_empty_t1 = furi_string_empty(mag_dev->dev_data.track[0].str);
bool is_empty_t2 = furi_string_empty(mag_dev->dev_data.track[1].str);
bool is_empty_t3 = furi_string_empty(mag_dev->dev_data.track[2].str);
if(!is_empty_t1 && !is_empty_t2) {
return MagTrackStateOneAndTwo;
} else if(!is_empty_t1) {
return MagTrackStateOne;
} else if(!is_empty_t2) {
return MagTrackStateTwo;
} else if(!is_empty_t3) {
return MagTrackStateThree;
}
// if all empty (or something wrong with the above code)
// return default value
return MAG_STATE_DEFAULT_TRACK;
}
void mag_device_set_loading_callback(
MagDevice* mag_dev,
MagLoadingCallback callback,
void* context) {
furi_assert(mag_dev);
mag_dev->loading_cb = callback;
mag_dev->loading_cb_ctx = context;
}