1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

added new helper functions to convert struct msg between network and host byte-order

This commit is contained in:
Steffen Vogel 2017-04-23 22:12:10 +02:00
parent 1c95caac50
commit d6f2697e1b
4 changed files with 86 additions and 63 deletions

View file

@ -1,27 +1,28 @@
/** Message related functions.
/** Message related functions
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @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);

View file

@ -4,27 +4,41 @@
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
*********************************************************************************/
#ifdef __linux__
#include <byteswap.h>
#elif defined(__PPC__) /* Xilinx toolchain */
#include <xil_io.h>
#define bswap_16(x) Xil_EndianSwap16(x)
#define bswap_32(x) Xil_EndianSwap32(x)
#endif
#include <arpa/inet.h>
#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;
}

View file

@ -7,24 +7,22 @@
#pragma once
#include <stdio.h>
/* 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.
*

View file

@ -4,26 +4,41 @@
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
*********************************************************************************/
#ifdef __linux__
#include <byteswap.h>
#elif defined(__PPC__) /* Xilinx toolchain */
#include <xil_io.h>
#define bswap_16(x) Xil_EndianSwap16(x)
#define bswap_32(x) Xil_EndianSwap32(x)
#endif
#include <arpa/inet.h>
#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;
}
}