storage_glue.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. bool storage_path_already_open(string_t path, StorageFileList_t array) {
  87. bool open = false;
  88. StorageFileList_it_t it;
  89. for(StorageFileList_it(it, array); !StorageFileList_end_p(it); StorageFileList_next(it)) {
  90. const StorageFile* storage_file = StorageFileList_cref(it);
  91. if(string_cmp(storage_file->path, path) == 0) {
  92. open = true;
  93. break;
  94. }
  95. }
  96. return open;
  97. }
  98. void storage_set_storage_file_data(const File* file, void* file_data, StorageData* storage) {
  99. StorageFile* founded_file = NULL;
  100. StorageFileList_it_t it;
  101. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  102. StorageFileList_next(it)) {
  103. StorageFile* storage_file = StorageFileList_ref(it);
  104. if(storage_file->file->file_id == file->file_id) {
  105. founded_file = storage_file;
  106. break;
  107. }
  108. }
  109. furi_check(founded_file != NULL);
  110. founded_file->file_data = file_data;
  111. }
  112. void* storage_get_storage_file_data(const File* file, StorageData* storage) {
  113. const 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. const StorageFile* storage_file = StorageFileList_cref(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. return founded_file->file_data;
  125. }
  126. void storage_push_storage_file(File* file, string_t path, StorageType type, StorageData* storage) {
  127. StorageFile* storage_file = StorageFileList_push_new(storage->files);
  128. furi_check(storage_file != NULL);
  129. file->file_id = (uint32_t)storage_file;
  130. storage_file->file = file;
  131. storage_file->type = type;
  132. string_set(storage_file->path, path);
  133. }
  134. bool storage_pop_storage_file(File* file, StorageData* storage) {
  135. StorageFileList_it_t it;
  136. bool result = false;
  137. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  138. StorageFileList_next(it)) {
  139. if(StorageFileList_cref(it)->file->file_id == file->file_id) {
  140. result = true;
  141. break;
  142. }
  143. }
  144. if(result) {
  145. StorageFileList_remove(storage->files, it);
  146. }
  147. return result;
  148. }