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/formats/protobuf.cpp

265 lines
7 KiB
C++
Raw Permalink Normal View History

2017-10-27 19:16:09 +02:00
/** Protobuf IO format
*
* @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-10-27 19:16:09 +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.
*
* 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/>.
*********************************************************************************/
/* Generated message descriptors by protoc */
2018-02-06 23:42:56 +01:00
#include <villas.pb-c.h>
2017-10-27 19:16:09 +02:00
2018-02-06 23:42:56 +01:00
#include <villas/sample.h>
#include <villas/signal.h>
#include <villas/io.h>
2018-02-06 23:42:56 +01:00
#include <villas/plugin.h>
2018-05-12 13:56:12 +02:00
#include <villas/formats/protobuf.h>
2020-07-04 18:23:02 +02:00
#include <villas/exceptions.hpp>
2017-10-27 19:16:09 +02:00
2020-07-04 18:23:02 +02:00
using namespace villas;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
2019-06-23 16:13:23 +02:00
static enum SignalType protobuf_detect_format(Villas__Node__Value *val)
{
switch (val->value_case) {
case VILLAS__NODE__VALUE__VALUE_F:
2019-06-23 16:13:23 +02:00
return SignalType::FLOAT;
case VILLAS__NODE__VALUE__VALUE_I:
2019-06-23 16:13:23 +02:00
return SignalType::INTEGER;
case VILLAS__NODE__VALUE__VALUE_B:
2019-06-23 16:13:23 +02:00
return SignalType::BOOLEAN;
case VILLAS__NODE__VALUE__VALUE_Z:
2019-06-23 16:13:23 +02:00
return SignalType::COMPLEX;
case VILLAS__NODE__VALUE__VALUE__NOT_SET:
default:
2019-06-23 16:13:23 +02:00
return SignalType::INVALID;
}
}
2018-05-12 18:01:48 +02:00
int protobuf_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct sample *smps[], unsigned cnt)
2017-10-27 19:16:09 +02:00
{
unsigned psz;
auto *pb_msg = new Villas__Node__Message;
if (!pb_msg)
throw MemoryAllocationError();
2017-10-27 19:16:09 +02:00
villas__node__message__init(pb_msg);
pb_msg->n_samples = cnt;
pb_msg->samples = new Villas__Node__Sample*[pb_msg->n_samples];
if (!pb_msg->samples)
throw MemoryAllocationError();
2017-10-27 19:16:09 +02:00
for (unsigned i = 0; i < pb_msg->n_samples; i++) {
Villas__Node__Sample *pb_smp = pb_msg->samples[i] = new Villas__Node__Sample;
if (!pb_msg->samples[i])
throw MemoryAllocationError();
2017-10-27 19:16:09 +02:00
villas__node__sample__init(pb_smp);
struct sample *smp = smps[i];
pb_smp->type = VILLAS__NODE__SAMPLE__TYPE__DATA;
2019-06-23 16:13:23 +02:00
if (io->flags & smp->flags & (int) SampleFlags::HAS_SEQUENCE) {
2017-10-28 10:17:36 +02:00
pb_smp->has_sequence = 1;
pb_smp->sequence = smp->sequence;
}
2019-06-23 16:13:23 +02:00
if (io->flags & smp->flags & (int) SampleFlags::HAS_TS_ORIGIN) {
pb_smp->timestamp = new Villas__Node__Timestamp;
if (!pb_smp->timestamp)
throw MemoryAllocationError();
2017-10-28 10:17:36 +02:00
villas__node__timestamp__init(pb_smp->timestamp);
pb_smp->timestamp->sec = smp->ts.origin.tv_sec;
pb_smp->timestamp->nsec = smp->ts.origin.tv_nsec;
}
2017-10-27 19:16:09 +02:00
pb_smp->n_values = smp->length;
pb_smp->values = new Villas__Node__Value*[pb_smp->n_values];
2020-07-04 18:23:02 +02:00
if (!pb_smp->values)
throw MemoryAllocationError();
2017-10-27 19:16:09 +02:00
for (unsigned j = 0; j < pb_smp->n_values; j++) {
Villas__Node__Value *pb_val = pb_smp->values[j] = new Villas__Node__Value;
2020-07-06 13:39:52 +02:00
if (!pb_val)
throw MemoryAllocationError();
2017-10-27 19:16:09 +02:00
villas__node__value__init(pb_val);
2019-06-23 16:13:23 +02:00
enum SignalType fmt = sample_format(smp, j);
2017-10-27 19:16:09 +02:00
switch (fmt) {
2019-06-23 16:13:23 +02:00
case SignalType::FLOAT:
pb_val->value_case = VILLAS__NODE__VALUE__VALUE_F;
pb_val->f = smp->data[j].f;
break;
2019-06-23 16:13:23 +02:00
case SignalType::INTEGER:
pb_val->value_case = VILLAS__NODE__VALUE__VALUE_I;
pb_val->i = smp->data[j].i;
break;
2019-06-23 16:13:23 +02:00
case SignalType::BOOLEAN:
pb_val->value_case = VILLAS__NODE__VALUE__VALUE_B;
pb_val->b = smp->data[j].b;
break;
2019-06-23 16:13:23 +02:00
case SignalType::COMPLEX:
pb_val->value_case = VILLAS__NODE__VALUE__VALUE_Z;
pb_val->z = new Villas__Node__Complex;
if (!pb_val->z)
throw MemoryAllocationError();
villas__node__complex__init(pb_val->z);
pb_val->z->real = std::real(smp->data[j].z);
pb_val->z->imag = std::imag(smp->data[j].z);
break;
2019-06-23 16:13:23 +02:00
case SignalType::INVALID:
pb_val->value_case = VILLAS__NODE__VALUE__VALUE__NOT_SET;
break;
2017-10-27 19:16:09 +02:00
}
}
}
psz = villas__node__message__get_packed_size(pb_msg);
if (psz > len)
goto out;
villas__node__message__pack(pb_msg, (uint8_t *) buf);
2019-06-23 16:58:28 +02:00
villas__node__message__free_unpacked(pb_msg, nullptr);
2017-10-27 19:16:09 +02:00
*wbytes = psz;
return cnt;
out:
2019-06-23 16:58:28 +02:00
villas__node__message__free_unpacked(pb_msg, nullptr);
2017-10-27 19:16:09 +02:00
return -1;
}
int protobuf_sscan(struct io *io, const char *buf, size_t len, size_t *rbytes, struct sample *smps[], unsigned cnt)
2017-10-27 19:16:09 +02:00
{
unsigned i, j;
Villas__Node__Message *pb_msg;
2019-06-23 16:58:28 +02:00
pb_msg = villas__node__message__unpack(nullptr, len, (uint8_t *) buf);
if (!pb_msg)
return -1;
2017-10-27 19:16:09 +02:00
for (i = 0; i < MIN(pb_msg->n_samples, cnt); i++) {
struct sample *smp = smps[i];
Villas__Node__Sample *pb_smp = pb_msg->samples[i];
2019-10-29 20:19:19 +01:00
smp->flags = 0;
smp->signals = io->signals;
2017-10-28 10:17:36 +02:00
2017-10-27 19:16:09 +02:00
if (pb_smp->type != VILLAS__NODE__SAMPLE__TYPE__DATA) {
warning("Parsed non supported message type. Skipping");
continue;
2017-10-27 19:16:09 +02:00
}
2017-10-28 10:17:36 +02:00
if (pb_smp->has_sequence) {
2019-06-23 16:13:23 +02:00
smp->flags |= (int) SampleFlags::HAS_SEQUENCE;
2017-10-28 10:17:36 +02:00
smp->sequence = pb_smp->sequence;
}
2017-10-27 19:16:09 +02:00
if (pb_smp->timestamp) {
2019-06-23 16:13:23 +02:00
smp->flags |= (int) SampleFlags::HAS_TS_ORIGIN;
2017-10-27 19:16:09 +02:00
smp->ts.origin.tv_sec = pb_smp->timestamp->sec;
smp->ts.origin.tv_nsec = pb_smp->timestamp->nsec;
}
for (j = 0; j < MIN(pb_smp->n_values, smp->capacity); j++) {
Villas__Node__Value *pb_val = pb_smp->values[j];
2019-06-23 16:13:23 +02:00
enum SignalType fmt = protobuf_detect_format(pb_val);
2017-10-27 19:16:09 +02:00
2019-01-07 10:28:55 +01:00
struct signal *sig = (struct signal *) vlist_at_safe(smp->signals, j);
if (!sig)
return -1;
2019-03-09 13:34:51 +01:00
if (sig->type != fmt) {
error("Received invalid data type in Protobuf payload: Received %s, expected %s for signal %s (index %u).",
signal_type_to_str(fmt), signal_type_to_str(sig->type), sig->name, i);
return -2;
2017-10-27 19:16:09 +02:00
}
switch (sig->type) {
2019-06-23 16:13:23 +02:00
case SignalType::FLOAT:
smp->data[j].f = pb_val->f;
break;
2019-06-23 16:13:23 +02:00
case SignalType::INTEGER:
smp->data[j].i = pb_val->i;
break;
2019-06-23 16:13:23 +02:00
case SignalType::BOOLEAN:
smp->data[j].b = pb_val->b;
break;
2019-06-23 16:13:23 +02:00
case SignalType::COMPLEX:
smp->data[j].z = std::complex<float>(pb_val->z->real, pb_val->z->imag);
break;
default: { }
}
2017-10-27 19:16:09 +02:00
}
2017-10-28 10:17:36 +02:00
if (pb_smp->n_values > 0)
2019-06-23 16:13:23 +02:00
smp->flags |= (int) SampleFlags::HAS_DATA;
2017-10-28 10:17:36 +02:00
2017-10-27 19:16:09 +02:00
smp->length = j;
}
2018-01-17 01:22:43 +01:00
if (rbytes)
*rbytes = villas__node__message__get_packed_size(pb_msg);
2017-10-27 19:16:09 +02:00
2019-06-23 16:58:28 +02:00
villas__node__message__free_unpacked(pb_msg, nullptr);
2017-10-27 19:16:09 +02:00
return i;
}
static struct plugin p;
__attribute__((constructor(110))) static void UNIQUE(__ctor)() {
p.name = "protobuf";
p.description = "Google Protobuf";
2019-06-23 16:13:23 +02:00
p.type = PluginType::FORMAT;
p.format.sprint = protobuf_sprint;
p.format.sscan = protobuf_sscan;
2019-06-23 16:13:23 +02:00
p.format.flags = (int) IOFlags::HAS_BINARY_PAYLOAD |
(int) SampleFlags::HAS_TS_ORIGIN | (int) SampleFlags::HAS_SEQUENCE | (int) SampleFlags::HAS_DATA;
vlist_init_and_push(&plugins, &p);
}
__attribute__((destructor(110))) static void UNIQUE(__dtor)() {
2020-06-16 02:35:34 +02:00
vlist_remove_all(&plugins, &p);
}