[FL-520] Filesystem Api and App (#280)
* update fatfs integer types * fix sector size to 512 * fix sector size calculation * common fs api * fs api realization (sd card + fat fs) * better sector size definition * more api realization fns * add error description api, add common api * fix api flag naming, run app * add fs_info call * disable fatfs strfuncs, enable fatfs chmod * rework filesystem app * sd detect cycle, sd menu, sd eject feature * fix sd detect cycle * sd card format routine * ui improvements, sd info routine * properly unmount card * separate mode flags * add api folder, move app, rename app * fix api naming * update st-card-test to use api * update path to app * fixed potential problem of using sizeof union * updated api documentation, new time/date fns * update codeowners * changed app requirements * changed app order * sd insert/remove log
This commit is contained in:
738
applications/sd-filesystem/sd-filesystem-api.c
Normal file
738
applications/sd-filesystem/sd-filesystem-api.c
Normal file
@@ -0,0 +1,738 @@
|
||||
#include "fatfs.h"
|
||||
#include "filesystem-api.h"
|
||||
#include "sd-filesystem.h"
|
||||
|
||||
/******************* Global vars for api *******************/
|
||||
|
||||
static SdFsInfo* fs_info;
|
||||
|
||||
/******************* Core Functions *******************/
|
||||
|
||||
bool _fs_init(SdFsInfo* _fs_info) {
|
||||
bool result = true;
|
||||
_fs_info->mutex = osMutexNew(NULL);
|
||||
if(_fs_info->mutex == NULL) result = false;
|
||||
|
||||
for(uint8_t i = 0; i < SD_FS_MAX_FILES; i++) {
|
||||
_fs_info->files[i].thread_id = NULL;
|
||||
}
|
||||
|
||||
_fs_info->path = "0:/";
|
||||
_fs_info->status = SD_NO_CARD;
|
||||
|
||||
// store pointer for api fns
|
||||
fs_info = _fs_info;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool _fs_lock(SdFsInfo* fs_info) {
|
||||
return (osMutexAcquire(fs_info->mutex, osWaitForever) == osOK);
|
||||
}
|
||||
|
||||
bool _fs_unlock(SdFsInfo* fs_info) {
|
||||
return (osMutexRelease(fs_info->mutex) == osOK);
|
||||
}
|
||||
|
||||
SDError _get_filedata(SdFsInfo* fs_info, File* file, FileData** filedata, FiledataFilter filter) {
|
||||
SDError error = SD_OK;
|
||||
_fs_lock(fs_info);
|
||||
|
||||
if(fs_info->status == SD_OK) {
|
||||
if(file != NULL && file->file_id < SD_FS_MAX_FILES) {
|
||||
if(fs_info->files[file->file_id].thread_id == osThreadGetId()) {
|
||||
if(filter == FDF_ANY) {
|
||||
// any type
|
||||
*filedata = &fs_info->files[file->file_id];
|
||||
} else if(filter == FDF_FILE) {
|
||||
// file type
|
||||
if(!fs_info->files[file->file_id].is_dir) {
|
||||
*filedata = &fs_info->files[file->file_id];
|
||||
} else {
|
||||
error = SD_NOT_A_FILE;
|
||||
}
|
||||
} else if(filter == FDF_DIR) {
|
||||
// dir type
|
||||
if(fs_info->files[file->file_id].is_dir) {
|
||||
*filedata = &fs_info->files[file->file_id];
|
||||
} else {
|
||||
error = SD_NOT_A_DIR;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error = SD_OTHER_APP;
|
||||
}
|
||||
} else {
|
||||
error = SD_INVALID_PARAMETER;
|
||||
}
|
||||
} else {
|
||||
error = SD_NO_CARD;
|
||||
}
|
||||
|
||||
_fs_unlock(fs_info);
|
||||
return error;
|
||||
}
|
||||
|
||||
SDError _get_file(SdFsInfo* fs_info, File* file, FileData** filedata) {
|
||||
return _get_filedata(fs_info, file, filedata, FDF_FILE);
|
||||
}
|
||||
|
||||
SDError _get_dir(SdFsInfo* fs_info, File* file, FileData** filedata) {
|
||||
return _get_filedata(fs_info, file, filedata, FDF_DIR);
|
||||
}
|
||||
|
||||
SDError _get_any(SdFsInfo* fs_info, File* file, FileData** filedata) {
|
||||
return _get_filedata(fs_info, file, filedata, FDF_ANY);
|
||||
}
|
||||
|
||||
SDError _fs_status(SdFsInfo* fs_info) {
|
||||
SDError result;
|
||||
|
||||
_fs_lock(fs_info);
|
||||
result = fs_info->status;
|
||||
_fs_unlock(fs_info);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void _fs_on_client_app_exit(SdFsInfo* fs_info) {
|
||||
_fs_lock(fs_info);
|
||||
for(uint8_t i = 0; i < SD_FS_MAX_FILES; i++) {
|
||||
if(fs_info->files[i].thread_id == osThreadGetId()) {
|
||||
if(fs_info->files[i].is_dir) {
|
||||
// TODO close dir
|
||||
} else {
|
||||
// TODO close file
|
||||
}
|
||||
}
|
||||
}
|
||||
_fs_unlock(fs_info);
|
||||
}
|
||||
|
||||
FS_Error _fs_parse_error(SDError error) {
|
||||
FS_Error result;
|
||||
switch(error) {
|
||||
case SD_OK:
|
||||
result = FSE_OK;
|
||||
break;
|
||||
case SD_INT_ERR:
|
||||
result = FSE_INTERNAL;
|
||||
break;
|
||||
case SD_NO_FILE:
|
||||
result = FSE_NOT_EXIST;
|
||||
break;
|
||||
case SD_NO_PATH:
|
||||
result = FSE_NOT_EXIST;
|
||||
break;
|
||||
case SD_INVALID_NAME:
|
||||
result = FSE_INVALID_NAME;
|
||||
break;
|
||||
case SD_DENIED:
|
||||
result = FSE_DENIED;
|
||||
break;
|
||||
case SD_EXIST:
|
||||
result = FSE_EXIST;
|
||||
break;
|
||||
case SD_INVALID_OBJECT:
|
||||
result = FSE_INTERNAL;
|
||||
break;
|
||||
case SD_WRITE_PROTECTED:
|
||||
result = FSE_INTERNAL;
|
||||
break;
|
||||
case SD_INVALID_DRIVE:
|
||||
result = FSE_INTERNAL;
|
||||
break;
|
||||
case SD_NOT_ENABLED:
|
||||
result = FSE_INTERNAL;
|
||||
break;
|
||||
case SD_NO_FILESYSTEM:
|
||||
result = FSE_NOT_READY;
|
||||
break;
|
||||
case SD_MKFS_ABORTED:
|
||||
result = FSE_INTERNAL;
|
||||
break;
|
||||
case SD_TIMEOUT:
|
||||
result = FSE_INTERNAL;
|
||||
break;
|
||||
case SD_LOCKED:
|
||||
result = FSE_INTERNAL;
|
||||
break;
|
||||
case SD_NOT_ENOUGH_CORE:
|
||||
result = FSE_INTERNAL;
|
||||
break;
|
||||
case SD_TOO_MANY_OPEN_FILES:
|
||||
result = FSE_INTERNAL;
|
||||
break;
|
||||
case SD_INVALID_PARAMETER:
|
||||
result = FSE_INVALID_PARAMETER;
|
||||
break;
|
||||
case SD_NO_CARD:
|
||||
result = FSE_NOT_READY;
|
||||
break;
|
||||
case SD_NOT_A_FILE:
|
||||
result = FSE_INVALID_PARAMETER;
|
||||
break;
|
||||
case SD_NOT_A_DIR:
|
||||
result = FSE_INVALID_PARAMETER;
|
||||
break;
|
||||
case SD_OTHER_APP:
|
||||
result = FSE_INTERNAL;
|
||||
break;
|
||||
|
||||
default:
|
||||
result = FSE_INTERNAL;
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/******************* File Functions *******************/
|
||||
|
||||
// Open/Create a file
|
||||
bool fs_file_open(File* file, const char* path, FS_AccessMode access_mode, FS_OpenMode open_mode) {
|
||||
SDFile* sd_file = NULL;
|
||||
|
||||
_fs_lock(fs_info);
|
||||
for(uint8_t index = 0; index < SD_FS_MAX_FILES; index++) {
|
||||
FileData* filedata = &fs_info->files[index];
|
||||
|
||||
if(filedata->thread_id == NULL) {
|
||||
file->file_id = index;
|
||||
|
||||
memset(&(filedata->data), 0, sizeof(SDFileDirStorage));
|
||||
filedata->thread_id = osThreadGetId();
|
||||
filedata->is_dir = false;
|
||||
sd_file = &(filedata->data.file);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
_fs_unlock(fs_info);
|
||||
|
||||
if(sd_file == NULL) {
|
||||
file->internal_error_id = SD_TOO_MANY_OPEN_FILES;
|
||||
} else {
|
||||
uint8_t _mode = 0;
|
||||
|
||||
if(access_mode & FSAM_READ) _mode |= FA_READ;
|
||||
if(access_mode & FSAM_WRITE) _mode |= FA_WRITE;
|
||||
if(open_mode & FSOM_OPEN_EXISTING) _mode |= FA_OPEN_EXISTING;
|
||||
if(open_mode & FSOM_OPEN_ALWAYS) _mode |= FA_OPEN_ALWAYS;
|
||||
if(open_mode & FSOM_OPEN_APPEND) _mode |= FA_OPEN_APPEND;
|
||||
if(open_mode & FSOM_CREATE_NEW) _mode |= FA_CREATE_NEW;
|
||||
if(open_mode & FSOM_CREATE_ALWAYS) _mode |= FA_CREATE_ALWAYS;
|
||||
|
||||
file->internal_error_id = f_open(sd_file, path, _mode);
|
||||
}
|
||||
|
||||
// TODO on exit
|
||||
//furiac_onexit(_fs_on_client_app_exit, fs_info);
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return (file->internal_error_id == SD_OK);
|
||||
}
|
||||
|
||||
// Close an opened file
|
||||
bool fs_file_close(File* file) {
|
||||
FileData* filedata = NULL;
|
||||
file->internal_error_id = _get_file(fs_info, file, &filedata);
|
||||
|
||||
if(file->internal_error_id == SD_OK) {
|
||||
file->internal_error_id = f_close(&filedata->data.file);
|
||||
|
||||
_fs_lock(fs_info);
|
||||
filedata->thread_id = NULL;
|
||||
_fs_unlock(fs_info);
|
||||
}
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return (file->internal_error_id == SD_OK);
|
||||
}
|
||||
|
||||
// Read data from the file
|
||||
uint16_t fs_file_read(File* file, void* buff, uint16_t const bytes_to_read) {
|
||||
FileData* filedata = NULL;
|
||||
uint16_t bytes_readed = 0;
|
||||
|
||||
file->internal_error_id = _get_file(fs_info, file, &filedata);
|
||||
|
||||
if(file->internal_error_id == SD_OK) {
|
||||
file->internal_error_id = f_read(&filedata->data.file, buff, bytes_to_read, &bytes_readed);
|
||||
}
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return bytes_readed;
|
||||
}
|
||||
|
||||
// Write data to the file
|
||||
uint16_t fs_file_write(File* file, void* buff, uint16_t const bytes_to_write) {
|
||||
FileData* filedata = NULL;
|
||||
uint16_t bytes_written = 0;
|
||||
|
||||
file->internal_error_id = _get_file(fs_info, file, &filedata);
|
||||
|
||||
if(file->internal_error_id == SD_OK) {
|
||||
file->internal_error_id =
|
||||
f_write(&filedata->data.file, buff, bytes_to_write, &bytes_written);
|
||||
}
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return bytes_written;
|
||||
}
|
||||
|
||||
// Move read/write pointer, expand size
|
||||
bool fs_file_seek(File* file, const uint32_t offset, const bool from_start) {
|
||||
FileData* filedata = NULL;
|
||||
|
||||
file->internal_error_id = _get_file(fs_info, file, &filedata);
|
||||
|
||||
if(file->internal_error_id == SD_OK) {
|
||||
if(from_start) {
|
||||
file->internal_error_id = f_lseek(&filedata->data.file, offset);
|
||||
} else {
|
||||
uint64_t position = f_tell(&filedata->data.file);
|
||||
position += offset;
|
||||
file->internal_error_id = f_lseek(&filedata->data.file, position);
|
||||
}
|
||||
}
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return (file->internal_error_id == SD_OK);
|
||||
}
|
||||
|
||||
// Tell pointer position
|
||||
uint64_t fs_file_tell(File* file) {
|
||||
FileData* filedata = NULL;
|
||||
uint64_t position = 0;
|
||||
file->internal_error_id = _get_file(fs_info, file, &filedata);
|
||||
|
||||
if(file->internal_error_id == SD_OK) {
|
||||
position = f_tell(&filedata->data.file);
|
||||
}
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return position;
|
||||
}
|
||||
|
||||
// Truncate file size to current pointer value
|
||||
bool fs_file_truncate(File* file) {
|
||||
FileData* filedata = NULL;
|
||||
uint64_t position = 0;
|
||||
|
||||
file->internal_error_id = _get_file(fs_info, file, &filedata);
|
||||
|
||||
if(file->internal_error_id == SD_OK) {
|
||||
file->internal_error_id = f_truncate(&filedata->data.file);
|
||||
}
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return (file->internal_error_id == SD_OK);
|
||||
}
|
||||
|
||||
// Flush cached data
|
||||
bool fs_file_sync(File* file) {
|
||||
FileData* filedata = NULL;
|
||||
|
||||
file->internal_error_id = _get_file(fs_info, file, &filedata);
|
||||
|
||||
if(file->internal_error_id == SD_OK) {
|
||||
file->internal_error_id = f_sync(&filedata->data.file);
|
||||
}
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return (file->internal_error_id == SD_OK);
|
||||
}
|
||||
|
||||
// Get size
|
||||
uint64_t fs_file_size(File* file) {
|
||||
FileData* filedata = NULL;
|
||||
uint64_t size = 0;
|
||||
file->internal_error_id = _get_file(fs_info, file, &filedata);
|
||||
|
||||
if(file->internal_error_id == SD_OK) {
|
||||
size = f_size(&filedata->data.file);
|
||||
}
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return size;
|
||||
}
|
||||
|
||||
// Test EOF
|
||||
bool fs_file_eof(File* file) {
|
||||
FileData* filedata = NULL;
|
||||
bool eof = true;
|
||||
file->internal_error_id = _get_file(fs_info, file, &filedata);
|
||||
|
||||
if(file->internal_error_id == SD_OK) {
|
||||
eof = f_eof(&filedata->data.file);
|
||||
}
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return eof;
|
||||
}
|
||||
|
||||
/******************* Dir Functions *******************/
|
||||
|
||||
// Open directory
|
||||
bool fs_dir_open(File* file, const char* path) {
|
||||
SDDir* sd_dir = NULL;
|
||||
|
||||
_fs_lock(fs_info);
|
||||
for(uint8_t index = 0; index < SD_FS_MAX_FILES; index++) {
|
||||
FileData* filedata = &fs_info->files[index];
|
||||
|
||||
if(filedata->thread_id == NULL) {
|
||||
file->file_id = index;
|
||||
|
||||
memset(&(filedata->data), 0, sizeof(SDFileDirStorage));
|
||||
filedata->thread_id = osThreadGetId();
|
||||
filedata->is_dir = true;
|
||||
sd_dir = &(filedata->data.dir);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
_fs_unlock(fs_info);
|
||||
|
||||
if(sd_dir == NULL) {
|
||||
file->internal_error_id = SD_TOO_MANY_OPEN_FILES;
|
||||
} else {
|
||||
if(file->internal_error_id == SD_OK) file->internal_error_id = f_opendir(sd_dir, path);
|
||||
}
|
||||
|
||||
// TODO on exit
|
||||
//furiac_onexit(_fs_on_client_app_exit, fs_info);
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return (file->internal_error_id == SD_OK);
|
||||
}
|
||||
|
||||
// Close directory
|
||||
bool fs_dir_close(File* file) {
|
||||
FileData* filedata = NULL;
|
||||
file->internal_error_id = _get_dir(fs_info, file, &filedata);
|
||||
|
||||
if(file->internal_error_id == SD_OK) {
|
||||
file->internal_error_id = f_closedir(&filedata->data.dir);
|
||||
|
||||
_fs_lock(fs_info);
|
||||
filedata->thread_id = NULL;
|
||||
_fs_unlock(fs_info);
|
||||
}
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return (file->internal_error_id == SD_OK);
|
||||
}
|
||||
|
||||
// Read next file info and name from directory
|
||||
bool fs_dir_read(File* file, FileInfo* fileinfo, char* name, const uint16_t name_length) {
|
||||
FileData* filedata = NULL;
|
||||
file->internal_error_id = _get_dir(fs_info, file, &filedata);
|
||||
|
||||
if(file->internal_error_id == SD_OK) {
|
||||
SDFileInfo _fileinfo;
|
||||
file->internal_error_id = f_readdir(&filedata->data.dir, &_fileinfo);
|
||||
|
||||
if(fileinfo != NULL) {
|
||||
fileinfo->date.value = _fileinfo.fdate;
|
||||
fileinfo->time.value = _fileinfo.ftime;
|
||||
fileinfo->size = _fileinfo.fsize;
|
||||
fileinfo->flags = 0;
|
||||
|
||||
if(_fileinfo.fattrib & AM_RDO) fileinfo->flags |= FSF_READ_ONLY;
|
||||
if(_fileinfo.fattrib & AM_HID) fileinfo->flags |= FSF_HIDDEN;
|
||||
if(_fileinfo.fattrib & AM_SYS) fileinfo->flags |= FSF_SYSTEM;
|
||||
if(_fileinfo.fattrib & AM_DIR) fileinfo->flags |= FSF_DIRECTORY;
|
||||
if(_fileinfo.fattrib & AM_ARC) fileinfo->flags |= FSF_ARCHIVE;
|
||||
}
|
||||
|
||||
if(name != NULL && name_length > 0) {
|
||||
strncpy(name, _fileinfo.fname, name_length);
|
||||
}
|
||||
}
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return (file->internal_error_id == SD_OK);
|
||||
}
|
||||
|
||||
bool fs_dir_rewind(File* file) {
|
||||
FileData* filedata = NULL;
|
||||
file->internal_error_id = _get_dir(fs_info, file, &filedata);
|
||||
|
||||
if(file->internal_error_id == SD_OK) {
|
||||
file->internal_error_id = f_readdir(&filedata->data.dir, NULL);
|
||||
}
|
||||
|
||||
file->error_id = _fs_parse_error(file->internal_error_id);
|
||||
return (file->internal_error_id == SD_OK);
|
||||
}
|
||||
|
||||
/******************* Common FS Functions *******************/
|
||||
|
||||
// Get info about file/dir
|
||||
FS_Error
|
||||
fs_common_info(const char* path, FileInfo* fileinfo, char* name, const uint16_t name_length) {
|
||||
SDFileInfo _fileinfo;
|
||||
SDError fresult = _fs_status(fs_info);
|
||||
|
||||
if(fresult == SD_OK) {
|
||||
fresult = f_stat(path, &_fileinfo);
|
||||
if(fresult == FR_OK) {
|
||||
if(fileinfo != NULL) {
|
||||
fileinfo->date.value = _fileinfo.fdate;
|
||||
fileinfo->time.value = _fileinfo.ftime;
|
||||
fileinfo->size = _fileinfo.fsize;
|
||||
fileinfo->flags = 0;
|
||||
|
||||
if(_fileinfo.fattrib & AM_RDO) fileinfo->flags |= FSF_READ_ONLY;
|
||||
if(_fileinfo.fattrib & AM_HID) fileinfo->flags |= FSF_HIDDEN;
|
||||
if(_fileinfo.fattrib & AM_SYS) fileinfo->flags |= FSF_SYSTEM;
|
||||
if(_fileinfo.fattrib & AM_DIR) fileinfo->flags |= FSF_DIRECTORY;
|
||||
if(_fileinfo.fattrib & AM_ARC) fileinfo->flags |= FSF_ARCHIVE;
|
||||
}
|
||||
|
||||
if(name != NULL && name_length > 0) {
|
||||
strncpy(name, _fileinfo.fname, name_length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return _fs_parse_error(fresult);
|
||||
}
|
||||
|
||||
// Delete file/dir
|
||||
// File/dir must not have read-only attribute.
|
||||
// File/dir must be empty.
|
||||
// File/dir must not be opened, or the FAT volume can be collapsed. FF_FS_LOCK fix that.
|
||||
FS_Error fs_common_remove(const char* path) {
|
||||
SDError fresult = _fs_status(fs_info);
|
||||
|
||||
if(fresult == SD_OK) {
|
||||
fresult = f_unlink(path);
|
||||
}
|
||||
|
||||
return _fs_parse_error(fresult);
|
||||
}
|
||||
|
||||
// Rename file/dir
|
||||
// File/dir must not be opened, or the FAT volume can be collapsed. FF_FS_LOCK fix that.
|
||||
FS_Error fs_common_rename(const char* old_path, const char* new_path) {
|
||||
SDError fresult = _fs_status(fs_info);
|
||||
|
||||
if(fresult == SD_OK) {
|
||||
fresult = f_rename(old_path, new_path);
|
||||
}
|
||||
|
||||
return _fs_parse_error(fresult);
|
||||
}
|
||||
|
||||
// Set attributes of file/dir
|
||||
// For example:
|
||||
// set "read only" flag and remove "hidden" flag
|
||||
// fs_common_set_attr("file.txt", FSF_READ_ONLY, FSF_READ_ONLY | FSF_HIDDEN);
|
||||
FS_Error fs_common_set_attr(const char* path, uint8_t attr, uint8_t mask) {
|
||||
SDError fresult = _fs_status(fs_info);
|
||||
|
||||
if(fresult == SD_OK) {
|
||||
uint8_t _mask = 0;
|
||||
uint8_t _attr = 0;
|
||||
|
||||
if(mask & FSF_READ_ONLY) _mask |= AM_RDO;
|
||||
if(mask & FSF_HIDDEN) _mask |= AM_HID;
|
||||
if(mask & FSF_SYSTEM) _mask |= AM_SYS;
|
||||
if(mask & FSF_DIRECTORY) _mask |= AM_DIR;
|
||||
if(mask & FSF_ARCHIVE) _mask |= AM_ARC;
|
||||
|
||||
if(attr & FSF_READ_ONLY) _attr |= AM_RDO;
|
||||
if(attr & FSF_HIDDEN) _attr |= AM_HID;
|
||||
if(attr & FSF_SYSTEM) _attr |= AM_SYS;
|
||||
if(attr & FSF_DIRECTORY) _attr |= AM_DIR;
|
||||
if(attr & FSF_ARCHIVE) _attr |= AM_ARC;
|
||||
|
||||
fresult = f_chmod(path, attr, mask);
|
||||
}
|
||||
|
||||
return _fs_parse_error(fresult);
|
||||
}
|
||||
|
||||
// Set time of file/dir
|
||||
FS_Error fs_common_set_time(const char* path, FileDateUnion date, FileTimeUnion time) {
|
||||
SDError fresult = _fs_status(fs_info);
|
||||
|
||||
if(fresult == SD_OK) {
|
||||
SDFileInfo _fileinfo;
|
||||
|
||||
_fileinfo.fdate = date.value;
|
||||
_fileinfo.ftime = time.value;
|
||||
|
||||
fresult = f_utime(path, &_fileinfo);
|
||||
}
|
||||
|
||||
return _fs_parse_error(fresult);
|
||||
}
|
||||
|
||||
// Create new directory
|
||||
FS_Error fs_common_mkdir(const char* path) {
|
||||
SDError fresult = _fs_status(fs_info);
|
||||
|
||||
if(fresult == SD_OK) {
|
||||
fresult = f_mkdir(path);
|
||||
}
|
||||
|
||||
return _fs_parse_error(fresult);
|
||||
}
|
||||
|
||||
// Get common info about FS
|
||||
FS_Error fs_get_fs_info(uint64_t* total_space, uint64_t* free_space) {
|
||||
SDError fresult = _fs_status(fs_info);
|
||||
|
||||
if(fresult == SD_OK) {
|
||||
DWORD free_clusters;
|
||||
FATFS* fs;
|
||||
|
||||
fresult = f_getfree("0:/", &free_clusters, &fs);
|
||||
if(fresult == FR_OK) {
|
||||
uint32_t total_sectors = (fs->n_fatent - 2) * fs->csize;
|
||||
uint32_t free_sectors = free_clusters * fs->csize;
|
||||
|
||||
uint16_t sector_size = _MAX_SS;
|
||||
#if _MAX_SS != _MIN_SS
|
||||
sector_size = fs->ssize;
|
||||
#endif
|
||||
|
||||
if(total_space != NULL) {
|
||||
*total_space = (uint64_t)total_sectors * (uint64_t)sector_size;
|
||||
}
|
||||
|
||||
if(free_space != NULL) {
|
||||
*free_space = (uint64_t)free_sectors * (uint64_t)sector_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return _fs_parse_error(fresult);
|
||||
}
|
||||
|
||||
/******************* Error Reporting Functions *******************/
|
||||
|
||||
// Get common error description
|
||||
const char* fs_error_get_desc(FS_Error error_id) {
|
||||
const char* result;
|
||||
switch(error_id) {
|
||||
case(FSE_OK):
|
||||
result = "OK";
|
||||
break;
|
||||
case(FSE_NOT_READY):
|
||||
result = "filesystem not ready";
|
||||
break;
|
||||
case(FSE_EXIST):
|
||||
result = "file/dir already exist";
|
||||
break;
|
||||
case(FSE_NOT_EXIST):
|
||||
result = "file/dir not exist";
|
||||
break;
|
||||
case(FSE_INVALID_PARAMETER):
|
||||
result = "invalid parameter";
|
||||
break;
|
||||
case(FSE_DENIED):
|
||||
result = "access denied";
|
||||
break;
|
||||
case(FSE_INVALID_NAME):
|
||||
result = "invalid name/path";
|
||||
break;
|
||||
case(FSE_INTERNAL):
|
||||
result = "internal error";
|
||||
break;
|
||||
case(FSE_NOT_IMPLEMENTED):
|
||||
result = "function not implemented";
|
||||
break;
|
||||
default:
|
||||
result = "unknown error";
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Get internal error description
|
||||
const char* fs_error_get_internal_desc(uint32_t internal_error_id) {
|
||||
const char* result;
|
||||
switch(internal_error_id) {
|
||||
case(SD_OK):
|
||||
result = "OK";
|
||||
break;
|
||||
case(SD_DISK_ERR):
|
||||
result = "disk error";
|
||||
break;
|
||||
case(SD_INT_ERR):
|
||||
result = "internal error";
|
||||
break;
|
||||
case(SD_NO_FILE):
|
||||
result = "no file";
|
||||
break;
|
||||
case(SD_NO_PATH):
|
||||
result = "no path";
|
||||
break;
|
||||
case(SD_INVALID_NAME):
|
||||
result = "invalid name";
|
||||
break;
|
||||
case(SD_DENIED):
|
||||
result = "access denied";
|
||||
break;
|
||||
case(SD_EXIST):
|
||||
result = "file/dir exist";
|
||||
break;
|
||||
case(SD_INVALID_OBJECT):
|
||||
result = "invalid object";
|
||||
break;
|
||||
case(SD_WRITE_PROTECTED):
|
||||
result = "write protected";
|
||||
break;
|
||||
case(SD_INVALID_DRIVE):
|
||||
result = "invalid drive";
|
||||
break;
|
||||
case(SD_NOT_ENABLED):
|
||||
result = "not enabled";
|
||||
break;
|
||||
case(SD_NO_FILESYSTEM):
|
||||
result = "no filesystem";
|
||||
break;
|
||||
case(SD_MKFS_ABORTED):
|
||||
result = "aborted";
|
||||
break;
|
||||
case(SD_TIMEOUT):
|
||||
result = "timeout";
|
||||
break;
|
||||
case(SD_LOCKED):
|
||||
result = "file locked";
|
||||
break;
|
||||
case(SD_NOT_ENOUGH_CORE):
|
||||
result = "not enough memory";
|
||||
break;
|
||||
case(SD_TOO_MANY_OPEN_FILES):
|
||||
result = "too many open files";
|
||||
break;
|
||||
case(SD_INVALID_PARAMETER):
|
||||
result = "invalid parameter";
|
||||
break;
|
||||
case(SD_NO_CARD):
|
||||
result = "no SD Card";
|
||||
break;
|
||||
case(SD_NOT_A_FILE):
|
||||
result = "not a file";
|
||||
break;
|
||||
case(SD_NOT_A_DIR):
|
||||
result = "not a directory";
|
||||
break;
|
||||
case(SD_OTHER_APP):
|
||||
result = "opened by other app";
|
||||
break;
|
||||
case(SD_LOW_LEVEL_ERR):
|
||||
result = "low level error";
|
||||
break;
|
||||
default:
|
||||
result = "unknown error";
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
433
applications/sd-filesystem/sd-filesystem.c
Normal file
433
applications/sd-filesystem/sd-filesystem.c
Normal file
@@ -0,0 +1,433 @@
|
||||
#include "fatfs.h"
|
||||
#include "filesystem-api.h"
|
||||
#include "sd-filesystem.h"
|
||||
#include "menu/menu.h"
|
||||
#include "menu/menu_item.h"
|
||||
|
||||
FS_Api* fs_api_alloc() {
|
||||
FS_Api* fs_api = furi_alloc(sizeof(FS_Api));
|
||||
|
||||
// fill file api
|
||||
fs_api->file.open = fs_file_open;
|
||||
fs_api->file.close = fs_file_close;
|
||||
fs_api->file.read = fs_file_read;
|
||||
fs_api->file.write = fs_file_write;
|
||||
fs_api->file.seek = fs_file_seek;
|
||||
fs_api->file.tell = fs_file_tell;
|
||||
fs_api->file.truncate = fs_file_truncate;
|
||||
fs_api->file.size = fs_file_size;
|
||||
fs_api->file.sync = fs_file_sync;
|
||||
fs_api->file.eof = fs_file_eof;
|
||||
|
||||
// fill dir api
|
||||
fs_api->dir.open = fs_dir_open;
|
||||
fs_api->dir.close = fs_dir_close;
|
||||
fs_api->dir.read = fs_dir_read;
|
||||
fs_api->dir.rewind = fs_dir_rewind;
|
||||
|
||||
// fill common api
|
||||
fs_api->common.info = fs_common_info;
|
||||
fs_api->common.remove = fs_common_remove;
|
||||
fs_api->common.rename = fs_common_rename;
|
||||
fs_api->common.set_attr = fs_common_set_attr;
|
||||
fs_api->common.mkdir = fs_common_mkdir;
|
||||
fs_api->common.set_time = fs_common_set_time;
|
||||
fs_api->common.get_fs_info = fs_get_fs_info;
|
||||
|
||||
// fill errors api
|
||||
fs_api->error.get_desc = fs_error_get_desc;
|
||||
fs_api->error.get_internal_desc = fs_error_get_internal_desc;
|
||||
|
||||
return fs_api;
|
||||
}
|
||||
|
||||
void sd_set_lines(SdApp* sd_app, uint8_t count, ...) {
|
||||
va_list argptr;
|
||||
count = min(count, SD_STATE_LINES_COUNT);
|
||||
|
||||
for(uint8_t i = 0; i < SD_STATE_LINES_COUNT; i++) {
|
||||
sd_app->line[i] = "";
|
||||
}
|
||||
|
||||
va_start(argptr, count);
|
||||
|
||||
for(uint8_t i = 0; i < count; i++) {
|
||||
sd_app->line[i] = va_arg(argptr, char*);
|
||||
}
|
||||
|
||||
va_end(argptr);
|
||||
}
|
||||
|
||||
void sd_icon_draw_callback(Canvas* canvas, void* context) {
|
||||
furi_assert(canvas);
|
||||
furi_assert(context);
|
||||
SdApp* sd_app = context;
|
||||
|
||||
switch(sd_app->info.status) {
|
||||
case SD_NO_CARD:
|
||||
break;
|
||||
case SD_OK:
|
||||
canvas_draw_icon(canvas, 0, 0, sd_app->icon.mounted);
|
||||
break;
|
||||
default:
|
||||
canvas_draw_icon(canvas, 0, 0, sd_app->icon.fail);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void sd_app_draw_callback(Canvas* canvas, void* context) {
|
||||
furi_assert(canvas);
|
||||
furi_assert(context);
|
||||
SdApp* sd_app = context;
|
||||
|
||||
canvas_clear(canvas);
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
|
||||
for(uint8_t i = 0; i < SD_STATE_LINES_COUNT; i++) {
|
||||
canvas_draw_str(canvas, 0, (i + 1) * 10, sd_app->line[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void sd_app_input_callback(InputEvent* event, void* context) {
|
||||
furi_assert(context);
|
||||
SdApp* sd_app = context;
|
||||
|
||||
osMessageQueuePut(sd_app->event_queue, event, 0, 0);
|
||||
}
|
||||
|
||||
SdApp* sd_app_alloc() {
|
||||
SdApp* sd_app = furi_alloc(sizeof(SdApp));
|
||||
|
||||
// init inner fs data
|
||||
if(!_fs_init(&sd_app->info)) {
|
||||
furiac_exit(NULL);
|
||||
}
|
||||
|
||||
sd_app->event_queue = osMessageQueueNew(1, sizeof(InputEvent), NULL);
|
||||
|
||||
// init widget
|
||||
sd_app->widget = widget_alloc();
|
||||
widget_draw_callback_set(sd_app->widget, sd_app_draw_callback, sd_app);
|
||||
widget_input_callback_set(sd_app->widget, sd_app_input_callback, sd_app);
|
||||
widget_enabled_set(sd_app->widget, false);
|
||||
|
||||
// init lines
|
||||
sd_set_lines(sd_app, 0);
|
||||
|
||||
// init icon widget
|
||||
sd_app->icon.widget = widget_alloc();
|
||||
sd_app->icon.mounted = assets_icons_get(I_SDcardMounted_11x8);
|
||||
sd_app->icon.fail = assets_icons_get(I_SDcardFail_11x8);
|
||||
widget_set_width(sd_app->icon.widget, icon_get_width(sd_app->icon.mounted));
|
||||
widget_draw_callback_set(sd_app->icon.widget, sd_icon_draw_callback, sd_app);
|
||||
widget_enabled_set(sd_app->icon.widget, false);
|
||||
|
||||
return sd_app;
|
||||
}
|
||||
|
||||
bool app_sd_ask(SdApp* sd_app, Input input_true, Input input_false) {
|
||||
bool result;
|
||||
|
||||
InputEvent event;
|
||||
while(1) {
|
||||
osStatus_t event_status =
|
||||
osMessageQueueGet(sd_app->event_queue, &event, NULL, osWaitForever);
|
||||
|
||||
if(event_status == osOK) {
|
||||
if(event.state && event.input == input_true) {
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
if(event.state && event.input == InputBack) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void app_sd_info_callback(void* context) {
|
||||
furi_assert(context);
|
||||
SdApp* sd_app = context;
|
||||
widget_enabled_set(sd_app->widget, true);
|
||||
|
||||
// dynamic strings
|
||||
const uint8_t str_buffer_size = 26;
|
||||
const uint8_t str_count = 6;
|
||||
char* str_buffer[str_count];
|
||||
bool memory_error = false;
|
||||
|
||||
// info vars
|
||||
uint32_t serial_num;
|
||||
SDError get_label_result, get_free_result;
|
||||
FATFS* fs;
|
||||
uint32_t free_clusters, free_sectors, total_sectors;
|
||||
char volume_label[34];
|
||||
|
||||
// init strings
|
||||
for(uint8_t i = 0; i < str_count; i++) {
|
||||
str_buffer[i] = malloc(str_buffer_size + 1);
|
||||
if(str_buffer[i] == NULL) {
|
||||
memory_error = true;
|
||||
} else {
|
||||
snprintf(str_buffer[i], str_buffer_size, "");
|
||||
}
|
||||
}
|
||||
|
||||
if(memory_error) {
|
||||
sd_set_lines(sd_app, 1, "not enough memory");
|
||||
} else {
|
||||
// get fs info
|
||||
_fs_lock(&sd_app->info);
|
||||
get_label_result = f_getlabel(sd_app->info.path, volume_label, &serial_num);
|
||||
get_free_result = f_getfree(sd_app->info.path, &free_clusters, &fs);
|
||||
_fs_unlock(&sd_app->info);
|
||||
|
||||
// calculate size
|
||||
total_sectors = (fs->n_fatent - 2) * fs->csize;
|
||||
free_sectors = free_clusters * fs->csize;
|
||||
uint16_t sector_size = _MAX_SS;
|
||||
#if _MAX_SS != _MIN_SS
|
||||
sector_size = fs->ssize;
|
||||
#endif
|
||||
|
||||
// output info to dynamic strings
|
||||
if(get_label_result == SD_OK && get_free_result == SD_OK) {
|
||||
snprintf(str_buffer[0], str_buffer_size, "%s", volume_label);
|
||||
|
||||
const char* fs_type = "";
|
||||
|
||||
switch(fs->fs_type) {
|
||||
case(FS_FAT12):
|
||||
fs_type = "FAT12";
|
||||
break;
|
||||
case(FS_FAT16):
|
||||
fs_type = "FAT16";
|
||||
break;
|
||||
case(FS_FAT32):
|
||||
fs_type = "FAT32";
|
||||
break;
|
||||
case(FS_EXFAT):
|
||||
fs_type = "EXFAT";
|
||||
break;
|
||||
default:
|
||||
fs_type = "UNKNOWN";
|
||||
break;
|
||||
}
|
||||
|
||||
snprintf(str_buffer[1], str_buffer_size, "%s, S/N: %lu", fs_type, serial_num);
|
||||
|
||||
snprintf(str_buffer[2], str_buffer_size, "Cluster: %d sectors", fs->csize);
|
||||
snprintf(str_buffer[3], str_buffer_size, "Sector: %d bytes", sector_size);
|
||||
snprintf(
|
||||
str_buffer[4], str_buffer_size, "%lu KB total", total_sectors / 1024 * sector_size);
|
||||
snprintf(
|
||||
str_buffer[5], str_buffer_size, "%lu KB free", free_sectors / 1024 * sector_size);
|
||||
} else {
|
||||
snprintf(str_buffer[0], str_buffer_size, "SD status error:");
|
||||
snprintf(
|
||||
str_buffer[1],
|
||||
str_buffer_size,
|
||||
"%s",
|
||||
fs_error_get_internal_desc(_fs_status(&sd_app->info)));
|
||||
snprintf(str_buffer[2], str_buffer_size, "Label error:");
|
||||
snprintf(
|
||||
str_buffer[3], str_buffer_size, "%s", fs_error_get_internal_desc(get_label_result));
|
||||
snprintf(str_buffer[4], str_buffer_size, "Get free error:");
|
||||
snprintf(
|
||||
str_buffer[5], str_buffer_size, "%s", fs_error_get_internal_desc(get_free_result));
|
||||
}
|
||||
|
||||
// dynamic strings to screen
|
||||
sd_set_lines(
|
||||
sd_app,
|
||||
6,
|
||||
str_buffer[0],
|
||||
str_buffer[1],
|
||||
str_buffer[2],
|
||||
str_buffer[3],
|
||||
str_buffer[4],
|
||||
str_buffer[5]);
|
||||
}
|
||||
|
||||
app_sd_ask(sd_app, InputBack, InputBack);
|
||||
|
||||
sd_set_lines(sd_app, 0);
|
||||
widget_enabled_set(sd_app->widget, false);
|
||||
|
||||
for(uint8_t i = 0; i < str_count; i++) {
|
||||
free(str_buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void app_sd_format_callback(void* context) {
|
||||
furi_assert(context);
|
||||
SdApp* sd_app = context;
|
||||
uint8_t* work_area;
|
||||
|
||||
// ask to really format
|
||||
sd_set_lines(sd_app, 2, "Press UP to format", "or BACK to exit");
|
||||
widget_enabled_set(sd_app->widget, true);
|
||||
|
||||
// wait for input
|
||||
if(!app_sd_ask(sd_app, InputUp, InputBack)) {
|
||||
widget_enabled_set(sd_app->widget, false);
|
||||
return;
|
||||
}
|
||||
|
||||
// show warning
|
||||
sd_set_lines(sd_app, 3, "formatting SD card", "procedure can be lengthy", "please wait");
|
||||
|
||||
// format card
|
||||
_fs_lock(&sd_app->info);
|
||||
work_area = malloc(_MAX_SS);
|
||||
if(work_area == NULL) {
|
||||
sd_app->info.status = SD_NOT_ENOUGH_CORE;
|
||||
} else {
|
||||
sd_app->info.status = f_mkfs(sd_app->info.path, FM_ANY, 0, work_area, _MAX_SS);
|
||||
free(work_area);
|
||||
|
||||
if(sd_app->info.status == SD_OK) {
|
||||
// set label and mount card
|
||||
f_setlabel("Flipper SD");
|
||||
sd_app->info.status = f_mount(&sd_app->info.fat_fs, sd_app->info.path, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if(sd_app->info.status != SD_OK) {
|
||||
sd_set_lines(
|
||||
sd_app, 2, "SD card format error", fs_error_get_internal_desc(sd_app->info.status));
|
||||
} else {
|
||||
sd_set_lines(sd_app, 1, "SD card formatted");
|
||||
}
|
||||
|
||||
_fs_unlock(&sd_app->info);
|
||||
|
||||
// wait for BACK
|
||||
app_sd_ask(sd_app, InputBack, InputBack);
|
||||
|
||||
widget_enabled_set(sd_app->widget, false);
|
||||
}
|
||||
|
||||
void app_sd_unmount_card(SdApp* sd_app) {
|
||||
_fs_lock(&sd_app->info);
|
||||
|
||||
// set status
|
||||
sd_app->info.status = SD_NO_CARD;
|
||||
widget_enabled_set(sd_app->icon.widget, false);
|
||||
|
||||
// close files
|
||||
for(uint8_t index = 0; index < SD_FS_MAX_FILES; index++) {
|
||||
FileData* filedata = &sd_app->info.files[index];
|
||||
|
||||
if(filedata->thread_id != NULL) {
|
||||
if(filedata->is_dir) {
|
||||
f_closedir(&filedata->data.dir);
|
||||
} else {
|
||||
f_close(&filedata->data.file);
|
||||
}
|
||||
filedata->thread_id = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// unmount volume
|
||||
f_mount(0, sd_app->info.path, 0);
|
||||
|
||||
_fs_unlock(&sd_app->info);
|
||||
}
|
||||
|
||||
void app_sd_eject_callback(void* context) {
|
||||
furi_assert(context);
|
||||
SdApp* sd_app = context;
|
||||
|
||||
sd_set_lines(sd_app, 1, "ejecting SD card");
|
||||
widget_enabled_set(sd_app->widget, true);
|
||||
|
||||
app_sd_unmount_card(sd_app);
|
||||
|
||||
sd_set_lines(sd_app, 1, "SD card can be pulled out");
|
||||
|
||||
// wait for BACK
|
||||
app_sd_ask(sd_app, InputBack, InputBack);
|
||||
|
||||
widget_enabled_set(sd_app->widget, false);
|
||||
}
|
||||
|
||||
void sd_filesystem(void* p) {
|
||||
SdApp* sd_app = sd_app_alloc();
|
||||
FS_Api* fs_api = fs_api_alloc();
|
||||
|
||||
Gui* gui = furi_open("gui");
|
||||
gui_add_widget(gui, sd_app->widget, GuiLayerFullscreen);
|
||||
gui_add_widget(gui, sd_app->icon.widget, GuiLayerStatusBarLeft);
|
||||
|
||||
// add api record
|
||||
if(!furi_create("sdcard", fs_api)) {
|
||||
furiac_exit(NULL);
|
||||
}
|
||||
|
||||
// init menu
|
||||
// TODO menu icon
|
||||
MenuItem* menu_item;
|
||||
menu_item = menu_item_alloc_menu("SD Card", assets_icons_get(I_SDcardMounted_11x8));
|
||||
|
||||
menu_item_subitem_add(
|
||||
menu_item, menu_item_alloc_function("Info", NULL, app_sd_info_callback, sd_app));
|
||||
menu_item_subitem_add(
|
||||
menu_item, menu_item_alloc_function("Format", NULL, app_sd_format_callback, sd_app));
|
||||
menu_item_subitem_add(
|
||||
menu_item, menu_item_alloc_function("Eject", NULL, app_sd_eject_callback, sd_app));
|
||||
|
||||
// add item to menu
|
||||
ValueMutex* menu_vm = furi_open("menu");
|
||||
furi_check(menu_vm);
|
||||
with_value_mutex(
|
||||
menu_vm, (Menu * menu) { menu_item_add(menu, menu_item); });
|
||||
|
||||
furiac_ready();
|
||||
|
||||
printf("[sd_filesystem] start\n");
|
||||
|
||||
// sd card cycle
|
||||
bool sd_was_present = true;
|
||||
|
||||
while(true) {
|
||||
if(sd_was_present) {
|
||||
if(hal_gpio_read_sd_detect()) {
|
||||
printf("[sd_filesystem] card detected\n");
|
||||
|
||||
uint8_t bsp_result = BSP_SD_Init();
|
||||
|
||||
if(bsp_result) {
|
||||
sd_app->info.status = SD_LOW_LEVEL_ERR;
|
||||
printf("[sd_filesystem] bsp error: %x\n", bsp_result);
|
||||
} else {
|
||||
printf("[sd_filesystem] bsp ok\n");
|
||||
sd_app->info.status = f_mount(&sd_app->info.fat_fs, sd_app->info.path, 1);
|
||||
|
||||
if(sd_app->info.status != SD_OK) {
|
||||
printf("[sd_filesystem] mount error: %d\n", sd_app->info.status);
|
||||
} else {
|
||||
printf("[sd_filesystem] mount ok\n");
|
||||
}
|
||||
}
|
||||
|
||||
widget_enabled_set(sd_app->icon.widget, true);
|
||||
sd_was_present = false;
|
||||
}
|
||||
} else {
|
||||
if(!hal_gpio_read_sd_detect()) {
|
||||
printf("[sd_filesystem] card removed\n");
|
||||
|
||||
widget_enabled_set(sd_app->icon.widget, false);
|
||||
app_sd_unmount_card(sd_app);
|
||||
sd_was_present = true;
|
||||
}
|
||||
}
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
}
|
122
applications/sd-filesystem/sd-filesystem.h
Normal file
122
applications/sd-filesystem/sd-filesystem.h
Normal file
@@ -0,0 +1,122 @@
|
||||
#pragma once
|
||||
#include "flipper.h"
|
||||
#include "flipper_v2.h"
|
||||
|
||||
#define SD_FS_MAX_FILES _FS_LOCK
|
||||
#define SD_STATE_LINES_COUNT 6
|
||||
|
||||
#ifndef min
|
||||
#define min(a, b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
/* api data */
|
||||
typedef FIL SDFile;
|
||||
typedef DIR SDDir;
|
||||
typedef FILINFO SDFileInfo;
|
||||
|
||||
/* storage for file/directory objects*/
|
||||
typedef union {
|
||||
SDFile file;
|
||||
SDDir dir;
|
||||
} SDFileDirStorage;
|
||||
|
||||
typedef enum {
|
||||
SD_OK = FR_OK,
|
||||
SD_DISK_ERR = FR_DISK_ERR,
|
||||
SD_INT_ERR = FR_INT_ERR,
|
||||
SD_NO_FILE = FR_NO_FILE,
|
||||
SD_NO_PATH = FR_NO_PATH,
|
||||
SD_INVALID_NAME = FR_INVALID_NAME,
|
||||
SD_DENIED = FR_DENIED,
|
||||
SD_EXIST = FR_EXIST,
|
||||
SD_INVALID_OBJECT = FR_INVALID_OBJECT,
|
||||
SD_WRITE_PROTECTED = FR_WRITE_PROTECTED,
|
||||
SD_INVALID_DRIVE = FR_INVALID_DRIVE,
|
||||
SD_NOT_ENABLED = FR_NOT_ENABLED,
|
||||
SD_NO_FILESYSTEM = FR_NO_FILESYSTEM,
|
||||
SD_MKFS_ABORTED = FR_MKFS_ABORTED,
|
||||
SD_TIMEOUT = FR_TIMEOUT,
|
||||
SD_LOCKED = FR_LOCKED,
|
||||
SD_NOT_ENOUGH_CORE = FR_NOT_ENOUGH_CORE,
|
||||
SD_TOO_MANY_OPEN_FILES = FR_TOO_MANY_OPEN_FILES,
|
||||
SD_INVALID_PARAMETER = FR_INVALID_PARAMETER,
|
||||
SD_NO_CARD,
|
||||
SD_NOT_A_FILE,
|
||||
SD_NOT_A_DIR,
|
||||
SD_OTHER_APP,
|
||||
SD_LOW_LEVEL_ERR,
|
||||
} SDError;
|
||||
|
||||
typedef enum {
|
||||
FDF_DIR,
|
||||
FDF_FILE,
|
||||
FDF_ANY,
|
||||
} FiledataFilter;
|
||||
|
||||
typedef struct {
|
||||
osThreadId_t thread_id;
|
||||
bool is_dir;
|
||||
SDFileDirStorage data;
|
||||
} FileData;
|
||||
|
||||
/* application data */
|
||||
typedef struct {
|
||||
Widget* widget;
|
||||
Icon* mounted;
|
||||
Icon* fail;
|
||||
} SdFsIcon;
|
||||
|
||||
typedef struct {
|
||||
osMutexId_t mutex;
|
||||
FileData files[SD_FS_MAX_FILES];
|
||||
SDError status;
|
||||
char* path;
|
||||
FATFS fat_fs;
|
||||
} SdFsInfo;
|
||||
|
||||
typedef struct {
|
||||
SdFsInfo info;
|
||||
SdFsIcon icon;
|
||||
|
||||
Widget* widget;
|
||||
const char* line[SD_STATE_LINES_COUNT];
|
||||
osMessageQueueId_t event_queue;
|
||||
} SdApp;
|
||||
|
||||
/* core api fns */
|
||||
bool _fs_init(SdFsInfo* _fs_info);
|
||||
bool _fs_lock(SdFsInfo* fs_info);
|
||||
bool _fs_unlock(SdFsInfo* fs_info);
|
||||
SDError _fs_status(SdFsInfo* fs_info);
|
||||
|
||||
/* sd api fns */
|
||||
bool fs_file_open(File* file, const char* path, FS_AccessMode access_mode, FS_OpenMode open_mode);
|
||||
bool fs_file_close(File* file);
|
||||
uint16_t fs_file_read(File* file, void* buff, uint16_t bytes_to_read);
|
||||
uint16_t fs_file_write(File* file, void* buff, uint16_t bytes_to_write);
|
||||
bool fs_file_seek(File* file, uint32_t offset, bool from_start);
|
||||
uint64_t fs_file_tell(File* file);
|
||||
bool fs_file_truncate(File* file);
|
||||
uint64_t fs_file_size(File* file);
|
||||
bool fs_file_sync(File* file);
|
||||
bool fs_file_eof(File* file);
|
||||
|
||||
/* dir api */
|
||||
bool fs_dir_open(File* file, const char* path);
|
||||
bool fs_dir_close(File* file);
|
||||
bool fs_dir_read(File* file, FileInfo* fileinfo, char* name, uint16_t name_length);
|
||||
bool fs_dir_rewind(File* file);
|
||||
|
||||
/* common api */
|
||||
FS_Error
|
||||
fs_common_info(const char* path, FileInfo* fileinfo, char* name, const uint16_t name_length);
|
||||
FS_Error fs_common_remove(const char* path);
|
||||
FS_Error fs_common_rename(const char* old_path, const char* new_path);
|
||||
FS_Error fs_common_set_attr(const char* path, uint8_t attr, uint8_t mask);
|
||||
FS_Error fs_common_mkdir(const char* path);
|
||||
FS_Error fs_common_set_time(const char* path, FileDateUnion date, FileTimeUnion time);
|
||||
FS_Error fs_get_fs_info(uint64_t* total_space, uint64_t* free_space);
|
||||
|
||||
/* errors api */
|
||||
const char* fs_error_get_desc(FS_Error error_id);
|
||||
const char* fs_error_get_internal_desc(uint32_t internal_error_id);
|
Reference in New Issue
Block a user