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

109 lines
2.3 KiB
C
Raw Permalink Normal View History

2018-03-21 16:59:19 +01:00
/** Signal meta data.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @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 <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <string.h>
#include "villas/signal.h"
#include "villas/list.h"
#include "villas/utils.h"
#include "villas/node.h"
2018-03-21 16:59:19 +01:00
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;
2018-04-09 10:52:54 +02:00
const char *unit = NULL;
2018-03-21 16:59:19 +01:00
2018-05-25 12:55:01 +02:00
/* Default values */
s->enabled = true;
ret = json_unpack_ex(cfg, &err, 0, "{ s: s, s?: s, s?: b }",
2018-04-09 10:52:54 +02:00
"name", &name,
2018-05-25 12:55:01 +02:00
"unit", &unit,
"enabled", &s->enabled
2018-03-21 16:59:19 +01:00
);
if (ret)
return -1;
s->name = strdup(name);
2018-04-09 10:52:54 +02:00
s->unit = unit
? strdup(unit)
: NULL;
2018-03-21 16:59:19 +01:00
return 0;
}
int signal_parse_list(struct list *list, json_t *cfg)
{
int ret;
2018-05-25 12:55:01 +02:00
struct signal *s;
2018-03-21 16:59:19 +01:00
if (!json_is_array(cfg))
return -1;
size_t index;
json_t *json_signal;
json_array_foreach(cfg, index, json_signal) {
2018-05-25 12:55:01 +02:00
s = alloc(sizeof(struct signal));
if (!s)
return -1;
2018-03-21 16:59:19 +01:00
ret = signal_parse(s, json_signal);
if (ret)
return -1;
list_push(list, s);
}
return 0;
}
int signal_get_offsets(const char *str, struct list *sigs)
{
int idx;
char *endptr;
struct signal *s;
/* Lets try to find a signal with a matching name */
if (1) {
s = list_lookup(sigs, str);
if (s)
return list_index(sigs, s);
}
/* Lets try to interpret the signal name as an index */
idx = strtoul(str, &endptr, 10);
if (endptr == str + strlen(str))
return idx;
return -1;
}