sd-filesystem.h 3.3 KB

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