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

opal: moved example project

This commit is contained in:
Steffen Vogel 2017-06-29 19:42:28 +02:00
parent 130b8cf21d
commit 5b201a11e2
23 changed files with 3347 additions and 0 deletions

1
clients/opal/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.log

12
clients/opal/.project Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>villas_udp</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
<nature>com.opalrt.rtlab.ui.rtlabnature</nature>
</natures>
</projectDescription>

View file

@ -0,0 +1,2 @@
eclipse.preferences.version=1
rtprojectfile=villas_udp.llp

View file

@ -0,0 +1,3 @@
OPAL-1.0 Object
ListMap<String,Link<DriverDefinition>> {
}

View file

@ -0,0 +1,17 @@
[
{"version":2},
{
"Opal::Simulation::SerializeableAliasList":
{
"copyOnWrite":0,
"locked":0,
"name":"D804207F-3D61-429A-9CBD-659A211EF103",
"versioned":0,
"aliasSets":
[
],
"history":"",
"parent":"/"
}
}
]

View file

@ -0,0 +1,13 @@
OPAL-1.0 Object
Opal::Simulation::Configuration {
projectID=7B042784-8CF2-444A-92E3-8457E429E28A
copyOnWrite=0
locked=0
name=7B042784-8CF2-444A-92E3-8457E429E28A-default
versioned=0
connectionSets {
}
syncExchangerRegistry=7B042784-8CF2-444A-92E3-8457E429E28A-default/SyncExchangerRegistry
history=
parent=/
}

View file

@ -0,0 +1,8 @@
*.o
*.d
AsyncIP
Opcommon/
report/
*_sm_model/
*_ss_model/
*_sc_console.mdl

View file

@ -0,0 +1,79 @@
# Makefile.
#
# @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
# @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
# @license GNU General Public License (version 3)
#
# VILLASnode
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###################################################################################
TARGET = AsyncIP
VPATH = src
RTLAB_INTEL_COMPILER ?= 1
# Compiler selection
ifeq ($(RTLAB_INTEL_COMPILER),1)
CC = opicc
LD = opicpc
else
CC = gcc
LD = g++
INTEL_LIBS = -limf -lirc
INTEL_OBJS = compat.o
endif
# Support for debugging symbols
ifeq ($(DEBUG),1)
CC_DEBUG_OPTS = -g -D_DEBUG
LD_DEBUG_OPTS = -g
else
CC_DEBUG_OPTS = -O
LD_DEBUG_OPTS =
endif
TARGET_LIB = -lpthread -lm -ldl -lutil -lrt $(INTEL_LIBS)
INCLUDES = -I. $(OPAL_INCPATH) -Iinclude
LIBPATH = -L. $(OPAL_LIBPATH)
CC_OPTS = -m32 -std=c99 -D_GNU_SOURCE -MMD
LD_OPTS = -m32
OBJS = main.o msg.o utils.o socket.o $(INTEL_OBJS)
ifneq ($(PROTOCOL),)
CC_OPTS += -DPROTOCOL=$(PROTOCOL)
endif
ADDLIB = -lOpalCore -lOpalUtils
LIBS = -lOpalAsyncApiCore $(ADDLIB) $(TARGET_LIB) $(OPAL_LIBS)
CFLAGS = -c $(CC_OPTS) $(CC_DEBUG_OPTS) $(INCLUDES)
LDFLAGS = $(LD_OPTS) $(LD_DEBUG_OPTS) $(LIBPATH)
all: $(TARGET)
install: $(TARGET)
install -m 0755 -D -t $(DESTDIR)$(PREFIX)/bin $(TARGET)
clean:
rm -f $(OBJS) $(OBJS:%.o=%.d) $(TARGET)
$(TARGET): $(OBJS)
$(LD) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
-include $(wildcard *.d)

View file

@ -0,0 +1,41 @@
/** Compile-time configuration.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#ifndef _CONFIG_H_
#define _CONFIG_H_
#define PROGNAME "VILLASnode-OPAL-UDP"
#define VERSION "0.6"
#define MAX_VALUES 64
/* List of protocols */
#define VILLAS 1
#define GTNET_SKT 2
/* Default protocol */
#ifndef PROTOCOL
#define PROTOCOL VILLAS
#endif
#endif /* _CONFIG_H_ */

View file

@ -0,0 +1,51 @@
/** Message related functions
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#pragma once
/* Forward declarations. */
struct msg;
/** Swaps the byte-order of the message.
*
* Message are always transmitted in network (big endian) byte order.
*
* @param m A pointer to the message
*/
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.
*
* The functions checks the header fields of a message.
*
* @param m A pointer to the message
* @retval 0 The message header is valid.
* @retval <0 The message header is invalid.
*/
int msg_verify(struct msg *m);

View file

@ -0,0 +1,92 @@
/** Message format
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#pragma once
#include <stdint.h>
/** The current version number for the message format */
#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 */
/** The total size in bytes of a message */
#define MSG_LEN(values) (sizeof(struct msg) + MSG_DATA_LEN(values))
/** The length of \p values values in bytes. */
#define MSG_DATA_LEN(values) (sizeof(float) * (values))
/** 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 \
}
/** 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 rsvd1 : 2; /**< Reserved bits */
#elif BYTE_ORDER == LITTLE_ENDIAN
unsigned rsvd1 : 2; /**< Reserved bits */
unsigned type : 2; /**< Data or control message (see MSG_TYPE_*) */
unsigned version: 4; /**< Specifies the format of the remaining message (see MGS_VERSION) */
#else
#error Invalid byte-order
#endif
uint8_t rsvd2; /**< Reserved bits */
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. */
struct {
uint32_t sec; /**< Seconds since 1970-01-01 00:00:00 */
uint32_t nsec; /**< Nanoseconds of the current second. */
} ts;
/** The message payload. */
union {
float f; /**< Floating point values. */
uint32_t i; /**< Integer values. */
} data[];
} __attribute__((packed));

