diff --git a/include/villas/node.h b/include/villas/node.h index 04d113624..9d99ff9c4 100644 --- a/include/villas/node.h +++ b/include/villas/node.h @@ -65,15 +65,17 @@ struct node int node_init(struct node *n, struct node_type *vt); -/** Parse a single node and add it to the global configuration. +/** Parse settings of a node. * * @param cfg A libconfig object pointing to the node. - * @param nodes Add new nodes to this linked list. * @retval 0 Success. Everything went well. * @retval <0 Error. Something went wrong. */ int node_parse(struct node *n, config_setting_t *cfg); +/** Parse settings of a node from cmdline. */ +int node_parse_cli(struct node *n, int argc, char *argv[]); + /** Validate node configuration. */ int node_check(struct node *n); @@ -135,4 +137,4 @@ int node_write(struct node *n, struct sample *smps[], unsigned cnt); */ int node_parse_list(struct list *list, config_setting_t *cfg, struct list *all); -/** @} */ \ No newline at end of file +/** @} */ diff --git a/include/villas/node_type.h b/include/villas/node_type.h index 2beef8e85..b728349fb 100644 --- a/include/villas/node_type.h +++ b/include/villas/node_type.h @@ -75,7 +75,7 @@ struct node_type { */ int (*destroy)(struct node *n); - /** Parse node connection details.‚ + /** Parse node connection details. * * @param n A pointer to the node object. * @param cfg A libconfig object pointing to the node. @@ -83,6 +83,9 @@ struct node_type { * @retval <0 Error. Something went wrong. */ int (*parse)(struct node *n, config_setting_t *cfg); + + /** Parse node from command line arguments. */ + int (*parse_cli)(struct node *n, int argc, char *argv[]); /** Returns a string with a textual represenation of this node. * diff --git a/lib/node.c b/lib/node.c index 9770a49f0..69b57a073 100644 --- a/lib/node.c +++ b/lib/node.c @@ -78,6 +78,24 @@ int node_parse(struct node *n, config_setting_t *cfg) return ret; } +int node_parse_cli(struct node *n, int argc, char *argv[]) +{ + int ret; + + assert(n->_vt); + + n->vectorize = 1; + n->name = "cli"; + + ret = n->_vt->parse_cli ? n->_vt->parse_cli(n, argc, argv) : 0; + if (ret) + error("Failed to parse node '%s'", node_name(n)); + + n->state = STATE_PARSED; + + return ret; +} + int node_check(struct node *n) { assert(n->state != STATE_DESTROYED); diff --git a/lib/nodes/signal.c b/lib/nodes/signal.c index ec1bc2a4c..2994c5f4c 100644 --- a/lib/nodes/signal.c +++ b/lib/nodes/signal.c @@ -83,6 +83,69 @@ int signal_parse(struct node *n, config_setting_t *cfg) return 0; } +int signal_parse_cli(struct node *n, int argc, char *argv[]) +{ + char *type; + + struct signal *s = n->_vd; + + /* Default values */ + s->rate = 10; + s->frequency = 1; + s->amplitude = 1; + s->stddev = 0.02; + s->type = SIGNAL_TYPE_MIXED; + s->rt = 1; + s->values = 1; + s->limit = -1; + + /* Parse optional command line arguments */ + char c, *endptr; + while ((c = getopt(argc, argv, "v:r:f:l:a:D:n")) != -1) { + switch (c) { + case 'n': + s->rt = 0; + break; + case 'l': + s->limit = strtoul(optarg, &endptr, 10); + goto check; + case 'v': + s->values = strtoul(optarg, &endptr, 10); + goto check; + case 'r': + s->rate = strtof(optarg, &endptr); + goto check; + case 'f': + s->frequency = strtof(optarg, &endptr); + goto check; + case 'a': + s->amplitude = strtof(optarg, &endptr); + goto check; + case 'D': + s->stddev = strtof(optarg, &endptr); + goto check; + case '?': + break; + } + + continue; + +check: if (optarg == endptr) + error("Failed to parse parse option argument '-%c %s'", c, optarg); + } + + if (argc != optind + 1) + return -1; + + type = argv[optind]; + + s->type = signal_lookup_type(type); + if (s->type == -1) + error("Invalid signal type: %s", type); + + return 0; +} + int signal_open(struct node *n) { struct signal *s = n->_vd; @@ -199,6 +262,7 @@ static struct plugin p = { .vectorize = 1, .size = sizeof(struct signal), .parse = signal_parse, + .parse_cli = signal_parse_cli, .print = signal_print, .start = signal_open, .stop = signal_close,