##// END OF EJS Templates
getConnectionStatus changes
imanay -
r111:112
parent child
Show More
@@ -8,6 +8,10
8 * -Carga en memoria los apuntes contenidos en un archivo de experimentos: apunte0 -> GPIO
8 * -Carga en memoria los apuntes contenidos en un archivo de experimentos: apunte0 -> GPIO
9 * -Cambio de apunte.
9 * -Cambio de apunte.
10 * -Lectura del estado actual del apunte y grabado del mismo en un archivo
10 * -Lectura del estado actual del apunte y grabado del mismo en un archivo
11 *
12 * Modified by Iván Manay since Nov 2012
13 * -From UDP to TCP.
14 * -Use of a frame for TCP communications with the central control module.
11 */
15 */
12
16
13 #include <stdio.h>
17 #include <stdio.h>
@@ -56,12 +60,11
56
60
57 typedef struct control_module_parameters cmp;
61 typedef struct control_module_parameters cmp;
58
62
59
60 char *header = NULL;
63 char *header = NULL;
61 char *TypeOfInstrument = NULL;
64 char *TypeOfInstrument = NULL;
62 char *iDSource = NULL;
65 char *iDSource = NULL;
63 char *iDDestino = NULL;
66 char *iDDestino = NULL;
64 char *len = NULL;
67 char *rx_len = NULL;
65 char *cmd = NULL;
68 char *cmd = NULL;
66 char *rx_data = NULL;
69 char *rx_data = NULL;
67 char *crc = NULL;
70 char *crc = NULL;
@@ -79,11 +82,9
79 int cambia_apuntamiento(char *puntero_char);
82 int cambia_apuntamiento(char *puntero_char);
80 int carga_experimento(char *nombre_archivo);
83 int carga_experimento(char *nombre_archivo);
81 char *chequeo_sistema(char *filename, char *numero_muestras);
84 char *chequeo_sistema(char *filename, char *numero_muestras);
82 int chequeo_sistema2(char *filename, char *numero_muestras);
83 void recibe_experimento(char *data, char filename[]);
85 void recibe_experimento(char *data, char filename[]);
84 char* File2buffer(char *filename2, int n);
85 void SplitFrame(char *frame);
86 void SplitFrame(char *frame);
86 void procesa_peticion2(char *rx_buffer, char *tx_buffer);
87 void intToStr( int number, char* str );
87
88
88 /*
89 /*
89 *
90 *
@@ -190,6 +191,7
190
191
191 close(clntSocket); /* Close client socket */
192 close(clntSocket); /* Close client socket */
192 }
193 }
194
193 /*
195 /*
194 * Esta funcion incializa el puerto GPIO
196 * Esta funcion incializa el puerto GPIO
195 */
197 */
@@ -211,25 +213,78
211 pio_disable_multiple_driver(piob, maskb_in);
213 pio_disable_multiple_driver(piob, maskb_in);
212 pio_disable_pull_ups(piob, maskb_in);
214 pio_disable_pull_ups(piob, maskb_in);
213 pio_input_enable(piob, maskb_in);
215 pio_input_enable(piob, maskb_in);
214
216 }
215 }
217
218
219 /*
220 * Divide rx frame into the frame components
221 */
222 void SplitFrame(char *frame){
223 header = malloc(4);
224 *header = *frame;
225 *(header + 1) = *(frame + 1);
226 *(header + 2) = *(frame + 2);
227 *(header + 3) = '\0';
228 TypeOfInstrument = malloc(4);
229 *TypeOfInstrument = *(frame + 3);
230 *(TypeOfInstrument + 1) = *(frame + 4);
231 *(TypeOfInstrument + 2) = *(frame + 5);
232 *(TypeOfInstrument + 3) = '\0';
233 iDSource = malloc(8);
234 *iDSource = *(frame + 6);
235 *(iDSource + 1) = *(frame + 7);
236 *(iDSource + 2) = *(frame + 8);
237 *(iDSource + 3) = *(frame + 9);
238 *(iDSource + 4) = *(frame + 10);
239 *(iDSource + 5) = *(frame + 11);
240 *(iDSource + 6) = *(frame + 12);
241 *(iDSource + 7) = '\0';
242 iDDestino = malloc(8);
243 *iDDestino = *(frame + 13);
244 *(iDDestino + 1) = *(frame + 14);
245 *(iDDestino + 2) = *(frame + 15);
246 *(iDDestino + 3) = *(frame + 16);
247 *(iDDestino + 4) = *(frame + 17);
248 *(iDDestino + 5) = *(frame + 18);
249 *(iDDestino + 6) = *(frame + 19);
250 *(iDDestino + 7) = '\0';
251 rx_len = malloc(7);
252 *rx_len = *(frame + 20);
253 *(rx_len + 1) = *(frame + 21);
254 *(rx_len + 2) = *(frame + 22);
255 *(rx_len + 3) = *(frame + 23);
256 *(rx_len + 4) = *(frame + 24);
257 *(rx_len + 5) = *(frame + 25);
258 *(rx_len + 6) = '\0';
259 cmd = malloc(5);
260 *cmd = *(frame + 26);
261 *(cmd + 1) = *(frame + 27);
262 *(cmd + 2) = *(frame + 28);
263 *(cmd + 3) = *(frame + 29);
264 *(cmd + 4) = '\0';
265
266 int l = atoi(rx_len);
267 rx_data = malloc(l + 1);
268 int i;
269 for (i = 30; i < 30 + l; i++)
270 *(rx_data + (i-30)) = *(frame + i);
271 *(rx_data + l) = '\0';
272 crc = malloc(2);
273 *crc = *(frame + 30 + l);
274 *(crc + 1) = '\0';
275 }
276
216
277
217 /*
278 /*
218 * Esta funcion procesa el mensaje de peticion y genera respuesta
279 * Esta funcion procesa el mensaje de peticion y genera respuesta
219 */
280 */
220 void procesa_peticion2(char *rx_buffer, char *tx_buffer){
281 void procesa_peticion(char *rx_buffer, char *tx_buffer){
221 int n = 0;
282 // int n = 0;
222 char filename1[50];
283 char filename1[50];
223 char filename2[] = "verificacion.txt";
284 char filename2[] = "verificacion.txt";
224 char *tx_data = NULL;
285 char *tx_data = NULL;
225 char *header = strtok(rx_buffer, ":");
286 char *tx_len = NULL;
226 char *TypeOfInstrument = strtok(NULL, ":");
287 SplitFrame(rx_buffer);
227 char *iDSource = strtok(NULL, ":");
228 char *iDDestino = strtok(NULL, ":");
229 char *len = strtok(NULL, ":");
230 char *cmd = strtok(NULL, ":");
231 char *rx_data = strtok(NULL, ":");
232 char *crc = strtok(NULL, ":");
233
288
234 if ((cmd == NULL) || (rx_data == NULL)){
289 if ((cmd == NULL) || (rx_data == NULL)){
235 ERROR("procesarPeticion: formato de mensaje incorrecto");
290 ERROR("procesarPeticion: formato de mensaje incorrecto");
@@ -246,143 +301,14
246 cambia_apuntamiento(rx_data);
301 cambia_apuntamiento(rx_data);
247 tx_data = (char*)malloc(3);
302 tx_data = (char*)malloc(3);
248 tx_data = "OK";
303 tx_data = "OK";
249
250 }
304 }
251
252 else if(strcmp(cmd,"ANST") == 0){
305 else if(strcmp(cmd,"ANST") == 0){
253 n = chequeo_sistema2(filename2,rx_data);
306 tx_data = chequeo_sistema(filename2,rx_data);
254 printf("%i\n",n);
307 printf("%s\n",tx_data);
255 tx_data = File2buffer(filename2, n);
256 }
308 }
257 else{
309 else if(strcmp(cmd,"NTST") == 0){
258 tx_data = (char*)malloc(6);
310 tx_data = malloc(strlen(parameters.ID) + 1);
259 tx_data = "Error";
311 strcpy(tx_data,parameters.ID);
260 ERROR("procesa_peticion: comando no reconocido");
261 }
262
263 strcpy(tx_buffer,header);
264 strcat(tx_buffer,":");
265 strcat(tx_buffer,TypeOfInstrument);
266 strcat(tx_buffer,":");
267 strcat(tx_buffer,iDDestino);
268 strcat(tx_buffer,":");
269 strcat(tx_buffer,iDSource);
270 strcat(tx_buffer,":");
271 strcat(tx_buffer,len);
272 strcat(tx_buffer,":");
273 strcat(tx_buffer,cmd);
274 strcat(tx_buffer,":");
275 strcat(tx_buffer,tx_data);
276 strcat(tx_buffer,":");
277 strcat(tx_buffer,crc);
278 strcat(tx_buffer,":");
279 strcat(tx_buffer,"quit");
280 }
281 }
282
283 void SplitFrame(char *frame){
284 header = malloc(4);
285 *header = *frame;
286 *(header + 1) = *(frame + 1);
287 *(header + 2) = *(frame + 2);
288 *(header + 3) = '\0';
289 printf("%s\n",header);
290 TypeOfInstrument = malloc(4);
291 *TypeOfInstrument = *(frame + 3);
292 *(TypeOfInstrument + 1) = *(frame + 4);
293 *(TypeOfInstrument + 2) = *(frame + 5);
294 *(TypeOfInstrument + 3) = '\0';
295 printf("%s\n",TypeOfInstrument);
296 iDSource = malloc(8);
297 *iDSource = *(frame + 6);
298 *(iDSource + 1) = *(frame + 7);
299 *(iDSource + 2) = *(frame + 8);
300 *(iDSource + 3) = *(frame + 9);
301 *(iDSource + 4) = *(frame + 10);
302 *(iDSource + 5) = *(frame + 11);
303 *(iDSource + 6) = *(frame + 12);
304 *(iDSource + 7) = '\0';
305 printf("%s\n",iDSource);
306 iDDestino = malloc(8);
307 *iDDestino = *(frame + 13);
308 *(iDDestino + 1) = *(frame + 14);
309 *(iDDestino + 2) = *(frame + 15);
310 *(iDDestino + 3) = *(frame + 16);
311 *(iDDestino + 4) = *(frame + 17);
312 *(iDDestino + 5) = *(frame + 18);
313 *(iDDestino + 6) = *(frame + 19);
314 *(iDDestino + 7) = '\0';
315 printf("%s\n",iDDestino);
316 len = malloc(7);
317 *len = *(frame + 20);
318 *(len + 1) = *(frame + 21);
319 *(len + 2) = *(frame + 22);
320 *(len + 3) = *(frame + 23);
321 *(len + 4) = *(frame + 24);
322 *(len + 5) = *(frame + 25);
323 *(len + 6) = '\0';
324 printf("%s\n",len);
325 cmd = malloc(5);
326 *cmd = *(frame + 26);
327 *(cmd + 1) = *(frame + 27);
328 *(cmd + 2) = *(frame + 28);
329 *(cmd + 3) = *(frame + 29);
330 *(cmd + 4) = '\0';
331 printf("%s\n",cmd);
332
333 int l = atoi(len) - 31; //Resto del tamaño total de la trama los 31 bytes fijos
334 rx_data = malloc(l + 1);
335 int i;
336 for (i = 30; i < 30 + l; i++)
337 *(rx_data + (i-30)) = *(frame + i);
338 *(rx_data + l) = '\0';
339 printf("%s\n",rx_data);
340 crc = malloc(2);
341 *crc = *(frame + 30 + l);
342 *(crc + 1) = '\0';
343 printf("%s\n",crc);
344 }
345
346 void procesa_peticion(char *rx_buffer, char *tx_buffer){
347 // int n = 0;
348 char filename1[50];
349 char filename2[] = "verificacion.txt";
350 char *tx_data = NULL;
351 SplitFrame(rx_buffer);
352 printf("Split\n");
353 printf("%s\n",header);
354 printf("%s\n",TypeOfInstrument);
355 printf("%s\n",iDSource);
356 printf("%s\n",iDDestino);
357 printf("%s\n",len);
358 printf("%s\n",cmd);
359 printf("%s\n",rx_data);
360 printf("%s\n",crc);
361 printf("Split\n");
362
363 if ((cmd == NULL) || (rx_data == NULL)){
364 ERROR("procesarPeticion: formato de mensaje incorrecto");
365 }
366 else{
367 if(strcmp(cmd,"SNDF") == 0){
368 recibe_experimento(rx_data,filename1);
369 carga_experimento(filename1);
370 cambia_apuntamiento("0");
371 tx_data = (char*)malloc(3);
372 tx_data = "OK";
373 }
374 else if(strcmp(cmd,"CHGB") == 0){
375 cambia_apuntamiento(rx_data);
376 tx_data = (char*)malloc(3);
377 tx_data = "OK";
378
379 }
380
381 else if(strcmp(cmd,"ANST") == 0){
382 /*n = chequeo_sistema(filename2,rx_data);
383 printf("%i\n",n);
384 tx_data = File2buffer(filename2, n);*/
385 tx_data = chequeo_sistema(filename2,rx_data);
386 printf("%s\n",tx_data);
312 printf("%s\n",tx_data);
387 }
313 }
388 else{
314 else{
@@ -391,13 +317,17
391 ERROR("procesa_peticion: comando no reconocido");
317 ERROR("procesa_peticion: comando no reconocido");
392 }
318 }
393
319
320 tx_len = malloc(7);
321 int number = strlen(tx_data);
322 intToStr(number, tx_len );
323
394 strcpy(tx_buffer,header); //3
324 strcpy(tx_buffer,header); //3
395 strcat(tx_buffer,TypeOfInstrument); //3
325 strcat(tx_buffer,TypeOfInstrument); //3
396 strcat(tx_buffer,parameters.ID); //7
326 strcat(tx_buffer,parameters.ID); //7
397 strcat(tx_buffer,iDSource); //7
327 strcat(tx_buffer,iDSource); //7
398 strcat(tx_buffer,"000033"); //6
328 strcat(tx_buffer,tx_len); //6
399 strcat(tx_buffer,cmd); //4
329 strcat(tx_buffer,cmd); //4
400 strcat(tx_buffer,tx_data); //2
330 strcat(tx_buffer,tx_data); //?
401 strcat(tx_buffer,crc); //1
331 strcat(tx_buffer,crc); //1
402
332
403 }
333 }
@@ -407,7 +337,6
407 /*
337 /*
408 * Esta función genera el archivo de experimento a partir de la trama TCP recibida
338 * Esta función genera el archivo de experimento a partir de la trama TCP recibida
409 */
339 */
410
411 void recibe_experimento(char *p_data, char filename[]){
340 void recibe_experimento(char *p_data, char filename[]){
412 FILE *fd;
341 FILE *fd;
413 int i = 0;
342 int i = 0;
@@ -516,71 +445,6
516
445
517 return 1;
446 return 1;
518
447
519 }
520
521 /*
522 * Esta funcion lee "n" veces el estado del APUNTE actual y lo guarda en el
523 * archivo Verificacion.
524 */
525
526 int chequeo_sistema2(char *filename, char *numero_muestras){
527
528 char valor[7];
529 int i,cnt;
530 unsigned int entradac= 0;
531 FILE *fd;
532 fd=fopen(filename,"w");
533 fprintf(fd,"%s\n","Verificacion");
534 fprintf(fd,"%s\n",parameters.ID);
535 fprintf(fd,"%s\n","------------");
536 cnt=0;
537 do
538 {
539 //Inicializando arreglo
540 for(i=0;i<6;i++)
541 valor[i]='0';
542
543 valor[6]='\0';
544
545 //Lectura de puerto
546 entradac= pio_in(piob,maskb_in);
547
548 //Dandole formato al dato
549 if (!(entradac & bit_up_2))
550 valor[0] = '1';
551 if (!(entradac & bit_up_1))
552 valor[1] = '1';
553 if (!(entradac & bit_up_0))
554 valor[2] = '1';
555 if (!(entradac & bit_dow_2))
556 valor[3] = '1';
557 if (!(entradac & bit_dow_1))
558 valor[4] = '1';
559 if (!(entradac & bit_dow_0))
560 valor[5] = '1';
561
562 //Escribiendo en archivo
563 fprintf(fd,"%s\n",valor);
564 cnt=cnt+1;
565 usleep(1*1000*1000);
566
567 }while(cnt < atoi(numero_muestras));
568
569 fclose(fd);
570
571 return 7*atoi(numero_muestras) + 26 + 4 + 1; //incluye eof
572 }
573
574 char* File2buffer(char *filename, int n){
575 FILE *fd;
576 char* tx_data = (char *)malloc(n);
577 fd = fopen(filename,"r");
578 fread(tx_data, 1, n-1, fd);
579 fclose(fd);
580 tx_data = tx_data + n - 1;
581 *tx_data = '\0';
582 tx_data = tx_data - n + 1;
583 return tx_data;
584 }
448 }
585
449
586 /*
450 /*
@@ -635,6 +499,10
635 strcpy(all_pages, page0);
499 strcpy(all_pages, page0);
636 return all_pages;
500 return all_pages;
637 }
501 }
502
503 /*
504 *
505 */
638 cmp inicializa_modulo(cmp p){
506 cmp inicializa_modulo(cmp p){
639 FILE *fd = fopen("configuration.txt","r");
507 FILE *fd = fopen("configuration.txt","r");
640 fgets(p.ID,20,fd);
508 fgets(p.ID,20,fd);
@@ -648,3 +516,46
648 fclose(fd);
516 fclose(fd);
649 return p;
517 return p;
650 }
518 }
519
520 /*
521 *
522 */
523 void intToStr( int number, char* str )
524
525 {
526 int index = 0;
527
528 while( number > 0 )
529 {
530 int digit = number%10;
531 str[index++] = digit + '0';
532 number /= 10;
533 }
534 str[index] = '\0';
535 //Adding zero to the left
536 int n= strlen(str);
537 if (n == 1) {
538 strcat(str,"00000");
539 index = index + 5;
540 }else if(n == 2){
541 strcat(str,"0000");
542 index = index + 4;
543 }else if(n == 3){
544 strcat(str,"000");
545 index = index + 3;
546 }else if(n == 4){
547 strcat(str,"00");
548 index = index + 2;
549 }else if(n == 5){
550 strcat(str,"0");
551 index = index + 1;
552 }
553 //Now reverse the numbers in the string.
554 int position;
555 for( position = 0; position <= (index-1)/2; ++position )
556 {
557 char tmp = str[position];
558 str[position] = str[(index-1)-position];
559 str[(index-1)-position] = tmp;
560 }
561 }
General Comments 0
You need to be logged in to leave comments. Login now