View file

@ -0,0 +1,49 @@
/** Helper functions for sockets.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#ifndef _SOCKET_H_
#define _SOCKET_H_
#include <netinet/in.h>
#define RT
#include "OpalGenAsyncParamCtrl.h"
#define UDP_PROTOCOL 1
#define TCP_PROTOCOL 2
struct socket {
struct sockaddr_in send_ad; /* Send address */
struct sockaddr_in recv_ad; /* Receive address */
int sd; /* socket descriptor */
};
int socket_init(struct socket *s, Opal_GenAsyncParam_Ctrl IconCtrlStruct);
int socket_send(struct socket *s, char *data, int len);
int socket_recv(struct socket *s, char *data, int len, double timeout);
int socket_close(struct socket *s, Opal_GenAsyncParam_Ctrl IconCtrlStruct);
#endif /* _SOCKET_H_ */

View file

@ -0,0 +1,31 @@
/** Configure scheduler.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @author Mathieu Dubé-Dallaire
* @copyright 2003, OPAL-RT Technologies inc
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#ifndef _UTILS_H_
#define _UTILS_H_
int AssignProcToCpu0(void);
#endif /* _UTILS_H_ */

View file

@ -0,0 +1,118 @@
[EnvVars]
ABORT_COMPILE_WHEN_NO_BITSTREAM=0
ACTION_AFTER_N_OVERRUNS=10
ACTION_ON_OVERRUNS=0
AcquisitionMemory=0,2500,24,100
ActiveGroups=7/0/24/25/26/27/28/29/
CACHEABLE_DMA_MEMORY_ACCESS=ON
COMM_RT=UDP/IP
ClockPeriodMode=Free-Clock
ClockPeriodTime=10
DEBUG=0
DETECT_OVERRUNS=ON
ENABLE_WATCHDOG=ON
EXT_CC_OPTS=
EXT_LD_OPTS=
EXT_LIB=
EXT_LIBPATH=
MODEL_PAUSE_TIME=0.000000
MODEL_STOP_TIME=0.000000
MONITORING=ON
MONITORING_BLOCK=OFF
MONITORING_DISPLAY=ALL
MSG_PRECISION_FACTOR=0
MaxDynamicSignals=2/0/100/24/100/
NB_STEP_WITHOUT_OVERRUNS=10
OPAL_DEBUG=OFF
OP_MATLABR2014B=1
OS_COMPILE_RELEASE=2.6.29.6-opalrt-5
PRINT_LOG_LEVEL=DEBUG_RUN
RESET_IO_MISSING=ON
SYSNAME=linux
USER_INCS=
USER_SRCS=
WATCHDOG_TIMEOUT=5000
[EnvVars_REDHAWK_DYN_1]
INTERNAL_IGN_SOURCE_FILE=sfun_gen_async_ctrl.c sfun_recv_async.c sfun_send_async.c
INTERNAL_LIBRARY2=-lOpalAsyncApiR2014b
INTERNAL_LIBRARY3=-lOpalAsyncApiCore
[ExtraGetFilesComp_1_RT_LAB]
AsyncIP=Binary|Async_Proc
[ExtraPutFilesComp]
Makefile.mk=Ascii
include\config.h=Ascii
include\msg.h=Ascii
include\msg_format.h=Ascii
include\socket.h=Ascii
include\utils.h=Ascii
src\compat.c=Ascii
src\main.c=Ascii
src\msg.c=Ascii
src\socket.c=Ascii
src\utils.c=Ascii
[ExtraPutFilesComp_1_RT_LAB]
C:\OPAL-RT\RT-LAB\v11.0.8.13\common\lib\redhawk\libOpalAsyncApiCore.a=Binary|Other
[ExtraPutFilesLoad_1_RT_LAB]
.\send_receive_sm_model\OpREDHAWKtarget\AsyncIP=Binary|Async_Proc
[General]
ATT_CHECKSUM1=2360032266
ATT_CHECKSUM2=1041833943
ATT_CHECKSUM3=842695092
ATT_CHECKSUM4=2435976698
ATT_CREATED_BY=jwu
ATT_CREATED_ON=Thu Apr 15 08:21:54 1999
ATT_ENABLE_PTA=OFF
ATT_HANDLE_CONSOLE=ON
ATT_LAST_SAVED_BY=svo
ATT_LAST_SAVED_ON=Tue Jun 27 10:20:09 2017
ATT_REFERENCE_MDL_PATHS=
ATT_REVISION=1.453
AutoRetrieveFiles=ON
AutoRetrieveRtlab=ON
CompilerVersion=AUTOMATIC
DESCRIPTION=
DinamoFlag=OFF
FILENAME=C:\Users\svo\workspace\villas\node\clients\opal\villas_udp\models\send_receive\send_receive.mdl
FORCE_RECOMPILE=0
IMPORTED_GLOBAL_VARIABLES=1
LastCompileRtlabVersion=v11.0.8.13
LastMatlabUsed=27
LastMatlabUsedName=v8.4
MATLAB_USED_IN_MODEL=21
Name=send_receive
PETALINUX_LAST_COMPILE_VERSION=
PRINT_CYCLE=OFF
PostBuildCmd=
PreBuildCmd=
QNX_LAST_COMPILE_VERSION=
RH64_LAST_COMPILE_VERSION=
RH_LAST_COMPILE_VERSION=2.6.29.6-opalrt-5
ReportFileId=
RetrieveBuildTree=ON
RetrieveRootDir=
SimulationMode=2
TLC=Automatic
TMF=Automatic
TRANSFERFILE_AT_LOAD=ON
TargetCompileCmd=/usr/bin/make -f /usr/opalrt/common/bin/opalmodelmk
TargetPlatform=REDHAWK
TimeFactor=1.000000000000000
TimeStep=0.000050000000000
sc_consoleTimeStep=-1.000000000000000
sm_modelTimeStep=0.000049999998737
sm_send_receiveTimeStep=0.000049999998737
[General_1]
TargetPreCompileCmd=make -f /usr/opalrt/common/bin/opalmodelmk
[NodeMapping]
sm_model=ACS_OPAL_RT
sm_model_CORE_ASSIGNATION=1
sm_model_CPU=-1
sm_model_DEBUG=OFF
sm_model_XHP_ENABLE=FALSE
sm_send_receive=ACS_OPAL_RT
sm_send_receive_CORE_ASSIGNATION=1
sm_send_receive_CPU=-1
sm_send_receive_DEBUG=OFF
sm_send_receive_XHP_ENABLE=FALSE
[UserEnvVars]
PROTOCOL=VILLAS

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,57 @@
[General]
ATT_VERSION=6
FileVersion=6
numClientsProbe=0
[Group_1]
condition=0
decimationFactor=1
duration=0.010000
dynSignalsList=0
fileLimit=1000000
fixSignalsList=13|1|2|3|4|5|6|7|8|9|10|11|12|13
level=0.000000
maxDynSignals=100
newMemSize=2500
nodeId=0
numValues=200
offset=0
rearmDelay=0.000000
repetitive=1
signalName=
signalNameId=0
trigSigNameId=339755367
trigSignalName=
trigType=0
triggerButton=2
triggerEnabled=0
triggerState=0
valueType=0
writeEnabled=0
writeFileName=
writeFileVarName=
[Group_25]
condition=0
decimationFactor=1
duration=0.005000
dynSignalsList=0
fileLimit=0
level=0.000000
maxDynSignals=100
newMemSize=100
nodeId=158494572
numValues=100
offset=0
rearmDelay=0.000000
repetitive=1
signalName=
signalNameId=1818584175
trigSigNameId=1866690149
trigSignalName=
trigType=0
triggerButton=2
triggerEnabled=0
triggerState=0
valueType=0
writeEnabled=0
writeFileName=
writeFileVarName=

