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/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-03-26 07:09:55 +01:00
|
|
|
/* 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>
|
2018-08-20 18:30:24 +02:00
|
|
|
#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)
|
2018-08-20 18:30:24 +02:00
|
|
|
{
|
|
|
|
switch (val->value_case) {
|
|
|
|
case VILLAS__NODE__VALUE__VALUE_F:
|
2019-06-23 16:13:23 +02:00
|
|
|
return SignalType::FLOAT;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
|
|
|
case VILLAS__NODE__VALUE__VALUE_I:
|
2019-06-23 16:13:23 +02:00
|
|
|
return SignalType::INTEGER;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
|
|
|
case VILLAS__NODE__VALUE__VALUE_B:
|
2019-06-23 16:13:23 +02:00
|
|
|
return SignalType::BOOLEAN;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
|
|
|
case VILLAS__NODE__VALUE__VALUE_Z:
|
2019-06-23 16:13:23 +02:00
|
|
|
return SignalType::COMPLEX;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
|
|
|
case VILLAS__NODE__VALUE__VALUE__NOT_SET:
|
|
|
|
default:
|
2019-06-23 16:13:23 +02:00
|
|
|
return SignalType::INVALID;
|
2018-08-20 18:30:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2020-01-21 16:26:51 +01:00
|
|
|
auto *pb_msg = new Villas__Node__Message;
|
2020-07-04 16:22:10 +02:00
|
|
|
if (!pb_msg)
|
|
|
|
throw MemoryAllocationError();
|
|
|
|
|
2017-10-27 19:16:09 +02:00
|
|
|
villas__node__message__init(pb_msg);
|
|
|
|
|
|
|
|
pb_msg->n_samples = cnt;
|
2020-01-21 16:26:51 +01:00
|
|
|
pb_msg->samples = new Villas__Node__Sample*[pb_msg->n_samples];
|
2020-07-04 16:22:10 +02:00
|
|
|
if (!pb_msg->samples)
|
|
|
|
throw MemoryAllocationError();
|
2017-10-27 19:16:09 +02:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < pb_msg->n_samples; i++) {
|
2020-01-21 16:26:51 +01:00
|
|
|
Villas__Node__Sample *pb_smp = pb_msg->samples[i] = new Villas__Node__Sample;
|
2020-07-04 16:22:10 +02:00
|
|
|
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) {
|
2020-01-21 16:26:51 +01:00
|
|
|
pb_smp->timestamp = new Villas__Node__Timestamp;
|
2020-07-04 16:22:10 +02:00
|
|
|
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;
|
2020-01-21 16:26:51 +01:00
|
|
|
pb_smp->values = new Villas__Node__Value*[pb_smp->n_values];
|
2020-07-04 18:23:02 +02:00
|
|
|
if (!pb_smp->values)
|
2020-07-04 16:22:10 +02:00
|
|
|
throw MemoryAllocationError();
|
2017-10-27 19:16:09 +02:00
|
|
|
|
|
|
|
for (unsigned j = 0; j < pb_smp->n_values; j++) {
|
2020-01-21 16:26:51 +01:00
|
|
|
Villas__Node__Value *pb_val = pb_smp->values[j] = new Villas__Node__Value;
|
2020-07-06 13:39:52 +02:00
|
|
|
if (!pb_val)
|
2020-07-04 16:22:10 +02:00
|
|
|
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:
|
2018-08-20 18:30:24 +02:00
|
|
|
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:
|
2018-08-20 18:30:24 +02:00
|
|
|
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:
|
2018-08-20 18:30:24 +02:00
|
|
|
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:
|
2018-08-20 18:30:24 +02:00
|
|
|
pb_val->value_case = VILLAS__NODE__VALUE__VALUE_Z;
|
2020-01-21 16:26:51 +01:00
|
|
|
pb_val->z = new Villas__Node__Complex;
|
2020-07-04 16:22:10 +02:00
|
|
|
if (!pb_val->z)
|
|
|
|
throw MemoryAllocationError();
|
2018-08-20 18:30:24 +02:00
|
|
|
|
|
|
|
villas__node__complex__init(pb_val->z);
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
pb_val->z->real = std::real(smp->data[j].z);
|
|
|
|
pb_val->z->imag = std::imag(smp->data[j].z);
|
2018-08-20 18:30:24 +02:00
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case SignalType::INVALID:
|
2018-08-20 18:30:24 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
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);
|
2018-08-20 18:30:24 +02:00
|
|
|
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;
|
2018-08-20 18:30:24 +02:00
|
|
|
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) {
|
2018-10-21 21:36:08 +01:00
|
|
|
warning("Parsed non supported message type. Skipping");
|
2018-08-20 18:30:24 +02:00
|
|
|
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);
|
2018-08-20 18:30:24 +02:00
|
|
|
if (!sig)
|
|
|
|
return -1;
|
|
|
|
|
2019-03-09 13:34:51 +01:00
|
|
|
if (sig->type != fmt) {
|
2018-08-20 18:30:24 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
switch (sig->type) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case SignalType::FLOAT:
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->data[j].f = pb_val->f;
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case SignalType::INTEGER:
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->data[j].i = pb_val->i;
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case SignalType::BOOLEAN:
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->data[j].b = pb_val->b;
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case SignalType::COMPLEX:
|
2019-06-23 16:57:00 +02:00
|
|
|
smp->data[j].z = std::complex<float>(pb_val->z->real, pb_val->z->imag);
|
2018-08-20 18:30:24 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:27:55 +02:00
|
|
|
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;
|
2019-04-09 14:27:55 +02:00
|
|
|
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;
|
2019-04-09 14:27:55 +02:00
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
vlist_init_and_push(&plugins, &p);
|
2019-04-09 14:27:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((destructor(110))) static void UNIQUE(__dtor)() {
|
2020-06-16 02:35:34 +02:00
|
|
|
vlist_remove_all(&plugins, &p);
|
2019-04-08 14:00:46 +01:00
|
|
|
}
|
2019-04-09 14:27:55 +02:00
|
|
|
|