ecs: refactoring injector_req message handling
authorJinhyung Choi <jinhyung2.choi@samsung.com>
Fri, 19 Sep 2014 08:48:23 +0000 (17:48 +0900)
committerJinhyung Choi <jinhyung2.choi@samsung.com>
Mon, 6 Oct 2014 10:22:33 +0000 (19:22 +0900)
Change-Id: Id2704b4791123b1b42b165ca77119e6986d222f3
Signed-off-by: Jinhyung Choi <jinhyung2.choi@samsung.com>
tizen/src/ecs/ecs_msg.c

index c05cfca0013c0c4f5bbf2f0616c860f693378a0a..8cf336244f69c2901ae88c11e69e21af8e7b07a5 100644 (file)
@@ -306,108 +306,36 @@ static void msgproc_device_ans(ECS_Client* ccli, const char* category, bool succ
         g_free(ans.category);
 }
 
-bool msgproc_injector_req(ECS_Client* ccli, ECS__InjectorReq* msg)
+#define STATUS_RSSI_LEVEL 104
+
+static bool injector_send(ECS_Client* ccli, ECS__InjectorReq* msg, char* cmd)
 {
-    char cmd[10];
-    char data[MAX_INJECTOR_REQ_DATA];
-    bool ret = false;
-    int sndlen = 0;
-    int value = 0;
+    int sndlen = 15; // HEADER(CMD + LENGTH + GROUP + ACTION) + 1
+    const char* msg_data;
     char* sndbuf;
-    memset(cmd, 0, 10);
-    strncpy(cmd, msg->category, sizeof(cmd) -1);
-    type_length length = (type_length) msg->length;
-    type_group group = (type_group) (msg->group & 0xff);
-    type_action action = (type_action) (msg->action & 0xff);
-
-
-    int datalen = 0;
-    if (msg->has_data)
-    {
-        datalen = msg->data.len;
-    }
-    //TRACE(">> count= %d", ++ijcount);
+    bool ret = false;
+    type_group group;
 
-    TRACE(">> injector_req: header = cmd = %s, length = %d, action=%d, group=%d, data= %s\n", cmd, length,
-            action, group, msg->data.data);
+    group = (type_group) (msg->group & 0xff);
 
-    /*SD CARD msg process*/
-    if (!strcmp(cmd, MSG_TYPE_SDCARD)) {
-        if (msg->has_data) {
-            TRACE("msg(%zu) : %s\n", msg->data.len, msg->data.data);
-            if (handle_sdcard((char*) msg->data.data, msg->data.len) > 0) {
-                return false;
-            }
-        } else {
-            ERR("has no msg\n");
-        }
+    if (msg->has_data && msg->data.data && msg->data.len > 0)
+        sndlen += msg->data.len;
 
-    } else if (!strcmp(cmd, MSG_TYPE_SENSOR)) {
-        if (group == MSG_GROUP_STATUS) {
-            memset(data, 0, MAX_INJECTOR_REQ_DATA);
-            if (action == MSG_ACT_BATTERY_LEVEL) {
-                sprintf(data, "%d", get_power_capacity());
-            } else if (action == MSG_ACT_BATTERY_CHARGER) {
-                sprintf(data, "%d", get_jack_charger());
-            } else if (action == MSG_ACT_USB) {
-                sprintf(data, "%d", get_jack_usb());
-            } else if (action == MSG_ACT_EARJACK) {
-                sprintf(data, "%d", get_jack_earjack());
-            } else if (action == MSG_ACT_LOCATION) {
-                qemu_mutex_lock(&mutex_location_data);
-                sprintf(data, "%s", location_data);
-                qemu_mutex_unlock(&mutex_location_data);
-            } else {
-                goto injector_send;
-            }
-            TRACE("status : %s\n", data);
-            send_status_injector_ntf(MSG_TYPE_SENSOR, 6, action, data);
-            //msgproc_injector_ans(ccli, cmd, true);
-            return true;
-        } else {
-            if (msg->data.data && datalen > 0) {
-                set_injector_data((char*) msg->data.data);
-            }
-        }
-    } else if (!strcmp(cmd, MSG_TYPE_GUEST)) {
-        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;
-    } else if (!strcmp(cmd, MSG_TYPE_LOCATION)) {
-        if (msg->data.data && datalen > 0) {
-            qemu_mutex_lock(&mutex_location_data);
-            snprintf(location_data, msg->data.len + 1, "%s", (char*)msg->data.data);
-            qemu_mutex_unlock(&mutex_location_data);
-        }
-    }
-
-injector_send:
-    sndlen = datalen + 14;
-    sndbuf = (char*) g_malloc(sndlen + 1);
+    sndbuf = (char*) g_malloc0(sndlen);
     if (!sndbuf) {
-        msgproc_injector_ans(ccli, cmd, ret);
+        msgproc_injector_ans(ccli, cmd, false);
         return false;
     }
 
-    memset(sndbuf, 0, sndlen + 1);
-
-    // set data
     memcpy(sndbuf, cmd, 10);
-    memcpy(sndbuf + 10, &length, 2);
-    memcpy(sndbuf + 12, &group, 1);
-    memcpy(sndbuf + 13, &action, 1);
-
-
-    if (msg->has_data)
-    {
-        if (msg->data.data && msg->data.len > 0)
-        {
-            const char* data = (const char*)msg->data.data;
-            memcpy(sndbuf + 14, data, datalen);
-            TRACE(">> print len = %zd, data\" %s\"\n", strlen(data), data);
-        }
+    memcpy(sndbuf + 10, &msg->length, 2);
+    memcpy(sndbuf + 12, &msg->group, 1);
+    memcpy(sndbuf + 13, &msg->action, 1);
+
+    if (msg->has_data && msg->data.data && msg->data.len > 0) {
+        msg_data = (const char*)msg->data.data;
+        memcpy(sndbuf + 14, msg_data, msg->data.len);
+        TRACE(">> print len = %zd, data\" %s\"\n", msg->data.len, msg_data);
     }
 
     if(strcmp(cmd, "telephony") == 0) {
@@ -418,7 +346,6 @@ injector_send:
         ret = send_to_evdi(route_ij, sndbuf, sndlen);
     }
 
-
     g_free(sndbuf);
 
     if (group != MSG_GROUP_STATUS) {
@@ -432,6 +359,107 @@ injector_send:
     return true;
 }
 
+static bool injector_req_sdcard(ECS_Client* ccli, ECS__InjectorReq* msg, char *cmd)
+{
+    if (msg->has_data) {
+        TRACE("msg(%zu) : %s\n", msg->data.len, msg->data.data);
+        if (handle_sdcard((char*) msg->data.data, msg->data.len) > 0) {
+            return false;
+        }
+    } else {
+        ERR("has no msg\n");
+    }
+
+    return injector_send(ccli, msg, cmd);
+}
+
+static bool injector_req_sensor(ECS_Client* ccli, ECS__InjectorReq* msg, char *cmd)
+{
+    char data[MAX_INJECTOR_REQ_DATA];
+    type_group group;
+    type_action action;
+
+    memset(data, 0, MAX_INJECTOR_REQ_DATA);
+    group = (type_group) (msg->group & 0xff);
+    action = (type_action) (msg->action & 0xff);
+
+    if (group == MSG_GROUP_STATUS) {
+        switch (action) {
+        case MSG_ACT_BATTERY_LEVEL:
+            sprintf(data, "%d", get_power_capacity());
+            break;
+        case MSG_ACT_BATTERY_CHARGER:
+            sprintf(data, "%d", get_jack_charger());
+            break;
+        case MSG_ACT_USB:
+            sprintf(data, "%d", get_jack_usb());
+            break;
+        case MSG_ACT_EARJACK:
+            sprintf(data, "%d", get_jack_earjack());
+            break;
+        case MSG_ACT_LOCATION:
+            qemu_mutex_lock(&mutex_location_data);
+            sprintf(data, "%s", location_data);
+            qemu_mutex_unlock(&mutex_location_data);
+            break;
+        default:
+            return injector_send(ccli, msg, cmd);
+        }
+        TRACE("status : %s\n", data);
+        send_status_injector_ntf(MSG_TYPE_SENSOR, 6, action, data);
+        return true;
+    } else if (msg->data.data && msg->data.len > 0) {
+        set_injector_data((char*) msg->data.data);
+        return injector_send(ccli, msg, cmd);
+    }
+
+    return false;
+}
+
+static bool injector_req_guest(void)
+{
+    int value = 0;
+    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;
+}
+
+static bool injector_req_location(ECS_Client* ccli, ECS__InjectorReq* msg, char *cmd)
+{
+    if (msg->data.data != NULL && msg->data.len > 0) {
+        qemu_mutex_lock(&mutex_location_data);
+        snprintf(location_data, msg->data.len + 1, "%s", (char*)msg->data.data);
+        qemu_mutex_unlock(&mutex_location_data);
+        return injector_send(ccli, msg, cmd);
+    }
+
+    return false;
+}
+
+bool msgproc_injector_req(ECS_Client* ccli, ECS__InjectorReq* msg)
+{
+    char cmd[11];
+    bool ret = false;
+
+    strncpy(cmd, msg->category, sizeof(cmd) - 1);
+
+    if (!strcmp(cmd, MSG_TYPE_SDCARD)) {
+        ret = injector_req_sdcard(ccli, msg, cmd);
+    } else if (!strcmp(cmd, MSG_TYPE_SENSOR)) {
+        ret = injector_req_sensor(ccli, msg, cmd);
+    } else if (!strcmp(cmd, MSG_TYPE_GUEST)) {
+        ret = injector_req_guest();
+    } else if (!strcmp(cmd, MSG_TYPE_LOCATION)) {
+        ret = injector_req_location(ccli, msg, cmd);
+    } else {
+        ret = injector_send(ccli, msg, cmd);
+    }
+
+    return ret;
+}
+
 void ecs_suspend_lock_state(int state)
 {
     int catlen;