View file

@ -0,0 +1,46 @@
/** Compatibility code for GCC
*
* OPAL-RT's libSystem.a links against some Intel
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <string.h>
size_t __intel_sse2_strlen(const char *s)
{
return strlen(s);
}
void * _intel_fast_memset(void *b, int c, size_t len)
{
return memset(b, c, len);
}
void * _intel_fast_memcpy(void *restrict dst, const void *restrict src, size_t n)
{
return memcpy(dst, src, n);
}
int _intel_fast_memcmp(const void *s1, const void *s2, size_t n)
{
return memcmp(s1, s2, n);
}

View file

@ -0,0 +1,352 @@
/** Main routine of AsyncIP.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU Lesser General Public License v2.1
*
* VILLASnode - connecting real-time simulation equipment
*
* This application is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
*
* This application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*********************************************************************************/
/* Standard ANSI C headers needed for this program */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <pthread.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"
/* This is the message format */
#include "config.h"
#include "socket.h"
#include "utils.h"
#if PROTOCOL == VILLAS
#include "msg.h"
#include "msg_format.h"
#endif
/* 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]
/* Global Variables */
struct socket skt;
static void * SendToIPPort(void *arg)
{
unsigned int ModelState, SendID = 1, Sequence = 0;
int nbSend = 0, ret, cnt, len;
/* Data from OPAL-RT model */
double mdldata[MAX_VALUES];
int mdldata_size;
#if PROTOCOL == VILLAS
char buf[MSG_LEN(MAX_VALUES)];
struct msg *msg = (struct msg *) buf;
#elif PROTOCOL == GTNET_SKT
char buf[MAX_VALUES * sizeof(float)];
float *msg = (float *) buf;
#endif
OpalPrint("%s: SendToIPPort thread started\n", PROGNAME);
OpalGetNbAsyncSendIcon(&nbSend);
if (nbSend < 1) {
OpalPrint("%s: SendToIPPort: No transimission block for this controller. Stopping thread.\n", PROGNAME);
return NULL;
}
do {
/* This call unblocks when the 'Data Ready' line of a send icon is asserted. */
ret = OpalWaitForAsyncSendRequest(&SendID);
if (ret != EOK) {
ModelState = OpalGetAsyncModelState();
if ((ModelState != STATE_RESET) && (ModelState != STATE_STOP)) {
OpalSetAsyncSendIconError(ret, SendID);
OpalPrint("%s: OpalWaitForAsyncSendRequest(), errno %d\n", PROGNAME, ret);
}
continue;
}
/* No errors encountered yet */
OpalSetAsyncSendIconError(0, SendID);
/* Get the size of the data being sent by the unblocking SendID */
OpalGetAsyncSendIconDataLength(&mdldata_size, SendID);
cnt = mdldata_size / sizeof(double);
if (cnt > MAX_VALUES) {
OpalPrint("%s: Number of signals for SendID=%d exceeds allowed maximum (%d)\n",
PROGNAME, SendID, MAX_VALUES);
return NULL;
}
/* Read data from the model */
OpalGetAsyncSendIconData(mdldata, mdldata_size, SendID);
#if PROTOCOL == VILLAS
/* Get current time */
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
msg->length = mdldata_size / sizeof(double);
msg->sequence = Sequence++;
msg->ts.sec = now.tv_sec;
msg->ts.nsec = now.tv_nsec;
for (int i = 0; i < msg->length; i++)
msg->data[i].f = (float) mdldata[i];
msg_hton(msg);
len = MSG_LEN(msg->length);
#elif PROTOCOL == GTNET_SKT
uint32_t *imsg = (uint32_t *) msg;
for (int i = 0; i < cnt; i++) {
msg[i] = (float) mdldata[i];
imsg[i] = htonl(imsg[i]);
}
len = mdldata_size / sizeof(double) * sizeof(float);
#else
#error Unknown protocol
#endif
/* Perform the actual write to the ip port */
ret = socket_send(&skt, (char *) msg, len);
if (ret < 0)
OpalSetAsyncSendIconError(errno, SendID);
else
OpalSetAsyncSendIconError(0, SendID);
/* This next call allows the execution of the "asynchronous" process
* to actually be synchronous with the model. To achieve this, you
* should set the "Sending Mode" in the Async_Send block to
* NEED_REPLY_BEFORE_NEXT_SEND or NEED_REPLY_NOW. This will force
* the model to wait for this process to call this
* OpalAsyncSendRequestDone function before continuing. */
OpalAsyncSendRequestDone(SendID);
/* Before continuing, we make sure that the real-time model
* has not been stopped. If it has, we quit. */
ModelState = OpalGetAsyncModelState();
} while ((ModelState != STATE_RESET) && (ModelState != STATE_STOP));
OpalPrint("%s: SendToIPPort: Finished\n", PROGNAME);
return NULL;
}
static void * RecvFromIPPort(void *arg)
{
unsigned int ModelState, RecvID = 1;
int nbRecv = 0, ret, cnt;
/* Data from OPAL-RT model */
double mdldata[MAX_VALUES];
int mdldata_size;
#if PROTOCOL == VILLAS
char buf[MSG_LEN(MAX_VALUES)];
struct msg *msg = (struct msg *) buf;
#elif PROTOCOL == GTNET_SKT
char buf[MAX_VALUES * sizeof(float)];
float *msg = (float *) buf;
#else
#error Unknown protocol
#endif
OpalPrint("%s: RecvFromIPPort thread started\n", PROGNAME);
OpalGetNbAsyncRecvIcon(&nbRecv);
if (nbRecv < 1) {
OpalPrint("%s: RecvFromIPPort: No reception block for this controller. Stopping thread.\n", PROGNAME);
return NULL;
}
do {
/* Receive message */
ret = socket_recv(&skt, (char *) msg, sizeof(buf), 1.0);
if (ret < 1) {
ModelState = OpalGetAsyncModelState();
if ((ModelState != STATE_RESET) && (ModelState != STATE_STOP)) {
if (ret == 0) /* timeout, so we continue silently */
OpalPrint("%s: Timeout while waiting for data\n", PROGNAME, errno);
if (ret == -1) /* a more serious error, so we print it */
OpalPrint("%s: Error %d while waiting for data\n", PROGNAME, errno);
continue;
}
break;
}
/* Get the number of signals to send back to the model */
OpalGetAsyncRecvIconDataLength(&mdldata_size, RecvID);
cnt = mdldata_size / sizeof(double);
if (cnt > MAX_VALUES) {
OpalPrint("%s: Number of signals for RecvID=%d (%d) exceeds allowed maximum (%d)\n",
PROGNAME, RecvID, cnt, MAX_VALUES);
return NULL;
}
#if PROTOCOL == VILLAS
msg_ntoh(msg);
ret = msg_verify(msg);
if (ret) {
OpalPrint("%s: Skipping invalid packet\n", PROGNAME);
continue;
}
if (cnt > msg->length) {
OpalPrint("%s: Number of signals for RecvID=%d (%d) exceeds what was received (%d)\n",
PROGNAME, RecvID, cnt, msg->length);
}
for (int i = 0; i < msg->length; i++) {
mdldata[i] = (double) msg->data[i].f;
printf("Data rcvd from VILLAS %f\n", mdldata[i]);
}
/* Update OPAL model */
OpalSetAsyncRecvIconStatus(msg->sequence, RecvID); /* Set the Status to the message ID */
#elif PROTOCOL == GTNET_SKT
uint32_t *imsg = (uint32_t *) msg;
for (int i = 0; i < cnt; i++)
imsg[i] = ntohl(imsg[i]);
printf("Protocol GTNET_SKT\n");
for (int i = 0; i < cnt; i++) {
mdldata[i] = (double) msg[i];
printf("Data rcvd from GTNET_SKT %f\n", mdldata[i]);
}
#else
#error Unknown protocol
#endif
OpalSetAsyncRecvIconError(0, RecvID); /* Set the Error to 0 */
OpalSetAsyncRecvIconData(mdldata, mdldata_size, RecvID);
/* Before continuing, we make sure that the real-time model
* has not been stopped. If it has, we quit. */
ModelState = OpalGetAsyncModelState();
} while ((ModelState != STATE_RESET) && (ModelState != STATE_STOP));
OpalPrint("%s: RecvFromIPPort: Finished\n", PROGNAME);
return NULL;
}
int main(int argc, char *argv[])
{
/* @todo remove after testing */
printf("*****************Starting the Application****************\n");
FILE * testfile = fopen ("testfile.txt","w");
if (testfile!=NULL)
{
fputs ("test file to check if main runs", testfile);
fclose (testfile);
}
int ret;
Opal_GenAsyncParam_Ctrl IconCtrlStruct;
pthread_t tid_send, tid_recv;
OpalPrint("%s: This is %s client version %s\n", PROGNAME, PROGNAME, VERSION);
/* Check for the proper arguments to the program */
if (argc < 4) {
printf("Invalid Arguments: 1-AsyncShmemName 2-AsyncShmemSize 3-PrintShmemName\n");
exit(0);
}
/* Enable the OpalPrint function. This prints to the OpalDisplay. */
ret = OpalSystemCtrl_Register(PRINT_SHMEM_NAME);
if (ret != EOK) {
printf("%s: ERROR: OpalPrint() access not available\n", PROGNAME);
exit(EXIT_FAILURE);
}
/* Open Share Memory created by the model. */
ret = OpalOpenAsyncMem(ASYNC_SHMEM_SIZE, ASYNC_SHMEM_NAME);
if (ret != EOK) {
OpalPrint("%s: ERROR: Model shared memory not available\n", PROGNAME);
exit(EXIT_FAILURE);
}
AssignProcToCpu0();
/* Get IP Controler Parameters (ie: ip address, port number...) and
* initialize the device on the QNX node. */
memset(&IconCtrlStruct, 0, sizeof(IconCtrlStruct));
ret = OpalGetAsyncCtrlParameters(&IconCtrlStruct, sizeof(IconCtrlStruct));
if (ret != EOK) {
OpalPrint("%s: ERROR: Could not get controller parameters (%d).\n", PROGNAME, ret);
exit(EXIT_FAILURE);
}
/* Initialize socket */
ret = socket_init(&skt, IconCtrlStruct);
if (ret != EOK) {
OpalPrint("%s: ERROR: Initialization failed.\n", PROGNAME);
exit(EXIT_FAILURE);
}
/* Start send/receive threads */
ret = pthread_create(&tid_send, NULL, SendToIPPort, NULL);
if (ret == -1)
OpalPrint("%s: ERROR: Could not create thread (SendToIPPort), errno %d\n", PROGNAME, errno);
ret = pthread_create(&tid_recv, NULL, RecvFromIPPort, NULL);
if (ret == -1)
OpalPrint("%s: ERROR: Could not create thread (RecvFromIPPort), errno %d\n", PROGNAME, errno);
/* Wait for both threads to finish */
ret = pthread_join(tid_send, NULL);
if (ret != 0)
OpalPrint("%s: ERROR: pthread_join (SendToIPPort), errno %d\n", PROGNAME, ret);
ret = pthread_join(tid_recv, NULL);
if (ret != 0)
OpalPrint("%s: ERROR: pthread_join (RecvFromIPPort), errno %d\n", PROGNAME, ret);
/* Close the ip port and shared memories */
socket_close(&skt, IconCtrlStruct);
OpalCloseAsyncMem (ASYNC_SHMEM_SIZE, ASYNC_SHMEM_NAME);
OpalSystemCtrl_UnRegister(PRINT_SHMEM_NAME);
return 0;
}

