From: Kitae Kim Date: Mon, 11 Aug 2014 09:24:15 +0000 (+0900) Subject: tethering: add functions to get connected ip address and port X-Git-Tag: TizenStudio_2.0_p3.0~407^2~72^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c0f16227206af7b485847125846d5eb21186e853;p=sdk%2Femulator%2Fqemu.git tethering: add functions to get connected ip address and port Those functions are required to give connection infomation to ECP-UI and ECP-CLI. Change-Id: I5d38f038a53a721491a1a0eab2ce90e21dedca96 Signed-off-by: Kitae Kim --- diff --git a/tizen/src/ecs/ecs_tethering.c b/tizen/src/ecs/ecs_tethering.c index 52cab9e..cff8843 100644 --- a/tizen/src/ecs/ecs_tethering.c +++ b/tizen/src/ecs/ecs_tethering.c @@ -72,6 +72,7 @@ void send_tethering_connection_status_ecp(void) ECS_TETHERING_MSG_ACTION_CONNECTION_STATUS); } +#if 0 static void send_tethering_port_ecp(void) { type_length length; @@ -104,6 +105,55 @@ static void send_tethering_port_ecp(void) g_free(msg); } } +#endif + +static void send_tethering_connection_info(void) +{ + type_length length; + type_group group = ECS_TETHERING_MSG_GROUP_ECP; + type_action action = ECS_TETHERING_MSG_ACTION_CONNECT; + uint8_t *msg = NULL; + gchar data[64]; + + msg = g_malloc(MSG_BUF_SIZE); + if (!msg) { + LOG_SEVERE("failed to allocate memory\n"); + return; + } + + LOG_INFO(">> send port_num: %d\n", tethering_port); + { + const char *ip = get_tethering_connected_ipaddr(); + int port = get_tethering_connected_port(); + + if (!ip) { + LOG_SEVERE("invalid connected ip\n"); + return; + } + + if (!port) { + LOG_SEVERE("invalid connected port\n"); + return; + } + g_snprintf(data, sizeof(data) - 1, "%s:%d", ip, port); + length = strlen(data); + data[length] = '\0'; + } + + memcpy(msg, ECS_TETHERING_MSG_CATEGORY, 10); + memcpy(msg + 10, &length, sizeof(unsigned short)); + memcpy(msg + 12, &group, sizeof(unsigned char)); + memcpy(msg + 13, &action, sizeof(unsigned char)); + memcpy(msg + 14, data, length); + + LOG_INFO(">> send connection msg to ecp. " + "action=%d, group=%d, data=%s length=%d\n", + action, group, data, length); + + send_tethering_ntf((const char *)msg); + + g_free(msg); +} static void send_tethering_status_ntf(type_group group, type_action action) { @@ -116,7 +166,7 @@ static void send_tethering_status_ntf(type_group group, type_action action) case ECS_TETHERING_MSG_ACTION_CONNECTION_STATUS: status = get_tethering_connection_status(); if (status == CONNECTED) { - send_tethering_port_ecp(); + send_tethering_connection_info(); } break; case ECS_TETHERING_MSG_ACTION_SENSOR_STATUS: @@ -229,7 +279,7 @@ bool msgproc_tethering_req(ECS_Client* ccli, ECS__TetheringReq* msg) gchar cmd[10] = {0}; gchar **server_addr = NULL; - LOG_INFO("enter %s\n", __func__); + LOG_TRACE("enter %s\n", __func__); g_strlcpy(cmd, msg->category, sizeof(cmd)); type_length length = (type_length) msg->length; @@ -265,7 +315,6 @@ bool msgproc_tethering_req(ECS_Client* ccli, ECS__TetheringReq* msg) } else { LOG_SEVERE("failed to parse port number\n"); } - LOG_INFO("len = %zd, data\" %s\"", strlen(data), data); connect_tethering_app(ip_address, port); @@ -293,7 +342,7 @@ bool msgproc_tethering_req(ECS_Client* ccli, ECS__TetheringReq* msg) break; } - LOG_INFO("leave %s\n", __func__); + LOG_TRACE("leave %s\n", __func__); return true; } diff --git a/tizen/src/ecs/ecs_tethering.h b/tizen/src/ecs/ecs_tethering.h index f56bd05..3d1b3af 100644 --- a/tizen/src/ecs/ecs_tethering.h +++ b/tizen/src/ecs/ecs_tethering.h @@ -52,4 +52,4 @@ void send_tethering_sensor_data(const char *data, int len); void send_tethering_touch_data(int x, int y, int index, int status); -void send_tethering_hwkey_data(int keycode); +void send_tethering_hwkey_data(int keycode); \ No newline at end of file diff --git a/tizen/src/tethering/common.c b/tizen/src/tethering/common.c index 0d28923..60ede59 100644 --- a/tizen/src/tethering/common.c +++ b/tizen/src/tethering/common.c @@ -27,6 +27,7 @@ * - S-Core Co., Ltd * */ + #ifndef __WIN32 #include #else @@ -60,7 +61,13 @@ DECLARE_DEBUG_CHANNEL(app_tethering); #endif #define SEND_BUF_MAX_SIZE 4096 -static const char *loopback="127.0.0.1"; +static const char *loopback = "127.0.0.1"; + +enum connection_type { + NONE = 0, + USB, + WIFI, +}; typedef struct tethering_recv_buf { uint32_t len; @@ -77,6 +84,7 @@ typedef struct _TetheringState { // connection state int status; + int type; // receiver handling thread QemuThread thread; @@ -90,13 +98,14 @@ typedef struct _TetheringState { } TetheringState; -static TetheringState *tethering_client; +static TetheringState *tethering_client = NULL; static tethering_recv_buf recv_buf; -// static bool app_state = false; static void end_tethering_socket(int sockfd); static void set_tethering_connection_status(int status); #if 0 +static bool app_state = false; + static void set_tethering_app_state(bool state); static bool get_tethering_app_state(void); #endif @@ -302,8 +311,7 @@ static bool send_set_event_status_msg(Tethering__EventType event_type, // message handlers static void msgproc_tethering_handshake_ans(Tethering__HandShakeAns *msg) { - // FIXME: handle handshake answer - // ans = msg->result; + // handle handshake answer } static void msgproc_app_state_msg(Tethering__AppState *msg) @@ -318,8 +326,6 @@ static void msgproc_app_state_msg(Tethering__AppState *msg) set_tethering_touch_status(status); disconnect_tethering_app(); - } else { - // does nothing } } @@ -552,17 +558,15 @@ static void tethering_io_handler(void *opaque) static int start_tethering_socket(const char *ipaddress, int port) { struct sockaddr_in addr; - int sock = -1; - int ret = 0; + int sock = -1, ret = 0; addr.sin_family = AF_INET; addr.sin_port = htons(port); // i.e. 1234 LOG_INFO("server ip address: %s, port: %d\n", ipaddress, port); ret = inet_aton(ipaddress, &addr.sin_addr); - if (ret == 0) { - LOG_SEVERE("inet_aton failure\n"); + perror("inet_aton failure\n"); return -1; } @@ -682,16 +686,42 @@ int get_tethering_connection_status(void) int status = 0; if (!tethering_client) { - return -1; + LOG_INFO("tethering_client is null\n"); + LOG_INFO("tetherging connection status: %d\n", status); + return DISCONNECTED; } qemu_mutex_lock(&tethering_client->mutex); status = tethering_client->status; qemu_mutex_unlock(&tethering_client->mutex); + LOG_INFO("tetherging connection status: %d\n", status); + return status; } +int get_tethering_connected_port(void) +{ + if (!tethering_client) { + LOG_SEVERE("tethering_client is null\n"); + return 0; + } + + LOG_TRACE("connected port: %d\n", tethering_client->port); + return tethering_client->port; +} + +const char *get_tethering_connected_ipaddr(void) +{ + if (!tethering_client) { + LOG_SEVERE("tethering client is null\n"); + return NULL; + } + + LOG_TRACE("connected ip address: %s\n", tethering_client->ipaddress); + return tethering_client->ipaddress; +} + static void set_tethering_connection_status(int status) { if (!tethering_client) { @@ -728,22 +758,27 @@ int connect_tethering_app(const char *ipaddress, int port) if (ipaddress) { ipaddr_len = strlen(ipaddress); + client->type = WIFI; } else { - // ipaddr_len = strlen(LOOPBACK); ipaddr_len = strlen(loopback); ipaddress = loopback; + client->type = USB; } client->ipaddress = g_malloc0(ipaddr_len + 1); if (!client->ipaddress) { + LOG_SEVERE("failed to allocate ipaddress buffer\n"); g_free(client); return -1; } + g_strlcpy(client->ipaddress, ipaddress, ipaddr_len + 1); + LOG_INFO("connection info. ip %s, port %d type %d\n", + client->ipaddress, client->port, client->type); tethering_client = client; - qemu_mutex_init(&tethering_client->mutex); + qemu_mutex_init(&tethering_client->mutex); qemu_thread_create(&tethering_client->thread, "tethering-io-thread", initialize_tethering_socket, client, QEMU_THREAD_DETACHED); @@ -763,7 +798,7 @@ int disconnect_tethering_app(void) sock = tethering_client->fd; if (sock < 0) { - LOG_SEVERE("tethering socket is terminated or not ready\n"); + LOG_SEVERE("tethering socket is already terminated or not ready\n"); return -1; } else { send_emul_state_msg(); diff --git a/tizen/src/tethering/common.h b/tizen/src/tethering/common.h index 3a882c8..a75fa70 100644 --- a/tizen/src/tethering/common.h +++ b/tizen/src/tethering/common.h @@ -28,19 +28,9 @@ * */ -#include "qemu/queue.h" - -typedef struct input_device_list { - int type; - void *opaque; - - QTAILQ_ENTRY(input_device_list) node; -} input_device_list; - -// common enum connection_status { - CONNECTED = 1, - DISCONNECTED, + DISCONNECTED = 0, + CONNECTED, CONNECTING, CONNREFUSED, }; @@ -58,4 +48,6 @@ int disconnect_tethering_app(void); int get_tethering_connection_status(void); -int add_input_device(void *opaque); +int get_tethering_connected_port(void); + +const char *get_tethering_connected_ipaddr(void);