#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; } bool save_world( const char *name, const char *world_data) { // Create the directory for saving settings char directory_path[256]; snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds"); // 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/worlds/%s.json", 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(world_data) + 1; // Include null terminator if (storage_file_write(file, world_data, 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_world( const char *name, char *json_data, size_t json_data_size) { 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/worlds/%s.json", 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, json_data, json_data_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 json_data[read_count - 1] = '\0'; storage_file_close(file); storage_file_free(file); furi_record_close(RECORD_STORAGE); return true; } FuriString *load_furi_world( const char *name) { 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/worlds/%s.json", 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 } // Allocate a FuriString to hold the received data FuriString *str_result = furi_string_alloc(); if (!str_result) { FURI_LOG_E(HTTP_TAG, "Failed to allocate FuriString"); storage_file_close(file); storage_file_free(file); furi_record_close(RECORD_STORAGE); return NULL; } // Reset the FuriString to ensure it's empty before reading furi_string_reset(str_result); // Define a buffer to hold the read data uint8_t *buffer = (uint8_t *)malloc(MAX_FILE_SHOW); if (!buffer) { FURI_LOG_E(HTTP_TAG, "Failed to allocate buffer"); furi_string_free(str_result); storage_file_close(file); storage_file_free(file); furi_record_close(RECORD_STORAGE); return NULL; } // Read data into the buffer size_t read_count = storage_file_read(file, buffer, MAX_FILE_SHOW); if (storage_file_get_error(file) != FSE_OK) { FURI_LOG_E(HTTP_TAG, "Error reading from file."); furi_string_free(str_result); storage_file_close(file); storage_file_free(file); furi_record_close(RECORD_STORAGE); return NULL; } // Append each byte to the FuriString for (size_t i = 0; i < read_count; i++) { furi_string_push_back(str_result, buffer[i]); } // Check if there is more data beyond the maximum size char extra_byte; storage_file_read(file, &extra_byte, 1); // Clean up storage_file_close(file); storage_file_free(file); furi_record_close(RECORD_STORAGE); free(buffer); return str_result; } bool world_exists(const char *name) { if (!name) { FURI_LOG_E(TAG, "Invalid name"); return false; } Storage *storage = furi_record_open(RECORD_STORAGE); if (!storage) { FURI_LOG_E(TAG, "Failed to open storage"); return false; } char file_path[256]; snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name); bool does_exist = storage_file_exists(storage, file_path); // Clean up furi_record_close(RECORD_STORAGE); return does_exist; }