View file

@ -0,0 +1,340 @@
/** Main routine of AsyncIP.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
/* Standard ANSI C headers needed for this program */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <pthread.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"
/* This is the message format */
#include "config.h"
#include "socket.h"
#include "utils.h"
#if PROTOCOL == VILLAS
#include "msg.h"
#include "msg_format.h"
#endif
/* 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]
/* Global Variables */
struct socket skt;
static void * SendToIPPort(void *arg)
{
unsigned int ModelState, SendID = 1, Sequence = 0;
int nbSend = 0, ret, cnt, len;
/* Data from OPAL-RT model */
double mdldata[MAX_VALUES];
int mdldata_size;
#if PROTOCOL == VILLAS
char buf[MSG_LEN(MAX_VALUES)];
struct msg *msg = (struct msg *) buf;
#elif PROTOCOL == GTNET_SKT
char buf[MAX_VALUES * sizeof(float)];
float *msg = (float *) buf;
#endif
OpalPrint("%s: SendToIPPort thread started\n", PROGNAME);
OpalGetNbAsyncSendIcon(&nbSend);
if (nbSend < 1) {
OpalPrint("%s: SendToIPPort: No transimission block for this controller. Stopping thread.\n", PROGNAME);
return NULL;
}
do {
/* This call unblocks when the 'Data Ready' line of a send icon is asserted. */
ret = OpalWaitForAsyncSendRequest(&SendID);
if (ret != EOK) {
ModelState = OpalGetAsyncModelState();
if ((ModelState != STATE_RESET) && (ModelState != STATE_STOP)) {
OpalSetAsyncSendIconError(ret, SendID);
OpalPrint("%s: OpalWaitForAsyncSendRequest(), errno %d\n", PROGNAME, ret);
}
continue;
}
/* No errors encountered yet */
OpalSetAsyncSendIconError(0, SendID);
/* Get the size of the data being sent by the unblocking SendID */
OpalGetAsyncSendIconDataLength(&mdldata_size, SendID);
cnt = mdldata_size / sizeof(double);
if (cnt > MAX_VALUES) {
OpalPrint("%s: Number of signals for SendID=%d exceeds allowed maximum (%d). Limiting...\n",
PROGNAME, SendID, MAX_VALUES);
cnt = MAX_VALUES;
}
/* Read data from the model */
OpalGetAsyncSendIconData(mdldata, mdldata_size, SendID);
#if PROTOCOL == VILLAS
/* Get current time */
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
msg->version = MSG_VERSION;
msg->type = MSG_TYPE_DATA;
msg->rsvd1 = 0;
msg->rsvd2 = 0;
msg->length = cnt;
msg->sequence = Sequence++;
msg->ts.sec = now.tv_sec;
msg->ts.nsec = now.tv_nsec;
for (int i = 0; i < msg->length; i++)
msg->data[i].f = (float) mdldata[i];
len = MSG_LEN(msg->length);
msg_hton(msg);
#elif PROTOCOL == GTNET_SKT
uint32_t *imsg = (uint32_t *) msg;
for (int i = 0; i < cnt; i++) {
msg[i] = (float) mdldata[i];
imsg[i] = htonl(imsg[i]);
}
len = cnt * sizeof(float);
#else
#error Unknown protocol
#endif
/* Perform the actual write to the ip port */
ret = socket_send(&skt, (char *) msg, len);
if (ret < 0)
OpalSetAsyncSendIconError(errno, SendID);
else
OpalSetAsyncSendIconError(0, SendID);
/* This next call allows the execution of the "asynchronous" process
* to actually be synchronous with the model. To achieve this, you
* should set the "Sending Mode" in the Async_Send block to
* NEED_REPLY_BEFORE_NEXT_SEND or NEED_REPLY_NOW. This will force
* the model to wait for this process to call this
* OpalAsyncSendRequestDone function before continuing. */
OpalAsyncSendRequestDone(SendID);
/* Before continuing, we make sure that the real-time model
* has not been stopped. If it has, we quit. */
ModelState = OpalGetAsyncModelState();
} while ((ModelState != STATE_RESET) && (ModelState != STATE_STOP));
OpalPrint("%s: SendToIPPort: Finished\n", PROGNAME);
return NULL;
}
static void * RecvFromIPPort(void *arg)
{
unsigned int ModelState, RecvID = 1;
int nbRecv = 0, ret, cnt;
/* Data from OPAL-RT model */
double mdldata[MAX_VALUES];
int mdldata_size;
#if PROTOCOL == VILLAS
char buf[MSG_LEN(MAX_VALUES)];
struct msg *msg = (struct msg *) buf;
#elif PROTOCOL == GTNET_SKT
char buf[MAX_VALUES * sizeof(float)];
float *msg = (float *) buf;
#else
#error Unknown protocol
#endif
OpalPrint("%s: RecvFromIPPort thread started\n", PROGNAME);
OpalGetNbAsyncRecvIcon(&nbRecv);
if (nbRecv < 1) {
OpalPrint("%s: RecvFromIPPort: No reception block for this controller. Stopping thread.\n", PROGNAME);
return NULL;
}
do {
/* Receive message */
ret = socket_recv(&skt, (char *) msg, sizeof(buf), 1.0);
if (ret < 1) {
ModelState = OpalGetAsyncModelState();
if ((ModelState != STATE_RESET) && (ModelState != STATE_STOP)) {
if (ret == 0) /* timeout, so we continue silently */
OpalPrint("%s: Timeout while waiting for data\n", PROGNAME, errno);
if (ret == -1) /* a more serious error, so we print it */
OpalPrint("%s: Error %d while waiting for data\n", PROGNAME, errno);
continue;
}
break;
}
/* Get the number of signals to send back to the model */
OpalGetAsyncRecvIconDataLength(&mdldata_size, RecvID);
cnt = mdldata_size / sizeof(double);
if (cnt > MAX_VALUES) {
OpalPrint("%s: Number of signals for RecvID=%d (%d) exceeds allowed maximum (%d)\n",
PROGNAME, RecvID, cnt, MAX_VALUES);
return NULL;
}
#if PROTOCOL == VILLAS
msg_ntoh(msg);
ret = msg_verify(msg);
if (ret) {
OpalPrint("%s: Skipping invalid packet\n", PROGNAME);
continue;
}
if (cnt > msg->length) {
OpalPrint("%s: Number of signals for RecvID=%d (%d) exceeds what was received (%d)\n",
PROGNAME, RecvID, cnt, msg->length);
}
for (int i = 0; i < msg->length; i++)
mdldata[i] = (double) msg->data[i].f;
/* Update OPAL model */
OpalSetAsyncRecvIconStatus(msg->sequence, RecvID); /* Set the Status to the message ID */
#elif PROTOCOL == GTNET_SKT
uint32_t *imsg = (uint32_t *) msg;
for (int i = 0; i < cnt; i++) {
imsg[i] = ntohl(imsg[i]);
mdldata[i] = (double) msg[i];
}
#else
#error Unknown protocol
#endif
OpalSetAsyncRecvIconError(0, RecvID); /* Set the Error to 0 */
OpalSetAsyncRecvIconData(mdldata, mdldata_size, RecvID);
/* Before continuing, we make sure that the real-time model
* has not been stopped. If it has, we quit. */
ModelState = OpalGetAsyncModelState();
} while ((ModelState != STATE_RESET) && (ModelState != STATE_STOP));
OpalPrint("%s: RecvFromIPPort: Finished\n", PROGNAME);
return NULL;
}
int main(int argc, char *argv[])
{
int ret;
Opal_GenAsyncParam_Ctrl IconCtrlStruct;
pthread_t tid_send, tid_recv;
OpalPrint("%s: This is %s client version %s\n", PROGNAME, PROGNAME, VERSION);
/* Check for the proper arguments to the program */
if (argc < 4) {
printf("Invalid Arguments: 1-AsyncShmemName 2-AsyncShmemSize 3-PrintShmemName\n");
exit(0);
}
/* Enable the OpalPrint function. This prints to the OpalDisplay. */
ret = OpalSystemCtrl_Register(PRINT_SHMEM_NAME);
if (ret != EOK) {
printf("%s: ERROR: OpalPrint() access not available\n", PROGNAME);
exit(EXIT_FAILURE);
}
/* Open Share Memory created by the model. */
ret = OpalOpenAsyncMem(ASYNC_SHMEM_SIZE, ASYNC_SHMEM_NAME);
if (ret != EOK) {
OpalPrint("%s: ERROR: Model shared memory not available\n", PROGNAME);
exit(EXIT_FAILURE);
}
AssignProcToCpu0();
/* Get IP Controler Parameters (ie: ip address, port number...) and
* initialize the device on the QNX node. */
memset(&IconCtrlStruct, 0, sizeof(IconCtrlStruct));
ret = OpalGetAsyncCtrlParameters(&IconCtrlStruct, sizeof(IconCtrlStruct));
if (ret != EOK) {
OpalPrint("%s: ERROR: Could not get controller parameters (%d).\n", PROGNAME, ret);
exit(EXIT_FAILURE);
}
/* Initialize socket */
ret = socket_init(&skt, IconCtrlStruct);
if (ret != EOK) {
OpalPrint("%s: ERROR: Initialization failed.\n", PROGNAME);
exit(EXIT_FAILURE);
}
/* Start send/receive threads */
ret = pthread_create(&tid_send, NULL, SendToIPPort, NULL);
if (ret == -1)
OpalPrint("%s: ERROR: Could not create thread (SendToIPPort), errno %d\n", PROGNAME, errno);
ret = pthread_create(&tid_recv, NULL, RecvFromIPPort, NULL);
if (ret == -1)
OpalPrint("%s: ERROR: Could not create thread (RecvFromIPPort), errno %d\n", PROGNAME, errno);
/* Wait for both threads to finish */
ret = pthread_join(tid_send, NULL);
if (ret != 0)
OpalPrint("%s: ERROR: pthread_join (SendToIPPort), errno %d\n", PROGNAME, ret);
ret = pthread_join(tid_recv, NULL);
if (ret != 0)
OpalPrint("%s: ERROR: pthread_join (RecvFromIPPort), errno %d\n", PROGNAME, ret);
/* Close the ip port and shared memories */
socket_close(&skt, IconCtrlStruct);
OpalCloseAsyncMem (ASYNC_SHMEM_SIZE, ASYNC_SHMEM_NAME);
OpalSystemCtrl_UnRegister(PRINT_SHMEM_NAME);
return 0;
}

