| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #include "flip_storage/storage.h"
- void save_settings(
- const char *ssid,
- const char *password)
- {
- // Create the directory for saving settings
- char directory_path[256];
- snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
- // Create the directory
- Storage *storage = furi_record_open(RECORD_STORAGE);
- storage_common_mkdir(storage, directory_path);
- // Open the settings file
- File *file = storage_file_alloc(storage);
- if (!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
- {
- FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- return;
- }
- // 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");
- }
- // 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");
- }
- storage_file_close(file);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- }
- 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;
- }
- bool save_char(
- const char *path_name, const char *value)
- {
- if (!value)
- {
- return false;
- }
- // Create the directory for saving settings
- char directory_path[256];
- snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
- // Create the directory
- Storage *storage = furi_record_open(RECORD_STORAGE);
- storage_common_mkdir(storage, directory_path);
- // Open the settings file
- File *file = storage_file_alloc(storage);
- char file_path[256];
- snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/%s.txt", path_name);
- // Open the file in write mode
- if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
- {
- FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- return false;
- }
- // Write the data to the file
- size_t data_size = strlen(value) + 1; // Include null terminator
- if (storage_file_write(file, value, data_size) != data_size)
- {
- FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
- storage_file_close(file);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- return false;
- }
- storage_file_close(file);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- return true;
- }
- bool load_char(
- const char *path_name,
- char *value,
- size_t value_size)
- {
- if (!value)
- {
- return false;
- }
- Storage *storage = furi_record_open(RECORD_STORAGE);
- File *file = storage_file_alloc(storage);
- char file_path[256];
- snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/%s.txt", path_name);
- // Open the file for reading
- if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
- {
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- return NULL; // Return false if the file does not exist
- }
- // Read data into the buffer
- size_t read_count = storage_file_read(file, value, value_size);
- if (storage_file_get_error(file) != FSE_OK)
- {
- FURI_LOG_E(HTTP_TAG, "Error reading from file.");
- storage_file_close(file);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- return false;
- }
- // Ensure null-termination
- value[read_count - 1] = '\0';
- storage_file_close(file);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- return true;
- }
|