Index: trunk/absroot/source/absc/Control_Module/ServidorTCP.c =================================================================== diff --git a/trunk/absroot/source/absc/Control_Module/ServidorTCP.c b/trunk/absroot/source/absc/Control_Module/ServidorTCP.c --- a/trunk/absroot/source/absc/Control_Module/ServidorTCP.c (revision 111) +++ b/trunk/absroot/source/absc/Control_Module/ServidorTCP.c (revision 112) @@ -8,6 +8,10 @@ * -Carga en memoria los apuntes contenidos en un archivo de experimentos: apunte0 -> GPIO * -Cambio de apunte. * -Lectura del estado actual del apunte y grabado del mismo en un archivo + * + * Modified by Iván Manay since Nov 2012 + * -From UDP to TCP. + * -Use of a frame for TCP communications with the central control module. */ #include @@ -56,12 +60,11 @@ typedef struct control_module_parameters cmp; - char *header = NULL; char *TypeOfInstrument = NULL; char *iDSource = NULL; char *iDDestino = NULL; -char *len = NULL; +char *rx_len = NULL; char *cmd = NULL; char *rx_data = NULL; char *crc = NULL; @@ -79,11 +82,9 @@ int cambia_apuntamiento(char *puntero_char); int carga_experimento(char *nombre_archivo); char *chequeo_sistema(char *filename, char *numero_muestras); -int chequeo_sistema2(char *filename, char *numero_muestras); void recibe_experimento(char *data, char filename[]); -char* File2buffer(char *filename2, int n); void SplitFrame(char *frame); -void procesa_peticion2(char *rx_buffer, char *tx_buffer); +void intToStr( int number, char* str ); /* * @@ -190,6 +191,7 @@ close(clntSocket); /* Close client socket */ } + /* * Esta funcion incializa el puerto GPIO */ @@ -211,25 +213,78 @@ pio_disable_multiple_driver(piob, maskb_in); pio_disable_pull_ups(piob, maskb_in); pio_input_enable(piob, maskb_in); - -} +} + + +/* + * Divide rx frame into the frame components + */ +void SplitFrame(char *frame){ + header = malloc(4); + *header = *frame; + *(header + 1) = *(frame + 1); + *(header + 2) = *(frame + 2); + *(header + 3) = '\0'; + TypeOfInstrument = malloc(4); + *TypeOfInstrument = *(frame + 3); + *(TypeOfInstrument + 1) = *(frame + 4); + *(TypeOfInstrument + 2) = *(frame + 5); + *(TypeOfInstrument + 3) = '\0'; + iDSource = malloc(8); + *iDSource = *(frame + 6); + *(iDSource + 1) = *(frame + 7); + *(iDSource + 2) = *(frame + 8); + *(iDSource + 3) = *(frame + 9); + *(iDSource + 4) = *(frame + 10); + *(iDSource + 5) = *(frame + 11); + *(iDSource + 6) = *(frame + 12); + *(iDSource + 7) = '\0'; + iDDestino = malloc(8); + *iDDestino = *(frame + 13); + *(iDDestino + 1) = *(frame + 14); + *(iDDestino + 2) = *(frame + 15); + *(iDDestino + 3) = *(frame + 16); + *(iDDestino + 4) = *(frame + 17); + *(iDDestino + 5) = *(frame + 18); + *(iDDestino + 6) = *(frame + 19); + *(iDDestino + 7) = '\0'; + rx_len = malloc(7); + *rx_len = *(frame + 20); + *(rx_len + 1) = *(frame + 21); + *(rx_len + 2) = *(frame + 22); + *(rx_len + 3) = *(frame + 23); + *(rx_len + 4) = *(frame + 24); + *(rx_len + 5) = *(frame + 25); + *(rx_len + 6) = '\0'; + cmd = malloc(5); + *cmd = *(frame + 26); + *(cmd + 1) = *(frame + 27); + *(cmd + 2) = *(frame + 28); + *(cmd + 3) = *(frame + 29); + *(cmd + 4) = '\0'; + + int l = atoi(rx_len); + rx_data = malloc(l + 1); + int i; + for (i = 30; i < 30 + l; i++) + *(rx_data + (i-30)) = *(frame + i); + *(rx_data + l) = '\0'; + crc = malloc(2); + *crc = *(frame + 30 + l); + *(crc + 1) = '\0'; +} + /* * Esta funcion procesa el mensaje de peticion y genera respuesta */ -void procesa_peticion2(char *rx_buffer, char *tx_buffer){ - int n = 0; +void procesa_peticion(char *rx_buffer, char *tx_buffer){ +// int n = 0; char filename1[50]; char filename2[] = "verificacion.txt"; char *tx_data = NULL; - char *header = strtok(rx_buffer, ":"); - char *TypeOfInstrument = strtok(NULL, ":"); - char *iDSource = strtok(NULL, ":"); - char *iDDestino = strtok(NULL, ":"); - char *len = strtok(NULL, ":"); - char *cmd = strtok(NULL, ":"); - char *rx_data = strtok(NULL, ":"); - char *crc = strtok(NULL, ":"); + char *tx_len = NULL; + SplitFrame(rx_buffer); if ((cmd == NULL) || (rx_data == NULL)){ ERROR("procesarPeticion: formato de mensaje incorrecto"); @@ -246,143 +301,14 @@ cambia_apuntamiento(rx_data); tx_data = (char*)malloc(3); tx_data = "OK"; - } - else if(strcmp(cmd,"ANST") == 0){ - n = chequeo_sistema2(filename2,rx_data); - printf("%i\n",n); - tx_data = File2buffer(filename2, n); + tx_data = chequeo_sistema(filename2,rx_data); + printf("%s\n",tx_data); } - else{ - tx_data = (char*)malloc(6); - tx_data = "Error"; - ERROR("procesa_peticion: comando no reconocido"); - } - - strcpy(tx_buffer,header); - strcat(tx_buffer,":"); - strcat(tx_buffer,TypeOfInstrument); - strcat(tx_buffer,":"); - strcat(tx_buffer,iDDestino); - strcat(tx_buffer,":"); - strcat(tx_buffer,iDSource); - strcat(tx_buffer,":"); - strcat(tx_buffer,len); - strcat(tx_buffer,":"); - strcat(tx_buffer,cmd); - strcat(tx_buffer,":"); - strcat(tx_buffer,tx_data); - strcat(tx_buffer,":"); - strcat(tx_buffer,crc); - strcat(tx_buffer,":"); - strcat(tx_buffer,"quit"); - } -} - -void SplitFrame(char *frame){ - header = malloc(4); - *header = *frame; - *(header + 1) = *(frame + 1); - *(header + 2) = *(frame + 2); - *(header + 3) = '\0'; - printf("%s\n",header); - TypeOfInstrument = malloc(4); - *TypeOfInstrument = *(frame + 3); - *(TypeOfInstrument + 1) = *(frame + 4); - *(TypeOfInstrument + 2) = *(frame + 5); - *(TypeOfInstrument + 3) = '\0'; - printf("%s\n",TypeOfInstrument); - iDSource = malloc(8); - *iDSource = *(frame + 6); - *(iDSource + 1) = *(frame + 7); - *(iDSource + 2) = *(frame + 8); - *(iDSource + 3) = *(frame + 9); - *(iDSource + 4) = *(frame + 10); - *(iDSource + 5) = *(frame + 11); - *(iDSource + 6) = *(frame + 12); - *(iDSource + 7) = '\0'; - printf("%s\n",iDSource); - iDDestino = malloc(8); - *iDDestino = *(frame + 13); - *(iDDestino + 1) = *(frame + 14); - *(iDDestino + 2) = *(frame + 15); - *(iDDestino + 3) = *(frame + 16); - *(iDDestino + 4) = *(frame + 17); - *(iDDestino + 5) = *(frame + 18); - *(iDDestino + 6) = *(frame + 19); - *(iDDestino + 7) = '\0'; - printf("%s\n",iDDestino); - len = malloc(7); - *len = *(frame + 20); - *(len + 1) = *(frame + 21); - *(len + 2) = *(frame + 22); - *(len + 3) = *(frame + 23); - *(len + 4) = *(frame + 24); - *(len + 5) = *(frame + 25); - *(len + 6) = '\0'; - printf("%s\n",len); - cmd = malloc(5); - *cmd = *(frame + 26); - *(cmd + 1) = *(frame + 27); - *(cmd + 2) = *(frame + 28); - *(cmd + 3) = *(frame + 29); - *(cmd + 4) = '\0'; - printf("%s\n",cmd); - - int l = atoi(len) - 31; //Resto del tamaño total de la trama los 31 bytes fijos - rx_data = malloc(l + 1); - int i; - for (i = 30; i < 30 + l; i++) - *(rx_data + (i-30)) = *(frame + i); - *(rx_data + l) = '\0'; - printf("%s\n",rx_data); - crc = malloc(2); - *crc = *(frame + 30 + l); - *(crc + 1) = '\0'; - printf("%s\n",crc); -} - -void procesa_peticion(char *rx_buffer, char *tx_buffer){ -// int n = 0; - char filename1[50]; - char filename2[] = "verificacion.txt"; - char *tx_data = NULL; - SplitFrame(rx_buffer); - printf("Split\n"); - printf("%s\n",header); - printf("%s\n",TypeOfInstrument); - printf("%s\n",iDSource); - printf("%s\n",iDDestino); - printf("%s\n",len); - printf("%s\n",cmd); - printf("%s\n",rx_data); - printf("%s\n",crc); - printf("Split\n"); - - if ((cmd == NULL) || (rx_data == NULL)){ - ERROR("procesarPeticion: formato de mensaje incorrecto"); - } - else{ - if(strcmp(cmd,"SNDF") == 0){ - recibe_experimento(rx_data,filename1); - carga_experimento(filename1); - cambia_apuntamiento("0"); - tx_data = (char*)malloc(3); - tx_data = "OK"; - } - else if(strcmp(cmd,"CHGB") == 0){ - cambia_apuntamiento(rx_data); - tx_data = (char*)malloc(3); - tx_data = "OK"; - - } - - else if(strcmp(cmd,"ANST") == 0){ - /*n = chequeo_sistema(filename2,rx_data); - printf("%i\n",n); - tx_data = File2buffer(filename2, n);*/ - tx_data = chequeo_sistema(filename2,rx_data); + else if(strcmp(cmd,"NTST") == 0){ + tx_data = malloc(strlen(parameters.ID) + 1); + strcpy(tx_data,parameters.ID); printf("%s\n",tx_data); } else{ @@ -391,13 +317,17 @@ ERROR("procesa_peticion: comando no reconocido"); } + tx_len = malloc(7); + int number = strlen(tx_data); + intToStr(number, tx_len ); + strcpy(tx_buffer,header); //3 strcat(tx_buffer,TypeOfInstrument); //3 strcat(tx_buffer,parameters.ID); //7 strcat(tx_buffer,iDSource); //7 - strcat(tx_buffer,"000033"); //6 + strcat(tx_buffer,tx_len); //6 strcat(tx_buffer,cmd); //4 - strcat(tx_buffer,tx_data); //2 + strcat(tx_buffer,tx_data); //? strcat(tx_buffer,crc); //1 } @@ -407,7 +337,6 @@ /* * Esta función genera el archivo de experimento a partir de la trama TCP recibida */ - void recibe_experimento(char *p_data, char filename[]){ FILE *fd; int i = 0; @@ -516,71 +445,6 @@ return 1; -} - -/* - * Esta funcion lee "n" veces el estado del APUNTE actual y lo guarda en el - * archivo Verificacion. - */ - -int chequeo_sistema2(char *filename, char *numero_muestras){ - - char valor[7]; - int i,cnt; - unsigned int entradac= 0; - FILE *fd; - fd=fopen(filename,"w"); - fprintf(fd,"%s\n","Verificacion"); - fprintf(fd,"%s\n",parameters.ID); - fprintf(fd,"%s\n","------------"); - cnt=0; - do - { - //Inicializando arreglo - for(i=0;i<6;i++) - valor[i]='0'; - - valor[6]='\0'; - - //Lectura de puerto - entradac= pio_in(piob,maskb_in); - - //Dandole formato al dato - if (!(entradac & bit_up_2)) - valor[0] = '1'; - if (!(entradac & bit_up_1)) - valor[1] = '1'; - if (!(entradac & bit_up_0)) - valor[2] = '1'; - if (!(entradac & bit_dow_2)) - valor[3] = '1'; - if (!(entradac & bit_dow_1)) - valor[4] = '1'; - if (!(entradac & bit_dow_0)) - valor[5] = '1'; - - //Escribiendo en archivo - fprintf(fd,"%s\n",valor); - cnt=cnt+1; - usleep(1*1000*1000); - - }while(cnt < atoi(numero_muestras)); - - fclose(fd); - - return 7*atoi(numero_muestras) + 26 + 4 + 1; //incluye eof -} - -char* File2buffer(char *filename, int n){ - FILE *fd; - char* tx_data = (char *)malloc(n); - fd = fopen(filename,"r"); - fread(tx_data, 1, n-1, fd); - fclose(fd); - tx_data = tx_data + n - 1; - *tx_data = '\0'; - tx_data = tx_data - n + 1; - return tx_data; } /* @@ -635,6 +499,10 @@ strcpy(all_pages, page0); return all_pages; } + +/* + * + */ cmp inicializa_modulo(cmp p){ FILE *fd = fopen("configuration.txt","r"); fgets(p.ID,20,fd); @@ -648,3 +516,46 @@ fclose(fd); return p; } + +/* + * + */ +void intToStr( int number, char* str ) + +{ + int index = 0; + + while( number > 0 ) + { + int digit = number%10; + str[index++] = digit + '0'; + number /= 10; + } + str[index] = '\0'; + //Adding zero to the left + int n= strlen(str); + if (n == 1) { + strcat(str,"00000"); + index = index + 5; + }else if(n == 2){ + strcat(str,"0000"); + index = index + 4; + }else if(n == 3){ + strcat(str,"000"); + index = index + 3; + }else if(n == 4){ + strcat(str,"00"); + index = index + 2; + }else if(n == 5){ + strcat(str,"0"); + index = index + 1; + } + //Now reverse the numbers in the string. + int position; + for( position = 0; position <= (index-1)/2; ++position ) + { + char tmp = str[position]; + str[position] = str[(index-1)-position]; + str[(index-1)-position] = tmp; + } +}