@@ -1,460 +1,459 | |||||
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 | cmp parameters; | ||
60 | /* |
|
60 | /* | |
61 | * Zona de declaracion de cabeceras. |
|
61 | * Zona de declaracion de cabeceras. | |
62 | */ |
|
62 | */ | |
63 | cmp inicializa_modulo(cmp p); |
|
63 | cmp inicializa_modulo(cmp p); | |
64 | int inicializa_ethernet(); |
|
64 | int inicializa_ethernet(); | |
65 | int rxData(int, char*); |
|
65 | int rxData(int, char*); | |
66 | void txData(int, char*); |
|
66 | void txData(int, char*); | |
67 | void inicializa_gpio(); |
|
67 | void inicializa_gpio(); | |
68 | void procesa_peticion(char *rx_buffer, char *tx_buffer); |
|
68 | void procesa_peticion(char *rx_buffer, char *tx_buffer); | |
69 | int cambia_apuntamiento(char *puntero_char); |
|
69 | int cambia_apuntamiento(char *puntero_char); | |
70 | int carga_experimento(char *nombre_archivo); |
|
70 | int carga_experimento(char *nombre_archivo); | |
71 | int chequeo_sistema(char *filename, char *numero_muestras); |
|
71 | int chequeo_sistema(char *filename, char *numero_muestras); | |
72 | void recibe_experimento(char *data, char filename[]); |
|
72 | void recibe_experimento(char *data, char filename[]); | |
73 | char* File2buffer(char *filename2, int n); |
|
73 | char* File2buffer(char *filename2, int n); | |
74 |
|
74 | |||
75 | /* |
|
75 | /* | |
76 | * |
|
76 | * | |
77 | */ |
|
77 | */ | |
78 | int main(){ |
|
78 | int main(){ | |
79 |
|
79 | |||
80 | int servSocket; |
|
80 | int servSocket; | |
81 | int clntSocket; |
|
81 | int clntSocket; | |
82 | cmp parameters; |
|
82 | ||
83 |
|
83 | |||
84 | char *rx_buffer = (char *) malloc(TAM_BUFFER); |
|
84 | char *rx_buffer = (char *) malloc(TAM_BUFFER); | |
85 | char *tx_buffer = (char *) malloc(TAM_BUFFER); |
|
85 | char *tx_buffer = (char *) malloc(TAM_BUFFER); | |
86 | /* Inicializa parametros del modulo*/ |
|
86 | /* Inicializa parametros del modulo*/ | |
87 | parameters = inicializa_modulo(parameters); |
|
87 | parameters = inicializa_modulo(parameters); | |
88 | printf("%s%s%s%s",parameters.ID, parameters.param2, parameters.param3, parameters.param4); |
|
88 | printf("%s%s%s%s",parameters.ID, parameters.param2, parameters.param3, parameters.param4); | |
89 | /* Inicializa red*/ |
|
89 | /* Inicializa red*/ | |
90 | servSocket = inicializa_ethernet(); |
|
90 | servSocket = inicializa_ethernet(); | |
91 | /* Inicializamos el puerto GPIO del sistema embebido GSBC-9260S */ |
|
91 | /* Inicializamos el puerto GPIO del sistema embebido GSBC-9260S */ | |
92 | inicializa_gpio(); |
|
92 | inicializa_gpio(); | |
93 |
|
93 | |||
94 | while(1){ |
|
94 | while(1){ | |
95 | // Recepción TCP de petición |
|
95 | // Recepción TCP de petición | |
96 | clntSocket = rxData(servSocket, rx_buffer); |
|
96 | clntSocket = rxData(servSocket, rx_buffer); | |
97 | //testpoint |
|
97 | //testpoint | |
98 | printf("rx:%s\n",rx_buffer); |
|
98 | printf("rx:%s\n",rx_buffer); | |
99 | // Procesamiento de la petición |
|
99 | // Procesamiento de la petición | |
100 | procesa_peticion(rx_buffer, tx_buffer); |
|
100 | procesa_peticion(rx_buffer, tx_buffer); | |
101 | //testpoint |
|
101 | //testpoint | |
102 | printf("tx:%s\n",tx_buffer); |
|
102 | printf("tx:%s\n",tx_buffer); | |
103 | // Respuesta del modulo de control |
|
103 | // Respuesta del modulo de control | |
104 | txData(clntSocket, tx_buffer); |
|
104 | txData(clntSocket, tx_buffer); | |
105 |
|
105 | |||
106 | } |
|
106 | } | |
107 | } |
|
107 | } | |
108 |
|
108 | |||
109 |
|
109 | |||
110 | int inicializa_ethernet(){ |
|
110 | int inicializa_ethernet(){ | |
111 |
|
111 | |||
112 | struct sockaddr_in inf_servidor; |
|
112 | struct sockaddr_in inf_servidor; | |
113 |
|
113 | |||
114 | int servSocket; |
|
114 | int servSocket; | |
115 |
|
115 | |||
116 | int resultado; |
|
116 | int resultado; | |
117 |
|
117 | |||
118 | /* Haciendo la estructura local*/ |
|
118 | /* Haciendo la estructura local*/ | |
119 | memset(&inf_servidor, 0, sizeof(inf_servidor)); |
|
119 | memset(&inf_servidor, 0, sizeof(inf_servidor)); | |
120 | inf_servidor.sin_family= AF_INET; |
|
120 | inf_servidor.sin_family= AF_INET; | |
121 | inf_servidor.sin_port= htons(PUERTO_SERVIDOR); |
|
121 | inf_servidor.sin_port= htons(PUERTO_SERVIDOR); | |
122 | inf_servidor.sin_addr.s_addr= INADDR_ANY; |
|
122 | inf_servidor.sin_addr.s_addr= INADDR_ANY; | |
123 |
|
123 | |||
124 | /* Se establece el socket */ |
|
124 | /* Se establece el socket */ | |
125 | servSocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP); |
|
125 | servSocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP); | |
126 | if (servSocket == -1){ |
|
126 | if (servSocket == -1){ | |
127 | ERROR_FATAL("No se establecio correctamente el socket: socket()\n"); |
|
127 | ERROR_FATAL("No se establecio correctamente el socket: socket()\n"); | |
128 | exit(-1); |
|
128 | exit(-1); | |
129 | } |
|
129 | } | |
130 |
|
130 | |||
131 | /* Se asocia el socket a un puerto y una IP */ |
|
131 | /* Se asocia el socket a un puerto y una IP */ | |
132 | resultado = bind(servSocket,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor)); |
|
132 | resultado = bind(servSocket,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor)); | |
133 | if (resultado== -1){ |
|
133 | if (resultado== -1){ | |
134 | ERROR_FATAL("No se establecio correctamente el socket: bind()\n"); |
|
134 | ERROR_FATAL("No se establecio correctamente el socket: bind()\n"); | |
135 | exit(-1); |
|
135 | exit(-1); | |
136 | } |
|
136 | } | |
137 |
|
137 | |||
138 | if (listen(servSocket, MAXPENDING) < 0){ |
|
138 | if (listen(servSocket, MAXPENDING) < 0){ | |
139 | printf("listen() failed\n"); |
|
139 | printf("listen() failed\n"); | |
140 | exit(-1); |
|
140 | exit(-1); | |
141 | } |
|
141 | } | |
142 |
|
142 | |||
143 | return servSocket; |
|
143 | return servSocket; | |
144 |
|
144 | |||
145 | } |
|
145 | } | |
146 |
|
146 | |||
147 | int rxData(int servSocket, char* rx_buffer){ |
|
147 | int rxData(int servSocket, char* rx_buffer){ | |
148 |
|
148 | |||
149 | int clntSocket; |
|
149 | int clntSocket; | |
150 | struct sockaddr_in inf_cliente; |
|
150 | struct sockaddr_in inf_cliente; | |
151 | int numbytes_recibidos; |
|
151 | int numbytes_recibidos; | |
152 | unsigned int inf_client_Len; |
|
152 | unsigned int inf_client_Len; | |
153 |
|
153 | |||
154 | printf("\nEsperando solicitud de cliente...\n"); |
|
154 | printf("\nEsperando solicitud de cliente...\n"); | |
155 |
|
155 | |||
156 | /* Set the size of the in-out parameter */ |
|
156 | /* Set the size of the in-out parameter */ | |
157 | inf_client_Len = sizeof(inf_cliente); |
|
157 | inf_client_Len = sizeof(inf_cliente); | |
158 | /* Se espera hasta que un cliente se conecte */ |
|
158 | /* Se espera hasta que un cliente se conecte */ | |
159 | if ((clntSocket = accept(servSocket, (struct sockaddr *) &inf_cliente, |
|
159 | if ((clntSocket = accept(servSocket, (struct sockaddr *) &inf_cliente, | |
160 | &inf_client_Len)) < 0) |
|
160 | &inf_client_Len)) < 0) | |
161 | printf("accept() failed\n"); |
|
161 | printf("accept() failed\n"); | |
162 |
|
162 | |||
163 | if ((numbytes_recibidos = recv(clntSocket, rx_buffer, TAM_BUFFER, 0)) < 0) |
|
163 | if ((numbytes_recibidos = recv(clntSocket, rx_buffer, TAM_BUFFER, 0)) < 0) | |
164 | printf("recv() failed\n"); |
|
164 | printf("recv() failed\n"); | |
165 |
|
165 | |||
166 | /* Se procede a procesar los datos recibidos */ |
|
166 | /* Se procede a procesar los datos recibidos */ | |
167 | rx_buffer[numbytes_recibidos]= '\0'; |
|
167 | rx_buffer[numbytes_recibidos]= '\0'; | |
168 |
|
168 | |||
169 | return clntSocket; |
|
169 | return clntSocket; | |
170 | } |
|
170 | } | |
171 |
|
171 | |||
172 | void txData(int clntSocket, char* data){ |
|
172 | void txData(int clntSocket, char* data){ | |
173 |
|
173 | |||
174 | /* Echo message back to client */ |
|
174 | /* Echo message back to client */ | |
175 | if (send(clntSocket, data, strlen(data), 0) != strlen(data)) |
|
175 | if (send(clntSocket, data, strlen(data), 0) != strlen(data)) | |
176 | printf("send() failed\n"); |
|
176 | printf("send() failed\n"); | |
177 |
|
177 | |||
178 | close(clntSocket); /* Close client socket */ |
|
178 | close(clntSocket); /* Close client socket */ | |
179 | } |
|
179 | } | |
180 | /* |
|
180 | /* | |
181 | * Esta funcion incializa el puerto GPIO |
|
181 | * Esta funcion incializa el puerto GPIO | |
182 | */ |
|
182 | */ | |
183 | void inicializa_gpio(){ |
|
183 | void inicializa_gpio(){ | |
184 |
|
184 | |||
185 | // Configuracion de los pines de APUNTE |
|
185 | // Configuracion de los pines de APUNTE | |
186 | pioc = pio_map(PIOC_BASE); |
|
186 | pioc = pio_map(PIOC_BASE); | |
187 | pio_enable(pioc, maskc_out); |
|
187 | pio_enable(pioc, maskc_out); | |
188 | pio_disable_irq(pioc, maskc_out); |
|
188 | pio_disable_irq(pioc, maskc_out); | |
189 | pio_disable_multiple_driver(pioc, maskc_out); |
|
189 | pio_disable_multiple_driver(pioc, maskc_out); | |
190 | pio_disable_pull_ups(pioc, maskc_out); |
|
190 | pio_disable_pull_ups(pioc, maskc_out); | |
191 | pio_synchronous_data_output(pioc, maskc_out); |
|
191 | pio_synchronous_data_output(pioc, maskc_out); | |
192 | pio_output_enable(pioc, maskc_out); |
|
192 | pio_output_enable(pioc, maskc_out); | |
193 |
|
193 | |||
194 | // Configuracion de los pines de VERIFICACION |
|
194 | // Configuracion de los pines de VERIFICACION | |
195 | piob = pio_map(PIOB_BASE); |
|
195 | piob = pio_map(PIOB_BASE); | |
196 | pio_enable(piob, maskb_in); |
|
196 | pio_enable(piob, maskb_in); | |
197 | pio_disable_irq(piob, maskb_in); |
|
197 | pio_disable_irq(piob, maskb_in); | |
198 | pio_disable_multiple_driver(piob, maskb_in); |
|
198 | pio_disable_multiple_driver(piob, maskb_in); | |
199 | pio_disable_pull_ups(piob, maskb_in); |
|
199 | pio_disable_pull_ups(piob, maskb_in); | |
200 | pio_input_enable(piob, maskb_in); |
|
200 | pio_input_enable(piob, maskb_in); | |
201 |
|
201 | |||
202 | } |
|
202 | } | |
203 |
|
203 | |||
204 | /* |
|
204 | /* | |
205 | * Esta funcion procesa el mensaje de peticion y genera respuesta |
|
205 | * Esta funcion procesa el mensaje de peticion y genera respuesta | |
206 | */ |
|
206 | */ | |
207 | void procesa_peticion(char *rx_buffer, char *tx_buffer){ |
|
207 | void procesa_peticion(char *rx_buffer, char *tx_buffer){ | |
208 | int n = 0; |
|
208 | int n = 0; | |
209 | char filename1[50]; |
|
209 | char filename1[50]; | |
210 | char filename2[] = "verificacion.txt"; |
|
210 | char filename2[] = "verificacion.txt"; | |
211 | char *tx_data = NULL; |
|
211 | char *tx_data = NULL; | |
212 | char *header = strtok(rx_buffer, ":"); |
|
212 | char *header = strtok(rx_buffer, ":"); | |
213 | char *TypeOfInstrument = strtok(NULL, ":"); |
|
213 | char *TypeOfInstrument = strtok(NULL, ":"); | |
214 | char *iDSource = strtok(NULL, ":"); |
|
214 | char *iDSource = strtok(NULL, ":"); | |
215 | char *iDDestino = strtok(NULL, ":"); |
|
215 | char *iDDestino = strtok(NULL, ":"); | |
216 | char *len = strtok(NULL, ":"); |
|
216 | char *len = strtok(NULL, ":"); | |
217 | char *cmd = strtok(NULL, ":"); |
|
217 | char *cmd = strtok(NULL, ":"); | |
218 | char *rx_data = strtok(NULL, ":"); |
|
218 | char *rx_data = strtok(NULL, ":"); | |
219 | char *crc = strtok(NULL, ":"); |
|
219 | char *crc = strtok(NULL, ":"); | |
220 |
|
220 | |||
221 | if ((cmd == NULL) || (rx_data == NULL)){ |
|
221 | if ((cmd == NULL) || (rx_data == NULL)){ | |
222 | ERROR("procesarPeticion: formato de mensaje incorrecto"); |
|
222 | ERROR("procesarPeticion: formato de mensaje incorrecto"); | |
223 | } |
|
223 | } | |
224 | else{ |
|
224 | else{ | |
225 | if(strcmp(cmd,"SNDF") == 0){ |
|
225 | if(strcmp(cmd,"SNDF") == 0){ | |
226 | recibe_experimento(rx_data,filename1); |
|
226 | recibe_experimento(rx_data,filename1); | |
227 | carga_experimento(filename1); |
|
227 | carga_experimento(filename1); | |
228 | cambia_apuntamiento("0"); |
|
228 | cambia_apuntamiento("0"); | |
229 | tx_data = (char*)malloc(3); |
|
229 | tx_data = (char*)malloc(3); | |
230 | tx_data = "OK"; |
|
230 | tx_data = "OK"; | |
231 | } |
|
231 | } | |
232 | else if(strcmp(cmd,"CHGB") == 0){ |
|
232 | else if(strcmp(cmd,"CHGB") == 0){ | |
233 |
|
|
233 | cambia_apuntamiento(rx_data); | |
234 | printf("%s\n",rx_data); |
|
|||
235 | //cambia_apuntamiento(rx_data); |
|
|||
236 | cambia_apuntamiento("1"); |
|
|||
237 | tx_data = (char*)malloc(3); |
|
234 | tx_data = (char*)malloc(3); | |
238 | tx_data = "OK"; |
|
235 | tx_data = "OK"; | |
239 |
|
236 | |||
240 | } |
|
237 | } | |
241 |
|
238 | |||
242 | else if(strcmp(cmd,"ANST") == 0){ |
|
239 | else if(strcmp(cmd,"ANST") == 0){ | |
243 | n = chequeo_sistema(filename2,rx_data); |
|
240 | n = chequeo_sistema(filename2,rx_data); | |
244 | printf("%i\n",n); |
|
241 | printf("%i\n",n); | |
245 | tx_data = File2buffer(filename2, n); |
|
242 | tx_data = File2buffer(filename2, n); | |
246 | } |
|
243 | } | |
247 | else{ |
|
244 | else{ | |
248 | tx_data = (char*)malloc(6); |
|
245 | tx_data = (char*)malloc(6); | |
249 | tx_data = "Error"; |
|
246 | tx_data = "Error"; | |
250 | ERROR("procesa_peticion: comando no reconocido"); |
|
247 | ERROR("procesa_peticion: comando no reconocido"); | |
251 | } |
|
248 | } | |
252 |
|
249 | |||
253 | strcpy(tx_buffer,header); |
|
250 | strcpy(tx_buffer,header); | |
254 | strcat(tx_buffer,":"); |
|
251 | strcat(tx_buffer,":"); | |
255 | strcat(tx_buffer,TypeOfInstrument); |
|
252 | strcat(tx_buffer,TypeOfInstrument); | |
256 | strcat(tx_buffer,":"); |
|
253 | strcat(tx_buffer,":"); | |
257 | strcat(tx_buffer,iDDestino); |
|
254 | strcat(tx_buffer,iDDestino); | |
258 | strcat(tx_buffer,":"); |
|
255 | strcat(tx_buffer,":"); | |
259 | strcat(tx_buffer,iDSource); |
|
256 | strcat(tx_buffer,iDSource); | |
260 | strcat(tx_buffer,":"); |
|
257 | strcat(tx_buffer,":"); | |
261 | strcat(tx_buffer,len); |
|
258 | strcat(tx_buffer,len); | |
262 | strcat(tx_buffer,":"); |
|
259 | strcat(tx_buffer,":"); | |
263 | strcat(tx_buffer,cmd); |
|
260 | strcat(tx_buffer,cmd); | |
264 | strcat(tx_buffer,":"); |
|
261 | strcat(tx_buffer,":"); | |
265 | strcat(tx_buffer,tx_data); |
|
262 | strcat(tx_buffer,tx_data); | |
266 | strcat(tx_buffer,":"); |
|
263 | strcat(tx_buffer,":"); | |
267 | strcat(tx_buffer,crc); |
|
264 | strcat(tx_buffer,crc); | |
|
265 | strcat(tx_buffer,":"); | |||
|
266 | strcat(tx_buffer,"quit"); | |||
268 |
|
267 | |||
269 | } |
|
268 | } | |
270 |
|
269 | |||
271 | } |
|
270 | } | |
272 |
|
271 | |||
273 | /* |
|
272 | /* | |
274 | * Esta función genera el archivo de experimento a partir de la trama TCP recibida |
|
273 | * Esta función genera el archivo de experimento a partir de la trama TCP recibida | |
275 | */ |
|
274 | */ | |
276 |
|
275 | |||
277 | void recibe_experimento(char *p_data, char filename[]){ |
|
276 | void recibe_experimento(char *p_data, char filename[]){ | |
278 | FILE *fd; |
|
277 | FILE *fd; | |
279 | int i = 0; |
|
278 | int i = 0; | |
280 |
|
279 | |||
281 | while (*p_data != '\n'){ |
|
280 | while (*p_data != '\n'){ | |
282 | filename[i] = *p_data; |
|
281 | filename[i] = *p_data; | |
283 | i++; |
|
282 | i++; | |
284 | p_data++; |
|
283 | p_data++; | |
285 | } |
|
284 | } | |
286 | filename[i] = '\0'; |
|
285 | filename[i] = '\0'; | |
287 | p_data = p_data - i; |
|
286 | p_data = p_data - i; | |
288 | fd = fopen(filename,"w"); |
|
287 | fd = fopen(filename,"w"); | |
289 | fprintf(fd, p_data); |
|
288 | fprintf(fd, p_data); | |
290 | fclose(fd); |
|
289 | fclose(fd); | |
291 | } |
|
290 | } | |
292 |
|
291 | |||
293 | /* |
|
292 | /* | |
294 | * Esta funcion carga un archivo en un buffer que esta ubicado en memoria, luego |
|
293 | * Esta funcion carga un archivo en un buffer que esta ubicado en memoria, luego | |
295 | * este buffer es usado en la funcion "cambia_apuntamiento" para obtener el dato |
|
294 | * este buffer es usado en la funcion "cambia_apuntamiento" para obtener el dato | |
296 | * que sera usado en el cambio de apuntamiento. |
|
295 | * que sera usado en el cambio de apuntamiento. | |
297 | */ |
|
296 | */ | |
298 | int carga_experimento(char *nombre_archivo){ |
|
297 | int carga_experimento(char *nombre_archivo){ | |
299 |
|
298 | |||
300 | FILE *Archivo_Fd; |
|
299 | FILE *Archivo_Fd; | |
301 |
|
300 | |||
302 | char *cadena = (char *) malloc(25); |
|
301 | char *cadena = (char *) malloc(25); | |
303 |
|
302 | |||
304 | int longitud_cadena; |
|
303 | int longitud_cadena; | |
305 | int num_bytes= 0; |
|
304 | int num_bytes= 0; | |
306 | int num_filas= 0; |
|
305 | int num_filas= 0; | |
307 |
|
306 | |||
308 | Archivo_Fd = fopen(nombre_archivo,"r"); // Se procede a abrir el archivo, segun la ruta especificada |
|
307 | Archivo_Fd = fopen(nombre_archivo,"r"); // Se procede a abrir el archivo, segun la ruta especificada | |
309 | if(!Archivo_Fd){ |
|
308 | if(!Archivo_Fd){ | |
310 | ERROR("carga_archivo: No se pudo abrir el archivo!!! --> fopen()\n"); |
|
309 | ERROR("carga_archivo: No se pudo abrir el archivo!!! --> fopen()\n"); | |
311 | return -1; |
|
310 | return -1; | |
312 | }else{ |
|
311 | }else{ | |
313 |
|
312 | |||
314 | while(!feof(Archivo_Fd)){ // Se procede a calcular la longitud del archivo para separar memoria |
|
313 | while(!feof(Archivo_Fd)){ // Se procede a calcular la longitud del archivo para separar memoria | |
315 | fgets(cadena,20,Archivo_Fd); |
|
314 | fgets(cadena,20,Archivo_Fd); | |
316 | longitud_cadena= strlen(cadena); |
|
315 | longitud_cadena= strlen(cadena); | |
317 | cadena[longitud_cadena-1] = '\0'; |
|
316 | cadena[longitud_cadena-1] = '\0'; | |
318 | num_bytes = num_bytes + longitud_cadena; |
|
317 | num_bytes = num_bytes + longitud_cadena; | |
319 | num_filas++; |
|
318 | num_filas++; | |
320 | } |
|
319 | } | |
321 |
|
320 | |||
322 | rewind(Archivo_Fd); // Se reinicia el puntero del archivo |
|
321 | rewind(Archivo_Fd); // Se reinicia el puntero del archivo | |
323 |
|
322 | |||
324 | char *buffer_temporal = (char *) malloc(num_bytes+1); // Se separa espacio de memoria segun |
|
323 | char *buffer_temporal = (char *) malloc(num_bytes+1); // Se separa espacio de memoria segun | |
325 | // la longitud del archivo |
|
324 | // la longitud del archivo | |
326 | fread(buffer_temporal, sizeof(char), num_bytes, Archivo_Fd); |
|
325 | fread(buffer_temporal, sizeof(char), num_bytes, Archivo_Fd); | |
327 |
|
326 | |||
328 |
char *puntero= strstr(buffer_temporal,".ab |
|
327 | char *puntero= strstr(buffer_temporal,".ab1"); // Se procede a eliminar la cabecera del archivo | |
329 | puntero= puntero + 12; |
|
328 | puntero= puntero + 12; | |
330 |
|
329 | |||
331 | buff_experimento = (char *) malloc(7*(num_filas-3)); // num_bytes_fila*(num_filas-3); |
|
330 | buff_experimento = (char *) malloc(7*(num_filas-3)); // num_bytes_fila*(num_filas-3); | |
332 | strncpy(buff_experimento,puntero,7*(num_filas-3)); // Se carga en memoria la informacion del archivo |
|
331 | strncpy(buff_experimento,puntero,7*(num_filas-3)); // Se carga en memoria la informacion del archivo | |
333 |
|
332 | |||
334 | fclose(Archivo_Fd); |
|
333 | fclose(Archivo_Fd); | |
335 |
|
334 | |||
336 | return 1; |
|
335 | return 1; | |
337 | } |
|
336 | } | |
338 | } |
|
337 | } | |
339 |
|
338 | |||
340 | /* |
|
339 | /* | |
341 | * Esta funcion recibe un numero en formato char, el dato se transforma a su equivalente en |
|
340 | * Esta funcion recibe un numero en formato char, el dato se transforma a su equivalente en | |
342 | * un numero entero, que sera usado para sacar un dato del buffer "buff_experimento", esta |
|
341 | * un numero entero, que sera usado para sacar un dato del buffer "buff_experimento", esta | |
343 | * dato es el valor que se enviara al sistema de conmutacion RF para el cambio de apunte a |
|
342 | * dato es el valor que se enviara al sistema de conmutacion RF para el cambio de apunte a | |
344 | * traves del puerto GPIO. |
|
343 | * traves del puerto GPIO. | |
345 | */ |
|
344 | */ | |
346 | int cambia_apuntamiento(char *puntero_char){ |
|
345 | int cambia_apuntamiento(char *puntero_char){ | |
347 |
|
346 | |||
348 | /*MSB-UP-LSB MSB-DOWN-LSB*/ |
|
347 | /*MSB-UP-LSB MSB-DOWN-LSB*/ | |
349 | int desplazamiento[6]={30,28,26,24,22,20}; // Defino los dezplazamientos que se aplicara |
|
348 | int desplazamiento[6]={30,28,26,24,22,20}; // Defino los dezplazamientos que se aplicara | |
350 | // al dato que ingresa para formar el número |
|
349 | // al dato que ingresa para formar el número | |
351 | // entero que se le pasara al puerto GPIO |
|
350 | // entero que se le pasara al puerto GPIO | |
352 | // Estos números son los pines del puerto GPIO |
|
351 | // Estos números son los pines del puerto GPIO | |
353 | // que se estan usando para el control |
|
352 | // que se estan usando para el control | |
354 |
|
353 | |||
355 | int puntero= atoi(puntero_char); // Se convierte a entero la direccion del puntero |
|
354 | int puntero= atoi(puntero_char); // Se convierte a entero la direccion del puntero | |
356 |
|
355 | |||
357 | int base= 7*puntero; // base= cantidad_bytes del dato x puntero |
|
356 | int base= 7*puntero; // base= cantidad_bytes del dato x puntero | |
358 | // cantidad de bytes es el numero de bytes que |
|
357 | // cantidad de bytes es el numero de bytes que | |
359 |
printf("%s\n |
|
358 | printf("%s\n",puntero_char); // contiene cada dato, para este caso es 7 | |
360 | // porque es 6 bits de datos + 1 bit del cambio |
|
359 | // porque es 6 bits de datos + 1 bit del cambio | |
361 | // de linea. |
|
360 | // de linea. | |
362 | char valor_char; |
|
361 | char valor_char; | |
363 | unsigned long valor; |
|
362 | unsigned long valor; | |
364 | unsigned long acumulado_ceros=0; |
|
363 | unsigned long acumulado_ceros=0; | |
365 | unsigned long acumulado_unos=0; |
|
364 | unsigned long acumulado_unos=0; | |
366 |
|
365 | |||
367 | int offset; // Defino offset para el desplazamiento a traves |
|
366 | int offset; // Defino offset para el desplazamiento a traves | |
368 | for(offset=0;offset<6;offset++){ // de cada dato que se obtiene del "buff_experimento" |
|
367 | for(offset=0;offset<6;offset++){ // de cada dato que se obtiene del "buff_experimento" | |
369 | printf("tp1\n"); |
|
368 | ||
370 | printf("%i\n",base+offset); |
|
|||
371 | valor_char= buff_experimento[base+offset]; // Obtengo el dato |
|
369 | valor_char= buff_experimento[base+offset]; // Obtengo el dato | |
372 |
|
370 | |||
373 | if (valor_char == '0'){ // Obtengo el número acumulado segun sea un cero o un uno |
|
371 | if (valor_char == '0'){ // Obtengo el número acumulado segun sea un cero o un uno | |
374 | valor= 0; |
|
372 | valor= 0; | |
375 | acumulado_ceros= acumulado_ceros + (1 << desplazamiento[offset]); |
|
373 | acumulado_ceros= acumulado_ceros + (1 << desplazamiento[offset]); | |
376 | }else{ |
|
374 | }else{ | |
377 | valor= 1; |
|
375 | valor= 1; | |
378 | acumulado_unos= acumulado_unos + (1 << desplazamiento[offset]); |
|
376 | acumulado_unos= acumulado_unos + (1 << desplazamiento[offset]); | |
379 | } |
|
377 | } | |
380 | } |
|
378 | } | |
381 | printf("tp2\n"); |
|
|||
382 | pio_out(pioc, maskc_out, acumulado_unos, 1); |
|
379 | pio_out(pioc, maskc_out, acumulado_unos, 1); | |
383 | printf("tp3\n"); |
|
|||
384 | pio_out(pioc, maskc_out, acumulado_ceros, 0); |
|
380 | pio_out(pioc, maskc_out, acumulado_ceros, 0); | |
385 | printf("tp4\n"); |
|
|||
386 |
|
381 | |||
387 | return 1; |
|
382 | return 1; | |
388 |
|
383 | |||
389 | } |
|
384 | } | |
390 |
|
385 | |||
391 | /* |
|
386 | /* | |
392 | * Esta funcion lee "n" veces el estado del APUNTE actual y lo guarda en el |
|
387 | * Esta funcion lee "n" veces el estado del APUNTE actual y lo guarda en el | |
393 | * archivo Verificacion. |
|
388 | * archivo Verificacion. | |
394 | */ |
|
389 | */ | |
395 |
|
390 | |||
396 | int chequeo_sistema(char *filename, char *numero_muestras){ |
|
391 | int chequeo_sistema(char *filename, char *numero_muestras){ | |
397 |
|
392 | |||
398 | char valor[7]; |
|
393 | char valor[7]; | |
399 | int i,cnt; |
|
394 | int i,cnt; | |
400 | unsigned int entradac= 0; |
|
395 | unsigned int entradac= 0; | |
401 | FILE *fd; |
|
396 | FILE *fd; | |
402 | fd=fopen(filename,"w"); |
|
397 | fd=fopen(filename,"w"); | |
403 | fprintf(fd,"%s\n","Verificacion"); |
|
398 | fprintf(fd,"%s\n","Verificacion"); | |
|
399 | fprintf(fd,"%s",parameters.ID); | |||
404 | fprintf(fd,"%s\n","------------"); |
|
400 | fprintf(fd,"%s\n","------------"); | |
405 | cnt=0; |
|
401 | cnt=0; | |
406 | do |
|
402 | do | |
407 | { |
|
403 | { | |
408 | //Inicializando arreglo |
|
404 | //Inicializando arreglo | |
409 | for(i=0;i<6;i++) |
|
405 | for(i=0;i<6;i++) | |
410 | valor[i]='0'; |
|
406 | valor[i]='0'; | |
411 |
|
407 | |||
412 | valor[6]='\0'; |
|
408 | valor[6]='\0'; | |
413 |
|
409 | |||
414 | //Lectura de puerto |
|
410 | //Lectura de puerto | |
415 | entradac= pio_in(piob,maskb_in); |
|
411 | entradac= pio_in(piob,maskb_in); | |
416 |
|
412 | |||
417 | //Dandole formato al dato |
|
413 | //Dandole formato al dato | |
418 | if (!(entradac & bit_up_2)) |
|
414 | if (!(entradac & bit_up_2)) | |
419 | valor[0] = '1'; |
|
415 | valor[0] = '1'; | |
420 | if (!(entradac & bit_up_1)) |
|
416 | if (!(entradac & bit_up_1)) | |
421 | valor[1] = '1'; |
|
417 | valor[1] = '1'; | |
422 | if (!(entradac & bit_up_0)) |
|
418 | if (!(entradac & bit_up_0)) | |
423 | valor[2] = '1'; |
|
419 | valor[2] = '1'; | |
424 | if (!(entradac & bit_dow_2)) |
|
420 | if (!(entradac & bit_dow_2)) | |
425 | valor[3] = '1'; |
|
421 | valor[3] = '1'; | |
426 | if (!(entradac & bit_dow_1)) |
|
422 | if (!(entradac & bit_dow_1)) | |
427 | valor[4] = '1'; |
|
423 | valor[4] = '1'; | |
428 | if (!(entradac & bit_dow_0)) |
|
424 | if (!(entradac & bit_dow_0)) | |
429 | valor[5] = '1'; |
|
425 | valor[5] = '1'; | |
430 |
|
426 | |||
431 | //Escribiendo en archivo |
|
427 | //Escribiendo en archivo | |
432 | fprintf(fd,"%s\n",valor); |
|
428 | fprintf(fd,"%s\n",valor); | |
433 | cnt=cnt+1; |
|
429 | cnt=cnt+1; | |
434 | usleep(1*1000*1000); |
|
430 | usleep(1*1000*1000); | |
435 |
|
431 | |||
436 | }while(cnt < atoi(numero_muestras)); |
|
432 | }while(cnt < atoi(numero_muestras)); | |
437 |
|
433 | |||
438 | fclose(fd); |
|
434 | fclose(fd); | |
439 |
|
435 | |||
440 | return 7*atoi(numero_muestras) + 26 + 1; //incluye eof |
|
436 | return 7*atoi(numero_muestras) + 26 + 4 + 1; //incluye eof | |
441 | } |
|
437 | } | |
442 |
|
438 | |||
443 | char* File2buffer(char *filename, int n){ |
|
439 | char* File2buffer(char *filename, int n){ | |
444 | FILE *fd; |
|
440 | FILE *fd; | |
445 | char* tx_data = (char *)malloc(n); |
|
441 | char* tx_data = (char *)malloc(n); | |
446 | fd = fopen(filename,"r"); |
|
442 | fd = fopen(filename,"r"); | |
447 | fread(tx_data, 1, n, fd); |
|
443 | fread(tx_data, 1, n-1, fd); | |
448 | fclose(fd); |
|
444 | fclose(fd); | |
|
445 | tx_data = tx_data + n - 1; | |||
|
446 | *tx_data = '\0'; | |||
|
447 | tx_data = tx_data - n + 1; | |||
449 | return tx_data; |
|
448 | return tx_data; | |
450 | } |
|
449 | } | |
451 |
|
450 | |||
452 | cmp inicializa_modulo(cmp p){ |
|
451 | cmp inicializa_modulo(cmp p){ | |
453 | FILE *fd = fopen("configuration.txt","r"); |
|
452 | FILE *fd = fopen("configuration.txt","r"); | |
454 | fgets(p.ID,20,fd); |
|
453 | fgets(p.ID,20,fd); | |
455 | fgets(p.param2,20,fd); |
|
454 | fgets(p.param2,20,fd); | |
456 | fgets(p.param3,20,fd); |
|
455 | fgets(p.param3,20,fd); | |
457 | fgets(p.param4,20,fd); |
|
456 | fgets(p.param4,20,fd); | |
458 | fclose(fd); |
|
457 | fclose(fd); | |
459 | return p; |
|
458 | return p; | |
460 | } |
|
459 | } |
General Comments 0
You need to be logged in to leave comments.
Login now