View file

@ -0,0 +1,70 @@
/** Message related functions.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <arpa/inet.h>
#include "msg.h"
#include "msg_format.h"
void msg_ntoh(struct msg *m)
{
msg_hdr_ntoh(m);
for (int i = 0; i < m->length; i++)
m->data[i].i = ntohl(m->data[i].i);
}
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)
{
if (m->version != MSG_VERSION)
return -1;
else if (m->type != MSG_TYPE_DATA)
return -2;
else if ((m->rsvd1 != 0) || (m->rsvd2 != 0))
return -3;
else
return 0;
}

View file

@ -0,0 +1,190 @@
/** Helper functions for sockets.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <arpa/inet.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 "config.h"
#include "socket.h"
int socket_init(struct socket *s, Opal_GenAsyncParam_Ctrl IconCtrlStruct)
{
struct ip_mreq mreq; /* Multicast group structure */
unsigned char TTL = 1, LOOP = 0;
int rc, proto, ret;
proto = (int) IconCtrlStruct.FloatParam[0];
if (proto != UDP_PROTOCOL) {
OpalPrint("%s: This version of %s only supports UDP\n", PROGNAME, PROGNAME);
return EIO;
}
OpalPrint("%s: Version : %s\n", PROGNAME, VERSION);
OpalPrint("%s: Remote Address : %s\n", PROGNAME, IconCtrlStruct.StringParam[0]);
OpalPrint("%s: Remote Port : %d\n", PROGNAME, (int) IconCtrlStruct.FloatParam[1]);
/* Initialize the socket */
s->sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s->sd < 0) {
OpalPrint("%s: ERROR: Could not open socket\n", PROGNAME);
return EIO;
}
/* Set the structure for the remote port and address */
memset(&s->send_ad, 0, sizeof(s->send_ad));
s->send_ad.sin_family = AF_INET;
s->send_ad.sin_addr.s_addr = inet_addr(IconCtrlStruct.StringParam[0]);
s->send_ad.sin_port = htons((u_short) IconCtrlStruct.FloatParam[1]);
/* Set the structure for the local port and address */
memset(&s->recv_ad, 0, sizeof(s->recv_ad));
s->recv_ad.sin_family = AF_INET;
s->recv_ad.sin_addr.s_addr = INADDR_ANY;
s->recv_ad.sin_port = htons((u_short) IconCtrlStruct.FloatParam[2]);
/* Bind local port and address to socket. */
ret = bind(s->sd, (struct sockaddr *) &s->recv_ad, sizeof(struct sockaddr_in));
if (ret == -1) {
OpalPrint("%s: ERROR: Could not bind local port to socket\n", PROGNAME);
return EIO;
}
else
OpalPrint("%s: Local Port : %d\n", PROGNAME, (int) IconCtrlStruct.FloatParam[2]);
/* If sending to a multicast address */
if ((inet_addr(IconCtrlStruct.StringParam[0]) & inet_addr("240.0.0.0")) == inet_addr("224.0.0.0")) {
ret = setsockopt(s->sd, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &TTL, sizeof(TTL));
if (ret == -1) {
OpalPrint("%s: ERROR: Could not set TTL for multicast send (%d)\n", PROGNAME, errno);
return EIO;
}
ret = setsockopt(s->sd, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&LOOP, sizeof(LOOP));
if (ret == -1) {
OpalPrint("%s: ERROR: Could not set loopback for multicast send (%d)\n", PROGNAME, errno);
return EIO;
}
OpalPrint("%s: Configured socket for sending to multicast address\n", PROGNAME);
}
/* If receiving from a multicast group, register for it. */
if (inet_addr(IconCtrlStruct.StringParam[1]) > 0) {
if ((inet_addr(IconCtrlStruct.StringParam[1]) & inet_addr("240.0.0.0")) == inet_addr("224.0.0.0")) {
mreq.imr_multiaddr.s_addr = inet_addr(IconCtrlStruct.StringParam[1]);
mreq.imr_interface.s_addr = INADDR_ANY;
/* Have the multicast socket join the multicast group */
ret = setsockopt(s->sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq));
if (ret == -1) {
OpalPrint("%s: ERROR: Could not join multicast group (%d)\n", PROGNAME, errno);
return EIO;
}
OpalPrint("%s: Added process to multicast group (%s)\n",
PROGNAME, IconCtrlStruct.StringParam[1]);
}
else {
OpalPrint("%s: WARNING: IP address for multicast group is not in multicast range. Ignored\n",
PROGNAME);
}
}
return EOK;
}
int socket_close(struct socket *s, Opal_GenAsyncParam_Ctrl IconCtrlStruct)
{
if (s->sd < 0) {
shutdown(s->sd, SHUT_RDWR);
close(s->sd);
}
return 0;
}
int socket_send(struct socket *s, char *data, int len)
{
if (s->sd < 0)
return -1;
/* Send the packet */
return sendto(s->sd, data, len, 0, (struct sockaddr *) &s->send_ad, sizeof(s->send_ad));
}
int socket_recv(struct socket *s, char *data, int len, double timeout)
{
int ret;
struct sockaddr_in client_ad;
struct timeval tv;
socklen_t client_ad_size = sizeof(client_ad);
fd_set sd_set;
if (s->sd < 0)
return -1;
/* Set the descriptor set for the select() call */
FD_ZERO(&sd_set);
FD_SET(s->sd, &sd_set);
/* Set the tv structure to the correct timeout value */
tv.tv_sec = (int) timeout;
tv.tv_usec = (int) ((timeout - tv.tv_sec) * 1000000);
/* Wait for a packet. We use select() to have a timeout. This is
* necessary when reseting the model so we don't wait indefinitely
* and prevent the process from exiting and freeing the port for
* a future instance (model load). */
ret = select(s->sd + 1, &sd_set, (fd_set *) 0, (fd_set *) 0, &tv);
switch (ret) {
case -1: /* Error */
return -1;
case 0: /* We hit the timeout */
return 0;
default:
if (!(FD_ISSET(s->sd, &sd_set))) {
/* We received something, but it's not on "sd". Since sd is the only
* descriptor in the set... */
OpalPrint("%s: RecvPacket: God, is that You trying to reach me?\n", PROGNAME);
return -1;
}
}
/* Clear the data array (in case we receive an incomplete packet) */
memset(data, 0, len);
/* Perform the reception */
return recvfrom(s->sd, data, len, 0, (struct sockaddr *) &client_ad, &client_ad_size);
}

