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