| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- #include "flip_storage/flip_store_storage.h"
- void save_settings(
- const char *ssid,
- const char *password)
- {
- // Create the directory for saving settings
- char directory_path[128];
- snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store");
- // 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;
- }
- // future implenetation because we need the app category
- bool delete_app(const char *app_id, const char *app_category)
- {
- // Create the directory for saving settings
- char directory_path[128];
- snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps/%s/%s", app_category, app_id);
- // Create the directory
- Storage *storage = furi_record_open(RECORD_STORAGE);
- if (!storage_simply_remove_recursive(storage, directory_path))
- {
- FURI_LOG_E(TAG, "Failed to delete app: %s", app_id);
- furi_record_close(RECORD_STORAGE);
- return false;
- }
- furi_record_close(RECORD_STORAGE);
- return true;
- }
- bool app_exists(const char *app_id, const char *app_category)
- {
- // Check if the app exists
- char directory_path[128];
- snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps/%s/%s.fap", app_category, app_id);
- Storage *storage = furi_record_open(RECORD_STORAGE);
- bool exists = storage_common_exists(storage, directory_path);
- furi_record_close(RECORD_STORAGE);
- return exists;
- }
- // Function to parse JSON incrementally from a file
- bool parse_json_incrementally(const char *file_path, const char *target_key, char *value_buffer, size_t value_buffer_size)
- {
- Storage *_storage = NULL;
- File *_file = NULL;
- char buffer[BUFFER_SIZE];
- size_t bytes_read;
- bool key_found = false;
- bool in_string = false;
- bool is_escaped = false;
- bool reading_key = false;
- bool reading_value = false;
- char current_key[MAX_KEY_LENGTH] = {0};
- size_t key_index = 0;
- size_t value_index = 0;
- // Open storage and file
- _storage = furi_record_open(RECORD_STORAGE);
- if (!_storage)
- {
- FURI_LOG_E("JSON_PARSE", "Failed to open storage.");
- return false;
- }
- _file = storage_file_alloc(_storage);
- if (!_file)
- {
- FURI_LOG_E("JSON_PARSE", "Failed to allocate file.");
- furi_record_close(RECORD_STORAGE);
- return false;
- }
- if (!storage_file_open(_file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
- {
- FURI_LOG_E("JSON_PARSE", "Failed to open JSON file for reading.");
- goto cleanup;
- }
- while ((bytes_read = storage_file_read(_file, buffer, BUFFER_SIZE)) > 0)
- {
- for (size_t i = 0; i < bytes_read; ++i)
- {
- char c = buffer[i];
- if (is_escaped)
- {
- is_escaped = false;
- if (reading_key)
- {
- if (key_index < MAX_KEY_LENGTH - 1)
- {
- current_key[key_index++] = c;
- }
- }
- else if (reading_value)
- {
- if (value_index < value_buffer_size - 1)
- {
- value_buffer[value_index++] = c;
- }
- }
- continue;
- }
- if (c == '\\')
- {
- is_escaped = true;
- continue;
- }
- if (c == '\"')
- {
- in_string = !in_string;
- if (in_string)
- {
- // Start of a string
- if (!reading_key && !reading_value)
- {
- // Possible start of a key
- reading_key = true;
- key_index = 0;
- current_key[0] = '\0';
- }
- }
- else
- {
- // End of a string
- if (reading_key)
- {
- reading_key = false;
- current_key[key_index] = '\0';
- if (strcmp(current_key, target_key) == 0)
- {
- key_found = true;
- }
- }
- else if (reading_value)
- {
- reading_value = false;
- value_buffer[value_index] = '\0';
- if (key_found)
- {
- // Found the target value
- goto success;
- }
- }
- }
- continue;
- }
- if (in_string)
- {
- if (reading_key)
- {
- if (key_index < MAX_KEY_LENGTH - 1)
- {
- current_key[key_index++] = c;
- }
- }
- else if (reading_value)
- {
- if (value_index < value_buffer_size - 1)
- {
- value_buffer[value_index++] = c;
- }
- }
- continue;
- }
- if (c == ':' && key_found && !reading_value)
- {
- // After colon, start reading the value
- // Skip whitespace and possible opening quote
- while (i + 1 < bytes_read && (buffer[i + 1] == ' ' || buffer[i + 1] == '\n' || buffer[i + 1] == '\r'))
- {
- i++;
- }
- if (i + 1 < bytes_read && buffer[i + 1] == '\"')
- {
- i++; // Move to the quote
- in_string = true;
- reading_value = true;
- value_index = 0;
- }
- else
- {
- // Handle non-string values (e.g., numbers, booleans)
- reading_value = true;
- value_index = 0;
- }
- continue;
- }
- if (reading_value && (c == ',' || c == '}' || c == ']'))
- {
- // End of the value
- reading_value = false;
- value_buffer[value_index] = '\0';
- if (key_found)
- {
- // Found the target value
- goto success;
- }
- key_found = false;
- }
- }
- }
- success:
- storage_file_close(_file);
- storage_file_free(_file);
- furi_record_close(RECORD_STORAGE);
- return key_found;
- cleanup:
- if (_file)
- {
- storage_file_free(_file);
- }
- if (_storage)
- {
- furi_record_close(RECORD_STORAGE);
- }
- return false;
- }
- 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_store/data");
- // 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_store/data/%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_store/data/%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;
- }
- bool save_char_with_path(const char *full_path, const char *value)
- {
- if (!value)
- {
- return false;
- }
- Storage *storage = furi_record_open(RECORD_STORAGE);
- File *file = storage_file_alloc(storage);
- // Open the file in write mode
- if (!storage_file_open(file, full_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
- {
- FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", full_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;
- }
|