storage-external-api.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include "storage.h"
  2. #include "storage-i.h"
  3. #include "storage-message.h"
  4. #define S_API_PROLOGUE \
  5. osSemaphoreId_t semaphore = osSemaphoreNew(1, 0, NULL); \
  6. furi_check(semaphore != NULL);
  7. #define S_FILE_API_PROLOGUE \
  8. Storage* storage = file->storage; \
  9. furi_assert(storage);
  10. #define S_API_EPILOGUE \
  11. furi_check(osMessageQueuePut(storage->message_queue, &message, 0, osWaitForever) == osOK); \
  12. osSemaphoreAcquire(semaphore, osWaitForever); \
  13. osSemaphoreDelete(semaphore);
  14. #define S_API_MESSAGE(_command) \
  15. SAReturn return_data; \
  16. StorageMessage message = { \
  17. .semaphore = semaphore, \
  18. .command = _command, \
  19. .data = &data, \
  20. .return_data = &return_data, \
  21. };
  22. #define S_API_DATA_FILE \
  23. SAData data = { \
  24. .file = { \
  25. .file = file, \
  26. }};
  27. #define S_API_DATA_PATH \
  28. SAData data = { \
  29. .path = { \
  30. .path = path, \
  31. }};
  32. #define S_RETURN_BOOL (return_data.bool_value);
  33. #define S_RETURN_UINT16 (return_data.uint16_value);
  34. #define S_RETURN_UINT64 (return_data.uint64_value);
  35. #define S_RETURN_ERROR (return_data.error_value);
  36. #define S_RETURN_CSTRING (return_data.cstring_value);
  37. #define FILE_OPENED 1
  38. #define FILE_CLOSED 0
  39. /****************** FILE ******************/
  40. bool storage_file_open(
  41. File* file,
  42. const char* path,
  43. FS_AccessMode access_mode,
  44. FS_OpenMode open_mode) {
  45. S_FILE_API_PROLOGUE;
  46. S_API_PROLOGUE;
  47. SAData data = {
  48. .fopen = {
  49. .file = file,
  50. .path = path,
  51. .access_mode = access_mode,
  52. .open_mode = open_mode,
  53. }};
  54. file->file_id = FILE_OPENED;
  55. S_API_MESSAGE(StorageCommandFileOpen);
  56. S_API_EPILOGUE;
  57. return S_RETURN_BOOL;
  58. }
  59. bool storage_file_close(File* file) {
  60. S_FILE_API_PROLOGUE;
  61. S_API_PROLOGUE;
  62. S_API_DATA_FILE;
  63. S_API_MESSAGE(StorageCommandFileClose);
  64. S_API_EPILOGUE;
  65. file->file_id = FILE_CLOSED;
  66. return S_RETURN_BOOL;
  67. }
  68. uint16_t storage_file_read(File* file, void* buff, uint16_t bytes_to_read) {
  69. S_FILE_API_PROLOGUE;
  70. S_API_PROLOGUE;
  71. SAData data = {
  72. .fread = {
  73. .file = file,
  74. .buff = buff,
  75. .bytes_to_read = bytes_to_read,
  76. }};
  77. S_API_MESSAGE(StorageCommandFileRead);
  78. S_API_EPILOGUE;
  79. return S_RETURN_UINT16;
  80. }
  81. uint16_t storage_file_write(File* file, const void* buff, uint16_t bytes_to_write) {
  82. S_FILE_API_PROLOGUE;
  83. S_API_PROLOGUE;
  84. SAData data = {
  85. .fwrite = {
  86. .file = file,
  87. .buff = buff,
  88. .bytes_to_write = bytes_to_write,
  89. }};
  90. S_API_MESSAGE(StorageCommandFileWrite);
  91. S_API_EPILOGUE;
  92. return S_RETURN_UINT16;
  93. }
  94. bool storage_file_seek(File* file, uint32_t offset, bool from_start) {
  95. S_FILE_API_PROLOGUE;
  96. S_API_PROLOGUE;
  97. SAData data = {
  98. .fseek = {
  99. .file = file,
  100. .offset = offset,
  101. .from_start = from_start,
  102. }};
  103. S_API_MESSAGE(StorageCommandFileSeek);
  104. S_API_EPILOGUE;
  105. return S_RETURN_BOOL;
  106. }
  107. uint64_t storage_file_tell(File* file) {
  108. S_FILE_API_PROLOGUE;
  109. S_API_PROLOGUE;
  110. S_API_DATA_FILE;
  111. S_API_MESSAGE(StorageCommandFileTell);
  112. S_API_EPILOGUE;
  113. return S_RETURN_UINT64;
  114. }
  115. bool storage_file_truncate(File* file) {
  116. S_FILE_API_PROLOGUE;
  117. S_API_PROLOGUE;
  118. S_API_DATA_FILE;
  119. S_API_MESSAGE(StorageCommandFileTruncate);
  120. S_API_EPILOGUE;
  121. return S_RETURN_BOOL;
  122. }
  123. uint64_t storage_file_size(File* file) {
  124. S_FILE_API_PROLOGUE;
  125. S_API_PROLOGUE;
  126. S_API_DATA_FILE;
  127. S_API_MESSAGE(StorageCommandFileSize);
  128. S_API_EPILOGUE;
  129. return S_RETURN_UINT64;
  130. }
  131. bool storage_file_sync(File* file) {
  132. S_FILE_API_PROLOGUE;
  133. S_API_PROLOGUE;
  134. S_API_DATA_FILE;
  135. S_API_MESSAGE(StorageCommandFileSync);
  136. S_API_EPILOGUE;
  137. return S_RETURN_BOOL;
  138. }
  139. bool storage_file_eof(File* file) {
  140. S_FILE_API_PROLOGUE;
  141. S_API_PROLOGUE;
  142. S_API_DATA_FILE;
  143. S_API_MESSAGE(StorageCommandFileEof);
  144. S_API_EPILOGUE;
  145. return S_RETURN_BOOL;
  146. }
  147. /****************** DIR ******************/
  148. bool storage_dir_open(File* file, const char* path) {
  149. S_FILE_API_PROLOGUE;
  150. S_API_PROLOGUE;
  151. SAData data = {
  152. .dopen = {
  153. .file = file,
  154. .path = path,
  155. }};
  156. file->file_id = FILE_OPENED;
  157. S_API_MESSAGE(StorageCommandDirOpen);
  158. S_API_EPILOGUE;
  159. return S_RETURN_BOOL;
  160. }
  161. bool storage_dir_close(File* file) {
  162. S_FILE_API_PROLOGUE;
  163. S_API_PROLOGUE;
  164. S_API_DATA_FILE;
  165. S_API_MESSAGE(StorageCommandDirClose);
  166. S_API_EPILOGUE;
  167. file->file_id = FILE_CLOSED;
  168. return S_RETURN_BOOL;
  169. }
  170. bool storage_dir_read(File* file, FileInfo* fileinfo, char* name, uint16_t name_length) {
  171. S_FILE_API_PROLOGUE;
  172. S_API_PROLOGUE;
  173. SAData data = {
  174. .dread = {
  175. .file = file,
  176. .fileinfo = fileinfo,
  177. .name = name,
  178. .name_length = name_length,
  179. }};
  180. S_API_MESSAGE(StorageCommandDirRead);
  181. S_API_EPILOGUE;
  182. return S_RETURN_BOOL;
  183. }
  184. bool storage_dir_rewind(File* file) {
  185. S_FILE_API_PROLOGUE;
  186. S_API_PROLOGUE;
  187. S_API_DATA_FILE;
  188. S_API_MESSAGE(StorageCommandDirRewind);
  189. S_API_EPILOGUE;
  190. return S_RETURN_BOOL;
  191. }
  192. /****************** COMMON ******************/
  193. FS_Error storage_common_stat(Storage* storage, const char* path, FileInfo* fileinfo) {
  194. S_API_PROLOGUE;
  195. SAData data = {.cstat = {.path = path, .fileinfo = fileinfo}};
  196. S_API_MESSAGE(StorageCommandCommonStat);
  197. S_API_EPILOGUE;
  198. return S_RETURN_ERROR;
  199. }
  200. FS_Error storage_common_remove(Storage* storage, const char* path) {
  201. S_API_PROLOGUE;
  202. S_API_DATA_PATH;
  203. S_API_MESSAGE(StorageCommandCommonRemove);
  204. S_API_EPILOGUE;
  205. return S_RETURN_ERROR;
  206. }
  207. FS_Error storage_common_rename(Storage* storage, const char* old_path, const char* new_path) {
  208. S_API_PROLOGUE;
  209. SAData data = {
  210. .cpaths = {
  211. .old = old_path,
  212. .new = new_path,
  213. }};
  214. S_API_MESSAGE(StorageCommandCommonRename);
  215. S_API_EPILOGUE;
  216. return S_RETURN_ERROR;
  217. }
  218. FS_Error storage_common_copy(Storage* storage, const char* old_path, const char* new_path) {
  219. S_API_PROLOGUE;
  220. SAData data = {
  221. .cpaths = {
  222. .old = old_path,
  223. .new = new_path,
  224. }};
  225. S_API_MESSAGE(StorageCommandCommonCopy);
  226. S_API_EPILOGUE;
  227. return S_RETURN_ERROR;
  228. }
  229. FS_Error storage_common_mkdir(Storage* storage, const char* path) {
  230. S_API_PROLOGUE;
  231. S_API_DATA_PATH;
  232. S_API_MESSAGE(StorageCommandCommonMkDir);
  233. S_API_EPILOGUE;
  234. return S_RETURN_ERROR;
  235. }
  236. FS_Error storage_common_fs_info(
  237. Storage* storage,
  238. const char* fs_path,
  239. uint64_t* total_space,
  240. uint64_t* free_space) {
  241. S_API_PROLOGUE;
  242. SAData data = {
  243. .cfsinfo = {
  244. .fs_path = fs_path,
  245. .total_space = total_space,
  246. .free_space = free_space,
  247. }};
  248. S_API_MESSAGE(StorageCommandCommonFSInfo);
  249. S_API_EPILOGUE;
  250. return S_RETURN_ERROR;
  251. }
  252. /****************** ERROR ******************/
  253. const char* storage_error_get_desc(FS_Error error_id) {
  254. return filesystem_api_error_get_desc(error_id);
  255. }
  256. FS_Error storage_file_get_error(File* file) {
  257. furi_check(file != NULL);
  258. return file->error_id;
  259. }
  260. const char* storage_file_get_error_desc(File* file) {
  261. furi_check(file != NULL);
  262. return filesystem_api_error_get_desc(file->error_id);
  263. }
  264. /****************** Raw SD API ******************/
  265. FS_Error storage_sd_format(Storage* storage) {
  266. S_API_PROLOGUE;
  267. SAData data = {};
  268. S_API_MESSAGE(StorageCommandSDFormat);
  269. S_API_EPILOGUE;
  270. return S_RETURN_ERROR;
  271. }
  272. FS_Error storage_sd_unmount(Storage* storage) {
  273. S_API_PROLOGUE;
  274. SAData data = {};
  275. S_API_MESSAGE(StorageCommandSDUnmount);
  276. S_API_EPILOGUE;
  277. return S_RETURN_ERROR;
  278. }
  279. FS_Error storage_sd_info(Storage* storage, SDInfo* info) {
  280. S_API_PROLOGUE;
  281. SAData data = {
  282. .sdinfo = {
  283. .info = info,
  284. }};
  285. S_API_MESSAGE(StorageCommandSDInfo);
  286. S_API_EPILOGUE;
  287. return S_RETURN_ERROR;
  288. }
  289. FS_Error storage_sd_status(Storage* storage) {
  290. S_API_PROLOGUE;
  291. SAData data = {};
  292. S_API_MESSAGE(StorageCommandSDStatus);
  293. S_API_EPILOGUE;
  294. return S_RETURN_ERROR;
  295. }
  296. File* storage_file_alloc(Storage* storage) {
  297. File* file = furi_alloc(sizeof(File));
  298. file->file_id = FILE_CLOSED;
  299. file->storage = storage;
  300. return file;
  301. }
  302. bool storage_file_is_open(File* file) {
  303. return (file->file_id != FILE_CLOSED);
  304. }
  305. void storage_file_free(File* file) {
  306. if(storage_file_is_open(file)) {
  307. storage_file_close(file);
  308. }
  309. free(file);
  310. }