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

@@ -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);
@@ -91,24 +101,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]) {
@@ -122,21 +125,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;
}
@@ -154,16 +151,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;
}