From b3409a74c548c181c7ee13c10106008bd425f797 Mon Sep 17 00:00:00 2001 From: Krzysztof Wieclaw Date: Tue, 9 Oct 2018 14:25:00 +0200 Subject: [PATCH] Added service of message_config_user_name Change-Id: Ia10ffea582ee34a068cbdcd925df994e59d4ca56 Signed-off-by: Krzysztof Wieclaw --- CMakeLists.txt | 3 ++- inc/controller_connection_manager.h | 14 +++++++++++++- src/app.c | 10 +++++++++- src/controller_connection_manager.c | 21 +++++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b8d3eb1..ead3d4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,12 +32,13 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") INCLUDE_DIRECTORIES(${PROJECT_ROOT_DIR}/inc) ADD_EXECUTABLE(${PROJECT_NAME} - ${PROJECT_ROOT_DIR}/src/messages/message_config_user_name.c + ${PROJECT_ROOT_DIR}/src/lap_counter/lap_counter.c ${PROJECT_ROOT_DIR}/src/messages/clock.c ${PROJECT_ROOT_DIR}/src/messages/message_command.c ${PROJECT_ROOT_DIR}/src/messages/message_factory.c ${PROJECT_ROOT_DIR}/src/controller_connection_manager.c ${PROJECT_ROOT_DIR}/src/messages/writer.c + ${PROJECT_ROOT_DIR}/src/messages/message_config_user_name.c ${PROJECT_ROOT_DIR}/src/messages/message_ack.c ${PROJECT_ROOT_DIR}/src/messages/message_connect_accepted.c ${PROJECT_ROOT_DIR}/src/messages/message_keep_alive.c diff --git a/inc/controller_connection_manager.h b/inc/controller_connection_manager.h index 35f8777..c87e132 100644 --- a/inc/controller_connection_manager.h +++ b/inc/controller_connection_manager.h @@ -36,11 +36,17 @@ typedef void (*connection_state_cb)(controller_connection_state_e previous, cont /** * @brief Called whenever new command arrives. - * @param[in] message Message tat arrived. + * @param[in] command Command that arrived. */ typedef void (*command_received_cb)(command_s command); /** + * @brief Called whenever new user name arrives. + * @param[in] name User name that arrived. + */ +typedef void (*user_name_received_cb)(const char *name); + +/** * @brief Starts listening on the given port for messages. * @return 0 on success, -1 otherwise. * @remarks This function allocates resources and that has to be freed with controller_connection_manager_release. @@ -72,6 +78,12 @@ void controller_connection_manager_handle_message(message_t *message); void controller_connection_manager_set_command_received_cb(command_received_cb callback); /** + * @brief Sets callback function called whenever new message arrives. + * @param[in] callback Callback function to be set. + */ +void controller_connection_manager_set_user_name_received_cb(user_name_received_cb callback); + +/** * @brief Stops listening for messages and release resources connected with it. */ void controller_connection_manager_release(); diff --git a/src/app.c b/src/app.c index 60c5c20..03e3e95 100644 --- a/src/app.c +++ b/src/app.c @@ -29,6 +29,7 @@ #include "cloud/cloud_communication.h" #include "messages/message_manager.h" #include "controller_connection_manager.h" +#include "lap_counter/lap_counter.h" #include "command.h" #define ENABLE_MOTOR 1 @@ -59,6 +60,7 @@ typedef struct app_data_s { unsigned int f_value; unsigned int r_value; unsigned int dir_state; + const char *user_name; guint idle_h; } app_data; @@ -165,6 +167,10 @@ static void __command_received_cb(command_s command) { } } +static void __user_name_received_cb(const char *name) { + lap_counter_set_user_name(name); +} + static void _initialize_config() { net_util_init(); @@ -198,6 +204,7 @@ static void _initialize_components(app_data *ad) cloud_communication_init(); message_manager_init(); controller_connection_manager_listen(); + lap_counter_init(); } static bool service_app_create(void *data) @@ -227,6 +234,7 @@ static bool service_app_create(void *data) cloud_communication_start(CLOUD_REQUESTS_FREQUENCY); controller_connection_manager_set_command_received_cb(__command_received_cb); + controller_connection_manager_set_user_name_received_cb(__user_name_received_cb); return true; } @@ -257,7 +265,7 @@ static void service_app_terminate(void *data) if (ad->idle_h) g_source_remove(ad->idle_h); - + lap_counter_shutdown(); controller_connection_manager_release(); message_manager_shutdown(); diff --git a/src/controller_connection_manager.c b/src/controller_connection_manager.c index fb1f590..2cce144 100644 --- a/src/controller_connection_manager.c +++ b/src/controller_connection_manager.c @@ -19,6 +19,7 @@ #include "messages/message_command.h" #include "messages/message_ack.h" #include "messages/message_factory.h" +#include "messages/message_config_user_name.h" #include #include #include "log.h" @@ -43,6 +44,7 @@ typedef struct _controller_connection_manager_info { int controller_port; connection_state_cb state_cb; command_received_cb command_cb; + user_name_received_cb user_name_cb; int keep_alive_check_attempts_left; int connect_accept_attempts_left; guint connect_accept_timer; @@ -96,6 +98,11 @@ void controller_connection_manager_set_command_received_cb(command_received_cb c s_info.command_cb = callback; } +void controller_connection_manager_set_user_name_received_cb(user_name_received_cb callback) +{ + s_info.user_name_cb = callback; +} + void controller_connection_manager_handle_message(message_t *message) { if(!s_info.message_factory) { @@ -167,6 +174,20 @@ void controller_connection_manager_handle_message(message_t *message) _W("Unexpectedly received BYE from %s:%d (address_match == %d)", msg_address, msg_port, address_match); } break; + case MESSAGE_CONFIG_USER_NAME: + if(s_info.state == CONTROLLER_CONNECTION_STATE_RESERVED && address_match) { + if(s_info.user_name_cb) { + s_info.user_name_cb(message_config_user_name_get_name((message_config_user_name_t*)message)); + } + message_ack_t response; + message_ack_init_from_request(&response, message); + message_set_receiver((message_t*)&response, s_info.controller_address, s_info.controller_port); + message_manager_send_message((message_t*)&response); + message_destroy((message_t*)&response); + } else { + _W("Unexpectedly received BYE from %s:%d (address_match == %d)", msg_address, msg_port, address_match); + }; + break; default: _W("Received incorrect message"); } -- 2.7.4