1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00
lvgl/lv_misc/lv_ufs.c

512 lines
13 KiB
C
Raw Normal View History

2017-11-23 20:42:14 +01:00
/**
2017-11-24 17:48:47 +01:00
* @file lv_ufs.c
2017-11-23 20:42:14 +01:00
* Implementation of RAM file system which do NOT support directories.
2018-06-19 09:49:58 +02:00
* The API is compatible with the lv_fs_int module.
2017-11-23 20:42:14 +01:00
*/
/*********************
* INCLUDES
*********************/
2017-11-26 11:38:28 +01:00
#include "lv_ufs.h"
#if USE_LV_FILESYSTEM
2017-11-26 11:38:28 +01:00
#include "lv_ll.h"
2017-11-23 20:42:14 +01:00
#include <string.h>
#include <stdio.h>
#include <errno.h>
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2018-06-19 09:49:58 +02:00
static lv_ufs_ent_t * lv_ufs_ent_get(const char * fn);
static lv_ufs_ent_t * lv_ufs_ent_new(const char * fn);
2017-11-23 20:42:14 +01:00
/**********************
* STATIC VARIABLES
**********************/
2017-11-24 17:48:47 +01:00
static lv_ll_t file_ll;
2017-11-23 20:42:14 +01:00
static bool inited = false;
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Create a driver for ufs and initialize it.
*/
2017-11-24 17:48:47 +01:00
void lv_ufs_init(void)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01:00
lv_ll_init(&file_ll, sizeof(lv_ufs_ent_t));
2017-11-23 20:42:14 +01:00
2017-11-24 17:48:47 +01:00
lv_fs_drv_t ufs_drv;
memset(&ufs_drv, 0, sizeof(lv_fs_drv_t)); /*Initialization*/
2018-06-19 09:49:58 +02:00
2017-11-24 17:48:47 +01:00
ufs_drv.file_size = sizeof(lv_ufs_file_t);
ufs_drv.rddir_size = sizeof(lv_ufs_dir_t);
2017-11-23 20:42:14 +01:00
ufs_drv.letter = UFS_LETTER;
2017-11-24 17:48:47 +01:00
ufs_drv.ready = lv_ufs_ready;
2018-06-19 09:49:58 +02:00
2017-11-24 17:48:47 +01:00
ufs_drv.open = lv_ufs_open;
ufs_drv.close = lv_ufs_close;
ufs_drv.remove = lv_ufs_remove;
ufs_drv.read = lv_ufs_read;
ufs_drv.write = lv_ufs_write;
ufs_drv.seek = lv_ufs_seek;
ufs_drv.tell = lv_ufs_tell;
ufs_drv.size = lv_ufs_size;
ufs_drv.trunc = lv_ufs_trunc;
ufs_drv.free = lv_ufs_free;
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
ufs_drv.dir_open = lv_ufs_dir_open;
ufs_drv.dir_read = lv_ufs_dir_read;
ufs_drv.dir_close = lv_ufs_dir_close;
2018-06-19 09:49:58 +02:00
2017-11-24 17:48:47 +01:00
lv_fs_add_drv(&ufs_drv);
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
inited = true;
}
/**
* Give the state of the ufs
* @return true if ufs is initialized and can be used else false
*/
2017-11-24 17:48:47 +01:00
bool lv_ufs_ready(void)
2017-11-23 20:42:14 +01:00
{
return inited;
}
/**
* Open a file in ufs
2017-11-24 17:48:47 +01:00
* @param file_p pointer to a lv_ufs_file_t variable
2017-11-23 20:42:14 +01:00
* @param fn name of the file. There are no directories so e.g. "myfile.txt"
* @param mode element of 'fs_mode_t' enum or its 'OR' connection (e.g. FS_MODE_WR | FS_MODE_RD)
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK: no error, the file is opened
2017-11-24 17:48:47 +01:00
* any error from lv__fs_res_t enum
2017-11-23 20:42:14 +01:00
*/
2018-06-19 09:49:58 +02:00
lv_fs_res_t lv_ufs_open(void * file_p, const char * fn, lv_fs_mode_t mode)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01:00
lv_ufs_file_t * fp = file_p; /*Convert type*/
2018-06-19 09:49:58 +02:00
lv_ufs_ent_t * ent = lv_ufs_ent_get(fn);
2017-11-23 20:42:14 +01:00
fp->ent = NULL;
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
/*If the file not exists ...*/
2018-06-19 09:49:58 +02:00
if(ent == NULL) {
2017-12-03 22:32:09 +01:00
if((mode & LV_FS_MODE_WR) != 0) { /*Create the file if opened for write*/
2018-06-19 09:49:58 +02:00
ent = lv_ufs_ent_new(fn);
2017-12-03 22:32:09 +01:00
if(ent == NULL) return LV_FS_RES_FULL; /*No space for the new file*/
2018-06-19 09:49:58 +02:00
} else {
2017-12-03 22:32:09 +01:00
return LV_FS_RES_NOT_EX; /*Can not read not existing file*/
2017-11-23 20:42:14 +01:00
}
2018-06-19 09:49:58 +02:00
}
2017-11-23 20:42:14 +01:00
/*Can not write already opened and const data files*/
2017-12-03 22:32:09 +01:00
if((mode & LV_FS_MODE_WR) != 0) {
if(ent->oc != 0) return LV_FS_RES_LOCKED;
if(ent->const_data != 0) return LV_FS_RES_DENIED;
2017-11-23 20:42:14 +01:00
}
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
/*No error, the file can be opened*/
fp->ent = ent;
2017-12-03 22:32:09 +01:00
fp->ar = mode & LV_FS_MODE_RD ? 1 : 0;
fp->aw = mode & LV_FS_MODE_WR ? 1 : 0;
2017-11-23 20:42:14 +01:00
fp->rwp = 0;
ent->oc ++;
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
* Create a file with a constant data
* @param fn name of the file (directories are not supported)
* @param const_p pointer to a constant data
* @param len length of the data pointed by 'const_p' in bytes
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK: no error, the file is read
2017-11-24 17:48:47 +01:00
* any error from lv__fs_res_t enum
2017-11-23 20:42:14 +01:00
*/
2017-11-24 17:48:47 +01:00
lv_fs_res_t lv_ufs_create_const(const char * fn, const void * const_p, uint32_t len)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01:00
lv_ufs_file_t file;
lv_fs_res_t res;
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
/*Error if the file already exists*/
2017-12-03 22:32:09 +01:00
res = lv_ufs_open(&file, fn, LV_FS_MODE_RD);
if(res == LV_FS_RES_OK) {
2017-11-24 17:48:47 +01:00
lv_ufs_close(&file);
2017-12-03 22:32:09 +01:00
return LV_FS_RES_DENIED;
2017-11-23 20:42:14 +01:00
}
2018-06-19 09:49:58 +02:00
2017-11-24 17:48:47 +01:00
lv_ufs_close(&file);
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
res = lv_ufs_open(&file, fn, LV_FS_MODE_WR);
if(res != LV_FS_RES_OK) return res;
2018-06-19 09:49:58 +02:00
lv_ufs_ent_t * ent = file.ent;
2017-12-03 22:32:09 +01:00
if(ent->data_d != NULL) return LV_FS_RES_DENIED;
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
ent->data_d = (void *) const_p;
ent->size = len;
ent->const_data = 1;
2018-06-19 09:49:58 +02:00
2017-11-24 17:48:47 +01:00
res = lv_ufs_close(&file);
2017-12-03 22:32:09 +01:00
if(res != LV_FS_RES_OK) return res;
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
2018-06-19 09:49:58 +02:00
* Close an opened file
* @param file_p pointer to an 'ufs_file_t' variable. (opened with lv_ufs_open)
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK: no error, the file is read
2017-11-24 17:48:47 +01:00
* any error from lv__fs_res_t enum
2017-11-23 20:42:14 +01:00
*/
2018-06-19 09:49:58 +02:00
lv_fs_res_t lv_ufs_close(void * file_p)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01:00
lv_ufs_file_t * fp = file_p; /*Convert type*/
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
if(fp->ent == NULL) return LV_FS_RES_OK;
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
/*Decrement the Open counter*/
if(fp->ent->oc > 0) {
fp->ent->oc--;
}
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
* Remove a file. The file can not be opened.
* @param fn '\0' terminated string
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK: no error, the file is removed
* LV_FS_RES_DENIED: the file was opened, remove failed
2017-11-23 20:42:14 +01:00
*/
2017-11-24 17:48:47 +01:00
lv_fs_res_t lv_ufs_remove(const char * fn)
2017-11-23 20:42:14 +01:00
{
2018-06-19 09:49:58 +02:00
lv_ufs_ent_t * ent = lv_ufs_ent_get(fn);
if(ent == NULL) return LV_FS_RES_DENIED; /*File not exists*/
2017-11-23 20:42:14 +01:00
/*Can not be deleted is opened*/
2017-12-03 22:32:09 +01:00
if(ent->oc != 0) return LV_FS_RES_DENIED;
2018-01-08 09:47:46 +01:00
2017-11-24 17:48:47 +01:00
lv_ll_rem(&file_ll, ent);
2017-11-23 21:28:36 +01:00
lv_mem_free(ent->fn_d);
2017-11-23 20:42:14 +01:00
ent->fn_d = NULL;
2018-06-19 09:49:58 +02:00
if(ent->const_data == 0) {
2017-11-23 21:28:36 +01:00
lv_mem_free(ent->data_d);
2017-11-23 20:42:14 +01:00
ent->data_d = NULL;
}
2018-06-19 09:49:58 +02:00
2017-11-23 21:28:36 +01:00
lv_mem_free(ent);
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
* Read data from an opened file
2017-11-24 17:48:47 +01:00
* @param file_p pointer to an 'ufs_file_t' variable. (opened with lv_ufs_open )
2017-11-23 20:42:14 +01:00
* @param buf pointer to a memory block where to store the read data
2018-06-19 09:49:58 +02:00
* @param btr number of Bytes To Read
2017-11-23 20:42:14 +01:00
* @param br the real number of read bytes (Byte Read)
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK: no error, the file is read
2017-11-24 17:48:47 +01:00
* any error from lv__fs_res_t enum
2017-11-23 20:42:14 +01:00
*/
2018-06-19 09:49:58 +02:00
lv_fs_res_t lv_ufs_read(void * file_p, void * buf, uint32_t btr, uint32_t * br)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01:00
lv_ufs_file_t * fp = file_p; /*Convert type*/
2018-06-19 09:49:58 +02:00
lv_ufs_ent_t * ent = fp->ent;
2017-11-23 20:42:14 +01:00
*br = 0;
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
if(ent->data_d == NULL || ent->size == 0) { /*Don't read empty files*/
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
} else if(fp->ar == 0) { /*The file is not opened for read*/
2018-06-19 09:49:58 +02:00
return LV_FS_RES_DENIED;
}
2017-11-23 20:42:14 +01:00
/*No error, read the file*/
if(fp->rwp + btr > ent->size) { /*Check too much bytes read*/
2018-06-19 09:49:58 +02:00
*br = ent->size - fp->rwp;
2017-11-23 20:42:14 +01:00
} else {
2018-06-19 09:49:58 +02:00
*br = btr;
2017-11-23 20:42:14 +01:00
}
2018-06-19 09:49:58 +02:00
/*Read the data*/
2017-11-23 20:42:14 +01:00
uint8_t * data8_p;
if(ent->const_data == 0) {
2018-06-19 09:49:58 +02:00
data8_p = (uint8_t *) ent->data_d;
2017-11-23 20:42:14 +01:00
} else {
data8_p = ent->data_d;
}
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
data8_p += fp->rwp;
memcpy(buf, data8_p, *br);
fp->rwp += *br; /*Refresh the read write pointer*/
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
* Write data to an opened file
2017-11-24 17:48:47 +01:00
* @param file_p pointer to an 'ufs_file_t' variable. (opened with lv_ufs_open)
2017-11-23 20:42:14 +01:00
* @param buf pointer to a memory block which content will be written
* @param btw the number Bytes To Write
* @param bw The real number of written bytes (Byte Written)
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK: no error, the file is read
2017-11-24 17:48:47 +01:00
* any error from lv__fs_res_t enum
2017-11-23 20:42:14 +01:00
*/
2018-06-19 09:49:58 +02:00
lv_fs_res_t lv_ufs_write(void * file_p, const void * buf, uint32_t btw, uint32_t * bw)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01:00
lv_ufs_file_t * fp = file_p; /*Convert type*/
2017-11-23 20:42:14 +01:00
*bw = 0;
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
if(fp->aw == 0) return LV_FS_RES_DENIED; /*Not opened for write*/
2018-06-19 09:49:58 +02:00
lv_ufs_ent_t * ent = fp->ent;
2017-11-23 20:42:14 +01:00
/*Reallocate data array if it necessary*/
uint32_t new_size = fp->rwp + btw;
if(new_size > ent->size) {
2018-06-19 09:49:58 +02:00
uint8_t * new_data = lv_mem_realloc(ent->data_d, new_size);
lv_mem_assert(new_data);
2017-12-03 22:32:09 +01:00
if(new_data == NULL) return LV_FS_RES_FULL; /*Cannot allocate the new memory*/
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
ent->data_d = new_data;
ent->size = new_size;
}
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
/*Write the file*/
2018-06-19 09:49:58 +02:00
uint8_t * data8_p = (uint8_t *) ent->data_d;
2017-11-23 20:42:14 +01:00
data8_p += fp->rwp;
memcpy(data8_p, buf, btw);
*bw = btw;
fp->rwp += *bw;
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
* Set the read write pointer. Also expand the file size if necessary.
2017-11-24 17:48:47 +01:00
* @param file_p pointer to an 'ufs_file_t' variable. (opened with lv_ufs_open )
2017-11-23 20:42:14 +01:00
* @param pos the new position of read write pointer
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK: no error, the file is read
2017-11-24 17:48:47 +01:00
* any error from lv__fs_res_t enum
2017-11-23 20:42:14 +01:00
*/
2018-06-19 09:49:58 +02:00
lv_fs_res_t lv_ufs_seek(void * file_p, uint32_t pos)
{
2017-11-24 17:48:47 +01:00
lv_ufs_file_t * fp = file_p; /*Convert type*/
2018-06-19 09:49:58 +02:00
lv_ufs_ent_t * ent = fp->ent;
2017-11-23 20:42:14 +01:00
/*Simply move the rwp before EOF*/
if(pos < ent->size) {
fp->rwp = pos;
} else { /*Expand the file size*/
2017-12-03 22:32:09 +01:00
if(fp->aw == 0) return LV_FS_RES_DENIED; /*Not opened for write*/
2018-06-19 09:49:58 +02:00
uint8_t * new_data = lv_mem_realloc(ent->data_d, pos);
lv_mem_assert(new_data);
2017-12-03 22:32:09 +01:00
if(new_data == NULL) return LV_FS_RES_FULL; /*Out of memory*/
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
ent->data_d = new_data;
ent->size = pos;
2018-06-19 09:49:58 +02:00
fp->rwp = pos;
2017-11-23 20:42:14 +01:00
}
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
* Give the position of the read write pointer
2017-11-24 17:48:47 +01:00
* @param file_p pointer to an 'ufs_file_t' variable. (opened with lv_ufs_open )
2017-11-23 20:42:14 +01:00
* @param pos_p pointer to to store the result
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK: no error, the file is read
2017-11-24 17:48:47 +01:00
* any error from lv__fs_res_t enum
2017-11-23 20:42:14 +01:00
*/
2018-06-19 09:49:58 +02:00
lv_fs_res_t lv_ufs_tell(void * file_p, uint32_t * pos_p)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01:00
lv_ufs_file_t * fp = file_p; /*Convert type*/
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
*pos_p = fp->rwp;
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
* Truncate the file size to the current position of the read write pointer
2017-11-24 17:48:47 +01:00
* @param file_p pointer to an 'ufs_file_t' variable. (opened with lv_ufs_open )
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK: no error, the file is read
2017-11-24 17:48:47 +01:00
* any error from lv__fs_res_t enum
2017-11-23 20:42:14 +01:00
*/
2018-06-19 09:49:58 +02:00
lv_fs_res_t lv_ufs_trunc(void * file_p)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01:00
lv_ufs_file_t * fp = file_p; /*Convert type*/
2018-06-19 09:49:58 +02:00
lv_ufs_ent_t * ent = fp->ent;
2017-12-03 22:32:09 +01:00
if(fp->aw == 0) return LV_FS_RES_DENIED; /*Not opened for write*/
2018-06-19 09:49:58 +02:00
2017-11-23 21:28:36 +01:00
void * new_data = lv_mem_realloc(ent->data_d, fp->rwp);
lv_mem_assert(new_data);
2017-12-03 22:32:09 +01:00
if(new_data == NULL) return LV_FS_RES_FULL; /*Out of memory*/
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
ent->data_d = new_data;
ent->size = fp->rwp;
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
* Give the size of the file in bytes
2017-11-24 17:48:47 +01:00
* @param file_p file_p pointer to an 'ufs_file_t' variable. (opened with lv_ufs_open )
2017-11-23 20:42:14 +01:00
* @param size_p pointer to store the size
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK: no error, the file is read
2017-11-24 17:48:47 +01:00
* any error from lv__fs_res_t enum
2017-11-23 20:42:14 +01:00
*/
2018-06-19 09:49:58 +02:00
lv_fs_res_t lv_ufs_size(void * file_p, uint32_t * size_p)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01:00
lv_ufs_file_t * fp = file_p; /*Convert type*/
2018-06-19 09:49:58 +02:00
lv_ufs_ent_t * ent = fp->ent;
2017-11-23 20:42:14 +01:00
*size_p = ent->size;
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
2017-11-24 17:48:47 +01:00
* Initialize a lv_ufs_read_dir_t variable to directory reading
2017-12-03 22:32:09 +01:00
* @param rddir_p pointer to a 'ufs_dir_t' variable
2017-11-23 20:42:14 +01:00
* @param path uFS doesn't support folders so it has to be ""
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK or any error from lv__fs_res_t enum
2017-11-23 20:42:14 +01:00
*/
2017-12-03 22:32:09 +01:00
lv_fs_res_t lv_ufs_dir_open(void * rddir_p, const char * path)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01:00
lv_ufs_dir_t * lv_ufs_rddir_p = rddir_p;
2018-06-19 09:49:58 +02:00
2017-11-24 17:48:47 +01:00
lv_ufs_rddir_p->last_ent = NULL;
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
if(path[0] != '\0') return LV_FS_RES_NOT_EX; /*Must be "" */
else return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
* Read the next file name
2017-12-03 22:32:09 +01:00
* @param dir_p pointer to an initialized 'ufs_dir_t' variable
2017-11-23 20:42:14 +01:00
* @param fn pointer to buffer to sore the file name
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK or any error from lv__fs_res_t enum
2017-11-23 20:42:14 +01:00
*/
2017-12-03 22:32:09 +01:00
lv_fs_res_t lv_ufs_dir_read(void * dir_p, char * fn)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01:00
lv_ufs_dir_t * ufs_dir_p = dir_p;
2018-06-19 09:49:58 +02:00
2017-11-24 17:48:47 +01:00
if(ufs_dir_p->last_ent == NULL) {
ufs_dir_p->last_ent = lv_ll_get_head(&file_ll);
2017-11-23 20:42:14 +01:00
} else {
2017-11-24 17:48:47 +01:00
ufs_dir_p->last_ent = lv_ll_get_next(&file_ll, ufs_dir_p->last_ent);
2017-11-23 20:42:14 +01:00
}
2018-06-19 09:49:58 +02:00
2017-11-24 17:48:47 +01:00
if(ufs_dir_p->last_ent != NULL) {
2018-06-19 09:49:58 +02:00
strcpy(fn, ufs_dir_p->last_ent->fn_d);
2017-11-23 20:42:14 +01:00
} else {
fn[0] = '\0';
}
2018-06-19 09:49:58 +02:00
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
* Close the directory reading
2017-12-03 22:32:09 +01:00
* @param rddir_p pointer to an initialized 'ufs_dir_t' variable
* @return LV_FS_RES_OK or any error from lv__fs_res_t enum
2017-11-23 20:42:14 +01:00
*/
2017-12-03 22:32:09 +01:00
lv_fs_res_t lv_ufs_dir_close(void * rddir_p)
2017-11-23 20:42:14 +01:00
{
2017-12-02 20:43:50 +01:00
(void)rddir_p;
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**
* Give the size of a drive
* @param total_p pointer to store the total size [kB]
* @param free_p pointer to store the free site [kB]
2017-12-03 22:32:09 +01:00
* @return LV_FS_RES_OK or any error from 'lv_fs_res_t'
2017-11-23 20:42:14 +01:00
*/
2018-06-19 09:49:58 +02:00
lv_fs_res_t lv_ufs_free(uint32_t * total_p, uint32_t * free_p)
2017-11-23 20:42:14 +01:00
{
2017-11-26 11:38:28 +01:00
#if LV_MEM_CUSTOM == 0
lv_mem_monitor_t mon;
2017-11-23 20:42:14 +01:00
2017-11-26 11:38:28 +01:00
lv_mem_monitor(&mon);
*total_p = LV_MEM_SIZE >> 10; /*Convert bytes to kB*/
*free_p = mon.free_size >> 10;
#else
*free_p = 0;
#endif
2017-12-03 22:32:09 +01:00
return LV_FS_RES_OK;
2017-11-23 20:42:14 +01:00
}
/**********************
* STATIC FUNCTIONS
**********************/
/**
2017-11-24 17:48:47 +01:00
* Gives the lv_ufs_entry from a filename
2017-11-23 20:42:14 +01:00
* @param fn filename ('\0' terminated string)
* @return pointer to the dynamically allocated entry with 'fn' filename.
* NULL if no entry found with that name.
*/
2018-06-19 09:49:58 +02:00
static lv_ufs_ent_t * lv_ufs_ent_get(const char * fn)
2017-11-23 20:42:14 +01:00
{
2018-06-19 09:49:58 +02:00
lv_ufs_ent_t * fp;
2017-11-23 20:42:14 +01:00
LL_READ(file_ll, fp) {
if(strcmp(fp->fn_d, fn) == 0) {
return fp;
2018-06-19 09:49:58 +02:00
}
2017-11-23 20:42:14 +01:00
}
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
return NULL;
}
/**
2018-06-19 09:49:58 +02:00
* Create a new entry with 'fn' filename
2017-11-23 20:42:14 +01:00
* @param fn filename ('\0' terminated string)
* @return pointer to the dynamically allocated new entry.
* NULL if no space for the entry.
*/
2018-06-19 09:49:58 +02:00
static lv_ufs_ent_t * lv_ufs_ent_new(const char * fn)
2017-11-23 20:42:14 +01:00
{
2018-06-19 09:49:58 +02:00
lv_ufs_ent_t * new_ent = NULL;
2017-11-24 17:48:47 +01:00
new_ent = lv_ll_ins_head(&file_ll); /*Create a new file*/
lv_mem_assert(new_ent);
if(new_ent == NULL) return NULL;
2018-06-19 09:49:58 +02:00
2017-11-23 21:28:36 +01:00
new_ent->fn_d = lv_mem_alloc(strlen(fn) + 1); /*Save the name*/
lv_mem_assert(new_ent->fn_d);
if(new_ent->fn_d == NULL) return NULL;
2017-11-23 20:42:14 +01:00
strcpy(new_ent->fn_d, fn);
new_ent->data_d = NULL;
new_ent->size = 0;
new_ent->oc = 0;
new_ent->const_data = 0;
2018-06-19 09:49:58 +02:00
2017-11-23 20:42:14 +01:00
return new_ent;
}
#endif /*USE_LV_FILESYSTEM*/