storage_glue.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. obj->path = furi_string_alloc();
  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. obj->path = furi_string_alloc_set(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. furi_string_set(obj->path, src->path);
  21. }
  22. void storage_file_clear(StorageFile* obj) {
  23. furi_string_free(obj->path);
  24. }
  25. /****************** storage data ******************/
  26. void storage_data_init(StorageData* storage) {
  27. storage->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  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. return (furi_mutex_acquire(storage->mutex, FuriWaitForever) == FuriStatusOk);
  35. }
  36. bool storage_data_unlock(StorageData* storage) {
  37. return (furi_mutex_release(storage->mutex) == FuriStatusOk);
  38. }
  39. StorageStatus storage_data_status(StorageData* storage) {
  40. StorageStatus status;
  41. storage_data_lock(storage);
  42. status = storage->status;
  43. storage_data_unlock(storage);
  44. return status;
  45. }
  46. const char* storage_data_status_text(StorageData* storage) {
  47. const char* result = "unknown";
  48. switch(storage->status) {
  49. case StorageStatusOK:
  50. result = "ok";
  51. break;
  52. case StorageStatusNotReady:
  53. result = "not ready";
  54. break;
  55. case StorageStatusNotMounted:
  56. result = "not mounted";
  57. break;
  58. case StorageStatusNoFS:
  59. result = "no filesystem";
  60. break;
  61. case StorageStatusNotAccessible:
  62. result = "not accessible";
  63. break;
  64. case StorageStatusErrorInternal:
  65. result = "internal";
  66. break;
  67. }
  68. return result;
  69. }
  70. void storage_data_timestamp(StorageData* storage) {
  71. storage->timestamp = furi_hal_rtc_get_timestamp();
  72. }
  73. uint32_t storage_data_get_timestamp(StorageData* storage) {
  74. return storage->timestamp;
  75. }
  76. /****************** storage glue ******************/
  77. bool storage_has_file(const File* file, StorageData* storage_data) {
  78. bool result = false;
  79. StorageFileList_it_t it;
  80. for(StorageFileList_it(it, storage_data->files); !StorageFileList_end_p(it);
  81. StorageFileList_next(it)) {
  82. const StorageFile* storage_file = StorageFileList_cref(it);
  83. if(storage_file->file->file_id == file->file_id) {
  84. result = true;
  85. break;
  86. }
  87. }
  88. return result;
  89. }
  90. bool storage_path_already_open(FuriString* path, StorageFileList_t array) {
  91. bool open = false;
  92. StorageFileList_it_t it;
  93. for(StorageFileList_it(it, array); !StorageFileList_end_p(it); StorageFileList_next(it)) {
  94. const StorageFile* storage_file = StorageFileList_cref(it);
  95. if(furi_string_cmp(storage_file->path, path) == 0) {
  96. open = true;
  97. break;
  98. }
  99. }
  100. return open;
  101. }
  102. void storage_set_storage_file_data(const File* file, void* file_data, StorageData* storage) {
  103. StorageFile* founded_file = NULL;
  104. StorageFileList_it_t it;
  105. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  106. StorageFileList_next(it)) {
  107. StorageFile* storage_file = StorageFileList_ref(it);
  108. if(storage_file->file->file_id == file->file_id) {
  109. founded_file = storage_file;
  110. break;
  111. }
  112. }
  113. furi_check(founded_file != NULL);
  114. founded_file->file_data = file_data;
  115. }
  116. void* storage_get_storage_file_data(const File* file, StorageData* storage) {
  117. const StorageFile* founded_file = NULL;
  118. StorageFileList_it_t it;
  119. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  120. StorageFileList_next(it)) {
  121. const StorageFile* storage_file = StorageFileList_cref(it);
  122. if(storage_file->file->file_id == file->file_id) {
  123. founded_file = storage_file;
  124. break;
  125. }
  126. }
  127. furi_check(founded_file != NULL);
  128. return founded_file->file_data;
  129. }
  130. void storage_push_storage_file(
  131. File* file,
  132. FuriString* path,
  133. StorageType type,
  134. StorageData* storage) {
  135. StorageFile* storage_file = StorageFileList_push_new(storage->files);
  136. furi_check(storage_file != NULL);
  137. file->file_id = (uint32_t)storage_file;
  138. storage_file->file = file;
  139. storage_file->type = type;
  140. furi_string_set(storage_file->path, path);
  141. }
  142. bool storage_pop_storage_file(File* file, StorageData* storage) {
  143. StorageFileList_it_t it;
  144. bool result = false;
  145. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  146. StorageFileList_next(it)) {
  147. if(StorageFileList_cref(it)->file->file_id == file->file_id) {
  148. result = true;
  149. break;
  150. }
  151. }
  152. if(result) {
  153. StorageFileList_remove(storage->files, it);
  154. }
  155. return result;
  156. }