diff --git a/clients/opal/models/AsyncIP_sl/AsyncIP.c b/clients/opal/models/AsyncIP_sl/src/AsyncIP.c similarity index 89% rename from clients/opal/models/AsyncIP_sl/AsyncIP.c rename to clients/opal/models/AsyncIP_sl/src/AsyncIP.c index 05444ec79..32f64421f 100644 --- a/clients/opal/models/AsyncIP_sl/AsyncIP.c +++ b/clients/opal/models/AsyncIP_sl/src/AsyncIP.c @@ -33,29 +33,30 @@ # define _GNU_SOURCE 1 #endif -/* This is the message format */ -#include "msg_types.h" - /* Define RTLAB before including OpalPrint.h for messages to be sent * to the OpalDisplay. Otherwise stdout will be used. */ #define RTLAB #include "OpalPrint.h" #include "AsyncApi.h" -#include "AsyncIPUtils.h" + +/* This is the message format */ +#include "MsgFormat.h" +#include "Socket.h" +#include "Interface.h" /* This is just for initializing the shared memory access to communicate * with the RT-LAB model. It's easier to remember the arguments like this */ -#define ASYNC_SHMEM_NAME argv[1] -#define ASYNC_SHMEM_SIZE atoi(argv[2]) -#define PRINT_SHMEM_NAME argv[3] +#define ASYNC_SHMEM_NAME argv[1] +#define ASYNC_SHMEM_SIZE atoi(argv[2]) +#define PRINT_SHMEM_NAME argv[3] /* This defines the maximum number of signals (doubles) that can be sent * or received by any individual Send or Recv block in the model. This * only applies to the "model <-> asynchronous process" communication. */ -#define MAXSENDSIZE 64 -#define MAXRECVSIZE 64 +#define MAX_SEND_SIZE 64 +#define MAX_RECV_SIZE 64 -void *SendToIPPort(void * arg) +static void *SendToIPPort(void *arg) { int SendID = 1; int i, n; @@ -106,14 +107,12 @@ void *SendToIPPort(void * arg) OpalGetAsyncSendIconData(mdldata, mdldata_size, SendID); /******* FORMAT TO SPECIFIC PROTOCOL HERE *****************************/ - // msg.dev_id = SendID; /* Use the SendID as a device ID here */ msg.sequence++; msg.length = mdldata_size / sizeof(double); for (i = 0; i < msg.length; i++) msg.data[i] = (float) mdldata[i]; - /**********************************************************************/ /* Perform the actual write to the ip port */ @@ -144,7 +143,7 @@ void *SendToIPPort(void * arg) return NULL; } -void *RecvFromIPPort(void * arg) +static void *RecvFromIPPort(void *arg) { int RecvID = 1; int i, n; @@ -162,14 +161,21 @@ void *RecvFromIPPort(void * arg) OpalGetNbAsyncRecvIcon(&nbRecv); if (nbRecv >= 1) { do { - memset(&msg, 0, sizeof(msg)); -/******* FORMAT TO SPECIFIC PROTOCOL HERE ****************************** - * Modify this section if your protocol needs to receive more than one - * packet to process the data */ +/******* FORMAT TO SPECIFIC PROTOCOL HERE ******************************/ msg_size = sizeof(msg); n = RecvPacket((char *) &msg, msg_size, 1.0); + if (msg.version != MSG_VERSION) { + OpalPrint("%s: Received message with unknown version. Skipping..\n", PROGNAME); + continue; + } + + if (msg.type != MSG_TYPE_DATA) { + OpalPrint("%s: Received no data. Skipping..\n", PROGNAME); + continue; + } + msg_size = 4 * (msg.length + 1); /***********************************************************************/ @@ -199,13 +205,13 @@ void *RecvFromIPPort(void * arg) /* Get the number of signals to send back to the model */ OpalGetAsyncRecvIconDataLength(&mdldata_size, RecvID); - if (mdldata_size / sizeof(double) > MAX_VALUES) { + if (mdldata_size / sizeof(double) > MAX_RECV_VALUES) { OpalPrint("%s: Number of signals for RecvID=%d (%d) exceeds allowed maximum (%d)\n", - PROGNAME, RecvID, mdldata_size/sizeof(double), MAXRECVSIZE); + PROGNAME, RecvID, mdldata_size / sizeof(double), MAX_RECV_SIZE); return NULL; } - if (mdldata_size > msg.length * sizeof(float)) { + if (mdldata_size / sizeof(double) > msg.length) { OpalPrint("%s: Number of signals for RecvID=%d (%d) exceeds what was received (%d)\n", PROGNAME, RecvID, mdldata_size / sizeof(double), msg.length); } @@ -232,9 +238,11 @@ void *RecvFromIPPort(void * arg) int main(int argc, char *argv[]) { - Opal_GenAsyncParam_Ctrl IconCtrlStruct; int err; - pthread_t tid_send, tid_recv; + + Opal_GenAsyncParam_Ctrl IconCtrlStruct; + + pthread_t tid_send, tid_recv; pthread_attr_t attr_send, attr_recv; /* Check for the proper arguments to the program */ diff --git a/clients/opal/models/AsyncIP_sl/AsyncIP.mk b/clients/opal/models/AsyncIP_sl/src/AsyncIP.mk similarity index 90% rename from clients/opal/models/AsyncIP_sl/AsyncIP.mk rename to clients/opal/models/AsyncIP_sl/src/AsyncIP.mk index 0dded5209..5af739955 100644 --- a/clients/opal/models/AsyncIP_sl/AsyncIP.mk +++ b/clients/opal/models/AsyncIP_sl/src/AsyncIP.mk @@ -6,11 +6,9 @@ TARGET_OPALRT_ROOT = /usr/opalrt # QNX v6.x ifeq "$(SYSNAME)" "nto" - CC = gcc LD = $(CC) - TARGET_LIB = -lsocket - + TARGET_LIB = -lsocket endif # RedHawk Linux @@ -38,7 +36,6 @@ ifeq "$(shell uname)" "Linux" endif TARGET_LIB = -lpthread -lm -ldl -lutil -lrt $(RH_LIBS) $(INTEL_LIBS) - endif # Support for debugging symbols @@ -50,12 +47,11 @@ else LD_DEBUG_OPTS= endif - INCLUDES = -I. LIBPATH = -L. CC_OPTS = LD_OPTS = -OBJS = ${PROGRAM}.o +OBJS = ${PROGRAM}.o Sched.o Interface.o Socket.o ADDLIB = -lOpalCore -lOpalUtils LIBS = -lOpalAsyncApiCore $(ADDLIB) $(TARGET_LIB) diff --git a/clients/opal/models/AsyncIP_sl/interface.c b/clients/opal/models/AsyncIP_sl/src/Interface.c similarity index 69% rename from clients/opal/models/AsyncIP_sl/interface.c rename to clients/opal/models/AsyncIP_sl/src/Interface.c index bb73bc6ea..8881ee374 100644 --- a/clients/opal/models/AsyncIP_sl/interface.c +++ b/clients/opal/models/AsyncIP_sl/src/Interface.c @@ -1,4 +1,11 @@ +/** OPAL Interface setup + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + * @file + */ +#include "Interface.h" int if_setup(const char *op, const char *iface, const char *addr) { diff --git a/clients/opal/models/AsyncIP_sl/src/Interface.h b/clients/opal/models/AsyncIP_sl/src/Interface.h new file mode 100644 index 000000000..288aef7f9 --- /dev/null +++ b/clients/opal/models/AsyncIP_sl/src/Interface.h @@ -0,0 +1,13 @@ +/** OPAL Interface setup + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + * @file + */ + +#ifndef _INTERFACE_H_ +#define _INTERFACE_H_ + +int if_setup(const char *op, const char *iface, const char *addr) + +#endif /* _INTERFACE_H_ */ diff --git a/clients/opal/models/AsyncIP_sl/msg_format.h b/clients/opal/models/AsyncIP_sl/src/MsgFormat.h similarity index 100% rename from clients/opal/models/AsyncIP_sl/msg_format.h rename to clients/opal/models/AsyncIP_sl/src/MsgFormat.h diff --git a/clients/opal/models/AsyncIP_sl/sched.c b/clients/opal/models/AsyncIP_sl/src/Sched.c similarity index 98% rename from clients/opal/models/AsyncIP_sl/sched.c rename to clients/opal/models/AsyncIP_sl/src/Sched.c index 065bc46bf..4c3baa7e4 100644 --- a/clients/opal/models/AsyncIP_sl/sched.c +++ b/clients/opal/models/AsyncIP_sl/src/Sched.c @@ -7,6 +7,7 @@ * @file */ +#include "Sched.h" #if defined(__QNXNTO__) # include diff --git a/clients/opal/models/AsyncIP_sl/src/Sched.h b/clients/opal/models/AsyncIP_sl/src/Sched.h new file mode 100644 index 000000000..2afd1a174 --- /dev/null +++ b/clients/opal/models/AsyncIP_sl/src/Sched.h @@ -0,0 +1,15 @@ +/** Configure Scheduler + * + * @author Steffen Vogel + * @author Mathieu DubĂ©-Dallaire + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + * @copyright 2003, OPAL-RT Technologies inc + * @file + */ + +#ifndef _SCHED_H_ +#define _SCHED_H_ + +int AssignProcToCpu0(void); + +#endif /* _SCHED_H_ */ diff --git a/clients/opal/models/AsyncIP_sl/socket.c b/clients/opal/models/AsyncIP_sl/src/Socket.c similarity index 90% rename from clients/opal/models/AsyncIP_sl/socket.c rename to clients/opal/models/AsyncIP_sl/src/Socket.c index 3a781c1aa..6c2f0f761 100644 --- a/clients/opal/models/AsyncIP_sl/socket.c +++ b/clients/opal/models/AsyncIP_sl/src/Socket.c @@ -12,15 +12,6 @@ * @file */ -#ifndef OPAL_IP_H -#define OPAL_IP_H - -#ifndef PROGNAME -#define PROGNAME "AsyncIPUtils" -#endif -/* Modify this version line if you make changes to this file */ -#define VERSION "Opal-RT_20060524" - #include #include #include @@ -29,15 +20,9 @@ #include #include -#define RT +#include "Socket.c" #include "OpalPrint.h" #include "AsyncApi.h" -#include "OpalGenAsyncParamCtrl.h" -#pragma pack() - -#define UDP_PROTOCOL 1 -#define TCP_PROTOCOL 2 -#define EOK 0 /* Globals variables */ struct sockaddr_in send_ad; /* Send address */ @@ -98,7 +83,7 @@ int InitSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct) return EIO; } else - OpalPrint("%s: Local Port : %d\n", PROGNAME, (int)IconCtrlStruct.FloatParam[2]); + OpalPrint("%s: Local Port : %d\n", PROGNAME, (int) IconCtrlStruct.FloatParam[2]); switch (proto) { case UDP_PROTOCOL: /* Communication using UDP/IP protocol */ @@ -224,5 +209,3 @@ int CloseSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct) return 0; } - -#endif /* OPAL_IP_H */ diff --git a/clients/opal/models/AsyncIP_sl/src/Socket.h b/clients/opal/models/AsyncIP_sl/src/Socket.h new file mode 100644 index 000000000..bfbfe5f75 --- /dev/null +++ b/clients/opal/models/AsyncIP_sl/src/Socket.h @@ -0,0 +1,36 @@ +/** Helper functions for socket + * + * Code example of an asynchronous program. This program is started + * by the asynchronous controller and demonstrates how to send and + * 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 + */ + +#ifndef _SOCKET_H_ +#define _SOCKET_H_ + +/* Modify this version line if you make changes to this file */ +#define VERSION "Opal-RT_20060524" + +#define RT +#include "OpalGenAsyncParamCtrl.h" + +#define UDP_PROTOCOL 1 +#define TCP_PROTOCOL 2 +#define EOK 0 + +int InitSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct); + +int SendPacket(char* DataSend, int datalength); + +int RecvPacket(char* DataRecv, int datalength, double timeout); + +int CloseSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct); + +#endif /* _SOCKET_H_ */