diff --git a/clients/opal/models/AsyncIP_sl/AsyncIPUtils.h b/clients/opal/models/AsyncIP_sl/socket.c similarity index 72% rename from clients/opal/models/AsyncIP_sl/AsyncIPUtils.h rename to clients/opal/models/AsyncIP_sl/socket.c index 8cf2a4c2c..3a781c1aa 100644 --- a/clients/opal/models/AsyncIP_sl/AsyncIPUtils.h +++ b/clients/opal/models/AsyncIP_sl/socket.c @@ -1,13 +1,16 @@ -/*------------------------------------------------------------------- - * OPAL-RT Technologies inc +/** Helper functions for socket * - * Copyright (C) 2003. All rights reserved. + * 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. * - * File name = AsyncIPUtils.c - * Last modified by = Mathieu Dubé-Dallaire - * - * - *-----------------------------------------------------------------*/ + * @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 OPAL_IP_H #define OPAL_IP_H @@ -37,8 +40,8 @@ #define EOK 0 /* Globals variables */ -struct sockaddr_in send_ad; /* Send address */ -struct sockaddr_in recv_ad; /* Recveive address */ +struct sockaddr_in send_ad; /* Send address */ +struct sockaddr_in recv_ad; /* Recveive address */ int sd = -1; /* socket descriptor */ int proto = 1; @@ -69,7 +72,7 @@ int InitSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct) } OpalPrint("%s: Remote Address : %s\n", PROGNAME, IconCtrlStruct.StringParam[0]); - OpalPrint("%s: Remote Port : %d\n", PROGNAME, (int)IconCtrlStruct.FloatParam[1]); + OpalPrint("%s: Remote Port : %d\n", PROGNAME, (int) IconCtrlStruct.FloatParam[1]); /* Initialize the socket */ if ((sd = socket(AF_INET, socket_type, socket_proto)) < 0) { @@ -90,7 +93,7 @@ int InitSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct) recv_ad.sin_port = htons((u_short)IconCtrlStruct.FloatParam[2]); /* Bind local port and address to socket. */ - if (bind(sd, (struct sockaddr *)&recv_ad, sizeof(struct sockaddr_in)) == -1) { + if (bind(sd, (struct sockaddr *) &recv_ad, sizeof(struct sockaddr_in)) == -1) { OpalPrint("%s: ERROR: Could not bind local port to socket\n", PROGNAME); return EIO; } @@ -101,11 +104,11 @@ int InitSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct) case UDP_PROTOCOL: /* Communication using UDP/IP protocol */ /* If sending to a multicast address */ if ((inet_addr(IconCtrlStruct.StringParam[0]) & inet_addr("240.0.0.0")) == inet_addr("224.0.0.0")) { - if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&TTL, sizeof(TTL)) == -1) { + if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &TTL, sizeof(TTL)) == -1) { OpalPrint("%s: ERROR: Could not set TTL for multicast send (%d)\n", PROGNAME, errno); return EIO; } - if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, (char*)&LOOP, sizeof(LOOP)) == -1) { + if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&LOOP, sizeof(LOOP)) == -1) { OpalPrint("%s: ERROR: Could not set loopback for multicast send (%d)\n", PROGNAME, errno); return EIO; } @@ -120,7 +123,7 @@ int InitSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct) mreq.imr_interface.s_addr = INADDR_ANY; /* Have the multicast socket join the multicast group */ - if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mreq, sizeof(mreq)) == -1) { + if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq)) == -1) { OpalPrint("%s: ERROR: Could not join multicast group (%d)\n", PROGNAME, errno); return EIO; } @@ -137,7 +140,7 @@ int InitSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct) OpalPrint("%s: Calling connect()\n", PROGNAME); /* Connect to server to start data transmission */ - rc = connect(sd, (struct sockaddr *)&send_ad, sizeof(send_ad)); + rc = connect(sd, (struct sockaddr *) &send_ad, sizeof(send_ad)); if (rc < 0) { OpalPrint("%s: ERROR: Call to connect() failed\n", PROGNAME); return EIO; @@ -147,7 +150,7 @@ int InitSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct) return EOK; } -int SendPacket (char* DataSend, int datalength) +int SendPacket(char* DataSend, int datalength) { int err; @@ -156,14 +159,14 @@ int SendPacket (char* DataSend, int datalength) /* Send the packet */ if (proto == TCP_PROTOCOL) - err = send (sd, DataSend, datalength, 0); + err = send(sd, DataSend, datalength, 0); else - err = sendto (sd, DataSend, datalength, 0, (struct sockaddr *)&send_ad, sizeof(send_ad)); + err = sendto(sd, DataSend, datalength, 0, (struct sockaddr *)&send_ad, sizeof(send_ad)); return err; } -int RecvPacket (char* DataRecv, int datalength, double timeout) +int RecvPacket(char* DataRecv, int datalength, double timeout) { int len; struct sockaddr_in client_ad; @@ -179,20 +182,20 @@ int RecvPacket (char* DataRecv, int datalength, double timeout) FD_SET (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); + 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). */ - switch (select(sd+1, &sd_set, (fd_set*)0, (fd_set*)0, &tv)) { + switch (select(sd+1, &sd_set, (fd_set *) 0, (fd_set *) 0, &tv)) { case -1: /* Error */ return -1; case 0: /* We hit the timeout */ return 0; default: - if (!(FD_ISSET (sd, &sd_set))) { + if (!(FD_ISSET(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); @@ -201,18 +204,18 @@ int RecvPacket (char* DataRecv, int datalength, double timeout) } /* Clear the DataRecv array (in case we receive an incomplete packet) */ - memset (DataRecv, 0, datalength); + memset(DataRecv, 0, datalength); /* Perform the reception */ if (proto == TCP_PROTOCOL) - len = recv (sd, DataRecv, datalength, 0); + len = recv(sd, DataRecv, datalength, 0); else - len = recvfrom (sd, DataRecv, datalength, 0, (struct sockaddr *)&client_ad, &client_ad_size); + len = recvfrom(sd, DataRecv, datalength, 0, (struct sockaddr *) &client_ad, &client_ad_size); return len; } -int CloseSocket (Opal_GenAsyncParam_Ctrl IconCtrlStruct) +int CloseSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct) { if (sd < 0) { shutdown(sd, SHUT_RDWR);