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/server/src/msg.c

101 lines
2.1 KiB
C
Raw Normal View History

/** Message related functions.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2015-06-02 21:53:04 +02:00
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
2015-08-07 01:11:43 +02:00
* Unauthorized copying of this file, via any medium is strictly prohibited.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef __linux__
2015-08-21 10:19:32 +02:00
#include <byteswap.h>
#elif defined(__PPC__) /* Xilinx toolchain */
2015-08-21 10:19:32 +02:00
#include <xil_io.h>
#define bswap_32(x) Xil_EndianSwap32(x)
#endif
#include "msg.h"
#include "node.h"
#include "utils.h"
void msg_swap(struct msg *m)
{
int i;
for (i = 0; i < m->length; i++)
m->data[i].i = bswap_32(m->data[i].i);
m->endian ^= 1;
}
int msg_verify(struct msg *m)
{
2015-06-02 22:39:15 +02:00
if (m->version != MSG_VERSION)
return -1;
else if (m->type != MSG_TYPE_DATA)
return -2;
else if ((m->length <= 0) || (m->length > MSG_VALUES))
return -3;
else if ((m->rsvd1 != 0) || (m->rsvd2 != 0))
return -4;
else
return 0;
}
int msg_fprint(FILE *f, struct msg *m)
{
if (m->endian != MSG_ENDIAN_HOST)
msg_swap(m);
2015-06-02 22:36:17 +02:00
fprintf(f, "%10u.%09u\t%hu", m->ts.sec, m->ts.nsec, m->sequence);
for (int i = 0; i < m->length; i++)
fprintf(f, "\t%.6f", m->data[i].f);
fprintf(f, "\n");
return 0;
}
2015-06-02 22:36:17 +02:00
/** @todo Currently only floating point values are supported */
int msg_fscan(FILE *f, struct msg *m)
{
char line[MSG_VALUES * 16];
2015-06-02 22:36:17 +02:00
char *next, *ptr = line;
if (fgets(line, sizeof(line), f) == NULL)
return -1; /* An error occured */
2015-08-07 01:11:43 +02:00
2015-06-02 22:36:17 +02:00
m->ts.sec = (uint32_t) strtoul(ptr, &ptr, 10); ptr++;
m->ts.nsec = (uint32_t) strtoul(ptr, &ptr, 10);
m->sequence = (uint16_t) strtoul(ptr, &ptr, 10);
m->version = MSG_VERSION;
m->endian = MSG_ENDIAN_HOST;
m->length = 0;
m->rsvd1 = 0;
m->rsvd2 = 0;
while (m->length < MSG_VALUES) {
m->data[m->length].f = strtod(ptr, &next);
2015-08-07 01:11:43 +02:00
2015-06-02 22:36:17 +02:00
if (next == ptr)
break;
2015-06-02 22:36:17 +02:00
ptr = next;
m->length++;
}
2015-08-07 01:11:43 +02:00
return m->length;
}
void msg_random(struct msg *m)
{
for (int i = 0; i < m->length; i++)
m->data[i].f += box_muller(0, 1);
m->endian = MSG_ENDIAN_HOST;
}