-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings_file.cpp
1235 lines (1174 loc) · 39.2 KB
/
settings_file.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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.
*/
// Make asserts work correctly, even for release builds
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include "settings_file.h"
#include "midi_processor_manager.h"
#include "rp2040_rtc.h"
rppicomidi::Settings_file::Settings_file() : vid{0}, pid{0}
{
// 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");
if (pico_unmount() != 0) {
printf("Failed to unmount the flash file system\r\n");
}
}
void rppicomidi::Settings_file::add_all_cli_commands(EmbeddedCli *cli)
{
assert(embeddedCliAddBinding(cli, {
"format",
"format settings file system",
false,
this,
static_file_system_format
}));
assert(embeddedCliAddBinding(cli, {
"fsstat",
"display settings file system status",
false,
this,
static_file_system_status
}));
assert(embeddedCliAddBinding(cli, {
"ls",
"list all settings files",
true,
this,
static_list_files
}));
assert(embeddedCliAddBinding(cli, {
"cat",
"print file contents. usage: cat <fn>",
true,
this,
static_print_file
}));
assert(embeddedCliAddBinding(cli, {
"rm",
"delete. usage: rm <fn>",
true,
this,
static_delete_file
}));
assert(embeddedCliAddBinding(cli, {
"fatcd",
"change current directory. usage: fatcd <path>",
true,
this,
static_fatfs_cd
}));
assert(embeddedCliAddBinding(cli, {
"fatls",
"list current directory. usage: fatls",
false,
this,
static_fatfs_ls
}));
assert(embeddedCliAddBinding(cli, {
"backup",
"backup current presets. usage: backup",
false,
this,
static_fatfs_backup
}));
assert(embeddedCliAddBinding(cli, {
"restore",
"restore presets from back. usage: restore [path] or restore [path to one file]",
true,
this,
static_fatfs_restore
}));
assert(embeddedCliAddBinding(cli, {
"save-screenshots",
"saves screenshots already stored internally to flash",
false,
this,
static_fatfs_save_screenshots
}));
}
void rppicomidi::Settings_file::set_vid_pid(uint16_t vid_, uint16_t pid_)
{
vid = vid_;
pid = pid_;
}
int rppicomidi::Settings_file::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;
}
}
int rppicomidi::Settings_file::load_settings_string(char** raw_settings_ptr)
{
if (vid != 0 && pid != 0) {
char settings_filename[]="0000-0000.json";
get_filename(settings_filename);
return load_settings_string(settings_filename, raw_settings_ptr);
}
return LFS_ERR_INVAL; // vid and pid are not valid yet
}
bool rppicomidi::Settings_file::load()
{
char* raw_settings = NULL;
auto error_code = load_settings_string(&raw_settings);
bool result = false;
if (error_code > 0) {
//printf("load (%04x-%04x):\r\n", vid, pid);
result = Midi_processor_manager::instance().deserialize(raw_settings);
}
else {
char* default_raw_settings = Midi_processor_manager::instance().serialize_default();
if (!default_raw_settings) {
printf("Failed to allocate previous raw settings\r\n");
}
else {
printf("loading defaults:\r\n");
result = Midi_processor_manager::instance().deserialize(default_raw_settings);
json_free_serialized_string(default_raw_settings);
}
}
if (error_code != LFS_ERR_OK)
printf("settings file load error %s\r\n", pico_errmsg(error_code));
if (raw_settings)
delete[] raw_settings;
return result;
}
bool rppicomidi::Settings_file::get_next_backup_directory_name(char* dirname, size_t maxname)
{
// 10 characters for the date plus a null terminator
if (maxname < 11)
return false;
FRESULT fatres = f_chdrive("0:");
if (fatres != FR_OK)
return false; // Need to be able to access the drive
fatres = f_chdir("/");
if (fatres != FR_OK)
return false; // need to at least be able to access the root directory
uint8_t month, day;
uint16_t year;
Rp2040_rtc::instance().get_date(year, month, day);
sprintf(dirname, "%02u-%02u-%04u", month, day, year);
FRESULT res = f_chdir(base_preset_path);
if (res == FR_NO_PATH)
return true; // no backup has ever been done, so the dirname is correct
else if (res != FR_OK) {
return false; // the directory exists, but changing into it failed
}
DIR dir;
uint8_t version = 1;
while(res == FR_OK && version > 0) {
res = f_opendir(&dir, dirname);
if (res == FR_OK) {
res = f_closedir(&dir);
// need enough space for 4 more characters
if (res == FR_OK) {
if (maxname > 14) // 10 characters for the date, 1 for a dash, maximum of 3 for version, plus Null terminator
sprintf(dirname+10, "-%u", version++); // version will wrap around if there are 256 backups on the same date
else
return false; // not enough space for the filename
}
}
}
return (version != 0) && res == FR_NO_PATH;
}
FRESULT rppicomidi::Settings_file::backup_all_presets()
{
FRESULT fatres = f_chdrive("0:");
if (fatres != FR_OK)
return fatres;
fatres = f_chdir("/");
if (fatres != FR_OK)
return fatres;
lfs_dir_t dir;
int err = pico_mount(false);
if (err)
return FR_INT_ERR;
err = lfs_dir_open(&dir, "/");
if (err) {
pico_unmount();
return FR_INT_ERR;
}
struct lfs_info info;
bool flash_file_directory_ok = false;
char dirname[30];
while (true) {
int res = lfs_dir_read(&dir, &info);
if (res < 0) {
lfs_dir_close(&dir);
pico_unmount();
return FR_INT_ERR;
}
if (res == 0) {
break;
}
// There is at least one settings file. Make sure there is a directory to store it in.
// Presets are stored in 0:/rppicomidi-pico-usb-midi-processor/date[-version for that date]
if (!flash_file_directory_ok) {
if (!get_next_backup_directory_name(dirname, sizeof(dirname))) {
lfs_dir_close(&dir);
pico_unmount();
return FR_INT_ERR;
}
fatres = f_chdir(base_preset_path);
if (fatres == FR_NO_PATH) {
fatres = f_mkdir(base_preset_path);
if (fatres != FR_OK) {
lfs_dir_close(&dir);
pico_unmount();
return fatres;
}
}
fatres = f_chdir(base_preset_path);
if (fatres != FR_OK) {
lfs_dir_close(&dir);
pico_unmount();
return fatres;
}
fatres = f_mkdir(dirname);
if (fatres != FR_OK) {
lfs_dir_close(&dir);
pico_unmount();
return fatres;
}
fatres = f_chdir(dirname);
if (fatres != FR_OK) {
lfs_dir_close(&dir);
pico_unmount();
return fatres;
}
flash_file_directory_ok = true;
}
char* raw_settings = nullptr;
if(info.type == LFS_TYPE_REG) {
int nread = load_settings_string(info.name, &raw_settings, false);
if (nread > 0) {
FIL bufile;
fatres = f_open(&bufile, info.name, FA_CREATE_NEW | FA_WRITE);
if (fatres != FR_OK) {
lfs_dir_close(&dir);
pico_unmount();
return fatres;
}
UINT written;
fatres = f_write(&bufile, raw_settings, nread, &written);
delete[] raw_settings;
f_close(&bufile);
if (fatres != FR_OK) {
lfs_dir_close(&dir);
pico_unmount();
return fatres;
}
printf("backed up preset 0:%s/%s/%s\r\n", base_preset_path, dirname, info.name);
}
else {
if (raw_settings) {
delete[] raw_settings;
}
lfs_dir_close(&dir);
pico_unmount();
return FR_INT_ERR;
}
}
}
err = lfs_dir_close(&dir);
if (err) {
pico_unmount();
return FR_INT_ERR;
}
pico_unmount();
return FR_OK;
}
FRESULT rppicomidi::Settings_file::restore_one_file(const char* fullpath, const char* filename)
{
printf("Restoring %s\r\n", filename);
FIL file;
FRESULT fatres = f_open(&file, fullpath, FA_READ);
if (fatres != FR_OK) {
printf("error %u opening file %s\r\n", fatres, fullpath);
return fatres;
}
UINT filesize = f_size(&file);
char* buffer = new char[filesize];
UINT bytes_read;
fatres = f_read(&file, buffer, filesize, &bytes_read);
f_close(&file);
if (fatres != FR_OK) {
printf("error %u reading file %s\r\n", fatres, fullpath);
return fatres;
}
int error_code = pico_mount(false);
if (error_code != 0) {
free((void*)buffer);
printf("unexpected error %s mounting flash\r\n", pico_errmsg(error_code));
return FR_INT_ERR;
}
printf("opening file %s for read only\r\n", filename);
int localfile = pico_open(filename, LFS_O_RDONLY);
if (localfile < 0) {
// file does not exist
printf("opening/creating file %s for read/write\r\n", filename);
localfile = pico_open(filename, LFS_O_WRONLY | LFS_O_CREAT);
if (localfile < 0) {
printf("error %s creating %s for writing\r\n", pico_errmsg(localfile), filename);
free((void*)buffer);
pico_unmount();
return FR_INT_ERR;
}
}
else {
// file exists. Truncate it to 0 length in preparation for new data
error_code = pico_close(localfile);
if (error_code != LFS_ERR_OK) {
printf("error %s closing %s\r\n", pico_errmsg(error_code), filename);
free((void*)buffer);
pico_unmount();
return FR_INT_ERR;
}
localfile = pico_open(filename, LFS_O_WRONLY);
if (localfile < 0) {
printf("error %s opening %s for writing\r\n", pico_errmsg(localfile), filename);
free((void*)buffer);
pico_unmount();
return FR_INT_ERR;
}
}
printf("writing settings to file %s\r\n", filename);
// file is open for writing; write the settings
error_code = pico_write(localfile, buffer, strlen(buffer)+1);
free(buffer);
pico_close(localfile);
pico_unmount();
if (error_code < 0) {
printf("error %s writing settings to file\r\n", pico_errmsg(error_code));
return FR_INT_ERR;
}
else if ((size_t)error_code < strlen(buffer)) {
printf("store: Only %d bytes stored out of %u\r\n", error_code, strlen(buffer));
// hmm. no idea why that should happen
return FR_INT_ERR;
}
return FR_OK;
}
bool rppicomidi::Settings_file::get_setting_file_json_string(const char* directory, const char* filename, char** json_string)
{
FIL file;
char restore_path[256];
size_t max_path = sizeof(restore_path)-1;
strncpy(restore_path, base_preset_path, max_path);
strncat(restore_path, "/", max_path);
strncat(restore_path, directory, max_path);
strncat(restore_path, "/", max_path);
strncat(restore_path, filename, max_path);
restore_path[max_path] = '\0';
FRESULT fatres = f_open(&file, restore_path, FA_READ);
*json_string = nullptr;
if (fatres != FR_OK) {
printf("error %u opening file %s\r\n", fatres, restore_path);
return false;
}
UINT filesize = f_size(&file);
char* buffer = new char[filesize];
UINT bytes_read;
fatres = f_read(&file, buffer, filesize, &bytes_read);
f_close(&file);
if (fatres != FR_OK) {
printf("error %u reading file %s\r\n", fatres, restore_path);
delete[] buffer;
return false;
}
*json_string = buffer;
return true;
}
FRESULT rppicomidi::Settings_file::restore_presets(const char* backup_path)
{
FRESULT fatres = f_chdrive("0:");
if (fatres != FR_OK)
return fatres;
if (strlen(backup_path) >= strlen(".json")) {
char* ptr = strstr(backup_path, ".json");
if (ptr != nullptr && strlen(ptr) == strlen(".json")) {
// should be a single file
char fullpath[strlen(base_preset_path) + 1 + strlen(backup_path)]; // need space for base_preset path '/' backup_path
strcpy(fullpath, base_preset_path);
strcat(fullpath, "/");
strcat(fullpath, backup_path);
char* filename = strstr(fullpath, ".json") - 9;
if ((ptr - 9) >= backup_path && *(ptr-10) == '/') {
fatres = restore_one_file(fullpath, filename);
}
else {
printf("poorly formed backup_path=%s\r\n", backup_path);
fatres = FR_INVALID_PARAMETER;
}
}
else {
// need to restore every file in the directory
char fullpath[strlen(base_preset_path) + 1 + strlen(backup_path)+16]; // need space for base_preset path '/' backup_path + /xxxx-yyyy.json
char* filename = fullpath + strlen(base_preset_path) + strlen(backup_path) + 2; // +2 is for the two '/' characters
strcpy(fullpath, base_preset_path);
strcat(fullpath, "/");
strcat(fullpath, backup_path);
DIR dir;
fatres = f_opendir(&dir, fullpath);
if (fatres == FR_OK) {
FILINFO info;
strcat(fullpath, "/");
fatres = f_readdir(&dir, &info);
while (fatres == FR_OK && info.fname[0] != 0) {
strncpy(filename, info.fname, 16);
filename[15] = '\0';
if (strncmp(filename+9, ".json", 5) != 0) {
// filename is not formed correctly
fatres = FR_NO_FILE;
}
else {
fatres = restore_one_file(fullpath, filename);
if (fatres == FR_OK) {
fatres = f_readdir(&dir, &info);
}
else {
printf("error %u restoring file %s\r\n", fatres, filename);
}
}
}
f_closedir(&dir);
}
else {
printf("error opening directory %s\r\n", fullpath);
}
}
}
else {
fatres = FR_INVALID_PARAMETER;
}
return fatres;
}
bool rppicomidi::Settings_file::get_all_preset_directory_names(std::vector<std::string>& dirname_list)
{
FRESULT fatres = f_chdrive("0:");
if (fatres != FR_OK)
return false;
DIR dir;
fatres = f_opendir(&dir, base_preset_path);
if (fatres != FR_OK) {
return false;
}
FILINFO info;
fatres = f_readdir(&dir, &info);
while (fatres == FR_OK && info.fname[0] != 0) {
if (info.fattrib & AM_DIR) {
dirname_list.push_back(std::string(info.fname));
}
fatres = f_readdir(&dir, &info);
}
return fatres == FR_OK;
}
bool rppicomidi::Settings_file::get_all_preset_filenames(const char* directory_name, std::vector<std::string>& filename_list)
{
FRESULT fatres = f_chdrive("0:");
if (fatres != FR_OK)
return false;
fatres = f_chdir(base_preset_path);
if (fatres != FR_OK)
return false;
DIR dir;
fatres = f_opendir(&dir, directory_name);
if (fatres != FR_OK) {
return false;
}
FILINFO info;
fatres = f_readdir(&dir, &info);
while (fatres == FR_OK && info.fname[0] != 0) {
if ((info.fattrib & AM_DIR) == 0)
filename_list.push_back(std::string(info.fname));
fatres = f_readdir(&dir, &info);
}
return fatres == FR_OK;
}
bool rppicomidi::Settings_file::load_preset(uint8_t next_preset)
{
char* raw_settings = NULL;
auto error_code = load_settings_string(&raw_settings);
bool result = false;
if (error_code > 0) {
printf("load (%04x-%04x):\r\n", vid, pid);
result = Midi_processor_manager::instance().deserialize_preset(next_preset, raw_settings);
}
else {
char* default_raw_settings = Midi_processor_manager::instance().serialize_default();
if (!default_raw_settings) {
printf("Failed to allocate previous raw settings\r\n");
}
else {
printf("loading defaults:\r\n");
result = Midi_processor_manager::instance().deserialize_preset(next_preset, default_raw_settings);
json_free_serialized_string(default_raw_settings);
}
}
printf("settings file load error %s\r\n", pico_errmsg(error_code));
if (raw_settings)
delete[] raw_settings;
return result;
}
int rppicomidi::Settings_file::store()
{
// Get previous settings values
char* previous_raw_settings = NULL;
int error_code = load_settings_string(&previous_raw_settings);
if (error_code < 0) {
if (previous_raw_settings)
delete[] previous_raw_settings;
previous_raw_settings = Midi_processor_manager::instance().serialize_default();
if (!previous_raw_settings) {
printf("Failed to allocate previous raw settings\r\n");
return error_code;
}
}
// Serialize to a string for storage
char* settings_str = Midi_processor_manager::instance().serialize(Midi_processor_manager::instance().get_current_preset(), previous_raw_settings);
if (settings_str) {
printf("store (%04x-%04x):\r\n",vid,pid);
}
else {
return LFS_ERR_NOMEM;
}
// Move current settings file to a backup file if current settings file exists
error_code = pico_mount(false);
if (error_code != 0) {
free((void*)settings_str);
printf("unexpected error %s mounting flash\r\n", pico_errmsg(error_code));
return error_code;
}
char settings_filename[]="0000-0000.json";
get_filename(settings_filename);
printf("opening file %s for read only\r\n", settings_filename);
int file = pico_open(settings_filename, LFS_O_RDONLY);
if (file < 0) {
// file does not exist
printf("opening/creating file %s for read/write\r\n", settings_filename);
file = pico_open(settings_filename, LFS_O_WRONLY | LFS_O_CREAT);
if (file < 0) {
printf("error %s creating %s for writing\r\n", pico_errmsg(file), settings_filename);
free((void*)settings_str);
pico_unmount();
return file;
}
}
else {
// file exists. Truncate it to 0 length in preparation for new data
error_code = pico_close(file);
if (error_code != LFS_ERR_OK) {
printf("error %s closing %s\r\n", pico_errmsg(error_code), settings_filename);
free((void*)settings_str);
pico_unmount();
return file;
}
file = pico_open(settings_filename, LFS_O_WRONLY);
if (file < 0) {
printf("error %s opening %s for writing\r\n", pico_errmsg(file), settings_filename);
free((void*)settings_str);
pico_unmount();
return file;
}
}
printf("writing settings to file %s\r\n", settings_filename);
// file is open for writing; write the settings
error_code = pico_write(file, settings_str, strlen(settings_str)+1);
free(settings_str);
pico_close(file);
pico_unmount();
if (error_code < 0) {
printf("error %s writing settings to file\r\n", pico_errmsg(error_code));
return error_code;
}
else if ((size_t)error_code < strlen(settings_str)) {
printf("store: Only %d bytes stored out of %u\r\n", error_code, strlen(settings_str));
// hmm. no idea why that should happen
return LFS_ERR_IO;
}
return LFS_ERR_OK;
}
int rppicomidi::Settings_file::lfs_ls(const char *path)
{
lfs_dir_t dir;
int err = lfs_dir_open(&dir, path);
if (err) {
return err;
}
struct lfs_info info;
while (true) {
int res = lfs_dir_read(&dir, &info);
if (res < 0) {
return res;
}
if (res == 0) {
break;
}
switch (info.type) {
case LFS_TYPE_REG: printf("reg "); break;
case LFS_TYPE_DIR: printf("dir "); break;
default: printf("? "); break;
}
static const char *prefixes[] = {"", "K", "M", "G"};
for (int i = sizeof(prefixes)/sizeof(prefixes[0])-1; i >= 0; i--) {
if (info.size >= static_cast<size_t>((1 << 10*i)-1)) {
printf("%*lu%sB ", 4-(i != 0), info.size >> 10*i, prefixes[i]);
break;
}
}
printf("%s\n", info.name);
}
err = lfs_dir_close(&dir);
if (err) {
return err;
}
return 0;
}
void rppicomidi::Settings_file::static_file_system_format(EmbeddedCli*, char*, void*)
{
printf("formatting settings file system then mounting it\r\n");
int error_code = pico_mount(true);
if (error_code != LFS_ERR_OK) {
}
else {
error_code = pico_unmount();
if (error_code != LFS_ERR_OK) {
printf("unexpected error %s unmounting settings file system\r\n", pico_errmsg(error_code));
}
else {
printf("File system successfully formated and unmounted\r\n");
}
}
}
void rppicomidi::Settings_file::static_file_system_status(EmbeddedCli*, char*, void*)
{
int error_code = pico_mount(false);
if (error_code != LFS_ERR_OK) {
printf("can't mount settings file system\r\n");
return;
}
struct pico_fsstat_t stat;
if (pico_fsstat(&stat) == LFS_ERR_OK) {
printf("FS: blocks %d, block size %d, used %d\n", (int)stat.block_count, (int)stat.block_size,
(int)stat.blocks_used);
}
else {
printf("could not read file system status\r\n");
}
error_code = pico_unmount();
if (error_code != LFS_ERR_OK) {
printf("can't unmount settings file system\r\n");
return;
}
}
void rppicomidi::Settings_file::static_list_files(EmbeddedCli* cli, char* args, void* context)
{
(void)cli;
(void)args;
char path[256] = {'/','\0'};
uint16_t argc = embeddedCliGetTokenCount(args);
if (argc == 1) {
strncpy(path, embeddedCliGetToken(args, 1), sizeof(path)-1);
path[sizeof(path)-1] = '\0';
}
else {
printf("usage: ls [path]\r\n");
}
auto me = reinterpret_cast<Settings_file*>(context);
int error_code = pico_mount(false);
if (error_code != LFS_ERR_OK) {
printf("can't mount settings file system\r\n");
return;
}
error_code = me->lfs_ls(path);
if (error_code != LFS_ERR_OK) {
printf("error listing path \"/\"\r\n");
}
error_code = pico_unmount();
if (error_code != LFS_ERR_OK) {
printf("can't unmount settings file system\r\n");
return;
}
}
void rppicomidi::Settings_file::print_fat_date(WORD wdate)
{
uint16_t year = 1980 + ((wdate >> 9) & 0x7f);
uint16_t month = (wdate >> 5) & 0xf;
uint16_t day = wdate & 0x1f;
printf("%02u/%02u/%04u\t", month, day, year);
}
void rppicomidi::Settings_file::print_fat_time(WORD wtime)
{
uint8_t hour = ((wtime >> 11) & 0x1f);
uint8_t min = ((wtime >> 5) & 0x3F);
uint8_t sec = ((wtime &0x1f)*2);
printf("%02u:%02u:%02u\t", hour, min, sec);
}
FRESULT rppicomidi::Settings_file::scan_files(const char* path)
{
FRESULT res;
DIR dir;
static FILINFO fno;
res = f_opendir(&dir, path); /* Open the directory */
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno); /* Read a directory item */
if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
printf("%lu\t",fno.fsize);
print_fat_date(fno.fdate);
print_fat_time(fno.ftime);
printf("%s%c\r\n",fno.fname, (fno.fattrib & AM_DIR) ? '/' : ' ');
}
f_closedir(&dir);
}
return res;
}
void rppicomidi::Settings_file::static_fatfs_ls(EmbeddedCli *cli, char *args, void *context)
{
(void)cli;
(void)args;
(void)context;
FRESULT res = instance().scan_files(".");
if (res != FR_OK) {
printf("Error %u listing files on drive\r\n", res);
}
}
void rppicomidi::Settings_file::static_fatfs_backup(EmbeddedCli *cli, char *args, void *context)
{
(void)cli;
(void)args;
(void)context;
FRESULT res = instance().backup_all_presets();
if (res != FR_OK) {
printf("Error %u backing up files on drive\r\n", res);
}
}
void rppicomidi::Settings_file::static_fatfs_restore(EmbeddedCli* cli, char* args, void*)
{
(void)cli;
uint16_t argc = embeddedCliGetTokenCount(args);
FRESULT res;
char path[256];
if (argc == 1) {
strncpy(path, embeddedCliGetToken(args, 1), sizeof(path)-1);
res = Settings_file::instance().restore_presets(path);
}
else {
printf("usage: fatcd <new path>\r\n");
return;
}
if (res != FR_OK) {
printf("error %u restoring presets from %s\r\n", res, path);
}
}
void rppicomidi::Settings_file::static_fatfs_save_screenshots(EmbeddedCli*, char*, void*)
{
FRESULT res = instance().export_all_screenshots();
if (res != FR_OK) {
printf("error %u saving screenshots\r\n", res);
}
}
void rppicomidi::Settings_file::static_fatfs_cd(EmbeddedCli* cli, char* args, void* context)
{
(void)cli;
(void)context;
uint16_t argc = embeddedCliGetTokenCount(args);
FRESULT res;
char temp_cwd[256] = {'/', '\0'};
if (argc == 0) {
res = f_chdir(temp_cwd);
}
else if (argc == 1) {
strncpy(temp_cwd, embeddedCliGetToken(args, 1), sizeof(temp_cwd)-1);
temp_cwd[sizeof(temp_cwd)-1] = '\0';
res = f_chdir(temp_cwd);
}
else {
printf("usage: fatcd <new path>\r\n");
return;
}
if (res != FR_OK) {
printf("error %u setting cwd to %s\r\n", res, temp_cwd);
}
res = f_getcwd(temp_cwd, sizeof(temp_cwd));
if (res == FR_OK)
printf("cwd=\"%s\"\r\n", temp_cwd);
else
printf("error %u getting cwd\r\n", res);
}
void rppicomidi::Settings_file::static_print_file(EmbeddedCli* cli, char* args, void* context)
{
(void)cli;
auto me = reinterpret_cast<Settings_file*>(context);
if (embeddedCliGetTokenCount(args) != 1) {
printf("usage: cat <filename>");
return;
}
const char* fn=embeddedCliGetToken(args, 1);
char* raw_settings;
int error_code = me->load_settings_string(fn, &raw_settings);
if (error_code > 0) {
printf("File: %s\r\n%s\r\n", fn, raw_settings);
delete[] raw_settings;
}
else {
switch(error_code) {
case LFS_ERR_NOENT:
printf("File %s does not exist\r\n", fn);
break;
case LFS_ERR_ISDIR:
printf("%s is a directory\r\n",fn);
break;
case LFS_ERR_CORRUPT:
printf("%s is corrupt\r\n",fn);
break;
case LFS_ERR_IO:
printf("IO Error reading %s\r\n", fn);
break;
default:
printf("Unexpected Error %s reading %s\r\n", pico_errmsg(error_code), fn);
break;
}
}
}
int rppicomidi::Settings_file::delete_file(const char* filename, bool mount)
{
int error_code = LFS_ERR_OK;
if (mount)
error_code = pico_mount(false);
if (error_code == LFS_ERR_OK) {
error_code = pico_remove(filename);
if (error_code != LFS_ERR_OK) {
switch(error_code) {
case LFS_ERR_NOENT:
printf("File %s does not exist\r\n", filename);
break;
case LFS_ERR_ISDIR:
printf("%s is a directory\r\n",filename);
break;
case LFS_ERR_CORRUPT:
printf("%s is corrupt\r\n",filename);
break;
case LFS_ERR_IO:
printf("IO Error deleting %s\r\n", filename);
break;
default:
printf("Unexpected Error %s deleting %s\r\n", pico_errmsg(error_code), filename);
break;
}
}
if (mount)
error_code = pico_unmount();
if (error_code != LFS_ERR_OK) {
printf("Unexpected Error %s unmounting settings file system\r\n", pico_errmsg(error_code));
}
}
return error_code;
}
int rppicomidi::Settings_file::delete_all_files(const char* path)
{
int error_code = pico_mount(false);