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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

135 lines
3.2 KiB
C++
Raw Permalink Normal View History

2017-09-04 16:19:27 +02:00
/* Message related functions.
*
2022-03-15 09:18:01 -04:00
* Author: Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
2022-07-04 18:20:03 +02:00
* SPDX-License-Identifier: Apache-2.0
2017-09-04 16:19:27 +02:00
*/
#include <arpa/inet.h>
2021-05-10 00:12:30 +02:00
#include <villas/formats/msg.hpp>
#include <villas/formats/msg_format.hpp>
#include <villas/list.hpp>
#include <villas/sample.hpp>
#include <villas/signal.hpp>
#include <villas/utils.hpp>
2017-09-04 16:19:27 +02:00
using namespace villas;
using namespace villas::node;
void villas::node::msg_ntoh(struct Message *m) {
2017-09-04 16:19:27 +02:00
msg_hdr_ntoh(m);
for (int i = 0; i < m->length; i++)
m->data[i].i = ntohl(m->data[i].i);
}
void villas::node::msg_hton(struct Message *m) {
2017-09-04 16:19:27 +02:00
for (int i = 0; i < m->length; i++)
m->data[i].i = htonl(m->data[i].i);
msg_hdr_hton(m);
}
void villas::node::msg_hdr_hton(struct Message *m) {
2017-09-04 16:19:27 +02:00
m->length = htons(m->length);
m->sequence = htonl(m->sequence);
m->ts.sec = htonl(m->ts.sec);
m->ts.nsec = htonl(m->ts.nsec);
}
void villas::node::msg_hdr_ntoh(struct Message *m) {
2017-09-04 16:19:27 +02:00
m->length = ntohs(m->length);
m->sequence = ntohl(m->sequence);
m->ts.sec = ntohl(m->ts.sec);
m->ts.nsec = ntohl(m->ts.nsec);
}
int villas::node::msg_verify(const struct Message *m) {
2017-09-04 16:19:27 +02:00
if (m->version != MSG_VERSION)
return -1;
else if (m->type != MSG_TYPE_DATA)
2017-09-04 16:19:27 +02:00
return -2;
2019-09-03 22:06:23 +02:00
else if (m->reserved1 != 0)
2017-09-04 16:19:27 +02:00
return -3;
else
return 0;
}
int villas::node::msg_to_sample(const struct Message *msg, struct Sample *smp,
const SignalList::Ptr sigs,
uint8_t *source_index) {
2017-09-04 16:19:27 +02:00
int ret;
unsigned i;
2017-09-04 16:19:27 +02:00
ret = msg_verify(msg);
if (ret)
return ret;
unsigned len = MIN(msg->length, smp->capacity);
for (i = 0; i < MIN(len, sigs->size()); i++) {
auto sig = sigs->getByIndex(i);
if (!sig)
return -1;
switch (sig->type) {
2019-06-23 16:13:23 +02:00
case SignalType::FLOAT:
smp->data[i].f = msg->data[i].f;
break;
2019-06-23 16:13:23 +02:00
case SignalType::INTEGER:
smp->data[i].i = msg->data[i].i;
break;
default:
return -1;
2017-09-04 16:19:27 +02:00
}
}
smp->flags = (int)SampleFlags::HAS_TS_ORIGIN |
(int)SampleFlags::HAS_SEQUENCE | (int)SampleFlags::HAS_DATA;
smp->length = i;
smp->sequence = msg->sequence;
MSG_TS(msg, smp->ts.origin);
if (source_index)
*source_index = msg->source_index;
2017-09-04 16:19:27 +02:00
return 0;
}
int villas::node::msg_from_sample(struct Message *msg_in,
const struct Sample *smp,
const SignalList::Ptr sigs,
uint8_t source_index) {
msg_in->type = MSG_TYPE_DATA;
msg_in->version = MSG_VERSION;
2019-10-03 11:34:59 +02:00
msg_in->reserved1 = 0;
msg_in->source_index = source_index;
msg_in->length = (uint16_t)smp->length;
msg_in->sequence = (uint32_t)smp->sequence;
msg_in->ts.sec = smp->ts.origin.tv_sec;
msg_in->ts.nsec = smp->ts.origin.tv_nsec;
2019-04-07 15:13:40 +02:00
for (unsigned i = 0; i < smp->length; i++) {
auto sig = sigs->getByIndex(i);
if (!sig)
return -1;
switch (sig->type) {
2019-06-23 16:13:23 +02:00
case SignalType::FLOAT:
msg_in->data[i].f = smp->data[i].f;
break;
2019-06-23 16:13:23 +02:00
case SignalType::INTEGER:
msg_in->data[i].i = smp->data[i].i;
break;
default:
return -1;
2017-09-04 16:19:27 +02:00
}
}
2017-09-04 16:19:27 +02:00
return 0;
}