flip_wifi_storage.c 14 KB

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