|
|
/*
|
|
|
* Servidor.c
|
|
|
*
|
|
|
* Created on: Nov 3, 2009
|
|
|
* Author: Jose Francisco Quenta
|
|
|
*
|
|
|
* Se implementa:
|
|
|
* -Carga en memoria los apuntes contenidos en un archivo de experimentos: apunte0 -> GPIO
|
|
|
* -Cambio de apunte.
|
|
|
* -Lectura del estado actual del apunte y grabado del mismo en un archivo
|
|
|
*/
|
|
|
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
#include <unistd.h>
|
|
|
#include <errno.h>
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
#include <sys/socket.h>
|
|
|
#include <netinet/in.h>
|
|
|
#include <arpa/inet.h>
|
|
|
#include <netdb.h>
|
|
|
|
|
|
#include "./Librerias/AT91gpio_Funciones.h"
|
|
|
#include "./Librerias/Mensajes.h"
|
|
|
|
|
|
#define PUERTO_SERVIDOR 5500
|
|
|
#define TAM_BUFFER 1024
|
|
|
|
|
|
#define maskc_out PC30+PC28+PC26+PC24+PC22+PC20 //MSB-UP-LSB MSB-DOWN-LSB //APUNTE
|
|
|
|
|
|
#define maskb_in PB16+PB18+PB20+PB30+PB24+PB22 //MSB-UP-LSB MSB-DOWN-LSB //VERIFICACION
|
|
|
|
|
|
#define bit_up_2 0x00010000 //Mascara de cada bit a revisar: bit_up_2 es MSB
|
|
|
#define bit_up_1 0x00040000
|
|
|
#define bit_up_0 0x00100000
|
|
|
#define bit_dow_2 0x40000000
|
|
|
#define bit_dow_1 0x01000000
|
|
|
#define bit_dow_0 0x00400000
|
|
|
|
|
|
#define MyID 11
|
|
|
#define MAXPENDING 5 /* Maximum outstanding connection requests */
|
|
|
|
|
|
char *buff_experimento= NULL;
|
|
|
|
|
|
AT91S_PIO *pioc;
|
|
|
AT91S_PIO *piob;
|
|
|
|
|
|
struct control_module_parameters {
|
|
|
char ID[20];
|
|
|
char param2[20];
|
|
|
char param3[20];
|
|
|
char param4[20];
|
|
|
};
|
|
|
|
|
|
typedef struct control_module_parameters cmp;
|
|
|
|
|
|
cmp parameters;
|
|
|
/*
|
|
|
* Zona de declaracion de cabeceras.
|
|
|
*/
|
|
|
cmp inicializa_modulo(cmp p);
|
|
|
int inicializa_ethernet();
|
|
|
int rxData(int, char*);
|
|
|
void txData(int, char*);
|
|
|
void inicializa_gpio();
|
|
|
void procesa_peticion(char *rx_buffer, char *tx_buffer);
|
|
|
int cambia_apuntamiento(char *puntero_char);
|
|
|
int carga_experimento(char *nombre_archivo);
|
|
|
int chequeo_sistema(char *filename, char *numero_muestras);
|
|
|
void recibe_experimento(char *data, char filename[]);
|
|
|
char* File2buffer(char *filename2, int n);
|
|
|
|
|
|
/*
|
|
|
*
|
|
|
*/
|
|
|
int main(){
|
|
|
|
|
|
int servSocket;
|
|
|
int clntSocket;
|
|
|
|
|
|
|
|
|
char *rx_buffer = (char *) malloc(TAM_BUFFER);
|
|
|
char *tx_buffer = (char *) malloc(TAM_BUFFER);
|
|
|
/* Inicializa parametros del modulo*/
|
|
|
parameters = inicializa_modulo(parameters);
|
|
|
printf("%s%s%s%s",parameters.ID, parameters.param2, parameters.param3, parameters.param4);
|
|
|
/* Inicializa red*/
|
|
|
servSocket = inicializa_ethernet();
|
|
|
/* Inicializamos el puerto GPIO del sistema embebido GSBC-9260S */
|
|
|
inicializa_gpio();
|
|
|
|
|
|
while(1){
|
|
|
// Recepción TCP de petición
|
|
|
clntSocket = rxData(servSocket, rx_buffer);
|
|
|
//testpoint
|
|
|
printf("rx:%s\n",rx_buffer);
|
|
|
// Procesamiento de la petición
|
|
|
procesa_peticion(rx_buffer, tx_buffer);
|
|
|
//testpoint
|
|
|
printf("tx:%s\n",tx_buffer);
|
|
|
// Respuesta del modulo de control
|
|
|
txData(clntSocket, tx_buffer);
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
int inicializa_ethernet(){
|
|
|
|
|
|
struct sockaddr_in inf_servidor;
|
|
|
|
|
|
int servSocket;
|
|
|
|
|
|
int resultado;
|
|
|
|
|
|
/* Haciendo la estructura local*/
|
|
|
memset(&inf_servidor, 0, sizeof(inf_servidor));
|
|
|
inf_servidor.sin_family= AF_INET;
|
|
|
inf_servidor.sin_port= htons(PUERTO_SERVIDOR);
|
|
|
inf_servidor.sin_addr.s_addr= INADDR_ANY;
|
|
|
|
|
|
/* Se establece el socket */
|
|
|
servSocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
|
|
|
if (servSocket == -1){
|
|
|
ERROR_FATAL("No se establecio correctamente el socket: socket()\n");
|
|
|
exit(-1);
|
|
|
}
|
|
|
|
|
|
/* Se asocia el socket a un puerto y una IP */
|
|
|
resultado = bind(servSocket,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor));
|
|
|
if (resultado== -1){
|
|
|
ERROR_FATAL("No se establecio correctamente el socket: bind()\n");
|
|
|
exit(-1);
|
|
|
}
|
|
|
|
|
|
if (listen(servSocket, MAXPENDING) < 0){
|
|
|
printf("listen() failed\n");
|
|
|
exit(-1);
|
|
|
}
|
|
|
|
|
|
return servSocket;
|
|
|
|
|
|
}
|
|
|
|
|
|
int rxData(int servSocket, char* rx_buffer){
|
|
|
|
|
|
int clntSocket;
|
|
|
struct sockaddr_in inf_cliente;
|
|
|
int numbytes_recibidos;
|
|
|
unsigned int inf_client_Len;
|
|
|
|
|
|
printf("\nEsperando solicitud de cliente...\n");
|
|
|
|
|
|
/* Set the size of the in-out parameter */
|
|
|
inf_client_Len = sizeof(inf_cliente);
|
|
|
/* Se espera hasta que un cliente se conecte */
|
|
|
if ((clntSocket = accept(servSocket, (struct sockaddr *) &inf_cliente,
|
|
|
&inf_client_Len)) < 0)
|
|
|
printf("accept() failed\n");
|
|
|
|
|
|
if ((numbytes_recibidos = recv(clntSocket, rx_buffer, TAM_BUFFER, 0)) < 0)
|
|
|
printf("recv() failed\n");
|
|
|
|
|
|
/* Se procede a procesar los datos recibidos */
|
|
|
rx_buffer[numbytes_recibidos]= '\0';
|
|
|
|
|
|
return clntSocket;
|
|
|
}
|
|
|
|
|
|
void txData(int clntSocket, char* data){
|
|
|
|
|
|
/* Echo message back to client */
|
|
|
if (send(clntSocket, data, strlen(data), 0) != strlen(data))
|
|
|
printf("send() failed\n");
|
|
|
|
|
|
close(clntSocket); /* Close client socket */
|
|
|
}
|
|
|
/*
|
|
|
* Esta funcion incializa el puerto GPIO
|
|
|
*/
|
|
|
void inicializa_gpio(){
|
|
|
|
|
|
// Configuracion de los pines de APUNTE
|
|
|
pioc = pio_map(PIOC_BASE);
|
|
|
pio_enable(pioc, maskc_out);
|
|
|
pio_disable_irq(pioc, maskc_out);
|
|
|
pio_disable_multiple_driver(pioc, maskc_out);
|
|
|
pio_disable_pull_ups(pioc, maskc_out);
|
|
|
pio_synchronous_data_output(pioc, maskc_out);
|
|
|
pio_output_enable(pioc, maskc_out);
|
|
|
|
|
|
// Configuracion de los pines de VERIFICACION
|
|
|
piob = pio_map(PIOB_BASE);
|
|
|
pio_enable(piob, maskb_in);
|
|
|
pio_disable_irq(piob, maskb_in);
|
|
|
pio_disable_multiple_driver(piob, maskb_in);
|
|
|
pio_disable_pull_ups(piob, maskb_in);
|
|
|
pio_input_enable(piob, maskb_in);
|
|
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
* Esta funcion procesa el mensaje de peticion y genera respuesta
|
|
|
*/
|
|
|
void procesa_peticion(char *rx_buffer, char *tx_buffer){
|
|
|
int n = 0;
|
|
|
char filename1[50];
|
|
|
char filename2[] = "verificacion.txt";
|
|
|
char *tx_data = NULL;
|
|
|
char *header = strtok(rx_buffer, ":");
|
|
|
char *TypeOfInstrument = strtok(NULL, ":");
|
|
|
char *iDSource = strtok(NULL, ":");
|
|
|
char *iDDestino = strtok(NULL, ":");
|
|
|
char *len = strtok(NULL, ":");
|
|
|
char *cmd = strtok(NULL, ":");
|
|
|
char *rx_data = strtok(NULL, ":");
|
|
|
char *crc = strtok(NULL, ":");
|
|
|
|
|
|
if ((cmd == NULL) || (rx_data == NULL)){
|
|
|
ERROR("procesarPeticion: formato de mensaje incorrecto");
|
|
|
}
|
|
|
else{
|
|
|
if(strcmp(cmd,"SNDF") == 0){
|
|
|
recibe_experimento(rx_data,filename1);
|
|
|
carga_experimento(filename1);
|
|
|
cambia_apuntamiento("0");
|
|
|
tx_data = (char*)malloc(3);
|
|
|
tx_data = "OK";
|
|
|
}
|
|
|
else if(strcmp(cmd,"CHGB") == 0){
|
|
|
cambia_apuntamiento(rx_data);
|
|
|
tx_data = (char*)malloc(3);
|
|
|
tx_data = "OK";
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(strcmp(cmd,"ANST") == 0){
|
|
|
n = chequeo_sistema(filename2,rx_data);
|
|
|
printf("%i\n",n);
|
|
|
tx_data = File2buffer(filename2, n);
|
|
|
}
|
|
|
else{
|
|
|
tx_data = (char*)malloc(6);
|
|
|
tx_data = "Error";
|
|
|
ERROR("procesa_peticion: comando no reconocido");
|
|
|
}
|
|
|
|
|
|
strcpy(tx_buffer,header);
|
|
|
strcat(tx_buffer,":");
|
|
|
strcat(tx_buffer,TypeOfInstrument);
|
|
|
strcat(tx_buffer,":");
|
|
|
strcat(tx_buffer,iDDestino);
|
|
|
strcat(tx_buffer,":");
|
|
|
strcat(tx_buffer,iDSource);
|
|
|
strcat(tx_buffer,":");
|
|
|
strcat(tx_buffer,len);
|
|
|
strcat(tx_buffer,":");
|
|
|
strcat(tx_buffer,cmd);
|
|
|
strcat(tx_buffer,":");
|
|
|
strcat(tx_buffer,tx_data);
|
|
|
strcat(tx_buffer,":");
|
|
|
strcat(tx_buffer,crc);
|
|
|
strcat(tx_buffer,":");
|
|
|
strcat(tx_buffer,"quit");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
* Esta función genera el archivo de experimento a partir de la trama TCP recibida
|
|
|
*/
|
|
|
|
|
|
void recibe_experimento(char *p_data, char filename[]){
|
|
|
FILE *fd;
|
|
|
int i = 0;
|
|
|
|
|
|
while (*p_data != '\n'){
|
|
|
filename[i] = *p_data;
|
|
|
i++;
|
|
|
p_data++;
|
|
|
}
|
|
|
filename[i] = '\0';
|
|
|
p_data = p_data - i;
|
|
|
fd = fopen(filename,"w");
|
|
|
fprintf(fd, p_data);
|
|
|
fclose(fd);
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
* Esta funcion carga un archivo en un buffer que esta ubicado en memoria, luego
|
|
|
* este buffer es usado en la funcion "cambia_apuntamiento" para obtener el dato
|
|
|
* que sera usado en el cambio de apuntamiento.
|
|
|
*/
|
|
|
int carga_experimento(char *nombre_archivo){
|
|
|
|
|
|
FILE *Archivo_Fd;
|
|
|
|
|
|
char *cadena = (char *) malloc(25);
|
|
|
|
|
|
int longitud_cadena;
|
|
|
int num_bytes= 0;
|
|
|
int num_filas= 0;
|
|
|
|
|
|
Archivo_Fd = fopen(nombre_archivo,"r"); // Se procede a abrir el archivo, segun la ruta especificada
|
|
|
if(!Archivo_Fd){
|
|
|
ERROR("carga_archivo: No se pudo abrir el archivo!!! --> fopen()\n");
|
|
|
return -1;
|
|
|
}else{
|
|
|
|
|
|
while(!feof(Archivo_Fd)){ // Se procede a calcular la longitud del archivo para separar memoria
|
|
|
fgets(cadena,20,Archivo_Fd);
|
|
|
longitud_cadena= strlen(cadena);
|
|
|
cadena[longitud_cadena-1] = '\0';
|
|
|
num_bytes = num_bytes + longitud_cadena;
|
|
|
num_filas++;
|
|
|
}
|
|
|
|
|
|
rewind(Archivo_Fd); // Se reinicia el puntero del archivo
|
|
|
|
|
|
char *buffer_temporal = (char *) malloc(num_bytes+1); // Se separa espacio de memoria segun
|
|
|
// la longitud del archivo
|
|
|
fread(buffer_temporal, sizeof(char), num_bytes, Archivo_Fd);
|
|
|
|
|
|
char *puntero= strstr(buffer_temporal,".ab1"); // Se procede a eliminar la cabecera del archivo
|
|
|
puntero= puntero + 12;
|
|
|
|
|
|
buff_experimento = (char *) malloc(7*(num_filas-3)); // num_bytes_fila*(num_filas-3);
|
|
|
strncpy(buff_experimento,puntero,7*(num_filas-3)); // Se carga en memoria la informacion del archivo
|
|
|
|
|
|
fclose(Archivo_Fd);
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
* Esta funcion recibe un numero en formato char, el dato se transforma a su equivalente en
|
|
|
* un numero entero, que sera usado para sacar un dato del buffer "buff_experimento", esta
|
|
|
* dato es el valor que se enviara al sistema de conmutacion RF para el cambio de apunte a
|
|
|
* traves del puerto GPIO.
|
|
|
*/
|
|
|
int cambia_apuntamiento(char *puntero_char){
|
|
|
|
|
|
/*MSB-UP-LSB MSB-DOWN-LSB*/
|
|
|
int desplazamiento[6]={30,28,26,24,22,20}; // Defino los dezplazamientos que se aplicara
|
|
|
// al dato que ingresa para formar el número
|
|
|
// entero que se le pasara al puerto GPIO
|
|
|
// Estos números son los pines del puerto GPIO
|
|
|
// que se estan usando para el control
|
|
|
|
|
|
int puntero= atoi(puntero_char); // Se convierte a entero la direccion del puntero
|
|
|
|
|
|
int base= 7*puntero; // base= cantidad_bytes del dato x puntero
|
|
|
// cantidad de bytes es el numero de bytes que
|
|
|
printf("%s\n",puntero_char); // contiene cada dato, para este caso es 7
|
|
|
// porque es 6 bits de datos + 1 bit del cambio
|
|
|
// de linea.
|
|
|
char valor_char;
|
|
|
unsigned long valor;
|
|
|
unsigned long acumulado_ceros=0;
|
|
|
unsigned long acumulado_unos=0;
|
|
|
|
|
|
int offset; // Defino offset para el desplazamiento a traves
|
|
|
for(offset=0;offset<6;offset++){ // de cada dato que se obtiene del "buff_experimento"
|
|
|
|
|
|
valor_char= buff_experimento[base+offset]; // Obtengo el dato
|
|
|
|
|
|
if (valor_char == '0'){ // Obtengo el número acumulado segun sea un cero o un uno
|
|
|
valor= 0;
|
|
|
acumulado_ceros= acumulado_ceros + (1 << desplazamiento[offset]);
|
|
|
}else{
|
|
|
valor= 1;
|
|
|
acumulado_unos= acumulado_unos + (1 << desplazamiento[offset]);
|
|
|
}
|
|
|
}
|
|
|
pio_out(pioc, maskc_out, acumulado_unos, 1);
|
|
|
pio_out(pioc, maskc_out, acumulado_ceros, 0);
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
* Esta funcion lee "n" veces el estado del APUNTE actual y lo guarda en el
|
|
|
* archivo Verificacion.
|
|
|
*/
|
|
|
|
|
|
int chequeo_sistema(char *filename, char *numero_muestras){
|
|
|
|
|
|
char valor[7];
|
|
|
int i,cnt;
|
|
|
unsigned int entradac= 0;
|
|
|
FILE *fd;
|
|
|
fd=fopen(filename,"w");
|
|
|
fprintf(fd,"%s\n","Verificacion");
|
|
|
fprintf(fd,"%s",parameters.ID);
|
|
|
fprintf(fd,"%s\n","------------");
|
|
|
cnt=0;
|
|
|
do
|
|
|
{
|
|
|
//Inicializando arreglo
|
|
|
for(i=0;i<6;i++)
|
|
|
valor[i]='0';
|
|
|
|
|
|
valor[6]='\0';
|
|
|
|
|
|
//Lectura de puerto
|
|
|
entradac= pio_in(piob,maskb_in);
|
|
|
|
|
|
//Dandole formato al dato
|
|
|
if (!(entradac & bit_up_2))
|
|
|
valor[0] = '1';
|
|
|
if (!(entradac & bit_up_1))
|
|
|
valor[1] = '1';
|
|
|
if (!(entradac & bit_up_0))
|
|
|
valor[2] = '1';
|
|
|
if (!(entradac & bit_dow_2))
|
|
|
valor[3] = '1';
|
|
|
if (!(entradac & bit_dow_1))
|
|
|
valor[4] = '1';
|
|
|
if (!(entradac & bit_dow_0))
|
|
|
valor[5] = '1';
|
|
|
|
|
|
//Escribiendo en archivo
|
|
|
fprintf(fd,"%s\n",valor);
|
|
|
cnt=cnt+1;
|
|
|
usleep(1*1000*1000);
|
|
|
|
|
|
}while(cnt < atoi(numero_muestras));
|
|
|
|
|
|
fclose(fd);
|
|
|
|
|
|
return 7*atoi(numero_muestras) + 26 + 4 + 1; //incluye eof
|
|
|
}
|
|
|
|
|
|
char* File2buffer(char *filename, int n){
|
|
|
FILE *fd;
|
|
|
char* tx_data = (char *)malloc(n);
|
|
|
fd = fopen(filename,"r");
|
|
|
fread(tx_data, 1, n-1, fd);
|
|
|
fclose(fd);
|
|
|
tx_data = tx_data + n - 1;
|
|
|
*tx_data = '\0';
|
|
|
tx_data = tx_data - n + 1;
|
|
|
return tx_data;
|
|
|
}
|
|
|
|
|
|
cmp inicializa_modulo(cmp p){
|
|
|
FILE *fd = fopen("configuration.txt","r");
|
|
|
fgets(p.ID,20,fd);
|
|
|
fgets(p.param2,20,fd);
|
|
|
fgets(p.param3,20,fd);
|
|
|
fgets(p.param4,20,fd);
|
|
|
fclose(fd);
|
|
|
return p;
|
|
|
}
|
|
|
|