| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- #ifndef FLIP_WIFI_STORAGE_H
- #define FLIP_WIFI_STORAGE_H
- // define the paths for all of the FlipperHTTP apps
- #define WIFI_SSID_LIST_PATH STORAGE_EXT_PATH_PREFIX "/apps_data/flip_wifi/wifi_list.txt"
- // Function to save the playlist
- void save_playlist(const WiFiPlaylist *playlist)
- {
- if (!playlist)
- {
- FURI_LOG_E(TAG, "Playlist is NULL");
- return;
- }
- // Create the directory for saving settings
- char directory_path[128];
- snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_wifi");
- // Open storage
- Storage *storage = furi_record_open(RECORD_STORAGE);
- if (!storage)
- {
- FURI_LOG_E(TAG, "Failed to open storage record");
- return;
- }
- // Create the directory
- storage_common_mkdir(storage, directory_path);
- // Open the settings file
- File *file = storage_file_alloc(storage);
- if (!file)
- {
- FURI_LOG_E(TAG, "Failed to allocate file handle");
- furi_record_close(RECORD_STORAGE);
- return;
- }
- if (!storage_file_open(file, WIFI_SSID_LIST_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
- {
- FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", WIFI_SSID_LIST_PATH);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- return;
- }
- for (size_t i = 0; i < playlist->count; i++)
- {
- if (!playlist->ssids[i] || !playlist->passwords[i])
- {
- FURI_LOG_E(TAG, "Invalid SSID or password at index %zu", i);
- continue;
- }
- size_t ssid_length = strlen(playlist->ssids[i]);
- size_t password_length = strlen(playlist->passwords[i]);
- if (storage_file_write(file, playlist->ssids[i], ssid_length) != ssid_length ||
- storage_file_write(file, ",", 1) != 1 ||
- storage_file_write(file, playlist->passwords[i], password_length) != password_length ||
- storage_file_write(file, "\n", 1) != 1)
- {
- FURI_LOG_E(TAG, "Failed to write playlist");
- }
- }
- storage_file_close(file);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- }
- // Function to load the playlist
- bool load_playlist(WiFiPlaylist *playlist)
- {
- if (!playlist)
- {
- FURI_LOG_E(TAG, "Playlist is NULL");
- return false;
- }
- // Initialize playlist count
- playlist->count = 0;
- // Allocate memory for SSIDs and passwords if not already allocated
- for (size_t i = 0; i < MAX_WIFI_NETWORKS; i++)
- {
- if (!playlist->ssids[i])
- {
- playlist->ssids[i] = malloc(64); // Adjust size as needed
- if (!playlist->ssids[i])
- {
- FURI_LOG_E(TAG, "Memory allocation failed for ssids[%zu]", i);
- // Handle memory allocation failure (e.g., clean up and return)
- return false;
- }
- }
- if (!playlist->passwords[i])
- {
- playlist->passwords[i] = malloc(64); // Adjust size as needed
- if (!playlist->passwords[i])
- {
- FURI_LOG_E(TAG, "Memory allocation failed for passwords[%zu]", i);
- // Handle memory allocation failure (e.g., clean up and return)
- return false;
- }
- }
- }
- // Open the settings file
- Storage *storage = furi_record_open(RECORD_STORAGE);
- if (!storage)
- {
- FURI_LOG_E(TAG, "Failed to open storage record");
- return false;
- }
- File *file = storage_file_alloc(storage);
- if (!file)
- {
- FURI_LOG_E(TAG, "Failed to allocate file handle");
- furi_record_close(RECORD_STORAGE);
- return false;
- }
- if (!storage_file_open(file, WIFI_SSID_LIST_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
- {
- FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", WIFI_SSID_LIST_PATH);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- return false; // Return false if the file does not exist
- }
- // Buffer to hold each line
- char line_buffer[128];
- size_t line_pos = 0;
- char ch;
- while (storage_file_read(file, &ch, 1) == 1)
- {
- if (ch == '\n')
- {
- // Null-terminate the line
- line_buffer[line_pos] = '\0';
- // Split the line into SSID and Password
- char *comma_pos = strchr(line_buffer, ',');
- if (comma_pos)
- {
- *comma_pos = '\0'; // Replace comma with null character
- // Copy SSID
- strncpy(playlist->ssids[playlist->count], line_buffer, 63);
- playlist->ssids[playlist->count][63] = '\0'; // Ensure null-termination
- // Copy Password
- strncpy(playlist->passwords[playlist->count], comma_pos + 1, 63);
- playlist->passwords[playlist->count][63] = '\0'; // Ensure null-termination
- playlist->count++;
- if (playlist->count >= MAX_WIFI_NETWORKS)
- {
- FURI_LOG_W(TAG, "Reached maximum number of WiFi networks: %d", MAX_WIFI_NETWORKS);
- break;
- }
- }
- else
- {
- FURI_LOG_E(TAG, "Invalid line format (no comma found): %s", line_buffer);
- }
- // Reset line buffer position for the next line
- line_pos = 0;
- }
- else
- {
- if (line_pos < sizeof(line_buffer) - 1)
- {
- line_buffer[line_pos++] = ch;
- }
- else
- {
- FURI_LOG_E(TAG, "Line buffer overflow");
- // Optionally handle line overflow (e.g., skip the rest of the line)
- line_pos = 0;
- }
- }
- }
- // Handle the last line if it does not end with a newline
- if (line_pos > 0)
- {
- line_buffer[line_pos] = '\0';
- char *comma_pos = strchr(line_buffer, ',');
- if (comma_pos)
- {
- *comma_pos = '\0'; // Replace comma with null character
- // Copy SSID
- strncpy(playlist->ssids[playlist->count], line_buffer, 63);
- playlist->ssids[playlist->count][63] = '\0'; // Ensure null-termination
- // Copy Password
- strncpy(playlist->passwords[playlist->count], comma_pos + 1, 63);
- playlist->passwords[playlist->count][63] = '\0'; // Ensure null-termination
- playlist->count++;
- if (playlist->count >= MAX_WIFI_NETWORKS)
- {
- FURI_LOG_W(TAG, "Reached maximum number of WiFi networks: %d", MAX_WIFI_NETWORKS);
- }
- }
- else
- {
- FURI_LOG_E(TAG, "Invalid line format (no comma found): %s", line_buffer);
- }
- }
- // Close and free file resources
- storage_file_close(file);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- return true;
- }
- char *app_ids[7] = {
- "flip_wifi",
- "flip_store",
- "flip_social",
- "flip_trader",
- "flip_weather",
- "flip_library",
- "web_crawler"};
- void save_settings(const char *ssid, const char *password)
- {
- char edited_directory_path[128];
- char edited_file_path[128];
- for (size_t i = 0; i < 7; i++)
- {
- snprintf(edited_directory_path, sizeof(edited_directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s", app_ids[i]);
- snprintf(edited_file_path, sizeof(edited_file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/settings.bin", app_ids[i]);
- Storage *storage = furi_record_open(RECORD_STORAGE);
- if (!storage)
- {
- FURI_LOG_E(TAG, "Failed to open storage record for app: %s", app_ids[i]);
- continue; // Skip to the next app
- }
- storage_common_mkdir(storage, edited_directory_path);
- File *file = storage_file_alloc(storage);
- if (!file)
- {
- FURI_LOG_E(TAG, "Failed to allocate storage file for app: %s", app_ids[i]);
- furi_record_close(RECORD_STORAGE);
- continue; // Skip to the next app
- }
- if (!storage_file_open(file, edited_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
- {
- FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", edited_file_path);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- continue; // Skip to the next app
- }
- // Save the ssid length and data
- size_t ssid_length = strlen(ssid) + 1; // Include null terminator
- if (storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
- storage_file_write(file, ssid, ssid_length) != ssid_length)
- {
- FURI_LOG_E(TAG, "Failed to write SSID for app: %s", app_ids[i]);
- // Continue to attempt writing password
- }
- // Save the password length and data
- size_t password_length = strlen(password) + 1; // Include null terminator
- if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
- storage_file_write(file, password, password_length) != password_length)
- {
- FURI_LOG_E(TAG, "Failed to write password for app: %s", app_ids[i]);
- }
- storage_file_close(file);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- }
- }
- // static bool load_settings(
- // char *ssid,
- // size_t ssid_size,
- // char *password,
- // size_t password_size)
- // {
- // Storage *storage = furi_record_open(RECORD_STORAGE);
- // File *file = storage_file_alloc(storage);
- // if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
- // {
- // FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
- // storage_file_free(file);
- // furi_record_close(RECORD_STORAGE);
- // return false; // Return false if the file does not exist
- // }
- // // Load the ssid
- // size_t ssid_length;
- // if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
- // storage_file_read(file, ssid, ssid_length) != ssid_length)
- // {
- // FURI_LOG_E(TAG, "Failed to read SSID");
- // storage_file_close(file);
- // storage_file_free(file);
- // furi_record_close(RECORD_STORAGE);
- // return false;
- // }
- // ssid[ssid_length - 1] = '\0'; // Ensure null-termination
- // // Load the password
- // size_t password_length;
- // if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
- // storage_file_read(file, password, password_length) != password_length)
- // {
- // FURI_LOG_E(TAG, "Failed to read password");
- // storage_file_close(file);
- // storage_file_free(file);
- // furi_record_close(RECORD_STORAGE);
- // return false;
- // }
- // password[password_length - 1] = '\0'; // Ensure null-termination
- // storage_file_close(file);
- // storage_file_free(file);
- // furi_record_close(RECORD_STORAGE);
- // return true;
- // }
- #endif
|