156 lines
4.8 KiB
C
156 lines
4.8 KiB
C
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <sys/unistd.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include "esp_err.h"
|
||
|
#include "esp_log.h"
|
||
|
#include "esp_spiffs.h"
|
||
|
#include "file.h"
|
||
|
|
||
|
|
||
|
static const char *TAG = "SPIFFS";
|
||
|
|
||
|
esp_vfs_spiffs_conf_t conf = {
|
||
|
.base_path = "/spiffs",
|
||
|
.partition_label = "storage_spiffs",
|
||
|
.max_files = 5,
|
||
|
.format_if_mount_failed = true
|
||
|
};
|
||
|
|
||
|
void init_memory(){
|
||
|
ESP_LOGI(TAG, "Initializing SPIFFS");
|
||
|
|
||
|
|
||
|
|
||
|
// Use settings defined above to initialize and mount SPIFFS filesystem.
|
||
|
// Note: esp_vfs_spiffs_register is an all-in-one convenience function.
|
||
|
esp_err_t ret = esp_vfs_spiffs_register(&conf);
|
||
|
|
||
|
if (ret != ESP_OK) {
|
||
|
if (ret == ESP_FAIL) {
|
||
|
ESP_LOGE(TAG, "Failed to mount or format filesystem");
|
||
|
} else if (ret == ESP_ERR_NOT_FOUND) {
|
||
|
ESP_LOGE(TAG, "Failed to find SPIFFS partition");
|
||
|
} else {
|
||
|
ESP_LOGE(TAG, "Failed to initialize 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, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
|
||
|
} else {
|
||
|
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void close_memory(){
|
||
|
esp_vfs_spiffs_unregister(conf.partition_label);
|
||
|
ESP_LOGI(TAG, "SPIFFS unmounted");
|
||
|
}
|
||
|
|
||
|
void create_file(char adresa[],char comment[]){
|
||
|
ESP_LOGI(TAG, "Opening file");
|
||
|
FILE* f = fopen(adresa, "w");
|
||
|
if (f == NULL) {
|
||
|
ESP_LOGE(TAG, "Failed to open file for writing");
|
||
|
return;
|
||
|
}
|
||
|
fprintf(f, comment);
|
||
|
fclose(f);
|
||
|
ESP_LOGI(TAG, "File written");
|
||
|
}
|
||
|
|
||
|
void read_file(char adresa[]){
|
||
|
char line[120];
|
||
|
ESP_LOGI(TAG, "Reading file");
|
||
|
FILE* f = fopen(adresa, "r");
|
||
|
if (f == NULL) {
|
||
|
ESP_LOGE(TAG, "Failed to open file for reading");
|
||
|
return;
|
||
|
}
|
||
|
ESP_LOGI(TAG, "Read from file: ");
|
||
|
while(fgets(line, 120, f)){
|
||
|
printf("%s",line);
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
fclose(f);
|
||
|
// strip newline
|
||
|
//char* pos = strchr(line, '\n');
|
||
|
//if (pos) {
|
||
|
// *pos = '\0';
|
||
|
//}
|
||
|
}
|
||
|
/*
|
||
|
#include<SPIFFS.h>
|
||
|
#include<FS.h>
|
||
|
|
||
|
|
||
|
void create_files(){
|
||
|
File serial = SPIFFS.open("/serial.txt",FILE_WRITE);
|
||
|
serial.print("1");
|
||
|
serial.close();
|
||
|
serial = SPIFFS.open("/keyfile.key",FILE_WRITE);
|
||
|
serial.print("-----BEGIN EC PRIVATE KEY-----\n"\
|
||
|
"MIHcAgEBBEIA4THscwU+atnO/MHMqUx65bQl8qipWp3jaoym1C01lHHB1D7o94ed\n"\
|
||
|
"sKo2P0V5lBWvDrvDHvnXF8aoajIE8a502JmgBwYFK4EEACOhgYkDgYYABAFf3u2n\n"\
|
||
|
"+Z2Y7iw3JwfFVjihJDyaLrjjojTgw9JXuZ3mDpC+RbmtvfowO0Pk6NPl8LxeqCf3\n"\
|
||
|
"19rhscMthQQ1NocsBgAWkaAwWhamRW3CBvmgioz9Bh6ETkE7/zJ1USvvoMfXkGqO\n"\
|
||
|
"ZI0MKehIwLGpGkL2JseYKnnNh/Ue5fHzpzfwncpf4g==\n"\
|
||
|
"-----END EC PRIVATE KEY-----\n");
|
||
|
serial.close();
|
||
|
serial = SPIFFS.open("/CA.crt",FILE_WRITE);
|
||
|
serial.print("-----BEGIN CERTIFICATE-----\n"\
|
||
|
"MIICQDCCAZ+gAwIBAgIBATAMBggqhkjOPQQDAgUAMDkxETAPBgNVBAMMCG15c2Vy\n"\
|
||
|
"dmVyMRcwFQYDVQQKDA5teW9yZ2FuaXNhdGlvbjELMAkGA1UEBhMCTkwwHhcNMTkw\n"\
|
||
|
"MTAxMDAwMDAwWhcNMjExMjMxMjM1OTU5WjA5MREwDwYDVQQDDAhteXNlcnZlcjEX\n"\
|
||
|
"MBUGA1UECgwObXlvcmdhbmlzYXRpb24xCzAJBgNVBAYTAk5MMIGbMBAGByqGSM49\n"\
|
||
|
"AgEGBSuBBAAjA4GGAAQBX97tp/mdmO4sNycHxVY4oSQ8mi6446I04MPSV7md5g6Q\n"\
|
||
|
"vkW5rb36MDtD5OjT5fC8Xqgn99fa4bHDLYUENTaHLAYAFpGgMFoWpkVtwgb5oIqM\n"\
|
||
|
"/QYehE5BO/8ydVEr76DH15BqjmSNDCnoSMCxqRpC9ibHmCp5zYf1HuXx86c38J3K\n"\
|
||
|
"X+KjUzBRMA8GA1UdEwQIMAYBAf8CAQAwHQYDVR0OBBYEFPq3svLwKboIsDWdsSyi\n"\
|
||
|
"Ay/Jw3HQMB8GA1UdIwQYMBaAFPq3svLwKboIsDWdsSyiAy/Jw3HQMAwGCCqGSM49\n"\
|
||
|
"BAMCBQADgYwAMIGIAkIB5yPjh42zT9dkWKthaW5Sf7EFs5yQnadfhVnnyqEho2Bt\n"\
|
||
|
"7KkRrI5/+QH0bZ98JeNvtJimAMXXxP2/uSY1pa+es/ACQgG6MbUQOLvxMyIevF5i\n"\
|
||
|
"PkZrwhm8VfpAmwPIU/6xzcpry8wBbCm/vro1oTdQdGdOUlSSlTQ8QFBCqwMxVt9T\n"\
|
||
|
"1RDiwg==\n"\
|
||
|
"-----END CERTIFICATE-----\n");
|
||
|
serial.close();
|
||
|
|
||
|
serial = SPIFFS.open("/cert.req",FILE_WRITE);
|
||
|
serial.print("-----BEGIN CERTIFICATE REQUEST-----\n"\
|
||
|
"MIIBlDCB9AIBADBPMREwDwYDVQQDDAhlc3AzMi5zazEUMBIGA1UECgwLRXhhbXBs\n"\
|
||
|
"ZSBMdGQxJDAiBgNVBAYTG1VLIG91dHB1dF9maWxlPWVzcDMyLnNrLnJlcTCBmzAQ\n"\
|
||
|
"BgcqhkjOPQIBBgUrgQQAIwOBhgAEAV/e7af5nZjuLDcnB8VWOKEkPJouuOOiNODD\n"\
|
||
|
"0le5neYOkL5Fua29+jA7Q+To0+XwvF6oJ/fX2uGxwy2FBDU2hywGABaRoDBaFqZF\n"\
|
||
|
"bcIG+aCKjP0GHoROQTv/MnVRK++gx9eQao5kjQwp6EjAsakaQvYmx5gqec2H9R7l\n"\
|
||
|
"8fOnN/Cdyl/ioAAwDAYIKoZIzj0EAwIFAAOBjAAwgYgCQgHb3sNKKOcUOLIiCIYx\n"\
|
||
|
"wUbyDFUWMOkrZH0sRvKTyyOH+zmP7Z8rJu4pqMoR5N6a3kZ8c+3WTiiOlSb5Hvtd\n"\
|
||
|
"Ttya4gJCAT4HcYqMUBjoz5m0Pv67vIQ/tAhh6+uyX+GjuYhZJNW3NwImhWDWbofW\n"\
|
||
|
"7M4UaISadRE3K9bykB3WoGGeeqI8tI0c\n"\
|
||
|
"-----END CERTIFICATE REQUEST-----\n");
|
||
|
serial.close();
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
void read_and_remove_files(){
|
||
|
File root = SPIFFS.open("/");
|
||
|
File file = root.openNextFile();
|
||
|
|
||
|
while(file){
|
||
|
|
||
|
Serial.print("FILE: ");
|
||
|
Serial.println(file.name());
|
||
|
Serial.print(file.readString());
|
||
|
//SPIFFS.remove(file.name());
|
||
|
file = root.openNextFile();
|
||
|
|
||
|
}
|
||
|
}
|
||
|
*/
|