flip_wifi_storage.h 14 KB

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