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>
|
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.
|
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
|
|
|
*********************************************************************************/
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-09-07 16:29:05 +00:00
|
|
|
#include <ctype.h>
|
2014-08-31 16:15:54 +00:00
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
#include <byteswap.h>
|
2014-09-04 13:25:21 +00:00
|
|
|
#elif defined(__PPC__) /* Xilinx toolchain */
|
2014-08-31 16:15:54 +00:00
|
|
|
#include <xil_io.h>
|
2014-09-04 13:25:21 +00:00
|
|
|
#define bswap_32(x) Xil_EndianSwap32(x)
|
2014-08-31 16:15:54 +00:00
|
|
|
#endif
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include "msg.h"
|
2014-07-07 07:53:42 +00:00
|
|
|
#include "node.h"
|
2014-06-05 09:34:55 +00:00
|
|
|
#include "utils.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-07-18 16:05:49 +00:00
|
|
|
void msg_swap(struct msg *m)
|
2014-06-05 09:34:29 +00:00
|
|
|
{
|
2014-09-04 13:25:21 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < m->length; i++)
|
2014-08-31 16:15:54 +00:00
|
|
|
m->data[i].i = bswap_32(m->data[i].i);
|
2014-07-18 16:05:49 +00:00
|
|
|
|
|
|
|
m->endian ^= 1;
|
|
|
|
}
|
|
|
|
|
2015-04-01 13:25:03 +02:00
|
|
|
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;
|
2015-04-01 13:25:03 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 16:05:49 +00:00
|
|
|
int msg_fprint(FILE *f, struct msg *m)
|
|
|
|
{
|
2014-09-07 16:29:03 +00:00
|
|
|
if (m->endian != MSG_ENDIAN_HOST)
|
|
|
|
msg_swap(m);
|
2014-07-18 16:05:49 +00:00
|
|
|
|
2015-06-02 22:36:17 +02:00
|
|
|
fprintf(f, "%10u.%09u\t%hu", m->ts.sec, m->ts.nsec, m->sequence);
|
2014-07-18 16:05:49 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < m->length; i++)
|
2014-09-07 16:29:05 +00:00
|
|
|
fprintf(f, "\t%.6f", m->data[i].f);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
fprintf(f, "\n");
|
2014-06-05 09:34:53 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-02 22:36:17 +02:00
|
|
|
/** @todo Currently only floating point values are supported */
|
2014-07-04 15:58:11 +00:00
|
|
|
int msg_fscan(FILE *f, struct msg *m)
|
2014-06-05 09:34:53 +00:00
|
|
|
{
|
2014-09-07 16:29:05 +00:00
|
|
|
char line[MSG_VALUES * 16];
|
2015-06-02 22:36:17 +02:00
|
|
|
char *next, *ptr = line;
|
2014-06-05 09:34:53 +00:00
|
|
|
|
2014-09-07 16:29:05 +00:00
|
|
|
if (!fgets(line, sizeof(line), f))
|
|
|
|
return 0;
|
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);
|
|
|
|
|
|
|
|
if (next == ptr)
|
2014-09-07 16:29:05 +00:00
|
|
|
break;
|
2014-07-18 16:05:49 +00:00
|
|
|
|
2015-06-02 22:36:17 +02:00
|
|
|
ptr = next;
|
|
|
|
m->length++;
|
2014-09-07 16:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m->length;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:53 +00:00
|
|
|
void msg_random(struct msg *m)
|
2014-06-05 09:34:29 +00:00
|
|
|
{
|
2014-07-04 15:58:11 +00:00
|
|
|
for (int i = 0; i < m->length; i++)
|
2015-05-19 16:54:00 +02:00
|
|
|
m->data[i].f += box_muller(0, 1);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-08-31 15:25:59 +00:00
|
|
|
m->endian = MSG_ENDIAN_HOST;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|