diff options
author | Krzysztof Wieclaw <k.wieclaw@samsung.com> | 2018-09-26 18:19:02 +0200 |
---|---|---|
committer | Krzysztof Wieclaw <k.wieclaw@samsung.com> | 2018-10-04 15:13:44 +0200 |
commit | e5ccbe0d8ff14daecc69f7847f3c20aa86b258b4 (patch) | |
tree | 6e1839fd126d0acc0f02f85e44b1af395e6d5f9d | |
parent | 95f5f7297357bd6d764ae7b55943cc7a15a7b4bd (diff) | |
download | gear-racing-controller-e5ccbe0d8ff14daecc69f7847f3c20aa86b258b4.tar.gz gear-racing-controller-e5ccbe0d8ff14daecc69f7847f3c20aa86b258b4.tar.bz2 gear-racing-controller-e5ccbe0d8ff14daecc69f7847f3c20aa86b258b4.zip |
Integrated car connection manager
Change-Id: I8d8fde1b8c39bb00c25cbd64ebf85387d42ba333
Signed-off-by: Krzysztof Wieclaw <k.wieclaw@samsung.com>
-rw-r--r-- | .cproject | 26 | ||||
-rw-r--r-- | inc/model/model_car_connection.h | 4 | ||||
-rw-r--r-- | src/controller/controller_racing.c | 1 | ||||
-rw-r--r-- | src/model/model_car_connection.c | 243 |
4 files changed, 60 insertions, 214 deletions
@@ -16,18 +16,18 @@ </extensions> </storageModule> <storageModule moduleId="cdtBuildSystem" version="4.0.0"> - <configuration artifactName="gear-racing-controller" buildArtefactType="org.tizen.nativecore.buildArtefactType.app" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.tizen.nativecore.buildArtefactType.app,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" description="" errorParsers="org.eclipse.cdt.core.MakeErrorParser;org.eclipse.cdt.core.GCCErrorParser;" id="org.tizen.nativecore.config.sbi.gcc45.app.debug.1322065053" name="Debug" parent="org.tizen.nativecore.config.sbi.gcc45.app.debug"> + <configuration artifactName="org.tizen.gear-racing-controller-sb" buildArtefactType="org.tizen.nativecore.buildArtefactType.app" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.tizen.nativecore.buildArtefactType.app,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" description="" errorParsers="org.eclipse.cdt.core.MakeErrorParser;org.eclipse.cdt.core.GCCErrorParser;" id="org.tizen.nativecore.config.sbi.gcc45.app.debug.1322065053" name="Debug" parent="org.tizen.nativecore.config.sbi.gcc45.app.debug"> <folderInfo id="org.tizen.nativecore.config.sbi.gcc45.app.debug.1322065053." name="/" resourcePath=""> <toolChain id="org.tizen.nativecore.toolchain.sbi.gcc45.app.debug.1847471508" name="Tizen Native Toolchain" superClass="org.tizen.nativecore.toolchain.sbi.gcc45.app.debug"> <targetPlatform binaryParser="org.eclipse.cdt.core.ELF" id="org.tizen.nativeide.target.sbi.gnu.platform.base.328737878" osList="linux,win32" superClass="org.tizen.nativeide.target.sbi.gnu.platform.base"/> <builder autoBuildTarget="all" buildPath="${workspace_loc:/gear-racing-controller}/Debug" enableAutoBuild="true" id="org.tizen.nativecore.target.sbi.gnu.builder.227410736" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Tizen Application Builder" superClass="org.tizen.nativecore.target.sbi.gnu.builder"/> - <tool command="i586-linux-gnueabi-ar" id="org.tizen.nativecore.tool.sbi.gnu.archiver.1679874459" name="Archiver" superClass="org.tizen.nativecore.tool.sbi.gnu.archiver"/> + <tool command="arm-linux-gnueabi-ar" id="org.tizen.nativecore.tool.sbi.gnu.archiver.1679874459" name="Archiver" superClass="org.tizen.nativecore.tool.sbi.gnu.archiver"/> <tool command="clang++" id="org.tizen.nativecore.tool.sbi.gnu.cpp.compiler.1354257060" name="C++ Compiler" superClass="org.tizen.nativecore.tool.sbi.gnu.cpp.compiler"> <option id="gnu.cpp.compiler.option.optimization.level.796007872" name="Optimization Level" superClass="gnu.cpp.compiler.option.optimization.level" value="gnu.cpp.compiler.optimization.level.none" valueType="enumerated"/> <option defaultValue="gnu.cpp.compiler.debugging.level.max" id="sbi.gnu.cpp.compiler.option.debugging.level.core.1472200190" name="Debug level" superClass="sbi.gnu.cpp.compiler.option.debugging.level.core" valueType="enumerated"/> <option defaultValue="false" id="sbi.gnu.cpp.compiler.option.misc.pic.core.1659893418" name="-fPIC option" superClass="sbi.gnu.cpp.compiler.option.misc.pic.core" valueType="boolean"/> <option id="sbi.gnu.cpp.compiler.option.912899474" name="Tizen-Target" superClass="sbi.gnu.cpp.compiler.option" valueType="userObjs"> - <listOptionValue builtIn="false" value="wearable-4.0-emulator.core_llvm40.i386"/> + <listOptionValue builtIn="false" value="wearable-4.0-device.core_llvm40.armel"/> </option> <option id="sbi.gnu.cpp.compiler.option.frameworks_inc.core.395715001" name="Tizen-Frameworks-Include-Path" superClass="sbi.gnu.cpp.compiler.option.frameworks_inc.core" valueType="includePath"> <listOptionValue builtIn="false" value=""${SBI_SYSROOT}/usr/include/libxml2""/> @@ -122,6 +122,7 @@ <listOptionValue builtIn="false" value="${RS_COMPILER_MISC}"/> <listOptionValue builtIn="false" value=" -fPIE"/> <listOptionValue builtIn="false" value="--sysroot="${SBI_SYSROOT}""/> + <listOptionValue builtIn="false" value="-mthumb"/> </option> <option id="gnu.cpp.compiler.option.include.paths.1203848534" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" valueType="includePath"> <listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/inc}""/> @@ -141,7 +142,7 @@ <option defaultValue="gnu.c.debugging.level.max" id="sbi.gnu.c.compiler.option.debugging.level.core.1140874669" name="Debug level" superClass="sbi.gnu.c.compiler.option.debugging.level.core" valueType="enumerated"/> <option defaultValue="false" id="sbi.gnu.c.compiler.option.misc.pic.core.1096056089" name="-fPIC option" superClass="sbi.gnu.c.compiler.option.misc.pic.core" valueType="boolean"/> <option id="sbi.gnu.c.compiler.option.1123455384" name="Tizen-Target" superClass="sbi.gnu.c.compiler.option" valueType="userObjs"> - <listOptionValue builtIn="false" value="wearable-4.0-emulator.core_llvm40.i386"/> + <listOptionValue builtIn="false" value="wearable-4.0-device.core_llvm40.armel"/> </option> <option id="sbi.gnu.c.compiler.option.frameworks_inc.core.1389731891" name="Tizen-Frameworks-Include-Path" superClass="sbi.gnu.c.compiler.option.frameworks_inc.core" valueType="includePath"> <listOptionValue builtIn="false" value=""${SBI_SYSROOT}/usr/include/libxml2""/> @@ -232,10 +233,11 @@ <listOptionValue builtIn="false" value=""${SBI_SYSROOT}/usr/lib/glib-2.0/include""/> </option> <option id="sbi.gnu.c.compiler.option.frameworks_cflags.core.398744298" name="Tizen-Frameworks-Other-Cflags" superClass="sbi.gnu.c.compiler.option.frameworks_cflags.core" valueType="stringList"> - <listOptionValue builtIn="false" value="${TC_COMPILER_MISC}"/> - <listOptionValue builtIn="false" value="${RS_COMPILER_MISC}"/> + <listOptionValue builtIn="false" value="$(TC_COMPILER_MISC)"/> + <listOptionValue builtIn="false" value="$(RS_COMPILER_MISC)"/> <listOptionValue builtIn="false" value=" -fPIE"/> - <listOptionValue builtIn="false" value="--sysroot="${SBI_SYSROOT}""/> + <listOptionValue builtIn="false" value="--sysroot="$(SBI_SYSROOT)""/> + <listOptionValue builtIn="false" value="-mthumb"/> </option> <option id="gnu.c.compiler.option.include.paths.321037953" name="Include paths (-I)" superClass="gnu.c.compiler.option.include.paths" valueType="includePath"> <listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/inc}""/> @@ -256,12 +258,12 @@ <option defaultValue="false" id="sbi.gnu.cpp.linker.option.shared_flag.core.544193418" name="Linker.Shared" superClass="sbi.gnu.cpp.linker.option.shared_flag.core" valueType="boolean"/> <option defaultValue="false" id="sbi.gnu.cpp.linker.option.noundefined.core.1152050479" name="Report unresolved symbol references (-Wl,--no-undefined)" superClass="sbi.gnu.cpp.linker.option.noundefined.core" valueType="boolean"/> <option id="sbi.gnu.cpp.linker.option.frameworks_lflags.core.2074193141" name="Tizen-Frameworks-Other-Lflags" superClass="sbi.gnu.cpp.linker.option.frameworks_lflags.core" valueType="stringList"> - <listOptionValue builtIn="false" value="${TC_LINKER_MISC}"/> - <listOptionValue builtIn="false" value="${RS_LINKER_MISC}"/> + <listOptionValue builtIn="false" value="$(TC_LINKER_MISC)"/> + <listOptionValue builtIn="false" value="$(RS_LINKER_MISC)"/> <listOptionValue builtIn="false" value="-pie -lpthread "/> - <listOptionValue builtIn="false" value="--sysroot="${SBI_SYSROOT}""/> - <listOptionValue builtIn="false" value="-Xlinker --version-script="${PROJ_PATH}/.exportMap""/> - <listOptionValue builtIn="false" value="-L"${SBI_SYSROOT}/usr/lib""/> + <listOptionValue builtIn="false" value="--sysroot="$(SBI_SYSROOT)""/> + <listOptionValue builtIn="false" value="-Xlinker --version-script="$(PROJ_PATH)/.exportMap""/> + <listOptionValue builtIn="false" value="-L"$(SBI_SYSROOT)/usr/lib""/> <listOptionValue builtIn="false" value="$(RS_LIBRARIES)"/> </option> <option id="gnu.cpp.link.option.paths.83999257" name="Library search path (-L)" superClass="gnu.cpp.link.option.paths" valueType="libPaths"> diff --git a/inc/model/model_car_connection.h b/inc/model/model_car_connection.h index 964b22e..648ceef 100644 --- a/inc/model/model_car_connection.h +++ b/inc/model/model_car_connection.h @@ -19,6 +19,7 @@ #define MODEL_MODEL_CAR_CONNECTION_H_ #include "model/model_base.h" +#include "car_connection_manager.h" #include <stdbool.h> typedef struct _s_model_car_connection_cb_data { @@ -30,9 +31,8 @@ typedef void (*t_model_car_connection_update_cb)(s_model_car_connection_cb_data void model_car_connection_init(void); void model_car_connection_subscribe_event(t_model_car_connection_update_cb model_update_cb); void model_car_connection_unsubscirbe_event(); -void model_car_connection_model_state_change(void); -void model_car_connection_start_connection(char *address, int port); +void model_car_connection_start_connection(const char *address, int port); void model_car_connection_end_connection(void); void model_car_connection_send_direction(float direction); void model_car_connection_send_throttle(float throttle); diff --git a/src/controller/controller_racing.c b/src/controller/controller_racing.c index f75e4da..5ea4e46 100644 --- a/src/controller/controller_racing.c +++ b/src/controller/controller_racing.c @@ -80,6 +80,5 @@ void controller_racing_back(void) void controller_racing_next() { model_car_connection_ready_to_drive(false); - model_car_connection_model_state_change(); } diff --git a/src/model/model_car_connection.c b/src/model/model_car_connection.c index 6bb9684..f37aa6f 100644 --- a/src/model/model_car_connection.c +++ b/src/model/model_car_connection.c @@ -13,6 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "model/model_car_connection.h" + #include <stdlib.h> #include <stdio.h> #include <glib.h> @@ -21,44 +23,19 @@ #include <sys/socket.h> #include <netinet/in.h> +#include "command.h" +#include "messages/message_manager.h" +#include "car_connection_manager.h" #include "gear-racing-controller.h" -#include "model/model_car_connection.h" #include "log.h" -#define DIRECTION_BASE_VALUE 1000 +#define SEND_TIMEOUT 20 +#define DIRECTION_BASE_VALUE 10000 #define THROTTLE_BASE_VALUE 10000 #define HELLO_TIMER_WAIT 3.0 -typedef enum _e_message_type { - MESSAGE_HELLO, - MESSAGE_CALIBRATION, - MESSAGE_ALIVE, - MESSAGE_DRIVE, - MESSAGE_CAMERA, - MESSAGE_DRIVE_CAMERA, - MESSAGE_BYE, -} e_message_type; - -typedef struct _s_message { - unsigned long long int seq_num; - int cmd; - int servo; - int speed; - int cam_azimuth; - int cam_elevation; - unsigned long long int time; -} s_message; - typedef struct _s_car_model_connection { t_model_car_connection_update_cb controller_update_cb; - - GSocket *socket; - GSocketAddress *car_address; - gint port; - guint io_watch_id; - - Ecore_Timer *connection_wait_timer; - bool ready_to_drive; float direction; @@ -67,154 +44,41 @@ typedef struct _s_car_model_connection { float cam_azimuth; bool stop; - Ecore_Timer *send_timer; + guint send_timer; } s_model_car_connection; s_model_car_connection s_info = { 0, }; +void _connection_state_cb(car_connection_state_e previous, car_connection_state_e current); -gboolean _read_socket_cb(GIOChannel *source, GIOCondition condition, gpointer data) +static void _send_data(int servo, int speed, int cam_azimuth, int cam_elevation) { - GIOChannel *channel = data; - GString *buffer = g_string_new(NULL); - GIOStatus state = G_IO_STATUS_NORMAL; - GError *error = NULL; - - 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 | G_IO_ERR | G_IO_HUP, _read_socket_cb, channel); - g_io_channel_unref(channel); -} - -static void _send_data(int command, int servo, int speed, int cam_azimuth, int cam_elevation) -{ - GError *error = NULL; - static s_message message = { 0, }; - - message.cmd = command; - message.servo = servo; - message.speed = speed; - message.cam_azimuth = cam_azimuth; - message.cam_elevation = cam_elevation; - message.seq_num++; - message.time = time(NULL); - - _D("Send CMD[%d], Servo[%d], Speed[%d]", message.cmd, message.servo, message.speed, message.cam_azimuth, message.cam_elevation); - - ASSERT(!s_info.socket, "Socket not created"); - 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) { - _W("condition == %d", condition); -// return; - } - - 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") - - 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); - } -} - -static Eina_Bool _connection_wait_timer_cb(void *data) -{ - static s_model_car_connection_cb_data model_data = { - .type = MODEL_TYPE_FAIL, + command_s command = { + .type = COMMAND_TYPE_DRIVE_AND_CAMERA, }; + command.data.steering_and_camera.speed = speed; + command.data.steering_and_camera.direction = servo; + command.data.steering_and_camera.camera_azimuth = cam_azimuth; + command.data.steering_and_camera.camera_elevation = cam_elevation; + car_connection_manager_send_command(command); - 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; - return renew; } -static Eina_Bool _send_timer_cb(void *data) +static gboolean _send_timer_cb(gpointer data) { - _send_data(MESSAGE_DRIVE_CAMERA, s_info.direction * -DIRECTION_BASE_VALUE, s_info.throttle * THROTTLE_BASE_VALUE, + _D("Sending command..."); + _send_data(s_info.direction * -DIRECTION_BASE_VALUE, s_info.throttle * THROTTLE_BASE_VALUE, s_info.cam_azimuth * -DIRECTION_BASE_VALUE, s_info.cam_elevation * -DIRECTION_BASE_VALUE); - return ECORE_CALLBACK_RENEW; + return TRUE; } void model_car_connection_init(void) { - GError *error = NULL; - - 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(); + message_manager_init(); + car_connection_manager_init(); + car_connection_manager_set_state_change_cb(_connection_state_cb); } void model_car_connection_subscribe_event(t_model_car_connection_update_cb model_update_cb) @@ -222,38 +86,18 @@ void model_car_connection_subscribe_event(t_model_car_connection_update_cb model s_info.controller_update_cb = model_update_cb; } -void model_car_connection_model_state_change(void) -{ - static s_model_car_connection_cb_data model_data = { - .type = MODEL_TYPE_END, - }; - - s_info.controller_update_cb(&model_data); -} void model_car_connection_unsubscirbe_event() { s_info.controller_update_cb = NULL; } -void model_car_connection_start_connection(char *address, int port) +void model_car_connection_start_connection(const char *address, int port) { - s_info.port = port; - s_info.car_address = g_inet_socket_address_new_from_string(address, port); - ASSERT_MEMORY(!s_info.car_address); - - 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, 0, 0); - s_info.connection_wait_timer = ecore_timer_add(HELLO_TIMER_WAIT, _connection_wait_timer_cb, (void *)ECORE_CALLBACK_CANCEL); - - s_info.send_timer = ecore_timer_add(1.0 / 60.0, _send_timer_cb, NULL); - - if (error) { - g_error_free(error); + if(car_connection_manager_connect(address, port)) { + _E("Failed to connect with %s:%d", address, port); + return; } + s_info.send_timer = g_timeout_add(SEND_TIMEOUT, _send_timer_cb, NULL); } void model_car_connection_ready_to_drive(bool is_ready) @@ -263,21 +107,11 @@ void model_car_connection_ready_to_drive(bool is_ready) void model_car_connection_end_connection(void) { - if (!s_info.socket) { - return; - } - - 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); + if(s_info.send_timer) { + g_source_remove(s_info.send_timer); + s_info.send_timer = 0; } - - ecore_timer_del(s_info.send_timer); + car_connection_manager_disconnect(); } void model_car_connection_send_direction(float direction) @@ -329,3 +163,14 @@ void model_car_connection_set_stop(bool stop) } } +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) { + model_data.type = MODEL_TYPE_WAIT; + } else if(current == CAR_CONNECTION_STATE_DISCONNECTED) { + model_data.type = MODEL_TYPE_FAIL; + } else { + model_data.type = MODEL_TYPE_END; + } + s_info.controller_update_cb(&model_data); +} |