storage_glue.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "storage_glue.h"
  2. #include <furi_hal.h>
  3. /****************** storage file ******************/
  4. void storage_file_init(StorageFile* obj) {
  5. obj->file = NULL;
  6. obj->type = ST_ERROR;
  7. obj->file_data = NULL;
  8. string_init(obj->path);
  9. }
  10. void storage_file_init_set(StorageFile* obj, const StorageFile* src) {
  11. obj->file = src->file;
  12. obj->type = src->type;
  13. obj->file_data = src->file_data;
  14. string_init_set(obj->path, src->path);
  15. }
  16. void storage_file_set(StorageFile* obj, const StorageFile* src) {
  17. obj->file = src->file;
  18. obj->type = src->type;
  19. obj->file_data = src->file_data;
  20. string_set(obj->path, src->path);
  21. }
  22. void storage_file_clear(StorageFile* obj) {
  23. string_clear(obj->path);
  24. }
  25. /****************** storage data ******************/
  26. void storage_data_init(StorageData* storage) {
  27. storage->mutex = osMutexNew(NULL);
  28. furi_check(storage->mutex != NULL);
  29. storage->data = NULL;
  30. storage->status = StorageStatusNotReady;
  31. StorageFileList_init(storage->files);
  32. }
  33. bool storage_data_lock(StorageData* storage) {
  34. furi_hal_power_insomnia_enter();
  35. return (osMutexAcquire(storage->mutex, osWaitForever) == osOK);
  36. }
  37. bool storage_data_unlock(StorageData* storage) {
  38. furi_hal_power_insomnia_exit();
  39. return (osMutexRelease(storage->mutex) == osOK);
  40. }
  41. StorageStatus storage_data_status(StorageData* storage) {
  42. StorageStatus status;
  43. storage_data_lock(storage);
  44. status = storage->status;
  45. storage_data_unlock(storage);
  46. return status;
  47. }
  48. const char* storage_data_status_text(StorageData* storage) {
  49. const char* result = "unknown";
  50. switch(storage->status) {
  51. case StorageStatusOK:
  52. result = "ok";
  53. break;
  54. case StorageStatusNotReady:
  55. result = "not ready";
  56. break;
  57. case StorageStatusNotMounted:
  58. result = "not mounted";
  59. break;
  60. case StorageStatusNoFS:
  61. result = "no filesystem";
  62. break;
  63. case StorageStatusNotAccessible:
  64. result = "not accessible";
  65. break;
  66. case StorageStatusErrorInternal:
  67. result = "internal";
  68. break;
  69. }
  70. return result;
  71. }
  72. /****************** storage glue ******************/
  73. bool storage_has_file(const File* file, StorageData* storage_data) {
  74. bool result = false;
  75. StorageFileList_it_t it;
  76. for(StorageFileList_it(it, storage_data->files); !StorageFileList_end_p(it);
  77. StorageFileList_next(it)) {
  78. const StorageFile* storage_file = StorageFileList_cref(it);
  79. if(storage_file->file->file_id == file->file_id) {
  80. result = true;
  81. break;
  82. }
  83. }
  84. return result;
  85. }
  86. StorageType storage_get_type_by_path(const char* path) {
  87. StorageType type = ST_ERROR;
  88. const char* ext_path = "/ext";
  89. const char* int_path = "/int";
  90. const char* any_path = "/any";
  91. if(strlen(path) >= strlen(ext_path) && memcmp(path, ext_path, strlen(ext_path)) == 0) {
  92. type = ST_EXT;
  93. } else if(strlen(path) >= strlen(int_path) && memcmp(path, int_path, strlen(int_path)) == 0) {
  94. type = ST_INT;
  95. } else if(strlen(path) >= strlen(any_path) && memcmp(path, any_path, strlen(any_path)) == 0) {
  96. type = ST_ANY;
  97. }
  98. return type;
  99. }
  100. bool storage_path_already_open(const char* path, StorageFileList_t array) {
  101. bool open = false;
  102. StorageFileList_it_t it;
  103. for(StorageFileList_it(it, array); !StorageFileList_end_p(it); StorageFileList_next(it)) {
  104. const StorageFile* storage_file = StorageFileList_cref(it);
  105. if(string_cmp(storage_file->path, path) == 0) {
  106. open = true;
  107. break;
  108. }
  109. }
  110. return open;
  111. }
  112. void storage_set_storage_file_data(const File* file, void* file_data, StorageData* storage) {
  113. StorageFile* founded_file = NULL;
  114. StorageFileList_it_t it;
  115. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  116. StorageFileList_next(it)) {
  117. StorageFile* storage_file = StorageFileList_ref(it);
  118. if(storage_file->file->file_id == file->file_id) {
  119. founded_file = storage_file;
  120. break;
  121. }
  122. }
  123. furi_check(founded_file != NULL);
  124. founded_file->file_data = file_data;
  125. }
  126. void* storage_get_storage_file_data(const File* file, StorageData* storage) {
  127. const StorageFile* founded_file = NULL;
  128. StorageFileList_it_t it;
  129. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  130. StorageFileList_next(it)) {
  131. const StorageFile* storage_file = StorageFileList_cref(it);
  132. if(storage_file->file->file_id == file->file_id) {
  133. founded_file = storage_file;
  134. break;
  135. }
  136. }
  137. furi_check(founded_file != NULL);
  138. return founded_file->file_data;
  139. }
  140. void storage_push_storage_file(
  141. File* file,
  142. const char* path,
  143. StorageType type,
  144. StorageData* storage) {
  145. StorageFile* storage_file = StorageFileList_push_new(storage->files);
  146. furi_check(storage_file != NULL);
  147. file->file_id = (uint32_t)storage_file;
  148. storage_file->file = file;
  149. storage_file->type = type;
  150. string_set(storage_file->path, path);
  151. }
  152. bool storage_pop_storage_file(File* file, StorageData* storage) {
  153. StorageFileList_it_t it;
  154. bool result = false;
  155. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  156. StorageFileList_next(it)) {
  157. if(StorageFileList_cref(it)->file->file_id == file->file_id) {
  158. result = true;
  159. break;
  160. }
  161. }
  162. if(result) {
  163. StorageFileList_remove(storage->files, it);
  164. }
  165. return result;
  166. }