flip_store_storage.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #ifndef FLIP_STORE_STORAGE_H
  2. #define FLIP_STORE_STORAGE_H
  3. #include <furi.h>
  4. #include <storage/storage.h>
  5. #define SETTINGS_PATH STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/settings.bin"
  6. static void save_settings(const char* ssid, const char* password) {
  7. // Create the directory for saving settings
  8. char directory_path[128];
  9. snprintf(
  10. directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store");
  11. // Create the directory
  12. Storage* storage = furi_record_open(RECORD_STORAGE);
  13. storage_common_mkdir(storage, directory_path);
  14. // Open the settings file
  15. File* file = storage_file_alloc(storage);
  16. if(!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  17. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  18. storage_file_free(file);
  19. furi_record_close(RECORD_STORAGE);
  20. return;
  21. }
  22. // Save the ssid length and data
  23. size_t ssid_length = strlen(ssid) + 1; // Include null terminator
  24. if(storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  25. storage_file_write(file, ssid, ssid_length) != ssid_length) {
  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. FURI_LOG_E(TAG, "Failed to write password");
  33. }
  34. storage_file_close(file);
  35. storage_file_free(file);
  36. furi_record_close(RECORD_STORAGE);
  37. }
  38. static bool load_settings(char* ssid, size_t ssid_size, char* password, size_t password_size) {
  39. Storage* storage = furi_record_open(RECORD_STORAGE);
  40. File* file = storage_file_alloc(storage);
  41. if(!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  42. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  43. storage_file_free(file);
  44. furi_record_close(RECORD_STORAGE);
  45. return false; // Return false if the file does not exist
  46. }
  47. // Load the ssid
  48. size_t ssid_length;
  49. if(storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  50. ssid_length > ssid_size || storage_file_read(file, ssid, ssid_length) != ssid_length) {
  51. FURI_LOG_E(TAG, "Failed to read SSID");
  52. storage_file_close(file);
  53. storage_file_free(file);
  54. furi_record_close(RECORD_STORAGE);
  55. return false;
  56. }
  57. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  58. // Load the password
  59. size_t password_length;
  60. if(storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  61. password_length > password_size ||
  62. storage_file_read(file, password, password_length) != password_length) {
  63. FURI_LOG_E(TAG, "Failed to read password");
  64. storage_file_close(file);
  65. storage_file_free(file);
  66. furi_record_close(RECORD_STORAGE);
  67. return false;
  68. }
  69. password[password_length - 1] = '\0'; // Ensure null-termination
  70. storage_file_close(file);
  71. storage_file_free(file);
  72. furi_record_close(RECORD_STORAGE);
  73. return true;
  74. }
  75. // future implenetation because we need the app category
  76. bool delete_app(const char* app_id, const char* app_category) {
  77. // Create the directory for saving settings
  78. char directory_path[128];
  79. snprintf(
  80. directory_path,
  81. sizeof(directory_path),
  82. STORAGE_EXT_PATH_PREFIX "/apps/%s/%s",
  83. app_category,
  84. app_id);
  85. // Create the directory
  86. Storage* storage = furi_record_open(RECORD_STORAGE);
  87. if(!storage_simply_remove_recursive(storage, directory_path)) {
  88. FURI_LOG_E(TAG, "Failed to delete app: %s", app_id);
  89. furi_record_close(RECORD_STORAGE);
  90. return false;
  91. }
  92. furi_record_close(RECORD_STORAGE);
  93. return true;
  94. }
  95. #define BUFFER_SIZE 64
  96. #define MAX_KEY_LENGTH 32
  97. #define MAX_VALUE_LENGTH 64
  98. // Function to parse JSON incrementally from a file
  99. bool parse_json_incrementally(
  100. const char* file_path,
  101. const char* target_key,
  102. char* value_buffer,
  103. size_t value_buffer_size) {
  104. Storage* _storage = NULL;
  105. File* _file = NULL;
  106. char buffer[BUFFER_SIZE];
  107. size_t bytes_read;
  108. bool key_found = false;
  109. bool in_string = false;
  110. bool is_escaped = false;
  111. bool reading_key = false;
  112. bool reading_value = false;
  113. char current_key[MAX_KEY_LENGTH] = {0};
  114. size_t key_index = 0;
  115. size_t value_index = 0;
  116. // Open storage and file
  117. _storage = furi_record_open(RECORD_STORAGE);
  118. if(!_storage) {
  119. FURI_LOG_E("JSON_PARSE", "Failed to open storage.");
  120. return false;
  121. }
  122. _file = storage_file_alloc(_storage);
  123. if(!_file) {
  124. FURI_LOG_E("JSON_PARSE", "Failed to allocate file.");
  125. furi_record_close(RECORD_STORAGE);
  126. return false;
  127. }
  128. if(!storage_file_open(_file, file_path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  129. FURI_LOG_E("JSON_PARSE", "Failed to open JSON file for reading.");
  130. goto cleanup;
  131. }
  132. while((bytes_read = storage_file_read(_file, buffer, BUFFER_SIZE)) > 0) {
  133. for(size_t i = 0; i < bytes_read; ++i) {
  134. char c = buffer[i];
  135. if(is_escaped) {
  136. is_escaped = false;
  137. if(reading_key) {
  138. if(key_index < MAX_KEY_LENGTH - 1) {
  139. current_key[key_index++] = c;
  140. }
  141. } else if(reading_value) {
  142. if(value_index < value_buffer_size - 1) {
  143. value_buffer[value_index++] = c;
  144. }
  145. }
  146. continue;
  147. }
  148. if(c == '\\') {
  149. is_escaped = true;
  150. continue;
  151. }
  152. if(c == '\"') {
  153. in_string = !in_string;
  154. if(in_string) {
  155. // Start of a string
  156. if(!reading_key && !reading_value) {
  157. // Possible start of a key
  158. reading_key = true;
  159. key_index = 0;
  160. current_key[0] = '\0';
  161. }
  162. } else {
  163. // End of a string
  164. if(reading_key) {
  165. reading_key = false;
  166. current_key[key_index] = '\0';
  167. if(strcmp(current_key, target_key) == 0) {
  168. key_found = true;
  169. }
  170. } else if(reading_value) {
  171. reading_value = false;
  172. value_buffer[value_index] = '\0';
  173. if(key_found) {
  174. // Found the target value
  175. goto success;
  176. }
  177. }
  178. }
  179. continue;
  180. }
  181. if(in_string) {
  182. if(reading_key) {
  183. if(key_index < MAX_KEY_LENGTH - 1) {
  184. current_key[key_index++] = c;
  185. }
  186. } else if(reading_value) {
  187. if(value_index < value_buffer_size - 1) {
  188. value_buffer[value_index++] = c;
  189. }
  190. }
  191. continue;
  192. }
  193. if(c == ':' && key_found && !reading_value) {
  194. // After colon, start reading the value
  195. // Skip whitespace and possible opening quote
  196. while(i + 1 < bytes_read &&
  197. (buffer[i + 1] == ' ' || buffer[i + 1] == '\n' || buffer[i + 1] == '\r')) {
  198. i++;
  199. }
  200. if(i + 1 < bytes_read && buffer[i + 1] == '\"') {
  201. i++; // Move to the quote
  202. in_string = true;
  203. reading_value = true;
  204. value_index = 0;
  205. } else {
  206. // Handle non-string values (e.g., numbers, booleans)
  207. reading_value = true;
  208. value_index = 0;
  209. }
  210. continue;
  211. }
  212. if(reading_value && (c == ',' || c == '}' || c == ']')) {
  213. // End of the value
  214. reading_value = false;
  215. value_buffer[value_index] = '\0';
  216. if(key_found) {
  217. // Found the target value
  218. goto success;
  219. }
  220. key_found = false;
  221. }
  222. }
  223. }
  224. success:
  225. storage_file_close(_file);
  226. storage_file_free(_file);
  227. furi_record_close(RECORD_STORAGE);
  228. return key_found;
  229. cleanup:
  230. if(_file) {
  231. storage_file_free(_file);
  232. }
  233. if(_storage) {
  234. furi_record_close(RECORD_STORAGE);
  235. }
  236. return false;
  237. }
  238. #endif