storage_glue.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. return (osMutexAcquire(storage->mutex, osWaitForever) == osOK);
  35. }
  36. bool storage_data_unlock(StorageData* storage) {
  37. return (osMutexRelease(storage->mutex) == osOK);
  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. /****************** storage glue ******************/
  71. bool storage_has_file(const File* file, StorageData* storage_data) {
  72. bool result = false;
  73. StorageFileList_it_t it;
  74. for(StorageFileList_it(it, storage_data->files); !StorageFileList_end_p(it);
  75. StorageFileList_next(it)) {
  76. const StorageFile* storage_file = StorageFileList_cref(it);
  77. if(storage_file->file->file_id == file->file_id) {
  78. result = true;
  79. break;
  80. }
  81. }
  82. return result;
  83. }
  84. bool storage_path_already_open(string_t path, StorageFileList_t array) {
  85. bool open = false;
  86. StorageFileList_it_t it;
  87. for(StorageFileList_it(it, array); !StorageFileList_end_p(it); StorageFileList_next(it)) {
  88. const StorageFile* storage_file = StorageFileList_cref(it);
  89. if(string_cmp(storage_file->path, path) == 0) {
  90. open = true;
  91. break;
  92. }
  93. }
  94. return open;
  95. }
  96. void storage_set_storage_file_data(const File* file, void* file_data, StorageData* storage) {
  97. StorageFile* founded_file = NULL;
  98. StorageFileList_it_t it;
  99. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  100. StorageFileList_next(it)) {
  101. StorageFile* storage_file = StorageFileList_ref(it);
  102. if(storage_file->file->file_id == file->file_id) {
  103. founded_file = storage_file;
  104. break;
  105. }
  106. }
  107. furi_check(founded_file != NULL);
  108. founded_file->file_data = file_data;
  109. }
  110. void* storage_get_storage_file_data(const File* file, StorageData* storage) {
  111. const StorageFile* founded_file = NULL;
  112. StorageFileList_it_t it;
  113. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  114. StorageFileList_next(it)) {
  115. const StorageFile* storage_file = StorageFileList_cref(it);
  116. if(storage_file->file->file_id == file->file_id) {
  117. founded_file = storage_file;
  118. break;
  119. }
  120. }
  121. furi_check(founded_file != NULL);
  122. return founded_file->file_data;
  123. }
  124. void storage_push_storage_file(File* file, string_t path, StorageType type, StorageData* storage) {
  125. StorageFile* storage_file = StorageFileList_push_new(storage->files);
  126. furi_check(storage_file != NULL);
  127. file->file_id = (uint32_t)storage_file;
  128. storage_file->file = file;
  129. storage_file->type = type;
  130. string_set(storage_file->path, path);
  131. }
  132. bool storage_pop_storage_file(File* file, StorageData* storage) {
  133. StorageFileList_it_t it;
  134. bool result = false;
  135. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  136. StorageFileList_next(it)) {
  137. if(StorageFileList_cref(it)->file->file_id == file->file_id) {
  138. result = true;
  139. break;
  140. }
  141. }
  142. if(result) {
  143. StorageFileList_remove(storage->files, it);
  144. }
  145. return result;
  146. }