-
Notifications
You must be signed in to change notification settings - Fork 0
/
preset_manager.cpp
483 lines (453 loc) · 16.2 KB
/
preset_manager.cpp
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/**
* @file preset_manager.cpp
* @brief this file supports loading and storing presets in
* non-volatile storage
*
* MIT License
*
* Copyright (c) 2022 rppicomidi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#ifdef NDEBUG
// Need to do this here for release builds or no CLI commands will be added
// All build variants except DEBUG define NDEBUG, which makes assert() macro generate
// no code at all, which prevents msc_demo_cli_init() from adding any CLI commands.
#undef NDEBUG
#endif
#include "preset_manager.h"
#include "midi2piousbhub.h"
#include "diskio.h"
#include "rp2040_rtc.h"
rppicomidi::Preset_manager::Preset_manager()
{
}
void rppicomidi::Preset_manager::init()
{
// Make sure the flash filesystem is working
int error_code = pico_mount(false);
if (error_code != 0) {
printf("Error %s mounting the flash file system\r\nFormatting...", pico_errmsg(error_code));
error_code = pico_mount(true);
if (error_code != 0) {
printf("Error %s mounting the flash file system\r\nFormatting...", pico_errmsg(error_code));
exit(-1);
}
else {
printf("completed\r\n");
}
}
printf("successfully mounted flash file system\r\n");
lfs_file_t file;
error_code = lfs_file_open(&file, current_preset_filename, LFS_O_RDONLY);
if (error_code == LFS_ERR_OK) {
char curr_preset[LFS_NAME_MAX+1];
lfs_ssize_t nread = lfs_file_read(&file, curr_preset, LFS_NAME_MAX);
if (nread > 0) {
curr_preset[nread] = '\0'; // just in case
current_preset_name = std::string(curr_preset);
}
}
else {
printf("Error %s opening file %s for reading\r\n", pico_errmsg(error_code), current_preset_filename);
}
if (pico_unmount() != 0) {
printf("Failed to unmount the flash file system\r\n");
}
msc_fat_init();
}
bool rppicomidi::Preset_manager::save_current_preset(std::string preset_name)
{
// mount the lfs
int error_code = pico_mount(false);
if (error_code != 0) {
printf("Error %s mounting the flash file system\r\n", pico_errmsg(error_code));
return false;
}
std::string ser;
Midi2PioUsbhub::instance().serialize(ser);
lfs_file_t file;
std::string filename = preset_name;
error_code = lfs_file_open(&file, filename.c_str(),
LFS_O_WRONLY | LFS_O_TRUNC | LFS_O_CREAT); // open for write, truncate if it exists and create if it doesn't
if (error_code != LFS_ERR_OK) {
pico_unmount();
printf("error %s opening file %s\r\n", pico_errmsg(error_code), filename.c_str());
return false;
}
lfs_ssize_t size = lfs_file_write(&file, ser.c_str(), ser.length());
error_code = lfs_file_close(&file);
if (size < 0 || size != static_cast<lfs_ssize_t>(ser.length())) {
printf("error %s writing preset data to file %s\r\n", pico_errmsg(size), filename.c_str());
if (error_code != LFS_ERR_OK) {
printf("error %s closing to file %s\r\n", pico_errmsg(error_code), filename.c_str());
}
pico_unmount();
return false;
}
// preset data is written. Now need to update the current preset file
bool result = update_current_preset(preset_name, false);
pico_unmount();
return result;
}
bool rppicomidi::Preset_manager::update_current_preset(std::string& preset_name, bool mount)
{
bool result = true;
// See if the current preset file is present
// If it isn't, clear current_preset_name. This
// can happen if someone deletes the file
// or reformats the the littlefs file system
// outside of the control of Preset_manager.
if (mount) {
int err = pico_mount(false);
if (err != 0) {
printf("Error %s mounting the flash file system\r\n", pico_errmsg(err));
return false;
}
}
lfs_file_t file;
int error_code = lfs_file_open(&file, current_preset_filename, LFS_O_RDONLY); // open for read (see if file exists)
if (error_code == LFS_ERR_OK) {
char curr_preset[LFS_NAME_MAX+1];
lfs_ssize_t nread = lfs_file_read(&file, curr_preset, LFS_NAME_MAX);
if (nread > 0) {
curr_preset[nread] = '\0'; // just in case
current_preset_name = std::string(curr_preset);
}
error_code = lfs_file_close(&file);
if (error_code != LFS_ERR_OK) {
if (mount)
pico_unmount();
return false;
}
}
else {
current_preset_name = "";
}
// only need to do stuff if the preset_name is not the current_preset_name
if (preset_name != current_preset_name) {
int error_code = lfs_file_open(&file, current_preset_filename,
LFS_O_WRONLY | LFS_O_TRUNC | LFS_O_CREAT); // open for write, truncate if it exists and create if it doesn't
if (error_code != LFS_ERR_OK) {
if (mount)
pico_unmount();
printf("error %s opening file %s\r\n", pico_errmsg(error_code), current_preset_filename);
return false;
}
auto size = lfs_file_write(&file, preset_name.c_str(), preset_name.length());
error_code = lfs_file_close(&file);
if (error_code != LFS_ERR_OK) {
result = false;
printf("error %s closing to file %s\r\n", pico_errmsg(error_code), current_preset_filename);
}
if (size < 0 || size != static_cast<lfs_ssize_t>(preset_name.length())) {
printf("error %s writing preset name to file %s\r\n", pico_errmsg(size), current_preset_filename);
result = false;
}
if (result) {
current_preset_name = preset_name;
}
else {
current_preset_name = "";
}
if (mount)
pico_unmount();
}
else if (mount)
pico_unmount();
return result;
}
int rppicomidi::Preset_manager::load_settings_string(const char* settings_filename, char** raw_settings_ptr, bool mount)
{
int error_code = 0;
if (mount)
error_code = pico_mount(false);
if (error_code != 0) {
printf("unexpected error %s mounting flash\r\n", pico_errmsg(error_code));
return error_code;
}
int file = pico_open(settings_filename, LFS_O_RDONLY);
if (file < 0) {
// file isn't there
if (mount)
pico_unmount();
return file; // the error code
}
else {
// file is open. Read the whole contents to a string
auto flen = pico_size(file);
if (flen < 0) {
// Something went wrong
if (mount)
pico_unmount();
return flen;
}
// create a string long enough
*raw_settings_ptr = new char[flen+1];
if (!raw_settings_ptr) {
pico_close(file);
if (mount)
pico_unmount();
return LFS_ERR_NOMEM; // new failed
}
auto nread = pico_read(file, *raw_settings_ptr, flen);
pico_close(file);
if (mount)
pico_unmount();
if (nread == (lfs_size_t)flen) {
// Success!
(*raw_settings_ptr)[flen]='\0'; // just in case, add null termination
return nread;
}
if (nread > 0) {
delete[] *raw_settings_ptr;
*raw_settings_ptr = nullptr;
nread = LFS_ERR_IO;
}
return nread;
}
}
bool rppicomidi::Preset_manager::load_preset(std::string preset_name)
{
char* raw_preset_string;
int error_code = load_settings_string(preset_name.c_str(), &raw_preset_string);
bool result = false;
if (error_code > 0) {
std::string settings = std::string(raw_preset_string);
delete[] raw_preset_string;
if (Midi2PioUsbhub::instance().deserialize(settings)) {
if (update_current_preset(preset_name)) {
printf("load preset %s successful\r\n", preset_name.c_str());
result = true;
}
}
else {
printf("error deserilizating the preset\r\n");
}
}
else {
printf("error %s loading settings %s\r\n", pico_errmsg(error_code), preset_name.c_str());
}
return result;
}
FRESULT rppicomidi::Preset_manager::backup_preset(const char* preset_name, bool mount)
{
int error_code = LFS_ERR_OK;
if (mount) {
error_code = pico_mount(false);
if (error_code != 0) {
printf("unexpected error %s mounting flash\r\n", pico_errmsg(error_code));
return FR_INT_ERR;
}
}
lfs_file_t file;
error_code = lfs_file_open(&file, preset_name, LFS_O_RDONLY);
if (error_code != LFS_ERR_OK) {
printf("error %s opening preset file %s\r\n", pico_errmsg(error_code), preset_name);
if (mount)
pico_unmount();
return FR_INT_ERR;
}
lfs_soff_t sz_src = lfs_file_size(&file);
if (sz_src < 0) {
printf("error %s determining preset file %s size\r\n", pico_errmsg(error_code), preset_name);
lfs_file_close(&file);
if (mount)
pico_unmount();
return FR_INT_ERR;
}
uint8_t buffer[sz_src];
lfs_ssize_t nread = lfs_file_read(&file, buffer, sz_src);
if (nread < 0) {
printf("error %s reading preset file %s\r\n", pico_errmsg(error_code), preset_name);
return FR_INT_ERR;
}
lfs_file_close(&file);
if (mount)
pico_unmount();
DIR dir;
FRESULT res = f_opendir(&dir, preset_dir_name);
if (res == FR_NO_PATH) {
res = f_mkdir(preset_dir_name);
if (res != FR_OK) {
printf("error %d creating directory %s\r\n", res, preset_dir_name);
return res;
}
else {
res = f_opendir(&dir, preset_dir_name);
if (res != FR_OK) {
printf("error %d probing for directory %s\r\n", res, preset_dir_name);
return res;
}
}
}
else if (res != FR_OK) {
printf("error %d probing for directory %s\r\n", res, preset_dir_name);
return res;
}
res = f_closedir(&dir);
if (res != FR_OK) {
printf("error %d closing directory %s\r\n", res, preset_dir_name);
return res;
}
std::string path = std::string(preset_dir_name) + "/" + std::string(preset_name);
FIL fil;
res = f_open(&fil, path.c_str(), FA_WRITE | FA_CREATE_ALWAYS);
if (res == FR_OK) {
UINT nwritten;
res = f_write(&fil, buffer, nread, &nwritten);
f_close(&fil);
if (res != FR_OK) {
printf("error %d writing file %s\r\n", res, path.c_str());
}
else if (nwritten != (UINT)nread) {
printf("error writing %s: nread=%ld nwritten=%u\r\n", path.c_str(), nread, nwritten);
}
}
return res;
}
FRESULT rppicomidi::Preset_manager::backup_all_presets()
{
int error_code = pico_mount(false);
if (error_code != LFS_ERR_OK) {
printf("unexpected error %s mounting flash\r\n", pico_errmsg(error_code));
return FR_INT_ERR;
}
lfs_dir_t dir;
error_code = lfs_dir_open(&dir, "/");
if (error_code != LFS_ERR_OK) {
printf("unexpected error %s opening backup directory /\r\n", pico_errmsg(error_code));
pico_unmount();
return FR_INT_ERR;
}
struct lfs_info info;
while (true) {
lfs_ssize_t nread = lfs_dir_read(&dir, &info);
if (nread < 0) {
printf("errror %s reading preset root directory\r\n", pico_errmsg(nread));
return FR_INT_ERR;
}
if (nread == 0) {
break;
}
if (info.type == LFS_TYPE_REG) {
backup_preset(info.name, false);
}
}
error_code = lfs_dir_close(&dir);
if (error_code) {
return FR_INT_ERR;
}
return FR_OK;
}
FRESULT rppicomidi::Preset_manager::restore_preset(const char* preset_name)
{
FIL fil;
std::string path = std::string(preset_dir_name)+ "/" + std::string(preset_name);
FRESULT res = f_open(&fil,path.c_str(), FA_READ);
if (res != FR_OK) {
printf("error %d opening file %s\r\n", res, path.c_str());
return res;
}
size_t flen = f_size(&fil);
uint8_t buffer[flen];
UINT nread = 0;
res = f_read(&fil, buffer, flen, &nread);
if (res != FR_OK) {
printf("error %d reading file %s\r\n", res, path.c_str());
f_close(&fil);
return res;
}
f_close(&fil);
int error_code = pico_mount(false);
if (error_code != 0) {
printf("unexpected error %s mounting flash\r\n", pico_errmsg(error_code));
return FR_INT_ERR;
}
lfs_file_t file;
error_code = lfs_file_open(&file, preset_name, LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC);
if (error_code != LFS_ERR_OK) {
printf("error %s opening preset file %s for write\r\n", pico_errmsg(error_code), preset_name);
pico_unmount();
return FR_INT_ERR;
}
lfs_ssize_t nwritten = lfs_file_write(&file, buffer, flen);
error_code = lfs_file_close(&file);
pico_unmount();
if (nwritten < 0) {
printf("error %s opening preset file %s for write\r\n", pico_errmsg(error_code), preset_name);
}
else if (nwritten != (lfs_ssize_t)flen) {
printf("File %s nwritten=%ld nread=%u\r\n", preset_name, nwritten, flen);
}
else {
printf("preset %s restored\r\n", preset_name);
}
return FR_OK;
}
//-------------MSC/FATFS IMPLEMENTATION -------------//
static scsi_inquiry_resp_t inquiry_resp;
static FATFS fatfs[FF_VOLUMES];
static_assert(FF_VOLUMES == CFG_TUH_DEVICE_MAX);
bool inquiry_complete_cb(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data)
{
if (cb_data->csw->status != 0) {
printf("Inquiry failed\r\n");
return false;
}
// Print out Vendor ID, Product ID and Rev
printf("%.8s %.16s rev %.4s\r\n", inquiry_resp.vendor_id, inquiry_resp.product_id, inquiry_resp.product_rev);
// Get capacity of device
uint32_t const block_count = tuh_msc_get_block_count(dev_addr, cb_data->cbw->lun);
uint32_t const block_size = tuh_msc_get_block_size(dev_addr, cb_data->cbw->lun);
printf("Disk Size: %lu MB\r\n", block_count / ((1024*1024)/block_size));
printf("Block Count = %lu, Block Size: %lu\r\n", block_count, block_size);
return true;
}
void tuh_msc_mount_cb(uint8_t dev_addr)
{
uint8_t pdrv = mmc_map_next_pdrv(dev_addr);
assert(pdrv < FF_VOLUMES);
msc_fat_plug_in(pdrv);
uint8_t const lun = 0;
tuh_msc_inquiry(dev_addr, lun, &inquiry_resp, inquiry_complete_cb, 0);
char path[3] = "0:";
path[0] += pdrv;
if ( f_mount(&fatfs[pdrv],path, 0) != FR_OK ) {
printf("mount failed\r\n");
return;
if (f_chdrive(path) != FR_OK) {
printf("f_chdrive(%s) failed\r\n", path);
}
}
printf("\r\nMass Storage drive %u is mounted\r\n", pdrv);
printf("Run the set-date and set-time commands so file timestamps are correct\r\n\r\n");
}
void tuh_msc_umount_cb(uint8_t dev_addr)
{
uint8_t pdrv = mmc_unmap_pdrv(dev_addr);
char path[3] = "0:";
path[0] += pdrv;
f_mount(NULL, path, 0); // unmount disk
msc_fat_unplug(pdrv);
printf("Mass Storage drive %u is unmounted\r\n", pdrv);
}
void main_loop_task()
{
rppicomidi::Midi2PioUsbhub::instance().task();
}