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-06-05 09:34:55 +00:00
|
|
|
#include <errno.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)
|
|
|
|
{
|
|
|
|
return ((m->version == MSG_VERSION) &&
|
2015-05-06 11:49:13 +02:00
|
|
|
(m->type == MSG_TYPE_DATA) &&
|
|
|
|
(m->length > 0) &&
|
|
|
|
(m->length <= MSG_VALUES))
|
2015-04-01 13:25:03 +02:00
|
|
|
? 0
|
|
|
|
: -1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-09-07 16:29:05 +00:00
|
|
|
fprintf(f, "%hu", 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;
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
char *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;
|
2014-06-05 09:34:53 +00:00
|
|
|
|
2014-09-07 16:29:05 +00:00
|
|
|
m->sequence = (uint16_t) strtol(ptr, &ptr, 10);
|
2014-07-18 16:05:49 +00:00
|
|
|
|
2014-09-07 16:29:05 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i <= MSG_VALUES; i++) {
|
|
|
|
while(isblank(*ptr++));
|
|
|
|
if (*ptr == '\n' || *ptr == '\0')
|
|
|
|
break;
|
2014-07-18 16:05:49 +00:00
|
|
|
|
2014-09-07 16:29:05 +00:00
|
|
|
m->data[i].f = strtod(ptr, &ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
m->length = i;
|
|
|
|
m->endian = MSG_ENDIAN_HOST;
|
|
|
|
|
|
|
|
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
|
|
|
}
|