flip_store_storage.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. // Function to parse JSON incrementally from a file
  101. bool parse_json_incrementally(const char *file_path, const char *target_key, char *value_buffer, size_t value_buffer_size)
  102. {
  103. Storage *_storage = NULL;
  104. File *_file = NULL;
  105. char buffer[BUFFER_SIZE];
  106. size_t bytes_read;
  107. bool key_found = false;
  108. bool in_string = false;
  109. bool is_escaped = false;
  110. bool reading_key = false;
  111. bool reading_value = false;
  112. char current_key[MAX_KEY_LENGTH] = {0};
  113. size_t key_index = 0;
  114. size_t value_index = 0;
  115. // Open storage and file
  116. _storage = furi_record_open(RECORD_STORAGE);
  117. if (!_storage)
  118. {
  119. FURI_LOG_E("JSON_PARSE", "Failed to open storage.");
  120. return false;
  121. }
  122. _file = storage_file_alloc(_storage);
  123. if (!_file)
  124. {
  125. FURI_LOG_E("JSON_PARSE", "Failed to allocate file.");
  126. furi_record_close(RECORD_STORAGE);
  127. return false;
  128. }
  129. if (!storage_file_open(_file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  130. {
  131. FURI_LOG_E("JSON_PARSE", "Failed to open JSON file for reading.");
  132. goto cleanup;
  133. }
  134. while ((bytes_read = storage_file_read(_file, buffer, BUFFER_SIZE)) > 0)
  135. {
  136. for (size_t i = 0; i < bytes_read; ++i)
  137. {
  138. char c = buffer[i];
  139. if (is_escaped)
  140. {
  141. is_escaped = false;
  142. if (reading_key)
  143. {
  144. if (key_index < MAX_KEY_LENGTH - 1)
  145. {
  146. current_key[key_index++] = c;
  147. }
  148. }
  149. else if (reading_value)
  150. {
  151. if (value_index < value_buffer_size - 1)
  152. {
  153. value_buffer[value_index++] = c;
  154. }
  155. }
  156. continue;
  157. }
  158. if (c == '\\')
  159. {
  160. is_escaped = true;
  161. continue;
  162. }
  163. if (c == '\"')
  164. {
  165. in_string = !in_string;
  166. if (in_string)
  167. {
  168. // Start of a string
  169. if (!reading_key && !reading_value)
  170. {
  171. // Possible start of a key
  172. reading_key = true;
  173. key_index = 0;
  174. current_key[0] = '\0';
  175. }
  176. }
  177. else
  178. {
  179. // End of a string
  180. if (reading_key)
  181. {
  182. reading_key = false;
  183. current_key[key_index] = '\0';
  184. if (strcmp(current_key, target_key) == 0)
  185. {
  186. key_found = true;
  187. }
  188. }
  189. else if (reading_value)
  190. {
  191. reading_value = false;
  192. value_buffer[value_index] = '\0';
  193. if (key_found)
  194. {
  195. // Found the target value
  196. goto success;
  197. }
  198. }
  199. }
  200. continue;
  201. }
  202. if (in_string)
  203. {
  204. if (reading_key)
  205. {
  206. if (key_index < MAX_KEY_LENGTH - 1)
  207. {
  208. current_key[key_index++] = c;
  209. }
  210. }
  211. else if (reading_value)
  212. {
  213. if (value_index < value_buffer_size - 1)
  214. {
  215. value_buffer[value_index++] = c;
  216. }
  217. }
  218. continue;
  219. }
  220. if (c == ':' && key_found && !reading_value)
  221. {
  222. // After colon, start reading the value
  223. // Skip whitespace and possible opening quote
  224. while (i + 1 < bytes_read && (buffer[i + 1] == ' ' || buffer[i + 1] == '\n' || buffer[i + 1] == '\r'))
  225. {
  226. i++;
  227. }
  228. if (i + 1 < bytes_read && buffer[i + 1] == '\"')
  229. {
  230. i++; // Move to the quote
  231. in_string = true;
  232. reading_value = true;
  233. value_index = 0;
  234. }
  235. else
  236. {
  237. // Handle non-string values (e.g., numbers, booleans)
  238. reading_value = true;
  239. value_index = 0;
  240. }
  241. continue;
  242. }
  243. if (reading_value && (c == ',' || c == '}' || c == ']'))
  244. {
  245. // End of the value
  246. reading_value = false;
  247. value_buffer[value_index] = '\0';
  248. if (key_found)
  249. {
  250. // Found the target value
  251. goto success;
  252. }
  253. key_found = false;
  254. }
  255. }
  256. }
  257. success:
  258. storage_file_close(_file);
  259. storage_file_free(_file);
  260. furi_record_close(RECORD_STORAGE);
  261. return key_found;
  262. cleanup:
  263. if (_file)
  264. {
  265. storage_file_free(_file);
  266. }
  267. if (_storage)
  268. {
  269. furi_record_close(RECORD_STORAGE);
  270. }
  271. return false;
  272. }