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
|
/**
* @file lv_fs.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_fs_private.h"
#include "../misc/lv_assert.h"
#include "../misc/lv_profiler.h"
#include "../stdlib/lv_string.h"
#include "lv_ll.h"
#include "../core/lv_global.h"
/*********************
* DEFINES
*********************/
#define fsdrv_ll_p &(LV_GLOBAL_DEFAULT()->fsdrv_ll)
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static const char * lv_fs_get_real_path(const char * path);
static lv_fs_res_t lv_fs_read_cached(lv_fs_file_t * file_p, void * buf, uint32_t btr, uint32_t * br);
static lv_fs_res_t lv_fs_write_cached(lv_fs_file_t * file_p, const void * buf, uint32_t btw, uint32_t * bw);
static lv_fs_res_t lv_fs_seek_cached(lv_fs_file_t * file_p, uint32_t pos, lv_fs_whence_t whence);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
void lv_fs_init(void)
{
lv_ll_init(fsdrv_ll_p, sizeof(lv_fs_drv_t *));
}
void lv_fs_deinit(void)
{
lv_ll_clear(fsdrv_ll_p);
}
bool lv_fs_is_ready(char letter)
{
lv_fs_drv_t * drv = lv_fs_get_drv(letter);
if(drv == NULL) return false; /*An unknown driver in not ready*/
if(drv->ready_cb == NULL) return true; /*Assume the driver is always ready if no handler provided*/
return drv->ready_cb(drv);
}
lv_fs_res_t lv_fs_open(lv_fs_file_t * file_p, const char * path, lv_fs_mode_t mode)
{
if(path == NULL) {
LV_LOG_WARN("Can't open file: path is NULL");
return LV_FS_RES_INV_PARAM;
}
char letter = path[0];
lv_fs_drv_t * drv = lv_fs_get_drv(letter);
if(drv == NULL) {
LV_LOG_WARN("Can't open file (%s): unknown driver letter", path);
return LV_FS_RES_NOT_EX;
}
if(drv->ready_cb) {
if(drv->ready_cb(drv) == false) {
LV_LOG_WARN("Can't open file (%s): driver not ready", path);
return LV_FS_RES_HW_ERR;
}
}
if(drv->open_cb == NULL) {
LV_LOG_WARN("Can't open file (%s): open function not exists", path);
return LV_FS_RES_NOT_IMP;
}
LV_PROFILER_BEGIN;
file_p->drv = drv;
/* For memory-mapped files we set the file handle to our file descriptor so that we can access the cache from the file operations */
if(drv->cache_size == LV_FS_CACHE_FROM_BUFFER) {
file_p->file_d = file_p;
}
else {
const char * real_path = lv_fs_get_real_path(path);
void * file_d = drv->open_cb(drv, real_path, mode);
if(file_d == NULL || file_d == (void *)(-1)) {
LV_PROFILER_END;
return LV_FS_RES_UNKNOWN;
}
file_p->file_d = file_d;
}
if(drv->cache_size) {
file_p->cache = lv_malloc_zeroed(sizeof(lv_fs_file_cache_t));
LV_ASSERT_MALLOC(file_p->cache);
/* If this is a memory-mapped file, then set "cache" to the memory buffer */
if(drv->cache_size == LV_FS_CACHE_FROM_BUFFER) {
lv_fs_path_ex_t * path_ex = (lv_fs_path_ex_t *)path;
file_p->cache->buffer = (void *)path_ex->buffer;
file_p->cache->start = 0;
file_p->cache->file_position = 0;
file_p->cache->end = path_ex->size;
}
/*Set an invalid range by default*/
else {
file_p->cache->start = UINT32_MAX;
file_p->cache->end = UINT32_MAX - 1;
}
}
LV_PROFILER_END;
return LV_FS_RES_OK;
}
void lv_fs_make_path_from_buffer(lv_fs_path_ex_t * path, char letter, const void * buf, uint32_t size)
{
path->path[0] = letter;
path->path[1] = ':';
path->path[2] = 0;
path->buffer = buf;
path->size = size;
}
lv_fs_res_t lv_fs_close(lv_fs_file_t * file_p)
{
if(file_p->drv == NULL) {
return LV_FS_RES_INV_PARAM;
}
if(file_p->drv->close_cb == NULL) {
return LV_FS_RES_NOT_IMP;
}
LV_PROFILER_BEGIN;
lv_fs_res_t res = file_p->drv->close_cb(file_p->drv, file_p->file_d);
if(file_p->drv->cache_size && file_p->cache) {
/* Only free cache if it was pre-allocated (for memory-mapped files it is never allocated) */
if(file_p->drv->cache_size != LV_FS_CACHE_FROM_BUFFER && file_p->cache->buffer) {
lv_free(file_p->cache->buffer);
}
lv_free(file_p->cache);
}
file_p->file_d = NULL;
file_p->drv = NULL;
file_p->cache = NULL;
LV_PROFILER_END;
return res;
}
lv_fs_res_t lv_fs_read(lv_fs_file_t * file_p, void * buf, uint32_t btr, uint32_t * br)
{
if(br != NULL) *br = 0;
if(file_p->drv == NULL) return LV_FS_RES_INV_PARAM;
if(file_p->drv->cache_size) {
if(file_p->drv->read_cb == NULL || file_p->drv->seek_cb == NULL) return LV_FS_RES_NOT_IMP;
}
else {
if(file_p->drv->read_cb == NULL) return LV_FS_RES_NOT_IMP;
}
LV_PROFILER_BEGIN;
uint32_t br_tmp = 0;
lv_fs_res_t res;
if(file_p->drv->cache_size) {
res = lv_fs_read_cached(file_p, buf, btr, &br_tmp);
}
else {
res = file_p->drv->read_cb(file_p->drv, file_p->file_d, buf, btr, &br_tmp);
}
if(br != NULL) *br = br_tmp;
LV_PROFILER_END;
return res;
}
lv_fs_res_t lv_fs_write(lv_fs_file_t * file_p, const void * buf, uint32_t btw, uint32_t * bw)
{
if(bw != NULL) *bw = 0;
if(file_p->drv == NULL) {
return LV_FS_RES_INV_PARAM;
}
if(file_p->drv->cache_size) {
if(file_p->drv->write_cb == NULL || file_p->drv->seek_cb == NULL) return LV_FS_RES_NOT_IMP;
}
else {
if(file_p->drv->write_cb == NULL) return LV_FS_RES_NOT_IMP;
}
LV_PROFILER_BEGIN;
lv_fs_res_t res;
uint32_t bw_tmp = 0;
if(file_p->drv->cache_size) {
res = lv_fs_write_cached(file_p, buf, btw, &bw_tmp);
}
else {
res = file_p->drv->write_cb(file_p->drv, file_p->file_d, buf, btw, &bw_tmp);
}
if(bw != NULL) *bw = bw_tmp;
LV_PROFILER_END;
return res;
}
lv_fs_res_t lv_fs_seek(lv_fs_file_t * file_p, uint32_t pos, lv_fs_whence_t whence)
{
if(file_p->drv == NULL) {
return LV_FS_RES_INV_PARAM;
}
if(file_p->drv->cache_size) {
if(file_p->drv->seek_cb == NULL || file_p->drv->tell_cb == NULL) return LV_FS_RES_NOT_IMP;
}
else {
if(file_p->drv->seek_cb == NULL) return LV_FS_RES_NOT_IMP;
}
LV_PROFILER_BEGIN;
lv_fs_res_t res;
if(file_p->drv->cache_size) {
res = lv_fs_seek_cached(file_p, pos, whence);
}
else {
res = file_p->drv->seek_cb(file_p->drv, file_p->file_d, pos, whence);
}
LV_PROFILER_END;
return res;
}
lv_fs_res_t lv_fs_tell(lv_fs_file_t * file_p, uint32_t * pos)
{
if(file_p->drv == NULL) {
*pos = 0;
return LV_FS_RES_INV_PARAM;
}
if(file_p->drv->cache_size == 0 && file_p->drv->tell_cb == NULL) {
*pos = 0;
return LV_FS_RES_NOT_IMP;
}
LV_PROFILER_BEGIN;
lv_fs_res_t res;
if(file_p->drv->cache_size) {
*pos = file_p->cache->file_position;
res = LV_FS_RES_OK;
}
else {
res = file_p->drv->tell_cb(file_p->drv, file_p->file_d, pos);
}
LV_PROFILER_END;
return res;
}
lv_fs_res_t lv_fs_dir_open(lv_fs_dir_t * rddir_p, const char * path)
{
if(path == NULL) return LV_FS_RES_INV_PARAM;
char letter = path[0];
lv_fs_drv_t * drv = lv_fs_get_drv(letter);
if(drv == NULL) {
return LV_FS_RES_NOT_EX;
}
if(drv->ready_cb) {
if(drv->ready_cb(drv) == false) {
return LV_FS_RES_HW_ERR;
}
}
if(drv->dir_open_cb == NULL) {
return LV_FS_RES_NOT_IMP;
}
LV_PROFILER_BEGIN;
const char * real_path = lv_fs_get_real_path(path);
void * dir_d = drv->dir_open_cb(drv, real_path);
if(dir_d == NULL || dir_d == (void *)(-1)) {
LV_PROFILER_END;
return LV_FS_RES_UNKNOWN;
}
rddir_p->drv = drv;
rddir_p->dir_d = dir_d;
LV_PROFILER_END;
return LV_FS_RES_OK;
}
lv_fs_res_t lv_fs_dir_read(lv_fs_dir_t * rddir_p, char * fn, uint32_t fn_len)
{
if(fn_len == 0) {
return LV_FS_RES_INV_PARAM;
}
if(rddir_p->drv == NULL || rddir_p->dir_d == NULL) {
fn[0] = '\0';
return LV_FS_RES_INV_PARAM;
}
if(rddir_p->drv->dir_read_cb == NULL) {
fn[0] = '\0';
return LV_FS_RES_NOT_IMP;
}
LV_PROFILER_BEGIN;
lv_fs_res_t res = rddir_p->drv->dir_read_cb(rddir_p->drv, rddir_p->dir_d, fn, fn_len);
LV_PROFILER_END;
return res;
}
lv_fs_res_t lv_fs_dir_close(lv_fs_dir_t * rddir_p)
{
if(rddir_p->drv == NULL || rddir_p->dir_d == NULL) {
return LV_FS_RES_INV_PARAM;
}
if(rddir_p->drv->dir_close_cb == NULL) {
return LV_FS_RES_NOT_IMP;
}
LV_PROFILER_BEGIN;
lv_fs_res_t res = rddir_p->drv->dir_close_cb(rddir_p->drv, rddir_p->dir_d);
rddir_p->dir_d = NULL;
rddir_p->drv = NULL;
LV_PROFILER_END;
return res;
}
void lv_fs_drv_init(lv_fs_drv_t * drv)
{
lv_memzero(drv, sizeof(lv_fs_drv_t));
}
void lv_fs_drv_register(lv_fs_drv_t * drv_p)
{
/*Save the new driver*/
lv_fs_drv_t ** new_drv;
new_drv = lv_ll_ins_head(fsdrv_ll_p);
LV_ASSERT_MALLOC(new_drv);
if(new_drv == NULL) return;
*new_drv = drv_p;
}
lv_fs_drv_t * lv_fs_get_drv(char letter)
{
lv_fs_drv_t ** drv;
LV_LL_READ(fsdrv_ll_p, drv) {
if((*drv)->letter == letter) {
return *drv;
}
}
return NULL;
}
char * lv_fs_get_letters(char * buf)
{
lv_fs_drv_t ** drv;
uint8_t i = 0;
LV_LL_READ(fsdrv_ll_p, drv) {
buf[i] = (*drv)->letter;
i++;
}
buf[i] = '\0';
return buf;
}
const char * lv_fs_get_ext(const char * fn)
{
size_t i;
for(i = lv_strlen(fn); i > 0; i--) {
if(fn[i] == '.') {
return &fn[i + 1];
}
else if(fn[i] == '/' || fn[i] == '\\') {
return ""; /*No extension if a '\' or '/' found*/
}
}
return ""; /*Empty string if no '.' in the file name.*/
}
char * lv_fs_up(char * path)
{
size_t len = lv_strlen(path);
if(len == 0) return path;
len--; /*Go before the trailing '\0'*/
/*Ignore trailing '/' or '\'*/
while(path[len] == '/' || path[len] == '\\') {
path[len] = '\0';
if(len > 0)
len--;
else
return path;
}
size_t i;
for(i = len; i > 0; i--) {
if(path[i] == '/' || path[i] == '\\') break;
}
if(i > 0) path[i] = '\0';
return path;
}
const char * lv_fs_get_last(const char * path)
{
size_t len = lv_strlen(path);
if(len == 0) return path;
len--; /*Go before the trailing '\0'*/
/*Ignore trailing '/' or '\'*/
while(path[len] == '/' || path[len] == '\\') {
if(len > 0)
len--;
else
return path;
}
size_t i;
for(i = len; i > 0; i--) {
if(path[i] == '/' || path[i] == '\\') break;
}
/*No '/' or '\' in the path so return with path itself*/
if(i == 0) return path;
return &path[i + 1];
}
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Skip the driver letter and the possible : after the letter
* @param path path string (E.g. S:/folder/file.txt)
* @return pointer to the beginning of the real path (E.g. /folder/file.txt)
*/
static const char * lv_fs_get_real_path(const char * path)
{
path++; /*Ignore the driver letter*/
if(*path == ':') path++;
return path;
}
static lv_fs_res_t lv_fs_read_cached(lv_fs_file_t * file_p, void * buf, uint32_t btr, uint32_t * br)
{
lv_fs_res_t res = LV_FS_RES_OK;
uint32_t file_position = file_p->cache->file_position;
uint32_t start = file_p->cache->start;
uint32_t end = file_p->cache->end;
char * buffer = file_p->cache->buffer;
uint32_t buffer_size = file_p->drv->cache_size;
if(start <= file_position && file_position <= end) {
/* Data can be read from cache buffer */
uint32_t buffer_remaining_length = (uint32_t)end - file_position + 1;
uint32_t buffer_offset = (end - start) - buffer_remaining_length + 1;
/* Do not allow reading beyond the actual memory block for memory-mapped files */
if(file_p->drv->cache_size == LV_FS_CACHE_FROM_BUFFER) {
if(btr > buffer_remaining_length)
btr = buffer_remaining_length - 1;
}
if(btr <= buffer_remaining_length) {
/*Data is in cache buffer, and buffer end not reached, no need to read from FS*/
lv_memcpy(buf, buffer + buffer_offset, btr);
*br = btr;
}
else {
/*First part of data is in cache buffer, but we need to read rest of data from FS*/
lv_memcpy(buf, buffer + buffer_offset, buffer_remaining_length);
file_p->drv->seek_cb(file_p->drv, file_p->file_d, file_p->cache->end + 1,
LV_FS_SEEK_SET);
uint32_t bytes_read_to_buffer = 0;
if(btr - buffer_remaining_length > buffer_size) {
/*If remaining data chuck is bigger than buffer size, then do not use cache, instead read it directly from FS*/
res = file_p->drv->read_cb(file_p->drv, file_p->file_d, (char *)buf + buffer_remaining_length,
btr - buffer_remaining_length, &bytes_read_to_buffer);
}
else {
/*If remaining data chunk is smaller than buffer size, then read into cache buffer*/
res = file_p->drv->read_cb(file_p->drv, file_p->file_d, buffer, buffer_size, &bytes_read_to_buffer);
file_p->cache->start = file_p->cache->end + 1;
file_p->cache->end = file_p->cache->start + bytes_read_to_buffer - 1;
uint16_t data_chunk_remaining = LV_MIN(btr - buffer_remaining_length, bytes_read_to_buffer);
lv_memcpy((char *)buf + buffer_remaining_length, buffer, data_chunk_remaining);
}
*br = LV_MIN(buffer_remaining_length + bytes_read_to_buffer, btr);
}
}
else {
file_p->drv->seek_cb(file_p->drv, file_p->file_d, file_p->cache->file_position,
LV_FS_SEEK_SET);
/*Data is not in cache buffer*/
if(btr > buffer_size) {
/*If bigger data is requested, then do not use cache, instead read it directly*/
res = file_p->drv->read_cb(file_p->drv, file_p->file_d, (void *)buf, btr, br);
}
else {
/*If small data is requested, then read from FS into cache buffer*/
if(buffer == NULL) {
file_p->cache->buffer = lv_malloc(buffer_size);
LV_ASSERT_MALLOC(file_p->cache->buffer);
buffer = file_p->cache->buffer;
}
uint32_t bytes_read_to_buffer = 0;
res = file_p->drv->read_cb(file_p->drv, file_p->file_d, (void *)buffer, buffer_size, &bytes_read_to_buffer);
file_p->cache->start = file_position;
file_p->cache->end = file_p->cache->start + bytes_read_to_buffer - 1;
*br = LV_MIN(btr, bytes_read_to_buffer);
lv_memcpy(buf, buffer, *br);
}
}
if(res == LV_FS_RES_OK) {
file_p->cache->file_position += *br;
}
return res;
}
static lv_fs_res_t lv_fs_write_cached(lv_fs_file_t * file_p, const void * buf, uint32_t btw, uint32_t * bw)
{
lv_fs_res_t res = LV_FS_RES_OK;
/*Need to do FS seek before writing data to FS*/
res = file_p->drv->seek_cb(file_p->drv, file_p->file_d, file_p->cache->file_position, LV_FS_SEEK_SET);
if(res != LV_FS_RES_OK) return res;
res = file_p->drv->write_cb(file_p->drv, file_p->file_d, buf, btw, bw);
if(res != LV_FS_RES_OK) return res;
if(file_p->cache->end >= file_p->cache->start) {
uint32_t start_position = file_p->cache->file_position;
uint32_t end_position = file_p->cache->file_position + *bw - 1;
char * cache_buffer = file_p->cache->buffer;
const char * write_buffer = buf;
if(start_position <= file_p->cache->start && end_position >= file_p->cache->end) {
lv_memcpy(cache_buffer,
write_buffer + (file_p->cache->start - start_position),
file_p->cache->end + 1 - file_p->cache->start);
}
else if(start_position >= file_p->cache->start && end_position <= file_p->cache->end) {
lv_memcpy(cache_buffer + (start_position - file_p->cache->start),
write_buffer,
end_position + 1 - start_position);
}
else if(end_position >= file_p->cache->start && end_position <= file_p->cache->end) {
lv_memcpy(cache_buffer,
write_buffer + (file_p->cache->start - start_position),
end_position + 1 - file_p->cache->start);
}
else if(start_position >= file_p->cache->start && start_position <= file_p->cache->end) {
lv_memcpy(cache_buffer + (start_position - file_p->cache->start),
write_buffer,
file_p->cache->end + 1 - start_position);
}
}
file_p->cache->file_position += *bw;
return res;
}
static lv_fs_res_t lv_fs_seek_cached(lv_fs_file_t * file_p, uint32_t pos, lv_fs_whence_t whence)
{
lv_fs_res_t res = LV_FS_RES_OK;
switch(whence) {
case LV_FS_SEEK_SET: {
file_p->cache->file_position = pos;
break;
}
case LV_FS_SEEK_CUR: {
file_p->cache->file_position += pos;
break;
}
case LV_FS_SEEK_END: {
/*Because we don't know the file size, we do a little trick: do a FS seek, then get the new file position from FS*/
res = file_p->drv->seek_cb(file_p->drv, file_p->file_d, pos, whence);
if(res == LV_FS_RES_OK) {
uint32_t tmp_position;
res = file_p->drv->tell_cb(file_p->drv, file_p->file_d, &tmp_position);
if(res == LV_FS_RES_OK) {
file_p->cache->file_position = tmp_position;
}
}
break;
}
}
return res;
}
|