##// END OF EJS Templates
imanay -
r45:46
parent child
Show More
@@ -0,0 +1,324
1 /*
No newline at end of file
2 * Servidor.c
No newline at end of file
3 *
No newline at end of file
4 * Created on: Nov 3, 2009
No newline at end of file
5 * Author: Jose Francisco Quenta
No newline at end of file
6 *
No newline at end of file
7 * Se implementa:
No newline at end of file
8 * -Carga en memoria los apuntes contenidos en un archivo de experimentos: apunte0 -> GPIO
No newline at end of file
9 * -Cambio de apunte.
No newline at end of file
10 * -Lectura del estado actual del apunte y grabado del mismo en un archivo
No newline at end of file
11 */
No newline at end of file
12
No newline at end of file
13 #include <stdio.h>
No newline at end of file
14 #include <stdlib.h>
No newline at end of file
15 #include <string.h>
No newline at end of file
16 #include <unistd.h>
No newline at end of file
17 #include <errno.h>
No newline at end of file
18
No newline at end of file
19 #include <sys/types.h>
No newline at end of file
20 #include <sys/socket.h>
No newline at end of file
21 #include <netinet/in.h>
No newline at end of file
22 #include <arpa/inet.h>
No newline at end of file
23 #include <netdb.h>
No newline at end of file
24
No newline at end of file
25 #include "./Librerias/AT91gpio_Funciones.h"
No newline at end of file
26 #include "./Librerias/Mensajes.h"
No newline at end of file
27
No newline at end of file
28 #define PUERTO_SERVIDOR 5500
No newline at end of file
29 #define TAM_BUFFER 100
No newline at end of file
30
No newline at end of file
31 #define maskc_out PC30+PC28+PC26+PC24+PC22+PC20 //MSB-UP-LSB MSB-DOWN-LSB //APUNTE
No newline at end of file
32
No newline at end of file
33 #define maskb_in PB16+PB18+PB20+PB30+PB24+PB22 //MSB-UP-LSB MSB-DOWN-LSB //VERIFICACION
No newline at end of file
34
No newline at end of file
35 #define bit_up_2 0x00010000 //Mascara de cada bit a revisar: bit_up_2 es MSB
No newline at end of file
36 #define bit_up_1 0x00040000
No newline at end of file
37 #define bit_up_0 0x00100000
No newline at end of file
38 #define bit_dow_2 0x40000000
No newline at end of file
39 #define bit_dow_1 0x01000000
No newline at end of file
40 #define bit_dow_0 0x00400000
No newline at end of file
41
No newline at end of file
42 #define MyID 11
No newline at end of file
43
No newline at end of file
44 char *buff_experimento= NULL;
No newline at end of file
45
No newline at end of file
46 AT91S_PIO *pioc;
No newline at end of file
47 AT91S_PIO *piob;
No newline at end of file
48
No newline at end of file
49 /*
No newline at end of file
50 * Zona de declaracion de cabeceras.
No newline at end of file
51 */
No newline at end of file
52 void inicializa_gpio();
No newline at end of file
53 int procesa_peticion(char *buff_peticion);
No newline at end of file
54 int cambia_apuntamiento(char *puntero_char);
No newline at end of file
55 int carga_experimento(char *nombre_archivo);
No newline at end of file
56 int chequeo_sistema(char *numero_muestras);
No newline at end of file
57
No newline at end of file
58 /*
No newline at end of file
59 *
No newline at end of file
60 */
No newline at end of file
61 int main(){
No newline at end of file
62
No newline at end of file
63 int conexion_servidorFd;
No newline at end of file
64 struct sockaddr_in inf_servidor;
No newline at end of file
65 struct sockaddr_storage inf_cliente;
No newline at end of file
66 int resultado;
No newline at end of file
67 int numbytes_recibidos;
No newline at end of file
68 int rpta;
No newline at end of file
69
No newline at end of file
70 char *buff_peticion = (char *) malloc(TAM_BUFFER);
No newline at end of file
71
No newline at end of file
72 size_t addr_len;
No newline at end of file
73
No newline at end of file
74 memset(&inf_servidor, 0, sizeof(inf_servidor));
No newline at end of file
75 inf_servidor.sin_family= AF_INET;
No newline at end of file
76 inf_servidor.sin_port= htons(PUERTO_SERVIDOR);
No newline at end of file
77 inf_servidor.sin_addr.s_addr= INADDR_ANY;
No newline at end of file
78
No newline at end of file
79 /* Se establece el socket */
No newline at end of file
80 conexion_servidorFd = socket(AF_INET,SOCK_DGRAM,0);
No newline at end of file
81 if (conexion_servidorFd == -1){
No newline at end of file
82 ERROR_FATAL("No se establecio correctamente el socket: socket()");
No newline at end of file
83 }
No newline at end of file
84
No newline at end of file
85 /* Se asocia el socket a un puerto y una IP */
No newline at end of file
86 resultado = bind(conexion_servidorFd,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor));
No newline at end of file
87 if (resultado== -1){
No newline at end of file
88 ERROR_FATAL("No se establecio correctamente el socket: bind()");
No newline at end of file
89 }
No newline at end of file
90
No newline at end of file
91 /* Inicializamos el puerto GPIO del sistema embebido GSBC-9260S */
No newline at end of file
92 inicializa_gpio();
No newline at end of file
93
No newline at end of file
94 while(1){
No newline at end of file
95 LOG_SERVIDOR("Esperando solicitud de cliente...\n");
No newline at end of file
96
No newline at end of file
97 /* Se espera hasta que un cliente se conecte */
No newline at end of file
98 addr_len = sizeof(inf_cliente);
No newline at end of file
99 numbytes_recibidos = recvfrom(conexion_servidorFd, buff_peticion, TAM_BUFFER-1, 0, (struct sockaddr *)&inf_cliente, &addr_len);
No newline at end of file
100 if (numbytes_recibidos == -1){
No newline at end of file
101 ERROR_FATAL("Error en la recepcion de datos: recvfrom()");
No newline at end of file
102 }
No newline at end of file
103
No newline at end of file
104 /* Se procede a procesar los datos recibidos */
No newline at end of file
105 buff_peticion[numbytes_recibidos]= '\0';
No newline at end of file
106
No newline at end of file
107 /* procesamiento de la peticion */
No newline at end of file
108 rpta = procesa_peticion(buff_peticion);
No newline at end of file
109
No newline at end of file
110 //Respuesta del modulo de control
No newline at end of file
111 //
No newline at end of file
112
No newline at end of file
113
No newline at end of file
114 }
No newline at end of file
115 }
No newline at end of file
116
No newline at end of file
117
No newline at end of file
118
No newline at end of file
119 /*
No newline at end of file
120 * Esta funcion incializa el puerto GPIO
No newline at end of file
121 */
No newline at end of file
122 void inicializa_gpio(){
No newline at end of file
123
No newline at end of file
124 // Configuracion de los pines de APUNTE
No newline at end of file
125 pioc = pio_map(PIOC_BASE);
No newline at end of file
126 pio_enable(pioc, maskc_out);
No newline at end of file
127 pio_disable_irq(pioc, maskc_out);
No newline at end of file
128 pio_disable_multiple_driver(pioc, maskc_out);
No newline at end of file
129 pio_disable_pull_ups(pioc, maskc_out);
No newline at end of file
130 pio_synchronous_data_output(pioc, maskc_out);
No newline at end of file
131 pio_output_enable(pioc, maskc_out);
No newline at end of file
132
No newline at end of file
133 // Configuracion de los pines de VERIFICACION
No newline at end of file
134 piob = pio_map(PIOB_BASE);
No newline at end of file
135 pio_enable(piob, maskb_in);
No newline at end of file
136 pio_disable_irq(piob, maskb_in);
No newline at end of file
137 pio_disable_multiple_driver(piob, maskb_in);
No newline at end of file
138 pio_disable_pull_ups(piob, maskb_in);
No newline at end of file
139 pio_input_enable(piob, maskb_in);
No newline at end of file
140
No newline at end of file
141 }
No newline at end of file
142
No newline at end of file
143 /*
No newline at end of file
144 * Esta funcion procesa el mensaje de peticion y genera respuesta
No newline at end of file
145 */
No newline at end of file
146 int procesa_peticion(char *buff_peticion){
No newline at end of file
147 int rpta;
No newline at end of file
148 // char *header = strtok(buff_peticion, ":");
No newline at end of file
149 // char *ipSource = strtok(buff_peticion, ":");
No newline at end of file
150 // char *ipDestino = strtok(buff_peticion, ":");
No newline at end of file
151 char *comando = strtok(buff_peticion, ":");
No newline at end of file
152 char *valor = strtok(NULL, ":");
No newline at end of file
153
No newline at end of file
154 if ((comando == NULL) || (valor == NULL)){
No newline at end of file
155 ERROR("procesarPeticion: formato de mensaje incorrecto");
No newline at end of file
156 }
No newline at end of file
157 else{
No newline at end of file
158 if(strcmp(comando,"CARGA") == 0)
No newline at end of file
159 rpta = carga_experimento(valor);
No newline at end of file
160 else if(strcmp(comando,"CAMBIA") == 0)
No newline at end of file
161 rpta = cambia_apuntamiento(valor);
No newline at end of file
162 else if(strcmp(comando,"CHEQUEO") == 0)
No newline at end of file
163 rpta = chequeo_sistema(valor);
No newline at end of file
164 else
No newline at end of file
165 ERROR("procesa_peticion: comando no reconocido");
No newline at end of file
166 }
No newline at end of file
167
No newline at end of file
168 return rpta;
No newline at end of file
169 }
No newline at end of file
170
No newline at end of file
171
No newline at end of file
172 /*
No newline at end of file
173 * Esta funcion carga un archivo en un buffer que esta ubicado en memoria, luego
No newline at end of file
174 * este buffer es usado en la funcion "cambia_apuntamiento" para obtener el dato
No newline at end of file
175 * que sera usado en el cambio de apuntamiento.
No newline at end of file
176 */
No newline at end of file
177 int carga_experimento(char *nombre_archivo){
No newline at end of file
178
No newline at end of file
179 FILE *Archivo_Fd;
No newline at end of file
180
No newline at end of file
181 char *cadena = (char *) malloc(25);
No newline at end of file
182
No newline at end of file
183 int longitud_cadena;
No newline at end of file
184 int num_bytes= 0;
No newline at end of file
185 int num_filas= 0;
No newline at end of file
186
No newline at end of file
187 char ruta_archivo[50]; // Se crea la ruta para abrir el archivo
No newline at end of file
188 strcpy(ruta_archivo,"/mnt/sd/archivos/");
No newline at end of file
189 strcat(ruta_archivo,nombre_archivo);
No newline at end of file
190
No newline at end of file
191 Archivo_Fd = fopen(ruta_archivo,"r"); // Se procede a abrir el archivo, segun la ruta especificada
No newline at end of file
192 if(!Archivo_Fd){
No newline at end of file
193 ERROR("carga_archivo: No se pudo abrir el archivo!!! --> fopen()\n");
No newline at end of file
194 return -1;
No newline at end of file
195 }else{
No newline at end of file
196
No newline at end of file
197 while(!feof(Archivo_Fd)){ // Se procede a calcular la longitud del archivo para separar memoria
No newline at end of file
198 fgets(cadena,20,Archivo_Fd);
No newline at end of file
199 longitud_cadena= strlen(cadena);
No newline at end of file
200 cadena[longitud_cadena-1] = '\0';
No newline at end of file
201 num_bytes = num_bytes + longitud_cadena;
No newline at end of file
202 num_filas++;
No newline at end of file
203 }
No newline at end of file
204
No newline at end of file
205 rewind(Archivo_Fd); // Se reinicia el puntero del archivo
No newline at end of file
206
No newline at end of file
207 char *buffer_temporal = (char *) malloc(num_bytes+1); // Se separa espacio de memoria segun
No newline at end of file
208 // la longitud del archivo
No newline at end of file
209 fread(buffer_temporal, sizeof(char), num_bytes, Archivo_Fd);
No newline at end of file
210
No newline at end of file
211 char *puntero= strstr(buffer_temporal,".abs"); // Se procede a eliminar la cabecera del archivo
No newline at end of file
212 puntero= puntero + 12;
No newline at end of file
213
No newline at end of file
214 buff_experimento = (char *) malloc(7*(num_filas-3)); // num_bytes_fila*(num_filas-3);
No newline at end of file
215 strncpy(buff_experimento,puntero,7*(num_filas-3)); // Se carga en memoria la informacion del archivo
No newline at end of file
216
No newline at end of file
217 fclose(Archivo_Fd);
No newline at end of file
218
No newline at end of file
219 cambia_apuntamiento("0"); // Se apunta a la direccion 0
No newline at end of file
220
No newline at end of file
221 return 1;
No newline at end of file
222 }
No newline at end of file
223 }
No newline at end of file
224
No newline at end of file
225 /*
No newline at end of file
226 * Esta funcion recibe un numero en formato char, el dato se transforma a su equivalente en
No newline at end of file
227 * un numero entero, que sera usado para sacar un dato del buffer "buff_experimento", esta
No newline at end of file
228 * dato es el valor que se enviara al sistema de conmutacion RF para el cambio de apunte a
No newline at end of file
229 * traves del puerto GPIO.
No newline at end of file
230 */
No newline at end of file
231 int cambia_apuntamiento(char *puntero_char){
No newline at end of file
232
No newline at end of file
233 /*MSB-UP-LSB MSB-DOWN-LSB*/
No newline at end of file
234 int desplazamiento[6]={30,28,26,24,22,20}; // Defino los dezplazamientos que se aplicara
No newline at end of file
235 // al dato que ingresa para formar el número
No newline at end of file
236 // entero que se le pasara al puerto GPIO
No newline at end of file
237 // Estos números son los pines del puerto GPIO
No newline at end of file
238 // que se estan usando para el control
No newline at end of file
239
No newline at end of file
240 int puntero= atoi(puntero_char); // Se convierte a entero la direccion del puntero
No newline at end of file
241
No newline at end of file
242 int base= 7*puntero; // base= cantidad_bytes del dato x puntero
No newline at end of file
243 // cantidad de bytes es el numero de bytes que
No newline at end of file
244 printf("%i\n",puntero); // contiene cada dato, para este caso es 7
No newline at end of file
245 // porque es 6 bits de datos + 1 bit del cambio
No newline at end of file
246 // de linea.
No newline at end of file
247 char valor_char;
No newline at end of file
248 unsigned long valor;
No newline at end of file
249 unsigned long acumulado_ceros=0;
No newline at end of file
250 unsigned long acumulado_unos=0;
No newline at end of file
251
No newline at end of file
252 int offset; // Defino offset para el desplazamiento a traves
No newline at end of file
253 for(offset=0;offset<6;offset++){ // de cada dato que se obtiene del "buff_experimento"
No newline at end of file
254
No newline at end of file
255 valor_char= buff_experimento[base+offset]; // Obtengo el dato
No newline at end of file
256
No newline at end of file
257 if (valor_char == '0'){ // Obtengo el número acumulado segun sea un cero o un uno
No newline at end of file
258 valor= 0;
No newline at end of file
259 acumulado_ceros= acumulado_ceros + (1 << desplazamiento[offset]);
No newline at end of file
260 }else{
No newline at end of file
261 valor= 1;
No newline at end of file
262 acumulado_unos= acumulado_unos + (1 << desplazamiento[offset]);
No newline at end of file
263 }
No newline at end of file
264
No newline at end of file
265 }
No newline at end of file
266
No newline at end of file
267 pio_out(pioc, maskc_out, acumulado_unos, 1);
No newline at end of file
268 pio_out(pioc, maskc_out, acumulado_ceros, 0);
No newline at end of file
269
No newline at end of file
270 return 1;
No newline at end of file
271
No newline at end of file
272 }
No newline at end of file
273
No newline at end of file
274 /*
No newline at end of file
275 * Esta funcion lee "n" veces el estado del APUNTE actual y lo guarda en el
No newline at end of file
276 * archivo Verificacion.
No newline at end of file
277 */
No newline at end of file
278
No newline at end of file
279 int chequeo_sistema(char *numero_muestras){
No newline at end of file
280
No newline at end of file
281 char valor[7];
No newline at end of file
282 int i,cnt;
No newline at end of file
283 unsigned int entradac= 0;
No newline at end of file
284 FILE *fd;
No newline at end of file
285 fd=fopen("/mnt/sd/archivos/Verificacion","w");
No newline at end of file
286 fprintf(fd,"%s\n","Verificacion");
No newline at end of file
287 fprintf(fd,"%s\n","------------");
No newline at end of file
288 cnt=0;
No newline at end of file
289 do
No newline at end of file
290 {
No newline at end of file
291 //Inicializando arreglo
No newline at end of file
292 for(i=0;i<6;i++)
No newline at end of file
293 valor[i]='0';
No newline at end of file
294
No newline at end of file
295 valor[6]='\0';
No newline at end of file
296
No newline at end of file
297 //Lectura de puerto
No newline at end of file
298 entradac= pio_in(piob,maskb_in);
No newline at end of file
299
No newline at end of file
300 //Dandole formato al dato
No newline at end of file
301 if (!(entradac & bit_up_2))
No newline at end of file
302 valor[0] = '1';
No newline at end of file
303 if (!(entradac & bit_up_1))
No newline at end of file
304 valor[1] = '1';
No newline at end of file
305 if (!(entradac & bit_up_0))
No newline at end of file
306 valor[2] = '1';
No newline at end of file
307 if (!(entradac & bit_dow_2))
No newline at end of file
308 valor[3] = '1';
No newline at end of file
309 if (!(entradac & bit_dow_1))
No newline at end of file
310 valor[4] = '1';
No newline at end of file
311 if (!(entradac & bit_dow_0))
No newline at end of file
312 valor[5] = '1';
No newline at end of file
313
No newline at end of file
314 //Escribiendo en archivo
No newline at end of file
315 fprintf(fd,"%s\n",valor);
No newline at end of file
316 cnt=cnt+1;
No newline at end of file
317 usleep(1*1000*1000);
No newline at end of file
318
No newline at end of file
319 }while(cnt < atoi(numero_muestras));
No newline at end of file
320
No newline at end of file
321 fclose(fd);
No newline at end of file
322
No newline at end of file
323 return 1;
No newline at end of file
324 } No newline at end of file
@@ -15,7 +15,7
15 15 </extensions> No newline at end of file
16 16 </storageModule> No newline at end of file
17 17 <storageModule moduleId="cdtBuildSystem" version="4.0.0">
18 No newline at end of file
18 <configuration artifactName="Control_Module" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release,org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.exe.release.1568263498" name="Release" parent="cdt.managedbuild.config.gnu.exe.release"> No newline at end of file
19 19 <folderInfo id="cdt.managedbuild.config.gnu.exe.release.1568263498." name="/" resourcePath=""> No newline at end of file
20 20 <toolChain id="cdt.managedbuild.toolchain.gnu.exe.release.785297601" name="Linux GCC" superClass="cdt.managedbuild.toolchain.gnu.exe.release"> No newline at end of file
21 21 <targetPlatform id="cdt.managedbuild.target.gnu.platform.exe.release.1008424557" name="Debug Platform" superClass="cdt.managedbuild.target.gnu.platform.exe.release"/> No newline at end of file
@@ -50,10 +50,14
50 50 * Zona de declaracion de cabeceras. No newline at end of file
51 51 */ No newline at end of file
52 52 void inicializa_gpio();
53 No newline at end of file
53 int procesa_peticion(char *buff_peticion);
No newline at end of file
54 No newline at end of file
54 int cambia_apuntamiento(char *puntero_char);
No newline at end of file
55 No newline at end of file
55 int carga_experimento(char *nombre_archivo);
No newline at end of file
56 No newline at end of file
56 int chequeo_sistema(char *numero_muestras); No newline at end of file
No newline at end of file
57 //int carga_experimento(char *nombre_archivo);
No newline at end of file
58 void carga_experimento(char *nombre_archivo);
No newline at end of file
59 //int chequeo_sistema(char *numero_muestras);
No newline at end of file
60 void chequeo_sistema(char *numero_muestras); No newline at end of file
57 61 No newline at end of file
58 62 /* No newline at end of file
59 63 * No newline at end of file
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now