diff --git a/include/msg.h b/include/msg.h index 57a513017..02f7fa4e9 100644 --- a/include/msg.h +++ b/include/msg.h @@ -14,45 +14,57 @@ #include "config.h" #include "node.h" +#if PROTOCOL == 0 /** - * The structure of a message (OPAL-RT example format) + * The format of a message (OPAL-RT example format) * - * This struct defines the format of a message. + * This struct defines the format of a message (protocol version 0) * Its declared as "packed" because it represents the "on wire" data. */ -#if PROTOCOL != 0 - #error "Unknown protocol version!" -#endif - struct msg { /// Sender device ID - uint16_t dev_id; + uint16_t device; /// Message ID - uint32_t msg_id; - /// Message length (data only) - uint16_t msg_len; + uint32_t sequence; /// Message length (data only) + uint16_t length; + /// Message data double data[MAX_VALUES]; } __attribute__((packed)); +/** + * @brief Print a raw UDP packge in human readable form + * + * @param f The file stream + * @param msg A pointer to the message + * @return + * - 0 on success + * - otherwise an error occured + */ +int msg_fprint(FILE *f, struct msg *m); /** - * Print a raw UDP packge in human readable form + * @brief Read a message from a file stream * - * @param fd The file descriptor - * @param msg A pointer to the UDP message + * @param f The file stream + * @param m A pointer to the message + * @return + * - 0 on success + * - otherwise an error occured */ -void msg_fprint(FILE *f, struct msg *m); +int msg_fscan(FILE *f, struct msg *m); /** - * Craft message with random-walking values + * @brief Change the values of an existing message in a random fashion + * + * @param msg A pointer to the message whose values will be updated + */ +void msg_random(struct msg *m); + +/** + * * - * NOTE: random-walking behaviour is not reentrant! - * - * @param msg A pointer to a struct msg - * @param dev_id The device id of the message */ -void msg_random(struct msg *m, short dev_id); #endif /* _MSG_H_ */ diff --git a/src/msg.c b/src/msg.c index 70c1bc8c4..b71946252 100644 --- a/src/msg.c +++ b/src/msg.c @@ -7,31 +7,43 @@ #include #include -#include #include "msg.h" -void msg_fprint(FILE *f, struct msg *msg) +int msg_fprint(FILE *f, struct msg *msg) { - fprintf(f, "p: dev_id = %u, msg_id = %4u, data", msg->dev_id, msg->msg_id); + fprintf(f, "%-8u %-8u", msg->device, msg->sequence); - for (int i = 0; i < msg->msg_len / sizeof(double); i++) - fprintf(f, "%8.3f", msg->data[i]); + for (int i = 0; i < msg->length / sizeof(double); i++) { + fprintf(f, "%-12.6f ", msg->data[i]); + } fprintf(f, "\n"); + + return 0; } -void msg_random(struct msg *msg, short dev_id) +int msg_fscan(FILE *f, struct msg *msg) { - static uint16_t msg_id; - static double data[4]; + fscanf(f, "%8u %8u ", &msg->device, &msg->sequence); - for (int i = 0; i < MAX_VALUES; i++) - data[i] += (double) random() / RAND_MAX - .5; + for (int i = 0; i < msg->length / sizeof(double); i++) { + fscanf(f, "%12lf ", &msg->data[i]); + } - msg->msg_id = ++msg_id; - msg->dev_id = dev_id; - msg->msg_len = sizeof(data); - memcpy(&msg->data, data, msg->msg_len); + fscanf(f, "\n"); + + return 0; +} + +void msg_random(struct msg *m) +{ + int values = m->length / sizeof(double); + + for (int i = 0; i < values; i++) { + m->data[i] += (double) random() / RAND_MAX - .5; + } + + m->sequence++; }