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