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>
|
2016-02-09 05:33:19 +01:00
|
|
|
* @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC
|
2015-06-02 21:53:04 +02:00
|
|
|
* 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
|
|
|
*********************************************************************************/
|
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__
|
2015-08-21 10:19:32 +02:00
|
|
|
#include <byteswap.h>
|
2014-09-04 13:25:21 +00:00
|
|
|
#elif defined(__PPC__) /* Xilinx toolchain */
|
2015-08-21 10:19:32 +02:00
|
|
|
#include <xil_io.h>
|
2015-10-08 10:51:48 +02:00
|
|
|
#define bswap_16(x) Xil_EndianSwap16(x)
|
2015-08-21 10:19:32 +02: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
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
struct msg * msg_create(size_t values) {
|
|
|
|
struct msg m = {
|
|
|
|
.version = MSG_VERSION,
|
|
|
|
.type = MSG_TYPE_DATA,
|
|
|
|
.endian = MSG_ENDIAN_HOST,
|
|
|
|
.values = values,
|
|
|
|
.sequence = 0,
|
|
|
|
.rsvd1 = 0, .rsvd2 = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
return memdup(&m, sizeof(struct msg) + sizeof(float) * values);
|
|
|
|
}
|
|
|
|
|
|
|
|
void msg_destroy(struct msg *m)
|
|
|
|
{
|
|
|
|
free(m);
|
|
|
|
}
|
|
|
|
|
2014-07-18 16:05:49 +00:00
|
|
|
void msg_swap(struct msg *m)
|
2014-06-05 09:34:29 +00:00
|
|
|
{
|
2016-01-14 22:59:57 +01:00
|
|
|
m->values = bswap_16(m->values);
|
2015-10-08 10:51:48 +02:00
|
|
|
m->sequence = bswap_32(m->sequence);
|
|
|
|
m->ts.sec = bswap_32(m->ts.sec);
|
|
|
|
m->ts.nsec = bswap_32(m->ts.nsec);
|
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
for (int i = 0; i < m->values; 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->rsvd1 != 0) || (m->rsvd2 != 0))
|
2016-01-14 22:59:57 +01:00
|
|
|
return -3;
|
2015-06-02 22:39:15 +02:00
|
|
|
else
|
|
|
|
return 0;
|
2015-04-01 13:25:03 +02:00
|
|
|
}
|
|
|
|
|
2015-12-04 01:48:33 +01:00
|
|
|
int msg_print(char *buf, size_t len, struct msg *m, int flags, double offset)
|
2014-07-18 16:05:49 +00:00
|
|
|
{
|
2015-12-04 01:48:33 +01:00
|
|
|
size_t off = snprintf(buf, len, "%u", m->ts.sec);
|
2015-10-07 18:12:12 +02:00
|
|
|
|
|
|
|
if (flags & MSG_PRINT_NANOSECONDS)
|
2015-12-04 01:48:33 +01:00
|
|
|
off += snprintf(buf + off, len - off, ".%09u", m->ts.nsec);
|
2015-10-07 18:12:12 +02:00
|
|
|
|
|
|
|
if (flags & MSG_PRINT_OFFSET)
|
2015-12-04 01:48:33 +01:00
|
|
|
off += snprintf(buf + off, len - off, "%+g", offset);
|
2015-10-07 18:12:12 +02:00
|
|
|
|
|
|
|
if (flags & MSG_PRINT_SEQUENCE)
|
2015-12-04 01:48:33 +01:00
|
|
|
off += snprintf(buf + off, len - off, "(%u)", m->sequence);
|
2015-10-07 18:12:12 +02:00
|
|
|
|
|
|
|
if (flags & MSG_PRINT_VALUES) {
|
2016-01-14 22:59:57 +01:00
|
|
|
for (int i = 0; i < m->values; i++)
|
2015-12-04 01:48:33 +01:00
|
|
|
off += snprintf(buf + off, len - off, "\t%.6f", m->data[i].f);
|
2015-10-07 18:12:12 +02:00
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2015-12-04 01:48:33 +01:00
|
|
|
off += snprintf(buf + off, len - off, "\n");
|
2014-06-05 09:34:53 +00:00
|
|
|
|
2015-12-04 01:48:33 +01:00
|
|
|
return off + 1; /* trailing '\0' */
|
2014-06-05 09:34:53 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 01:48:33 +01:00
|
|
|
int msg_scan(const char *line, struct msg *m, int *fl, double *off)
|
2014-06-05 09:34:53 +00:00
|
|
|
{
|
2015-12-04 01:48:33 +01:00
|
|
|
char *end;
|
|
|
|
const char *ptr = line;
|
|
|
|
|
2015-10-07 18:12:12 +02:00
|
|
|
int flags = 0;
|
|
|
|
double offset;
|
|
|
|
|
|
|
|
m->version = MSG_VERSION;
|
|
|
|
m->endian = MSG_ENDIAN_HOST;
|
|
|
|
m->rsvd1 = m->rsvd2 = 0;
|
|
|
|
|
|
|
|
/* Format: Seconds.NanoSeconds+Offset(SequenceNumber) Value1 Value2 ...
|
|
|
|
* RegEx: (\d+(?:\.\d+)?)([-+]\d+(?:\.\d+)?(?:e[+-]?\d+)?)?(?:\((\d+)\))?
|
|
|
|
*
|
|
|
|
* Please note that only the seconds and at least one value are mandatory
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Mandatory: seconds */
|
|
|
|
m->ts.sec = (uint32_t) strtoul(ptr, &end, 10);
|
|
|
|
if (ptr == end)
|
|
|
|
return -2;
|
2015-06-02 22:36:17 +02:00
|
|
|
|
2015-10-07 18:12:12 +02:00
|
|
|
/* Optional: nano seconds */
|
|
|
|
if (*end == '.') {
|
|
|
|
ptr = end + 1;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-10-07 18:12:12 +02:00
|
|
|
m->ts.nsec = (uint32_t) strtoul(ptr, &end, 10);
|
|
|
|
if (ptr != end)
|
|
|
|
flags |= MSG_PRINT_NANOSECONDS;
|
|
|
|
else
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m->ts.nsec = 0;
|
|
|
|
|
|
|
|
/* Optional: offset / delay */
|
|
|
|
if (*end == '+' || *end == '-') {
|
|
|
|
ptr = end + 1;
|
2014-07-18 16:05:49 +00:00
|
|
|
|
2015-10-07 18:12:12 +02:00
|
|
|
offset = strtof(ptr, &end); /* offset is ignored for now */
|
|
|
|
if (ptr != end)
|
|
|
|
flags |= MSG_PRINT_OFFSET;
|
|
|
|
else
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Optional: sequence number */
|
|
|
|
if (*end == '(') {
|
|
|
|
ptr = end + 1;
|
|
|
|
|
|
|
|
m->sequence = (uint16_t) strtoul(ptr, &end, 10);
|
|
|
|
if (ptr != end && *end == ')')
|
|
|
|
flags |= MSG_PRINT_SEQUENCE;
|
2016-01-14 22:59:57 +01:00
|
|
|
else
|
2015-10-07 18:12:12 +02:00
|
|
|
return -5;
|
|
|
|
|
|
|
|
end = end + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m->sequence = 0;
|
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
for (m->values = 0, ptr = end; ;
|
|
|
|
m->values++, ptr = end) {
|
2015-10-07 18:12:12 +02:00
|
|
|
|
|
|
|
/** @todo We only support floating point values at the moment */
|
2016-01-14 22:59:57 +01:00
|
|
|
m->data[m->values].f = strtod(ptr, &end);
|
2015-10-07 18:12:12 +02:00
|
|
|
|
|
|
|
if (end == ptr) /* there are no valid FP values anymore */
|
|
|
|
break;
|
2014-09-07 16:29:05 +00:00
|
|
|
}
|
2015-10-07 18:12:12 +02:00
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
if (m->values > 0)
|
2015-10-07 18:12:12 +02:00
|
|
|
flags |= MSG_PRINT_VALUES;
|
|
|
|
|
|
|
|
if (fl)
|
|
|
|
*fl = flags;
|
|
|
|
if (off && (flags & MSG_PRINT_OFFSET))
|
|
|
|
*off = offset;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
return m->values;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 01:48:33 +01:00
|
|
|
int msg_fprint(FILE *f, struct msg *m, int flags, double offset)
|
|
|
|
{
|
|
|
|
char line[4096];
|
|
|
|
|
|
|
|
int len = msg_print(line, sizeof(line), m, flags, offset);
|
|
|
|
|
|
|
|
fputs(line, f);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int msg_fscan(FILE *f, struct msg *m, int *fl, double *off)
|
|
|
|
{
|
|
|
|
char *ptr, line[4096];
|
|
|
|
|
|
|
|
skip: if (fgets(line, sizeof(line), f) == NULL)
|
|
|
|
return -1; /* An error occured */
|
|
|
|
|
|
|
|
/* Skip whitespaces, empty and comment lines */
|
|
|
|
for (ptr = line; isspace(*ptr); ptr++);
|
|
|
|
if (*ptr == '\0' || *ptr == '#')
|
|
|
|
goto skip;
|
|
|
|
|
|
|
|
return msg_scan(line, m, fl, off);
|
|
|
|
}
|