LED_shit/main/fs_handler.c
2025-11-04 21:33:28 +00:00

55 lines
1.5 KiB
C

#include <stdio.h>
#include <string.h>
#include "esp_log.h"
#include "esp_spiffs.h"
static const char *TAG = "FS";
// --- MONTAR SPIFFS ---
void fs_init(void) {
esp_vfs_spiffs_conf_t conf = {
.base_path = "/spiffs",
.partition_label = "storage",
.max_files = 5,
.format_if_mount_failed = true
};
esp_err_t ret = esp_vfs_spiffs_register(&conf);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "❌ Falha ao montar SPIFFS (%s)", esp_err_to_name(ret));
return;
}
size_t total = 0, used = 0;
ret = esp_spiffs_info(conf.partition_label, &total, &used);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "❌ Erro ao obter info SPIFFS (%s)", esp_err_to_name(ret));
return;
}
ESP_LOGI(TAG, "📂 SPIFFS montado: total=%d KB, usado=%d KB",
(int)(total / 1024), (int)(used / 1024));
// Criar ficheiro de exemplo se não existir
FILE *f = fopen("/spiffs/config.json", "r");
if (!f) {
ESP_LOGW(TAG, "⚠️ config.json não existe, a criar...");
f = fopen("/spiffs/config.json", "w");
if (f) {
fprintf(f, "{ \"ssid\": \"ALQAEDA\", \"pass\": \"1q2w3e4r5t\" }");
fclose(f);
ESP_LOGI(TAG, "✅ config.json criado.");
} else {
ESP_LOGE(TAG, "❌ Erro a criar config.json");
}
} else {
fclose(f);
}
}
// --- DESMONTAR SPIFFS ---
void fs_deinit(void) {
ESP_LOGI(TAG, "🧹 Desmontando SPIFFS...");
esp_vfs_spiffs_unregister(NULL);
}