flip_wifi_storage.c 14 KB

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