Allocate necessary memory for struct members

This commit is contained in:
2023-04-11 07:35:27 +02:00
parent 92eeb0a638
commit 36c83f8dee
5 changed files with 45 additions and 57 deletions

View File

@@ -1,7 +1,19 @@
#include "config.h"
#include <ArduinoJson.h>
bool load_config_file(const char *file_path, struct Config &config) {
void initialize_config(Config *config, StaticJsonDocument<512> json) {
config->ssid = strdup(json["ssid"]);
config->psk = strdup(json["psk"]);
config->mqtt_host = strdup(json["mqtt_host"]);
config->mqtt_user = strdup(json["mqtt_user"]);
config->mqtt_password = strdup(json["mqtt_password"]);
config->topic = strdup(json["mqtt_topic"]);
config->device_id = strdup(json["device_id"]);
config->mqtt_port = json["mqtt_port"];
config->sleep_time = minutes_to_microseconds(json["sleep_time"]);
config->connection_attempts = json["connection_attempts"];
}
bool load_config_file(const char *file_path, Config *config) {
if (!LittleFS.begin())
return false;
File config_file = LittleFS.open(file_path, "r");
@@ -11,18 +23,7 @@ bool load_config_file(const char *file_path, struct Config &config) {
DeserializationError err = deserializeJson(json, config_file);
if (err)
return false;
config = {
.ssid = json["ssid"],
.psk = json["psk"],
.mqtt_host = json["mqtt_host"],
.mqtt_user = json["mqtt_user"],
.mqtt_password = json["mqtt_password"],
.mqtt_port = json["mqtt_port"],
.topic = json["mqtt_topic"],
.device_id = json["device_id"],
.sleep_time = minutes_to_microseconds(json["sleep_time"]),
.connection_attempts = json["connection_attempts"],
};
initialize_config(config, json);
return true;
}