2018-03-21 16:59:19 +01:00
|
|
|
/** Signal meta data.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2018-03-21 16:59:19 +01:00
|
|
|
* @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/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2018-08-02 10:31:58 +02:00
|
|
|
#include <villas/signal.h>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2018-08-02 10:47:53 +02:00
|
|
|
#include <villas/mapping.h>
|
2020-07-04 16:22:10 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2018-03-21 16:59:19 +01:00
|
|
|
|
2020-08-17 17:04:55 +02:00
|
|
|
#include <villas/signal_type.h>
|
|
|
|
#include <villas/signal_data.h>
|
|
|
|
|
2019-06-23 13:35:42 +02:00
|
|
|
using namespace villas;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2018-08-01 17:00:56 +02:00
|
|
|
int signal_init(struct signal *s)
|
|
|
|
{
|
2018-08-13 15:31:39 +02:00
|
|
|
s->enabled = true;
|
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
s->name = nullptr;
|
|
|
|
s->unit = nullptr;
|
2019-06-23 16:13:23 +02:00
|
|
|
s->type = SignalType::INVALID;
|
2020-07-04 17:16:58 +02:00
|
|
|
s->init = signal_data::nan();
|
2018-08-01 17:00:56 +02:00
|
|
|
|
2018-08-06 11:14:45 +02:00
|
|
|
s->refcnt = ATOMIC_VAR_INIT(1);
|
|
|
|
|
2018-08-01 17:00:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-02 10:47:53 +02:00
|
|
|
int signal_init_from_mapping(struct signal *s, const struct mapping_entry *me, unsigned index)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = signal_init(s);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2020-09-10 11:15:33 +02:00
|
|
|
ret = mapping_entry_to_str(me, index, &s->name);
|
2018-08-02 10:47:53 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (me->type) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case MappingType::STATS:
|
2019-06-23 13:35:42 +02:00
|
|
|
s->type = Stats::types[me->stats.type].signal_type;
|
2018-08-02 10:47:53 +02:00
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case MappingType::HEADER:
|
2018-08-13 15:31:39 +02:00
|
|
|
switch (me->header.type) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case MappingHeaderType::LENGTH:
|
|
|
|
case MappingHeaderType::SEQUENCE:
|
|
|
|
s->type = SignalType::INTEGER;
|
2018-08-02 10:47:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case MappingType::TIMESTAMP:
|
|
|
|
s->type = SignalType::INTEGER;
|
2018-08-02 10:47:53 +02:00
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case MappingType::DATA:
|
2019-04-07 15:13:40 +02:00
|
|
|
s->type = me->data.signal->type;
|
|
|
|
s->init = me->data.signal->init;
|
|
|
|
s->enabled = me->data.signal->enabled;
|
|
|
|
|
|
|
|
if (me->data.signal->name)
|
|
|
|
s->name = strdup(me->data.signal->name);
|
|
|
|
|
|
|
|
if (me->data.signal->unit)
|
|
|
|
s->name = strdup(me->data.signal->unit);
|
2018-08-02 10:47:53 +02:00
|
|
|
break;
|
2020-09-10 13:22:33 +02:00
|
|
|
|
|
|
|
case MappingType::UNKNOWN:
|
|
|
|
return -1;
|
2018-08-02 10:47:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-21 16:59:19 +01:00
|
|
|
int signal_destroy(struct signal *s)
|
|
|
|
{
|
|
|
|
if (s->name)
|
|
|
|
free(s->name);
|
|
|
|
|
2018-08-13 15:31:39 +02:00
|
|
|
if (s->unit)
|
|
|
|
free(s->unit);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
struct signal * signal_create(const char *name, const char *unit, enum SignalType fmt)
|
2018-08-13 15:31:39 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct signal *sig;
|
|
|
|
|
2020-01-21 16:26:51 +01:00
|
|
|
sig = new struct signal;
|
2018-08-13 15:31:39 +02:00
|
|
|
if (!sig)
|
2020-07-04 16:22:10 +02:00
|
|
|
throw MemoryAllocationError();
|
2018-08-13 15:31:39 +02:00
|
|
|
|
|
|
|
ret = signal_init(sig);
|
|
|
|
if (ret)
|
2019-04-07 15:13:40 +02:00
|
|
|
return nullptr;
|
2018-08-13 15:31:39 +02:00
|
|
|
|
|
|
|
if (name)
|
|
|
|
sig->name = strdup(name);
|
|
|
|
|
|
|
|
if (unit)
|
|
|
|
sig->unit = strdup(unit);
|
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
sig->type = fmt;
|
2018-08-13 15:31:39 +02:00
|
|
|
|
|
|
|
return sig;
|
|
|
|
}
|
|
|
|
|
|
|
|
int signal_free(struct signal *s)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = signal_destroy(s);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2020-01-21 16:26:51 +01:00
|
|
|
delete s;
|
2018-08-13 15:31:39 +02:00
|
|
|
|
2018-03-21 16:59:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-07 09:22:26 +02:00
|
|
|
int signal_incref(struct signal *s)
|
2018-08-06 11:14:45 +02:00
|
|
|
{
|
|
|
|
return atomic_fetch_add(&s->refcnt, 1) + 1;
|
|
|
|
}
|
|
|
|
|
2018-08-07 09:22:26 +02:00
|
|
|
int signal_decref(struct signal *s)
|
2018-08-06 11:14:45 +02:00
|
|
|
{
|
|
|
|
int prev = atomic_fetch_sub(&s->refcnt, 1);
|
|
|
|
|
|
|
|
/* Did we had the last reference? */
|
2020-09-10 11:11:42 +02:00
|
|
|
if (prev == 1) {
|
|
|
|
int ret = signal_free(s);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to release sample");
|
|
|
|
}
|
2018-08-06 11:14:45 +02:00
|
|
|
|
|
|
|
return prev - 1;
|
|
|
|
}
|
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
struct signal * signal_copy(struct signal *s)
|
|
|
|
{
|
2020-09-10 11:11:42 +02:00
|
|
|
int ret;
|
2018-08-20 18:31:27 +02:00
|
|
|
struct signal *ns;
|
|
|
|
|
2020-01-21 16:26:51 +01:00
|
|
|
ns = new struct signal;
|
2018-08-20 18:31:27 +02:00
|
|
|
if (!ns)
|
2020-07-04 16:22:10 +02:00
|
|
|
throw MemoryAllocationError();
|
2018-08-20 18:31:27 +02:00
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
ret = signal_init(ns);
|
|
|
|
if (!ret)
|
|
|
|
throw RuntimeError("Failed to initialize signal");
|
2018-08-20 18:31:27 +02:00
|
|
|
|
|
|
|
ns->type = s->type;
|
|
|
|
ns->init = s->init;
|
|
|
|
ns->enabled = s->enabled;
|
|
|
|
|
|
|
|
if (s->name)
|
|
|
|
ns->name = strdup(s->name);
|
|
|
|
|
|
|
|
if (s->unit)
|
|
|
|
ns->name = strdup(s->unit);
|
|
|
|
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
int signal_parse(struct signal *s, json_t *json)
|
2018-03-21 16:59:19 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
2019-04-07 15:13:40 +02:00
|
|
|
json_t *json_init = nullptr;
|
|
|
|
const char *name = nullptr;
|
|
|
|
const char *unit = nullptr;
|
2020-08-17 12:51:30 +02:00
|
|
|
const char *type = "float";
|
2021-06-19 13:09:02 -04:00
|
|
|
int en = -1;
|
2018-03-21 16:59:19 +01:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s?: s, s?: s, s?: o, s?: b }",
|
2018-04-09 10:52:54 +02:00
|
|
|
"name", &name,
|
2018-05-25 12:55:01 +02:00
|
|
|
"unit", &unit,
|
2018-08-20 18:31:27 +02:00
|
|
|
"type", &type,
|
2018-08-13 15:31:39 +02:00
|
|
|
"init", &json_init,
|
2021-06-19 13:09:02 -04:00
|
|
|
"enabled", &en
|
2018-03-21 16:59:19 +01:00
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
return -1;
|
|
|
|
|
2018-08-13 15:31:39 +02:00
|
|
|
if (name)
|
|
|
|
s->name = strdup(name);
|
|
|
|
|
|
|
|
if (unit)
|
|
|
|
s->unit = strdup(unit);
|
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
if (type) {
|
|
|
|
s->type = signal_type_from_str(type);
|
2019-06-23 16:13:23 +02:00
|
|
|
if (s->type == SignalType::INVALID)
|
2018-08-13 15:31:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_init) {
|
2021-02-19 06:37:21 +01:00
|
|
|
ret = signal_data_parse_json(&s->init, s->type, json_init);
|
2018-08-13 15:31:39 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
2021-02-19 06:37:21 +01:00
|
|
|
signal_data_set(&s->init, s->type, 0);
|
2018-03-21 16:59:19 +01:00
|
|
|
|
2021-06-19 13:09:02 -04:00
|
|
|
if (en >= 0)
|
2021-06-25 09:09:11 -04:00
|
|
|
s->enabled = en != 0;
|
2021-06-19 13:09:02 -04:00
|
|
|
|
2018-03-21 16:59:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-08-17 17:04:55 +02:00
|
|
|
|
|
|
|
json_t * signal_to_json(struct signal *s)
|
|
|
|
{
|
|
|
|
json_t *json_sig = json_pack("{ s: s, s: b, s: o }",
|
|
|
|
"type", signal_type_to_str(s->type),
|
|
|
|
"enabled", s->enabled,
|
2021-02-19 06:37:21 +01:00
|
|
|
"init", signal_data_to_json(&s->init, s->type)
|
2020-08-17 17:04:55 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (s->name)
|
|
|
|
json_object_set(json_sig, "name", json_string(s->name));
|
|
|
|
|
|
|
|
if (s->unit)
|
|
|
|
json_object_set(json_sig, "unit", json_string(s->unit));
|
|
|
|
|
|
|
|
return json_sig;
|
|
|
|
}
|