sd-filesystem.h 3.3 KB

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