mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
removed all 'struct msg' based functions (now in sample data structure)
This commit is contained in:
parent
ac61a8498e
commit
3e85ef8ea4
2 changed files with 1 additions and 207 deletions
|
@ -16,22 +16,6 @@
|
|||
|
||||
struct node;
|
||||
|
||||
/** These flags define the format which is used by msg_fscan() and msg_fprint(). */
|
||||
enum msg_flags {
|
||||
MSG_PRINT_NANOSECONDS = 1,
|
||||
MSG_PRINT_OFFSET = 2,
|
||||
MSG_PRINT_SEQUENCE = 4,
|
||||
MSG_PRINT_VALUES = 8,
|
||||
|
||||
MSG_PRINT_ALL = 0xFF
|
||||
};
|
||||
|
||||
/** Allocate and initialize memory of a sinle message. */
|
||||
struct msg * msg_create(size_t values);
|
||||
|
||||
/** Release memory allocated by msg_create(). */
|
||||
void msg_destroy(struct msg *m);
|
||||
|
||||
/** Swaps message contents byte-order.
|
||||
*
|
||||
* Message can either be transmitted in little or big endian
|
||||
|
@ -55,44 +39,4 @@ void msg_swap(struct msg *m);
|
|||
*/
|
||||
int msg_verify(struct msg *m);
|
||||
|
||||
/** Print a raw message in human readable form to a file stream.
|
||||
*
|
||||
* @param buf A character buffer of len bytes.
|
||||
* @param len The size of buf.
|
||||
* @param m A pointer to the message.
|
||||
* @param flags See msg_flags.
|
||||
* @param offset A optional offset in seconds. Only used if flags contains MSG_PRINT_OFFSET.
|
||||
* @return Number of bytes written to buf.
|
||||
*/
|
||||
int msg_print(char *buf, size_t len, struct msg *m, int flags, double offset);
|
||||
|
||||
/** Read a message from a character buffer.
|
||||
*
|
||||
* @param line A string which should be parsed.
|
||||
* @param m A pointer to the message.
|
||||
* @param flags et MSG_PRINT_* flags for each component found in sample if not NULL. See msg_flags.
|
||||
* @param offset Write offset to this pointer if not NULL.
|
||||
* @retval 0 Success. Everything went well.
|
||||
* @retval <0 Error. Something went wrong.
|
||||
*/
|
||||
int msg_scan(const char *line, struct msg *m, int *fl, double *off);
|
||||
|
||||
/** Print a raw message in human readable form to a file stream.
|
||||
*
|
||||
* @see msg_print()
|
||||
* @param f The file handle from fopen() or stdout, stderr.
|
||||
* @retval 0 Success. Everything went well.
|
||||
* @retval <0 Error. Something went wrong.
|
||||
*/
|
||||
int msg_fprint(FILE *f, struct msg *m, int flags, double offset);
|
||||
|
||||
/** Read a message from a file stream.
|
||||
*
|
||||
* @see msg_scan()
|
||||
* @param f The file handle from fopen() or stdin.
|
||||
* @retval 0 Success. Everything went well.
|
||||
* @retval <0 Error. Something went wrong.
|
||||
*/
|
||||
int msg_fscan(FILE *f, struct msg *m, int *flags, double *offset);
|
||||
|
||||
#endif /* _MSG_H_ */
|
||||
|
|
152
lib/msg.c
152
lib/msg.c
|
@ -22,24 +22,6 @@
|
|||
#include "node.h"
|
||||
#include "utils.h"
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void msg_swap(struct msg *m)
|
||||
{
|
||||
m->values = bswap_16(m->values);
|
||||
|
@ -59,140 +41,8 @@ int msg_verify(struct msg *m)
|
|||
return -1;
|
||||
else if (m->type != MSG_TYPE_DATA)
|
||||
return -2;
|
||||
else if ((m->rsvd1 != 0) || (m->rsvd2 != 0))
|
||||
else if ((m->rsvd1 != 0) || (m->rsvd2 != 0))
|
||||
return -3;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int msg_print(char *buf, size_t len, struct msg *m, int flags, double offset)
|
||||
{
|
||||
size_t off = snprintf(buf, len, "%u", m->ts.sec);
|
||||
|
||||
if (flags & MSG_PRINT_NANOSECONDS)
|
||||
off += snprintf(buf + off, len - off, ".%09u", m->ts.nsec);
|
||||
|
||||
if (flags & MSG_PRINT_OFFSET)
|
||||
off += snprintf(buf + off, len - off, "%+g", offset);
|
||||
|
||||
if (flags & MSG_PRINT_SEQUENCE)
|
||||
off += snprintf(buf + off, len - off, "(%u)", m->sequence);
|
||||
|
||||
if (flags & MSG_PRINT_VALUES) {
|
||||
for (int i = 0; i < m->values; i++)
|
||||
off += snprintf(buf + off, len - off, "\t%.6f", m->data[i].f);
|
||||
}
|
||||
|
||||
off += snprintf(buf + off, len - off, "\n");
|
||||
|
||||
return off + 1; /* trailing '\0' */
|
||||
}
|
||||
|
||||
int msg_scan(const char *line, struct msg *m, int *fl, double *off)
|
||||
{
|
||||
char *end;
|
||||
const char *ptr = line;
|
||||
|
||||
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;
|
||||
|
||||
/* Optional: nano seconds */
|
||||
if (*end == '.') {
|
||||
ptr = end + 1;
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
else
|
||||
return -5;
|
||||
|
||||
end = end + 1;
|
||||
}
|
||||
else
|
||||
m->sequence = 0;
|
||||
|
||||
for (m->values = 0, ptr = end; ;
|
||||
m->values++, ptr = end) {
|
||||
|
||||
/** @todo We only support floating point values at the moment */
|
||||
m->data[m->values].f = strtod(ptr, &end);
|
||||
|
||||
if (end == ptr) /* there are no valid FP values anymore */
|
||||
break;
|
||||
}
|
||||
|
||||
if (m->values > 0)
|
||||
flags |= MSG_PRINT_VALUES;
|
||||
|
||||
if (fl)
|
||||
*fl = flags;
|
||||
if (off && (flags & MSG_PRINT_OFFSET))
|
||||
*off = offset;
|
||||
|
||||
return m->values;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue