Hallo
ich lese im rahmen meines codes ein json file aus dem filesystem eines esp8622.
Das sieht aktuell so aus (extrem reduziert):
#include <ArduinoJson.h>
#include <FS.h>
#include "userconfig.h"
// Oject keeping the configuration
typedef struct _config_struct
{
char startSleeptime[25];
} config_struct;
class ConfigClass
{
public:
ConfigClass();
~ConfigClass();
void begin();
void save();
void load();
void reset();
void print();
// setter and getter
char* getStartSleeptime();
void setStartSleeptime(char*,int);
private:
// copy of config.json content
config_struct *config = new config_struct();
const char *filename = "/config.json";
};
extern ConfigClass Config;
void ConfigClass::load()
{
File file = SPIFFS.open(filename, "r");
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, file);
if (error)
{
Serial.println("Configuration: Failed to read file, using default configuration");
this->reset();
this->save();
}
else
{
Serial.println("Configuration: Loaded file sucessfully.");
}
strlcpy(this->config->startSleeptime, doc["startSleeptime"], sizeof(this->config->startSleeptime));
file.close();
}
Das problem liegt in dieser zeile code:
strlcpy(this->config->startSleeptime, doc["startSleeptime"], sizeof(this->config->startSleeptime));
Die erzeugt einen esp stack trace, sobald KEIN "startSleeptime" in dem json object ist (was technisch möglich und okay ist)
wie kann man das sauberer/stabiler/sicherer coden? TIL: es gibt kein try/catch in der arduino spec.