annotate core/client.cpp @ 39:638bc1ae2500 staging

Improved readibility of the DIGITAL code in the PRU, using register names instead of aliases and expanding some of the macros, removing unused macros. Binaries were not modified
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 13 May 2015 12:18:10 +0100
parents 12ee9a61e81d
children 4255ecbb9bec 579c86316008
rev   line source
giuliomoro@24 1 ///* UDP client in the internet domain */
giuliomoro@24 2
giuliomoro@24 3 #include <ctype.h>
giuliomoro@24 4 #include <sys/time.h>
giuliomoro@24 5 #include <fcntl.h>
giuliomoro@24 6 #include "../include/client.h"
giuliomoro@24 7 #include <unistd.h>
giuliomoro@24 8 #include <rtdk.h>
giuliomoro@24 9
giuliomoro@24 10 #define BUFF_LEN 1024
giuliomoro@32 11 #define MAX_VAR_STRING 20
giuliomoro@24 12
giuliomoro@24 13 struct sockaddr_in outServer, inServer;
giuliomoro@24 14 int outSock, inSock, n, length;
giuliomoro@24 15 socklen_t fromlen;
giuliomoro@24 16 struct sockaddr_in from;
giuliomoro@24 17 char inBuffer[1024];
giuliomoro@24 18 char variableString[MAX_VAR_STRING];
giuliomoro@24 19
giuliomoro@24 20 int setupSockets(int receivePort, int transmitPort, char const*serverName){
giuliomoro@24 21 //setup transmitter
giuliomoro@24 22 printf("receivePort: %d; transmitPort: %d; serverName: %s\n",receivePort, transmitPort, serverName);
giuliomoro@24 23 outSock= socket(AF_INET, SOCK_DGRAM, 0);
giuliomoro@24 24 outServer.sin_port = htons(transmitPort);
giuliomoro@24 25 if (outSock < 0){
giuliomoro@24 26 error("Opening out socket");
giuliomoro@24 27 return -1;
giuliomoro@24 28 }
giuliomoro@24 29 outServer.sin_family = AF_INET;
giuliomoro@24 30 inet_pton(AF_INET,serverName,&outServer.sin_addr);
giuliomoro@24 31
giuliomoro@24 32 //setup receiver
giuliomoro@24 33 inSock=socket(AF_INET, SOCK_DGRAM, 0);
giuliomoro@24 34 if (inSock < 0){
giuliomoro@24 35 return -1;
giuliomoro@24 36 error("Opening in socket");
giuliomoro@24 37 }
giuliomoro@24 38 length = sizeof(inServer);
giuliomoro@24 39 inServer.sin_family=AF_INET;
giuliomoro@24 40 inServer.sin_addr.s_addr=INADDR_ANY;
giuliomoro@24 41 inServer.sin_port=htons(receivePort);
giuliomoro@24 42 if (bind(inSock,(struct sockaddr *)&inServer,length)<0)
giuliomoro@24 43 error("binding");
giuliomoro@24 44 fromlen = sizeof(struct sockaddr_in);
giuliomoro@24 45 return 0;
giuliomoro@24 46 }
giuliomoro@24 47 int sendMessage(networkData message)
giuliomoro@24 48 {
giuliomoro@24 49 unsigned int length;
giuliomoro@24 50 char buffer[BUFF_LEN];
giuliomoro@24 51 length=sizeof(struct sockaddr_in);
giuliomoro@24 52 int k=0;
giuliomoro@24 53 k=sprintf(buffer+k, "%8d;",*message.counter);
giuliomoro@24 54 for(int j=0; j<message.numVariables; j++){
giuliomoro@24 55 k+=sprintf(buffer+k, "%.3f;",*message.variables[j]);
giuliomoro@24 56 if(k>BUFF_LEN - 20) //safety margin
giuliomoro@24 57 continue;
giuliomoro@24 58 }
giuliomoro@24 59 sprintf(buffer+k,"\n");
giuliomoro@24 60 // printf(buffer);
giuliomoro@24 61 n=sendto(outSock,buffer,
giuliomoro@24 62 strlen(buffer),0,(const struct sockaddr *)&outServer,length);
giuliomoro@24 63 if (n < 0) error("Sendto");
giuliomoro@24 64 return 0;
giuliomoro@24 65 }
giuliomoro@24 66 /*
giuliomoro@24 67 int receiveMessage()
giuliomoro@24 68 {
giuliomoro@24 69 int n = recvfrom(inSock,inBuffer,1024,0,(struct sockaddr *)&from,&fromlen);
giuliomoro@24 70 if (n < 0) error("recvfrom");
giuliomoro@24 71 printf("Received a datagram: ");
giuliomoro@24 72 printf(inBuffer);
giuliomoro@24 73 }
giuliomoro@24 74 */
giuliomoro@24 75
giuliomoro@24 76
giuliomoro@24 77 int receiveMessage(networkData message){
giuliomoro@24 78 struct timeval stTimeOut;
giuliomoro@24 79 fd_set stReadFDS;
giuliomoro@24 80 FD_ZERO(&stReadFDS);
giuliomoro@24 81 // Timeout of one second
giuliomoro@24 82 stTimeOut.tv_sec = 0;
giuliomoro@25 83 stTimeOut.tv_usec = 0;
giuliomoro@24 84 FD_SET(inSock, &stReadFDS);
giuliomoro@24 85
giuliomoro@24 86 int t = select(inSock+1, &stReadFDS, NULL, NULL, &stTimeOut);
giuliomoro@24 87 if (t == -1) {
giuliomoro@24 88 rt_fprintf(stderr, "Call to select() failed");
giuliomoro@24 89 return -1;
giuliomoro@24 90 }
giuliomoro@24 91 else if (t != 0) {
giuliomoro@24 92 if (FD_ISSET(inSock, &stReadFDS)) {
giuliomoro@24 93 // printf("There is data pending to be read..."); // Read data with recv()
giuliomoro@24 94 int n = recvfrom(inSock,inBuffer,1024,0,(struct sockaddr *)&from,&fromlen);
giuliomoro@24 95 if (n < 0){
giuliomoro@24 96 rt_fprintf(stderr,"Error while receiving");
giuliomoro@24 97 return -1;
giuliomoro@24 98 }
giuliomoro@24 99 printf("Received a datagram: ");
giuliomoro@24 100 printf(inBuffer);
giuliomoro@24 101 //the worst parser ever
giuliomoro@24 102 int previousN=0;
giuliomoro@24 103 int currentVariable=0;
giuliomoro@25 104 for(int n=0; inBuffer[n]!=0 && currentVariable<message.numVariables && n-previousN<MAX_VAR_STRING && n<BUFF_LEN; n++){ //scan the string
giuliomoro@25 105 if(inBuffer[n]==';'||inBuffer[n]==0||inBuffer[n]=='\n'){ // if you find a separator or you are at the end of the string, parse the variable
giuliomoro@24 106 int j=0;
giuliomoro@24 107 inBuffer[n]=0; //set the semicolon to 0 ...
giuliomoro@25 108 while( (variableString[j++]=inBuffer[previousN++]) );; // ... so that this will stop when it gets there
giuliomoro@24 109 rt_printf("variable %d: %s\n", currentVariable, variableString);
giuliomoro@24 110 *(message.variables[currentVariable])=atof(variableString);
giuliomoro@24 111 n++; //increment to step after the semicolon
giuliomoro@24 112 previousN=n;
giuliomoro@24 113 currentVariable++;
giuliomoro@24 114 }
giuliomoro@24 115 }
giuliomoro@24 116 }
giuliomoro@24 117 }
giuliomoro@24 118 return 0;
giuliomoro@24 119 }
giuliomoro@24 120
giuliomoro@24 121 void closeSockets(){
giuliomoro@24 122 close(outSock);
giuliomoro@24 123 close(inSock);
giuliomoro@24 124 }
giuliomoro@24 125 void error(const char *msg)
giuliomoro@24 126 {
giuliomoro@24 127 perror(msg);
giuliomoro@24 128 exit(0);
giuliomoro@24 129 }