2014-07-14 11:49:44 +00:00
|
|
|
/** Message related functions.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @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-05-05 19:24:16 +00:00
|
|
|
*
|
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-05-05 19:24:16 +00:00
|
|
|
*
|
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/>.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
#include <villas/io.h>
|
2018-05-12 13:56:12 +02:00
|
|
|
#include <villas/formats/villas_binary.h>
|
|
|
|
#include <villas/formats/msg.h>
|
|
|
|
#include <villas/formats/msg_format.h>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/sample.h>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/plugin.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
int villas_binary_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct sample *smps[], unsigned cnt)
|
2017-05-23 09:33:42 +02:00
|
|
|
{
|
2019-04-07 15:13:40 +02:00
|
|
|
int ret;
|
|
|
|
unsigned i = 0;
|
2017-05-23 09:33:42 +02:00
|
|
|
char *ptr = buf;
|
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
for (i = 0; i < cnt; i++) {
|
2017-08-22 14:15:19 +02:00
|
|
|
struct msg *msg = (struct msg *) ptr;
|
|
|
|
struct sample *smp = smps[i];
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 14:15:19 +02:00
|
|
|
if (ptr + MSG_LEN(smp->length) > buf + len)
|
2017-08-14 14:42:07 +02:00
|
|
|
break;
|
2017-05-23 09:33:42 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
ret = msg_from_sample(msg, smp, smp->signals);
|
2017-05-23 09:33:42 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
if (io->flags & VILLAS_BINARY_WEB) {
|
2017-08-22 14:15:19 +02:00
|
|
|
/** @todo convert to little endian */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
msg_hton(msg);
|
|
|
|
|
|
|
|
ptr += MSG_LEN(smp->length);
|
2017-05-23 09:33:42 +02:00
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
if (wbytes)
|
|
|
|
*wbytes = ptr - buf;
|
2017-07-22 14:07:43 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
return i;
|
2017-05-23 09:33:42 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
int villas_binary_sscan(struct io *io, const char *buf, size_t len, size_t *rbytes, struct sample *smps[], unsigned cnt)
|
2017-05-23 09:33:42 +02:00
|
|
|
{
|
2019-04-07 15:13:40 +02:00
|
|
|
int ret, values;
|
|
|
|
unsigned i = 0;
|
2018-08-20 18:30:24 +02:00
|
|
|
const char *ptr = buf;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
if (len % 4 != 0) {
|
2018-10-21 21:36:08 +01:00
|
|
|
warning("Packet size is invalid: %zd Must be multiple of 4 bytes.", len);
|
2017-08-27 17:04:45 +02:00
|
|
|
return -1;
|
2017-08-22 12:21:17 +02:00
|
|
|
}
|
2017-05-23 09:33:42 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
for (i = 0; i < cnt; i++) {
|
|
|
|
struct msg *msg = (struct msg *) ptr;
|
2017-08-22 14:15:19 +02:00
|
|
|
struct sample *smp = smps[i];
|
2017-08-27 17:04:45 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->signals = io->signals;
|
|
|
|
|
2017-08-27 17:04:45 +02:00
|
|
|
/* Complete buffer has been parsed */
|
|
|
|
if (ptr == buf + len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Check if header is still in buffer bounaries */
|
|
|
|
if (ptr + sizeof(struct msg) > buf + len) {
|
2018-10-21 21:36:08 +01:00
|
|
|
warning("Invalid msg received: reason=1");
|
2017-08-14 14:42:07 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
values = (io->flags & VILLAS_BINARY_WEB) ? msg->length : ntohs(msg->length);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
/* Check if remainder of message is in buffer boundaries */
|
2017-08-27 17:04:45 +02:00
|
|
|
if (ptr + MSG_LEN(values) > buf + len) {
|
2018-10-21 21:36:08 +01:00
|
|
|
warning("Invalid msg received: reason=2, msglen=%zu, len=%zu, ptr=%p, buf=%p, i=%u", MSG_LEN(values), len, ptr, buf, i);
|
2017-08-14 14:42:07 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-08-22 14:15:19 +02:00
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
if (io->flags & VILLAS_BINARY_WEB) {
|
|
|
|
/** @todo convert from little endian */
|
|
|
|
}
|
2017-08-22 14:15:19 +02:00
|
|
|
else
|
|
|
|
msg_ntoh(msg);
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
ret = msg_to_sample(msg, smp, io->signals);
|
2017-08-27 17:04:45 +02:00
|
|
|
if (ret) {
|
2018-10-21 21:36:08 +01:00
|
|
|
warning("Invalid msg received: reason=3, ret=%d", ret);
|
2017-08-27 17:04:45 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-05-23 09:33:42 +02:00
|
|
|
|
2017-08-22 14:15:19 +02:00
|
|
|
ptr += MSG_LEN(smp->length);
|
2017-05-23 09:33:42 +02:00
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
if (rbytes)
|
|
|
|
*rbytes = ptr - buf;
|
2017-07-22 14:07:43 +02:00
|
|
|
|
2017-05-23 09:33:42 +02:00
|
|
|
return i;
|
2017-07-22 14:07:43 +02:00
|
|
|
}
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2019-04-09 14:27:55 +02:00
|
|
|
static struct plugin p1;
|
|
|
|
|
|
|
|
__attribute__((constructor(110))) static void UNIQUE(__ctor)() {
|
|
|
|
p1.name = "villas.binary";
|
|
|
|
p1.description = "VILLAS binary network format";
|
2019-06-23 16:13:23 +02:00
|
|
|
p1.type = PluginType::FORMAT;
|
2019-04-09 14:27:55 +02:00
|
|
|
p1.format.sprint = villas_binary_sprint;
|
|
|
|
p1.format.sscan = villas_binary_sscan;
|
|
|
|
p1.format.size = 0;
|
2019-06-23 16:13:23 +02:00
|
|
|
p1.format.flags = (int) IOFlags::HAS_BINARY_PAYLOAD |
|
|
|
|
(int) SampleFlags::HAS_TS_ORIGIN | (int) SampleFlags::HAS_SEQUENCE | (int) SampleFlags::HAS_DATA;
|
2019-04-23 13:04:30 +02:00
|
|
|
|
2019-04-09 14:27:55 +02:00
|
|
|
vlist_push(&plugins, &p1);
|
|
|
|
}
|
2017-08-05 21:02:09 +02:00
|
|
|
|
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, &p1);
|
2019-04-09 14:27:55 +02:00
|
|
|
}
|
2017-08-27 17:04:45 +02:00
|
|
|
/** The WebSocket node-type usually uses little endian byte order intead of network byte order */
|
2019-04-09 14:27:55 +02:00
|
|
|
static struct plugin p2;
|
|
|
|
|
|
|
|
|
|
|
|
__attribute__((constructor(110))) static void UNIQUE(__ctor)() {
|
|
|
|
p2.name = "villas.web";
|
|
|
|
p2.description = "VILLAS binary network format for WebSockets";
|
2019-06-23 16:13:23 +02:00
|
|
|
p2.type = PluginType::FORMAT;
|
2019-04-09 14:27:55 +02:00
|
|
|
p2.format.sprint = villas_binary_sprint;
|
|
|
|
p2.format.sscan = villas_binary_sscan;
|
|
|
|
p2.format.size = 0;
|
2019-06-23 16:13:23 +02:00
|
|
|
p2.format.flags = (int) IOFlags::HAS_BINARY_PAYLOAD | VILLAS_BINARY_WEB |
|
|
|
|
(int) SampleFlags::HAS_TS_ORIGIN | (int) SampleFlags::HAS_SEQUENCE | (int) SampleFlags::HAS_DATA;
|
2019-04-09 14:27:55 +02:00
|
|
|
|
|
|
|
vlist_push(&plugins, &p2);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((destructor(110))) static void UNIQUE(__dtor)() {
|
2020-06-16 02:35:34 +02:00
|
|
|
vlist_remove_all(&plugins, &p2);
|
2019-04-09 14:27:55 +02:00
|
|
|
}
|