storage.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 save_world_furi(FuriString *name, FuriString *world_data)
  198. {
  199. // Create the directory for saving settings
  200. char directory_path[256];
  201. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  202. // Create the directory
  203. Storage *storage = furi_record_open(RECORD_STORAGE);
  204. storage_common_mkdir(storage, directory_path);
  205. // Open the settings file
  206. File *file = storage_file_alloc(storage);
  207. char file_path[256];
  208. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(name));
  209. // Open the file in write mode
  210. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  211. {
  212. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  213. storage_file_free(file);
  214. furi_record_close(RECORD_STORAGE);
  215. return false;
  216. }
  217. // Write the data to the file
  218. size_t data_size = furi_string_size(world_data) + 1; // Include null terminator
  219. if (storage_file_write(file, furi_string_get_cstr(world_data), data_size) != data_size)
  220. {
  221. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  222. storage_file_close(file);
  223. storage_file_free(file);
  224. furi_record_close(RECORD_STORAGE);
  225. return false;
  226. }
  227. storage_file_close(file);
  228. storage_file_free(file);
  229. furi_record_close(RECORD_STORAGE);
  230. return true;
  231. }
  232. bool load_world(
  233. const char *name,
  234. char *json_data,
  235. size_t json_data_size)
  236. {
  237. Storage *storage = furi_record_open(RECORD_STORAGE);
  238. File *file = storage_file_alloc(storage);
  239. char file_path[256];
  240. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  241. // Open the file for reading
  242. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  243. {
  244. storage_file_free(file);
  245. furi_record_close(RECORD_STORAGE);
  246. return NULL; // Return false if the file does not exist
  247. }
  248. // Read data into the buffer
  249. size_t read_count = storage_file_read(file, json_data, json_data_size);
  250. if (storage_file_get_error(file) != FSE_OK)
  251. {
  252. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  253. storage_file_close(file);
  254. storage_file_free(file);
  255. furi_record_close(RECORD_STORAGE);
  256. return false;
  257. }
  258. // Ensure null-termination
  259. json_data[read_count - 1] = '\0';
  260. storage_file_close(file);
  261. storage_file_free(file);
  262. furi_record_close(RECORD_STORAGE);
  263. return true;
  264. }
  265. FuriString *load_furi_world(
  266. const char *name)
  267. {
  268. Storage *storage = furi_record_open(RECORD_STORAGE);
  269. File *file = storage_file_alloc(storage);
  270. char file_path[128];
  271. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  272. // Open the file for reading
  273. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  274. {
  275. storage_file_free(file);
  276. furi_record_close(RECORD_STORAGE);
  277. return NULL; // Return false if the file does not exist
  278. }
  279. // Allocate a FuriString to hold the received data
  280. FuriString *str_result = furi_string_alloc();
  281. if (!str_result)
  282. {
  283. FURI_LOG_E(HTTP_TAG, "Failed to allocate FuriString");
  284. storage_file_close(file);
  285. storage_file_free(file);
  286. furi_record_close(RECORD_STORAGE);
  287. return NULL;
  288. }
  289. // Reset the FuriString to ensure it's empty before reading
  290. furi_string_reset(str_result);
  291. // Define a buffer to hold the read data
  292. uint8_t *buffer = (uint8_t *)malloc(MAX_FILE_SHOW);
  293. if (!buffer)
  294. {
  295. FURI_LOG_E(HTTP_TAG, "Failed to allocate buffer");
  296. furi_string_free(str_result);
  297. storage_file_close(file);
  298. storage_file_free(file);
  299. furi_record_close(RECORD_STORAGE);
  300. return NULL;
  301. }
  302. // Read data into the buffer
  303. size_t read_count = storage_file_read(file, buffer, MAX_FILE_SHOW);
  304. if (storage_file_get_error(file) != FSE_OK)
  305. {
  306. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  307. furi_string_free(str_result);
  308. storage_file_close(file);
  309. storage_file_free(file);
  310. furi_record_close(RECORD_STORAGE);
  311. return NULL;
  312. }
  313. // Append each byte to the FuriString
  314. for (size_t i = 0; i < read_count; i++)
  315. {
  316. furi_string_push_back(str_result, buffer[i]);
  317. }
  318. // Check if there is more data beyond the maximum size
  319. char extra_byte;
  320. storage_file_read(file, &extra_byte, 1);
  321. // Clean up
  322. storage_file_close(file);
  323. storage_file_free(file);
  324. furi_record_close(RECORD_STORAGE);
  325. free(buffer);
  326. return str_result;
  327. }
  328. bool world_exists(const char *name)
  329. {
  330. if (!name)
  331. {
  332. FURI_LOG_E(TAG, "Invalid name");
  333. return false;
  334. }
  335. Storage *storage = furi_record_open(RECORD_STORAGE);
  336. if (!storage)
  337. {
  338. FURI_LOG_E(TAG, "Failed to open storage");
  339. return false;
  340. }
  341. char file_path[256];
  342. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  343. bool does_exist = storage_file_exists(storage, file_path);
  344. // Clean up
  345. furi_record_close(RECORD_STORAGE);
  346. return does_exist;
  347. }
  348. bool save_world_names(const FuriString *json)
  349. {
  350. // Create the directory for saving settings
  351. char directory_path[256];
  352. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  353. // Create the directory
  354. Storage *storage = furi_record_open(RECORD_STORAGE);
  355. storage_common_mkdir(storage, directory_path);
  356. // Open the settings file
  357. File *file = storage_file_alloc(storage);
  358. char file_path[128];
  359. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  360. // Open the file in write mode
  361. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  362. {
  363. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  364. storage_file_free(file);
  365. furi_record_close(RECORD_STORAGE);
  366. return false;
  367. }
  368. // Write the data to the file
  369. size_t data_size = furi_string_size(json) + 1; // Include null terminator
  370. if (storage_file_write(file, furi_string_get_cstr(json), data_size) != data_size)
  371. {
  372. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  373. storage_file_close(file);
  374. storage_file_free(file);
  375. furi_record_close(RECORD_STORAGE);
  376. return false;
  377. }
  378. storage_file_close(file);
  379. storage_file_free(file);
  380. furi_record_close(RECORD_STORAGE);
  381. return true;
  382. }