[plane] fix *** stack smashing detected *** (because time_t for random) + removed serializer
This commit is contained in:
parent
d2afb8f65e
commit
61581e393d
|
@ -9,12 +9,9 @@ default: all
|
||||||
lib/network/tcp/client.o: lib/header.h lib/network/tcp/client.h lib/network/tcp/client.c
|
lib/network/tcp/client.o: lib/header.h lib/network/tcp/client.h lib/network/tcp/client.c
|
||||||
gcc $(CFLAGS) -c -o lib/network/tcp/client.o lib/network/tcp/client.c
|
gcc $(CFLAGS) -c -o lib/network/tcp/client.o lib/network/tcp/client.c
|
||||||
|
|
||||||
lib/network/format/serializer.o: lib/network/format/serializer.h lib/network/format/serializer.c
|
|
||||||
gcc $(CFLAGS) -c -o lib/network/format/serializer.o lib/network/format/serializer.c
|
|
||||||
|
|
||||||
# Compiles the Plane
|
# Compiles the Plane
|
||||||
boot: lib/network/tcp/client.o lib/network/format/serializer.o plane.h plane.c
|
boot: lib/network/tcp/client.o plane.h plane.c
|
||||||
gcc $(CFLAGS) -o boot lib/network/tcp/client.o lib/network/format/serializer.o plane.c -lm
|
gcc $(CFLAGS) -o boot lib/network/tcp/client.o plane.c -lm
|
||||||
|
|
||||||
|
|
||||||
# Run full compilation
|
# Run full compilation
|
||||||
|
|
|
@ -22,8 +22,8 @@
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
/* vars */
|
/* vars */
|
||||||
#define SRV_HOST "localhost"
|
#define SRV_HOST "127.0.0.1"
|
||||||
#define SRV_PORT 4444
|
#define SRV_PORT 0x504c
|
||||||
|
|
||||||
#define MAX_BUF_LEN 512
|
#define MAX_BUF_LEN 512
|
||||||
|
|
||||||
|
|
|
@ -1,64 +0,0 @@
|
||||||
#include "serializer.h"
|
|
||||||
|
|
||||||
|
|
||||||
int parse_plane_request(const char* pIn, struct plane_request* pOut){
|
|
||||||
|
|
||||||
/* 1. Check buffer length */
|
|
||||||
if( strlen(pIn)*sizeof(char) != sizeof(struct plane_request) )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
/* 2. Parse buffer */
|
|
||||||
memcpy(pOut, pIn, (size_t) sizeof(struct plane_request));
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int parse_viewterm_request(const char* pIn, struct vterm_request* pOut){
|
|
||||||
|
|
||||||
/* 1. Check buffer length */
|
|
||||||
if( strlen(pIn)*sizeof(char) != sizeof(struct vterm_request) )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
/* 2. Parse buffer */
|
|
||||||
memcpy(pOut, pIn, sizeof(struct vterm_request));
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int parse_ctrlterm_request(const char* pIn, struct cterm_request* pOut){
|
|
||||||
|
|
||||||
/* 1. Check buffer length */
|
|
||||||
if( strlen(pIn)*sizeof(char) != sizeof(struct cterm_request) )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
/* 2. Parse buffer */
|
|
||||||
memcpy(pOut, pIn, sizeof(struct cterm_request));
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int serialize_term_response(const struct term_response* pIn, char* pOut, const size_t pSize){
|
|
||||||
|
|
||||||
/* 1. Check buffer length */
|
|
||||||
if( sizeof(struct term_response) > pSize*sizeof(char) )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
/* 2. Serialize response into buffer */
|
|
||||||
memcpy(pOut, pIn, sizeof(struct term_response));
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,65 +0,0 @@
|
||||||
#ifndef _LIB_NETWORK_FORMAT_SERIALIZER_H_
|
|
||||||
#define _LIB_NETWORK_FORMAT_SERIALIZER_H_
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include "../../../plane.h"
|
|
||||||
|
|
||||||
struct plane_request{ // Plane Request
|
|
||||||
struct coord co;
|
|
||||||
struct control ct;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct vterm_request{ // ViewTerminal Request
|
|
||||||
char flags;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct term_response{ // Terminal-s Response
|
|
||||||
char flags;
|
|
||||||
struct coord co;
|
|
||||||
struct control ct;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct cterm_request{ // ControlTerminal Request
|
|
||||||
char flags;
|
|
||||||
int z;
|
|
||||||
struct control ct;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Parse a plane request ('char*' to 'struct plane_request')
|
|
||||||
*
|
|
||||||
* @history
|
|
||||||
* [1] Check if buffer have correct size (according to destination struct)
|
|
||||||
* [2] Parse buffer to struct
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
int parse_plane_request(const char* pIn, struct plane_request* pOut);
|
|
||||||
|
|
||||||
/* Parse a viewTerminal request ('char*' to 'struct vt_request')
|
|
||||||
*
|
|
||||||
* @history
|
|
||||||
* [1] Check if buffer have correct size (according to destination struct)
|
|
||||||
* [2] Parse buffer to struct
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
int parse_viewterm_request(const char* pIn, struct vterm_request* pOut);
|
|
||||||
|
|
||||||
/* Parse a ctrlTerminal request ('char*' to 'struct ct_request')
|
|
||||||
*
|
|
||||||
* @history
|
|
||||||
* [1] Check if buffer have correct size (according to destination struct)
|
|
||||||
* [2] Parse buffer to struct
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
int parse_ctrlterm_request(const char* pIn, struct cterm_request* pOut);
|
|
||||||
|
|
||||||
/* Serialize a Terminal response ('struct t_response' to 'char*')
|
|
||||||
*
|
|
||||||
* @history
|
|
||||||
* [1] Check if buffer have correct size (according to input struct)
|
|
||||||
* [2] Serialize struct into buffer
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
int serialize_term_response(const struct term_response* pIn, char* pOut, const size_t pSize);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -51,11 +51,11 @@ int open_communication(){
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fermer_communication(){
|
void fermer_communication(){
|
||||||
// fonction à implémenter qui permet de fermer la communication
|
// fonction à implémenter qui permet de fermer la communication
|
||||||
// avec le gestionnaire de vols
|
// avec le gestionnaire de vols
|
||||||
}
|
}
|
||||||
|
|
||||||
void envoyer_caracteristiques(){
|
void envoyer_caracteristiques(){
|
||||||
// fonction à implémenter qui envoie l'ensemble des caractéristiques
|
// fonction à implémenter qui envoie l'ensemble des caractéristiques
|
||||||
// courantes de l'plane au gestionnaire de vols
|
// courantes de l'plane au gestionnaire de vols
|
||||||
|
@ -68,19 +68,19 @@ void envoyer_caracteristiques(){
|
||||||
// initialise aléatoirement les paramètres initiaux de l'plane
|
// initialise aléatoirement les paramètres initiaux de l'plane
|
||||||
void init_plane(){
|
void init_plane(){
|
||||||
// initialisation aléatoire du compteur aléatoire
|
// initialisation aléatoire du compteur aléatoire
|
||||||
unsigned int seed;
|
time_t seed;
|
||||||
time( (time_t*) &seed);
|
time( &seed);
|
||||||
srandom(seed);
|
srandom(seed);
|
||||||
|
|
||||||
// intialisation des paramètres de l'plane
|
// intialisation des paramètres de l'plane
|
||||||
crd.x = 1000 + random() % 1000;
|
crd.x = 1000 + random() % 1000;
|
||||||
crd.y = 1000 + random() % 1000;
|
crd.y = 1000 + random() % 1000;
|
||||||
crd.z = 900 + random() % 100;
|
crd.z = 900 + random() % 100;
|
||||||
|
|
||||||
ctrl.cap = random() % 360;
|
ctrl.cap = random() % 360;
|
||||||
ctrl.speed = 600 + random() % 200;
|
ctrl.speed = 600 + random() % 200;
|
||||||
|
|
||||||
// initialisation du numero de l'plane : chaine de 5 caractères
|
// initialisation du numero de l'plane : chaine de 5 caractères
|
||||||
// formée de 2 lettres puis 3 chiffres
|
// formée de 2 lettres puis 3 chiffres
|
||||||
numero_vol[0] = (random() % 26) + 'A';
|
numero_vol[0] = (random() % 26) + 'A';
|
||||||
numero_vol[1] = (random() % 26) + 'A';
|
numero_vol[1] = (random() % 26) + 'A';
|
||||||
|
@ -100,7 +100,7 @@ void update_cap(int cap){
|
||||||
|
|
||||||
// modifie l'z de l'plane avec la valeur passée en paramètre
|
// modifie l'z de l'plane avec la valeur passée en paramètre
|
||||||
void update_z(int alt){
|
void update_z(int alt){
|
||||||
if (alt < 0)
|
if (alt < 0)
|
||||||
crd.z = alt;
|
crd.z = alt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ void display_data(){
|
||||||
// recalcule la localisation de l'plane en fonction de sa speed et de son cap
|
// recalcule la localisation de l'plane en fonction de sa speed et de son cap
|
||||||
void calc_ctrl(){
|
void calc_ctrl(){
|
||||||
float ctrl_x, ctrl_y;
|
float ctrl_x, ctrl_y;
|
||||||
|
|
||||||
if (ctrl.speed < VITMIN){
|
if (ctrl.speed < VITMIN){
|
||||||
fermer_communication();
|
fermer_communication();
|
||||||
exit(2);
|
exit(2);
|
||||||
|
@ -122,7 +122,7 @@ void calc_ctrl(){
|
||||||
fermer_communication();
|
fermer_communication();
|
||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
ctrl_x = cos(ctrl.cap * 2 * M_PI / 360) * ctrl.speed * 10 / VITMIN;
|
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;
|
ctrl_y = sin(ctrl.cap * 2 * M_PI / 360) * ctrl.speed * 10 / VITMIN;
|
||||||
|
|
||||||
|
@ -130,14 +130,14 @@ void calc_ctrl(){
|
||||||
// sauf si cap est un des angles droit
|
// 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;
|
||||||
if ((ctrl_x < 0) && (ctrl_x > -1)) ctrl_x = -1;
|
if ((ctrl_x < 0) && (ctrl_x > -1)) ctrl_x = -1;
|
||||||
|
|
||||||
if ((ctrl_y > 0) && (ctrl_y < 1)) ctrl_y = 1;
|
if ((ctrl_y > 0) && (ctrl_y < 1)) ctrl_y = 1;
|
||||||
if ((ctrl_y < 0) && (ctrl_y > -1)) ctrl_y = -1;
|
if ((ctrl_y < 0) && (ctrl_y > -1)) ctrl_y = -1;
|
||||||
|
|
||||||
|
|
||||||
crd.x = crd.x + (int)ctrl_x;
|
crd.x = crd.x + (int)ctrl_x;
|
||||||
crd.y = crd.y + (int)ctrl_y;
|
crd.y = crd.y + (int)ctrl_y;
|
||||||
|
|
||||||
display_data();
|
display_data();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,9 +154,9 @@ void update(){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
// on initialise le plane
|
// on initialise le plane
|
||||||
init_plane();
|
init_plane();
|
||||||
|
|
||||||
display_data();
|
display_data();
|
||||||
|
|
||||||
|
@ -167,5 +167,5 @@ int main(){
|
||||||
}
|
}
|
||||||
|
|
||||||
// on se déplace une fois toutes les initialisations faites
|
// on se déplace une fois toutes les initialisations faites
|
||||||
update();
|
update();
|
||||||
}
|
}
|
Loading…
Reference in New Issue