From c9ed2a1f74855ffdb75f6571b7e1e65881672669 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 5 Jun 2015 12:15:31 +0200 Subject: [PATCH] cleanup codestyle --- .../models/send_receive/include/msg.h | 11 +++++ .../models/send_receive/include/utils.h | 6 +-- .../opal_udp/models/send_receive/src/s2ss.c | 47 ++++++++----------- server/include/msg.h | 2 +- server/src/test.c | 3 +- server/src/utils.c | 9 ++-- 6 files changed, 43 insertions(+), 35 deletions(-) diff --git a/clients/opal_udp/models/send_receive/include/msg.h b/clients/opal_udp/models/send_receive/include/msg.h index f26ea65bd..70d5ae4b7 100644 --- a/clients/opal_udp/models/send_receive/include/msg.h +++ b/clients/opal_udp/models/send_receive/include/msg.h @@ -12,6 +12,17 @@ #include "msg_format.h" +/** Swap a message to host byte order. + * + * Message can either be transmitted in little or big endian + * format. The actual endianess for a message is defined by the + * msg::byteorder field. + * 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. + * + * @param m A pointer to the message + */ void msg_swap(struct msg *m); /** Check the consistency of a message. diff --git a/clients/opal_udp/models/send_receive/include/utils.h b/clients/opal_udp/models/send_receive/include/utils.h index c1783efbb..ddb1733fc 100644 --- a/clients/opal_udp/models/send_receive/include/utils.h +++ b/clients/opal_udp/models/send_receive/include/utils.h @@ -9,11 +9,11 @@ * Unauthorized copying of this file, via any medium is strictly prohibited. *********************************************************************************/ -#ifndef _SCHED_H_ -#define _SCHED_H_ +#ifndef _UTILS_H_ +#define _UTILS_H_ #define EOK 0 int AssignProcToCpu0(void); -#endif /* _SCHED_H_ */ +#endif /* _UTILS_H_ */ diff --git a/clients/opal_udp/models/send_receive/src/s2ss.c b/clients/opal_udp/models/send_receive/src/s2ss.c index 753131cb2..8c44ed0b8 100644 --- a/clients/opal_udp/models/send_receive/src/s2ss.c +++ b/clients/opal_udp/models/send_receive/src/s2ss.c @@ -5,12 +5,14 @@ * receive data to and from the asynchronous icons and a UDP or TCP * port. * - * @author Steffen Vogel - * @author Mathieu Dubé-Dallaire - * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC - * @copyright 2003, OPAL-RT Technologies inc * @file - */ + * @author Steffen Vogel + * @author Mathieu DubĂ©-Dallaire + * @copyright 2003, OPAL-RT Technologies inc + * @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC + * This file is part of S2SS. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + *********************************************************************************/ /* Standard ANSI C headers needed for this program */ #include @@ -25,12 +27,12 @@ #include #if defined(__QNXNTO__) -# include -# include -# include -# include + #include + #include + #include + #include #elif defined(__linux__) -# define _GNU_SOURCE 1 + #define _GNU_SOURCE 1 #endif /* Define RTLAB before including OpalPrint.h for messages to be sent @@ -51,7 +53,7 @@ #define PRINT_SHMEM_NAME argv[3] #ifdef _DEBUG // TODO: workaround -#define CPU_TICKS 3466948000 + #define CPU_TICKS 3466948000 struct msg *msg_send = NULL; void Tick(int sig, siginfo_t *si, void *ptr) @@ -87,7 +89,7 @@ static void *SendToIPPort(void *arg) /* Data from the S2SS server */ struct msg msg = MSG_INIT(0); - int msg_size; + int len; #ifdef _DEBUG // TODO: workaround msg_send = &msg; @@ -124,19 +126,16 @@ static void *SendToIPPort(void *arg) /* Read data from the model */ OpalGetAsyncSendIconData(mdldata, mdldata_size, SendID); -/******* FORMAT TO SPECIFIC PROTOCOL HERE *****************************/ - // msg.dev_id = SendID; /* Use the SendID as a device ID here */ msg.sequence = htons(seq++); msg.length = mdldata_size / sizeof(double); for (i = 0; i < msg.length; i++) msg.data[i].f = (float) mdldata[i]; - msg_size = MSG_LEN(&msg); -/**********************************************************************/ + len = MSG_LEN(&msg); /* Perform the actual write to the ip port */ - if (SendPacket((char *) &msg, msg_size) < 0) + if (SendPacket((char *) &msg, len) < 0) OpalSetAsyncSendIconError(errno, SendID); else OpalSetAsyncSendIconError(0, SendID); @@ -176,15 +175,13 @@ static void *RecvFromIPPort(void *arg) /* Data from the S2SS server */ struct msg msg = MSG_INIT(0); - unsigned msg_size; + int len; OpalPrint("%s: RecvFromIPPort thread started\n", PROGNAME); OpalGetNbAsyncRecvIcon(&nbRecv); if (nbRecv >= 1) { do { - -/******* FORMAT TO SPECIFIC PROTOCOL HERE ******************************/ n = RecvPacket((char *) &msg, sizeof(msg), 1.0); int ret = msg_verify(m); @@ -194,14 +191,12 @@ static void *RecvFromIPPort(void *arg) } /** @todo: We may check the sequence number here. */ - msg.sequence = ntohs(msg.sequence); if (msg.endian != MSG_ENDIAN_HOST) msg_swap(&msg); - msg_size = MSG_LEN(&msg); -/***********************************************************************/ + len = MSG_LEN(&msg); if (n < 1) { ModelState = OpalGetAsyncModelState(); @@ -216,12 +211,11 @@ static void *RecvFromIPPort(void *arg) } break; } - else if (n != msg_size) { - OpalPrint("%s: Received incoherent packet (size: %d, complete: %d)\n", PROGNAME, n, msg_size); + else if (n != len) { + OpalPrint("%s: Received incoherent packet (size: %d, complete: %d)\n", PROGNAME, n, len); continue; } -/******* FORMAT TO SPECIFIC PROTOCOL HERE *******************************/ OpalSetAsyncRecvIconStatus(msg.sequence, RecvID); /* Set the Status to the message ID */ OpalSetAsyncRecvIconError(0, RecvID); /* Set the Error to 0 */ @@ -241,7 +235,6 @@ static void *RecvFromIPPort(void *arg) for (i = 0; i < msg.length; i++) mdldata[i] = (double) msg.data[i].f; -/************************************************************************/ OpalSetAsyncRecvIconData(mdldata, mdldata_size, RecvID); diff --git a/server/include/msg.h b/server/include/msg.h index 676443d23..949cf52d3 100644 --- a/server/include/msg.h +++ b/server/include/msg.h @@ -39,7 +39,7 @@ void msg_swap(struct msg *m); */ int msg_verify(struct msg *m); -/** Print a raw UDP message in human readable form. +/** Print a raw message in human readable form to a file stream. * * @param f The file stream * @param m A pointer to the message diff --git a/server/src/test.c b/server/src/test.c index 7f68a5371..0b0112640 100644 --- a/server/src/test.c +++ b/server/src/test.c @@ -53,7 +53,7 @@ static double res = 1e-5; /* Prototypes */ void test_rtt(); -void quit(int sig, siginfo_t *si, void *ptr) +void quit() { running = 0; } @@ -187,6 +187,7 @@ void test_rtt() { 1e3 * hist_mean(&hist), 1e3 * hist_stddev(&hist)); } + /* Housekeeping */ free(ts2); hist_print(&hist); diff --git a/server/src/utils.c b/server/src/utils.c index 8598637a2..9e0b7fa53 100644 --- a/server/src/utils.c +++ b/server/src/utils.c @@ -1,4 +1,4 @@ -/** Some helper functions. +/** General purpose helper functions. * * @author Steffen Vogel * @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC @@ -19,8 +19,6 @@ #include "cfg.h" #include "utils.h" -pthread_t _mtid; - double box_muller(float m, float s) { double x1, x2, y1; @@ -53,6 +51,11 @@ double randf() return (double) random() / RAND_MAX; } +/** This global variable holds the main thread ID. + * Its used by die(). + */ +pthread_t _mtid; + void die() { if (pthread_equal(_mtid, pthread_self()))