From 3f836d2ded0d241b50d76f5ef22e81731a686e3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilja=20Karta=C5=A1ov?= Date: Mon, 29 Jul 2019 11:31:10 +0200 Subject: [PATCH] Add and debug config module, surgard prototype --- .gitignore | 2 +- cStuff | 2 +- data/arcd.config | 2 +- project.mk | 3 +++ src/agent.c | 9 +++++---- src/config.c | 34 ++++++++++++++++++++++++++++++++++ src/config.h | 4 ++++ src/main.c | 3 +++ src/surgard.c | 25 +++++++++++++++++++++++++ src/surgard.h | 22 ++++++++++++++++++++++ 10 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 src/surgard.c create mode 100644 src/surgard.h diff --git a/.gitignore b/.gitignore index 4af0fca..c78c903 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ build/ *.rar *.gz *.bz2 -*.vgcore +vgcore.* diff --git a/cStuff b/cStuff index 17ce2c5..2f5c10c 160000 --- a/cStuff +++ b/cStuff @@ -1 +1 @@ -Subproject commit 17ce2c5753c5512f7acf5aac0d15b9587be79b7f +Subproject commit 2f5c10cf5dc8f351b08936d6136e52b4b1784c82 diff --git a/data/arcd.config b/data/arcd.config index 72f257d..4581d94 100644 --- a/data/arcd.config +++ b/data/arcd.config @@ -1,3 +1,3 @@ agent: - surgard-1 = surgard-serial:///dev/ttyUSB0/?speed=9600 + jaga-1 = surgard:///dev/ttyUSB0/?speed=9600 osm-1 = osm://127.0.0.1/ diff --git a/project.mk b/project.mk index 7f01ee9..f6db044 100644 --- a/project.mk +++ b/project.mk @@ -25,13 +25,16 @@ PROJECT_VERSION_LABEL = 0 PROJECT_SOURCES := \ $(SRC_DIR)/main.c \ + $(SRC_DIR)/config.c \ $(SRC_DIR)/observer.c \ $(SRC_DIR)/agent.c \ + $(SRC_DIR)/surgard.c \ \ $(CSTUFF_DIR)/log.c \ $(CSTUFF_DIR)/string.c \ $(CSTUFF_DIR)/list.c \ $(CSTUFF_DIR)/file.c \ + $(CSTUFF_DIR)/config.c \ \ $(SDK_DIR)/components/query.c \ $(SDK_DIR)/components/quick.c \ diff --git a/src/agent.c b/src/agent.c index 851cd5f..968b199 100644 --- a/src/agent.c +++ b/src/agent.c @@ -13,6 +13,7 @@ * @see https://lowenware.com/ */ +#include "surgard.h" #include "agent.h" @@ -33,11 +34,11 @@ agent_release(void) int agent_run_cycle(void) { - int result = 0, rc; + int result = 1, rc; - if ((rc = surgard_run_cycle())) { + if (!(rc = surgard_run_cycle())) { result = rc; } -: - return 1; + + return result; } diff --git a/src/config.c b/src/config.c index 2945f30..ffe1bab 100644 --- a/src/config.c +++ b/src/config.c @@ -14,11 +14,45 @@ */ #include +#include "log.h" #include "config.h" + +static int +config_callback(struct cstuff_config *cfg, void *u_ptr) +{ + switch(cfg->evt) { + case CSTUFF_CONFIG_NODE: + LOG_STATE("node: %s(%d)", cfg->data.node.name, cfg->data.node.name_len); + return 0; + case CSTUFF_CONFIG_PAIR: + LOG_STATE("node: %s(%d)->%s(%d)", cfg->data.pair.key, + cfg->data.pair.key_len, cfg->data.pair.val, + cfg->data.pair.val_len); + return 0; + case CSTUFF_CONFIG_ERROR: + LOG_ERROR("config error at line %d:%d - %s", cfg->line_num, + cfg->data.error.char_num, cfg->data.error.err_txt); + break; + default: + LOG_ERROR("event unknown: %d", cfg->evt); + } + + (void) u_ptr; + return -1; +} + + int config_read(void) { + const char cfg_file[] = ARC_CONFIG_FILE; + if (!cstuff_config_parse(cfg_file, config_callback, NULL)) { + return 0; + } + LOG_ERROR("configuration failed"); + + return -1; } diff --git a/src/config.h b/src/config.h index 1f9c391..1120e8c 100644 --- a/src/config.h +++ b/src/config.h @@ -16,6 +16,10 @@ #ifndef CONFIG_H_18E4DCB3_B446_424F_9E5E_06DC15FA40BA #define CONFIG_H_18E4DCB3_B446_424F_9E5E_06DC15FA40BA +#ifndef ARC_CONFIG_FILE +#define ARC_CONFIG_FILE "data/arcd.config" +#endif + /** Default HTTP server port */ #ifndef CONFIG_HTTP_PORT #define CONFIG_HTTP_PORT 8080 diff --git a/src/main.c b/src/main.c index 5e0d11d..d1932e2 100644 --- a/src/main.c +++ b/src/main.c @@ -106,6 +106,9 @@ main(int argc, char **argv) LOG_STATE("working directory: `%s`", getcwd(cwd, sizeof(cwd))); + if (config_read()) + return -1; + /* Initialize instance */ if ((aisl = aisl_new(&m_cfg)) != NULL) { if (observer_init() == 0) { diff --git a/src/surgard.c b/src/surgard.c new file mode 100644 index 0000000..b0a3125 --- /dev/null +++ b/src/surgard.c @@ -0,0 +1,25 @@ +/****************************************************************************** + * + * Copyright (c) 2017-2019 by Löwenware Ltd + * Please, refer LICENSE file for legal information + * + ******************************************************************************/ + +/** + * @file surgard.c + * @author Ilja Kartašov + * @brief + * + * @see https://lowenware.com/ + */ + +#include "surgard.h" + + +int +surgard_run_cycle(void) +{ + return 1; +} + + diff --git a/src/surgard.h b/src/surgard.h new file mode 100644 index 0000000..61b6a4b --- /dev/null +++ b/src/surgard.h @@ -0,0 +1,22 @@ +/****************************************************************************** + * + * Copyright (c) 2017-2019 by Löwenware Ltd + * Please, refer LICENSE file for legal information + * + ******************************************************************************/ + +/** + * @file surgard.h + * @author Ilja Kartašov + * @brief + * + * @see https://lowenware.com/ + */ + +#ifndef SURGARD_H_0C382373_4757_4FC1_A2DE_991F9D71CDBC +#define SURGARD_H_0C382373_4757_4FC1_A2DE_991F9D71CDBC + +int +surgard_run_cycle(void); + +#endif /* !SURGARD_H */