mirror of
https://gitlab.com/mobian1/eg25-manager.git
synced 2025-08-31 00:02:18 +02:00
src: don't crash on incomplete user-edited config file
If a user created a custom config file and we added new fields in a newer version, `eg25-manager` will crash by assuming all config files are complete and up-to-date. Moreover, creating a custom config, even to change only one option, required copying a complete default config file and then editing the relevant field(s). This could lead to issues when upgrading, as default values of fields unrelated to the user change could be modified and not applied to the user config. This patch addresses both these issues by: * making sure at least one config file exists * requiring only the "default" config file (the one under `/usr/share`) to include all the required fields * trying to use user config for each field, falling back to the default config file if the field isn't present in the user config * exiting with a meaningful error message in case the default config file is missing a required field or section That way, it will be possible to have a minimal user config file containing only the field(s) needing a different value than the default one, falling back to the values in the default config file. Fixes #23
This commit is contained in:
committed by
Arnaud Ferraris
parent
55ed2dc39c
commit
4c6625a38d
67
src/gpio.c
67
src/gpio.c
@@ -4,6 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "gpio.h"
|
||||
|
||||
#include <unistd.h>
|
||||
@@ -13,8 +14,6 @@
|
||||
|
||||
#define MAX_GPIOCHIP_LINES 352
|
||||
|
||||
#define GPIO_IDX_INVAL 0xffff
|
||||
|
||||
enum {
|
||||
GPIO_OUT_DTR = 0,
|
||||
GPIO_OUT_PWRKEY,
|
||||
@@ -24,12 +23,23 @@ enum {
|
||||
GPIO_OUT_COUNT
|
||||
};
|
||||
|
||||
|
||||
enum {
|
||||
GPIO_IN_STATUS = 0,
|
||||
GPIO_IN_COUNT
|
||||
};
|
||||
|
||||
static char *gpio_out_names[] = {
|
||||
"dtr",
|
||||
"pwrkey",
|
||||
"reset",
|
||||
"apready",
|
||||
"disable",
|
||||
};
|
||||
|
||||
static char *gpio_in_names[] = {
|
||||
"status",
|
||||
};
|
||||
|
||||
int gpio_sequence_poweron(struct EG25Manager *manager)
|
||||
{
|
||||
gpiod_line_set_value(manager->gpio_out[GPIO_OUT_PWRKEY], 1);
|
||||
@@ -89,24 +99,17 @@ int gpio_sequence_sleep(struct EG25Manager *manager)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static guint get_config_gpio(toml_table_t *config, const char *id)
|
||||
{
|
||||
toml_datum_t value = toml_int_in(config, id);
|
||||
guint gpio;
|
||||
|
||||
if (!value.ok)
|
||||
return GPIO_IDX_INVAL;
|
||||
|
||||
gpio = (guint)value.u.i;
|
||||
|
||||
return gpio;
|
||||
}
|
||||
|
||||
int gpio_init(struct EG25Manager *manager, toml_table_t *config)
|
||||
int gpio_init(struct EG25Manager *manager, toml_table_t *config[])
|
||||
{
|
||||
int i, ret;
|
||||
guint gpio_out_idx[GPIO_OUT_COUNT];
|
||||
guint gpio_in_idx[GPIO_IN_COUNT];
|
||||
guint offset, chipidx, gpio_idx;
|
||||
toml_table_t *gpio_config[EG25_CONFIG_COUNT];
|
||||
|
||||
for (i = 0; i < EG25_CONFIG_COUNT; i++)
|
||||
gpio_config[i] = config[i] ? toml_table_in(config[i], "gpio") : NULL;
|
||||
|
||||
if (!gpio_config[EG25_CONFIG_SYS])
|
||||
g_error("Default config file lacks the 'gpio' section!");
|
||||
|
||||
manager->gpiochip[0] = gpiod_chip_open_by_label(GPIO_CHIP1_LABEL);
|
||||
if (!manager->gpiochip[0]) {
|
||||
@@ -120,21 +123,15 @@ int gpio_init(struct EG25Manager *manager, toml_table_t *config)
|
||||
return 1;
|
||||
}
|
||||
|
||||
gpio_out_idx[GPIO_OUT_DTR] = get_config_gpio(config, "dtr");
|
||||
gpio_out_idx[GPIO_OUT_PWRKEY] = get_config_gpio(config, "pwrkey");
|
||||
gpio_out_idx[GPIO_OUT_RESET] = get_config_gpio(config, "reset");
|
||||
gpio_out_idx[GPIO_OUT_APREADY] = get_config_gpio(config, "apready");
|
||||
gpio_out_idx[GPIO_OUT_DISABLE] = get_config_gpio(config, "disable");
|
||||
gpio_in_idx[GPIO_IN_STATUS] = get_config_gpio(config, "status");
|
||||
|
||||
for (i = 0; i < GPIO_OUT_COUNT; i++) {
|
||||
guint offset, chipidx;
|
||||
if (!config_get_uint(gpio_config, gpio_out_names[i], &gpio_idx))
|
||||
g_error("Unable to get config for output GPIO '%s'", gpio_out_names[i]);
|
||||
|
||||
if (gpio_out_idx[i] < MAX_GPIOCHIP_LINES) {
|
||||
offset = gpio_out_idx[i];
|
||||
if (gpio_idx < MAX_GPIOCHIP_LINES) {
|
||||
offset = gpio_idx;
|
||||
chipidx = 0;
|
||||
} else {
|
||||
offset = gpio_out_idx[i] - MAX_GPIOCHIP_LINES;
|
||||
offset = gpio_idx - MAX_GPIOCHIP_LINES;
|
||||
chipidx = 1;
|
||||
}
|
||||
|
||||
@@ -152,16 +149,14 @@ int gpio_init(struct EG25Manager *manager, toml_table_t *config)
|
||||
}
|
||||
|
||||
for (i = 0; i < GPIO_IN_COUNT; i++) {
|
||||
guint offset, chipidx;
|
||||
|
||||
if (gpio_in_idx[i] == GPIO_IDX_INVAL)
|
||||
if (!config_get_uint(gpio_config, gpio_in_names[i], &gpio_idx))
|
||||
continue;
|
||||
|
||||
if (gpio_in_idx[i] < MAX_GPIOCHIP_LINES) {
|
||||
offset = gpio_in_idx[i];
|
||||
if (gpio_idx < MAX_GPIOCHIP_LINES) {
|
||||
offset = gpio_idx;
|
||||
chipidx = 0;
|
||||
} else {
|
||||
offset = gpio_in_idx[i] - MAX_GPIOCHIP_LINES;
|
||||
offset = gpio_idx - MAX_GPIOCHIP_LINES;
|
||||
chipidx = 1;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user