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/mapping.cpp

415 lines
9.1 KiB
C++
Raw Permalink Normal View History

/** Sample value remapping for mux.
*
* @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
2017-04-27 12:56:43 +02: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.
*
2017-04-27 12:56:43 +02:00
* 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.
*
2017-04-27 12:56:43 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
2020-09-10 11:15:33 +02:00
#include <regex>
#include <iostream>
2017-12-09 02:19:28 +08:00
#include <villas/mapping.h>
#include <villas/sample.h>
#include <villas/list.h>
#include <villas/utils.hpp>
#include <villas/exceptions.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/node.h>
#include <villas/signal.h>
2019-06-23 13:35:42 +02:00
using namespace villas;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
2020-09-10 11:15:33 +02:00
int mapping_entry_parse_str(struct mapping_entry *me, const std::string &str)
{
2020-09-10 11:15:33 +02:00
std::smatch mr;
std::regex re(RE_MAPPING);
2020-09-10 11:15:33 +02:00
if (!std::regex_match(str, mr, re))
goto invalid_format;
2017-09-02 14:20:38 +02:00
2020-09-10 11:15:33 +02:00
if (mr[1].matched)
me->node_name = strdup(mr.str(1).c_str());
2020-09-10 13:22:33 +02:00
2020-09-10 11:15:33 +02:00
if (mr[9].matched)
me->node_name = strdup(mr.str(9).c_str());
2017-09-02 14:20:38 +02:00
2020-09-10 11:15:33 +02:00
if (mr[6].matched) {
me->data.first = strdup(mr.str(6).c_str());
2020-09-10 13:22:33 +02:00
me->data.last = mr[7].matched
? strdup(mr.str(7).c_str())
: nullptr;
2017-09-02 14:20:38 +02:00
2020-09-10 11:15:33 +02:00
me->type = MappingType::DATA;
}
2020-09-10 11:15:33 +02:00
else if (mr[10].matched) {
me->data.first = strdup(mr.str(10).c_str());
2020-09-10 13:22:33 +02:00
me->data.last = mr[11].matched
? strdup(mr.str(11).c_str())
: nullptr;
2017-09-02 14:20:38 +02:00
2020-09-10 11:15:33 +02:00
me->type = MappingType::DATA;
}
2020-09-10 11:15:33 +02:00
else if (mr[8].matched) {
me->data.first = strdup(mr.str(8).c_str());
2020-09-10 13:22:33 +02:00
me->data.last = nullptr;
2020-09-10 11:15:33 +02:00
me->type = MappingType::DATA;
}
2020-09-10 11:15:33 +02:00
else if (mr[2].matched) {
me->stats.type = Stats::lookupType(mr.str(3));
me->stats.metric = Stats::lookupMetric(mr.str(2));
2020-09-10 11:15:33 +02:00
me->type = MappingType::STATS;
}
2020-09-10 11:15:33 +02:00
else if (mr[5].matched) {
if (mr.str(5) == "origin")
2019-06-23 16:13:23 +02:00
me->timestamp.type = MappingTimestampType::ORIGIN;
2020-09-10 11:15:33 +02:00
else if (mr.str(5) == "received")
2019-06-23 16:13:23 +02:00
me->timestamp.type = MappingTimestampType::RECEIVED;
2020-09-10 11:15:33 +02:00
else
goto invalid_format;
2020-09-10 11:15:33 +02:00
me->type = MappingType::TIMESTAMP;
}
else if (mr[4].matched) {
if (mr.str(4) == "sequence")
me->header.type = MappingHeaderType::SEQUENCE;
else if (mr.str(4) == "length")
me->header.type = MappingHeaderType::LENGTH;
else
goto invalid_format;
2020-09-10 11:15:33 +02:00
me->type = MappingType::HEADER;
}
2020-09-10 11:15:33 +02:00
/* Only node name given.. We map all data */
else if (me->node_name) {
me->data.first = nullptr;
me->data.last = nullptr;
2020-09-10 11:15:33 +02:00
me->type = MappingType::DATA;
}
return 0;
invalid_format:
2020-09-10 13:22:33 +02:00
2020-09-10 11:15:33 +02:00
throw RuntimeError("Failed to parse mapping expression: {}", str);
}
2020-09-10 13:22:33 +02:00
int mapping_entry_init(struct mapping_entry *me)
{
me->type = MappingType::UNKNOWN;
me->node = nullptr;
me->node_name = nullptr;
return 0;
}
int mapping_entry_destroy(struct mapping_entry *me)
{
if (me->node_name)
free(me->node_name);
return 0;
}
2021-02-16 14:15:14 +01:00
int mapping_entry_parse(struct mapping_entry *me, json_t *json)
{
const char *str;
2021-02-16 14:15:14 +01:00
str = json_string_value(json);
if (!str)
return -1;
2020-09-10 11:15:33 +02:00
return mapping_entry_parse_str(me, str);
}
2021-02-16 14:15:14 +01:00
int mapping_list_parse(struct vlist *ml, json_t *json)
{
int ret;
size_t i;
json_t *json_entry;
json_t *json_mapping;
2021-02-16 14:15:14 +01:00
if (json_is_string(json)) {
json_mapping = json_array();
2021-02-16 14:15:14 +01:00
json_array_append(json_mapping, json);
}
2021-02-16 14:15:14 +01:00
else if (json_is_array(json))
json_mapping = json_incref(json);
else
return -1;
json_array_foreach(json_mapping, i, json_entry) {
auto *me = new struct mapping_entry;
if (!me)
throw MemoryAllocationError();
2020-09-10 13:22:33 +02:00
ret = mapping_entry_init(me);
if (ret)
goto out;
2020-09-10 11:15:33 +02:00
ret = mapping_entry_parse(me, json_entry);
if (ret)
2017-09-05 01:07:11 +02:00
goto out;
2017-09-02 14:20:38 +02:00
vlist_push(ml, me);
}
2017-09-05 01:07:11 +02:00
ret = 0;
2018-08-02 10:50:23 +02:00
out: json_decref(json_mapping);
2017-09-05 01:07:11 +02:00
return ret;
}
int mapping_update(const struct mapping_entry *me, struct sample *remapped, const struct sample *original)
{
2019-04-07 15:13:40 +02:00
unsigned len = me->length;
if (me->offset + len > remapped->capacity)
return -1;
2017-09-02 14:20:38 +02:00
switch (me->type) {
2019-06-23 16:13:23 +02:00
case MappingType::STATS:
2019-06-23 13:35:42 +02:00
remapped->data[me->offset] = me->node->stats->getValue(me->stats.metric, me->stats.type);
break;
2019-06-23 16:13:23 +02:00
case MappingType::TIMESTAMP: {
2018-08-06 11:08:23 +02:00
const struct timespec *ts;
2018-08-06 10:10:17 +02:00
switch (me->timestamp.type) {
2019-06-23 16:13:23 +02:00
case MappingTimestampType::RECEIVED:
ts = &original->ts.received;
break;
2019-06-23 16:13:23 +02:00
case MappingTimestampType::ORIGIN:
ts = &original->ts.origin;
break;
default:
return -1;
}
remapped->data[me->offset + 0].i = ts->tv_sec;
remapped->data[me->offset + 1].i = ts->tv_nsec;
break;
}
2019-06-23 16:13:23 +02:00
case MappingType::HEADER:
2018-08-06 10:10:17 +02:00
switch (me->header.type) {
2019-06-23 16:13:23 +02:00
case MappingHeaderType::LENGTH:
remapped->data[me->offset].i = original->length;
break;
2019-06-23 16:13:23 +02:00
case MappingHeaderType::SEQUENCE:
remapped->data[me->offset].i = original->sequence;
break;
default:
return -1;
}
break;
2019-06-23 16:13:23 +02:00
case MappingType::DATA:
2019-04-07 15:13:40 +02:00
for (unsigned j = me->data.offset,
i = me->offset;
j < MIN(original->length, (unsigned) (me->data.offset + me->length));
j++,
i++)
{
2018-08-20 18:31:27 +02:00
if (j >= original->length)
remapped->data[i].f = -1;
2018-08-20 18:31:27 +02:00
else
remapped->data[i] = original->data[j];
}
2019-04-07 15:13:40 +02:00
len = MIN((unsigned) me->length, original->length - me->data.offset);
break;
2020-09-10 13:22:33 +02:00
case MappingType::UNKNOWN:
return -1;
}
2017-09-02 14:20:38 +02:00
2019-04-07 15:13:40 +02:00
if (me->offset + len > remapped->length)
remapped->length = me->offset + len;
return 0;
}
int mapping_list_remap(const struct vlist *ml, struct sample *remapped, const struct sample *original)
{
int ret;
for (size_t i = 0; i < vlist_length(ml); i++) {
struct mapping_entry *me = (struct mapping_entry *) vlist_at(ml, i);
ret = mapping_update(me, remapped, original);
if (ret)
return ret;
}
return 0;
2017-09-02 14:20:38 +02:00
}
2018-08-02 10:46:03 +02:00
2020-09-10 11:15:33 +02:00
int mapping_entry_prepare(struct mapping_entry *me, struct vlist *nodes)
{
2020-09-10 11:15:33 +02:00
if (me->node_name && me->node == nullptr) {
me->node = vlist_lookup_name<struct vnode>(nodes, me->node_name);
if (!me->node)
throw RuntimeError("Invalid node name in mapping: {}", me->node_name);
}
if (me->type == MappingType::DATA) {
int first = -1, last = -1;
if (me->data.first) {
if (me->node)
first = vlist_lookup_index<struct signal>(&me->node->in.signals, me->data.first);
if (first < 0) {
char *endptr;
first = strtoul(me->data.first, &endptr, 10);
if (endptr != me->data.first + strlen(me->data.first))
throw RuntimeError("Failed to parse data index in mapping: {}", me->data.first);
}
}
else {
/* Map all signals */
me->data.offset = 0;
me->length = -1;
goto end;
}
2020-09-10 11:15:33 +02:00
if (me->data.last) {
if (me->node)
last = vlist_lookup_index<struct signal>(&me->node->in.signals, me->data.last);
2020-09-10 11:15:33 +02:00
if (last < 0) {
char *endptr;
last = strtoul(me->data.last, &endptr, 10);
if (endptr != me->data.last + strlen(me->data.last))
throw RuntimeError("Failed to parse data index in mapping: {}", me->data.last);
}
}
2020-09-10 11:15:33 +02:00
else
last = first; /* single element: data[5] => data[5-5] */
if (last < first)
throw RuntimeError("Invalid data range indices for mapping: {} < {}", last, first);
me->data.offset = first;
me->length = last - first + 1;
}
end:
if (me->length < 0) {
2020-10-21 20:56:51 +02:00
struct vlist *sigs = node_input_signals(me->node);
2020-09-10 11:15:33 +02:00
me->length = vlist_length(sigs);
}
return 0;
}
int mapping_list_prepare(struct vlist *ml, struct vlist *nodes)
{
int ret;
for (size_t i = 0, off = 0; i < vlist_length(ml); i++) {
struct mapping_entry *me = (struct mapping_entry *) vlist_at(ml, i);
ret = mapping_entry_prepare(me, nodes);
if (ret)
return ret;
me->offset = off;
off += me->length;
}
return 0;
}
2020-09-10 11:15:33 +02:00
int mapping_entry_to_str(const struct mapping_entry *me, unsigned index, char **str)
2018-08-02 10:46:03 +02:00
{
const char *type;
2019-04-07 15:13:40 +02:00
assert(me->length == 0 || (int) index < me->length);
2018-08-02 10:46:03 +02:00
if (me->node)
2018-08-20 18:31:27 +02:00
strcatf(str, "%s.", node_name_short(me->node));
2018-08-02 10:46:03 +02:00
switch (me->type) {
2019-06-23 16:13:23 +02:00
case MappingType::STATS:
strcatf(str, "stats.%s.%s",
2019-06-23 13:35:42 +02:00
Stats::metrics[me->stats.metric].name,
Stats::types[me->stats.type].name
);
2018-08-02 10:46:03 +02:00
break;
2019-06-23 16:13:23 +02:00
case MappingType::HEADER:
2018-08-06 10:10:17 +02:00
switch (me->header.type) {
2019-06-23 16:13:23 +02:00
case MappingHeaderType::LENGTH:
type = "length";
break;
2019-06-23 16:13:23 +02:00
case MappingHeaderType::SEQUENCE:
type = "sequence";
break;
default:
2019-04-07 15:13:40 +02:00
type = nullptr;
2018-08-02 10:46:03 +02:00
}
strcatf(str, "hdr.%s", type);
break;
2019-06-23 16:13:23 +02:00
case MappingType::TIMESTAMP:
2018-08-06 10:10:17 +02:00
switch (me->timestamp.type) {
2019-06-23 16:13:23 +02:00
case MappingTimestampType::ORIGIN:
type = "origin";
break;
2019-06-23 16:13:23 +02:00
case MappingTimestampType::RECEIVED:
type = "received";
break;
default:
2019-04-07 15:13:40 +02:00
type = nullptr;
2018-08-02 10:46:03 +02:00
}
strcatf(str, "ts.%s.%s", type, index == 0 ? "sec" : "nsec");
break;
2019-06-23 16:13:23 +02:00
case MappingType::DATA:
2019-02-06 13:11:57 +01:00
if (me->node && index < vlist_length(&me->node->in.signals)) {
2019-04-07 15:13:40 +02:00
struct signal *s = (struct signal *) vlist_at(&me->node->in.signals, index);
2018-08-02 10:46:03 +02:00
strcatf(str, "data[%s]", s->name);
}
else
strcatf(str, "data[%u]", index);
break;
2020-09-10 13:22:33 +02:00
case MappingType::UNKNOWN:
return -1;
2018-08-02 10:46:03 +02:00
}
return 0;
}