From: Krzysztof Wieclaw Date: Mon, 1 Oct 2018 09:20:15 +0000 (+0200) Subject: Cleanup unused functions and modules X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F77%2F190377%2F9;p=apps%2Fnative%2Fgear-racing-car.git Cleanup unused functions and modules Change-Id: I68208a94e55d905309ccb197eaae1ce2c8a94e4f Signed-off-by: Krzysztof Wieclaw --- diff --git a/CMakeLists.txt b/CMakeLists.txt index c4eadfe..dd6c623 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,11 +50,7 @@ ADD_EXECUTABLE(${PROJECT_NAME} ${PROJECT_ROOT_DIR}/src/config.c ${PROJECT_ROOT_DIR}/src/app.c ${PROJECT_ROOT_DIR}/src/log.c - ${PROJECT_ROOT_DIR}/src/connection_manager.c ${PROJECT_ROOT_DIR}/src/resource.c - ${PROJECT_ROOT_DIR}/src/message.c - ${PROJECT_ROOT_DIR}/src/receiver.c - ${PROJECT_ROOT_DIR}/src/receiver_udp.c ${PROJECT_ROOT_DIR}/src/resource/resource_infrared_obstacle_avoidance_sensor.c ${PROJECT_ROOT_DIR}/src/resource/resource_motor_driver_L298N.c ${PROJECT_ROOT_DIR}/src/resource/resource_PCA9685.c diff --git a/inc/connection_manager.h b/inc/connection_manager.h deleted file mode 100644 index 14a054f..0000000 --- a/inc/connection_manager.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Flora License, Version 1.1 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#ifndef __POSITION_FINDER_CONN_MGR_H__ -#define __POSITION_FINDER_CONN_MGR_H__ - -typedef enum { - CONNECTION_STATE_DISCONNECTED, - CONNECTION_STATE_CONNECTED, -} connection_state_e; - -typedef void(*connection_state_changed_cb) - (connection_state_e state, const char *ip, void* user_data); - -int connection_manager_get_ip(const char **ip); -int connection_manager_init(void); -int connection_manager_fini(void); -int connection_manager_set_state_changed_cb( - connection_state_changed_cb state_cb, void *user_data); - -#endif /* __POSITION_FINDER_CONN_MGR_H__ */ diff --git a/inc/message.h b/inc/message.h deleted file mode 100644 index c29f600..0000000 --- a/inc/message.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Flora License, Version 1.1 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CAR_APP_MESSAGE_H__ -#define __CAR_APP_MESSAGE_H__ - -typedef enum __message_cmd_e { - MESSAGE_CMD_HELLO, /* to use keep alive, if needed */ - MESSAGE_CMD_CALIBRATION, - MESSAGE_CMD_DRIVE, - MESSAGE_CMD_BYE, /* to notify explicitly closing connection */ -} message_cmd_e; - -struct __message_type_s { - unsigned long long int seq_num; - message_cmd_e cmd; - int servo; - int speed; - unsigned long long int time; /* to be used to order messages */ -}; -typedef struct __message_type_s message_s; - -int message_new_to_send(message_cmd_e cmd, - int servo, int speed, message_s *new_msg); - -void message_reset_seq_num(void); - -int message_queue_new(void); -void message_queue_clear(void); - -void message_push_to_inqueue(message_s *msg); -message_s *message_pop_from_inqueue(void); - -void message_push_to_outqueue(message_s *msg); -message_s *message_pop_from_outqueue(void); - -#endif /* __CAR_APP_MESSAGE_H__ */ diff --git a/inc/receiver.h b/inc/receiver.h deleted file mode 100644 index e194460..0000000 --- a/inc/receiver.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Flora License, Version 1.1 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CAR_APP_RECEIVER_H__ -#define __CAR_APP_RECEIVER_H__ - -#include "receiver_type.h" - -int receiver_init(receiver_type_e type); -void receiver_fini(receiver_type_e type); -int receiver_start(receiver_type_e type); -int receiver_stop(receiver_type_e type); -receiver_state_e receiver_get_state(receiver_type_e type); -int receiver_set_state_changed_cb(receiver_type_e type, - receiver_state_changed_cb callback, void *user_data); - -#endif /* __CAR_APP_RECEIVER_H__ */ diff --git a/inc/receiver_internal.h b/inc/receiver_internal.h deleted file mode 100644 index 30fb318..0000000 --- a/inc/receiver_internal.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Flora License, Version 1.1 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CAR_APP_RECEIVER_INTERNAL_H__ -#define __CAR_APP_RECEIVER_INTERNAL_H__ - -#include "receiver_type.h" - -/* TODO */ -typedef int (*receiver_init_func) (void *data); -typedef int (*receiver_fini_func) (void *data); -typedef int (*receiver_start_func) (void *data); -typedef int (*receiver_stop_func) (void *data); -typedef receiver_state_e (*receiver_get_state_func) (void *data); - -typedef struct __receiver_module_h receiver_module_h; - -int receiver_set_module_data(receiver_module_h *handle, void *module_data); -void *receiver_get_module_data(receiver_module_h *handle); - -int receiver_set_module_init_function( - receiver_module_h *handle, receiver_init_func func); - -int receiver_set_module_fini_function( - receiver_module_h *handle, receiver_fini_func func); - -int receiver_set_module_start_function( - receiver_module_h *handle, receiver_start_func func); - -int receiver_set_module_stop_function( - receiver_module_h *handle, receiver_stop_func func); - -int receiver_set_module_get_state_function( - receiver_module_h *handle, receiver_get_state_func func); - -void receiver_module_state_changed( - receiver_module_h *handle, receiver_state_e state); - -#endif /* __CAR_APP_RECEIVER_INTERNAL_H__ */ diff --git a/inc/receiver_type.h b/inc/receiver_type.h deleted file mode 100644 index 81e99d6..0000000 --- a/inc/receiver_type.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Flora License, Version 1.1 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CAR_APP_RECEIVER_TYPE_H__ -#define __CAR_APP_RECEIVER_TYPE_H__ - -typedef enum __receiver_type_e { - RECEIVER_TYPE_UDP = (1 << 0), - RECEIVER_TYPE_BLUETOOTH = (1 << 1), -} receiver_type_e; - -typedef enum __receiver_state_e { - RECEIVER_STATE_NONE, - RECEIVER_STATE_INIT, - RECEIVER_STATE_READY, - RECEIVER_STATE_CONNECTED, -} receiver_state_e; - -typedef void(*receiver_state_changed_cb) - (receiver_type_e type, receiver_state_e state, void* user_data); - -#endif /* __CAR_APP_RECEIVER_TYPE_H__ */ diff --git a/inc/receiver_udp.h b/inc/receiver_udp.h deleted file mode 100644 index ad0a100..0000000 --- a/inc/receiver_udp.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Flora License, Version 1.1 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CAR_APP_RECEIVER_UDP_H__ -#define __CAR_APP_RECEIVER_UDP_H__ - -#include "receiver_internal.h" - -int receiver_udp_module_register(receiver_module_h *handle); - -#endif /* __CAR_APP_RECEIVER_UDP_H__ */ diff --git a/src/app.c b/src/app.c index f6d813f..53f4e8d 100644 --- a/src/app.c +++ b/src/app.c @@ -24,9 +24,6 @@ #include #include "log.h" #include "resource.h" -#include "receiver.h" -#include "message.h" -#include "connection_manager.h" #include "net-util.h" #include "config.h" #include "cloud/cloud_communication.h" @@ -118,7 +115,6 @@ static int ___map_servo_val(int servo) servo_max, servo_min, servo); } - static int __driving_motors(int servo, int speed) { int val_speed; @@ -138,35 +134,6 @@ static int __driving_motors(int servo, int speed) return 0; } -static gboolean __message_dispatcher(gpointer user_data) -{ - message_s *msg = NULL; - - do { - msg = message_pop_from_inqueue(); - if (msg) { - switch (msg->cmd) { - case MESSAGE_CMD_HELLO: - /* TODO : say hello to sender */ - break; - case MESSAGE_CMD_CALIBRATION: - /* TODO : set calibration mode */ - break; - case MESSAGE_CMD_DRIVE: - /* TODO : driving car */ - __driving_motors(msg->servo, msg->speed); - break; - case MESSAGE_CMD_BYE: - __driving_motors(0, 0); - break; - } - } - free(msg); - } while (msg); - - return TRUE; -} - static void __camera(int azimuth, int elevation) { //TODO: Camera steering @@ -192,32 +159,10 @@ static void __command_received_cb(command_s command) { } } -static void __conn_state_changed_cb(connection_state_e state, - const char *ip, void* user_data) -{ - app_data *ad = user_data; - - _D("connection state changed : %d", state); - - if (state == CONNECTION_STATE_CONNECTED) { - receiver_start(RECEIVER_TYPE_UDP); - - } else { - receiver_stop(RECEIVER_TYPE_UDP); - - if (ad->idle_h) { - g_source_remove(ad->idle_h); - ad->idle_h = 0; - } - - __driving_motors(0, 0); - } - return; -} - static void _initialize_config() { config_init(); + char *id = NULL; char *name = NULL; gboolean modified = false; diff --git a/src/cloud/cloud_communication.c b/src/cloud/cloud_communication.c index 59703f3..a54ba35 100644 --- a/src/cloud/cloud_communication.c +++ b/src/cloud/cloud_communication.c @@ -22,7 +22,6 @@ #include "cloud/cloud_request.h" #include "log.h" #include "config.h" -#include "connection_manager.h" #include "net-util.h" typedef struct communication_data_ { @@ -186,4 +185,4 @@ static void wifi_changed_cb(const char *ap_mac, const char *ap_ssid, char *ip_ad car_info_set_car_ap_mac(_communication.car_info, ap_mac); car_info_set_ap_ssid(_communication.car_info, ap_ssid); car_info_set_car_ip(_communication.car_info, ip_addr); -} \ No newline at end of file +} diff --git a/src/connection_manager.c b/src/connection_manager.c deleted file mode 100644 index 2ed6d1b..0000000 --- a/src/connection_manager.c +++ /dev/null @@ -1,237 +0,0 @@ -/* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Flora License, Version 1.1 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include - -#include "log.h" -#include "connection_manager.h" - -struct conn_mgr_s { - connection_h connection; - connection_type_e net_state; - connection_wifi_state_e wifi_state; - char *ip_addr; - connection_state_changed_cb state_cb; - void *state_cb_data; -}; - -struct conn_mgr_s conn_mgr = { - NULL, - CONNECTION_TYPE_DISCONNECTED, - CONNECTION_WIFI_STATE_DEACTIVATED, - NULL, - NULL, - NULL -}; - -static const char *__connection_error_to_string(connection_error_e error) -{ - switch (error) { - case CONNECTION_ERROR_NONE: - return "CONNECTION_ERROR_NONE"; - case CONNECTION_ERROR_INVALID_PARAMETER: - return "CONNECTION_ERROR_INVALID_PARAMETER"; - case CONNECTION_ERROR_OUT_OF_MEMORY: - return "CONNECTION_ERROR_OUT_OF_MEMORY"; - case CONNECTION_ERROR_INVALID_OPERATION: - return "CONNECTION_ERROR_INVALID_OPERATION"; - case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED: - return "CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED"; - case CONNECTION_ERROR_OPERATION_FAILED: - return "CONNECTION_ERROR_OPERATION_FAILED"; - case CONNECTION_ERROR_ITERATOR_END: - return "CONNECTION_ERROR_ITERATOR_END"; - case CONNECTION_ERROR_NO_CONNECTION: - return "CONNECTION_ERROR_NO_CONNECTION"; - case CONNECTION_ERROR_NOW_IN_PROGRESS: - return "CONNECTION_ERROR_NOW_IN_PROGRESS"; - case CONNECTION_ERROR_ALREADY_EXISTS: - return "CONNECTION_ERROR_ALREADY_EXISTS"; - case CONNECTION_ERROR_OPERATION_ABORTED: - return "CONNECTION_ERROR_OPERATION_ABORTED"; - case CONNECTION_ERROR_DHCP_FAILED: - return "CONNECTION_ERROR_DHCP_FAILED"; - case CONNECTION_ERROR_INVALID_KEY: - return "CONNECTION_ERROR_INVALID_KEY"; - case CONNECTION_ERROR_NO_REPLY: - return "CONNECTION_ERROR_NO_REPLY"; - case CONNECTION_ERROR_PERMISSION_DENIED: - return "CONNECTION_ERROR_PERMISSION_DENIED"; - case CONNECTION_ERROR_NOT_SUPPORTED: - return "CONNECTION_ERROR_NOT_SUPPORTED"; - default: - return "CONNECTION_ERROR_UNKNOWN"; - } -} - -static void __conn_mgr_connection_changed_cb(connection_type_e type, - void* user_data) -{ - int ret = CONNECTION_ERROR_NONE; - connection_state_e state = CONNECTION_STATE_DISCONNECTED; - _D("connection changed from[%d] to[%d]", conn_mgr.net_state, type); - - conn_mgr.net_state = type; - - ret = connection_get_wifi_state(conn_mgr.connection, - &conn_mgr.wifi_state); - - if (CONNECTION_ERROR_NONE != ret) - _E("failed to connection_get_wifi_state - [%s]", - __connection_error_to_string(ret)); - - free(conn_mgr.ip_addr); - conn_mgr.ip_addr = NULL; - - if (conn_mgr.net_state != CONNECTION_TYPE_DISCONNECTED) { - state = CONNECTION_STATE_CONNECTED; - ret = connection_get_ip_address(conn_mgr.connection, - CONNECTION_ADDRESS_FAMILY_IPV4, &conn_mgr.ip_addr); - - if ((CONNECTION_ERROR_NONE != ret) || (conn_mgr.ip_addr == NULL)) - _E("failed to connection_get_ip_address() - [%s]", - __connection_error_to_string(ret)); - } - - if (conn_mgr.state_cb) - conn_mgr.state_cb(state, conn_mgr.ip_addr, conn_mgr.state_cb_data); - - return; -} - -int connection_manager_get_ip(const char **ip) -{ - int ret = CONNECTION_ERROR_NONE; - - retv_if(conn_mgr.connection == NULL, -1); - retv_if(ip == NULL, -1); - - if (conn_mgr.ip_addr) { - *ip = conn_mgr.ip_addr; - return 0; - } - - if (conn_mgr.net_state == CONNECTION_TYPE_DISCONNECTED) { - _W("disconnected now"); - - free(conn_mgr.ip_addr); - conn_mgr.ip_addr = NULL; - - return -1; - } - - ret = connection_get_ip_address(conn_mgr.connection, - CONNECTION_ADDRESS_FAMILY_IPV4, &conn_mgr.ip_addr); - - if ((CONNECTION_ERROR_NONE != ret) || (conn_mgr.ip_addr == NULL)) { - _E("failed to connection_get_ip_address() - [%s]", - __connection_error_to_string(ret)); - return -1; - } - - *ip = conn_mgr.ip_addr; - - return 0; -} - -int connection_manager_init(void) -{ - int ret = CONNECTION_ERROR_NONE; - if (conn_mgr.connection) { - _W("connection manager is already initialized"); - return 0; - } - - ret = connection_create(&conn_mgr.connection); - if (CONNECTION_ERROR_NONE != ret) { - _E("failed to create connection - [%s]", - __connection_error_to_string(ret)); - return -1; - } - - ret = connection_get_type(conn_mgr.connection, &conn_mgr.net_state); - if (CONNECTION_ERROR_NONE != ret) { - _E("failed to connection_get_type - [%s]", - __connection_error_to_string(ret)); - } - - if (conn_mgr.net_state != CONNECTION_TYPE_DISCONNECTED) { - ret = connection_get_ip_address(conn_mgr.connection, - CONNECTION_ADDRESS_FAMILY_IPV4, &conn_mgr.ip_addr); - if ((CONNECTION_ERROR_NONE != ret) || (conn_mgr.ip_addr == NULL)) - _E("failed to connection_get_ip_address() - [%s]", - __connection_error_to_string(ret)); - } - - ret = connection_get_wifi_state(conn_mgr.connection, &conn_mgr.wifi_state); - if (CONNECTION_ERROR_NONE != ret) - _E("failed to connection_get_wifi_state - [%s]", - __connection_error_to_string(ret)); - - _D("net_state[%d], wifi_state[%d], ip address[%s]", - conn_mgr.net_state, conn_mgr.wifi_state, conn_mgr.ip_addr); - - ret = connection_set_type_changed_cb(conn_mgr.connection, - __conn_mgr_connection_changed_cb, &conn_mgr); - if (CONNECTION_ERROR_NONE != ret) - _E("failed to connection_set_type_changed_cb - [%s]", - __connection_error_to_string(ret)); - - return 0; -} - -int connection_manager_fini(void) -{ - if (conn_mgr.connection) { - int ret = 0; - ret = connection_destroy(conn_mgr.connection); - _D("connection_destroy - [%s]", __connection_error_to_string(ret)); - } - - conn_mgr.net_state = CONNECTION_TYPE_DISCONNECTED; - conn_mgr.wifi_state = CONNECTION_WIFI_STATE_DEACTIVATED; - - if (conn_mgr.ip_addr) { - free(conn_mgr.ip_addr); - conn_mgr.ip_addr = NULL; - } - - conn_mgr.state_cb = NULL; - conn_mgr.state_cb_data = NULL; - - return 0; -} - -int connection_manager_set_state_changed_cb( - connection_state_changed_cb state_cb, void *user_data) -{ - conn_mgr.state_cb = state_cb; - - if (state_cb) { - connection_state_e state = CONNECTION_STATE_DISCONNECTED; - - conn_mgr.state_cb_data = user_data; - if (conn_mgr.net_state != CONNECTION_TYPE_DISCONNECTED) - state = CONNECTION_STATE_CONNECTED; - - conn_mgr.state_cb(state, conn_mgr.ip_addr, conn_mgr.state_cb_data); - } else - conn_mgr.state_cb_data = NULL; - - return 0; -} diff --git a/src/message.c b/src/message.c deleted file mode 100644 index 2069f57..0000000 --- a/src/message.c +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Flora License, Version 1.1 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include - -#include "log.h" -#include "message.h" - -static unsigned long long int sequence_number = 0; -static GQueue inqueue = G_QUEUE_INIT; -static GQueue outqueue = G_QUEUE_INIT; - -static unsigned long long int __message_get_monotonic_time(void) -{ - unsigned long long int c_time = 0; - struct timespec ts; - int ret = 0; - - ret = clock_gettime(CLOCK_MONOTONIC, &ts); - if (ret) - _E("failed to get monotonic time"); - else - c_time = (((unsigned long long int)ts.tv_sec) * 1000000) - + (ts.tv_nsec / 1000); - - return c_time; -} - -int message_new_to_send(message_cmd_e cmd, - int servo, int speed, message_s *new_msg) -{ - retv_if(!new_msg, -1); - - new_msg->seq_num = sequence_number++; - new_msg->cmd = cmd; - new_msg->servo = servo; - new_msg->speed = speed; - new_msg->time = __message_get_monotonic_time(); - - if (new_msg->seq_num >= ULLONG_MAX) { - /* maybe never reach here */ - _W("seq number reachs max value, reset it to 0"); - sequence_number = 0; - } - - return 0; -} - -void message_reset_seq_num(void) -{ - sequence_number = 0; - - return; -} - -int message_queue_new(void) -{ - /* Do nothing because we use static queue - * if we use multiple thread to handling messages, - * message queue should be changed to thread-safe one. - */ - return 0; -} - -static void __queue_clear_cb(gpointer data, gpointer user_data) -{ - free(data); - return; -} - -void message_queue_clear(void) -{ - g_queue_foreach(&inqueue, __queue_clear_cb, NULL); - g_queue_clear(&inqueue); - - g_queue_foreach(&outqueue, __queue_clear_cb, NULL); - g_queue_clear(&outqueue); - - return; -} - -void message_push_to_inqueue(message_s *msg) -{ - g_queue_push_tail(&inqueue, msg); - _D("seq[%llu] is pushed to in-queue", msg->seq_num); - return; -} - -void message_push_to_outqueue(message_s *msg) -{ - g_queue_push_tail(&outqueue, msg); - _D("seq[%llu] is pushed to out-queue", msg->seq_num); - return; -} - -message_s *message_pop_from_inqueue(void) -{ - return (message_s *)g_queue_pop_head(&inqueue); -} - -message_s *message_pop_from_outqueue(void) -{ - return (message_s *)g_queue_pop_head(&outqueue); -} diff --git a/src/receiver.c b/src/receiver.c deleted file mode 100644 index 4841afd..0000000 --- a/src/receiver.c +++ /dev/null @@ -1,328 +0,0 @@ -/* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Flora License, Version 1.1 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include "log.h" -#include "receiver_type.h" -#include "receiver_internal.h" -#include "receiver_udp.h" - -typedef struct __receiver_h { - receiver_type_e receiver_type; - /* TODO */ -} receiver_h; - -struct __receiver_module_h { - receiver_type_e receiver_type; - void *module_data; - receiver_init_func init; - receiver_fini_func fini; - receiver_start_func start; - receiver_stop_func stop; - receiver_get_state_func get_state; - receiver_state_changed_cb state_change_cb; - void *state_change_data; -}; - -static GHashTable *receiver_module_hash = NULL; - -static void ___module_hash_destroy(gpointer data) -{ - receiver_module_h *module_h = data; - - module_h->fini(module_h); - free(module_h); - - return; -} - -int receiver_init(receiver_type_e type) -{ - int ret = 0; - receiver_module_h *handle = NULL; - - if (!receiver_module_hash) { - receiver_module_hash = g_hash_table_new_full(g_direct_hash, - g_direct_equal, NULL, ___module_hash_destroy); - - if (!receiver_module_hash) { - _E("failed to create hash table"); - return -1; - } - } else { - handle = g_hash_table_lookup(receiver_module_hash, - GUINT_TO_POINTER(type)); - - if (handle) { - _D("receiver [%d] type is already initialized", type); - return 0; - } - } - - handle = calloc(1, sizeof(struct __receiver_module_h)); - if (!handle) { - _D("failed to alloc handle memory"); - return -1; - } - - handle->receiver_type = type; - - switch (type) { - case RECEIVER_TYPE_UDP: - /* TODO */ - ret = receiver_udp_module_register(handle); - if (ret) - goto ERROR; - break; - case RECEIVER_TYPE_BLUETOOTH: - /* TODO : for bluetooth module */ - // ret = receiver_bluetooth_module_register(handle); - // if (ret) - // goto ERROR; - break; - } - - if (handle->init) { - ret = handle->init(handle); - if (ret) { - _E("failed to initialized type[%d]", type); - goto ERROR; - } - } else { - _W("receiver [%d] type is not implemented init func", type); - goto ERROR; - } - - g_hash_table_insert(receiver_module_hash, GUINT_TO_POINTER(type), handle); - - return 0; - -ERROR: - free(handle); - return -1; -} - -void receiver_fini(receiver_type_e type) -{ - receiver_module_h *handle = NULL; - guint hash_size = 0; - - if (!receiver_module_hash) - return; - - handle = g_hash_table_lookup(receiver_module_hash, - GUINT_TO_POINTER(type)); - - if (!handle) { - _D("receiver [%d] type is not initialized", type); - return; - } - - if (!handle->fini) - handle->fini(handle); - - g_hash_table_remove(receiver_module_hash, GUINT_TO_POINTER(type)); - - hash_size = g_hash_table_size(receiver_module_hash); - if (hash_size == 0) { - g_hash_table_unref(receiver_module_hash); - receiver_module_hash = NULL; - } - - return; -} - -int receiver_start(receiver_type_e type) -{ - receiver_module_h *handle = NULL; - - if (!receiver_module_hash) { - _E("receiver is not initialized"); - return -1; - } - - handle = g_hash_table_lookup(receiver_module_hash, - GUINT_TO_POINTER(type)); - - if (!handle) { - _E("receiver [%d] type is not initialized", type); - return -1; - } - - if (!handle->start) { - _D("receiver [%d] type is not implemented start func", type); - return -1; - } - - return handle->start(handle); -} - -int receiver_stop(receiver_type_e type) -{ - receiver_module_h *handle = NULL; - - if (!receiver_module_hash) { - _E("receiver is not initialized"); - return -1; - } - - handle = g_hash_table_lookup(receiver_module_hash, - GUINT_TO_POINTER(type)); - - if (!handle) { - _E("receiver [%d] type is not initialized", type); - return -1; - } - - if (!handle->stop) { - _D("receiver [%d] type is not implemented stop func", type); - return -1; - } - - return handle->stop(handle); -} - -receiver_state_e receiver_get_state(receiver_type_e type) -{ - receiver_module_h *handle = NULL; - - if (!receiver_module_hash) { - _E("receiver is not initialized"); - return RECEIVER_STATE_NONE; - } - - handle = g_hash_table_lookup(receiver_module_hash, - GUINT_TO_POINTER(type)); - - if (!handle) { - _E("receiver [%d] type is not initialized", type); - return RECEIVER_STATE_NONE; - } - - if (!handle->get_state) { - _D("receiver [%d] type is not implemented get_state func", type); - return RECEIVER_STATE_NONE; - } - - return handle->get_state(handle); -} - -int receiver_set_state_changed_cb(receiver_type_e type, - receiver_state_changed_cb callback, void *user_data) -{ - receiver_module_h *handle = NULL; - - if (!receiver_module_hash) { - _E("receiver is not initialized"); - return -1; - } - - handle = g_hash_table_lookup(receiver_module_hash, - GUINT_TO_POINTER(type)); - - if (!handle) { - _E("receiver [%d] type is not initialized", type); - return -1; - } - - handle->state_change_cb = callback; - - if (callback) - handle->state_change_data = user_data; - else - handle->state_change_data = NULL; - - return 0; -} - -int receiver_set_module_data(receiver_module_h *handle, void *module_data) -{ - retv_if(!handle, -1); - - handle->module_data = module_data; - - return 0; -} - -void *receiver_get_module_data(receiver_module_h *handle) -{ - retv_if(!handle, NULL); - - return handle->module_data; -} - -int receiver_set_module_init_function( - receiver_module_h *handle, receiver_init_func func) -{ - retv_if(!handle, -1); - - handle->init = func; - - return 0; -} - -int receiver_set_module_fini_function( - receiver_module_h *handle, receiver_fini_func func) -{ - retv_if(!handle, -1); - - handle->fini = func; - - return 0; -} - -int receiver_set_module_start_function( - receiver_module_h *handle, receiver_start_func func) -{ - retv_if(!handle, -1); - - handle->start = func; - - return 0; -} - -int receiver_set_module_stop_function( - receiver_module_h *handle, receiver_stop_func func) -{ - retv_if(!handle, -1); - - handle->stop = func; - - return 0; -} - -int receiver_set_module_get_state_function( - receiver_module_h *handle, receiver_get_state_func func) -{ - retv_if(!handle, -1); - - handle->get_state = func; - - return 0; -} - -void receiver_module_state_changed( - receiver_module_h *handle, receiver_state_e state) -{ - ret_if(!handle); - - if (handle->state_change_cb) - handle->state_change_cb(handle->receiver_type, - state, handle->state_change_data); - - return; -} diff --git a/src/receiver_udp.c b/src/receiver_udp.c deleted file mode 100644 index 07c2028..0000000 --- a/src/receiver_udp.c +++ /dev/null @@ -1,477 +0,0 @@ -/* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Flora License, Version 1.1 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "log.h" -#include "receiver_internal.h" -#include "message.h" - -#define RECEIVER_UDP_PORT 57984 -#define RECEIVER_UDP_WAIT_TIMEOUT 3 - -typedef enum __receiver_udp_state_e { - RECEIVER_UDP_STATE_NONE, - RECEIVER_UDP_STATE_INIT, - RECEIVER_UDP_STATE_READY, - RECEIVER_UDP_STATE_CONNECTED, -} receiver_udp_state_e; - -typedef struct __receiver_udp_h { - guint io_watch_id; - guint wait_timer_id; - receiver_udp_state_e state; - GSocket *socket; -} receiver_udp_h; - -// static receiver_udp_h *udp_handle = NULL; - -static gchar *__socket_address_to_string(GSocketAddress *address) -{ - GInetAddress *inet_address = NULL; - char *str, *res; - int port; - - inet_address = - g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(address)); - - str = g_inet_address_to_string(inet_address); - port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(address)); - - res = g_strdup_printf("%s:%d", str, port); - g_free(str); - - return res; -} - -static receiver_state_e ___state_convert(receiver_udp_state_e state) -{ - receiver_state_e r_state = RECEIVER_STATE_NONE; - - switch (state) { - case RECEIVER_UDP_STATE_NONE: - r_state = RECEIVER_STATE_NONE; - break; - case RECEIVER_UDP_STATE_INIT: - r_state = RECEIVER_STATE_INIT; - break; - case RECEIVER_UDP_STATE_READY: - r_state = RECEIVER_STATE_READY; - break; - case RECEIVER_UDP_STATE_CONNECTED: - r_state = RECEIVER_STATE_CONNECTED; - break; - } - return r_state; -} - -static void __receiver_udp_state_set( - receiver_module_h *handle, receiver_udp_state_e state) -{ - receiver_udp_h *udp_handle = NULL; - - ret_if(!handle); - - udp_handle = receiver_get_module_data(handle); - ret_if(!udp_handle); - - udp_handle->state = state; - - receiver_module_state_changed(handle, ___state_convert(state)); - - return; -} - -/* Uses system call, because glib socket API doesn't support unset connect - * Please carefully use this function, and after use this function, - * DO NOT use g_socket_is_connected(). - */ -static int __receiver_udp_unset_connection(receiver_module_h *handle) -{ - receiver_udp_h *udp_handle = NULL; - struct sockaddr addr; - int s_fd = 0; - - retv_if(!handle, -1); - - udp_handle = receiver_get_module_data(handle); - - retvm_if(!udp_handle, -1, "handle is not created"); - retvm_if(!udp_handle->socket, -1, "socket is not created"); - - s_fd = g_socket_get_fd(udp_handle->socket); - bzero((char *)&addr, sizeof(addr)); - addr.sa_family = AF_UNSPEC; - - if (connect(s_fd, &addr, sizeof(addr))) { - _E("failed to unset connect - %s\n", strerror(errno)); - /* re-create socket or not ??? */ - return -1; - } - - __receiver_udp_state_set(handle, RECEIVER_UDP_STATE_READY); - - return 0; -} - -static int __receiver_udp_set_connection( - receiver_module_h *handle, GSocketAddress *sender_a) -{ - GError *error = NULL; - receiver_udp_h *udp_handle = NULL; - - retv_if(!handle, -1); - retv_if(!sender_a, -1); - - udp_handle = receiver_get_module_data(handle); - - retvm_if(!udp_handle, -1, "handle is not created"); - retvm_if(!udp_handle->socket, -1, "socket is not created"); - - if (udp_handle->state != RECEIVER_UDP_STATE_READY) { - _E("check state %d", udp_handle->state); - return -1; - } - - /* use connect() to specify sender address and reject other sender */ - if (!g_socket_connect(udp_handle->socket, sender_a, NULL, &error)) { - _E("failed to connect - %s", error->message); - g_error_free(error); - return -1; - } - - __receiver_udp_state_set(handle, RECEIVER_UDP_STATE_CONNECTED); - - return 0; -} - -static gboolean __wait_time_out(gpointer user_data) -{ - receiver_module_h *handle = user_data; - - retv_if(!handle, FALSE); - - __receiver_udp_unset_connection(handle); - - return FALSE; -} - -static void __receiver_udp_update_wait_timer(receiver_module_h *handle) -{ - receiver_udp_h *udp_handle = NULL; - - ret_if(!handle); - - udp_handle = receiver_get_module_data(handle); - - if (udp_handle) { - if (udp_handle->wait_timer_id) { - g_source_remove(udp_handle->wait_timer_id); - udp_handle->wait_timer_id = 0; - } - udp_handle->wait_timer_id = - g_timeout_add_seconds(RECEIVER_UDP_WAIT_TIMEOUT, - (GSourceFunc)__wait_time_out, handle); - } - - return; -} - -static gboolean __read_socket(GIOChannel *channel, - GIOCondition condition, - gpointer data) -{ - receiver_module_h *handle = data; - receiver_udp_h *udp_handle = NULL; - GError *error = NULL; - message_s *r_msg = NULL; - gssize size = 0; - - retv_if(!handle, TRUE); - - udp_handle = receiver_get_module_data(handle); - - retv_if(!udp_handle, TRUE); - retv_if(!udp_handle->socket, TRUE); - - if (udp_handle->state < RECEIVER_UDP_STATE_READY) { - _E("receiver udp is not ready yet"); - return TRUE; - } - - r_msg = malloc(sizeof(message_s)); - retv_if(!r_msg, TRUE); - - if (udp_handle->state == RECEIVER_UDP_STATE_READY) { - char *s_addr = NULL; - GSocketAddress *address = NULL; - - size = g_socket_receive_from(udp_handle->socket, &address, - (gchar *)r_msg, sizeof(message_s), NULL, &error); - - if (size < 0) { - _D("Error receiving from socket: %s", error->message); - g_error_free(error); - free(r_msg); - r_msg = NULL; - } - - s_addr = __socket_address_to_string(address); - _D("received first data from [%s]", s_addr); - - message_push_to_inqueue(r_msg); - - if (!__receiver_udp_set_connection(handle, address)) - __receiver_udp_update_wait_timer(handle); - else - _E("failed to set connection with [%s]", s_addr); - - free(s_addr); - g_object_unref(address); - } else { /* state is RECEIVER_UDP_STATE_CONNECTED */ - size = g_socket_receive(udp_handle->socket, - (gchar *)r_msg, sizeof(message_s), NULL, &error); - - if (size < 0) { - _D("Error receiving from socket: %s", error->message); - g_error_free(error); - free(r_msg); - r_msg = NULL; - } - _D("received data"); - message_push_to_inqueue(r_msg); - - __receiver_udp_update_wait_timer(handle); - } - - /* TODO : what should I do after receiveing some data? */ - - return TRUE; -} - -static int _receiver_udp_start(void *data) -{ - receiver_module_h *handle = data; - receiver_udp_h *udp_handle = NULL; - int socket_fd = 0; - GIOChannel *ch = NULL; - - retv_if(!handle, -1); - - udp_handle = receiver_get_module_data(handle); - - retv_if(!udp_handle, -1); - retv_if(!udp_handle->socket, -1); - - if (udp_handle->state != RECEIVER_UDP_STATE_INIT) { - if (udp_handle->state == RECEIVER_UDP_STATE_READY) { - _E("receiver udp is already started"); - return 0; - } else { - _E("receiver udp is invalid state [%d]", udp_handle->state); - return -1; - } - } - - socket_fd = g_socket_get_fd(udp_handle->socket); - ch = g_io_channel_unix_new(socket_fd); - udp_handle->io_watch_id = - g_io_add_watch(ch, G_IO_IN, __read_socket, handle); - g_io_channel_unref(ch); - ch = NULL; - - __receiver_udp_state_set(handle, RECEIVER_UDP_STATE_READY); - - return 0; -} - -static int _receiver_udp_stop(void *data) -{ - receiver_module_h *handle = data; - receiver_udp_h *udp_handle = NULL; - - retv_if(!handle, -1); - - udp_handle = receiver_get_module_data(handle); - - retv_if(!udp_handle, -1); - retv_if(!udp_handle->socket, -1); - - if (udp_handle->state < RECEIVER_UDP_STATE_READY) { - _E("receiver udp is invalid state [%d]", udp_handle->state); - return -1; - } - - if (udp_handle->wait_timer_id) { - g_source_remove(udp_handle->wait_timer_id); - udp_handle->wait_timer_id = 0; - } - - if (udp_handle->io_watch_id) { - g_source_remove(udp_handle->io_watch_id); - udp_handle->io_watch_id = 0; - } - - __receiver_udp_state_set(handle, RECEIVER_UDP_STATE_INIT); - - return 0; -} - -static int _receiver_udp_init(void *data) -{ - receiver_module_h *handle = data; - receiver_udp_h *udp_handle = NULL; - GError *error = NULL; - GSocketAddress *address = NULL; - GInetAddress *i_addr = NULL; - - retv_if(!handle, -1); - - udp_handle = receiver_get_module_data(handle); - retv_if(!udp_handle, -1); - - if (udp_handle->state != RECEIVER_UDP_STATE_NONE) { - _E("receiver udp is invalid state [%d]", udp_handle->state); - return -1; - } - - udp_handle->socket = g_socket_new(G_SOCKET_FAMILY_IPV4, - G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, &error); - - if (udp_handle->socket == NULL) { - _E("failed to get new socket - %s", error->message); - goto ERROR; - } - - /* set non-blocking mode */ - g_socket_set_blocking(udp_handle->socket, FALSE); - - i_addr = g_inet_address_new_any(G_SOCKET_FAMILY_IPV4); - if (!i_addr) { - _E("failed to get inet any address"); - goto ERROR; - } - address = g_inet_socket_address_new(i_addr, RECEIVER_UDP_PORT); - g_object_unref(i_addr); - i_addr = NULL; - - if (!address) { - _E("failed to get socket address"); - goto ERROR; - } - - if (!g_socket_bind(udp_handle->socket, address, TRUE, &error)) { - _E("Can't bind socket: %s\n", error->message); - goto ERROR; - } - g_object_unref(address); - address = NULL; - - __receiver_udp_state_set(handle, RECEIVER_UDP_STATE_INIT); - - return 0; - -ERROR: - if (error) - g_error_free(error); - - if (address) - g_object_unref(address); - - if (i_addr) - g_object_unref(i_addr); - - return -1; -} - -static int _receiver_udp_fini(void *data) -{ - receiver_module_h *handle = data; - receiver_udp_h *udp_handle = NULL; - - retv_if(!handle, -1); - - udp_handle = receiver_get_module_data(handle); - retv_if(!udp_handle, -1); - - if (udp_handle) { - if (udp_handle->io_watch_id) - g_source_remove(udp_handle->io_watch_id); - - if (udp_handle->wait_timer_id) - g_source_remove(udp_handle->wait_timer_id); - - if (udp_handle->socket) { - g_socket_close(udp_handle->socket, NULL); - g_object_unref(udp_handle->socket); - } - - __receiver_udp_state_set(handle, RECEIVER_UDP_STATE_NONE); - - free(udp_handle); - udp_handle = NULL; - } - - return 0; -} - -static receiver_state_e _receiver_udp_get_state(void *data) -{ - receiver_module_h *handle = data; - receiver_udp_h *udp_handle = NULL; - - retv_if(!handle, RECEIVER_STATE_NONE); - - udp_handle = receiver_get_module_data(handle); - retv_if(!udp_handle, RECEIVER_STATE_NONE); - - return ___state_convert(udp_handle->state); -} - -/* Keep it here??? or move to new file??? */ -int receiver_udp_module_register(receiver_module_h *handle) -{ - receiver_udp_h *udp_handle = NULL; - - retv_if(!handle, -1); - - udp_handle = malloc(sizeof(receiver_udp_h)); - if (!udp_handle) { - _E("failed to alloc receiver udp handle"); - return -1; - } - - udp_handle->state = RECEIVER_UDP_STATE_NONE; - udp_handle->io_watch_id = 0; - udp_handle->wait_timer_id = 0; - udp_handle->socket = NULL; - - receiver_set_module_data(handle, udp_handle); - receiver_set_module_init_function(handle, _receiver_udp_init); - receiver_set_module_fini_function(handle, _receiver_udp_fini); - receiver_set_module_start_function(handle, _receiver_udp_start); - receiver_set_module_stop_function(handle, _receiver_udp_stop); - receiver_set_module_get_state_function(handle, _receiver_udp_get_state); - - return 0; -}