View file

@ -0,0 +1,52 @@
/** Configure scheduler.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @author Mathieu Dubé-Dallaire
* @copyright 2003, OPAL-RT Technologies inc
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <errno.h>
#include <sched.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 "config.h"
#include "utils.h"
int AssignProcToCpu0(void)
{
int ret;
cpu_set_t bindSet;
CPU_ZERO(&bindSet);
CPU_SET(0, &bindSet);
/* Changing process cpu affinity */
ret = sched_setaffinity(0, sizeof(cpu_set_t), &bindSet);
if (ret) {
OpalPrint("Unable to bind the process to CPU 0: %d\n", errno);
return EINVAL;
}
return 0;
}

View file

@ -0,0 +1,20 @@
<?xml version="1.0" ?>
<Project>
<General>
<att_name>villas_udp</att_name>
<att_description>This is a project!</att_description>
<att_ip_address>134.130.169.120:25251</att_ip_address>
<att_hashcode>FC70D2D4-2B11-40E7-8317-6C0EB3BB3182</att_hashcode>
<att_autosave>ON</att_autosave>
<att_filename>C:\Users\svo\workspace\villas\node\clients\opal\villas_udp\villas_udp.llp</att_filename>
</General>
<Models>
<Model>
<Path>
<Relative>models/send_receive/send_receive.mdl</Relative>
<Absolute>C:/Users/svo/workspace/villas/node/clients/opal/villas_udp/models/send_receive/send_receive.mdl</Absolute>
<UNC>C:/Users/svo/workspace/villas/node/clients/opal/villas_udp/models/send_receive/send_receive.mdl</UNC>
</Path>
</Model>
</Models>
</Project>