1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/formats/villas_binary.cpp

116 lines
2.9 KiB
C++
Raw Permalink Normal View History

/** Message related functions.
*
* @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/>.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <cstring>
2021-05-10 00:12:30 +02:00
#include <arpa/inet.h>
2021-05-10 00:12:30 +02:00
#include <villas/formats/villas_binary.hpp>
#include <villas/formats/msg.hpp>
#include <villas/formats/msg_format.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/sample.h>
#include <villas/utils.hpp>
2021-05-10 00:12:30 +02:00
using namespace villas::node;
int VillasBinaryFormat::sprint(char *buf, size_t len, size_t *wbytes, const struct sample * const smps[], unsigned cnt)
{
2019-04-07 15:13:40 +02:00
int ret;
unsigned i = 0;
char *ptr = buf;
for (i = 0; i < cnt; i++) {
2017-08-22 14:15:19 +02:00
struct msg *msg = (struct msg *) ptr;
2021-05-10 00:12:30 +02:00
const struct sample *smp = smps[i];
2017-08-22 14:15:19 +02:00
if (ptr + MSG_LEN(smp->length) > buf + len)
break;
ret = msg_from_sample(msg, smp, smp->signals);
if (ret)
return ret;
2021-05-10 00:12:30 +02:00
if (web) {
2017-08-22 14:15:19 +02:00
/** @todo convert to little endian */
}
else
msg_hton(msg);
ptr += MSG_LEN(smp->length);
}
if (wbytes)
*wbytes = ptr - buf;
return i;
}
2021-05-10 00:12:30 +02:00
int VillasBinaryFormat::sscan(const char *buf, size_t len, size_t *rbytes, struct sample * const smps[], unsigned cnt)
{
2019-04-07 15:13:40 +02:00
int ret, values;
unsigned i = 0;
const char *ptr = buf;
2021-02-16 14:15:14 +01:00
if (len % 4 != 0)
return -1; /* Packet size is invalid: Must be multiple of 4 bytes */
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
2021-05-10 00:12:30 +02:00
smp->signals = 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 */
2021-02-16 14:15:14 +01:00
if (ptr + sizeof(struct msg) > buf + len)
return -2; /* Invalid msg received */
2021-05-10 00:12:30 +02:00
values = web ? msg->length : ntohs(msg->length);
/* Check if remainder of message is in buffer boundaries */
2021-02-16 14:15:14 +01:00
if (ptr + MSG_LEN(values) > buf + len)
return -3; /*Invalid msg receive */
2017-08-22 14:15:19 +02:00
2021-05-10 00:12:30 +02:00
if (web) {
2018-05-12 18:01:48 +02:00
/** @todo convert from little endian */
}
2017-08-22 14:15:19 +02:00
else
msg_ntoh(msg);
2021-05-10 00:12:30 +02:00
ret = msg_to_sample(msg, smp, signals);
2021-02-16 14:15:14 +01:00
if (ret)
return ret; /* Invalid msg received */
2017-08-22 14:15:19 +02:00
ptr += MSG_LEN(smp->length);
}
if (rbytes)
*rbytes = ptr - buf;
return i;
}
2017-08-05 21:02:09 +02:00
2021-05-10 00:12:30 +02:00
static VillasBinaryFormatPlugin<false> p1;
static VillasBinaryFormatPlugin<true> p2;