sd-filesystem.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include "flipper.h"
  3. #include "flipper_v2.h"
  4. #define SD_FS_MAX_FILES _FS_LOCK
  5. #define SD_STATE_LINES_COUNT 6
  6. #ifndef min
  7. #define min(a, b) (((a) < (b)) ? (a) : (b))
  8. #endif
  9. /* api data */
  10. typedef FIL SDFile;
  11. typedef DIR SDDir;
  12. typedef FILINFO SDFileInfo;
  13. /* storage for file/directory objects*/
  14. typedef union {
  15. SDFile file;
  16. SDDir dir;
  17. } SDFileDirStorage;
  18. typedef enum {
  19. SD_OK = FR_OK,
  20. SD_DISK_ERR = FR_DISK_ERR,
  21. SD_INT_ERR = FR_INT_ERR,
  22. SD_NO_FILE = FR_NO_FILE,
  23. SD_NO_PATH = FR_NO_PATH,
  24. SD_INVALID_NAME = FR_INVALID_NAME,
  25. SD_DENIED = FR_DENIED,
  26. SD_EXIST = FR_EXIST,
  27. SD_INVALID_OBJECT = FR_INVALID_OBJECT,
  28. SD_WRITE_PROTECTED = FR_WRITE_PROTECTED,
  29. SD_INVALID_DRIVE = FR_INVALID_DRIVE,
  30. SD_NOT_ENABLED = FR_NOT_ENABLED,
  31. SD_NO_FILESYSTEM = FR_NO_FILESYSTEM,
  32. SD_MKFS_ABORTED = FR_MKFS_ABORTED,
  33. SD_TIMEOUT = FR_TIMEOUT,
  34. SD_LOCKED = FR_LOCKED,
  35. SD_NOT_ENOUGH_CORE = FR_NOT_ENOUGH_CORE,
  36. SD_TOO_MANY_OPEN_FILES = FR_TOO_MANY_OPEN_FILES,
  37. SD_INVALID_PARAMETER = FR_INVALID_PARAMETER,
  38. SD_NO_CARD,
  39. SD_NOT_A_FILE,
  40. SD_NOT_A_DIR,
  41. SD_OTHER_APP,
  42. SD_LOW_LEVEL_ERR,
  43. } SDError;
  44. typedef enum {
  45. FDF_DIR,
  46. FDF_FILE,
  47. FDF_ANY,
  48. } FiledataFilter;
  49. typedef struct {
  50. osThreadId_t thread_id;
  51. bool is_dir;
  52. SDFileDirStorage data;
  53. } FileData;
  54. /* application data */
  55. typedef struct {
  56. Widget* widget;
  57. Icon* mounted;
  58. Icon* fail;
  59. } SdFsIcon;
  60. typedef struct {
  61. osMutexId_t mutex;
  62. FileData files[SD_FS_MAX_FILES];
  63. SDError status;
  64. char* path;
  65. FATFS fat_fs;
  66. } SdFsInfo;
  67. typedef struct {
  68. SdFsInfo info;
  69. SdFsIcon icon;
  70. Widget* widget;
  71. const char* line[SD_STATE_LINES_COUNT];
  72. osMessageQueueId_t event_queue;
  73. } SdApp;
  74. /* core api fns */
  75. bool _fs_init(SdFsInfo* _fs_info);
  76. bool _fs_lock(SdFsInfo* fs_info);
  77. bool _fs_unlock(SdFsInfo* fs_info);
  78. SDError _fs_status(SdFsInfo* fs_info);
  79. /* sd api fns */
  80. bool fs_file_open(File* file, const char* path, FS_AccessMode access_mode, FS_OpenMode open_mode);
  81. bool fs_file_close(File* file);
  82. uint16_t fs_file_read(File* file, void* buff, uint16_t bytes_to_read);
  83. uint16_t fs_file_write(File* file, void* buff, uint16_t bytes_to_write);
  84. bool fs_file_seek(File* file, uint32_t offset, bool from_start);
  85. uint64_t fs_file_tell(File* file);
  86. bool fs_file_truncate(File* file);
  87. uint64_t fs_file_size(File* file);
  88. bool fs_file_sync(File* file);
  89. bool fs_file_eof(File* file);
  90. /* dir api */
  91. bool fs_dir_open(File* file, const char* path);
  92. bool fs_dir_close(File* file);
  93. bool fs_dir_read(File* file, FileInfo* fileinfo, char* name, uint16_t name_length);
  94. bool fs_dir_rewind(File* file);
  95. /* common api */
  96. FS_Error
  97. fs_common_info(const char* path, FileInfo* fileinfo, char* name, const uint16_t name_length);
  98. FS_Error fs_common_remove(const char* path);
  99. FS_Error fs_common_rename(const char* old_path, const char* new_path);
  100. FS_Error fs_common_set_attr(const char* path, uint8_t attr, uint8_t mask);
  101. FS_Error fs_common_mkdir(const char* path);
  102. FS_Error fs_common_set_time(const char* path, FileDateUnion date, FileTimeUnion time);
  103. FS_Error fs_get_fs_info(uint64_t* total_space, uint64_t* free_space);
  104. /* errors api */
  105. const char* fs_error_get_desc(FS_Error error_id);
  106. const char* fs_error_get_internal_desc(uint32_t internal_error_id);