storage.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "flip_storage/storage.h"
  2. void save_settings(
  3. const char *ssid,
  4. const char *password)
  5. {
  6. // Create the directory for saving settings
  7. char directory_path[256];
  8. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  9. // Create the directory
  10. Storage *storage = furi_record_open(RECORD_STORAGE);
  11. storage_common_mkdir(storage, directory_path);
  12. // Open the settings file
  13. File *file = storage_file_alloc(storage);
  14. if (!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  15. {
  16. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  17. storage_file_free(file);
  18. furi_record_close(RECORD_STORAGE);
  19. return;
  20. }
  21. // Save the ssid length and data
  22. size_t ssid_length = strlen(ssid) + 1; // Include null terminator
  23. if (storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  24. storage_file_write(file, ssid, ssid_length) != ssid_length)
  25. {
  26. FURI_LOG_E(TAG, "Failed to write SSID");
  27. }
  28. // Save the password length and data
  29. size_t password_length = strlen(password) + 1; // Include null terminator
  30. if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  31. storage_file_write(file, password, password_length) != password_length)
  32. {
  33. FURI_LOG_E(TAG, "Failed to write password");
  34. }
  35. storage_file_close(file);
  36. storage_file_free(file);
  37. furi_record_close(RECORD_STORAGE);
  38. }
  39. bool load_settings(
  40. char *ssid,
  41. size_t ssid_size,
  42. char *password,
  43. size_t password_size)
  44. {
  45. Storage *storage = furi_record_open(RECORD_STORAGE);
  46. File *file = storage_file_alloc(storage);
  47. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  48. {
  49. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  50. storage_file_free(file);
  51. furi_record_close(RECORD_STORAGE);
  52. return false; // Return false if the file does not exist
  53. }
  54. // Load the ssid
  55. size_t ssid_length;
  56. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  57. storage_file_read(file, ssid, ssid_length) != ssid_length)
  58. {
  59. FURI_LOG_E(TAG, "Failed to read SSID");
  60. storage_file_close(file);
  61. storage_file_free(file);
  62. furi_record_close(RECORD_STORAGE);
  63. return false;
  64. }
  65. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  66. // Load the password
  67. size_t password_length;
  68. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  69. storage_file_read(file, password, password_length) != password_length)
  70. {
  71. FURI_LOG_E(TAG, "Failed to read password");
  72. storage_file_close(file);
  73. storage_file_free(file);
  74. furi_record_close(RECORD_STORAGE);
  75. return false;
  76. }
  77. password[password_length - 1] = '\0'; // Ensure null-termination
  78. storage_file_close(file);
  79. storage_file_free(file);
  80. furi_record_close(RECORD_STORAGE);
  81. return true;
  82. }
  83. bool save_char(
  84. const char *path_name, const char *value)
  85. {
  86. if (!value)
  87. {
  88. return false;
  89. }
  90. // Create the directory for saving settings
  91. char directory_path[256];
  92. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  93. // Create the directory
  94. Storage *storage = furi_record_open(RECORD_STORAGE);
  95. storage_common_mkdir(storage, directory_path);
  96. // Open the settings file
  97. File *file = storage_file_alloc(storage);
  98. char file_path[256];
  99. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/%s.txt", path_name);
  100. // Open the file in write mode
  101. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  102. {
  103. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  104. storage_file_free(file);
  105. furi_record_close(RECORD_STORAGE);
  106. return false;
  107. }
  108. // Write the data to the file
  109. size_t data_size = strlen(value) + 1; // Include null terminator
  110. if (storage_file_write(file, value, data_size) != data_size)
  111. {
  112. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  113. storage_file_close(file);
  114. storage_file_free(file);
  115. furi_record_close(RECORD_STORAGE);
  116. return false;
  117. }
  118. storage_file_close(file);
  119. storage_file_free(file);
  120. furi_record_close(RECORD_STORAGE);
  121. return true;
  122. }
  123. bool load_char(
  124. const char *path_name,
  125. char *value,
  126. size_t value_size)
  127. {
  128. if (!value)
  129. {
  130. return false;
  131. }
  132. Storage *storage = furi_record_open(RECORD_STORAGE);
  133. File *file = storage_file_alloc(storage);
  134. char file_path[256];
  135. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/%s.txt", path_name);
  136. // Open the file for reading
  137. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  138. {
  139. storage_file_free(file);
  140. furi_record_close(RECORD_STORAGE);
  141. return NULL; // Return false if the file does not exist
  142. }
  143. // Read data into the buffer
  144. size_t read_count = storage_file_read(file, value, value_size);
  145. if (storage_file_get_error(file) != FSE_OK)
  146. {
  147. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  148. storage_file_close(file);
  149. storage_file_free(file);
  150. furi_record_close(RECORD_STORAGE);
  151. return false;
  152. }
  153. // Ensure null-termination
  154. value[read_count - 1] = '\0';
  155. storage_file_close(file);
  156. storage_file_free(file);
  157. furi_record_close(RECORD_STORAGE);
  158. return true;
  159. }