mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
simplified socket packet format by removing endianess flag: all values are now in network byte-order
This commit is contained in:
parent
6989ac06af
commit
1c95caac50
3 changed files with 44 additions and 128 deletions
|
@ -5,93 +5,64 @@
|
|||
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
||||
*********************************************************************************/
|
||||
|
||||
#ifndef _MSG_FORMAT_H_
|
||||
#define _MSG_FORMAT_H_
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __linux__
|
||||
#define _BSD_SOURCE 1
|
||||
#include <endian.h>
|
||||
#elif defined(__PPC__) /* Xilinx toolchain */
|
||||
#include <lwip/arch.h>
|
||||
#endif
|
||||
|
||||
#include "config.h"
|
||||
|
||||
/** Maximum number of dword values in a message */
|
||||
#define MSG_VALUES MAX_VALUES
|
||||
|
||||
/** The current version number for the message format */
|
||||
#define MSG_VERSION 1
|
||||
#define MSG_VERSION 2
|
||||
|
||||
/** @todo Implement more message types */
|
||||
#define MSG_TYPE_DATA 0 /**< Message contains float values */
|
||||
#define MSG_TYPE_START 1 /**< Message marks the beginning of a new simulation case */
|
||||
#define MSG_TYPE_STOP 2 /**< Message marks the end of a simulation case */
|
||||
|
||||
#define MSG_ENDIAN_LITTLE 0 /**< Message values are in little endian format (float too!) */
|
||||
#define MSG_ENDIAN_BIG 1 /**< Message values are in bit endian format */
|
||||
/** The total size in bytes of a message */
|
||||
#define MSG_LEN(values) (sizeof(struct msg) + MSG_DATA_LEN(values))
|
||||
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
#define MSG_ENDIAN_HOST MSG_ENDIAN_LITTLE
|
||||
#elif BYTE_ORDER == BIG_ENDIAN
|
||||
#define MSG_ENDIAN_HOST MSG_ENDIAN_BIG
|
||||
#else
|
||||
#error "Unknown byte order!"
|
||||
#endif
|
||||
/** The length of \p values values in bytes. */
|
||||
#define MSG_DATA_LEN(values) (sizeof(float) * (values))
|
||||
|
||||
/** The total length of a message */
|
||||
#define MSG_LEN(msg) (4 * ((msg)->length + 4))
|
||||
/** The offset to the first data value in a message. */
|
||||
#define MSG_DATA_OFFSET(msg) ((char *) (msg) + offsetof(struct msg, data))
|
||||
|
||||
/** Initialize a message with default values */
|
||||
#define MSG_INIT(len, seq) (struct msg) {\
|
||||
.version = MSG_VERSION, \
|
||||
.type = MSG_TYPE_DATA, \
|
||||
.length = len, \
|
||||
.sequence = seq \
|
||||
}
|
||||
|
||||
/** The timestamp of a message in struct timespec format */
|
||||
#define MSG_TS(msg) (struct timespec) { \
|
||||
.tv_sec = (msg)->ts.sec, \
|
||||
.tv_nsec = (msg)->ts.nsec \
|
||||
}
|
||||
|
||||
/** Initialize a message */
|
||||
#define MSG_INIT(i) (struct msg) { \
|
||||
.version = MSG_VERSION, \
|
||||
.type = MSG_TYPE_DATA, \
|
||||
.endian = MSG_ENDIAN_HOST, \
|
||||
.length = i, \
|
||||
.sequence = 0, \
|
||||
.rsvd1 = 0, .rsvd2 = 0 \
|
||||
}
|
||||
|
||||
/** This message format is used by all clients
|
||||
*
|
||||
* @diafile msg_format.dia
|
||||
**/
|
||||
struct msg
|
||||
{
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
unsigned version: 4; /**< Specifies the format of the remaining message (see MGS_VERSION) */
|
||||
unsigned type : 2; /**< Data or control message (see MSG_TYPE_*) */
|
||||
unsigned endian : 1; /**< Specifies the byteorder of the message (see MSG_ENDIAN_*) */
|
||||
unsigned rsvd1 : 1; /**< Reserved bits */
|
||||
#elif BYTE_ORDER == LITTLE_ENDIAN
|
||||
unsigned rsvd1 : 1; /**< Reserved bits */
|
||||
unsigned endian : 1; /**< Specifies the byteorder of the message (see MSG_ENDIAN_*) */
|
||||
unsigned type : 2; /**< Data or control message (see MSG_TYPE_*) */
|
||||
unsigned version: 4; /**< Specifies the format of the remaining message (see MGS_VERSION) */
|
||||
#endif
|
||||
unsigned rsvd1 : 2; /**< Reserved bits */
|
||||
unsigned rsvd2 : 8; /**< Reserved bits */
|
||||
|
||||
uint16_t length; /**< The number of values in msg::data[]. Endianess is specified in msg::endian. */
|
||||
uint32_t sequence; /**< The sequence number is incremented by one for consecutive messages. Endianess is specified in msg::endian. */
|
||||
uint16_t length; /**< The number of values in msg::data[]. */
|
||||
uint32_t sequence; /**< The sequence number is incremented by one for consecutive messages. */
|
||||
|
||||
/** A timestamp per message. Endianess is specified in msg::endian. */
|
||||
/** A timestamp per message. */
|
||||
struct {
|
||||
uint32_t sec; /**< Seconds since 1970-01-01 00:00:00 */
|
||||
uint32_t sec; /**< Seconds since 1970-01-01 00:00:00 */
|
||||
uint32_t nsec; /**< Nanoseconds of the current second. */
|
||||
} ts;
|
||||
|
||||
/** The message payload. Endianess is specified in msg::endian. */
|
||||
/** The message payload. */
|
||||
union {
|
||||
float f; /**< Floating point values (note msg::endian) */
|
||||
uint32_t i; /**< Integer values (note msg::endian) */
|
||||
} data[MSG_VALUES];
|
||||
} __attribute__((aligned(64), packed));
|
||||
|
||||
#endif /* _MSG_FORMAT_H_ */
|
||||
float f; /**< Floating point values. */
|
||||
uint32_t i; /**< Integer values. */
|
||||
} data[];
|
||||
} __attribute__((packed));
|
|
@ -9,32 +9,14 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __linux__
|
||||
#define _BSD_SOURCE 1
|
||||
#include <endian.h>
|
||||
#elif defined(__PPC__) /* Xilinx toolchain */
|
||||
#include <lwip/arch.h>
|
||||
#endif
|
||||
|
||||
/** The current version number for the message format */
|
||||
#define MSG_VERSION 1
|
||||
#define MSG_VERSION 2
|
||||
|
||||
/** @todo Implement more message types */
|
||||
#define MSG_TYPE_DATA 0 /**< Message contains float values */
|
||||
#define MSG_TYPE_START 1 /**< Message marks the beginning of a new simulation case */
|
||||
#define MSG_TYPE_STOP 2 /**< Message marks the end of a simulation case */
|
||||
|
||||
#define MSG_ENDIAN_LITTLE 0 /**< Message values are in little endian format (float too!) */
|
||||
#define MSG_ENDIAN_BIG 1 /**< Message values are in bit endian format */
|
||||
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
#define MSG_ENDIAN_HOST MSG_ENDIAN_LITTLE
|
||||
#elif BYTE_ORDER == BIG_ENDIAN
|
||||
#define MSG_ENDIAN_HOST MSG_ENDIAN_BIG
|
||||
#else
|
||||
#error "Unknown byte order!"
|
||||
#endif
|
||||
|
||||
/** The total size in bytes of a message */
|
||||
#define MSG_LEN(values) (sizeof(struct msg) + MSG_DATA_LEN(values))
|
||||
|
||||
|
@ -48,7 +30,6 @@
|
|||
#define MSG_INIT(len, seq) (struct msg) {\
|
||||
.version = MSG_VERSION, \
|
||||
.type = MSG_TYPE_DATA, \
|
||||
.endian = MSG_ENDIAN_HOST, \
|
||||
.length = len, \
|
||||
.sequence = seq \
|
||||
}
|
||||
|
@ -65,31 +46,23 @@
|
|||
**/
|
||||
struct msg
|
||||
{
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
unsigned version: 4; /**< Specifies the format of the remaining message (see MGS_VERSION) */
|
||||
unsigned type : 2; /**< Data or control message (see MSG_TYPE_*) */
|
||||
unsigned endian : 1; /**< Specifies the byteorder of the message (see MSG_ENDIAN_*) */
|
||||
unsigned rsvd1 : 1; /**< Reserved bits */
|
||||
#elif BYTE_ORDER == LITTLE_ENDIAN
|
||||
unsigned rsvd1 : 1; /**< Reserved bits */
|
||||
unsigned endian : 1; /**< Specifies the byteorder of the message (see MSG_ENDIAN_*) */
|
||||
unsigned type : 2; /**< Data or control message (see MSG_TYPE_*) */
|
||||
unsigned version: 4; /**< Specifies the format of the remaining message (see MGS_VERSION) */
|
||||
#endif
|
||||
unsigned rsvd1 : 2; /**< Reserved bits */
|
||||
unsigned rsvd2 : 8; /**< Reserved bits */
|
||||
|
||||
uint16_t length; /**< The number of values in msg::data[]. Endianess is specified in msg::endian. */
|
||||
uint32_t sequence; /**< The sequence number is incremented by one for consecutive messages. Endianess is specified in msg::endian. */
|
||||
uint16_t length; /**< The number of values in msg::data[]. */
|
||||
uint32_t sequence; /**< The sequence number is incremented by one for consecutive messages. */
|
||||
|
||||
/** A timestamp per message. Endianess is specified in msg::endian. */
|
||||
/** A timestamp per message. */
|
||||
struct {
|
||||
uint32_t sec; /**< Seconds since 1970-01-01 00:00:00 */
|
||||
uint32_t nsec; /**< Nanoseconds of the current second. */
|
||||
} ts;
|
||||
|
||||
/** The message payload. Endianess is specified in msg::endian. */
|
||||
/** The message payload. */
|
||||
union {
|
||||
float f; /**< Floating point values (note msg::endian) */
|
||||
uint32_t i; /**< Integer values (note msg::endian) */
|
||||
float f; /**< Floating point values. */
|
||||
uint32_t i; /**< Integer values. */
|
||||
} data[];
|
||||
} __attribute__((packed));
|
|
@ -11,34 +11,14 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "msg_format.h"
|
||||
|
||||
#ifdef __linux__
|
||||
#define _BSD_SOURCE 1
|
||||
#include <endian.h>
|
||||
#elif defined(__PPC__) /* Xilinx toolchain */
|
||||
#include <lwip/arch.h>
|
||||
#endif
|
||||
|
||||
/** The current version number for the message format */
|
||||
#define WEBMSG_VERSION 1
|
||||
#define WEBMSG_VERSION 2
|
||||
|
||||
/** @todo Implement more message types */
|
||||
#define WEBMSG_TYPE_DATA 0 /**< Message contains float values */
|
||||
#define WEBMSG_TYPE_START 1 /**< Message marks the beginning of a new simulation case */
|
||||
#define WEBMSG_TYPE_STOP 2 /**< Message marks the end of a simulation case */
|
||||
|
||||
#define WEBMSG_ENDIAN_LITTLE 0 /**< Message values are in little endian format (float too!) */
|
||||
#define WEBMSG_ENDIAN_BIG 1 /**< Message values are in bit endian format */
|
||||
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
#define WEBMSG_ENDIAN_HOST MSG_ENDIAN_LITTLE
|
||||
#elif BYTE_ORDER == BIG_ENDIAN
|
||||
#define WEBMSG_ENDIAN_HOST MSG_ENDIAN_BIG
|
||||
#else
|
||||
#error "Unknown byte order!"
|
||||
#endif
|
||||
|
||||
/** The total size in bytes of a message */
|
||||
#define WEBMSG_LEN(values) (sizeof(struct webmsg) + MSG_DATA_LEN(values))
|
||||
|
||||
|
@ -69,33 +49,25 @@
|
|||
**/
|
||||
struct webmsg
|
||||
{
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
unsigned version: 4; /**< Specifies the format of the remaining message (see MGS_VERSION) */
|
||||
unsigned type : 2; /**< Data or control message (see MSG_TYPE_*) */
|
||||
unsigned endian : 1; /**< Specifies the byteorder of the message (see MSG_ENDIAN_*) */
|
||||
unsigned rsvd1 : 1; /**< Reserved bits */
|
||||
#elif BYTE_ORDER == LITTLE_ENDIAN
|
||||
unsigned rsvd1 : 1; /**< Reserved bits */
|
||||
unsigned endian : 1; /**< Specifies the byteorder of the message (see MSG_ENDIAN_*) */
|
||||
unsigned type : 2; /**< Data or control message (see MSG_TYPE_*) */
|
||||
unsigned version: 4; /**< Specifies the format of the remaining message (see MGS_VERSION) */
|
||||
#endif
|
||||
unsigned rsvd1 : 2; /**< Reserved bits */
|
||||
|
||||
uint8_t id; /**< The node index from / to which this sample received / sent to.
|
||||
uint8_t id; /**< The node index from / to which this sample received / sent to.
|
||||
* Corresponds to the index of the node in the http://localhost/nodes.json array. */
|
||||
|
||||
uint16_t length; /**< The number of values in msg::data[]. Endianess is specified in msg::endian. */
|
||||
uint32_t sequence; /**< The sequence number is incremented by one for consecutive messages. Endianess is specified in msg::endian. */
|
||||
uint16_t length; /**< The number of values in msg::data[]. */
|
||||
uint32_t sequence; /**< The sequence number is incremented by one for consecutive messages. */
|
||||
|
||||
/** A timestamp per message. Endianess is specified in msg::endian. */
|
||||
/** A timestamp per message. */
|
||||
struct {
|
||||
uint32_t sec; /**< Seconds since 1970-01-01 00:00:00 */
|
||||
uint32_t nsec; /**< Nanoseconds of the current second. */
|
||||
} ts;
|
||||
|
||||
/** The message payload. Endianess is specified in msg::endian. */
|
||||
/** The message payload. */
|
||||
union {
|
||||
float f; /**< Floating point values (note msg::endian) */
|
||||
uint32_t i; /**< Integer values (note msg::endian) */
|
||||
float f; /**< Floating point values. */
|
||||
uint32_t i; /**< Integer values. */
|
||||
} data[];
|
||||
} __attribute__((packed));
|
Loading…
Add table
Reference in a new issue