flip_wifi_storage.h 14 KB

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