2017-04-11 17:12:34 +00:00
|
|
|
/**************************
|
|
|
|
* Plane *
|
|
|
|
***************************
|
|
|
|
* Designed & Developed by *
|
|
|
|
* Adrien Marquès *
|
|
|
|
* <xdrm-brackets> *
|
|
|
|
***************************
|
|
|
|
* doowap31@gmail.com *
|
|
|
|
**************************/
|
2017-04-03 16:04:57 +00:00
|
|
|
#include "plane.h"
|
|
|
|
|
2017-04-11 17:12:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
// numéro de vol de l'plane : code sur 5 caractères
|
|
|
|
char numero_vol[6];
|
|
|
|
|
|
|
|
struct coord crd;
|
|
|
|
struct control ctrl;
|
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
int mcast_socket = -1;
|
|
|
|
int commu_socket = -1;
|
2017-04-03 16:04:57 +00:00
|
|
|
char buffer[MAX_BUF_LEN] = {0};
|
2017-04-20 09:49:11 +00:00
|
|
|
struct sockaddr_in sgca;
|
2017-04-11 17:12:34 +00:00
|
|
|
|
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
|
|
|
|
int open_communication(){
|
|
|
|
/* 0. Initialisation des variables */
|
2017-04-20 09:49:11 +00:00
|
|
|
struct sockaddr_in udp; // données des sockets
|
|
|
|
char buffer[MAX_BUF_LEN] = {0}; // buffer
|
2017-04-08 17:25:44 +00:00
|
|
|
int status;
|
2017-04-12 07:57:59 +00:00
|
|
|
struct bind_header request;
|
2017-04-26 12:25:05 +00:00
|
|
|
struct in_addr ip; // pour afficher l'ip en dot notation
|
2017-04-08 17:25:44 +00:00
|
|
|
|
|
|
|
/* [1] Socket Multicast UDP
|
|
|
|
=========================================================*/
|
|
|
|
|
|
|
|
|
2017-04-18 18:14:50 +00:00
|
|
|
/* (1) Création socket multicast client* (écoute)
|
2017-04-08 17:25:44 +00:00
|
|
|
---------------------------------------------------------*/
|
2017-04-18 08:36:32 +00:00
|
|
|
if( DROP_MULTICAST_SERVER(MCST_HOST, MCST_PORT, &mcast_socket, &udp) < 0 )
|
2017-04-08 17:25:44 +00:00
|
|
|
return 0;
|
|
|
|
|
2017-04-18 18:14:50 +00:00
|
|
|
// *un client multicast est en fait un serveur UDP, mais le terme persiste car le service est délivré par le CLIENT UDP
|
2017-04-08 17:25:44 +00:00
|
|
|
|
2017-04-18 18:14:50 +00:00
|
|
|
|
|
|
|
/* (2) Attente réponse SGCA -> adresse+port TCP
|
2017-04-08 17:25:44 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* 1. Attente réponse */
|
|
|
|
status = recv(mcast_socket, buffer, MAX_BUF_LEN, 0);
|
2017-04-03 16:04:57 +00:00
|
|
|
|
2017-04-08 17:25:44 +00:00
|
|
|
/* 2. Gestion erreur */
|
|
|
|
if( status < 0 ){
|
|
|
|
close(mcast_socket);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-03 16:04:57 +00:00
|
|
|
|
2017-04-08 17:25:44 +00:00
|
|
|
/* 3. Vérification taille */
|
2017-04-12 07:57:59 +00:00
|
|
|
if( status < BINDHDR_LEN ){
|
2017-04-08 17:25:44 +00:00
|
|
|
close(mcast_socket);
|
2017-04-03 16:04:57 +00:00
|
|
|
return 0;
|
2017-04-08 17:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 4. On récupère les données */
|
2017-04-12 07:57:59 +00:00
|
|
|
bzero(&request, sizeof(struct bind_header));
|
2017-04-12 07:30:42 +00:00
|
|
|
|
2017-04-26 12:25:05 +00:00
|
|
|
memcpy(&request.flags, buffer, sizeof(char) );
|
|
|
|
memcpy(&request.addr, buffer+sizeof(char), sizeof(in_addr_t) );
|
|
|
|
memcpy(&request.port, buffer+sizeof(char)+sizeof(in_addr_t), sizeof(unsigned short) );
|
|
|
|
|
2017-04-18 18:14:50 +00:00
|
|
|
// on indian-switch
|
2017-04-26 12:25:05 +00:00
|
|
|
request.addr = ntohl(request.addr);
|
2017-04-18 18:14:50 +00:00
|
|
|
request.port = ntohs(request.port);
|
2017-04-26 12:25:05 +00:00
|
|
|
ip.s_addr = request.addr;
|
2017-04-12 07:57:59 +00:00
|
|
|
|
2017-04-25 09:54:15 +00:00
|
|
|
/* 5. On vérifie les flags */
|
|
|
|
if( !(request.flags&BINDHEAD_TCP) || request.flags&BINDHEAD_UDP ){
|
|
|
|
close(mcast_socket);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-20 09:49:11 +00:00
|
|
|
// printf("* hex("); for( int i = 0 ; i < BINDHDR_LEN/sizeof(char) ; i++ ) printf("\\x%02X", buffer[i]); printf(")\n");
|
2017-04-28 12:54:02 +00:00
|
|
|
printf("* received bind_header{flags = %d; addr = %x/'%s'; port = %d;}\n\n", request.flags, request.addr, inet_ntoa(ip), request.port);
|
2017-04-08 17:25:44 +00:00
|
|
|
|
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-08 17:25:44 +00:00
|
|
|
/* [2] Socket communication TCP
|
|
|
|
=========================================================*/
|
2017-04-03 16:04:57 +00:00
|
|
|
|
2017-04-08 17:25:44 +00:00
|
|
|
/* (1) Création socket TCP + connection
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* 1. Création socket TCP + connection serveur */
|
2017-04-28 12:54:02 +00:00
|
|
|
if( TCP_CONNECT(&commu_socket, request.addr, request.port, PAUSE, &sgca) < 0 )
|
2017-04-03 16:04:57 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:25:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-11 17:12:34 +00:00
|
|
|
void close_communication(){
|
2017-04-03 16:04:57 +00:00
|
|
|
// fonction à implémenter qui permet de fermer la communication
|
|
|
|
// avec le gestionnaire de vols
|
|
|
|
}
|
2017-04-08 16:37:14 +00:00
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-29 15:04:07 +00:00
|
|
|
|
2017-04-11 17:12:34 +00:00
|
|
|
void send_data(){
|
2017-04-08 17:25:44 +00:00
|
|
|
|
|
|
|
/* [0] Initialisation des variables
|
|
|
|
=========================================================*/
|
2017-04-12 08:32:48 +00:00
|
|
|
char buffer[MAX_BUF_LEN] = {0};
|
|
|
|
struct plane_data request;
|
|
|
|
int read;
|
2017-04-08 17:25:44 +00:00
|
|
|
|
|
|
|
|
2017-04-12 07:57:59 +00:00
|
|
|
/* [1] Envoi des caractéristiques
|
|
|
|
=========================================================*/
|
|
|
|
/* 1. Vérification socket */
|
|
|
|
if( commu_socket < 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* 2. création objet */
|
|
|
|
strcpy(request.code, numero_vol);
|
2017-04-25 14:34:04 +00:00
|
|
|
request.x = htonl( crd.x );
|
|
|
|
request.y = htonl( crd.y );
|
|
|
|
request.z = htonl( crd.z );
|
|
|
|
request.cap = htonl( ctrl.cap );
|
|
|
|
request.spd = htonl( ctrl.speed );
|
2017-04-12 07:57:59 +00:00
|
|
|
|
|
|
|
/* 3. Copie buffer */
|
2017-04-29 15:04:07 +00:00
|
|
|
buffer[0] = 0x55;
|
|
|
|
memcpy(buffer+sizeof(char)*1+sizeof(int)*0, &request.code, sizeof(char)*6 );
|
|
|
|
memcpy(buffer+sizeof(char)*7+sizeof(int)*0, &request.x, sizeof(int) );
|
|
|
|
memcpy(buffer+sizeof(char)*7+sizeof(int)*1, &request.y, sizeof(int) );
|
|
|
|
memcpy(buffer+sizeof(char)*7+sizeof(int)*2, &request.z, sizeof(int) );
|
|
|
|
memcpy(buffer+sizeof(char)*7+sizeof(int)*3, &request.cap, sizeof(int) );
|
|
|
|
memcpy(buffer+sizeof(char)*7+sizeof(int)*4, &request.spd, sizeof(int) );
|
2017-04-12 07:57:59 +00:00
|
|
|
|
|
|
|
read = send(commu_socket, buffer, PLANE_DATA_LEN/sizeof(char) + 1, 0);
|
|
|
|
|
2017-04-20 09:49:11 +00:00
|
|
|
if( read <= 0 ){
|
2017-04-28 12:54:02 +00:00
|
|
|
printf("Cannot send\n");
|
2017-04-12 07:57:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-20 09:49:11 +00:00
|
|
|
// printf("* hex("); for( int i = 0 ; i < PLANE_DATA_LEN/sizeof(char) ; i++ ) printf("\\x%02X", buffer[i]); printf(")\n");
|
2017-04-08 17:25:44 +00:00
|
|
|
|
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************
|
|
|
|
*** Fonctions gérant le déplacement de l'plane : ne pas modifier
|
|
|
|
********************************/
|
|
|
|
|
|
|
|
// initialise aléatoirement les paramètres initiaux de l'plane
|
2017-04-28 12:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
void init_plane(){
|
|
|
|
// initialisation aléatoire du compteur aléatoire
|
2017-04-08 16:37:14 +00:00
|
|
|
time_t seed;
|
|
|
|
time( &seed);
|
2017-04-03 16:04:57 +00:00
|
|
|
srandom(seed);
|
|
|
|
|
|
|
|
// intialisation des paramètres de l'plane
|
|
|
|
crd.x = 1000 + random() % 1000;
|
|
|
|
crd.y = 1000 + random() % 1000;
|
|
|
|
crd.z = 900 + random() % 100;
|
2017-04-08 16:37:14 +00:00
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
ctrl.cap = random() % 360;
|
|
|
|
ctrl.speed = 600 + random() % 200;
|
2017-04-08 16:37:14 +00:00
|
|
|
|
|
|
|
// initialisation du numero de l'plane : chaine de 5 caractères
|
2017-04-03 16:04:57 +00:00
|
|
|
// formée de 2 lettres puis 3 chiffres
|
|
|
|
numero_vol[0] = (random() % 26) + 'A';
|
|
|
|
numero_vol[1] = (random() % 26) + 'A';
|
2017-04-12 07:57:59 +00:00
|
|
|
numero_vol[2] = (random() % 10) + '0';
|
|
|
|
numero_vol[3] = (random() % 10) + '0';
|
|
|
|
numero_vol[4] = (random() % 10) + '0';
|
2017-04-03 16:04:57 +00:00
|
|
|
numero_vol[5] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// modifie la valeur de l'plane avec la valeur passée en paramètre
|
2017-04-28 12:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int update_speed(int speed){
|
|
|
|
if( speed < 0 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if( speed > VITMAX )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ctrl.speed = speed;
|
|
|
|
return 0;
|
2017-04-03 16:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// modifie le cap de l'plane avec la valeur passée en paramètre
|
2017-04-28 12:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int update_cap(int cap){
|
|
|
|
if( cap < 0 || cap >= 360 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ctrl.cap = cap;
|
|
|
|
return 0;
|
2017-04-03 16:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// modifie l'z de l'plane avec la valeur passée en paramètre
|
2017-04-28 12:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int update_z(int alt){
|
|
|
|
if( alt < 0 )
|
|
|
|
return 01;
|
|
|
|
|
|
|
|
crd.z = alt;
|
|
|
|
return 0;
|
2017-04-03 16:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// affiche les caractéristiques courantes de l'plane
|
2017-04-28 12:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
void display_data(){
|
2017-04-28 12:54:02 +00:00
|
|
|
printf("| Plane %s |\n * position(%d,%d, %d)\n * speed: %d\n * cap: %d\n\n", numero_vol, crd.x, crd.y, crd.z, ctrl.speed, ctrl.cap);
|
2017-04-03 16:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// recalcule la localisation de l'plane en fonction de sa speed et de son cap
|
2017-04-28 12:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
void calc_ctrl(){
|
|
|
|
float ctrl_x, ctrl_y;
|
2017-04-08 16:37:14 +00:00
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
if (ctrl.speed < VITMIN){
|
2017-04-11 17:12:34 +00:00
|
|
|
close_communication();
|
2017-04-03 16:04:57 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
if (crd.z == 0){
|
2017-04-11 17:12:34 +00:00
|
|
|
close_communication();
|
2017-04-03 16:04:57 +00:00
|
|
|
exit(3);
|
|
|
|
}
|
2017-04-08 16:37:14 +00:00
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
ctrl_x = cos(ctrl.cap * 2 * M_PI / 360) * ctrl.speed * 10 / VITMIN;
|
|
|
|
ctrl_y = sin(ctrl.cap * 2 * M_PI / 360) * ctrl.speed * 10 / VITMIN;
|
|
|
|
|
|
|
|
// on se déplace d'au moins une case quels que soient le cap et la speed
|
|
|
|
// sauf si cap est un des angles droit
|
|
|
|
if ((ctrl_x > 0) && (ctrl_x < 1)) ctrl_x = 1;
|
|
|
|
if ((ctrl_x < 0) && (ctrl_x > -1)) ctrl_x = -1;
|
2017-04-08 16:37:14 +00:00
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
if ((ctrl_y > 0) && (ctrl_y < 1)) ctrl_y = 1;
|
|
|
|
if ((ctrl_y < 0) && (ctrl_y > -1)) ctrl_y = -1;
|
2017-04-08 16:37:14 +00:00
|
|
|
|
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
crd.x = crd.x + (int)ctrl_x;
|
|
|
|
crd.y = crd.y + (int)ctrl_y;
|
2017-04-08 16:37:14 +00:00
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
display_data();
|
|
|
|
}
|
|
|
|
|
|
|
|
// fonction principale : gère l'exécution de l'plane au fil du temps
|
2017-04-28 12:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
void update(){
|
2017-04-28 12:54:02 +00:00
|
|
|
int received;
|
|
|
|
struct sgca_req request;
|
|
|
|
char buffer[MAX_BUF_LEN] = {0};
|
2017-04-03 16:04:57 +00:00
|
|
|
|
|
|
|
while( 1 ){
|
|
|
|
|
|
|
|
calc_ctrl();
|
2017-04-11 17:12:34 +00:00
|
|
|
send_data();
|
2017-04-28 12:54:02 +00:00
|
|
|
|
|
|
|
/* [1] Gestion réception avec timeout
|
|
|
|
=========================================================*/
|
|
|
|
/* 1. Attente de message */
|
|
|
|
received = recv(commu_socket, buffer, MAX_BUF_LEN, 0);
|
|
|
|
|
|
|
|
/* 2. Si rien -> on reboucle */
|
2017-04-29 15:04:07 +00:00
|
|
|
if( received != (sizeof(char)+3*sizeof(int)) )
|
2017-04-28 12:54:02 +00:00
|
|
|
continue;
|
|
|
|
|
2017-04-29 15:04:07 +00:00
|
|
|
printf("** Received update request **\n");
|
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
|
|
|
|
/* [2] Gestion de requête de mise à jour
|
|
|
|
=========================================================*/
|
|
|
|
/* 1. On parse la requête */
|
|
|
|
memcpy(&request.flags, buffer, sizeof(char) );
|
|
|
|
memcpy(&request.z, buffer+sizeof(char), sizeof(int) );
|
|
|
|
memcpy(&request.cap, buffer+sizeof(char)+sizeof(int), sizeof(int) );
|
|
|
|
memcpy(&request.spd, buffer+sizeof(char)+sizeof(int)*2, sizeof(int) );
|
|
|
|
|
|
|
|
/* 2. On indian-switch */
|
|
|
|
request.z = ntohl(request.z);
|
|
|
|
request.cap = ntohl(request.cap);
|
|
|
|
request.spd = ntohl(request.spd);
|
|
|
|
printf("Request received { flags = %d; z = %d; cap = %d; speed = %d }\n", request.flags, request.z, request.cap, request.spd);
|
|
|
|
|
|
|
|
/* 3. On essaie de mettre à jour en fonction des flags */
|
|
|
|
if( request.flags&REQ_ALT )
|
|
|
|
if( update_z(request.z) == -1 )
|
|
|
|
request.flags -= REQ_ALT;
|
|
|
|
|
|
|
|
if( request.flags&REQ_SPD )
|
2017-04-29 15:04:07 +00:00
|
|
|
if( update_speed(request.spd) == -1 )
|
2017-04-28 12:54:02 +00:00
|
|
|
request.flags -= REQ_SPD;
|
|
|
|
|
|
|
|
if( request.flags&REQ_CAP )
|
2017-04-29 15:04:07 +00:00
|
|
|
if( update_cap(request.cap) == -1 )
|
2017-04-28 12:54:02 +00:00
|
|
|
request.flags -= REQ_CAP;
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Gestion de la réponse
|
|
|
|
=========================================================*/
|
|
|
|
/* 1. On prépare la réponse */
|
2017-04-29 15:04:07 +00:00
|
|
|
buffer[0] = 0;
|
|
|
|
buffer[1] = request.flags;
|
|
|
|
printf("Response flags : %d\n", request.flags);
|
2017-04-28 12:54:02 +00:00
|
|
|
|
|
|
|
/* 2. Envoi de la réponse */
|
2017-04-29 15:04:07 +00:00
|
|
|
if( send(commu_socket, buffer, sizeof(char)*2, 0) <= 0 ){
|
|
|
|
printf("Cannot send response\n");
|
|
|
|
}
|
2017-04-03 16:04:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-08 16:37:14 +00:00
|
|
|
int main(){
|
2017-04-03 16:04:57 +00:00
|
|
|
// on initialise le plane
|
2017-04-08 16:37:14 +00:00
|
|
|
init_plane();
|
2017-04-03 16:04:57 +00:00
|
|
|
|
|
|
|
display_data();
|
|
|
|
|
|
|
|
// on quitte si on arrive à pas contacter le gestionnaire de vols
|
|
|
|
if( !open_communication() ){
|
2017-04-28 12:54:02 +00:00
|
|
|
printf("Cannot connect to SGCA.\n");
|
2017-04-03 16:04:57 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// on se déplace une fois toutes les initialisations faites
|
2017-04-08 16:37:14 +00:00
|
|
|
update();
|
2017-04-03 16:04:57 +00:00
|
|
|
}
|