From 239ef9c45da5fc647a997e87e8fa188f01f59705 Mon Sep 17 00:00:00 2001 From: Michal Skorupinski Date: Thu, 23 Aug 2018 13:56:47 +0200 Subject: [PATCH] Fixes in the car connection model. Networking privileges added Change-Id: I509ffb5f89179188d5798af23535ebb8ef8f3a28 Signed-off-by: Michal Skorupinski --- inc/model/model_car_connection.h | 4 +- src/controller/controller_car_selection.c | 5 + src/controller/controller_racing.c | 12 ++- src/controller/controller_reset.c | 4 + src/gear-racing-controller.c | 2 + src/model/model_car_connection.c | 161 ++++++++++++++++++++++++------ src/model/model_hw.c | 2 + src/model/model_sensors.c | 5 +- src/view/view_car_selection.c | 22 +++- src/view/view_racing.c | 2 +- src/view_manager/view_manager.c | 10 ++ tizen-manifest.xml | 6 ++ 12 files changed, 192 insertions(+), 43 deletions(-) diff --git a/inc/model/model_car_connection.h b/inc/model/model_car_connection.h index 8a7773f..372a4ae 100644 --- a/inc/model/model_car_connection.h +++ b/inc/model/model_car_connection.h @@ -34,7 +34,9 @@ void model_car_connection_model_state_change(void); void model_car_connection_start_connection(char *address, int port); void model_car_connection_end_connection(void); -void model_car_connection_send_stering_data(float direction, float throttle); +void model_car_connection_send_direction(float direction); +void model_car_connection_send_throttle(float throttle); +void model_car_connection_ready_to_drive(bool is_ready); #endif /* MODEL_MODEL_CAR_CONNECTION_H_ */ diff --git a/src/controller/controller_car_selection.c b/src/controller/controller_car_selection.c index c399da7..1cfc8cc 100644 --- a/src/controller/controller_car_selection.c +++ b/src/controller/controller_car_selection.c @@ -19,6 +19,10 @@ #include "controller/controller_car_selection.h" #include "view_manager/view_manager.h" +#define ADDRES "192.168.43.228" +//#define ADDRES "192.168.43.18" +#define PORT 57984 + static s_controller s_info = { 0, }; void controller_car_selection_destroy(void) @@ -65,6 +69,7 @@ void controller_car_selection_back(void) void controller_car_selection_next() { model_car_connection_model_state_change(); + model_car_connection_start_connection(ADDRES, PORT); } void controller_get_car_names() diff --git a/src/controller/controller_racing.c b/src/controller/controller_racing.c index 1f9fe1e..e88538f 100644 --- a/src/controller/controller_racing.c +++ b/src/controller/controller_racing.c @@ -15,7 +15,7 @@ */ #include "gear-racing-controller.h" -#include "model/model_cloud_connection.h" +#include "model/model_car_connection.h" #include "model/model_sensors.h" #include "model/model_hw.h" #include "controller/controller_racing.h" @@ -31,11 +31,11 @@ static s_racing_controller s_info = { { 0, } }; void controller_racing_destroy(void) { model_sensors_unsubscirbe_event(); - model_cloud_connection_unsubscirbe_event(); + model_car_connection_unsubscirbe_event(); model_hw_unsubscirbe_event(); } -static void _model_connected_cb(s_model_cloud_connection_cb_data *model_data) +static void _model_connected_cb(s_model_car_connection_cb_data *model_data) { view_manager_set_view(VIEW_RESET); } @@ -56,11 +56,12 @@ void controller_racing_init(t_view_update_cb view_update_cb) { s_info.base_controller.view_update_cb = view_update_cb; - model_cloud_connection_subscribe_event(_model_connected_cb); + model_car_connection_subscribe_event(_model_connected_cb); model_sensors_subscribe_event(_sensors_cb); model_hw_subscribe_event(_hw_cb); model_sensors_set_initial_values(); + model_car_connection_ready_to_drive(true); } void controller_racing_back(void) @@ -70,6 +71,7 @@ void controller_racing_back(void) void controller_racing_next() { - model_cloud_connection_model_state_change(); + model_car_connection_ready_to_drive(false); + model_car_connection_model_state_change(); } diff --git a/src/controller/controller_reset.c b/src/controller/controller_reset.c index a5e215b..1e905f3 100644 --- a/src/controller/controller_reset.c +++ b/src/controller/controller_reset.c @@ -18,6 +18,7 @@ #include "gear-racing-controller.h" #include "model/model_cloud_connection.h" +#include "model/model_car_connection.h" #include "controller/controller_reset.h" #include "view_manager/view_manager.h" @@ -26,6 +27,9 @@ static s_controller s_info = { 0, }; void controller_reset_destroy(void) { model_cloud_connection_unsubscirbe_event(); + + model_car_connection_end_connection(); + model_car_connection_unsubscirbe_event(); } static void _connected_cb(s_model_cloud_connection_cb_data *model_data) diff --git a/src/gear-racing-controller.c b/src/gear-racing-controller.c index 36d6deb..6302a4a 100644 --- a/src/gear-racing-controller.c +++ b/src/gear-racing-controller.c @@ -18,11 +18,13 @@ #include "view_manager/view_manager.h" #include "model/model_sensors.h" #include "model/model_hw.h" +#include "model/model_car_connection.h" static bool app_create(void *data) { model_sensors_init(); model_hw_init(); + model_car_connection_init(); view_manager_initialize_ui(); return true; } diff --git a/src/model/model_car_connection.c b/src/model/model_car_connection.c index 660a326..53d7ffa 100644 --- a/src/model/model_car_connection.c +++ b/src/model/model_car_connection.c @@ -48,11 +48,16 @@ typedef struct _s_car_model_connection { t_model_car_connection_update_cb controller_update_cb; GSocket *socket; - GSocketAddress *car; + GSocketAddress *car_address; gint port; guint io_watch_id; Ecore_Timer *connection_wait_timer; + + bool ready_to_drive; + + float direction; + float throttle; } s_model_car_connection; s_model_car_connection s_info = { 0, }; @@ -60,15 +65,67 @@ s_model_car_connection s_info = { 0, }; gboolean _read_socket_cb(GIOChannel *source, GIOCondition condition, gpointer data) { + GIOChannel *channel = data; + GString *buffer = g_string_new(NULL); + GIOStatus state = G_IO_STATUS_NORMAL; + GError *error = NULL; - return true; + while (state == G_IO_STATUS_NORMAL) + { + gsize delim = 0; + + state = g_io_channel_read_line_string(channel, buffer, &delim, &error); + + if (error) { + g_warning("%s\n", error->message); + } + + switch(state) + { + case G_IO_STATUS_NORMAL: + if (strlen(buffer->str) > 0) + { + _D("Got: %s\n", buffer->str); + if (!strncmp(buffer->str, "PING!", strlen("PING!"))) + { + _D("Received reply\n"); + } + else if (!strncmp(buffer->str, "Welcome to the echo service!", strlen("Welcome to the echo service!"))) + { + _D("Service online\n"); + } + + /* reset the buffer for the next call */ + g_string_set_size(buffer, 0); + delim = 0; + } + break; + case G_IO_STATUS_AGAIN: + /* no data right now... try again later */ + break; + case G_IO_STATUS_ERROR: + case G_IO_STATUS_EOF: + _W("Error reading: %s\n", error->message); + + /* drop last reference on connection */ + g_io_channel_shutdown(channel, FALSE, &error); + + /* don't need the input buffer anymore */ + g_string_free(buffer, TRUE); + + /* remove the event source */ + return(FALSE); + } + } + + return true; } static void _receiver_udp_start(void) { int socket_fd = g_socket_get_fd(s_info.socket); GIOChannel *channel = g_io_channel_unix_new(socket_fd); - s_info.io_watch_id = g_io_add_watch(channel, G_IO_IN, _read_socket_cb, NULL); + s_info.io_watch_id = g_io_add_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP, _read_socket_cb, channel); g_io_channel_unref(channel); } @@ -77,7 +134,7 @@ static void _send_data(int command, int servo, int speed) GError *error = NULL; static s_message message = { 0, }; - _D("Send Servo[%d], Speed[%d]", message.servo, message.speed); + _D("Send CMD[%d], Servo[%d], Speed[%d]", message.cmd, message.servo, message.speed); message.cmd = command; message.servo = servo; @@ -86,19 +143,23 @@ static void _send_data(int command, int servo, int speed) message.time = time(NULL); ASSERT(!s_info.socket, "Socket not created"); - ASSERT(!s_info.car, "Destination not set"); + ASSERT(!s_info.car_address, "Destination not set"); GIOCondition condition = g_socket_condition_check(s_info.socket, G_IO_OUT); + if (condition != G_IO_OUT) { - _D("condition != G_IO_OUT"); - return; + _W("condition == %d", condition); +// return; } - gssize size = g_socket_send_to(s_info.socket, s_info.car, - (const gchar *)&message, sizeof(message), NULL, &error); + bool connection_result = g_socket_check_connect_result(s_info.socket, &error); + ASSERT(error, "g_socket_check_connect_result(). Error: %s", error->message); + ASSERT(!connection_result, "connection_result == false") - if (size < 0) { - _E("Failed to send to socket: %s", error->message); + gssize size = g_socket_send(s_info.socket, (const gchar *)&message, sizeof(s_message), NULL, &error); + ASSERT(size < 0, "Failed to send to socket: %s", error->message) + + if (error) { g_error_free(error); } } @@ -109,7 +170,12 @@ static Eina_Bool _connection_wait_timer_cb(void *data) .type = MODEL_TYPE_FAIL, }; - s_info.controller_update_cb(&model_data); + return ECORE_CALLBACK_CANCEL; //TODO Test only + + if (s_info.controller_update_cb) { + s_info.controller_update_cb(&model_data); + } + model_car_connection_end_connection(); bool renew = (bool)data; @@ -124,6 +190,10 @@ void model_car_connection_init(void) s_info.socket = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &error); ASSERT(!s_info.socket, "Failed to get new Socket - %s", error->message); + if (error) { + g_error_free(error); + } + _receiver_udp_start(); } @@ -134,14 +204,11 @@ void model_car_connection_subscribe_event(t_model_car_connection_update_cb model void model_car_connection_model_state_change(void) { -// static s_model_car_connection_cb_data model_data = { -// .type = MODEL_TYPE_END, -// }; - - // TODO Wait for connection -// s_info.controller_update_cb(&model_data); + static s_model_car_connection_cb_data model_data = { + .type = MODEL_TYPE_END, + }; - model_car_connection_start_connection("192.168.0.1", 1234); + s_info.controller_update_cb(&model_data); } void model_car_connection_unsubscirbe_event() { @@ -151,18 +218,31 @@ void model_car_connection_unsubscirbe_event() void model_car_connection_start_connection(char *address, int port) { s_info.port = port; - s_info.car = g_inet_socket_address_new_from_string(address, port); - ASSERT_MEMORY(!s_info.car); + s_info.car_address = g_inet_socket_address_new_from_string(address, port); + ASSERT_MEMORY(!s_info.car_address); - _send_data(MESSAGE_HELLO, 0, 0); + GError *error = NULL; + bool connected = g_socket_connect(s_info.socket, s_info.car_address, NULL, &error); + ASSERT(error, "g_socket_connect(): %s", error->message); + ASSERT(!connected, "Failed to connect to socket"); + _send_data(MESSAGE_HELLO, 0, 0); s_info.connection_wait_timer = ecore_timer_add(HELLO_TIMER_WAIT, _connection_wait_timer_cb, (void *)ECORE_CALLBACK_CANCEL); +// static s_model_car_connection_cb_data model_data = { +// .type = MODEL_TYPE_END, +// }; - static s_model_car_connection_cb_data model_data = { - .type = MODEL_TYPE_WAIT, - }; - s_info.controller_update_cb(&model_data); +// s_info.controller_update_cb(&model_data); + + if (error) { + g_error_free(error); + } +} + +void model_car_connection_ready_to_drive(bool is_ready) +{ + s_info.ready_to_drive = is_ready; } void model_car_connection_end_connection(void) @@ -171,15 +251,34 @@ void model_car_connection_end_connection(void) return; } - g_socket_close(s_info.socket, NULL); - g_object_unref(s_info.socket); - g_object_unref(s_info.car); + GError *error = NULL; + g_socket_shutdown(s_info.socket, true, true, &error); + ASSERT(error, "Failed to shutdown the socket"); + + g_object_unref(s_info.car_address); + + if (error) { + g_error_free(error); + } } -void model_car_connection_send_stering_data(float direction, float throttle) +void model_car_connection_send_direction(float direction) { - _send_data(MESSAGE_DRIVE, direction * -BASE_VALUE, throttle * -BASE_VALUE); + if (!s_info.ready_to_drive) { + return; + } + + s_info.direction = direction; + _send_data(MESSAGE_DRIVE, s_info.direction * -BASE_VALUE, s_info.throttle * -BASE_VALUE); } +void model_car_connection_send_throttle(float throttle) +{ + if (!s_info.ready_to_drive) { + return; + } + s_info.throttle = throttle; + _send_data(MESSAGE_DRIVE, s_info.direction * -BASE_VALUE, s_info.throttle * -BASE_VALUE); +} diff --git a/src/model/model_hw.c b/src/model/model_hw.c index 2cefdae..4fe21ef 100644 --- a/src/model/model_hw.c +++ b/src/model/model_hw.c @@ -15,6 +15,7 @@ */ #include "model/model_hw.h" +#include "model/model_car_connection.h" #include @@ -49,6 +50,7 @@ static Eina_Bool _rotary_cb(void *data, Eext_Rotary_Event_Info *info) s_info.hw_update_cb(&cb_data); _D("THROTTLE: %f", cb_data.throttle); + model_car_connection_send_throttle(s_info.throttle); return true; } diff --git a/src/model/model_sensors.c b/src/model/model_sensors.c index 0c3f92f..3cdfdf4 100644 --- a/src/model/model_sensors.c +++ b/src/model/model_sensors.c @@ -17,6 +17,7 @@ #include #include "gear-racing-controller.h" #include "model/model_sensors.h" +#include "model/model_car_connection.h" #include "log.h" #include "math_helper.h" @@ -99,7 +100,9 @@ static void _sensor_event_cb(sensor_h sensor, sensor_event_s *event, void *data) model_data.direction = math_helper_range_map(direction, -8.00f, 8.00f, -1.0f, 1.0f); model_data.velocity = velocity; - _D("MODEL VALUES{%f}: dir:% .4f ranged:% .4f", len, direction, model_data.direction); +// _D("MODEL VALUES{%f}: dir:% .4f ranged:% .4f", len, direction, model_data.direction); + + model_car_connection_send_direction(model_data.direction); if (s_info.sensors_update_cb) { s_info.sensors_update_cb(&model_data); diff --git a/src/view/view_car_selection.c b/src/view/view_car_selection.c index 347e64b..c99c50b 100644 --- a/src/view/view_car_selection.c +++ b/src/view/view_car_selection.c @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include "view/view_base.h" #include "view/view_car_selection.h" #include "controller/controller_car_selection.h" @@ -140,12 +141,17 @@ static void _add_scroll_end_callbacks(void) evas_object_smart_callback_add(s_info.genlist, "drag,stop", _genlist_scroll_cb, (void *)SCROLL_DRAG_STOP); } -static void _create_genlist(Eext_Circle_Surface *circle_surface) +void _set_circular_parameters(Eext_Circle_Surface* circle_surface) { - s_info.genlist = elm_genlist_add(s_info.view_base.view); - ASSERT_MEMORY(!s_info.genlist); + bool is_circular = false; + system_info_get_platform_bool("http://tizen.org/feature/screen.shape.circle", &is_circular); - Evas_Object *circle_genlist = eext_circle_object_genlist_add(s_info.genlist, circle_surface); + if (!is_circular) { + _D("Circular screen shape is not supported"); + return; + } + + Evas_Object* circle_genlist = eext_circle_object_genlist_add(s_info.genlist, circle_surface); ASSERT_MEMORY(!circle_genlist); eext_circle_object_genlist_scroller_policy_set(circle_genlist, ELM_SCROLLER_POLICY_AUTO, ELM_SCROLLER_POLICY_AUTO); @@ -154,6 +160,14 @@ static void _create_genlist(Eext_Circle_Surface *circle_surface) eext_circle_object_item_color_set(circle_genlist, "default", 255, 0, 0, 128); eext_circle_object_item_color_set(circle_genlist, "vertical,scroll,bg", 255, 255, 255, 15); eext_circle_object_line_width_set(circle_genlist, 5); +} + +static void _create_genlist(Eext_Circle_Surface *circle_surface) +{ + s_info.genlist = elm_genlist_add(s_info.view_base.view); + ASSERT_MEMORY(!s_info.genlist); + + _set_circular_parameters(circle_surface); s_info.itc = elm_genlist_item_class_new(); ASSERT_MEMORY(!s_info.itc); diff --git a/src/view/view_racing.c b/src/view/view_racing.c index 298b0bc..0dc6200 100644 --- a/src/view/view_racing.c +++ b/src/view/view_racing.c @@ -91,7 +91,7 @@ static void _controller_cb(void *data) float angle = math_helper_range_map(controller_data->direction, -1, 1, s_info.dir_min_angle, s_info.dir_max_angle); view_base_set_angle(s_info.direction, angle, 180.0, 180.0); - _D("VIEW VALUES: %f %f DIR: %f", controller_data->direction, controller_data->throttle, angle); +// _D("VIEW VALUES: %f %f DIR: %f", controller_data->direction, controller_data->throttle, angle); angle = math_helper_range_map(fabsf(controller_data->throttle), 0, 1, s_info.vel_min_angle, s_info.vel_max_angle); view_base_set_angle(s_info.velocity, angle, 180.0, 180.0); diff --git a/src/view_manager/view_manager.c b/src/view_manager/view_manager.c index 4883fd0..ae1c2aa 100644 --- a/src/view_manager/view_manager.c +++ b/src/view_manager/view_manager.c @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include "gear-racing-controller.h" #include "view_manager/view_manager.h" #include "log.h" @@ -86,6 +87,15 @@ static void _create_theme_overlay(char *edc_file) static void _create_circle_surface(void) { + bool is_circular = false; + system_info_get_platform_bool("http://tizen.org/feature/screen.shape.circle", &is_circular); + + if (!is_circular) { + _D("Circular screen shape is not supported"); + return; + } + + s_info.circle_surface = eext_circle_surface_naviframe_add(s_info.naviframe); } diff --git a/tizen-manifest.xml b/tizen-manifest.xml index 5bb1198..926ed25 100644 --- a/tizen-manifest.xml +++ b/tizen-manifest.xml @@ -5,4 +5,10 @@ gear-racing-controller.png + + http://tizen.org/privilege/network.get + http://tizen.org/privilege/network.set + http://tizen.org/privilege/internet + http://tizen.org/privilege/network.profile + -- 2.7.4