2017-09-04 16:19:27 +02:00
|
|
|
/** 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-09-04 16:19:27 +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/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2018-05-12 13:56:12 +02:00
|
|
|
#include <villas/formats/msg.h>
|
|
|
|
#include <villas/formats/msg_format.h>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/sample.h>
|
2018-08-20 18:30:24 +02:00
|
|
|
#include <villas/signal.h>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2018-08-20 18:30:24 +02:00
|
|
|
#include <villas/list.h>
|
2017-09-04 16:19:27 +02:00
|
|
|
|
|
|
|
void msg_ntoh(struct msg *m)
|
|
|
|
{
|
|
|
|
msg_hdr_ntoh(m);
|
|
|
|
|
|
|
|
for (int i = 0; i < m->length; i++)
|
|
|
|
m->data[i].i = ntohl(m->data[i].i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void msg_hton(struct msg *m)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m->length; i++)
|
|
|
|
m->data[i].i = htonl(m->data[i].i);
|
|
|
|
|
|
|
|
msg_hdr_hton(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
void msg_hdr_hton(struct msg *m)
|
|
|
|
{
|
|
|
|
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 msg_hdr_ntoh(struct msg *m)
|
|
|
|
{
|
|
|
|
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 msg_verify(struct msg *m)
|
|
|
|
{
|
|
|
|
if (m->version != MSG_VERSION)
|
|
|
|
return -1;
|
2018-08-20 18:30:24 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
int msg_to_sample(struct msg *msg, struct sample *smp, struct vlist *signals)
|
2017-09-04 16:19:27 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2020-07-06 15:07:05 +02:00
|
|
|
unsigned i;
|
2017-09-04 16:19:27 +02:00
|
|
|
|
|
|
|
ret = msg_verify(msg);
|
|
|
|
if (ret)
|
2020-07-06 15:07:05 +02:00
|
|
|
return ret;
|
2017-09-04 16:19:27 +02:00
|
|
|
|
2020-07-06 15:07:05 +02:00
|
|
|
unsigned len = MIN(msg->length, smp->capacity);
|
|
|
|
for (i = 0; i < MIN(len, vlist_length(signals)); i++) {
|
|
|
|
struct signal *sig = (struct signal *) vlist_at_safe(signals, i);
|
|
|
|
if (!sig)
|
|
|
|
return -1;
|
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[i].f = msg->data[i].f;
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case SignalType::INTEGER:
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->data[i].i = msg->data[i].i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
2017-09-04 16:19:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 15:07:05 +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);
|
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:52:31 +02:00
|
|
|
int msg_from_sample(struct msg *msg_in, struct sample *smp, struct vlist *signals)
|
2017-09-04 16:19:27 +02:00
|
|
|
{
|
2019-04-10 09:52:31 +02:00
|
|
|
msg_in->type = MSG_TYPE_DATA;
|
|
|
|
msg_in->version = MSG_VERSION;
|
2019-10-03 11:34:59 +02:00
|
|
|
msg_in->reserved1 = 0;
|
2020-06-04 09:33:01 +02:00
|
|
|
msg_in->id = 0;
|
2019-04-10 09:52:31 +02:00
|
|
|
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;
|
2017-09-04 16:19:27 +02:00
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
for (unsigned i = 0; i < smp->length; i++) {
|
2020-07-06 15:07:05 +02:00
|
|
|
struct signal *sig = (struct signal *) vlist_at_safe(signals, i);
|
|
|
|
if (!sig)
|
|
|
|
return -1;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
|
|
|
switch (sig->type) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case SignalType::FLOAT:
|
2019-04-10 09:52:31 +02:00
|
|
|
msg_in->data[i].f = smp->data[i].f;
|
2018-08-20 18:30:24 +02:00
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case SignalType::INTEGER:
|
2019-04-10 09:52:31 +02:00
|
|
|
msg_in->data[i].i = smp->data[i].i;
|
2018-08-20 18:30:24 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
2017-09-04 16:19:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|