From f59d2ccf1b2dc33e409a5878d2f9c09e45d67fa1 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sun, 23 Apr 2017 22:16:26 +0200 Subject: [PATCH] cleanup coding style --- .../opal/udp/models/send_receive/src/main.c | 117 +++++++------ .../opal/udp/models/send_receive/src/socket.c | 164 +++++++----------- .../opal/udp/models/send_receive/src/utils.c | 51 ++---- 3 files changed, 135 insertions(+), 197 deletions(-) diff --git a/clients/opal/udp/models/send_receive/src/main.c b/clients/opal/udp/models/send_receive/src/main.c index e12c03380..2ecd769e3 100644 --- a/clients/opal/udp/models/send_receive/src/main.c +++ b/clients/opal/udp/models/send_receive/src/main.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -43,6 +44,7 @@ /* This is the message format */ #include "config.h" #include "msg.h" +#include "msg_format.h" #include "socket.h" #include "utils.h" @@ -53,6 +55,7 @@ #define PRINT_SHMEM_NAME argv[3] #ifdef _DEBUG // TODO: workaround + #define CPU_TICKS 3466948000 struct msg *msg_send = NULL; @@ -75,23 +78,22 @@ void Tick(int sig, siginfo_t *si, void *ptr) } #endif /* _DEBUG */ -static void *SendToIPPort(void *arg) +static void * SendToIPPort(void *arg) { - unsigned int SendID = 1; - unsigned int ModelState; - unsigned int i, n; - int nbSend = 0; + unsigned int ModelState, SendID = 1, i, n; + int nbSend = 0, ret; uint32_t seq = 0; /* Data from OPAL-RT model */ - double mdldata[MSG_VALUES]; + double mdldata[MAX_VALUES]; int mdldata_size; /* Data from VILLASnode */ - struct msg msg = MSG_INIT(0); + char buf[MSG_LEN(MAX_VALUES)]; + struct msg *msg = (struct msg *) buf; #ifdef _DEBUG // TODO: workaround - msg_send = &msg; + msg_send = msg; #endif /* _DEBUG */ OpalPrint("%s: SendToIPPort thread started\n", PROGNAME); @@ -104,7 +106,8 @@ static void *SendToIPPort(void *arg) do { /* This call unblocks when the 'Data Ready' line of a send icon is asserted. */ - if ((n = OpalWaitForAsyncSendRequest(&SendID)) != EOK) { + n = OpalWaitForAsyncSendRequest(&SendID); + if (n != EOK) { ModelState = OpalGetAsyncModelState(); if ((ModelState != STATE_RESET) && (ModelState != STATE_STOP)) { OpalSetAsyncSendIconError(n, SendID); @@ -119,9 +122,9 @@ static void *SendToIPPort(void *arg) /* Get the size of the data being sent by the unblocking SendID */ OpalGetAsyncSendIconDataLength(&mdldata_size, SendID); - if (mdldata_size / sizeof(double) > MSG_VALUES) { + if (mdldata_size / sizeof(double) > MAX_VALUES) { OpalPrint("%s: Number of signals for SendID=%d exceeds allowed maximum (%d)\n", - PROGNAME, SendID, MSG_VALUES); + PROGNAME, SendID, MAX_VALUES); return NULL; } @@ -132,18 +135,19 @@ static void *SendToIPPort(void *arg) struct timespec now; clock_gettime(CLOCK_REALTIME, &now); - msg.length = mdldata_size / sizeof(double); - for (i = 0; i < msg.length; i++) - msg.data[i].f = (float) mdldata[i]; + msg->length = mdldata_size / sizeof(double); + for (i = 0; i < msg->length; i++) + msg->data[i].f = (float) mdldata[i]; - msg.sequence = seq++; - msg.ts.sec = now.tv_sec; - msg.ts.nsec = now.tv_nsec; + msg->sequence = seq++; + msg->ts.sec = now.tv_sec; + msg->ts.nsec = now.tv_nsec; msg_hton(msg); /* Perform the actual write to the ip port */ - if (SendPacket((char *) &msg, MSG_LEN(&msg)) < 0) + ret = SendPacket((char *) msg, MSG_LEN(msg->length)); + if (ret < 0) OpalSetAsyncSendIconError(errno, SendID); else OpalSetAsyncSendIconError(0, SendID); @@ -166,19 +170,18 @@ static void *SendToIPPort(void *arg) return NULL; } -static void *RecvFromIPPort(void *arg) +static void * RecvFromIPPort(void *arg) { - unsigned RecvID = 1; - unsigned i, n; - int nbRecv = 0; - unsigned ModelState; + unsigned int ModelState, RecvID = 1, i, n; + int nbRecv = 0, ret; /* Data from OPAL-RT model */ - double mdldata[MSG_VALUES]; + double mdldata[MAX_VALUES]; int mdldata_size; /* Data from VILLASnode */ - struct msg msg = MSG_INIT(0); + char buf[MSG_LEN(MAX_VALUES)]; + struct msg *msg = (struct msg *) buf; OpalPrint("%s: RecvFromIPPort thread started\n", PROGNAME); @@ -190,7 +193,7 @@ static void *RecvFromIPPort(void *arg) do { /* Receive message */ - n = RecvPacket((char *) &msg, sizeof(msg), 1.0); + n = RecvPacket((char *) msg, sizeof(buf), 1.0); if (n < 1) { ModelState = OpalGetAsyncModelState(); if ((ModelState != STATE_RESET) && (ModelState != STATE_STOP)) { @@ -204,41 +207,32 @@ static void *RecvFromIPPort(void *arg) break; } - /* Check message contents */ - if (msg.version != MSG_VERSION) { - OpalPrint("%s: Received message with unknown version. Skipping..\n", PROGNAME); - continue; - } msg_ntoh(msg); - if (msg.type != MSG_TYPE_DATA) { - OpalPrint("%s: Received no data. Skipping..\n", PROGNAME); - continue; - } - - if (n != MSG_LEN(&msg)) { - OpalPrint("%s: Received incoherent packet (size: %d, complete: %d)\n", PROGNAME, n, MSG_LEN(&msg)); + ret = msg_verify(msg); + if (ret) { + OpalPrint("%s: Skipping invalid packet\n", PROGNAME); continue; } /* Update OPAL model */ - OpalSetAsyncRecvIconStatus(msg.sequence, RecvID); /* Set the Status to the message ID */ + OpalSetAsyncRecvIconStatus(msg->sequence, RecvID); /* Set the Status to the message ID */ OpalSetAsyncRecvIconError(0, RecvID); /* Set the Error to 0 */ /* Get the number of signals to send back to the model */ OpalGetAsyncRecvIconDataLength(&mdldata_size, RecvID); - if (mdldata_size / sizeof(double) > MSG_VALUES) { + if (mdldata_size / sizeof(double) > MAX_VALUES) { OpalPrint("%s: Number of signals for RecvID=%d (%d) exceeds allowed maximum (%d)\n", - PROGNAME, RecvID, mdldata_size / sizeof(double), MSG_VALUES); + PROGNAME, RecvID, mdldata_size / sizeof(double), MAX_VALUES); return NULL; } - if (mdldata_size / sizeof(double) > msg.length) + 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); + PROGNAME, RecvID, mdldata_size / sizeof(double), msg->length); - for (i = 0; i < msg.length; i++) - mdldata[i] = (double) msg.data[i].f; + for (i = 0; i < msg->length; i++) + mdldata[i] = (double) msg->data[i].f; OpalSetAsyncRecvIconData(mdldata, mdldata_size, RecvID); @@ -254,7 +248,7 @@ static void *RecvFromIPPort(void *arg) int main(int argc, char *argv[]) { - int err; + int ret; Opal_GenAsyncParam_Ctrl IconCtrlStruct; @@ -276,24 +270,27 @@ int main(int argc, char *argv[]) } /* Open Share Memory created by the model. */ - if ((OpalOpenAsyncMem(ASYNC_SHMEM_SIZE, ASYNC_SHMEM_NAME)) != EOK) { + ret = OpalOpenAsyncMem(ASYNC_SHMEM_SIZE, ASYNC_SHMEM_NAME); + if (ret != EOK) { OpalPrint("%s: ERROR: Model shared memory not available\n", PROGNAME); exit(EXIT_FAILURE); } - /* For Redhawk, Assign this process to CPU 0 in order to support partial XHP */ AssignProcToCpu0(); /* Get IP Controler Parameters (ie: ip address, port number...) and * initialize the device on the QNX node. */ memset(&IconCtrlStruct, 0, sizeof(IconCtrlStruct)); - if ((err = OpalGetAsyncCtrlParameters(&IconCtrlStruct, sizeof(IconCtrlStruct))) != EOK) { - OpalPrint("%s: ERROR: Could not get controller parameters (%d).\n", PROGNAME, err); + + 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 */ - if (InitSocket(IconCtrlStruct) != EOK) { + ret = InitSocket(IconCtrlStruct); + if (ret != EOK) { OpalPrint("%s: ERROR: Initialization failed.\n", PROGNAME); exit(EXIT_FAILURE); } @@ -326,16 +323,22 @@ int main(int argc, char *argv[]) #endif /* _DEBUG */ /* Start send/receive threads */ - if ((pthread_create(&tid_send, NULL, SendToIPPort, NULL)) == -1) + ret = pthread_create(&tid_send, NULL, SendToIPPort, NULL); + if (ret == -1) OpalPrint("%s: ERROR: Could not create thread (SendToIPPort), errno %d\n", PROGNAME, errno); - if ((pthread_create(&tid_recv, NULL, RecvFromIPPort, NULL)) == -1) + + 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 */ - if ((err = pthread_join(tid_send, NULL)) != 0) - OpalPrint("%s: ERROR: pthread_join (SendToIPPort), errno %d\n", PROGNAME, err); - if ((err = pthread_join(tid_recv, NULL)) != 0) - OpalPrint("%s: ERROR: pthread_join (RecvFromIPPort), errno %d\n", PROGNAME, err); + 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 */ CloseSocket(IconCtrlStruct); diff --git a/clients/opal/udp/models/send_receive/src/socket.c b/clients/opal/udp/models/send_receive/src/socket.c index 298404a51..f49cc022b 100644 --- a/clients/opal/udp/models/send_receive/src/socket.c +++ b/clients/opal/udp/models/send_receive/src/socket.c @@ -2,8 +2,7 @@ * * 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. + * receive data to and from the asynchronous icons and a UDP port. * * @author Steffen Vogel * @author Mathieu Dubé-Dallaire @@ -35,43 +34,27 @@ struct sockaddr_in send_ad; /* Send address */ struct sockaddr_in recv_ad; /* Receive address */ int sd = -1; /* socket descriptor */ -int proto = UDP_PROTOCOL; int InitSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct) { struct ip_mreq mreq; /* Multicast group structure */ - int socket_type; - int socket_proto; - unsigned char TTL = 1; - unsigned char LOOP = 0; - int rc; + unsigned char TTL = 1, LOOP = 0; + int rc, proto, ret; proto = (int) IconCtrlStruct.FloatParam[0]; - OpalPrint("%s: Version : %s\n", PROGNAME, VERSION); - - switch (proto) { - case UDP_PROTOCOL: /* Communication using UDP/IP protocol */ - socket_proto = IPPROTO_UDP; - socket_type = SOCK_DGRAM; - OpalPrint("%s: Protocol : UDP/IP\n", PROGNAME); - break; - - case TCP_PROTOCOL: /* Communication using TCP/IP protocol */ - socket_proto = IPPROTO_IP; - socket_type = SOCK_STREAM; - OpalPrint("%s: Protocol : TCP/IP\n", PROGNAME); - break; - - default: /* Protocol is not recognized */ - OpalPrint("%s: ERROR: Protocol (%d) not supported!\n", PROGNAME, proto); - return EINVAL; + 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 */ - if ((sd = socket(AF_INET, socket_type, socket_proto)) < 0) { + sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sd < 0) { OpalPrint("%s: ERROR: Could not open socket\n", PROGNAME); return EIO; } @@ -89,89 +72,82 @@ 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) { + ret = bind(sd, (struct sockaddr *) &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]); - switch (proto) { - 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) { - 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) { - OpalPrint("%s: ERROR: Could not set loopback for multicast send (%d)\n", PROGNAME, errno); - return EIO; - } + /* 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(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(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); - } + 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; + /* 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 */ - 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; - } - - 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); - } - } - break; - - case TCP_PROTOCOL: /* Communication using TCP/IP protocol */ - OpalPrint("%s: Calling connect()\n", PROGNAME); - - /* Connect to server to start data transmission */ - rc = connect(sd, (struct sockaddr *) &send_ad, sizeof(send_ad)); - if (rc < 0) { - OpalPrint("%s: ERROR: Call to connect() failed\n", PROGNAME); + /* Have the multicast socket join the multicast group */ + ret = setsockopt(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; } - break; + + 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 CloseSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct) +{ + if (sd < 0) { + shutdown(sd, SHUT_RDWR); + close(sd); + } + + return 0; +} + int SendPacket(char* DataSend, int datalength) { - int err; - - if(sd < 0) + if (sd < 0) return -1; /* Send the packet */ - if (proto == TCP_PROTOCOL) - err = send(sd, DataSend, datalength, 0); - else - err = sendto(sd, DataSend, datalength, 0, (struct sockaddr *)&send_ad, sizeof(send_ad)); - - return err; + return sendto(sd, DataSend, datalength, 0, (struct sockaddr *)&send_ad, sizeof(send_ad)); } int RecvPacket(char* DataRecv, int datalength, double timeout) { - int len; + int len, ret; struct sockaddr_in client_ad; + struct timeval tv; socklen_t client_ad_size = sizeof(client_ad); fd_set sd_set; - struct timeval tv; if (sd < 0) return -1; @@ -188,7 +164,8 @@ int RecvPacket(char* DataRecv, int datalength, double timeout) * 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)) { + ret = select(sd+1, &sd_set, (fd_set *) 0, (fd_set *) 0, &tv); + switch (ret) { case -1: /* Error */ return -1; case 0: /* We hit the timeout */ @@ -206,20 +183,5 @@ int RecvPacket(char* DataRecv, int datalength, double timeout) memset(DataRecv, 0, datalength); /* Perform the reception */ - if (proto == TCP_PROTOCOL) - len = recv(sd, DataRecv, datalength, 0); - else - len = recvfrom(sd, DataRecv, datalength, 0, (struct sockaddr *) &client_ad, &client_ad_size); - - return len; -} - -int CloseSocket(Opal_GenAsyncParam_Ctrl IconCtrlStruct) -{ - if (sd < 0) { - shutdown(sd, SHUT_RDWR); - close(sd); - } - - return 0; -} + return recvfrom(sd, DataRecv, datalength, 0, (struct sockaddr *) &client_ad, &client_ad_size); +} \ No newline at end of file diff --git a/clients/opal/udp/models/send_receive/src/utils.c b/clients/opal/udp/models/send_receive/src/utils.c index e8c599807..7a8e0ded3 100644 --- a/clients/opal/udp/models/send_receive/src/utils.c +++ b/clients/opal/udp/models/send_receive/src/utils.c @@ -18,57 +18,30 @@ #include "utils.h" #if defined(__QNXNTO__) -# include -# include -# include -# include + #include + #include + #include + #include #elif defined(__linux__) -# define _GNU_SOURCE 1 -# include -# if defined(__redhawk__) -# include -# include -# endif + #define _GNU_SOURCE 1 + #include #endif int AssignProcToCpu0(void) { -#if defined(__linux__) - #if defined(__redhawk__) - int rc; - pid_t pid = getpid(); - cpuset_t *pCpuset; - - pCpuset = cpuset_alloc(); - if (NULL == pCpuset) { - OpalPrint("Error allocating a cpuset\n"); - return ENOMEM; - } - - cpuset_init(pCpuset); - cpuset_set_cpu(pCpuset, 0, 1); - - rc = mpadvise(MPA_PRC_SETBIAS, MPA_TID, pid, pCpuset); - if (MPA_FAILURE == rc) { - rc = errno; - OpalPrint("Error from mpadvise, %d %s, for pid %d\n", errno, strerror(errno), pid); - cpuset_free(pCpuset); - return rc; - } - - cpuset_free(pCpuset); - #else +#ifdef __linux__ + int ret; cpu_set_t bindSet; CPU_ZERO(&bindSet); CPU_SET(0, &bindSet); - /* changing process cpu affinity */ - if (sched_setaffinity(0, sizeof(cpu_set_t), &bindSet) != 0) { + /* Changing process cpu affinity */ + ret = sched_setaffinity(0, sizeof(cpu_set_t), &bindSet); + if (ret != 0) { OpalPrint("Unable to bind the process to CPU 0. (sched_setaffinity errno %d)\n", errno); return EINVAL; } - - #endif + return EOK; #endif /* __linux__ */ }