flip_wifi_storage.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #include <flip_storage/flip_wifi_storage.h>
  2. static char *app_ids[8] = {
  3. "flip_wifi",
  4. "flip_store",
  5. "flip_social",
  6. "flip_trader",
  7. "flip_weather",
  8. "flip_library",
  9. "web_crawler",
  10. "flip_world"};
  11. // Function to save the playlist
  12. void save_playlist(WiFiPlaylist *playlist)
  13. {
  14. if (!playlist)
  15. {
  16. FURI_LOG_E(TAG, "Playlist is NULL");
  17. return;
  18. }
  19. // Create the directory for saving settings
  20. char directory_path[128];
  21. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_wifi/data");
  22. // Open storage
  23. Storage *storage = furi_record_open(RECORD_STORAGE);
  24. if (!storage)
  25. {
  26. FURI_LOG_E(TAG, "Failed to open storage record");
  27. return;
  28. }
  29. // Create the directory
  30. storage_common_mkdir(storage, directory_path);
  31. // Open the settings file
  32. File *file = storage_file_alloc(storage);
  33. if (!file)
  34. {
  35. FURI_LOG_E(TAG, "Failed to allocate file handle");
  36. furi_record_close(RECORD_STORAGE);
  37. return;
  38. }
  39. if (!storage_file_open(file, WIFI_SSID_LIST_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  40. {
  41. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", WIFI_SSID_LIST_PATH);
  42. storage_file_free(file);
  43. furi_record_close(RECORD_STORAGE);
  44. return;
  45. }
  46. FuriString *json_result = furi_string_alloc();
  47. if (!json_result)
  48. {
  49. FURI_LOG_E(TAG, "Failed to allocate FuriString");
  50. storage_file_close(file);
  51. storage_file_free(file);
  52. furi_record_close(RECORD_STORAGE);
  53. return;
  54. }
  55. furi_string_cat(json_result, "{\"ssids\":[\n");
  56. for (size_t i = 0; i < playlist->count; i++)
  57. {
  58. furi_string_cat_printf(json_result, "{\"ssid\":\"%s\",\"password\":\"%s\"}", playlist->ssids[i], playlist->passwords[i]);
  59. if (i < playlist->count - 1)
  60. {
  61. furi_string_cat(json_result, ",\n");
  62. }
  63. }
  64. furi_string_cat(json_result, "\n]}");
  65. size_t json_length = furi_string_size(json_result);
  66. if (storage_file_write(file, furi_string_get_cstr(json_result), json_length) != json_length)
  67. {
  68. FURI_LOG_E(TAG, "Failed to write playlist to file");
  69. }
  70. furi_string_free(json_result);
  71. storage_file_close(file);
  72. storage_file_free(file);
  73. furi_record_close(RECORD_STORAGE);
  74. }
  75. bool load_playlist(WiFiPlaylist *playlist)
  76. {
  77. if (!playlist)
  78. {
  79. FURI_LOG_E(TAG, "Playlist is NULL");
  80. return false;
  81. }
  82. FuriString *json_result = flipper_http_load_from_file(WIFI_SSID_LIST_PATH);
  83. if (!json_result)
  84. {
  85. FURI_LOG_E(TAG, "Failed to load playlist from file");
  86. return false;
  87. }
  88. // Initialize playlist count
  89. playlist->count = 0;
  90. // Parse the JSON result
  91. for (size_t i = 0; i < MAX_SAVED_NETWORKS; i++)
  92. {
  93. FuriString *json_data = get_json_array_value_furi("ssids", i, json_result);
  94. if (!json_data)
  95. {
  96. break;
  97. }
  98. FuriString *ssid = get_json_value_furi("ssid", json_data);
  99. FuriString *password = get_json_value_furi("password", json_data);
  100. if (!ssid || !password)
  101. {
  102. FURI_LOG_E(TAG, "Failed to get SSID or Password from JSON");
  103. furi_string_free(json_data);
  104. break;
  105. }
  106. snprintf(playlist->ssids[i], MAX_SSID_LENGTH, "%s", furi_string_get_cstr(ssid));
  107. snprintf(playlist->passwords[i], MAX_SSID_LENGTH, "%s", furi_string_get_cstr(password));
  108. playlist->count++;
  109. furi_string_free(json_data);
  110. furi_string_free(ssid);
  111. furi_string_free(password);
  112. }
  113. furi_string_free(json_result);
  114. return true;
  115. }
  116. void save_settings(const char *ssid, const char *password)
  117. {
  118. char edited_directory_path[128];
  119. char edited_file_path[128];
  120. for (size_t i = 0; i < 8; i++)
  121. {
  122. // Construct the directory and file paths for the current app
  123. snprintf(edited_directory_path, sizeof(edited_directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s", app_ids[i]);
  124. snprintf(edited_file_path, sizeof(edited_file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/settings.bin", app_ids[i]);
  125. // Open the storage record
  126. Storage *storage = furi_record_open(RECORD_STORAGE);
  127. if (!storage)
  128. {
  129. FURI_LOG_E(TAG, "Failed to open storage record for app: %s", app_ids[i]);
  130. continue; // Skip to the next app
  131. }
  132. // Ensure the directory exists
  133. storage_common_mkdir(storage, edited_directory_path);
  134. // Allocate a file handle
  135. File *file = storage_file_alloc(storage);
  136. if (!file)
  137. {
  138. FURI_LOG_E(TAG, "Failed to allocate storage file for app: %s", app_ids[i]);
  139. furi_record_close(RECORD_STORAGE);
  140. continue; // Skip to the next app
  141. }
  142. // Open the file in read mode to read existing data
  143. bool file_opened = storage_file_open(file, edited_file_path, FSAM_READ, FSOM_OPEN_EXISTING);
  144. size_t file_size = 0;
  145. uint8_t *buffer = NULL;
  146. if (file_opened)
  147. {
  148. // Get the file size
  149. file_size = storage_file_size(file);
  150. buffer = malloc(file_size);
  151. if (!buffer)
  152. {
  153. FURI_LOG_E(TAG, "Failed to allocate buffer for app: %s", app_ids[i]);
  154. storage_file_close(file);
  155. storage_file_free(file);
  156. furi_record_close(RECORD_STORAGE);
  157. continue;
  158. }
  159. // Read the existing data
  160. if (storage_file_read(file, buffer, file_size) != file_size)
  161. {
  162. FURI_LOG_E(TAG, "Failed to read settings file for app: %s", app_ids[i]);
  163. free(buffer);
  164. storage_file_close(file);
  165. storage_file_free(file);
  166. furi_record_close(RECORD_STORAGE);
  167. continue;
  168. }
  169. storage_file_close(file);
  170. }
  171. else
  172. {
  173. // If the file doesn't exist, initialize an empty buffer
  174. file_size = 0;
  175. buffer = NULL;
  176. }
  177. storage_file_free(file);
  178. // Prepare new SSID and Password
  179. size_t new_ssid_length = strlen(ssid) + 1; // Including null terminator
  180. size_t new_password_length = strlen(password) + 1; // Including null terminator
  181. // Calculate the new file size
  182. size_t new_file_size = sizeof(size_t) + new_ssid_length + sizeof(size_t) + new_password_length;
  183. // If there is additional data beyond SSID and Password, preserve it
  184. size_t additional_data_size = 0;
  185. uint8_t *additional_data = NULL;
  186. if (buffer)
  187. {
  188. // Parse existing SSID length
  189. if (file_size >= sizeof(size_t))
  190. {
  191. size_t existing_ssid_length;
  192. memcpy(&existing_ssid_length, buffer, sizeof(size_t));
  193. // Parse existing Password length
  194. if (file_size >= sizeof(size_t) + existing_ssid_length + sizeof(size_t))
  195. {
  196. size_t existing_password_length;
  197. memcpy(&existing_password_length, buffer + sizeof(size_t) + existing_ssid_length, sizeof(size_t));
  198. // Calculate the offset where additional data starts
  199. size_t additional_offset = sizeof(size_t) + existing_ssid_length + sizeof(size_t) + existing_password_length;
  200. if (additional_offset < file_size)
  201. {
  202. additional_data_size = file_size - additional_offset;
  203. additional_data = malloc(additional_data_size);
  204. if (additional_data)
  205. {
  206. memcpy(additional_data, buffer + additional_offset, additional_data_size);
  207. }
  208. else
  209. {
  210. FURI_LOG_E(TAG, "Failed to allocate memory for additional data for app: %s", app_ids[i]);
  211. free(buffer);
  212. furi_record_close(RECORD_STORAGE);
  213. continue;
  214. }
  215. }
  216. }
  217. else
  218. {
  219. FURI_LOG_E(TAG, "Settings file format invalid for app: %s", app_ids[i]);
  220. }
  221. }
  222. else
  223. {
  224. FURI_LOG_E(TAG, "Settings file too small for app: %s", app_ids[i]);
  225. }
  226. }
  227. // Allocate a new buffer for updated data
  228. size_t total_new_size = new_file_size + additional_data_size;
  229. uint8_t *new_buffer = malloc(total_new_size);
  230. if (!new_buffer)
  231. {
  232. FURI_LOG_E(TAG, "Failed to allocate new buffer for app: %s", app_ids[i]);
  233. if (buffer)
  234. free(buffer);
  235. if (additional_data)
  236. free(additional_data);
  237. furi_record_close(RECORD_STORAGE);
  238. continue;
  239. }
  240. size_t offset = 0;
  241. // Write new SSID length and SSID
  242. memcpy(new_buffer + offset, &new_ssid_length, sizeof(size_t));
  243. offset += sizeof(size_t);
  244. memcpy(new_buffer + offset, ssid, new_ssid_length);
  245. offset += new_ssid_length;
  246. // Write new Password length and Password
  247. memcpy(new_buffer + offset, &new_password_length, sizeof(size_t));
  248. offset += sizeof(size_t);
  249. memcpy(new_buffer + offset, password, new_password_length);
  250. offset += new_password_length;
  251. // Append any additional data if present
  252. if (additional_data_size > 0 && additional_data)
  253. {
  254. memcpy(new_buffer + offset, additional_data, additional_data_size);
  255. offset += additional_data_size;
  256. }
  257. // Free temporary buffers
  258. if (buffer)
  259. free(buffer);
  260. if (additional_data)
  261. free(additional_data);
  262. // Open the file in write mode with FSOM_CREATE_ALWAYS to overwrite it
  263. file = storage_file_alloc(storage);
  264. if (!file)
  265. {
  266. FURI_LOG_E(TAG, "Failed to allocate storage file for writing: %s", app_ids[i]);
  267. free(new_buffer);
  268. furi_record_close(RECORD_STORAGE);
  269. continue;
  270. }
  271. if (!storage_file_open(file, edited_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  272. {
  273. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", edited_file_path);
  274. storage_file_free(file);
  275. free(new_buffer);
  276. furi_record_close(RECORD_STORAGE);
  277. continue;
  278. }
  279. // Write the updated buffer back to the file
  280. if (storage_file_write(file, new_buffer, total_new_size) != total_new_size)
  281. {
  282. FURI_LOG_E(TAG, "Failed to write updated settings for app: %s", app_ids[i]);
  283. }
  284. // Clean up
  285. free(new_buffer);
  286. storage_file_close(file);
  287. storage_file_free(file);
  288. furi_record_close(RECORD_STORAGE);
  289. }
  290. }
  291. bool save_char(
  292. const char *path_name, const char *value)
  293. {
  294. if (!value)
  295. {
  296. return false;
  297. }
  298. // Create the directory for saving settings
  299. char directory_path[256];
  300. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_wifi/data");
  301. // Create the directory
  302. Storage *storage = furi_record_open(RECORD_STORAGE);
  303. if (!storage)
  304. {
  305. FURI_LOG_E(HTTP_TAG, "Failed to open storage record");
  306. return false;
  307. }
  308. storage_common_mkdir(storage, directory_path);
  309. // Open the settings file
  310. File *file = storage_file_alloc(storage);
  311. char file_path[256];
  312. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_wifi/data/%s.txt", path_name);
  313. // Open the file in write mode
  314. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  315. {
  316. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  317. storage_file_free(file);
  318. furi_record_close(RECORD_STORAGE);
  319. return false;
  320. }
  321. // Write the data to the file
  322. size_t data_size = strlen(value) + 1; // Include null terminator
  323. if (storage_file_write(file, value, data_size) != data_size)
  324. {
  325. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  326. storage_file_close(file);
  327. storage_file_free(file);
  328. furi_record_close(RECORD_STORAGE);
  329. return false;
  330. }
  331. storage_file_close(file);
  332. storage_file_free(file);
  333. furi_record_close(RECORD_STORAGE);
  334. return true;
  335. }
  336. bool load_char(
  337. const char *path_name,
  338. char *value,
  339. size_t value_size)
  340. {
  341. if (!value)
  342. {
  343. return false;
  344. }
  345. Storage *storage = furi_record_open(RECORD_STORAGE);
  346. File *file = storage_file_alloc(storage);
  347. char file_path[256];
  348. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_wifi/data/%s.txt", path_name);
  349. // Open the file for reading
  350. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  351. {
  352. storage_file_free(file);
  353. furi_record_close(RECORD_STORAGE);
  354. return NULL; // Return false if the file does not exist
  355. }
  356. // Read data into the buffer
  357. size_t read_count = storage_file_read(file, value, value_size);
  358. if (storage_file_get_error(file) != FSE_OK)
  359. {
  360. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  361. storage_file_close(file);
  362. storage_file_free(file);
  363. furi_record_close(RECORD_STORAGE);
  364. return false;
  365. }
  366. // Ensure null-termination
  367. value[read_count - 1] = '\0';
  368. storage_file_close(file);
  369. storage_file_free(file);
  370. furi_record_close(RECORD_STORAGE);
  371. return true;
  372. }