diff options
-rw-r--r-- | inc/car_connection_manager.h | 8 | ||||
-rw-r--r-- | inc/model/model_car_connection.h | 1 | ||||
-rw-r--r-- | inc/model/model_cloud_connection.h | 1 | ||||
-rw-r--r-- | src/car_connection_manager.c | 67 | ||||
-rw-r--r-- | src/controller/controller_name_input.c | 5 | ||||
-rw-r--r-- | src/messages/message_factory.c | 5 | ||||
-rw-r--r-- | src/model/model_car_connection.c | 20 | ||||
-rw-r--r-- | src/model/model_cloud_connection.c | 13 |
8 files changed, 94 insertions, 26 deletions
diff --git a/inc/car_connection_manager.h b/inc/car_connection_manager.h index 03ee807..de82f83 100644 --- a/inc/car_connection_manager.h +++ b/inc/car_connection_manager.h @@ -78,11 +78,17 @@ void car_connection_manager_handle_message(message_t *message); /** * @brief Sends data about driving. - * @param[in] command command to be + * @param[in] command Command to be sent. */ int car_connection_manager_send_command(command_s command); /** + * @brief Sends user name. + * @param[in] name User name to be sent. + */ +int car_connection_manager_send_user_name(const char *name); + +/** * @brief Destroys connection manager. */ void car_connection_manager_fini(); diff --git a/inc/model/model_car_connection.h b/inc/model/model_car_connection.h index 26ccc22..bdcb3ec 100644 --- a/inc/model/model_car_connection.h +++ b/inc/model/model_car_connection.h @@ -41,5 +41,6 @@ void model_car_connection_set_stop(bool stop); void model_car_connection_send_cam_elevation(float cam_elevation); void model_car_connection_send_cam_azimuth(float cam_azimuth); bool model_car_connection_is_connected(); +bool model_car_connection_player_name_set(const char *player_name); #endif /* MODEL_MODEL_CAR_CONNECTION_H_ */ diff --git a/inc/model/model_cloud_connection.h b/inc/model/model_cloud_connection.h index b79aae7..cfb9a4e 100644 --- a/inc/model/model_cloud_connection.h +++ b/inc/model/model_cloud_connection.h @@ -35,7 +35,6 @@ void model_cloud_connection_init(void); void model_cloud_connection_subscribe_event(t_model_cloud_connection_update_cb model_update_cb); void model_cloud_connection_unsubscirbe_event(void); void model_cloud_connection_subscribe_reconnect(t_model_cloud_connection_update_cb reconnect_cb); -bool model_cloud_connection_player_name_set(const char *player_name); void model_cloud_connection_try_reconnect(void); void model_cloud_connection_model_state_change(void); void model_cloud_connection_start_get_names(t_model_cloud_connection_names_cb callback); diff --git a/src/car_connection_manager.c b/src/car_connection_manager.c index 0d6ff09..f8efffb 100644 --- a/src/car_connection_manager.c +++ b/src/car_connection_manager.c @@ -24,13 +24,22 @@ #include "messages/message_manager.h" #include "messages/message_factory.h" #include "messages/message_ack.h" +#include "messages/message_config_user_name.h" #define KEEP_ALIVE_ATTEMPTS 5 #define HELLO_START_ATTEMPTS 5 #define KEEP_ALIVE_INTERVAL 1000 //In ms -#define HELLO_INTERVAL 1000 //In ms - -#define SAFE_SOURCE_REMOVE(source) do{if(source) { _W("Removing source no %d", source) ;g_source_remove(source); } source = 0;}while(0) +#define HELLO_INTERVAL 5000 //In ms +#define USER_NAME_INTERVAL 3000 //In ms + +#define SAFE_SOURCE_REMOVE(source) \ +do { \ + if(source) \ + { \ + g_source_remove(source); \ + } \ + source = 0; \ +}while(0) typedef struct _car_connection_manager_info { car_connection_state_e state; @@ -41,7 +50,9 @@ typedef struct _car_connection_manager_info { unsigned int hello_attempts_left; guint keep_alive_timer; guint hello_timer; + guint user_name_timer; message_factory_t *message_factory; + unsigned long long int user_name_serial; unsigned long long int last_acked; } _car_connection_manager_s; @@ -62,6 +73,8 @@ static void _receive_cb(message_t *message, void *data); static gboolean _send_hello(); static void _reset_counters(); static gboolean _send_keep_alive(); +static gboolean _send_user_name(); +static int _start_user_name_timer(); static gboolean _try_connect_cb(gpointer data); static gboolean _keep_alive_cb(gpointer data); static int _addr_cmp(const char *addr1, int port1, const char *addr2, int port2); @@ -139,6 +152,7 @@ void car_connection_manager_disconnect() s_info.car_port = -1; SAFE_SOURCE_REMOVE(s_info.hello_timer); SAFE_SOURCE_REMOVE(s_info.keep_alive_timer); + SAFE_SOURCE_REMOVE(s_info.user_name_timer); _set_state(CAR_CONNECTION_STATE_DISCONNECTED); } @@ -169,6 +183,10 @@ void car_connection_manager_handle_message(message_t *message) case MESSAGE_ACK: if(s_info.state == CAR_CONNECTION_STATE_CONNECTED) { unsigned long long int serial = message_ack_get_ack_serial((message_ack_t*)message); + if(serial == s_info.user_name_serial) { + SAFE_SOURCE_REMOVE(s_info.user_name_timer); + break; + } if(serial > s_info.last_acked) { _D("Received KEEP_ALIVE_ACK"); s_info.keep_alive_attempts_left = KEEP_ALIVE_ATTEMPTS; @@ -220,6 +238,17 @@ int car_connection_manager_send_command(command_s command) return 0; } +int car_connection_manager_send_user_name(const char *name) +{ + if(s_info.state != CAR_CONNECTION_STATE_CONNECTED) { + _E("Connection not established, user name not sent"); + return -1; + + } + _start_user_name_timer(name); + return 0; +} + void car_connection_manager_fini() { if(s_info.state == CAR_CONNECTION_STATE_CONNECTED || s_info.state == CAR_CONNECTION_STATE_CONNECTING) @@ -280,7 +309,6 @@ static gboolean _send_keep_alive() { if(s_info.state == CAR_CONNECTION_STATE_DISCONNECTED) { s_info.keep_alive_attempts_left = 0; - s_info.keep_alive_timer = 0; return FALSE; } @@ -309,6 +337,30 @@ static gboolean _send_keep_alive() return TRUE; } +static gboolean _send_user_name(gpointer data) +{ + if(s_info.state != CAR_CONNECTION_STATE_CONNECTED) { + return FALSE; + } + + message_t *message = message_factory_create_message(s_info.message_factory, MESSAGE_CONFIG_USER_NAME); + if(!message) { + _E("Failed to create CONFIG_USER_NAME message"); + return TRUE; + } + message_set_receiver(message, s_info.car_address, s_info.car_port); + message_config_user_name_set_name((message_config_user_name_t*)message, (const char*)data); + int res = message_manager_send_message(message); + if(res) { + _E("Failed to send user name"); + message_destroy(message); + return TRUE; + } + s_info.user_name_serial = message_get_serial(message); + message_destroy(message); + return TRUE; +} + static gboolean _keep_alive_cb(gpointer data) { return _send_keep_alive(); @@ -337,6 +389,13 @@ static int _start_keep_alive_timer() return -1; } +static int _start_user_name_timer(const char *name) +{ + _send_user_name((gpointer)name); + s_info.user_name_timer = g_timeout_add(USER_NAME_INTERVAL, _send_user_name, (gpointer)name); + return 0; +} + static void _reset_counters() { s_info.keep_alive_attempts_left = HELLO_START_ATTEMPTS; diff --git a/src/controller/controller_name_input.c b/src/controller/controller_name_input.c index da3e3b6..8567da5 100644 --- a/src/controller/controller_name_input.c +++ b/src/controller/controller_name_input.c @@ -16,7 +16,6 @@ #include "gear-racing-controller.h" -#include "model/model_cloud_connection.h" #include "model/model_car_connection.h" #include "controller/controller_name_input.h" #include "view_manager/view_manager.h" @@ -25,14 +24,12 @@ static s_controller s_info = { 0, }; void controller_name_input_destroy(void) { - model_cloud_connection_shutdown(); s_info.view_update_cb = NULL; } void controller_name_input_init(t_view_update_cb view_update_cb) { s_info.view_update_cb = view_update_cb; - model_cloud_connection_init(); } void controller_name_input_back(void) @@ -51,6 +48,6 @@ void controller_name_input_next() void controller_player_name_set(const char *player_name) { - model_cloud_connection_player_name_set(player_name); + model_car_connection_player_name_set(player_name); } diff --git a/src/messages/message_factory.c b/src/messages/message_factory.c index 03ec278..0ca2638 100644 --- a/src/messages/message_factory.c +++ b/src/messages/message_factory.c @@ -25,6 +25,7 @@ #include "messages/message_connect_accepted.h" #include "messages/message_connect_refused.h" #include "messages/message_keep_alive.h" +#include "messages/message_config_user_name.h" struct _message_factory { union { @@ -35,6 +36,7 @@ struct _message_factory { message_connect_accepted_t connect_accepted; message_connect_refused_t connect_refused; message_keep_alive_t keep_alive; + message_config_user_name_t config_user_name; } messages; }; @@ -62,6 +64,9 @@ message_t *message_factory_create_message(message_factory_t *factory, message_ty case MESSAGE_BYE: message_bye_init(&factory->messages.bye); return &factory->messages.bye.base; + case MESSAGE_CONFIG_USER_NAME: + message_config_user_name_init(&factory->messages.config_user_name); + return &factory->messages.config_user_name.base; default: return NULL; } diff --git a/src/model/model_car_connection.c b/src/model/model_car_connection.c index 5c7378e..1174a9a 100644 --- a/src/model/model_car_connection.c +++ b/src/model/model_car_connection.c @@ -29,7 +29,7 @@ #include "gear-racing-controller.h" #include "log.h" -#define SEND_TIMEOUT 20 +#define SEND_TIMEOUT 30 #define DIRECTION_BASE_VALUE 10000 #define THROTTLE_BASE_VALUE 10000 #define HELLO_TIMER_WAIT 3.0 @@ -37,7 +37,7 @@ typedef struct _s_car_model_connection { t_model_car_connection_update_cb controller_update_cb; bool ready_to_drive; - + char player_name[PLAYER_NAME_MAX_LEN + 1]; float direction; float throttle; float cam_elevation; @@ -162,10 +162,24 @@ void model_car_connection_set_stop(bool stop) } } -bool model_car_connection_is_connected() { +bool model_car_connection_is_connected() +{ return CAR_CONNECTION_STATE_CONNECTED == car_connection_manager_get_state(); } +bool model_car_connection_player_name_set(const char *player_name) +{ + if(!model_car_connection_is_connected()) { + _E("Connection hasn't been established"); + return false; + } + + strncpy(s_info.player_name, player_name, PLAYER_NAME_MAX_LEN); + + car_connection_manager_send_user_name(s_info.player_name); + return true; +} + void _connection_state_cb(car_connection_state_e previous, car_connection_state_e current) { static s_model_car_connection_cb_data model_data; if(current == CAR_CONNECTION_STATE_CONNECTING) { diff --git a/src/model/model_cloud_connection.c b/src/model/model_cloud_connection.c index 4493633..16fcfbf 100644 --- a/src/model/model_cloud_connection.c +++ b/src/model/model_cloud_connection.c @@ -24,7 +24,6 @@ typedef struct _s_cloud_model_connection { t_model_cloud_connection_update_cb controller_update_cb; t_model_cloud_connection_update_cb reconnect_cb; - char player_name[PLAYER_NAME_MAX_LEN + 1]; car_info_t **cars; int cars_size; gboolean is_initialized; @@ -78,18 +77,6 @@ void model_cloud_connection_unsubscirbe_event(void) model_cloud_connection.reconnect_cb = NULL; } -bool model_cloud_connection_player_name_set(const char *player_name) -{ - if(!model_cloud_connection.is_initialized) { - _E("Model cloud connection hasn't been initialized"); - return false; - } - - strncpy(model_cloud_connection.player_name, player_name, PLAYER_NAME_MAX_LEN); - //In case of failure, return 0 - return true; -} - void model_cloud_connection_subscribe_reconnect(t_model_cloud_connection_update_cb model_update_cb) { if(!model_cloud_connection.is_initialized) { |