unleashed-firmware/applications/storage/storage-external-api.c

395 lines
9.2 KiB
C
Raw Normal View History

[FL-1191][FL-1524] Filesystem rework (#568) * FS-Api: removed datetime manipulation functions and most of the file flags * Filesystem: common proxy api * Filesystem: renamed to Storage. Work has begun on a glue layer. Added functions for reentrance. * Storage: sd mount and sd file open * Storage: sd file close * Storage: temporary test app * Storage: free filedata on close * Storage: sd file read and write * Storage: added internal storage (LittleFS) * Storage: renamed internal commands * Storage: seek, tell, truncate, size, sync, eof * Storage: error descriptions * Storage: directory management api (open, close, read, rewind) * Storage: common management api (stat, fs_stat, remove, rename, mkdir) * Dolphin app and Notifications app now use raw storage. * Storage: storage statuses renamed. Implemented sd card icon. * Storage: added raw sd-card api. * Storage settings: work started * Assets: use new icons approach * Storage settings: working storage settings * Storage: completely redesigned api, no longer sticking out FS_Api * Storage: more simplified api, getting error_id from file is hidden from user, pointer to api is hidden inside file * Storage: cli info and format commands * Storage-cli: file list * Storage: a simpler and more reliable api * FatFS: slightly lighter and faster config. Also disabled reentrancy and file locking functions. They moved to a storage service. * Storage-cli: accommodate to the new cli api. * Storage: filesystem api is separated into internal and common api. * Cli: added the ability to print the list of free heap blocks * Storage: uses a list instead of an array to store the StorageFile. Rewrote api calls to use semaphores instead of thread flags. * Storage settings: added the ability to benchmark the SD card. * Gui module file select: uses new storage api * Apps: removed deprecated sd_card_test application * Args lib: support for enquoted arguments * Dialogs: a new gui app for simple non-asynchronous apps * Dialogs: view holder for easy single view work * File worker: use new storage api * IButton and lfrrfid apps: save keys to any storage * Apps: fix ibutton and lfrfid stack, remove sd_card_test. * SD filesystem: app removed * File worker: fixed api pointer type * Subghz: loading assets using the new storage api * NFC: use the new storage api * Dialogs: the better api for the message element * Archive: use new storage api * Irda: changed assest path, changed app path * FileWorker: removed unused file_buf_cnt * Storage: copying and renaming files now works between storages * Storage cli: read, copy, remove, rename commands * Archive: removed commented code * Storage cli: write command * Applications: add SRV_STORAGE and SRV_DIALOGS * Internal-storage: removed * Storage: improved api * Storage app: changed api pointer from StorageApp to Storage * Storage: better file_id handling * Storage: more consistent errors * Loader: support for NULL icons * Storage: do nothing with the lfs file or directory if it is not open * Storage: fix typo * Storage: minor float usage cleanup, rename some symbols. * Storage: compact doxygen comments. Co-authored-by: あく <alleteam@gmail.com>
2021-07-23 12:20:19 +00:00
#include "storage.h"
#include "storage-i.h"
#include "storage-message.h"
#define S_API_PROLOGUE \
osSemaphoreId_t semaphore = osSemaphoreNew(1, 0, NULL); \
furi_check(semaphore != NULL);
#define S_FILE_API_PROLOGUE \
Storage* storage = file->storage; \
furi_assert(storage);
#define S_API_EPILOGUE \
furi_check(osMessageQueuePut(storage->message_queue, &message, 0, osWaitForever) == osOK); \
osSemaphoreAcquire(semaphore, osWaitForever); \
osSemaphoreDelete(semaphore);
#define S_API_MESSAGE(_command) \
SAReturn return_data; \
StorageMessage message = { \
.semaphore = semaphore, \
.command = _command, \
.data = &data, \
.return_data = &return_data, \
};
#define S_API_DATA_FILE \
SAData data = { \
.file = { \
.file = file, \
}};
#define S_API_DATA_PATH \
SAData data = { \
.path = { \
.path = path, \
}};
#define S_RETURN_BOOL (return_data.bool_value);
#define S_RETURN_UINT16 (return_data.uint16_value);
#define S_RETURN_UINT64 (return_data.uint64_value);
#define S_RETURN_ERROR (return_data.error_value);
#define S_RETURN_CSTRING (return_data.cstring_value);
#define FILE_OPENED 1
#define FILE_CLOSED 0
/****************** FILE ******************/
bool storage_file_open(
File* file,
const char* path,
FS_AccessMode access_mode,
FS_OpenMode open_mode) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
SAData data = {
.fopen = {
.file = file,
.path = path,
.access_mode = access_mode,
.open_mode = open_mode,
}};
file->file_id = FILE_OPENED;
S_API_MESSAGE(StorageCommandFileOpen);
S_API_EPILOGUE;
return S_RETURN_BOOL;
}
bool storage_file_close(File* file) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
S_API_DATA_FILE;
S_API_MESSAGE(StorageCommandFileClose);
S_API_EPILOGUE;
file->file_id = FILE_CLOSED;
return S_RETURN_BOOL;
}
uint16_t storage_file_read(File* file, void* buff, uint16_t bytes_to_read) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
SAData data = {
.fread = {
.file = file,
.buff = buff,
.bytes_to_read = bytes_to_read,
}};
S_API_MESSAGE(StorageCommandFileRead);
S_API_EPILOGUE;
return S_RETURN_UINT16;
}
uint16_t storage_file_write(File* file, const void* buff, uint16_t bytes_to_write) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
SAData data = {
.fwrite = {
.file = file,
.buff = buff,
.bytes_to_write = bytes_to_write,
}};
S_API_MESSAGE(StorageCommandFileWrite);
S_API_EPILOGUE;
return S_RETURN_UINT16;
}
bool storage_file_seek(File* file, uint32_t offset, bool from_start) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
SAData data = {
.fseek = {
.file = file,
.offset = offset,
.from_start = from_start,
}};
S_API_MESSAGE(StorageCommandFileSeek);
S_API_EPILOGUE;
return S_RETURN_BOOL;
}
uint64_t storage_file_tell(File* file) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
S_API_DATA_FILE;
S_API_MESSAGE(StorageCommandFileTell);
S_API_EPILOGUE;
return S_RETURN_UINT64;
}
bool storage_file_truncate(File* file) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
S_API_DATA_FILE;
S_API_MESSAGE(StorageCommandFileTruncate);
S_API_EPILOGUE;
return S_RETURN_BOOL;
}
uint64_t storage_file_size(File* file) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
S_API_DATA_FILE;
S_API_MESSAGE(StorageCommandFileSize);
S_API_EPILOGUE;
return S_RETURN_UINT64;
}
bool storage_file_sync(File* file) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
S_API_DATA_FILE;
S_API_MESSAGE(StorageCommandFileSync);
S_API_EPILOGUE;
return S_RETURN_BOOL;
}
bool storage_file_eof(File* file) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
S_API_DATA_FILE;
S_API_MESSAGE(StorageCommandFileEof);
S_API_EPILOGUE;
return S_RETURN_BOOL;
}
/****************** DIR ******************/
bool storage_dir_open(File* file, const char* path) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
SAData data = {
.dopen = {
.file = file,
.path = path,
}};
file->file_id = FILE_OPENED;
S_API_MESSAGE(StorageCommandDirOpen);
S_API_EPILOGUE;
return S_RETURN_BOOL;
}
bool storage_dir_close(File* file) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
S_API_DATA_FILE;
S_API_MESSAGE(StorageCommandDirClose);
S_API_EPILOGUE;
file->file_id = FILE_CLOSED;
return S_RETURN_BOOL;
}
bool storage_dir_read(File* file, FileInfo* fileinfo, char* name, uint16_t name_length) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
SAData data = {
.dread = {
.file = file,
.fileinfo = fileinfo,
.name = name,
.name_length = name_length,
}};
S_API_MESSAGE(StorageCommandDirRead);
S_API_EPILOGUE;
return S_RETURN_BOOL;
}
bool storage_dir_rewind(File* file) {
S_FILE_API_PROLOGUE;
S_API_PROLOGUE;
S_API_DATA_FILE;
S_API_MESSAGE(StorageCommandDirRewind);
S_API_EPILOGUE;
return S_RETURN_BOOL;
}
/****************** COMMON ******************/
FS_Error storage_common_stat(Storage* storage, const char* path, FileInfo* fileinfo) {
S_API_PROLOGUE;
SAData data = {.cstat = {.path = path, .fileinfo = fileinfo}};
S_API_MESSAGE(StorageCommandCommonStat);
S_API_EPILOGUE;
return S_RETURN_ERROR;
}
FS_Error storage_common_remove(Storage* storage, const char* path) {
S_API_PROLOGUE;
S_API_DATA_PATH;
S_API_MESSAGE(StorageCommandCommonRemove);
S_API_EPILOGUE;
return S_RETURN_ERROR;
}
FS_Error storage_common_rename(Storage* storage, const char* old_path, const char* new_path) {
S_API_PROLOGUE;
SAData data = {
.cpaths = {
.old = old_path,
.new = new_path,
}};
S_API_MESSAGE(StorageCommandCommonRename);
S_API_EPILOGUE;
return S_RETURN_ERROR;
}
FS_Error storage_common_copy(Storage* storage, const char* old_path, const char* new_path) {
S_API_PROLOGUE;
SAData data = {
.cpaths = {
.old = old_path,
.new = new_path,
}};
S_API_MESSAGE(StorageCommandCommonCopy);
S_API_EPILOGUE;
return S_RETURN_ERROR;
}
FS_Error storage_common_mkdir(Storage* storage, const char* path) {
S_API_PROLOGUE;
S_API_DATA_PATH;
S_API_MESSAGE(StorageCommandCommonMkDir);
S_API_EPILOGUE;
return S_RETURN_ERROR;
}
FS_Error storage_common_fs_info(
Storage* storage,
const char* fs_path,
uint64_t* total_space,
uint64_t* free_space) {
S_API_PROLOGUE;
SAData data = {
.cfsinfo = {
.fs_path = fs_path,
.total_space = total_space,
.free_space = free_space,
}};
S_API_MESSAGE(StorageCommandCommonFSInfo);
S_API_EPILOGUE;
return S_RETURN_ERROR;
}
/****************** ERROR ******************/
const char* storage_error_get_desc(FS_Error error_id) {
return filesystem_api_error_get_desc(error_id);
}
FS_Error storage_file_get_error(File* file) {
furi_check(file != NULL);
return file->error_id;
}
const char* storage_file_get_error_desc(File* file) {
furi_check(file != NULL);
return filesystem_api_error_get_desc(file->error_id);
}
/****************** Raw SD API ******************/
FS_Error storage_sd_format(Storage* storage) {
S_API_PROLOGUE;
SAData data = {};
S_API_MESSAGE(StorageCommandSDFormat);
S_API_EPILOGUE;
return S_RETURN_ERROR;
}
FS_Error storage_sd_unmount(Storage* storage) {
S_API_PROLOGUE;
SAData data = {};
S_API_MESSAGE(StorageCommandSDUnmount);
S_API_EPILOGUE;
return S_RETURN_ERROR;
}
FS_Error storage_sd_info(Storage* storage, SDInfo* info) {
S_API_PROLOGUE;
SAData data = {
.sdinfo = {
.info = info,
}};
S_API_MESSAGE(StorageCommandSDInfo);
S_API_EPILOGUE;
return S_RETURN_ERROR;
}
FS_Error storage_sd_status(Storage* storage) {
S_API_PROLOGUE;
SAData data = {};
S_API_MESSAGE(StorageCommandSDStatus);
S_API_EPILOGUE;
return S_RETURN_ERROR;
}
File* storage_file_alloc(Storage* storage) {
File* file = furi_alloc(sizeof(File));
file->file_id = FILE_CLOSED;
file->storage = storage;
return file;
}
bool storage_file_is_open(File* file) {
return (file->file_id != FILE_CLOSED);
}
void storage_file_free(File* file) {
if(storage_file_is_open(file)) {
storage_file_close(file);
}
free(file);
}
bool storage_simply_remove(Storage* storage, const char* path) {
FS_Error result;
result = storage_common_remove(storage, path);
return result == FSE_OK || result == FSE_NOT_EXIST;
}
bool storage_simply_mkdir(Storage* storage, const char* path) {
FS_Error result;
result = storage_common_mkdir(storage, path);
return result == FSE_OK || result == FSE_EXIST;
[FL-1191][FL-1524] Filesystem rework (#568) * FS-Api: removed datetime manipulation functions and most of the file flags * Filesystem: common proxy api * Filesystem: renamed to Storage. Work has begun on a glue layer. Added functions for reentrance. * Storage: sd mount and sd file open * Storage: sd file close * Storage: temporary test app * Storage: free filedata on close * Storage: sd file read and write * Storage: added internal storage (LittleFS) * Storage: renamed internal commands * Storage: seek, tell, truncate, size, sync, eof * Storage: error descriptions * Storage: directory management api (open, close, read, rewind) * Storage: common management api (stat, fs_stat, remove, rename, mkdir) * Dolphin app and Notifications app now use raw storage. * Storage: storage statuses renamed. Implemented sd card icon. * Storage: added raw sd-card api. * Storage settings: work started * Assets: use new icons approach * Storage settings: working storage settings * Storage: completely redesigned api, no longer sticking out FS_Api * Storage: more simplified api, getting error_id from file is hidden from user, pointer to api is hidden inside file * Storage: cli info and format commands * Storage-cli: file list * Storage: a simpler and more reliable api * FatFS: slightly lighter and faster config. Also disabled reentrancy and file locking functions. They moved to a storage service. * Storage-cli: accommodate to the new cli api. * Storage: filesystem api is separated into internal and common api. * Cli: added the ability to print the list of free heap blocks * Storage: uses a list instead of an array to store the StorageFile. Rewrote api calls to use semaphores instead of thread flags. * Storage settings: added the ability to benchmark the SD card. * Gui module file select: uses new storage api * Apps: removed deprecated sd_card_test application * Args lib: support for enquoted arguments * Dialogs: a new gui app for simple non-asynchronous apps * Dialogs: view holder for easy single view work * File worker: use new storage api * IButton and lfrrfid apps: save keys to any storage * Apps: fix ibutton and lfrfid stack, remove sd_card_test. * SD filesystem: app removed * File worker: fixed api pointer type * Subghz: loading assets using the new storage api * NFC: use the new storage api * Dialogs: the better api for the message element * Archive: use new storage api * Irda: changed assest path, changed app path * FileWorker: removed unused file_buf_cnt * Storage: copying and renaming files now works between storages * Storage cli: read, copy, remove, rename commands * Archive: removed commented code * Storage cli: write command * Applications: add SRV_STORAGE and SRV_DIALOGS * Internal-storage: removed * Storage: improved api * Storage app: changed api pointer from StorageApp to Storage * Storage: better file_id handling * Storage: more consistent errors * Loader: support for NULL icons * Storage: do nothing with the lfs file or directory if it is not open * Storage: fix typo * Storage: minor float usage cleanup, rename some symbols. * Storage: compact doxygen comments. Co-authored-by: あく <alleteam@gmail.com>
2021-07-23 12:20:19 +00:00
}