Merge branch 'fix-config-crash' into 'master'

Don't crash on incomplete config files

Closes #23

See merge request mobian1/devices/eg25-manager!31
This commit is contained in:
Arnaud Ferraris
2021-10-05 20:34:28 +00:00
19 changed files with 275 additions and 158 deletions

View File

@@ -5,6 +5,7 @@
*/
#include "at.h"
#include "config.h"
#include "suspend.h"
#include "gpio.h"
#include "gnss.h"
@@ -314,67 +315,61 @@ static void parse_commands_list(toml_array_t *array, GArray **cmds)
continue;
value = toml_string_in(table, "cmd");
if (value.ok) {
cmd->cmd = g_strdup(value.u.s);
free(value.u.s);
}
if (value.ok)
cmd->cmd = value.u.s;
value = toml_string_in(table, "subcmd");
if (value.ok) {
cmd->subcmd = g_strdup(value.u.s);
free(value.u.s);
}
if (value.ok)
cmd->subcmd = value.u.s;
value = toml_string_in(table, "value");
if (value.ok) {
cmd->value = g_strdup(value.u.s);
free(value.u.s);
}
if (value.ok)
cmd->value = value.u.s;
value = toml_string_in(table, "expect");
if (value.ok) {
cmd->expected = g_strdup(value.u.s);
free(value.u.s);
}
if (value.ok)
cmd->expected = value.u.s;
}
}
int at_init(struct EG25Manager *manager, toml_table_t *config)
int at_init(struct EG25Manager *manager, toml_table_t *config[])
{
toml_array_t *commands;
toml_datum_t uart_port;
toml_array_t *commands = NULL;
gchar *uart_port = NULL;
toml_table_t *at_config[EG25_CONFIG_COUNT];
uart_port = toml_string_in(config, "uart");
if (!uart_port.ok)
for (int i = 0; i < EG25_CONFIG_COUNT; i++)
at_config[i] = config[i] ? toml_table_in(config[i], "at") : NULL;
if (!at_config[EG25_CONFIG_SYS])
g_error("Default config file lacks the 'at' section!");
if (!config_get_string(at_config, "uart", &uart_port))
g_error("Configuration file lacks UART port definition");
manager->at_fd = configure_serial(uart_port.u.s);
manager->at_fd = configure_serial(uart_port);
if (manager->at_fd < 0) {
g_critical("Unable to configure %s", uart_port.u.s);
free(uart_port.u.s);
g_critical("Unable to configure %s", uart_port);
g_free(uart_port);
return 1;
}
free(uart_port.u.s);
g_free(uart_port);
manager->at_source = g_unix_fd_add(manager->at_fd, G_IO_IN, modem_response, manager);
commands = toml_array_in(config, "configure");
if (!commands)
if (!config_get_array(at_config, "configure", &commands))
g_error("Configuration file lacks initial AT commands list");
parse_commands_list(commands, &configure_commands);
commands = toml_array_in(config, "suspend");
if (!commands)
if (!config_get_array(at_config, "suspend", &commands))
g_error("Configuration file lacks suspend AT commands list");
parse_commands_list(commands, &suspend_commands);
commands = toml_array_in(config, "resume");
if (!commands)
if (!config_get_array(at_config, "resume", &commands))
g_error("Configuration file lacks resume AT commands list");
parse_commands_list(commands, &resume_commands);
commands = toml_array_in(config, "reset");
if (!commands)
if (!config_get_array(at_config, "reset", &commands))
g_error("Configuration file lacks reset AT commands list");
parse_commands_list(commands, &reset_commands);