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

502 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.
2017-11-24 17:48:47 +01: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_conf.h"
#include "lv_ufs.h"
#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
**********************/
2017-11-24 17:48:47 +01: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;
/**********************
* 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*/
2017-11-23 20:42:14 +01: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;
2017-11-23 20:42:14 +01: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;
2017-11-23 20:42:14 +01:00
2017-11-24 17:48:47 +01:00
ufs_drv.rddir_init = lv_ufs_readdir_init;
ufs_drv.rddir = lv_ufs_readdir;
ufs_drv.rddir_close = lv_ufs_readdir_close;
2017-11-23 20:42:14 +01:00
2017-11-24 17:48:47 +01:00
lv_fs_add_drv(&ufs_drv);
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)
* @return 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
*/
2017-11-24 17:48:47 +01: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*/
lv_ufs_ent_t* ent = lv_ufs_ent_get(fn);
2017-11-23 20:42:14 +01:00
fp->ent = NULL;
/*If the file not exists ...*/
if( ent == NULL) {
if((mode & FS_MODE_WR) != 0) { /*Create the file if opened for write*/
2017-11-24 17:48:47 +01:00
ent = lv_ufs_ent_new(fn);
2017-11-23 20:42:14 +01:00
if(ent == NULL) return FS_RES_FULL; /*No space for the new file*/
} else {
return FS_RES_NOT_EX; /*Can not read not existing file*/
}
}
/*Can not write already opened and const data files*/
if((mode & FS_MODE_WR) != 0) {
if(ent->oc != 0) return FS_RES_LOCKED;
if(ent->const_data != 0) return FS_RES_DENIED;
}
/*No error, the file can be opened*/
fp->ent = ent;
fp->ar = mode & FS_MODE_RD ? 1 : 0;
fp->aw = mode & FS_MODE_WR ? 1 : 0;
fp->rwp = 0;
ent->oc ++;
return FS_RES_OK;
}
/**
* 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
* @return 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;
2017-11-23 20:42:14 +01:00
/*Error if the file already exists*/
2017-11-24 17:48:47 +01:00
res = lv_ufs_open(&file, fn, FS_MODE_RD);
2017-11-23 20:42:14 +01:00
if(res == FS_RES_OK) {
2017-11-24 17:48:47 +01:00
lv_ufs_close(&file);
2017-11-23 20:42:14 +01:00
return FS_RES_DENIED;
}
2017-11-24 17:48:47 +01:00
lv_ufs_close(&file);
2017-11-23 20:42:14 +01:00
2017-11-24 17:48:47 +01:00
res = lv_ufs_open(&file, fn, FS_MODE_WR);
2017-11-23 20:42:14 +01:00
if(res != FS_RES_OK) return res;
2017-11-24 17:48:47 +01:00
lv_ufs_ent_t* ent = file.ent;
2017-11-23 20:42:14 +01:00
if(ent->data_d != NULL) return FS_RES_DENIED;
ent->data_d = (void *) const_p;
ent->size = len;
ent->const_data = 1;
2017-11-24 17:48:47 +01:00
res = lv_ufs_close(&file);
2017-11-23 20:42:14 +01:00
if(res != FS_RES_OK) return res;
return FS_RES_OK;
}
/**
* Close 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
* @return 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_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*/
2017-11-23 20:42:14 +01:00
if(fp->ent == NULL) return FS_RES_OK;
/*Decrement the Open counter*/
if(fp->ent->oc > 0) {
fp->ent->oc--;
}
return FS_RES_OK;
}
/**
* Remove a file. The file can not be opened.
* @param fn '\0' terminated string
* @return FS_RES_OK: no error, the file is removed
* FS_RES_DENIED: the file was opened, remove failed
*/
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
{
2017-11-24 17:48:47 +01:00
lv_ufs_ent_t* ent = lv_ufs_ent_get(fn);
2017-11-23 20:42:14 +01:00
/*Can not be deleted is opened*/
if(ent->oc != 0) return FS_RES_DENIED;
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;
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;
}
2017-11-23 21:28:36 +01:00
lv_mem_free(ent);
2017-11-23 20:42:14 +01:00
return FS_RES_OK;
}
/**
* 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
* @param btr number of Bytes To Read
* @param br the real number of read bytes (Byte Read)
* @return 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_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*/
2017-11-23 20:42:14 +01:00
2017-11-24 17:48:47 +01:00
lv_ufs_ent_t* ent = fp->ent;
2017-11-23 20:42:14 +01:00
*br = 0;
if(ent->data_d == NULL || ent->size == 0) { /*Don't read empty files*/
return FS_RES_OK;
} else if(fp->ar == 0) { /*The file is not opened for read*/
return FS_RES_DENIED;
}
/*No error, read the file*/
if(fp->rwp + btr > ent->size) { /*Check too much bytes read*/
*br = ent->size - fp->rwp;
} else {
*br = btr;
}
/*Read the data*/
uint8_t * data8_p;
if(ent->const_data == 0) {
data8_p = (uint8_t*) ent->data_d;
} else {
data8_p = ent->data_d;
}
data8_p += fp->rwp;
memcpy(buf, data8_p, *br);
fp->rwp += *br; /*Refresh the read write pointer*/
return FS_RES_OK;
}
/**
* 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)
* @return 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_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;
if(fp->aw == 0) return FS_RES_DENIED; /*Not opened for write*/
2017-11-24 17:48:47 +01: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) {
2017-11-23 21:28:36 +01:00
uint8_t* new_data = lv_mem_realloc(ent->data_d, new_size);
2017-11-23 20:42:14 +01:00
if(new_data == NULL) return FS_RES_FULL; /*Cannot allocate the new memory*/
ent->data_d = new_data;
ent->size = new_size;
}
/*Write the file*/
uint8_t * data8_p = (uint8_t*) ent->data_d;
data8_p += fp->rwp;
memcpy(data8_p, buf, btw);
*bw = btw;
fp->rwp += *bw;
return FS_RES_OK;
}
/**
* 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
* @return 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_seek (void * file_p, uint32_t pos)
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*/
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*/
if(fp->aw == 0) return FS_RES_DENIED; /*Not opened for write*/
2017-11-23 21:28:36 +01:00
uint8_t* new_data = lv_mem_realloc(ent->data_d, pos);
2017-11-23 20:42:14 +01:00
if(new_data == NULL) return FS_RES_FULL; /*Out of memory*/
ent->data_d = new_data;
ent->size = pos;
fp->rwp = pos;
}
return FS_RES_OK;
}
/**
* 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
* @return 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_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*/
2017-11-23 20:42:14 +01:00
*pos_p = fp->rwp;
return FS_RES_OK;
}
/**
* 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-11-23 20:42:14 +01:00
* @return 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_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*/
lv_ufs_ent_t* ent = fp->ent;
2017-11-23 20:42:14 +01:00
if(fp->aw == 0) return FS_RES_DENIED; /*Not opened for write*/
2017-11-23 21:28:36 +01:00
void * new_data = lv_mem_realloc(ent->data_d, fp->rwp);
2017-11-23 20:42:14 +01:00
if(new_data == NULL) return FS_RES_FULL; /*Out of memory*/
ent->data_d = new_data;
ent->size = fp->rwp;
return FS_RES_OK;
}
/**
* 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
* @return 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_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*/
lv_ufs_ent_t* ent = fp->ent;
2017-11-23 20:42:14 +01:00
*size_p = ent->size;
return FS_RES_OK;
}
/**
2017-11-24 17:48:47 +01:00
* Initialize a lv_ufs_read_dir_t variable to directory reading
2017-11-23 20:42:14 +01:00
* @param rddir_p pointer to a 'ufs_read_dir_t' variable
* @param path uFS doesn't support folders so it has to be ""
2017-11-24 17:48:47 +01:00
* @return FS_RES_OK or 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_readdir_init(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;
2017-11-23 20:42:14 +01:00
2017-11-24 17:48:47 +01:00
lv_ufs_rddir_p->last_ent = NULL;
2017-11-23 20:42:14 +01:00
if(path[0] != '\0') return FS_RES_NOT_EX;
else return FS_RES_OK;
}
/**
* Read the next file name
2017-11-24 17:48:47 +01:00
* @param dir_p pointer to an initialized 'ufs_read_dir_t' variable
2017-11-23 20:42:14 +01:00
* @param fn pointer to buffer to sore the file name
2017-11-24 17:48:47 +01:00
* @return FS_RES_OK or 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_readdir(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;
2017-11-23 20:42:14 +01: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
}
2017-11-24 17:48:47 +01:00
if(ufs_dir_p->last_ent != NULL) {
strcpy(fn, ufs_dir_p->last_ent->fn_d);
2017-11-23 20:42:14 +01:00
} else {
fn[0] = '\0';
}
return FS_RES_OK;
}
/**
* Close the directory reading
* @param rddir_p pointer to an initialized 'ufs_read_dir_t' variable
2017-11-24 17:48:47 +01:00
* @return FS_RES_OK or 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_readdir_close(void * rddir_p)
2017-11-23 20:42:14 +01:00
{
return FS_RES_OK;
}
/**
* 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-11-24 17:48:47 +01:00
* @return FS_RES_OK or any error from 'lv_fs_res_t'
2017-11-23 20:42:14 +01:00
*/
2017-11-24 17:48:47 +01: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-11-23 20:42:14 +01:00
return FS_RES_OK;
}
/**********************
* 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.
*/
2017-11-24 17:48:47 +01:00
static lv_ufs_ent_t* lv_ufs_ent_get(const char * fn)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01: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;
}
}
return NULL;
}
/**
* Create a new entry with 'fn' filename
* @param fn filename ('\0' terminated string)
* @return pointer to the dynamically allocated new entry.
* NULL if no space for the entry.
*/
2017-11-24 17:48:47 +01:00
static lv_ufs_ent_t* lv_ufs_ent_new(const char * fn)
2017-11-23 20:42:14 +01:00
{
2017-11-24 17:48:47 +01:00
lv_ufs_ent_t* new_ent = NULL;
new_ent = lv_ll_ins_head(&file_ll); /*Create a new file*/
2017-11-23 20:42:14 +01:00
if(new_ent == NULL) {
return NULL;
}
2017-11-23 21:28:36 +01:00
new_ent->fn_d = lv_mem_alloc(strlen(fn) + 1); /*Save the name*/
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;
return new_ent;
}