diff --git a/stm-firmware/Makefile b/stm-firmware/Makefile index 8e4e703..eeb54b4 100644 --- a/stm-firmware/Makefile +++ b/stm-firmware/Makefile @@ -48,6 +48,8 @@ CFILES += pid-controller.c oven-driver.c CFILES += settings/settings.c settings/settings-sd-card.c CFILES += stm-periph/crc-unit.c CFILES += safety/safety-adc.c safety/safety-controller.c safety/watchdog.c safety/fault.c safety/safety-memory.c safety/stack-check.c +CFILES += config-parser/config-parser.c +INCLUDEPATH += -Iconfig-parser/include DEBUG_DEFINES = -DDEBUGBUILD RELEASE_DEFINES = diff --git a/stm-firmware/config-parser/config-parser.c b/stm-firmware/config-parser/config-parser.c new file mode 100644 index 0000000..740173b --- /dev/null +++ b/stm-firmware/config-parser/config-parser.c @@ -0,0 +1,92 @@ +/* Reflow Oven Controller + * + * Copyright (C) 2020 Mario Hüttel + * + * This file is part of the Reflow Oven Controller Project. + * + * The reflow oven controller is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * The Reflow Oven Control Firmware is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the reflow oven controller project. + * If not, see . + */ + +/** + * @addtogroup config-parser + * @{ + */ + +#include +#include +#include + +#define CONFIG_PARSER_MAGIC 0x464a6e2bUL +#define CONFIG_PARSER(p) ((struct config_parser *)(p)) + +#define config_parser_check_handle(handle) do { if (!(handle) || \ + ((struct config_parser *)(handle))->magic != CONFIG_PARSER_MAGIC) \ + return CONFIG_PARSER_PARAM_ERR; \ + } while (0) + +config_parser_handle_t config_parser_open_file(struct config_parser *config_parser, bool write, const char *file_name, + char *working_buffer) +{ + FRESULT res; + + if (!config_parser || !file_name || !working_buffer) + return NULL; + + config_parser->magic = CONFIG_PARSER_MAGIC; + config_parser->write = write; + config_parser->buffer = working_buffer; + + res = f_open(&config_parser->file, file_name, (write ? FA_CREATE_ALWAYS | FA_WRITE : FA_READ)); + if (res != FR_OK) + return NULL; + + return (config_parser_handle_t)config_parser; +} + +enum config_parser_ret config_parser_get_line(config_parser_handle_t handle, struct config_parser_entry *entry) +{ + (void)entry; + config_parser_check_handle(handle); + + return CONFIG_PARSER_OK; +} + +enum config_parser_ret config_parser_reset_to_start(config_parser_handle_t handle) +{ + config_parser_check_handle(handle); + + return CONFIG_PARSER_OK; +} + +enum config_parser_ret config_parser_write_entry(config_parser_handle_t handle, const struct config_parser_entry *entry) +{ + (void)entry; + config_parser_check_handle(handle); + + return CONFIG_PARSER_OK; +} + +enum config_parser_ret config_parser_close_file(config_parser_handle_t handle) +{ + struct config_parser *p; + FRESULT res; + config_parser_check_handle(handle); + p = CONFIG_PARSER(handle); + + res = f_close(&p->file); + + return (res == FR_OK ? CONFIG_PARSER_OK : CONFIG_PARSER_IOERR); +} + +/** @} */ diff --git a/stm-firmware/config-parser/include/config-parser/config-parser.h b/stm-firmware/config-parser/include/config-parser/config-parser.h new file mode 100644 index 0000000..cf11ba9 --- /dev/null +++ b/stm-firmware/config-parser/include/config-parser/config-parser.h @@ -0,0 +1,94 @@ +/* Reflow Oven Controller + * + * Copyright (C) 2020 Mario Hüttel + * + * This file is part of the Reflow Oven Controller Project. + * + * The reflow oven controller is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * The Reflow Oven Control Firmware is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the reflow oven controller project. + * If not, see . + */ + +/** + * @file config-parser.h + * @brief Header file for the key-value pair config parser + * @addtogroup config-parser + * @{ + */ + +#ifndef _CONFIG_PARSER_H_ +#define _CONFIG_PARSER_H_ + +#include +#include +#include + +struct config_parser { + uint32_t magic; + bool write; + FIL file; + char *buffer; +}; + +typedef void * config_parser_handle_t; + +enum config_parser_value_type { + CONFIG_PARSER_TYPE_UINT = 0, + CONFIG_PARSER_TYPE_INT, + CONFIG_PARSER_TYPE_STRING, + CONFIG_PARSER_TYPE_FLOAT, +}; + +enum config_parser_ret { + CONFIG_PARSER_OK = 0, + CONFIG_PARSER_PARAM_ERR, + CONFIG_PARSER_GENERIC_ERR, + CONFIG_PARSER_IOERR, + CONFIG_PARSER_LINE_TOO_LONG, + CONFIG_PARSER_LINE_MALFORM, + CONFIG_PARSER_END_REACHED, + CONFIG_PARSER_WRONG_MODE, +}; + +struct config_parser_entry { + const char *name; + enum config_parser_value_type type; + union { + uint32_t uint_val; + int32_t int_val; + const char *string_val; + float float_val; + } value; +}; + +config_parser_handle_t config_parser_open_file(struct config_parser *config_parser, bool write, const char *file_name, + char *working_buffer); + +/** + * @brief Parse the current line in the config file. + * @param handle Config parser handle + * @param[out] entry Entry read from config file. + * @warning \p entry is only valid as long as no other function was called on the same \p handle. If necessary, the values + * have to be copied + * @return Config parser error + */ +enum config_parser_ret config_parser_get_line(config_parser_handle_t handle, struct config_parser_entry *entry); + +enum config_parser_ret config_parser_reset_to_start(config_parser_handle_t handle); + +enum config_parser_ret config_parser_write_entry(config_parser_handle_t handle, const struct config_parser_entry *entry); + +enum config_parser_ret config_parser_close_file(config_parser_handle_t handle); + +#endif /* _CONFIG_PARSER_H_ */ + +/** @} */