storage.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. }
  160. bool save_world(
  161. const char *name,
  162. const char *world_data)
  163. {
  164. // Create the directory for saving settings
  165. char directory_path[256];
  166. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  167. // Create the directory
  168. Storage *storage = furi_record_open(RECORD_STORAGE);
  169. storage_common_mkdir(storage, directory_path);
  170. // Open the settings file
  171. File *file = storage_file_alloc(storage);
  172. char file_path[256];
  173. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  174. // Open the file in write mode
  175. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  176. {
  177. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  178. storage_file_free(file);
  179. furi_record_close(RECORD_STORAGE);
  180. return false;
  181. }
  182. // Write the data to the file
  183. size_t data_size = strlen(world_data) + 1; // Include null terminator
  184. if (storage_file_write(file, world_data, data_size) != data_size)
  185. {
  186. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  187. storage_file_close(file);
  188. storage_file_free(file);
  189. furi_record_close(RECORD_STORAGE);
  190. return false;
  191. }
  192. storage_file_close(file);
  193. storage_file_free(file);
  194. furi_record_close(RECORD_STORAGE);
  195. return true;
  196. }
  197. bool load_world(
  198. const char *name,
  199. char *json_data,
  200. size_t json_data_size)
  201. {
  202. Storage *storage = furi_record_open(RECORD_STORAGE);
  203. File *file = storage_file_alloc(storage);
  204. char file_path[256];
  205. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  206. // Open the file for reading
  207. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  208. {
  209. storage_file_free(file);
  210. furi_record_close(RECORD_STORAGE);
  211. return NULL; // Return false if the file does not exist
  212. }
  213. // Read data into the buffer
  214. size_t read_count = storage_file_read(file, json_data, json_data_size);
  215. if (storage_file_get_error(file) != FSE_OK)
  216. {
  217. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  218. storage_file_close(file);
  219. storage_file_free(file);
  220. furi_record_close(RECORD_STORAGE);
  221. return false;
  222. }
  223. // Ensure null-termination
  224. json_data[read_count - 1] = '\0';
  225. storage_file_close(file);
  226. storage_file_free(file);
  227. furi_record_close(RECORD_STORAGE);
  228. return true;
  229. }
  230. FuriString *load_furi_world(
  231. const char *name)
  232. {
  233. Storage *storage = furi_record_open(RECORD_STORAGE);
  234. File *file = storage_file_alloc(storage);
  235. char file_path[256];
  236. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  237. // Open the file for reading
  238. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  239. {
  240. storage_file_free(file);
  241. furi_record_close(RECORD_STORAGE);
  242. return NULL; // Return false if the file does not exist
  243. }
  244. // Allocate a FuriString to hold the received data
  245. FuriString *str_result = furi_string_alloc();
  246. if (!str_result)
  247. {
  248. FURI_LOG_E(HTTP_TAG, "Failed to allocate FuriString");
  249. storage_file_close(file);
  250. storage_file_free(file);
  251. furi_record_close(RECORD_STORAGE);
  252. return NULL;
  253. }
  254. // Reset the FuriString to ensure it's empty before reading
  255. furi_string_reset(str_result);
  256. // Define a buffer to hold the read data
  257. uint8_t *buffer = (uint8_t *)malloc(MAX_FILE_SHOW);
  258. if (!buffer)
  259. {
  260. FURI_LOG_E(HTTP_TAG, "Failed to allocate buffer");
  261. furi_string_free(str_result);
  262. storage_file_close(file);
  263. storage_file_free(file);
  264. furi_record_close(RECORD_STORAGE);
  265. return NULL;
  266. }
  267. // Read data into the buffer
  268. size_t read_count = storage_file_read(file, buffer, MAX_FILE_SHOW);
  269. if (storage_file_get_error(file) != FSE_OK)
  270. {
  271. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  272. furi_string_free(str_result);
  273. storage_file_close(file);
  274. storage_file_free(file);
  275. furi_record_close(RECORD_STORAGE);
  276. return NULL;
  277. }
  278. // Append each byte to the FuriString
  279. for (size_t i = 0; i < read_count; i++)
  280. {
  281. furi_string_push_back(str_result, buffer[i]);
  282. }
  283. // Check if there is more data beyond the maximum size
  284. char extra_byte;
  285. storage_file_read(file, &extra_byte, 1);
  286. // Clean up
  287. storage_file_close(file);
  288. storage_file_free(file);
  289. furi_record_close(RECORD_STORAGE);
  290. free(buffer);
  291. return str_result;
  292. }
  293. bool world_exists(const char *name)
  294. {
  295. if (!name)
  296. {
  297. FURI_LOG_E(TAG, "Invalid name");
  298. return false;
  299. }
  300. Storage *storage = furi_record_open(RECORD_STORAGE);
  301. if (!storage)
  302. {
  303. FURI_LOG_E(TAG, "Failed to open storage");
  304. return false;
  305. }
  306. char file_path[256];
  307. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  308. bool does_exist = storage_file_exists(storage, file_path);
  309. // Clean up
  310. furi_record_close(RECORD_STORAGE);
  311. return does_exist;
  312. }