-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDLogger.cpp
1161 lines (915 loc) · 26.8 KB
/
SDLogger.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
#include "SDLogger.hpp"
SDLogger::SDLogger(sd_logger_config_t cfg)
: initialized(false)
, mounted(false)
, cfg(cfg)
, pdrv(FF_DRV_NOT_USED)
{
spi_bus_config_t spi_bus_cfg = {.mosi_io_num = cfg.io_mosi,
.miso_io_num = cfg.io_miso,
.sclk_io_num = cfg.io_sclk,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4000};
cfg.sdmmc_host.max_freq_khz = cfg.sclk_speed_hz / 100000UL;
ESP_ERROR_CHECK(spi_bus_initialize(static_cast<spi_host_device_t>(cfg.sdmmc_host.slot), &spi_bus_cfg, SDSPI_DEFAULT_DMA));
slot_cfg = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_cfg.gpio_cs = cfg.io_cs;
slot_cfg.host_id = static_cast<spi_host_device_t>(cfg.sdmmc_host.slot);
}
SDLogger::~SDLogger()
{
close_all_files();
}
bool SDLogger::init()
{
const char* SUB_TAG = "SD->init()";
esp_err_t err = ESP_OK;
int card_hdl = -1;
err = (cfg.sdmmc_host.init)();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "%s: Host init failed.", SUB_TAG);
return initialized;
}
err = sdspi_host_init_device(&slot_cfg, &card_hdl);
if (err != ESP_OK)
{
// de-initialize host
if (cfg.sdmmc_host.flags & SDMMC_HOST_FLAG_DEINIT_ARG)
(cfg.sdmmc_host.deinit_p)(cfg.sdmmc_host.slot);
else
(cfg.sdmmc_host.deinit)();
ESP_LOGE(TAG, "%s: Slot init failed.", SUB_TAG);
return initialized;
}
if (card_hdl != cfg.sdmmc_host.slot)
cfg.sdmmc_host.slot = card_hdl;
card.host.command_timeout_ms = 4000U;
for (int trials = 0; trials < 3; trials++)
{
// sdmmc_card_init can take awhile to run, delay here to reset task watchdog and give time for init call
vTaskDelay(10 / portTICK_PERIOD_MS);
err = sdmmc_card_init(&cfg.sdmmc_host, &card);
if (err == ESP_OK)
break;
}
if (err != ESP_OK)
{
ESP_LOGE(TAG, "%s: sdmmc_card_init() call failure.", SUB_TAG);
return initialized;
}
initialized = true;
return initialized;
}
bool SDLogger::mount(size_t unit_size, int max_open_files, const char* path)
{
const char* SUB_TAG = "SD->mount()";
esp_err_t err = ESP_OK;
FRESULT res = FR_OK;
if (!initialized)
{
ESP_LOGE(TAG, "%s: Card not initialized.", SUB_TAG);
return false;
}
if (mounted)
{
ESP_LOGE(TAG, "%s: Drive already mounted.", SUB_TAG);
return false;
}
if (open_files.size() > 0)
close_all_files();
this->max_open_files = max_open_files;
if (strlen(path) + 1 > MAX_ROOT_PATH_SZ)
{
ESP_LOGE(TAG, "%s: Max root path length exceeded.", SUB_TAG);
return false;
}
strcpy(root_path, path);
if(!get_and_register_free_drive(SUB_TAG))
return false;
err = esp_vfs_fat_register(root_path, drv, max_open_files, &fs);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "%s: esp_vfs_fat_register() call failed 0x(%x)", SUB_TAG, err);
unmount();
return false;
}
res = f_mount(fs, drv, 1);
if (res != FR_OK)
{
print_fatfs_error(res, SUB_TAG, "f_mount()");
return false;
}
mounted = true;
load_info(); // ignore return statement, info can fail to load but card can still be mounted and usable
return true;
}
bool SDLogger::unmount()
{
const char* SUB_TAG = "SD->unmount()";
FRESULT res = FR_OK;
if (!mounted)
{
ESP_LOGW(TAG, "%s: Drive already unmounted", SUB_TAG);
return false;
}
if (open_files.size() > 0)
close_all_files();
res = f_mount(nullptr, drv, 0); // unregister file system object and unmount
if (res != FR_OK)
{
print_fatfs_error(res, SUB_TAG, "f_mount()");
}
else
{
ff_diskio_unregister(pdrv);
esp_vfs_fat_unregister_path(root_path);
mounted = false;
}
return (res == FR_OK);
}
bool SDLogger::is_mounted()
{
return mounted;
}
bool SDLogger::is_initialized()
{
return initialized;
}
bool SDLogger::format(size_t unit_size)
{
const constexpr char* SUB_TAG = "SD->format()";
FRESULT res = FR_OK;
const constexpr size_t work_buff_sz = 4096;
void* work_buff = nullptr;
size_t alloc_unit_sz = 0;
if (!initialized)
{
ESP_LOGE(TAG, "%s: Card not initialized.", SUB_TAG);
return false;
}
res = f_mount(nullptr, drv, 0);
if (res != FR_OK)
{
ESP_LOGE(TAG, "%s: Unmount failed (%d)", SUB_TAG, res);
return false;
}
work_buff = ff_memalloc(work_buff_sz);
if (work_buff == nullptr)
{
ESP_LOGE(TAG, "%s: No heap memory available for work buffer.", SUB_TAG);
return false;
}
alloc_unit_sz = esp_vfs_fat_get_allocation_unit_size(card.csd.sector_size, unit_size);
if (!mounted)
{
if(!get_and_register_free_drive(SUB_TAG))
return false;
}
// partition sd card
LBA_t plist[] = {100, 0, 0, 0}; // format entire drive, see f_fdisk documentation on elm-chan.org
res = f_fdisk(pdrv, plist, work_buff);
if (res != FR_OK)
{
print_fatfs_error(res, SUB_TAG, "f_fdisk()");
free(work_buff);
if (!mounted)
ff_diskio_unregister(pdrv);
return false;
}
// make the file system
const MKFS_PARM opt = {static_cast<BYTE>(FM_ANY), 0, 0, 0, alloc_unit_sz};
res = f_mkfs(drv, &opt, work_buff, work_buff_sz);
free(work_buff);
if (res != FR_OK)
{
print_fatfs_error(res, SUB_TAG, "f_mkfs()");
if (!mounted)
ff_diskio_unregister(pdrv);
return false;
}
if (!mounted)
ff_diskio_unregister(pdrv);
return true;
}
bool SDLogger::get_info(sd_info_t& sd_info)
{
const constexpr char* SUB_TAG = "SD->get_info()";
if (!usability_check(SUB_TAG))
return false;
if (!info.initialized)
{
ESP_LOGE(TAG, "%s: Card info never loaded.", SUB_TAG);
return false;
}
sd_info = info;
return true;
}
void SDLogger::print_info()
{
const constexpr char* SUB_TAG = "SD->print_info()";
if (!usability_check(SUB_TAG))
return;
if (!info.initialized)
{
ESP_LOGE(TAG, "%s: Card info never loaded.", SUB_TAG);
return;
}
ESP_LOGI(TAG,
"\n ------ SD Info ------ \n"
"Name: %s \n"
"Type: %s \n"
"Speed (MHz): %.2f \n"
"Size (MB): %lu \n"
"SSR->Bus Width: %d \n"
"CSD->Version: %d \n"
"CSD->Sector Size: %d \n"
"CSD->Capacity (bytes): %llu \n"
"CSD->Read Block Length: %d \n"
"-------------------- \n",
info.name, info.type, info.speed_mhz, info.size_mb, info.ssr_bus_width, info.csd.ver, info.csd.sector_sz, info.csd.capacity,
info.csd.read_bl_len);
}
const char* SDLogger::get_root_path()
{
const constexpr char* SUB_TAG = "SD->get_root_path()";
if (!usability_check(SUB_TAG))
return nullptr;
else
return root_path;
}
void SDLogger::print_fatfs_error(FRESULT f_res, const char* SUBTAG, const char* fatfs_fxn)
{
char res_str[40];
fatfs_res_to_str(f_res, res_str);
ESP_LOGE(TAG, "%s: %s did not return FR_OK, FRESULT: %s", SUBTAG, fatfs_fxn, res_str);
}
bool SDLogger::posix_perms_2_fatfs_perms(const char* posix_perms, uint8_t& fatfs_perms)
{
auto idx = permission_flag_map.find(posix_perms);
if (idx != permission_flag_map.end())
{
fatfs_perms = idx->second;
return true;
}
return false;
}
bool SDLogger::path_exists(const char* path, const char* SUB_TAG, bool suppress_no_dir_warning)
{
FRESULT res = FR_OK;
if (!usability_check(SUB_TAG))
return false;
res = f_stat(path, nullptr);
if (res != FR_OK && res != FR_NO_FILE)
print_fatfs_error(res, SUB_TAG, "f_stat()");
else if (res == FR_NO_FILE && !suppress_no_dir_warning)
ESP_LOGW(TAG, "%s: File or path does not exist.", SUB_TAG);
return (res == FR_OK);
}
bool SDLogger::get_and_register_free_drive(const char* SUB_TAG)
{
pdrv = FF_DRV_NOT_USED;
if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == FF_DRV_NOT_USED)
{
ESP_LOGE(TAG, "%s: Max volumes already mounted, could not take sdmmc driver resource.", SUB_TAG);
return false;
}
ff_diskio_register_sdmmc(pdrv, &card);
drv[0] = static_cast<char>('0' + pdrv);
return true;
}
void SDLogger::fatfs_res_to_str(FRESULT f_res, char* dest_str)
{
switch (f_res)
{
case FR_OK:
sprintf(dest_str, "FR_OK");
break;
case FR_DISK_ERR:
sprintf(dest_str, "FR_DISK_ERR");
break;
case FR_INT_ERR:
sprintf(dest_str, "FR_INT_ERR");
break;
case FR_NOT_READY:
sprintf(dest_str, "FR_NOT_READY");
break;
case FR_NO_FILE:
sprintf(dest_str, "FR_NO_FILE");
break;
case FR_NO_PATH:
sprintf(dest_str, "FR_NO_PATH");
break;
case FR_INVALID_NAME:
sprintf(dest_str, "FR_INVALID_NAME");
break;
case FR_DENIED:
sprintf(dest_str, "FR_DENIED");
break;
case FR_EXIST:
sprintf(dest_str, "FR_EXIST");
break;
case FR_INVALID_OBJECT:
sprintf(dest_str, "FR_INVALID_OBJECT");
break;
case FR_WRITE_PROTECTED:
sprintf(dest_str, "FR_WRITE_PROTECTED");
break;
case FR_INVALID_DRIVE:
sprintf(dest_str, "FR_INVALID_DRIVE");
break;
case FR_NOT_ENABLED:
sprintf(dest_str, "FR_NOT_ENABLED");
break;
case FR_NO_FILESYSTEM:
sprintf(dest_str, "FR_NO_FILESYSTEM");
break;
case FR_MKFS_ABORTED:
sprintf(dest_str, "FR_MKFS_ABORTED");
break;
case FR_TIMEOUT:
sprintf(dest_str, "FR_TIMEOUT");
break;
case FR_LOCKED:
sprintf(dest_str, "FR_LOCKED");
break;
case FR_NOT_ENOUGH_CORE:
sprintf(dest_str, "FR_NOT_ENOUGH_CORE");
break;
case FR_TOO_MANY_OPEN_FILES:
sprintf(dest_str, "FR_TOO_MANY_OPEN_FILES");
break;
case FR_INVALID_PARAMETER:
sprintf(dest_str, "FR_INVALID_PARAMETER");
break;
default:
sprintf(dest_str, "UNKNOWN_CODE");
break;
}
}
bool SDLogger::open_file(SDFile file, const char* permissions)
{
const constexpr char* SUB_TAG = "SD->open_file()";
char full_path[100];
FRESULT res;
uint8_t fatfs_mode = 0;
if (!usability_check(SUB_TAG))
return false;
if (!file || !file->initialized)
{
ESP_LOGE(TAG, "%s: File not correctly initialized.", SUB_TAG);
return false;
}
if (open_files.size() + 1 > max_open_files)
{
ESP_LOGE(TAG, "%s: Max files already opened.", SUB_TAG);
return false;
}
if (!posix_perms_2_fatfs_perms(permissions, fatfs_mode))
{
ESP_LOGE(TAG, "%s: Invalid posix permission flag.", SUB_TAG);
return false;
}
// build directory path if it does not exist
if (strcmp(file->directory_path, "") != 0)
{
if (!path_exists(file->directory_path, SUB_TAG, true))
if (!build_path(file->directory_path))
{
ESP_LOGE(TAG, "%s: Directory does not exist and path failed to build.", SUB_TAG);
}
}
// append root path to file path to create the full path
strcpy(full_path, root_path);
strcat(full_path, file->path);
// open the file
res = f_open(&file->stream, file->path, fatfs_mode);
if (res != FR_OK)
{
print_fatfs_error(res, SUB_TAG, "f_open()");
return false;
}
// add pointer newly opened file to open_files vector
open_files.push_back(file);
file->open = true;
return true;
}
bool SDLogger::close_file(SDFile file)
{
const constexpr char* SUB_TAG = "SD->close_file()";
bool found = false;
int idx = 0;
FRESULT res = FR_OK;
if (!usability_check(SUB_TAG))
return false;
if (!file || !file->initialized)
{
ESP_LOGE(TAG, "%s: File not correctly initialized.", SUB_TAG);
return false;
}
if (open_files.size() != 0)
for (int i = 0; i < open_files.size(); i++)
{
if (strcmp(open_files[i]->path, file->path) == 0)
{
res = f_close(&file->stream);
if (res != FR_OK)
{
print_fatfs_error(res, SUB_TAG, "f_close()");
}
idx = i;
found = true;
}
}
if (found)
{
open_files.erase(open_files.begin() + idx);
open_files.shrink_to_fit();
file->open = false;
}
else
{
ESP_LOGW(TAG, "%s: No matching open file found for path: %s", SUB_TAG, file->get_path());
}
return found;
}
bool SDLogger::close_all_files()
{
const constexpr char* SUB_TAG = "SD->close_all_files()";
FRESULT res = FR_OK;
for (SDFile& f : open_files)
{
res = f_close(&f->stream);
if (res != FR_OK)
{
print_fatfs_error(res, SUB_TAG, "f_close()");
return false;
}
f->open = false;
}
open_files.clear();
open_files.shrink_to_fit();
return true;
}
bool SDLogger::create_directory(const char* path, bool suppress_dir_exists_warning)
{
const constexpr char* SUB_TAG = "SD->create_directory()";
FRESULT res = FR_OK;
if (!usability_check(SUB_TAG))
return false;
res = f_mkdir(path);
if (res != FR_OK)
{
switch (res)
{
case FR_NO_PATH:
// attempt to build the path if it doesn't exists
if (!build_path(path))
{
ESP_LOGE(TAG, "%s: Path does not exist and could not be built.", SUB_TAG);
return false;
}
break;
case FR_EXIST:
if (!suppress_dir_exists_warning)
ESP_LOGW(TAG, "%s: Directory already exists.", SUB_TAG);
return true;
break;
default:
print_fatfs_error(res, SUB_TAG, "f_mkdir()");
return false;
break;
}
}
return true;
}
bool SDLogger::delete_file(SDFile file)
{
const constexpr char* SUB_TAG = "SD->delete_file()";
FRESULT res = FR_OK;
if (!usability_check(SUB_TAG))
return false;
if (!file || !file->initialized)
{
ESP_LOGE(TAG, "%s: File not correctly initialized.", SUB_TAG);
return false;
}
if (file->open)
{
ESP_LOGE(TAG, "%s: Close file before deleting.", SUB_TAG);
return false;
}
if (!path_exists(file->path, SUB_TAG))
return false;
res = f_unlink(file->path);
if (res != FR_OK)
{
print_fatfs_error(res, SUB_TAG, "f_unlink()");
return false;
}
return true;
}
bool SDLogger::delete_directory(const char* path)
{
// todo:
// 1) check if directory is empty
// 2) if it is proceed to 4, else 3
// 3) delete any files or sub_directories within the passed directory path while checking for errors (ie a file is open)
// 4) delete the passed directory
// this is the reverse of the the build_path function
return true;
}
bool SDLogger::file_exists(SDFile file)
{
const constexpr char* SUB_TAG = "SD->file_exists()";
if (!file || !file->initialized)
{
ESP_LOGE(TAG, "%s: File not correctly initialized.", SUB_TAG);
return false;
}
if (!path_exists(file->path, SUB_TAG))
return false;
return true;
}
bool SDLogger::path_exists(const char* path)
{
const constexpr char* SUB_TAG = "SD->path_exists()";
if (!path_exists(path, SUB_TAG))
return false;
return true;
}
bool SDLogger::build_path(const char* path)
{
const constexpr char* SUB_TAG = "SD->build_path()";
const char* start = path;
const char* end;
size_t length;
char* part = nullptr;
char path_str[100] = "";
while ((end = strchr(start, '/')) != nullptr)
{
length = end - start;
if (length > 0)
{
part = static_cast<char*>(malloc(length));
if (part == nullptr)
{
ESP_LOGE(TAG, "%s: No heap memory available to parse path.", SUB_TAG);
return false;
}
part[length] = '\0';
strncpy(part, start, length);
strcat(path_str, "/");
strcat(path_str, part);
free(part);
if (!path_exists(path_str, SUB_TAG, true))
if (!create_directory(path_str, true))
return false;
}
start = end + 1;
}
if (start != nullptr)
{
part = static_cast<char*>(malloc(strlen(start)));
if (part == nullptr)
{
ESP_LOGE(TAG, "%s: No heap memory available to parse path.", SUB_TAG);
return false;
}
strcpy(part, start);
strcat(path_str, "/");
strcat(path_str, part);
free(part);
if (!path_exists(path_str, SUB_TAG, true))
if (!create_directory(path_str, true))
return false;
}
return true;
}
bool SDLogger::write(SDFile file, const char* data)
{
const constexpr char* SUB_TAG = "SD->write()";
FRESULT res = FR_OK;
UINT bytes_written;
if (!usability_check(SUB_TAG))
return false;
if (!file || !file->initialized)
{
ESP_LOGE(TAG, "%s: File not correctly initialized.", SUB_TAG);
return false;
}
if (!file->open)
{
ESP_LOGE(TAG, "%s: File not open.", SUB_TAG);
return false;
}
// write here
res = f_write(&file->stream, data, strlen(data), &bytes_written);
if (res != FR_OK)
{
print_fatfs_error(res, SUB_TAG, "f_write()");
}
return true;
}
bool SDLogger::write_line(SDFile file, const char* line)
{
const constexpr char* SUB_TAG = "SD->write_line()";
size_t line_length;
size_t temp_buffer_sz;
char* temp_buffer;
FRESULT res;
UINT bytes_written;
if (!usability_check(SUB_TAG))
return false;
if (!file || !file->initialized)
{
ESP_LOGE(TAG, "%s: File not correctly initialized.", SUB_TAG);
return false;
}
if (!file->open)
{
ESP_LOGE(TAG, "%s: File not open.", SUB_TAG);
return false;
}
line_length = strlen(line);
temp_buffer_sz = line_length + 2;
temp_buffer = new char[temp_buffer_sz];
if (temp_buffer == nullptr)
{
ESP_LOGE(TAG, "%s: No heap memory available for line buffer.", SUB_TAG);
return false;
}
strncpy(temp_buffer, line, line_length);
temp_buffer[line_length] = '\n';
temp_buffer[line_length + 1] = '\0';
res = f_write(&file->stream, temp_buffer, strlen(temp_buffer), &bytes_written);
delete[] temp_buffer;
if (res != FR_OK)
{
print_fatfs_error(res, SUB_TAG, "f_write()");
return false;
}
return true;
}
bool SDLogger::parse_info(const char* info_buffer)
{
char temp_buff[50];
if (!parse_info_field(info_buffer, "Name", info.name, sizeof(info.name)))
return false;
if (!parse_info_field(info_buffer, "Type", info.type, sizeof(info.type)))
return false;
if (!parse_info_field(info_buffer, "Speed", temp_buff, sizeof(temp_buff)))
return false;
info.speed_mhz = strtof(temp_buff, nullptr);
if (!parse_info_field(info_buffer, "Size", temp_buff, sizeof(temp_buff)))
return false;
info.size_mb = strtoul(temp_buff, nullptr, 10);
if (!parse_info_field(info_buffer, "bus_width", temp_buff, sizeof(temp_buff)))
return false;
info.ssr_bus_width = static_cast<uint8_t>(strtoul(temp_buff, nullptr, 10));
if (!parse_info_field(info_buffer, "ver", temp_buff, sizeof(temp_buff)))
return false;
info.csd.ver = static_cast<uint8_t>(strtoul(temp_buff, nullptr, 10));
if (!parse_info_field(info_buffer, "sector_size", temp_buff, sizeof(temp_buff)))
return false;
info.csd.sector_sz = static_cast<uint16_t>(strtoul(temp_buff, nullptr, 10));
if (!parse_info_field(info_buffer, "capacity", temp_buff, sizeof(temp_buff)))
return false;
info.csd.capacity = strtoull(temp_buff, nullptr, 10);
if (!parse_info_field(info_buffer, "read_bl_len", temp_buff, sizeof(temp_buff)))
return false;
info.csd.read_bl_len = static_cast<uint8_t>(strtoul(temp_buff, nullptr, 10));
info.initialized = true;
return true;
}
bool SDLogger::parse_info_field(const char* info_buffer, const char* key, char* output, size_t output_sz)
{
const char* key_start = strstr(info_buffer, key);
if (!key_start)
return false;
key_start += strlen(key) + 1; // skip key string and colon
const char* key_end = strchr(key_start, '\n');
if (key_end)
{
size_t length = key_end - key_start;
if (length >= output_sz)
length = output_sz - 1;
strncpy(output, key_start, length);
output[length] = '\0';
}
else
{
strncpy(output, key_start, output_sz - 1);
output[output_sz - 1] = '\0';
}
return true;
}
bool SDLogger::load_info()
{
const constexpr char* SUB_TAG = "SD->load_info()";
size_t buffer_sz = 1024;
char* buffer = static_cast<char*>(malloc(buffer_sz));
FILE* memstream;
if (buffer == nullptr)
{
ESP_LOGE(TAG, "%s: No heap memory available for info buffer.", SUB_TAG);
return false;
}
memstream = fmemopen(buffer, buffer_sz, "w");
if (memstream == nullptr)
{
ESP_LOGW(TAG, "%s: Failed to open memory stream.", SUB_TAG);
free(buffer);
return false;
}
sdmmc_card_print_info(memstream, &card);
fclose(memstream);
// parse info
if (!parse_info(buffer))
{
ESP_LOGW(TAG, "%s: Failed to parse info buffer.", SUB_TAG);
free(buffer);
return false;
}
free(buffer);
return true;
}
bool SDLogger::usability_check(const char* SUB_TAG)
{
if (!initialized)
{
ESP_LOGE(TAG, "%s: Card not initialized.", SUB_TAG);
return false;
}
if (!mounted)
{
ESP_LOGE(TAG, "%s: No card mounted.", SUB_TAG);
return false;
}
return true;
}
SDFile SDLogger::File::create(const char* path)
{
auto instance = SDFile(new File());
if (instance->init(path))
return instance;
else
return nullptr;
}
SDLogger::File::File()
: initialized(false)
, open(false)
, path(nullptr)
, directory_path(nullptr)
{
}
SDLogger::File::~File()
{
if (path)
delete[] path;
if (directory_path)
delete[] directory_path;
}
bool SDLogger::File::init(const char* path)
{
const constexpr char* SUB_TAG = "SDFile->init()";
// reset fields if re-initializing
initialized = false;
open = false;
initialized = path_parse(path, SUB_TAG);
return initialized;
}
bool SDLogger::File::path_forbidden_char_check(const char* path, const char* SUB_TAG)
{
std::array<char, 8> forbidden_chars = {'\\', ':', '*', '?', '"', '<', '>', '|'};
uint8_t period_count = 0;
for (size_t i = 0; i < strlen(path); i++)
{
if (path[i] == '.')
period_count++;
if (period_count > 1)
{