mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
updated message functions
added documentation git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@25 8ec27952-4edc-4aab-86aa-e87bb2611832
This commit is contained in:
parent
332e09e24e
commit
b7cafc080c
2 changed files with 58 additions and 34 deletions
|
@ -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_ */
|
||||
|
|
40
src/msg.c
40
src/msg.c
|
@ -7,31 +7,43 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#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++;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue