ecs: added guest emuld connection message handling 04/23204/2
authorJinhyung Choi <jinhyung2.choi@samsung.com>
Mon, 16 Jun 2014 01:42:43 +0000 (10:42 +0900)
committerJinhyung Choi <jinhyung2.choi@samsung.com>
Mon, 23 Jun 2014 06:08:32 +0000 (15:08 +0900)
Change-Id: I48d6ae2aa7c033a52cf45dfd7ce11d0e082aef6d
Signed-off-by: Jinhyung Choi <jinhyung2.choi@samsung.com>
tizen/src/ecs/ecs.c
tizen/src/ecs/ecs.h
tizen/src/ecs/ecs_msg.c

index 310900a..27b94a1 100644 (file)
@@ -29,7 +29,6 @@
  */
 
 #include <stdbool.h>
-#include <pthread.h>
 #include <stdlib.h>
 
 #include "hw/qdev.h"
@@ -73,7 +72,10 @@ static int payloadsize;
 
 static int g_client_id = 1;
 
-static pthread_mutex_t mutex_clilist = PTHREAD_MUTEX_INITIALIZER;
+static QemuMutex mutex_clilist;
+QemuMutex mutex_guest_connection;
+
+static QemuThread ecs_thread_id;
 
 static int suspend_state = 1;
 
