##// END OF EJS Templates
tcp server with malloc bug corrected.
imanay -
r107:108
parent child
Show More
@@ -1,642 +1,642
1 1 /*
2 2 * Servidor.c
3 3 *
4 4 * Created on: Nov 3, 2009
5 5 * Author: Jose Francisco Quenta
6 6 *
7 7 * Se implementa:
8 8 * -Carga en memoria los apuntes contenidos en un archivo de experimentos: apunte0 -> GPIO
9 9 * -Cambio de apunte.
10 10 * -Lectura del estado actual del apunte y grabado del mismo en un archivo
11 11 */
12 12
13 13 #include <stdio.h>
14 14 #include <stdlib.h>
15 15 #include <string.h>
16 16 #include <unistd.h>
17 17 #include <errno.h>
18 18
19 19 #include <sys/types.h>
20 20 #include <sys/socket.h>
21 21 #include <netinet/in.h>
22 22 #include <arpa/inet.h>
23 23 #include <netdb.h>
24 24
25 25 #include "./Librerias/AT91gpio_Funciones.h"
26 26 #include "./Librerias/Mensajes.h"
27 27
28 28 #define PUERTO_SERVIDOR 5500
29 29 #define TAM_BUFFER 1024
30 30
31 31 #define maskc_out PC30+PC28+PC26+PC24+PC22+PC20 //MSB-UP-LSB MSB-DOWN-LSB //APUNTE
32 32
33 33 #define maskb_in PB16+PB18+PB20+PB30+PB24+PB22 //MSB-UP-LSB MSB-DOWN-LSB //VERIFICACION
34 34
35 35 #define bit_up_2 0x00010000 //Mascara de cada bit a revisar: bit_up_2 es MSB
36 36 #define bit_up_1 0x00040000
37 37 #define bit_up_0 0x00100000
38 38 #define bit_dow_2 0x40000000
39 39 #define bit_dow_1 0x01000000
40 40 #define bit_dow_0 0x00400000
41 41
42 42 #define MyID 11
43 43 #define MAXPENDING 5 /* Maximum outstanding connection requests */
44 44
45 45 char *buff_experimento= NULL;
46 46
47 47 AT91S_PIO *pioc;
48 48 AT91S_PIO *piob;
49 49
50 50 struct control_module_parameters {
51 51 char ID[20];
52 52 char param2[20];
53 53 char param3[20];
54 54 char param4[20];
55 55 };
56 56
57 57 typedef struct control_module_parameters cmp;
58 58
59 59
60 60 char *header = NULL;
61 61 char *TypeOfInstrument = NULL;
62 62 char *iDSource = NULL;
63 63 char *iDDestino = NULL;
64 64 char *len = NULL;
65 65 char *cmd = NULL;
66 66 char *rx_data = NULL;
67 67 char *crc = NULL;
68 68
69 69 cmp parameters;
70 70 /*
71 71 * Zona de declaracion de cabeceras.
72 72 */
73 73 cmp inicializa_modulo(cmp p);
74 74 int inicializa_ethernet();
75 75 int rxData(int, char*);
76 76 void txData(int, char*);
77 77 void inicializa_gpio();
78 78 void procesa_peticion(char *rx_buffer, char *tx_buffer);
79 79 int cambia_apuntamiento(char *puntero_char);
80 80 int carga_experimento(char *nombre_archivo);
81 81 char *chequeo_sistema(char *filename, char *numero_muestras);
82 82 int chequeo_sistema2(char *filename, char *numero_muestras);
83 83 void recibe_experimento(char *data, char filename[]);
84 84 char* File2buffer(char *filename2, int n);
85 85 void SplitFrame(char *frame);
86 86 void procesa_peticion2(char *rx_buffer, char *tx_buffer);
87 87
88 88 /*
89 89 *
90 90 */
91 91 int main(){
92 92
93 93 int servSocket;
94 94 int clntSocket;
95 95
96 96
97 97 char *rx_buffer = (char *) malloc(TAM_BUFFER);
98 98 char *tx_buffer = (char *) malloc(TAM_BUFFER);
99 99 /* Inicializa parametros del modulo*/
100 100 parameters = inicializa_modulo(parameters);
101 101 printf("%s\n%s\n%s\n%s\n",parameters.ID, parameters.param2, parameters.param3, parameters.param4);
102 102 /* Inicializa red*/
103 103 servSocket = inicializa_ethernet();
104 104 /* Inicializamos el puerto GPIO del sistema embebido GSBC-9260S */
105 105 inicializa_gpio();
106 106
107 107 while(1){
108 108 // RecepciΓ³n TCP de peticiΓ³n
109 109 clntSocket = rxData(servSocket, rx_buffer);
110 110 //testpoint
111 111 printf("rx:%s\n",rx_buffer);
112 112 // Procesamiento de la peticiΓ³n
113 113 procesa_peticion(rx_buffer, tx_buffer);
114 114 //testpoint
115 115 printf("tx:%s\n",tx_buffer);
116 116 // Respuesta del modulo de control
117 117 txData(clntSocket, tx_buffer);
118 118
119 119 }
120 120 }
121 121
122 122
123 123 int inicializa_ethernet(){
124 124
125 125 struct sockaddr_in inf_servidor;
126 126
127 127 int servSocket;
128 128
129 129 int resultado;
130 130
131 131 /* Haciendo la estructura local*/
132 132 memset(&inf_servidor, 0, sizeof(inf_servidor));
133 133 inf_servidor.sin_family= AF_INET;
134 134 inf_servidor.sin_port= htons(PUERTO_SERVIDOR);
135 135 inf_servidor.sin_addr.s_addr= INADDR_ANY;
136 136
137 137 /* Se establece el socket */
138 138 servSocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
139 139 if (servSocket == -1){
140 140 ERROR_FATAL("No se establecio correctamente el socket: socket()\n");
141 141 exit(-1);
142 142 }
143 143
144 144 /* Se asocia el socket a un puerto y una IP */
145 145 resultado = bind(servSocket,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor));
146 146 if (resultado== -1){
147 147 ERROR_FATAL("No se establecio correctamente el socket: bind()\n");
148 148 exit(-1);
149 149 }
150 150
151 151 if (listen(servSocket, MAXPENDING) < 0){
152 152 printf("listen() failed\n");
153 153 exit(-1);
154 154 }
155 155
156 156 return servSocket;
157 157
158 158 }
159 159
160 160 int rxData(int servSocket, char* rx_buffer){
161 161
162 162 int clntSocket;
163 163 struct sockaddr_in inf_cliente;
164 164 int numbytes_recibidos;
165 165 unsigned int inf_client_Len;
166 166
167 167 printf("\nEsperando solicitud de cliente...\n");
168 168
169 169 /* Set the size of the in-out parameter */
170 170 inf_client_Len = sizeof(inf_cliente);
171 171 /* Se espera hasta que un cliente se conecte */
172 172 if ((clntSocket = accept(servSocket, (struct sockaddr *) &inf_cliente,
173 173 &inf_client_Len)) < 0)
174 174 printf("accept() failed\n");
175 175
176 176 if ((numbytes_recibidos = recv(clntSocket, rx_buffer, TAM_BUFFER, 0)) < 0)
177 177 printf("recv() failed\n");
178 178
179 179 /* Se procede a procesar los datos recibidos */
180 180 rx_buffer[numbytes_recibidos]= '\0';
181 181
182 182 return clntSocket;
183 183 }
184 184
185 185 void txData(int clntSocket, char* data){
186 186
187 187 /* Echo message back to client */
188 188 if (send(clntSocket, data, strlen(data), 0) != strlen(data))
189 189 printf("send() failed\n");
190 190
191 191 close(clntSocket); /* Close client socket */
192 192 }
193 193 /*
194 194 * Esta funcion incializa el puerto GPIO
195 195 */
196 196 void inicializa_gpio(){
197 197
198 198 // Configuracion de los pines de APUNTE
199 199 pioc = pio_map(PIOC_BASE);
200 200 pio_enable(pioc, maskc_out);
201 201 pio_disable_irq(pioc, maskc_out);
202 202 pio_disable_multiple_driver(pioc, maskc_out);
203 203 pio_disable_pull_ups(pioc, maskc_out);
204 204 pio_synchronous_data_output(pioc, maskc_out);
205 205 pio_output_enable(pioc, maskc_out);
206 206
207 207 // Configuracion de los pines de VERIFICACION
208 208 piob = pio_map(PIOB_BASE);
209 209 pio_enable(piob, maskb_in);
210 210 pio_disable_irq(piob, maskb_in);
211 211 pio_disable_multiple_driver(piob, maskb_in);
212 212 pio_disable_pull_ups(piob, maskb_in);
213 213 pio_input_enable(piob, maskb_in);
214 214
215 215 }
216 216
217 217 /*
218 218 * Esta funcion procesa el mensaje de peticion y genera respuesta
219 219 */
220 220 void procesa_peticion2(char *rx_buffer, char *tx_buffer){
221 221 int n = 0;
222 222 char filename1[50];
223 223 char filename2[] = "verificacion.txt";
224 224 char *tx_data = NULL;
225 225 char *header = strtok(rx_buffer, ":");
226 226 char *TypeOfInstrument = strtok(NULL, ":");
227 227 char *iDSource = strtok(NULL, ":");
228 228 char *iDDestino = strtok(NULL, ":");
229 229 char *len = strtok(NULL, ":");
230 230 char *cmd = strtok(NULL, ":");
231 231 char *rx_data = strtok(NULL, ":");
232 232 char *crc = strtok(NULL, ":");
233 233
234 234 if ((cmd == NULL) || (rx_data == NULL)){
235 235 ERROR("procesarPeticion: formato de mensaje incorrecto");
236 236 }
237 237 else{
238 238 if(strcmp(cmd,"SNDF") == 0){
239 239 recibe_experimento(rx_data,filename1);
240 240 carga_experimento(filename1);
241 241 cambia_apuntamiento("0");
242 242 tx_data = (char*)malloc(3);
243 243 tx_data = "OK";
244 244 }
245 245 else if(strcmp(cmd,"CHGB") == 0){
246 246 cambia_apuntamiento(rx_data);
247 247 tx_data = (char*)malloc(3);
248 248 tx_data = "OK";
249 249
250 250 }
251 251
252 252 else if(strcmp(cmd,"ANST") == 0){
253 253 n = chequeo_sistema2(filename2,rx_data);
254 254 printf("%i\n",n);
255 255 tx_data = File2buffer(filename2, n);
256 256 }
257 257 else{
258 258 tx_data = (char*)malloc(6);
259 259 tx_data = "Error";
260 260 ERROR("procesa_peticion: comando no reconocido");
261 261 }
262 262
263 263 strcpy(tx_buffer,header);
264 264 strcat(tx_buffer,":");
265 265 strcat(tx_buffer,TypeOfInstrument);
266 266 strcat(tx_buffer,":");
267 267 strcat(tx_buffer,iDDestino);
268 268 strcat(tx_buffer,":");
269 269 strcat(tx_buffer,iDSource);
270 270 strcat(tx_buffer,":");
271 271 strcat(tx_buffer,len);
272 272 strcat(tx_buffer,":");
273 273 strcat(tx_buffer,cmd);
274 274 strcat(tx_buffer,":");
275 275 strcat(tx_buffer,tx_data);
276 276 strcat(tx_buffer,":");
277 277 strcat(tx_buffer,crc);
278 278 strcat(tx_buffer,":");
279 279 strcat(tx_buffer,"quit");
280 280 }
281 281 }
282 282
283 283 void SplitFrame(char *frame){
284 284 header = malloc(4);
285 285 *header = *frame;
286 286 *(header + 1) = *(frame + 1);
287 287 *(header + 2) = *(frame + 2);
288 288 *(header + 3) = '\0';
289 289 TypeOfInstrument = malloc(4);
290 290 *TypeOfInstrument = *(frame + 3);
291 291 *(TypeOfInstrument + 1) = *(frame + 4);
292 292 *(TypeOfInstrument + 2) = *(frame + 5);
293 293 *(TypeOfInstrument + 3) = '\0';
294 294 iDSource = malloc(8);
295 295 *iDSource = *(frame + 6);
296 296 *(iDSource + 1) = *(frame + 7);
297 297 *(iDSource + 2) = *(frame + 8);
298 298 *(iDSource + 3) = *(frame + 9);
299 299 *(iDSource + 4) = *(frame + 10);
300 300 *(iDSource + 5) = *(frame + 11);
301 301 *(iDSource + 6) = *(frame + 12);
302 302 *(iDSource + 7) = '\0';
303 303 iDDestino = malloc(8);
304 304 *iDDestino = *(frame + 13);
305 305 *(iDDestino + 1) = *(frame + 14);
306 306 *(iDDestino + 2) = *(frame + 15);
307 307 *(iDDestino + 3) = *(frame + 16);
308 308 *(iDDestino + 4) = *(frame + 17);
309 309 *(iDDestino + 5) = *(frame + 18);
310 310 *(iDDestino + 6) = *(frame + 19);
311 311 *(iDDestino + 7) = '\0';
312 312 len = malloc(7);
313 313 *len = *(frame + 20);
314 314 *(len + 1) = *(frame + 21);
315 315 *(len + 2) = *(frame + 22);
316 316 *(len + 3) = *(frame + 23);
317 317 *(len + 4) = *(frame + 24);
318 318 *(len + 5) = *(frame + 25);
319 319 *(len + 6) = '\0';
320 320 cmd = malloc(5);
321 321 *cmd = *(frame + 26);
322 322 *(cmd + 1) = *(frame + 27);
323 323 *(cmd + 2) = *(frame + 28);
324 324 *(cmd + 3) = *(frame + 29);
325 325 *(cmd + 4) = '\0';
326 326
327 327 int l = atoi(len) - 31; //Resto del tamaΓ±o total de la trama los 31 bytes fijos
328 rx_data = malloc(l);
328 rx_data = malloc(l + 1);
329 329 int i;
330 330 for (i = 30; i < 30 + l; i++)
331 331 *(rx_data + (i-30)) = *(frame + i);
332 332 *(rx_data + l) = '\0';
333 333 crc = malloc(2);
334 334 *crc = *(frame + 30 + l);
335 335 *(crc + 1) = '\0';
336 336 }
337 337
338 338 void procesa_peticion(char *rx_buffer, char *tx_buffer){
339 339 // int n = 0;
340 340 char filename1[50];
341 341 char filename2[] = "verificacion.txt";
342 342 char *tx_data = NULL;
343 343 SplitFrame(rx_buffer);
344 344 printf("Split\n");
345 345 printf("%s\n",header);
346 346 printf("%s\n",TypeOfInstrument);
347 347 printf("%s\n",iDSource);
348 348 printf("%s\n",iDDestino);
349 349 printf("%s\n",len);
350 350 printf("%s\n",cmd);
351 351 printf("%s\n",rx_data);
352 352 printf("%s\n",crc);
353 353 printf("Split\n");
354 354
355 355 if ((cmd == NULL) || (rx_data == NULL)){
356 356 ERROR("procesarPeticion: formato de mensaje incorrecto");
357 357 }
358 358 else{
359 359 if(strcmp(cmd,"SNDF") == 0){
360 360 recibe_experimento(rx_data,filename1);
361 361 carga_experimento(filename1);
362 362 cambia_apuntamiento("0");
363 363 tx_data = (char*)malloc(3);
364 364 tx_data = "OK";
365 365 }
366 366 else if(strcmp(cmd,"CHGB") == 0){
367 367 cambia_apuntamiento(rx_data);
368 368 tx_data = (char*)malloc(3);
369 369 tx_data = "OK";
370 370
371 371 }
372 372
373 373 else if(strcmp(cmd,"ANST") == 0){
374 374 /*n = chequeo_sistema(filename2,rx_data);
375 375 printf("%i\n",n);
376 376 tx_data = File2buffer(filename2, n);*/
377 377 tx_data = chequeo_sistema(filename2,rx_data);
378 378 printf("%s\n",tx_data);
379 379 }
380 380 else{
381 381 tx_data = (char*)malloc(6);
382 382 tx_data = "Error";
383 383 ERROR("procesa_peticion: comando no reconocido");
384 384 }
385 385
386 386 strcpy(tx_buffer,header); //3
387 387 strcat(tx_buffer,TypeOfInstrument); //3
388 388 strcat(tx_buffer,parameters.ID); //7
389 389 strcat(tx_buffer,iDSource); //7
390 390 strcat(tx_buffer,"00032"); //5
391 391 strcat(tx_buffer,cmd); //4
392 392 strcat(tx_buffer,tx_data); //2
393 393 strcat(tx_buffer,crc); //1
394 394
395 395 }
396 396
397 397 }
398 398
399 399 /*
400 400 * Esta funciΓ³n genera el archivo de experimento a partir de la trama TCP recibida
401 401 */
402 402
403 403 void recibe_experimento(char *p_data, char filename[]){
404 404 FILE *fd;
405 405 int i = 0;
406 406
407 407 while (*p_data != '\n'){
408 408 filename[i] = *p_data;
409 409 i++;
410 410 p_data++;
411 411 }
412 412 filename[i] = '\0';
413 413 p_data = p_data - i;
414 414 fd = fopen(filename,"w");
415 415 fprintf(fd, p_data);
416 416 fclose(fd);
417 417 }
418 418
419 419 /*
420 420 * Esta funcion carga un archivo en un buffer que esta ubicado en memoria, luego
421 421 * este buffer es usado en la funcion "cambia_apuntamiento" para obtener el dato
422 422 * que sera usado en el cambio de apuntamiento.
423 423 */
424 424 int carga_experimento(char *nombre_archivo){
425 425
426 426 FILE *Archivo_Fd;
427 427
428 428 char *cadena = (char *) malloc(25);
429 429
430 430 int longitud_cadena;
431 431 int num_bytes= 0;
432 432 int num_filas= 0;
433 433
434 434 Archivo_Fd = fopen(nombre_archivo,"r"); // Se procede a abrir el archivo, segun la ruta especificada
435 435 if(!Archivo_Fd){
436 436 ERROR("carga_archivo: No se pudo abrir el archivo!!! --> fopen()\n");
437 437 return -1;
438 438 }else{
439 439
440 440 while(!feof(Archivo_Fd)){ // Se procede a calcular la longitud del archivo para separar memoria
441 441 fgets(cadena,20,Archivo_Fd);
442 442 longitud_cadena= strlen(cadena);
443 443 cadena[longitud_cadena-1] = '\0';
444 444 num_bytes = num_bytes + longitud_cadena;
445 445 num_filas++;
446 446 }
447 447
448 448 rewind(Archivo_Fd); // Se reinicia el puntero del archivo
449 449
450 450 char *buffer_temporal = (char *) malloc(num_bytes+1); // Se separa espacio de memoria segun
451 451 // la longitud del archivo
452 452 fread(buffer_temporal, sizeof(char), num_bytes, Archivo_Fd);
453 453
454 454 char *puntero= strstr(buffer_temporal,".ab1"); // Se procede a eliminar la cabecera del archivo
455 455 puntero= puntero + 12;
456 456
457 457 buff_experimento = (char *) malloc(7*(num_filas-3)); // num_bytes_fila*(num_filas-3);
458 458 strncpy(buff_experimento,puntero,7*(num_filas-3)); // Se carga en memoria la informacion del archivo
459 459
460 460 fclose(Archivo_Fd);
461 461
462 462 return 1;
463 463 }
464 464 }
465 465
466 466 /*
467 467 * Esta funcion recibe un numero en formato char, el dato se transforma a su equivalente en
468 468 * un numero entero, que sera usado para sacar un dato del buffer "buff_experimento", esta
469 469 * dato es el valor que se enviara al sistema de conmutacion RF para el cambio de apunte a
470 470 * traves del puerto GPIO.
471 471 */
472 472 int cambia_apuntamiento(char *puntero_char){
473 473
474 474 /*MSB-UP-LSB MSB-DOWN-LSB*/
475 475 int desplazamiento[6]={30,28,26,24,22,20}; // Defino los dezplazamientos que se aplicara
476 476 // al dato que ingresa para formar el nΓΊmero
477 477 // entero que se le pasara al puerto GPIO
478 478 // Estos nΓΊmeros son los pines del puerto GPIO
479 479 // que se estan usando para el control
480 480
481 481 int puntero= atoi(puntero_char); // Se convierte a entero la direccion del puntero
482 482
483 483 int base= 7*puntero; // base= cantidad_bytes del dato x puntero
484 484 // cantidad de bytes es el numero de bytes que
485 485 printf("%s\n",puntero_char); // contiene cada dato, para este caso es 7
486 486 // porque es 6 bits de datos + 1 bit del cambio
487 487 // de linea.
488 488 char valor_char;
489 489 unsigned long valor;
490 490 unsigned long acumulado_ceros=0;
491 491 unsigned long acumulado_unos=0;
492 492
493 493 int offset; // Defino offset para el desplazamiento a traves
494 494 for(offset=0;offset<6;offset++){ // de cada dato que se obtiene del "buff_experimento"
495 495
496 496 valor_char= buff_experimento[base+offset]; // Obtengo el dato
497 497
498 498 if (valor_char == '0'){ // Obtengo el nΓΊmero acumulado segun sea un cero o un uno
499 499 valor= 0;
500 500 acumulado_ceros= acumulado_ceros + (1 << desplazamiento[offset]);
501 501 }else{
502 502 valor= 1;
503 503 acumulado_unos= acumulado_unos + (1 << desplazamiento[offset]);
504 504 }
505 505 }
506 506 pio_out(pioc, maskc_out, acumulado_unos, 1);
507 507 pio_out(pioc, maskc_out, acumulado_ceros, 0);
508 508
509 509 return 1;
510 510
511 511 }
512 512
513 513 /*
514 514 * Esta funcion lee "n" veces el estado del APUNTE actual y lo guarda en el
515 515 * archivo Verificacion.
516 516 */
517 517
518 518 int chequeo_sistema2(char *filename, char *numero_muestras){
519 519
520 520 char valor[7];
521 521 int i,cnt;
522 522 unsigned int entradac= 0;
523 523 FILE *fd;
524 524 fd=fopen(filename,"w");
525 525 fprintf(fd,"%s\n","Verificacion");
526 526 fprintf(fd,"%s\n",parameters.ID);
527 527 fprintf(fd,"%s\n","------------");
528 528 cnt=0;
529 529 do
530 530 {
531 531 //Inicializando arreglo
532 532 for(i=0;i<6;i++)
533 533 valor[i]='0';
534 534
535 535 valor[6]='\0';
536 536
537 537 //Lectura de puerto
538 538 entradac= pio_in(piob,maskb_in);
539 539
540 540 //Dandole formato al dato
541 541 if (!(entradac & bit_up_2))
542 542 valor[0] = '1';
543 543 if (!(entradac & bit_up_1))
544 544 valor[1] = '1';
545 545 if (!(entradac & bit_up_0))
546 546 valor[2] = '1';
547 547 if (!(entradac & bit_dow_2))
548 548 valor[3] = '1';
549 549 if (!(entradac & bit_dow_1))
550 550 valor[4] = '1';
551 551 if (!(entradac & bit_dow_0))
552 552 valor[5] = '1';
553 553
554 554 //Escribiendo en archivo
555 555 fprintf(fd,"%s\n",valor);
556 556 cnt=cnt+1;
557 557 usleep(1*1000*1000);
558 558
559 559 }while(cnt < atoi(numero_muestras));
560 560
561 561 fclose(fd);
562 562
563 563 return 7*atoi(numero_muestras) + 26 + 4 + 1; //incluye eof
564 564 }
565 565
566 566 char* File2buffer(char *filename, int n){
567 567 FILE *fd;
568 568 char* tx_data = (char *)malloc(n);
569 569 fd = fopen(filename,"r");
570 570 fread(tx_data, 1, n-1, fd);
571 571 fclose(fd);
572 572 tx_data = tx_data + n - 1;
573 573 *tx_data = '\0';
574 574 tx_data = tx_data - n + 1;
575 575 return tx_data;
576 576 }
577 577
578 578 /*
579 579 * Esta funcion lee "n" veces el estado del APUNTE actual y reporta
580 580 * una cadena de Verificacion.
581 581 */
582 582 char* chequeo_sistema(char *filename, char *numero_muestras){
583 583
584 584 int i;
585 585 int cnt = 0;
586 586 unsigned int entradac= 0;
587 587
588 588 char page0[250];
589 589
590 590 strcpy(page0,"Verificacion\n");
591 591 strcat(page0,parameters.ID);
592 592 strcat(page0,"\n------------\n");
593 593
594 594 char page1[8];
595 595
596 596 do{
597 597 //Inicializando arreglo
598 598 for(i=0;i<6;i++)
599 599 page1[i]='0';
600 600 page1[6] = '\n';
601 601 page1[7] = '\0';
602 602 //Lectura de puerto
603 603 entradac= pio_in(piob,maskb_in);
604 604 //Dandole formato al dato
605 605 if (!(entradac & bit_up_2))
606 606 page1[0] = '1';
607 607 if (!(entradac & bit_up_1))
608 608 page1[1] = '1';
609 609 if (!(entradac & bit_up_0))
610 610 page1[2] = '1';
611 611 if (!(entradac & bit_dow_2))
612 612 page1[3] = '1';
613 613 if (!(entradac & bit_dow_1))
614 614 page1[4] = '1';
615 615 if (!(entradac & bit_dow_0))
616 616 page1[5] = '1';
617 617
618 618 strcat(page0, page1);
619 619 cnt=cnt+1;
620 620 usleep(1*1000*1000);
621 621
622 622 }while(cnt < atoi(numero_muestras));
623 623
624 624 page0[strlen(page0)] = '\0';
625 625
626 626 char *all_pages = malloc(strlen(page0)+1);
627 627 strcpy(all_pages, page0);
628 628 return all_pages;
629 629 }
630 630 cmp inicializa_modulo(cmp p){
631 631 FILE *fd = fopen("configuration.txt","r");
632 632 fgets(p.ID,20,fd);
633 633 p.ID[7]='\0';
634 634 fgets(p.param2,20,fd);
635 635 p.param2[10]='\0';
636 636 fgets(p.param3,20,fd);
637 637 p.param3[10]='\0';
638 638 fgets(p.param4,20,fd);
639 639 p.param4[10]='\0';
640 640 fclose(fd);
641 641 return p;
642 642 }
General Comments 0
You need to be logged in to leave comments. Login now