flip_wifi_storage.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. snprintf(edited_directory_path, sizeof(edited_directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s", app_ids[i]);
  211. snprintf(edited_file_path, sizeof(edited_file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/settings.bin", app_ids[i]);
  212. Storage *storage = furi_record_open(RECORD_STORAGE);
  213. if (!storage)
  214. {
  215. FURI_LOG_E(TAG, "Failed to open storage record for app: %s", app_ids[i]);
  216. continue; // Skip to the next app
  217. }
  218. storage_common_mkdir(storage, edited_directory_path);
  219. File *file = storage_file_alloc(storage);
  220. if (!file)
  221. {
  222. FURI_LOG_E(TAG, "Failed to allocate storage file for app: %s", app_ids[i]);
  223. furi_record_close(RECORD_STORAGE);
  224. continue; // Skip to the next app
  225. }
  226. if (!storage_file_open(file, edited_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  227. {
  228. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", edited_file_path);
  229. storage_file_free(file);
  230. furi_record_close(RECORD_STORAGE);
  231. continue; // Skip to the next app
  232. }
  233. // Save the ssid length and data
  234. size_t ssid_length = strlen(ssid) + 1; // Include null terminator
  235. if (storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  236. storage_file_write(file, ssid, ssid_length) != ssid_length)
  237. {
  238. FURI_LOG_E(TAG, "Failed to write SSID for app: %s", app_ids[i]);
  239. // Continue to attempt writing password
  240. }
  241. // Save the password length and data
  242. size_t password_length = strlen(password) + 1; // Include null terminator
  243. if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  244. storage_file_write(file, password, password_length) != password_length)
  245. {
  246. FURI_LOG_E(TAG, "Failed to write password for app: %s", app_ids[i]);
  247. }
  248. storage_file_close(file);
  249. storage_file_free(file);
  250. furi_record_close(RECORD_STORAGE);
  251. }
  252. }
  253. // static bool load_settings(
  254. // char *ssid,
  255. // size_t ssid_size,
  256. // char *password,
  257. // size_t password_size)
  258. // {
  259. // Storage *storage = furi_record_open(RECORD_STORAGE);
  260. // File *file = storage_file_alloc(storage);
  261. // if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  262. // {
  263. // FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  264. // storage_file_free(file);
  265. // furi_record_close(RECORD_STORAGE);
  266. // return false; // Return false if the file does not exist
  267. // }
  268. // // Load the ssid
  269. // size_t ssid_length;
  270. // if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  271. // storage_file_read(file, ssid, ssid_length) != ssid_length)
  272. // {
  273. // FURI_LOG_E(TAG, "Failed to read SSID");
  274. // storage_file_close(file);
  275. // storage_file_free(file);
  276. // furi_record_close(RECORD_STORAGE);
  277. // return false;
  278. // }
  279. // ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  280. // // Load the password
  281. // size_t password_length;
  282. // if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  283. // storage_file_read(file, password, password_length) != password_length)
  284. // {
  285. // FURI_LOG_E(TAG, "Failed to read password");
  286. // storage_file_close(file);
  287. // storage_file_free(file);
  288. // furi_record_close(RECORD_STORAGE);
  289. // return false;
  290. // }
  291. // password[password_length - 1] = '\0'; // Ensure null-termination
  292. // storage_file_close(file);
  293. // storage_file_free(file);
  294. // furi_record_close(RECORD_STORAGE);
  295. // return true;
  296. // }
  297. #endif