flip_store_storage.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #include "flip_storage/flip_store_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[128];
  8. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store");
  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. // future implenetation because we need the app category
  84. bool delete_app(const char *app_id, const char *app_category)
  85. {
  86. // Create the directory for saving settings
  87. char directory_path[128];
  88. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps/%s/%s", app_category, app_id);
  89. // Create the directory
  90. Storage *storage = furi_record_open(RECORD_STORAGE);
  91. if (!storage_simply_remove_recursive(storage, directory_path))
  92. {
  93. FURI_LOG_E(TAG, "Failed to delete app: %s", app_id);
  94. furi_record_close(RECORD_STORAGE);
  95. return false;
  96. }
  97. furi_record_close(RECORD_STORAGE);
  98. return true;
  99. }
  100. bool app_exists(const char *app_id, const char *app_category)
  101. {
  102. // Check if the app exists
  103. char directory_path[128];
  104. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps/%s/%s.fap", app_category, app_id);
  105. Storage *storage = furi_record_open(RECORD_STORAGE);
  106. bool exists = storage_common_exists(storage, directory_path);
  107. furi_record_close(RECORD_STORAGE);
  108. return exists;
  109. }
  110. // Function to parse JSON incrementally from a file
  111. bool parse_json_incrementally(const char *file_path, const char *target_key, char *value_buffer, size_t value_buffer_size)
  112. {
  113. Storage *_storage = NULL;
  114. File *_file = NULL;
  115. char buffer[BUFFER_SIZE];
  116. size_t bytes_read;
  117. bool key_found = false;
  118. bool in_string = false;
  119. bool is_escaped = false;
  120. bool reading_key = false;
  121. bool reading_value = false;
  122. char current_key[MAX_KEY_LENGTH] = {0};
  123. size_t key_index = 0;
  124. size_t value_index = 0;
  125. // Open storage and file
  126. _storage = furi_record_open(RECORD_STORAGE);
  127. if (!_storage)
  128. {
  129. FURI_LOG_E("JSON_PARSE", "Failed to open storage.");
  130. return false;
  131. }
  132. _file = storage_file_alloc(_storage);
  133. if (!_file)
  134. {
  135. FURI_LOG_E("JSON_PARSE", "Failed to allocate file.");
  136. furi_record_close(RECORD_STORAGE);
  137. return false;
  138. }
  139. if (!storage_file_open(_file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  140. {
  141. FURI_LOG_E("JSON_PARSE", "Failed to open JSON file for reading.");
  142. goto cleanup;
  143. }
  144. while ((bytes_read = storage_file_read(_file, buffer, BUFFER_SIZE)) > 0)
  145. {
  146. for (size_t i = 0; i < bytes_read; ++i)
  147. {
  148. char c = buffer[i];
  149. if (is_escaped)
  150. {
  151. is_escaped = false;
  152. if (reading_key)
  153. {
  154. if (key_index < MAX_KEY_LENGTH - 1)
  155. {
  156. current_key[key_index++] = c;
  157. }
  158. }
  159. else if (reading_value)
  160. {
  161. if (value_index < value_buffer_size - 1)
  162. {
  163. value_buffer[value_index++] = c;
  164. }
  165. }
  166. continue;
  167. }
  168. if (c == '\\')
  169. {
  170. is_escaped = true;
  171. continue;
  172. }
  173. if (c == '\"')
  174. {
  175. in_string = !in_string;
  176. if (in_string)
  177. {
  178. // Start of a string
  179. if (!reading_key && !reading_value)
  180. {
  181. // Possible start of a key
  182. reading_key = true;
  183. key_index = 0;
  184. current_key[0] = '\0';
  185. }
  186. }
  187. else
  188. {
  189. // End of a string
  190. if (reading_key)
  191. {
  192. reading_key = false;
  193. current_key[key_index] = '\0';
  194. if (strcmp(current_key, target_key) == 0)
  195. {
  196. key_found = true;
  197. }
  198. }
  199. else if (reading_value)
  200. {
  201. reading_value = false;
  202. value_buffer[value_index] = '\0';
  203. if (key_found)
  204. {
  205. // Found the target value
  206. goto success;
  207. }
  208. }
  209. }
  210. continue;
  211. }
  212. if (in_string)
  213. {
  214. if (reading_key)
  215. {
  216. if (key_index < MAX_KEY_LENGTH - 1)
  217. {
  218. current_key[key_index++] = c;
  219. }
  220. }
  221. else if (reading_value)
  222. {
  223. if (value_index < value_buffer_size - 1)
  224. {
  225. value_buffer[value_index++] = c;
  226. }
  227. }
  228. continue;
  229. }
  230. if (c == ':' && key_found && !reading_value)
  231. {
  232. // After colon, start reading the value
  233. // Skip whitespace and possible opening quote
  234. while (i + 1 < bytes_read && (buffer[i + 1] == ' ' || buffer[i + 1] == '\n' || buffer[i + 1] == '\r'))
  235. {
  236. i++;
  237. }
  238. if (i + 1 < bytes_read && buffer[i + 1] == '\"')
  239. {
  240. i++; // Move to the quote
  241. in_string = true;
  242. reading_value = true;
  243. value_index = 0;
  244. }
  245. else
  246. {
  247. // Handle non-string values (e.g., numbers, booleans)
  248. reading_value = true;
  249. value_index = 0;
  250. }
  251. continue;
  252. }
  253. if (reading_value && (c == ',' || c == '}' || c == ']'))
  254. {
  255. // End of the value
  256. reading_value = false;
  257. value_buffer[value_index] = '\0';
  258. if (key_found)
  259. {
  260. // Found the target value
  261. goto success;
  262. }
  263. key_found = false;
  264. }
  265. }
  266. }
  267. success:
  268. storage_file_close(_file);
  269. storage_file_free(_file);
  270. furi_record_close(RECORD_STORAGE);
  271. return key_found;
  272. cleanup:
  273. if (_file)
  274. {
  275. storage_file_free(_file);
  276. }
  277. if (_storage)
  278. {
  279. furi_record_close(RECORD_STORAGE);
  280. }
  281. return false;
  282. }
  283. bool save_char(
  284. const char *path_name, const char *value)
  285. {
  286. if (!value)
  287. {
  288. return false;
  289. }
  290. // Create the directory for saving settings
  291. char directory_path[256];
  292. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data");
  293. // Create the directory
  294. Storage *storage = furi_record_open(RECORD_STORAGE);
  295. storage_common_mkdir(storage, directory_path);
  296. // Open the settings file
  297. File *file = storage_file_alloc(storage);
  298. char file_path[256];
  299. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s.txt", path_name);
  300. // Open the file in write mode
  301. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  302. {
  303. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  304. storage_file_free(file);
  305. furi_record_close(RECORD_STORAGE);
  306. return false;
  307. }
  308. // Write the data to the file
  309. size_t data_size = strlen(value) + 1; // Include null terminator
  310. if (storage_file_write(file, value, data_size) != data_size)
  311. {
  312. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  313. storage_file_close(file);
  314. storage_file_free(file);
  315. furi_record_close(RECORD_STORAGE);
  316. return false;
  317. }
  318. storage_file_close(file);
  319. storage_file_free(file);
  320. furi_record_close(RECORD_STORAGE);
  321. return true;
  322. }
  323. bool load_char(
  324. const char *path_name,
  325. char *value,
  326. size_t value_size)
  327. {
  328. if (!value)
  329. {
  330. return false;
  331. }
  332. Storage *storage = furi_record_open(RECORD_STORAGE);
  333. File *file = storage_file_alloc(storage);
  334. char file_path[256];
  335. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s.txt", path_name);
  336. // Open the file for reading
  337. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  338. {
  339. storage_file_free(file);
  340. furi_record_close(RECORD_STORAGE);
  341. return NULL; // Return false if the file does not exist
  342. }
  343. // Read data into the buffer
  344. size_t read_count = storage_file_read(file, value, value_size);
  345. if (storage_file_get_error(file) != FSE_OK)
  346. {
  347. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  348. storage_file_close(file);
  349. storage_file_free(file);
  350. furi_record_close(RECORD_STORAGE);
  351. return false;
  352. }
  353. // Ensure null-termination
  354. value[read_count - 1] = '\0';
  355. storage_file_close(file);
  356. storage_file_free(file);
  357. furi_record_close(RECORD_STORAGE);
  358. return true;
  359. }
  360. bool save_char_with_path(const char *full_path, const char *value)
  361. {
  362. if (!value)
  363. {
  364. return false;
  365. }
  366. Storage *storage = furi_record_open(RECORD_STORAGE);
  367. File *file = storage_file_alloc(storage);
  368. // Open the file in write mode
  369. if (!storage_file_open(file, full_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  370. {
  371. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", full_path);
  372. storage_file_free(file);
  373. furi_record_close(RECORD_STORAGE);
  374. return false;
  375. }
  376. // Write the data to the file
  377. size_t data_size = strlen(value) + 1; // Include null terminator
  378. if (storage_file_write(file, value, data_size) != data_size)
  379. {
  380. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  381. storage_file_close(file);
  382. storage_file_free(file);
  383. furi_record_close(RECORD_STORAGE);
  384. return false;
  385. }
  386. storage_file_close(file);
  387. storage_file_free(file);
  388. furi_record_close(RECORD_STORAGE);
  389. return true;
  390. }