From d6f2697e1b88b7c38a7e22cff7c5082f33d165b9 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sun, 23 Apr 2017 22:12:10 +0200 Subject: [PATCH] added new helper functions to convert struct msg between network and host byte-order --- .../udp/models/send_receive/include/msg.h | 30 ++++++------ .../opal/udp/models/send_receive/src/msg.c | 46 ++++++++++------- include/villas/msg.h | 24 +++++---- lib/msg.c | 49 ++++++++++++------- 4 files changed, 86 insertions(+), 63 deletions(-) diff --git a/clients/opal/udp/models/send_receive/include/msg.h b/clients/opal/udp/models/send_receive/include/msg.h index ee00377ab..7988d21b2 100644 --- a/clients/opal/udp/models/send_receive/include/msg.h +++ b/clients/opal/udp/models/send_receive/include/msg.h @@ -1,27 +1,28 @@ -/** Message related functions. +/** Message related functions * * @file * @author Steffen Vogel * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC *********************************************************************************/ -#ifndef _MSG_H_ -#define _MSG_H_ +#pragma once -#include "msg_format.h" +/* Forward declarations. */ +struct msg; -/** Swaps message contents byte-order. +/** Swaps the byte-order of the message. * - * Message can either be transmitted in little or big endian - * format. The actual endianess for a message is defined by the - * msg::endian field. This covers msg::length, msg::sequence, msg::data and msg::ts fields. - * Received message are usally converted to the endianess of the host. - * This is required for further sanity checks of the sequence number - * or parsing of the data. + * Message are always transmitted in network (big endian) byte order. * * @param m A pointer to the message */ -void msg_swap(struct msg *m); +void msg_hdr_ntoh(struct msg *m); + +void msg_hdr_hton(struct msg *m); + +void msg_ntoh(struct msg *m); + +void msg_hton(struct msg *m); /** Check the consistency of a message. * @@ -31,7 +32,4 @@ void msg_swap(struct msg *m); * @retval 0 The message header is valid. * @retval <0 The message header is invalid. */ -int msg_verify(struct msg *m); - -#endif /* _MSG_H_ */ - +int msg_verify(struct msg *m); \ No newline at end of file diff --git a/clients/opal/udp/models/send_receive/src/msg.c b/clients/opal/udp/models/send_receive/src/msg.c index dd472514e..c61a16345 100644 --- a/clients/opal/udp/models/send_receive/src/msg.c +++ b/clients/opal/udp/models/send_receive/src/msg.c @@ -4,27 +4,41 @@ * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC *********************************************************************************/ -#ifdef __linux__ - #include -#elif defined(__PPC__) /* Xilinx toolchain */ - #include - #define bswap_16(x) Xil_EndianSwap16(x) - #define bswap_32(x) Xil_EndianSwap32(x) -#endif +#include #include "msg.h" +#include "msg_format.h" -void msg_swap(struct msg *m) +void msg_ntoh(struct msg *m) { - m->length = bswap_16(m->length); - m->sequence = bswap_32(m->sequence); - m->ts.sec = bswap_32(m->ts.sec); - m->ts.nsec = bswap_32(m->ts.nsec); + msg_hdr_ntoh(m); for (int i = 0; i < m->length; i++) - m->data[i].i = bswap_32(m->data[i].i); + m->data[i].i = ntohl(m->data[i].i); +} - m->endian ^= 1; +void msg_hton(struct msg *m) +{ + for (int i = 0; i < m->length; i++) + m->data[i].i = htonl(m->data[i].i); + + msg_hdr_hton(m); +} + +void msg_hdr_hton(struct msg *m) +{ + m->length = htons(m->length); + m->sequence = htonl(m->sequence); + m->ts.sec = htonl(m->ts.sec); + m->ts.nsec = htonl(m->ts.nsec); +} + +void msg_hdr_ntoh(struct msg *m) +{ + m->length = ntohs(m->length); + m->sequence = ntohl(m->sequence); + m->ts.sec = ntohl(m->ts.sec); + m->ts.nsec = ntohl(m->ts.nsec); } int msg_verify(struct msg *m) @@ -33,10 +47,8 @@ int msg_verify(struct msg *m) return -1; else if (m->type != MSG_TYPE_DATA) return -2; - else if ((m->length <= 0) || (m->length > MSG_VALUES)) - return -3; else if ((m->rsvd1 != 0) || (m->rsvd2 != 0)) - return -4; + return -3; else return 0; } \ No newline at end of file diff --git a/include/villas/msg.h b/include/villas/msg.h index 130e06be1..7988d21b2 100644 --- a/include/villas/msg.h +++ b/include/villas/msg.h @@ -7,24 +7,22 @@ #pragma once -#include +/* Forward declarations. */ +struct msg; -#include "msg_format.h" - -struct node; - -/** Swaps the byte order of the header part of struct msg. +/** Swaps the byte-order of the message. * - * Message can either be transmitted in little or big endian - * format. The actual endianess for a message is defined by the - * msg::endian field. This covers msg::length, msg::sequence, msg::data and msg::ts fields. - * Received message are usally converted to the endianess of the host. - * This is required for further sanity checks of the sequence number - * or parsing of the data. + * Message are always transmitted in network (big endian) byte order. * * @param m A pointer to the message */ -void msg_hdr_swap(struct msg *m); +void msg_hdr_ntoh(struct msg *m); + +void msg_hdr_hton(struct msg *m); + +void msg_ntoh(struct msg *m); + +void msg_hton(struct msg *m); /** Check the consistency of a message. * diff --git a/lib/msg.c b/lib/msg.c index 006fae1a6..c61a16345 100644 --- a/lib/msg.c +++ b/lib/msg.c @@ -4,26 +4,41 @@ * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC *********************************************************************************/ -#ifdef __linux__ - #include -#elif defined(__PPC__) /* Xilinx toolchain */ - #include - #define bswap_16(x) Xil_EndianSwap16(x) - #define bswap_32(x) Xil_EndianSwap32(x) -#endif +#include #include "msg.h" -#include "node.h" -#include "utils.h" +#include "msg_format.h" -void msg_hdr_swap(struct msg *m) +void msg_ntoh(struct msg *m) { - m->length = bswap_16(m->length); - m->sequence = bswap_32(m->sequence); - m->ts.sec = bswap_32(m->ts.sec); - m->ts.nsec = bswap_32(m->ts.nsec); + msg_hdr_ntoh(m); + + for (int i = 0; i < m->length; i++) + m->data[i].i = ntohl(m->data[i].i); +} - m->endian ^= 1; +void msg_hton(struct msg *m) +{ + for (int i = 0; i < m->length; i++) + m->data[i].i = htonl(m->data[i].i); + + msg_hdr_hton(m); +} + +void msg_hdr_hton(struct msg *m) +{ + m->length = htons(m->length); + m->sequence = htonl(m->sequence); + m->ts.sec = htonl(m->ts.sec); + m->ts.nsec = htonl(m->ts.nsec); +} + +void msg_hdr_ntoh(struct msg *m) +{ + m->length = ntohs(m->length); + m->sequence = ntohl(m->sequence); + m->ts.sec = ntohl(m->ts.sec); + m->ts.nsec = ntohl(m->ts.nsec); } int msg_verify(struct msg *m) @@ -32,8 +47,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; -} +} \ No newline at end of file