sd-filesystem.h 3.8 KB

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