1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

node_type: add new function for parsing node configuration from CLI

This commit is contained in:
Steffen Vogel 2017-07-13 01:55:06 +02:00
parent c1148b11e6
commit 4b51c4dbba
4 changed files with 91 additions and 4 deletions

View file

@ -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);
/** @} */
/** @} */

View file

@ -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.
*

View file

@ -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);

View file

@ -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,