@@ -100,7 +102,7 @@ void ecs_client_close(ECS_Client* clii) {
     if (clii == NULL)
         return;
 
-    pthread_mutex_lock(&mutex_clilist);
+    qemu_mutex_lock(&mutex_clilist);
 
     if (clii->client_fd > 0) {
         INFO("ecs client closed with fd: %d\n", clii->client_fd);
@@ -116,12 +118,12 @@ void ecs_client_close(ECS_Client* clii) {
     g_free(clii);
     clii = NULL;
 
-    pthread_mutex_unlock(&mutex_clilist);
+    qemu_mutex_unlock(&mutex_clilist);
 }
 
 bool send_to_all_client(const char* data, const int len) {
     TRACE("data len: %d, data: %s\n", len, data);
-    pthread_mutex_lock(&mutex_clilist);
+    qemu_mutex_lock(&mutex_clilist);
 
     ECS_Client *clii,*next;
 
@@ -129,16 +131,16 @@ bool send_to_all_client(const char* data, const int len) {
     {
         send_to_client(clii->client_fd, data, len);
     }
-    pthread_mutex_unlock(&mutex_clilist);
+    qemu_mutex_unlock(&mutex_clilist);
 
     return true;
 }
 
 void send_to_single_client(ECS_Client *clii, const char* data, const int len)
 {
-    pthread_mutex_lock(&mutex_clilist);
+    qemu_mutex_lock(&mutex_clilist);
     send_to_client(clii->client_fd, data, len);
-    pthread_mutex_unlock(&mutex_clilist);
+    qemu_mutex_unlock(&mutex_clilist);
 }
 
 void send_to_client(int fd, const char* data, const int len)
@@ -232,6 +234,9 @@ static void ecs_close(ECS_State *cs) {
     g_free(cs);
     cs = NULL;
     current_ecs = NULL;
+
+    qemu_mutex_destroy(&mutex_clilist);
+    qemu_mutex_destroy(&mutex_guest_connection);
 }
 
 #ifndef _WIN32
@@ -415,13 +420,13 @@ static int ecs_add_client(ECS_State *cs, int fd) {
     FD_SET(fd, &cs->reads);
 #endif
 
-    pthread_mutex_lock(&mutex_clilist);
+    qemu_mutex_lock(&mutex_clilist);
 
     QTAILQ_INSERT_TAIL(&clients, clii, next);
 
     TRACE("Add an ecs client. fd: %d\n", fd);
 
-    pthread_mutex_unlock(&mutex_clilist);
+    qemu_mutex_unlock(&mutex_clilist);
 
 //    send_ecs_version_check(clii);
 
@@ -710,27 +715,36 @@ static void* ecs_initialize(void* args) {
     current_ecs = cs;
     cs->ecs_running = 1;
 
+    qemu_mutex_init(&mutex_clilist);
+    qemu_mutex_init(&mutex_guest_connection);
+
     TRACE("ecs_loop entered.\n");
     while (cs->ecs_running) {
         ret = ecs_loop(cs);
         if (0 > ret) {
-            ecs_close(cs);
             break;
         }
     }
     TRACE("ecs_loop exited.\n");
 
+    ecs_close(cs);
+
     return NULL;
 }
 
 static int stop_ecs(void) {
+    void *ret = NULL;
+
     INFO("ecs is closing.\n");
     if (NULL != current_ecs) {
         current_ecs->ecs_running = 0;
-    //    ecs_close(current_ecs);
     }
 
-    pthread_mutex_destroy(&mutex_clilist);
+    ret = qemu_thread_join(&ecs_thread_id);
+    if (ret) {
+        ERR("ecs is failed to join thread.\n");
+        return -1;
+    }
 
     return 0;
 }
@@ -741,12 +755,7 @@ static void ecs_notify_exit(Notifier *notifier, void *data) {
 static Notifier ecs_exit = { .notify = ecs_notify_exit };
 
 int start_ecs(void) {
-    pthread_t thread_id;
-
-    if (0 != pthread_create(&thread_id, NULL, ecs_initialize, NULL)) {
-        ERR("pthread creation failed.\n");
-        return -1;
-    }
+    qemu_thread_create(&ecs_thread_id, "ecs", ecs_initialize, NULL, QEMU_THREAD_JOINABLE);
 
     emulator_add_exit_notifier(&ecs_exit);
 
@@ -787,7 +796,7 @@ bool handle_protobuf_msg(ECS_Client* cli, char* data, int len)
         if (!msg)
             goto fail;
 
-        pthread_mutex_lock(&mutex_clilist);
+        qemu_mutex_lock(&mutex_clilist);
         if(cli->client_type == TYPE_NONE) {
             if (!strncmp(msg->category, MSG_TYPE_NFC, 3)) {
                 QTAILQ_REMOVE(&clients, cli, next);
@@ -810,11 +819,11 @@ bool handle_protobuf_msg(ECS_Client* cli, char* data, int len)
             }
             else {
                 ERR("unsupported category is found: %s\n", msg->category);
-                pthread_mutex_unlock(&mutex_clilist);
+                qemu_mutex_unlock(&mutex_clilist);
                 goto fail;
             }
         }
-        pthread_mutex_unlock(&mutex_clilist);
+        qemu_mutex_unlock(&mutex_clilist);
 
         msgproc_nfc_req(cli, msg);
     }
index 6a28958..1dfd862 100644 (file)
@@ -64,6 +64,7 @@
 #define MSG_TYPE_NFC            "nfc"
 #define MSG_TYPE_SIMUL_NFC      "simul_nfc"
 #define MSG_TYPE_SDCARD         "sdcard"
+#define MSG_TYPE_GUEST          "guest"
 
 #define MSG_GROUP_STATUS        15
 
index 23f9df1..573e48f 100644 (file)
@@ -28,7 +28,6 @@
  */
 
 #include <stdbool.h>
-#include <pthread.h>
 #include <glib.h>
 
 #include "hw/qdev.h"
 #include "emul_state.h"
 
 #include "debug_ch.h"
-MULTI_DEBUG_CHANNEL(qemu, ecs);
+MULTI_DEBUG_CHANNEL(qemu, ecs-msg);
 
 // utility functions
+static int guest_connection = 0;
+extern QemuMutex mutex_guest_connection;
 
 /*static function define*/
 static void handle_sdcard(char* dataBuf, size_t dataLen);
@@ -165,13 +166,18 @@ static void send_status_injector_ntf(const char* cmd, int cmdlen, int act, char*
     type_group group = GROUP_STATUS;
     type_action action = act;
 
-    if (cmd == NULL || on == NULL || cmdlen > 10)
+    if (cmd == NULL || cmdlen > 10)
         return;
 
-    datalen = strlen(on);
-    length  = (unsigned short)datalen;
+    if (on == NULL) {
+        msglen = 14;
+    } else {
+        datalen = strlen(on);
+        length  = (unsigned short)datalen;
+
+        msglen = datalen + 15;
+    }
 
-    msglen = datalen + 15;
     char* status_msg = (char*) malloc(msglen);
     if(!status_msg)
         return;
@@ -182,7 +188,10 @@ static void send_status_injector_ntf(const char* cmd, int cmdlen, int act, char*
     memcpy(status_msg + 10, &length, sizeof(unsigned short));
     memcpy(status_msg + 12, &group, sizeof(unsigned char));
     memcpy(status_msg + 13, &action, sizeof(unsigned char));
-    memcpy(status_msg + 14, on, datalen);
+
+    if (on != NULL) {
+        memcpy(status_msg + 14, on, datalen);
+    }
 
     send_injector_ntf(status_msg, msglen);
 
@@ -259,6 +268,7 @@ bool msgproc_injector_req(ECS_Client* ccli, ECS__InjectorReq* msg)
     char data[10];
     bool ret = false;
     int sndlen = 0;
+    int value = 0;
     char* sndbuf;
     memset(cmd, 0, 10);
     strncpy(cmd, msg->category, sizeof(cmd) -1);
@@ -299,20 +309,27 @@ bool msgproc_injector_req(ECS_Client* ccli, ECS__InjectorReq* msg)
             }
             TRACE("status : %s", data);
             send_status_injector_ntf(MSG_TYPE_SENSOR, 6, action, data);
-            ret = true;
-            goto injector_req_success;
+            msgproc_injector_ans(ccli, cmd, true);
+            return true;
         } else {
             if (msg->data.data && datalen > 0) {
                 set_injector_data((char*) msg->data.data);
             }
         }
+    } else if (!strncmp(cmd, MSG_TYPE_GUEST, 5)) {
+        qemu_mutex_lock(&mutex_guest_connection);
+        value = guest_connection;
+        qemu_mutex_unlock(&mutex_guest_connection);
+        send_status_injector_ntf(MSG_TYPE_GUEST, 5, value, NULL);
+        return true;
     }
 
 injector_send:
     sndlen = datalen + 14;
     sndbuf = (char*) g_malloc(sndlen + 1);
     if (!sndbuf) {
-        goto injector_req_fail;
+        msgproc_injector_ans(ccli, cmd, ret);
+        return false;
     }
 
     memset(sndbuf, 0, sndlen + 1);
@@ -338,16 +355,13 @@ injector_send:
 
     g_free(sndbuf);
 
-    if (!ret)
-        goto injector_req_fail;
-
-injector_req_success:
     msgproc_injector_ans(ccli, cmd, ret);
-    return true;
 
-injector_req_fail:
-    msgproc_injector_ans(ccli, cmd, ret);
-    return false;
+    if (!ret) {
+        return false;
+    }
+
+    return true;
 }
 
 void ecs_suspend_lock_state(int state)
@@ -689,16 +703,21 @@ bool ntf_to_injector(const char* data, const int len) {
     return true;
 }
 
-static bool injector_req_handle(const char* cat)
+static bool injector_req_handle(const char* cat, type_action action)
 {
     /*SD CARD msg process*/
     if (!strncmp(cat, MSG_TYPE_SDCARD, strlen(MSG_TYPE_SDCARD))) {
        return false;
 
-    }else
-    if (!strncmp(cat, "suspend", 7)) {
+    } else if (!strncmp(cat, "suspend", 7)) {
         ecs_suspend_lock_state(ecs_get_suspend_state());
         return true;
+    } else if (!strncmp(cat, MSG_TYPE_GUEST, 5)) {
+        INFO("emuld connection is %d\n", action);
+        qemu_mutex_lock(&mutex_guest_connection);
+        guest_connection = action;
+        qemu_mutex_unlock(&mutex_guest_connection);
+        return false;
     }
 
     return false;
@@ -719,7 +738,7 @@ bool send_injector_ntf(const char* data, const int len)
     read_val_char(data + catsize + 2, &group);
     read_val_char(data + catsize + 2 + 1, &action);
 
-    if (injector_req_handle(cat)) {
+    if (injector_req_handle(cat, action)) {
         return true;
     }