unleashed-firmware/applications/storage/storage.h
hedger e02040107b
[FL-2263] Flasher service & RAM exec (#1006)
* WIP on stripping fw
* Compact FW build - use RAM_EXEC=1 COMPACT=1 DEBUG=0
* Fixed uninitialized storage struct; small fixes to compact fw
* Flasher srv w/mocked flash ops
* Fixed typos & accomodated FFF changes
* Alternative fw startup branch
* Working load & jmp to RAM fw
* +manifest processing for stage loader; + crc verification for stage payload
* Fixed questionable code & potential leaks
* Lowered screen update rate; added radio stack update stubs; working dfu write
* Console EP with manifest & stage validation
* Added microtar lib; minor ui fixes for updater
* Removed microtar
* Removed mtar #2
* Added a better version of microtar
* TAR archive api; LFS backup & restore core
* Recursive backup/restore
* LFS worker thread
* Added system apps to loader - not visible in UI; full update process with restarts
* Typo fix
* Dropped BL & f6; tooling for updater WIP
* Minor py fixes
* Minor fixes to make it build after merge
* Ported flash workaround from BL + fixed visuals
* Minor cleanup
* Chmod + loader app search fix
* Python linter fix
* Removed usb stuff & float read support for staged loader == -10% of binary size
* Added backup/restore & update pb requests
* Added stub impl to RPC for backup/restore/update commands
* Reworked TAR to use borrowed Storage api; slightly reduced build size by removing `static string`; hidden update-related RPC behind defines
* Moved backup&restore to storage
* Fixed new message types
* Backup/restore/update RPC impl
* Moved furi_hal_crc to LL; minor fixes
* CRC HAL rework to LL
* Purging STM HAL
* Brought back minimal DFU boot mode (no gui); additional crc state checks
* Added splash screen, BROKEN usb function
* Clock init rework WIP
* Stripped graphics from DFU mode
* Temp fix for unused static fun
* WIP update picker - broken!
* Fixed UI
* Bumping version
* Fixed RTC setup
* Backup to update folder instead of ext root
* Removed unused scenes & more usb remnants from staged loader
* CI updates
* Fixed update bundle name
* Temporary restored USB handler
* Attempt to prevent .text corruption
* Comments on how I spent this Saturday
* Added update file icon
* Documentation updates
* Moved common code to lib folder
* Storage: more unit tests
* Storage: blocking dir open, differentiate file and dir when freed.
* Major refactoring; added input processing to updater to allow retrying on failures (not very useful prob). Added API for extraction of thread return value
* Removed re-init check for manifest
* Changed low-level path manipulation to toolbox/path.h; makefile cleanup; tiny fix in lint.py
* Increased update worker stack size
* Text fixes in backup CLI
* Displaying number of update stages to run; removed timeout in handling errors
* Bumping version
* Added thread cleanup for spawner thread
* Updated build targets to exclude firmware bundle from 'ALL'
* Fixed makefile for update_package; skipping VCP init for update mode (ugly)
* Switched github build from ALL to update_package
* Added +x for dist_update.sh
* Cli: add total heap size to "free" command
* Moved (RAM) suffix to build version instead of git commit no.
* DFU comment
* Some fixes suggested by clang-tidy
* Fixed recursive PREFIX macro
* Makefile: gather all new rules in updater namespace. FuriHal: rename bootloader to boot, isr safe delays
* Github: correct build target name in firmware build
* FuriHal: move target switch to boot
* Makefile: fix firmware flash
* Furi, FuriHal: move kernel start to furi, early init
* Drop bootloader related stuff
* Drop cube. Drop bootloader linker script.
* Renamed update_hl, moved constants to #defines
* Moved update-related boot mode to separate bitfield
* Reworked updater cli to single entry point; fixed crash on tar cleanup
* Added Python replacement for dist shell scripts
* Linter fixes for dist.py +x
* Fixes for environment suffix
* Dropped bash scripts
* Added dirty build flag to version structure & interfaces
* Version string escapes
* Fixed flag logic in dist.py; added support for App instances being imported and not terminating the whole program
* Fixed fw address in ReadMe.md
* Rpc: fix crash on double screen start
* Return back original boot behavior and fix jump to system bootloader
* Cleanup code, add error sequence for RTC
* Update firmware readme
* FuriHal: drop boot, restructure RTC registers usage and add header register check
* Furi goes first
* Toolchain: add ccache support
* Renamed update bundle dir

Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
Co-authored-by: あく <alleteam@gmail.com>
2022-04-13 23:50:25 +03:00

328 lines
9.9 KiB
C

#pragma once
#include <stdint.h>
#include <m-string.h>
#include "filesystem_api_defines.h"
#include "storage_sd_api.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct Storage Storage;
/** Allocates and initializes a file descriptor
* @return File*
*/
File* storage_file_alloc(Storage* storage);
/** Frees the file descriptor. Closes the file if it was open.
*/
void storage_file_free(File* file);
typedef enum {
StorageEventTypeCardMount,
StorageEventTypeCardUnmount,
StorageEventTypeCardMountError,
StorageEventTypeFileClose,
StorageEventTypeDirClose,
} StorageEventType;
typedef struct {
StorageEventType type;
} StorageEvent;
/**
* Get storage pubsub.
* Storage will send StorageEvent messages.
* @param storage
* @return FuriPubSub*
*/
FuriPubSub* storage_get_pubsub(Storage* storage);
/******************* File Functions *******************/
/** Opens an existing file or create a new one.
* @param file pointer to file object.
* @param path path to file
* @param access_mode access mode from FS_AccessMode
* @param open_mode open mode from FS_OpenMode
* @return success flag. You need to close the file even if the open operation failed.
*/
bool storage_file_open(
File* file,
const char* path,
FS_AccessMode access_mode,
FS_OpenMode open_mode);
/** Close the file.
* @param file pointer to a file object, the file object will be freed.
* @return success flag
*/
bool storage_file_close(File* file);
/** Tells if the file is open
* @param file pointer to a file object
* @return bool true if file is open
*/
bool storage_file_is_open(File* file);
/** Tells if the file is a directory
* @param file pointer to a file object
* @return bool true if file is a directory
*/
bool storage_file_is_dir(File* file);
/** Reads bytes from a file into a buffer
* @param file pointer to file object.
* @param buff pointer to a buffer, for reading
* @param bytes_to_read how many bytes to read. Must be less than or equal to the size of the buffer.
* @return uint16_t how many bytes were actually readed
*/
uint16_t storage_file_read(File* file, void* buff, uint16_t bytes_to_read);
/** Writes bytes from a buffer to a file
* @param file pointer to file object.
* @param buff pointer to buffer, for writing
* @param bytes_to_write how many bytes to write. Must be less than or equal to the size of the buffer.
* @return uint16_t how many bytes were actually written
*/
uint16_t storage_file_write(File* file, const void* buff, uint16_t bytes_to_write);
/** Moves the r/w pointer
* @param file pointer to file object.
* @param offset offset to move the r/w pointer
* @param from_start set an offset from the start or from the current position
* @return success flag
*/
bool storage_file_seek(File* file, uint32_t offset, bool from_start);
/** Gets the position of the r/w pointer
* @param file pointer to file object.
* @return uint64_t position of the r/w pointer
*/
uint64_t storage_file_tell(File* file);
/** Truncates the file size to the current position of the r/w pointer
* @param file pointer to file object.
* @return bool success flag
*/
bool storage_file_truncate(File* file);
/** Gets the size of the file
* @param file pointer to file object.
* @return uint64_t size of the file
*/
uint64_t storage_file_size(File* file);
/** Writes file cache to storage
* @param file pointer to file object.
* @return bool success flag
*/
bool storage_file_sync(File* file);
/** Checks that the r/w pointer is at the end of the file
* @param file pointer to file object.
* @return bool success flag
*/
bool storage_file_eof(File* file);
/******************* Dir Functions *******************/
/** Opens a directory to get objects from it
* @param app pointer to the api
* @param file pointer to file object.
* @param path path to directory
* @return bool success flag. You need to close the directory even if the open operation failed.
*/
bool storage_dir_open(File* file, const char* path);
/** Close the directory. Also free file handle structure and point it to the NULL.
* @param file pointer to a file object.
* @return bool success flag
*/
bool storage_dir_close(File* file);
/** Reads the next object in the directory
* @param file pointer to file object.
* @param fileinfo pointer to the readed FileInfo, may be NULL
* @param name pointer to name buffer, may be NULL
* @param name_length name buffer length
* @return success flag (if the next object does not exist, it also returns false and sets the file error id to FSE_NOT_EXIST)
*/
bool storage_dir_read(File* file, FileInfo* fileinfo, char* name, uint16_t name_length);
/** Rewinds the read pointer to first item in the directory
* @param file pointer to file object.
* @return bool success flag
*/
bool storage_dir_rewind(File* file);
/******************* Common Functions *******************/
/** Retrieves information about a file/directory
* @param app pointer to the api
* @param path path to file/directory
* @param fileinfo pointer to the readed FileInfo, may be NULL
* @return FS_Error operation result
*/
FS_Error storage_common_stat(Storage* storage, const char* path, FileInfo* fileinfo);
/** Removes a file/directory from the repository, the directory must be empty and the file/directory must not be open
* @param app pointer to the api
* @param path
* @return FS_Error operation result
*/
FS_Error storage_common_remove(Storage* storage, const char* path);
/** Renames file/directory, file/directory must not be open
* @param app pointer to the api
* @param old_path old path
* @param new_path new path
* @return FS_Error operation result
*/
FS_Error storage_common_rename(Storage* storage, const char* old_path, const char* new_path);
/** Copy file, file must not be open
* @param app pointer to the api
* @param old_path old path
* @param new_path new path
* @return FS_Error operation result
*/
FS_Error storage_common_copy(Storage* storage, const char* old_path, const char* new_path);
/** Creates a directory
* @param app pointer to the api
* @param path directory path
* @return FS_Error operation result
*/
FS_Error storage_common_mkdir(Storage* storage, const char* path);
/** Gets general information about the storage
* @param app pointer to the api
* @param fs_path the path to the storage of interest
* @param total_space pointer to total space record, will be filled
* @param free_space pointer to free space record, will be filled
* @return FS_Error operation result
*/
FS_Error storage_common_fs_info(
Storage* storage,
const char* fs_path,
uint64_t* total_space,
uint64_t* free_space);
/******************* Error Functions *******************/
/** Retrieves the error text from the error id
* @param error_id error id
* @return const char* error text
*/
const char* storage_error_get_desc(FS_Error error_id);
/** Retrieves the error id from the file object
* @param file pointer to file object. Pointer must not point to NULL. YOU CANNOT RETREIVE THE ERROR ID IF THE FILE HAS BEEN CLOSED
* @return FS_Error error id
*/
FS_Error storage_file_get_error(File* file);
/** Retrieves the internal (storage-specific) error id from the file object
* @param file pointer to file object. Pointer must not point to NULL. YOU CANNOT RETREIVE THE INTERNAL ERROR ID IF THE FILE HAS BEEN CLOSED
* @return FS_Error error id
*/
int32_t storage_file_get_internal_error(File* file);
/** Retrieves the error text from the file object
* @param file pointer to file object. Pointer must not point to NULL. YOU CANNOT RETREIVE THE ERROR TEXT IF THE FILE HAS BEEN CLOSED
* @return const char* error text
*/
const char* storage_file_get_error_desc(File* file);
/******************* SD Card Functions *******************/
/** Formats SD Card
* @param api pointer to the api
* @return FS_Error operation result
*/
FS_Error storage_sd_format(Storage* api);
/** Will unmount the SD card
* @param api pointer to the api
* @return FS_Error operation result
*/
FS_Error storage_sd_unmount(Storage* api);
/** Retrieves SD card information
* @param api pointer to the api
* @param info pointer to the info
* @return FS_Error operation result
*/
FS_Error storage_sd_info(Storage* api, SDInfo* info);
/** Retrieves SD card status
* @param api pointer to the api
* @return FS_Error operation result
*/
FS_Error storage_sd_status(Storage* api);
/******************* Internal LFS Functions *******************/
/** Backs up internal storage to a tar archive
* @param api pointer to the api
* @param dstmane destination archive path
* @return FS_Error operation result
*/
FS_Error storage_int_backup(Storage* api, const char* dstname);
/** Restores internal storage from a tar archive
* @param api pointer to the api
* @param dstmane archive path
* @return FS_Error operation result
*/
FS_Error storage_int_restore(Storage* api, const char* dstname);
/***************** Simplified Functions ******************/
/**
* Removes a file/directory from the repository, the directory must be empty and the file/directory must not be open
* @param storage pointer to the api
* @param path
* @return true on success or if file/dir is not exist
*/
bool storage_simply_remove(Storage* storage, const char* path);
/**
* Removes a file/directory from the repository, the directory can be not empty
* @param storage pointer to the api
* @param path
* @return true on success or if file/dir is not exist
*/
bool storage_simply_remove_recursive(Storage* storage, const char* path);
/**
* Creates a directory
* @param storage
* @param path
* @return true on success or if directory is already exist
*/
bool storage_simply_mkdir(Storage* storage, const char* path);
/**
* @brief Get next free filename.
*
* @param storage
* @param dirname
* @param filename
* @param fileextension
* @param nextfilename return name
* @param max_len max len name
*/
void storage_get_next_filename(
Storage* storage,
const char* dirname,
const char* filename,
const char* fileextension,
string_t nextfilename,
uint8_t max_len);
#ifdef __cplusplus
}
#endif