diff --git a/include/villas/node.h b/include/villas/node.h index 54ce70e81..93e8f6b7c 100644 --- a/include/villas/node.h +++ b/include/villas/node.h @@ -19,10 +19,12 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * + *********************************************************************************/ + +/** * @addtogroup node Node * @{ - *********************************************************************************/ + */ #pragma once @@ -59,6 +61,7 @@ struct node struct stats *stats; /**< Statistic counters. This is a pointer to the statistic hooks private data. */ struct list hooks; /**< List of write hooks (struct hook). */ + struct list signals; /**< List of signal meta data such as signal names */ enum state state; @@ -83,6 +86,21 @@ int node_parse(struct node *n, json_t *cfg, const char *name); /** Parse settings of a node from cmdline. */ int node_parse_cli(struct node *n, int argc, char *argv[]); +/** Parse an array or single node and checks if they exist in the "nodes" section. + * + * Examples: + * out = [ "sintef", "scedu" ] + * out = "acs" + * + * @param cfg A JSON array or string. See examples above. + * @param nodes The nodes will be added to this list. + * @param all This list contains all valid nodes. + */ +int node_parse_list(struct list *list, json_t *cfg, struct list *all); + +/** Parse the list of signal definitions. */ +int node_parse_signals(struct list *list, json_t *cfg); + /** Validate node configuration. */ int node_check(struct node *n); @@ -134,16 +152,4 @@ int node_write(struct node *n, struct sample *smps[], unsigned cnt); int node_fd(struct node *n); -/** Parse an array or single node and checks if they exist in the "nodes" section. - * - * Examples: - * out = [ "sintef", "scedu" ] - * out = "acs" - * - * @param cfg A JSON array or string. See examples above. - * @param nodes The nodes will be added to this list. - * @param all This list contains all valid nodes. - */ -int node_parse_list(struct list *list, json_t *cfg, struct list *all); - /** @} */ diff --git a/include/villas/signal.h b/include/villas/signal.h new file mode 100644 index 000000000..a0d3d2610 --- /dev/null +++ b/include/villas/signal.h @@ -0,0 +1,43 @@ +/** Signal meta data. + * + * @file + * @author Steffen Vogel + * @copyright 2018, Institute for Automation of Complex Power Systems, EONERC + * @license GNU General Public License (version 3) + * + * VILLASnode + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program 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 this program. If not, see . + *********************************************************************************/ + +#pragma once + +#include + +/* Forward declarations */ +struct list; + +struct signal { + char *name; /**< The name of the signal. */ + enum { + SIGNAL_FORMAT_INTEGER, + SIGNAL_FORMAT_REAL + } format; +}; + +int signal_destroy(struct signal *s); + +int signal_parse(struct signal *s, json_t *cfg); + +int signal_parse_list(struct list *list, json_t *cfg); diff --git a/lib/Makefile.villas.inc b/lib/Makefile.villas.inc index 7e212188a..013d0bad7 100644 --- a/lib/Makefile.villas.inc +++ b/lib/Makefile.villas.inc @@ -30,7 +30,7 @@ LIB_SRCS += $(addprefix lib/kernel/, kernel.c rt.c) \ utils.c super_node.c hist.c timing.c pool.c list.c queue.c \ queue_signalled.c memory.c advio.c plugin.c node_type.c stats.c \ mapping.c shmem.c config_helper.c crypt.c compat.c \ - log_helper.c task.c buffer.c table.c bitset.c \ + log_helper.c task.c buffer.c table.c bitset.c signal.c \ ) LIB_LDFLAGS += -shared diff --git a/lib/node.c b/lib/node.c index f5d2716d5..6c228ac0e 100644 --- a/lib/node.c +++ b/lib/node.c @@ -26,11 +26,11 @@ #include #include #include -#include #include #include #include #include +#include int node_init(struct node *n, struct node_type *vt) { @@ -104,17 +104,19 @@ int node_parse(struct node *n, json_t *cfg, const char *name) json_error_t err; json_t *json_hooks = NULL; + json_t *json_signals = NULL; const char *type; n->name = strdup(name); - ret = json_unpack_ex(cfg, &err, 0, "{ s: s, s?: i, s?: i, s?: o, s?: b }", + ret = json_unpack_ex(cfg, &err, 0, "{ s: s, s?: i, s?: i, s?: o, s?: b, s?: o }", "type", &type, "vectorize", &n->vectorize, "samplelen", &n->samplelen, "hooks", &json_hooks, - "no_builtin", &n->no_builtin + "no_builtin", &n->no_builtin, + "signals", &json_signals ); if (ret) jerror(&err, "Failed to parse node '%s'", node_name(n)); @@ -130,6 +132,12 @@ int node_parse(struct node *n, json_t *cfg, const char *name) } #endif /* WITH_HOOKS */ + if (json_signals) { + ret = signal_parse_list(&n->signals, json_signals); + if (ret) + error("Failed to parse signal definition of node '%s'", node_name(n)); + } + ret = n->_vt->parse ? n->_vt->parse(n, cfg) : 0; if (ret) error("Failed to parse node '%s'", node_name(n)); @@ -253,6 +261,8 @@ int node_destroy(struct node *n) list_remove(&n->_vt->instances, n); + list_destroy(&n->signals, (dtor_cb_t) signal_destroy, true); + if (n->_vd) free(n->_vd); diff --git a/lib/signal.c b/lib/signal.c new file mode 100644 index 000000000..5513b3356 --- /dev/null +++ b/lib/signal.c @@ -0,0 +1,74 @@ +/** Signal meta data. + * + * @author Steffen Vogel + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + * @license GNU General Public License (version 3) + * + * VILLASnode + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program 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 this program. If not, see . + *********************************************************************************/ + +#include + +#include "villas/signal.h" +#include "villas/list.h" +#include "villas/utils.h" + +int signal_destroy(struct signal *s) +{ + if (s->name) + free(s->name); + + return 0; +} + +int signal_parse(struct signal *s, json_t *cfg) +{ + int ret; + json_error_t err; + const char *name; + + ret = json_unpack_ex(cfg, &err, 0, "s: s", + "name", &name + ); + if (ret) + return -1; + + s->name = strdup(name); + + return 0; +} + +int signal_parse_list(struct list *list, json_t *cfg) +{ + int ret; + + if (!json_is_array(cfg)) + return -1; + + size_t index; + json_t *json_signal; + json_array_foreach(cfg, index, json_signal) { + struct signal *s = alloc(sizeof(struct signal)); + + ret = signal_parse(s, json_signal); + if (ret) + return -1; + + list_push(list, s); + } + + return 0; +}