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