owlps/loc-bts/code/client/locclient.c

109 lines
2.9 KiB
C
Raw Normal View History

/*
* This file is part of the rtap localisation project.
*/
#include "../librtaputil/rtaputil.h"
/* Codes d'erreurs */
#define ERR_CREATING_SOCKET 1
#define ERR_BAD_NUMBER_OF_ARGS 2
#define ERR_SENDING_INFO 3
/* Affiche le mode d'emploi du programme */
void print_usage(char *prog)
{
printf("Usage :\n\
\t- Demande de localisation : %s ip_serveur\n\
\t- Requête de calibration : %s ip_serveur direction x y z\n\
", prog, prog) ;
}
int main(int argc, char *argv[])
{
struct timeval request_time ;
char *buf = NULL ;
int buf_offset ;
ssize_t nsent ; // Retour de sendto
struct sockaddr_in server, client ;
int sockfd ;
int buf_size ;
int i ;
gettimeofday(&request_time, NULL) ;
if(argc == 2) // Paquet normal
{
printf("Envoi normal effectué à : %lu\n", timeval_to_ms(request_time)) ;
buf_size = sizeof(char) + sizeof(struct timeval) ;
buf = malloc(buf_size) ;
buf[0] = PACKET_TYPE_NORMAL ; // Type de paquet = demande
memcpy(&buf[1], &request_time, sizeof(request_time)) ;
}
else if(argc == 6) // Paquet calibration
{
printf("Envoi Calibration effectué à : %lu\n", timeval_to_ms(request_time)) ;
buf_offset = 0 ;
buf_size = sizeof(char) * 2 + sizeof(struct timeval) + sizeof(float) * 3 ;
buf = malloc(buf_size) ;
buf[buf_offset++] = PACKET_TYPE_CALIBRATION ; // Type de paquet = calibration
memcpy(&buf[buf_offset], &request_time, sizeof(request_time)) ;
buf_offset += sizeof(request_time) ;
buf[buf_offset++] = atoi(argv[2]) ; // Direction
float posX = atof(argv[3]) ;
float posY = atof(argv[4]) ;
float posZ = atof(argv[5]) ;
memcpy(&buf[buf_offset], &posX, sizeof(float)) ;
buf_offset += sizeof(float) ;
memcpy(&buf[buf_offset], &posY, sizeof(float)) ;
buf_offset += sizeof(float) ;
memcpy(&buf[buf_offset], &posZ, sizeof(float)) ;
}
else
{
print_usage(argv[0]) ;
return ERR_BAD_NUMBER_OF_ARGS ;
}
/* Ouverture de la socket UDP vers le serveur d'aggrégation */
sockfd = create_udp_sending_socket(argv[1], AGGREGATE_DEFAULT_PORT, &server, &client) ;
if (sockfd < 0)
{
perror("Erreur ! Impossible de créer la socket vers le serveur d'aggrégation \n");
return ERR_CREATING_SOCKET ;
}
/* Envoi des infos au serveur d'aggrégation */
nsent = sendto(sockfd, (void *) buf, buf_size, 0, (struct sockaddr *) &server, (socklen_t) sizeof(server)) ;
if (nsent != (ssize_t) buf_size)
{
perror("Erreur lors de l'envoi des infos au serveur ") ;
return ERR_SENDING_INFO ;
}
if (argc == 6)
for (i = 0 ; i < 19 ; i++)
{
nsent = sendto(sockfd, (void *) buf, buf_size, 0, (struct sockaddr *) &server, (socklen_t) sizeof(server)) ;
if (nsent != (ssize_t) buf_size)
{
perror("Erreur lors de l'envoi des infos au serveur ") ;
return ERR_SENDING_INFO ;
}
}
(void) close(sockfd) ;
return 0 ;
}