sdcard: prepare for multiple image
authorMunkyu Im <munkyu.im@samsung.com>
Mon, 22 Jun 2015 06:39:30 +0000 (15:39 +0900)
committerSangho Park <sangho.p@samsung.com>
Fri, 26 Jun 2015 05:11:44 +0000 (14:11 +0900)
sdcard list gets from qmp_query_block()
So, emuld dependency has been removed.

Change-Id: Ibaad255b506e10daa3a0364ae558e781cf9c33ad
Signed-off-by: Munkyu Im <munkyu.im@samsung.com>
tizen/src/ecs/Makefile.objs
tizen/src/ecs/ecs_msg_device.c
tizen/src/ecs/ecs_msg_injector.c
tizen/src/ecs/ecs_sdcard.c [new file with mode: 0644]
tizen/src/ecs/ecs_sdcard.h [new file with mode: 0644]
tizen/src/util/maru_device_hotplug.c
tizen/src/util/osutil-win32.c
tizen/src/util/osutil.c

index bc0a754..77cc81b 100644 (file)
@@ -4,3 +4,4 @@ obj-y += ecs_mon.o ecs-json-streamer.o
 obj-y += ecs_eventcast.o
 obj-y += ecs_sensor.o
 obj-y += ecs_nfc.o
+obj-y += ecs_sdcard.o
index 1311c82..1fda075 100644 (file)
@@ -45,6 +45,7 @@
 #include "emul_state.h"
 #include "ecs.h"
 #include "debug_ch.h"
+#include "ecs_sdcard.h"
 
 MULTI_DEBUG_CHANNEL(qemu, ecs);
 
@@ -415,6 +416,8 @@ bool msgproc_device_req(ECS_Client* ccli, ECS__DeviceReq* msg)
         msgproc_device_ans(ccli, cmd, true, vmname);
     } else if (!strcmp(cmd, "nfc")) {
         msgproc_device_req_nfc(ccli, msg);
+    } else if (!strcmp(cmd, "sdcard")) {
+        handle_sdcard((char*)msg->data.data, msg->data.len);
     } else {
         LOG_SEVERE("unknown cmd [%s]\n", cmd);
     }
index 2045443..62df95e 100644 (file)
@@ -44,7 +44,6 @@
 #include "ecs.h"
 #include "debug_ch.h"
 #include "util/osutil.h"
-
 #include "skin/maruskin_client.h"
 
 MULTI_DEBUG_CHANNEL(qemu, ecs);
@@ -59,44 +58,6 @@ static QemuThread hds_thread_id;
 
 extern QemuMutex mutex_location_data;
 static char location_data[MAX_INJECTOR_REQ_DATA];
-static void send_gen_injector_ntf(const char* cmd, int cmdlen, int grp, int act, char* on)
-{
-    int msglen = 0, datalen = 0;
-    type_length length  = 0;
-    type_group group = grp;
-    type_action action = act;
-
-    if (cmd == NULL || cmdlen > 10)
-        return;
-
-    if (on == NULL) {
-        msglen = 14;
-    } else {
-        datalen = strlen(on);
-        length  = (unsigned short)datalen;
-
-        msglen = datalen + 15;
-    }
-
-    char* status_msg = (char*) malloc(msglen);
-    if(!status_msg)
-        return;
-
-    memset(status_msg, 0, msglen);
-
-    memcpy(status_msg, cmd, cmdlen);
-    memcpy(status_msg + 10, &length, sizeof(unsigned short));
-    memcpy(status_msg + 12, &group, sizeof(unsigned char));
-    memcpy(status_msg + 13, &action, sizeof(unsigned char));
-
-    if (on != NULL) {
-        memcpy(status_msg + 14, on, datalen);
-    }
-
-    send_injector_ntf(status_msg, msglen);
-
-    free(status_msg);
-}
 
 static void msgproc_injector_ans(ECS_Client* ccli, const char* category, bool succeed)
 {
@@ -174,263 +135,6 @@ static bool injector_send(ECS_Client* ccli, ECS__InjectorReq* msg, char* cmd)
     return true;
 }
 
-static char* get_emulator_sdcard_path(void)
-{
-    char *emulator_sdcard_path = NULL;
-    char *tizen_sdk_data = NULL;
-
-#ifndef CONFIG_WIN32
-    char emulator_sdcard[] = "/emulator/sdcard/";
-#else
-    char emulator_sdcard[] = "\\emulator\\sdcard\\";
-#endif
-
-    LOG_TRACE("emulator_sdcard: %s, %zu\n", emulator_sdcard, sizeof(emulator_sdcard));
-
-    tizen_sdk_data = get_tizen_sdk_data_path();
-    if (!tizen_sdk_data) {
-        LOG_SEVERE("failed to get tizen-sdk-data path.\n");
-        return NULL;
-    }
-
-    emulator_sdcard_path =
-        g_malloc(strlen(tizen_sdk_data) + sizeof(emulator_sdcard) + 1);
-    if (!emulator_sdcard_path) {
-        LOG_SEVERE("failed to allocate memory.\n");
-        return NULL;
-    }
-
-    g_snprintf(emulator_sdcard_path, strlen(tizen_sdk_data) + sizeof(emulator_sdcard),
-             "%s%s", tizen_sdk_data, emulator_sdcard);
-
-    g_free(tizen_sdk_data);
-
-    LOG_TRACE("sdcard path: %s\n", emulator_sdcard_path);
-    return emulator_sdcard_path;
-}
-
-static char *get_old_tizen_sdk_data_path(void)
-{
-    char *tizen_sdk_data_path = NULL;
-
-    LOG_INFO("try to search tizen-sdk-data path in another way.\n");
-
-#ifndef CONFIG_WIN32
-    char tizen_sdk_data[] = "/tizen-sdk-data";
-    int tizen_sdk_data_len = 0;
-    char *home_dir;
-
-    home_dir = (char *)g_getenv("HOME");
-    if (!home_dir) {
-        home_dir = (char *)g_get_home_dir();
-    }
-
-    tizen_sdk_data_len = strlen(home_dir) + sizeof(tizen_sdk_data) + 1;
-    tizen_sdk_data_path = g_malloc(tizen_sdk_data_len);
-    if (!tizen_sdk_data_path) {
-        LOG_SEVERE("failed to allocate memory.\n");
-        return NULL;
-    }
-    g_strlcpy(tizen_sdk_data_path, home_dir, tizen_sdk_data_len);
-    g_strlcat(tizen_sdk_data_path, tizen_sdk_data, tizen_sdk_data_len);
-
-#else
-    char tizen_sdk_data[] = "\\tizen-sdk-data\\";
-    gint tizen_sdk_data_len = 0;
-    HKEY hKey;
-    char strLocalAppDataPath[1024] = { 0 };
-    DWORD dwBufLen = 1024;
-
-    RegOpenKeyEx(HKEY_CURRENT_USER,
-        "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
-        0, KEY_QUERY_VALUE, &hKey);
-
-    RegQueryValueEx(hKey, "Local AppData", NULL,
-                    NULL, (LPBYTE)strLocalAppDataPath, &dwBufLen);
-    RegCloseKey(hKey);
-
-    tizen_sdk_data_len = strlen(strLocalAppDataPath) + sizeof(tizen_sdk_data) + 1;
-    tizen_sdk_data_path = g_malloc(tizen_sdk_data_len);
-    if (!tizen_sdk_data_path) {
-        LOG_SEVERE("failed to allocate memory.\n");
-        return NULL;
-    }
-
-    g_strlcpy(tizen_sdk_data_path, strLocalAppDataPath, tizen_sdk_data_len);
-    g_strlcat(tizen_sdk_data_path, tizen_sdk_data, tizen_sdk_data_len);
-#endif
-
-    LOG_INFO("tizen-sdk-data path: %s\n", tizen_sdk_data_path);
-    return tizen_sdk_data_path;
-}
-
-/*
- *  get tizen-sdk-data path from sdk.info.
- */
-char *get_tizen_sdk_data_path(void)
-{
-    char const *emul_bin_path = NULL;
-    char *sdk_info_file_path = NULL;
-    char *tizen_sdk_data_path = NULL;
-#ifndef CONFIG_WIN32
-    const char *sdk_info = "../../../../../sdk.info";
-#else
-    const char *sdk_info = "..\\..\\..\\..\\..\\sdk.info";
-#endif
-    const char sdk_data_var[] = "TIZEN_SDK_DATA_PATH";
-
-    FILE *sdk_info_fp = NULL;
-    int sdk_info_path_len = 0;
-
-    LOG_TRACE("%s\n", __func__);
-
-    emul_bin_path = get_bin_path();
-    if (!emul_bin_path) {
-        LOG_SEVERE("failed to get emulator path.\n");
-        return NULL;
-    }
-
-    sdk_info_path_len = strlen(emul_bin_path) + strlen(sdk_info) + 1;
-    sdk_info_file_path = g_malloc(sdk_info_path_len);
-    if (!sdk_info_file_path) {
-        LOG_SEVERE("failed to allocate sdk-data buffer.\n");
-        return NULL;
-    }
-
-    g_snprintf(sdk_info_file_path, sdk_info_path_len, "%s%s",
-                emul_bin_path, sdk_info);
-    LOG_INFO("sdk.info path: %s\n", sdk_info_file_path);
-
-    sdk_info_fp = fopen(sdk_info_file_path, "r");
-    g_free(sdk_info_file_path);
-
-    if (sdk_info_fp) {
-        LOG_TRACE("Succeeded to open [sdk.info].\n");
-
-        char tmp[256] = { '\0', };
-        char *tmpline = NULL;
-        while (fgets(tmp, sizeof(tmp), sdk_info_fp) != NULL) {
-            if ((tmpline = g_strstr_len(tmp, sizeof(tmp), sdk_data_var))) {
-                tmpline += strlen(sdk_data_var) + 1; // 1 for '='
-                break;
-            }
-        }
-
-        if (tmpline) {
-            if (tmpline[strlen(tmpline) - 1] == '\n') {
-                tmpline[strlen(tmpline) - 1] = '\0';
-            }
-            if (tmpline[strlen(tmpline) - 1] == '\r') {
-                tmpline[strlen(tmpline) - 1] = '\0';
-            }
-
-            tizen_sdk_data_path = g_malloc(strlen(tmpline) + 1);
-            g_strlcpy(tizen_sdk_data_path, tmpline, strlen(tmpline) + 1);
-
-            LOG_INFO("tizen-sdk-data path: %s\n", tizen_sdk_data_path);
-
-            fclose(sdk_info_fp);
-            return tizen_sdk_data_path;
-        }
-
-        fclose(sdk_info_fp);
-    }
-
-    // legacy mode
-    LOG_SEVERE("Failed to open [sdk.info].\n");
-
-    return get_old_tizen_sdk_data_path();
-}
-
-static char* get_sdcard_img_path(char* sdcard_img_name, size_t dataLen) {
-    char* sdcard_img_path = NULL;
-    char* sdcard_path = NULL;
-    if (sdcard_img_name == NULL || dataLen < 3) {
-        return NULL;
-    }
-    dataLen = dataLen - 3;
-    if (sdcard_img_name[dataLen] == '\n') {
-        sdcard_img_name[dataLen] = '\0';
-        LOG_TRACE("sdcard_img_name: %s\n", sdcard_img_name);
-    } else {
-        LOG_SEVERE("wrong sdcard message!\n");
-        return NULL;
-    }
-
-    sdcard_path = get_emulator_sdcard_path();
-    if (sdcard_path != NULL) {
-        sdcard_img_path = g_malloc(DEFAULTBUFLEN);
-        g_strlcpy(sdcard_img_path, sdcard_path, DEFAULTBUFLEN);
-        g_strlcat(sdcard_img_path, sdcard_img_name, DEFAULTBUFLEN);
-        LOG_TRACE("sdcard img path: [%s] length: %d\n", sdcard_img_path, strlen(sdcard_img_path));
-        g_free(sdcard_path);
-        return sdcard_img_path;
-    }
-    return NULL;
-}
-
-static int handle_sdcard(char* dataBuf, size_t dataLen)
-{
-    int err_no = 0;
-    char ret = 0;
-    if (dataBuf != NULL){
-        ret = dataBuf[0];
-
-        if (ret == '0' ) {
-            /* umount sdcard */
-            LOG_INFO("datalen: %d\n", dataLen);
-            char* sdcard_img_path = get_sdcard_img_path(dataBuf + 2, dataLen);
-            err_no = remove_sdcard_lock_os(sdcard_img_path);
-            if (errno == 0 && is_sdcard_attached()) {
-                do_hotplug(DETACH_SDCARD, NULL, 0);
-            } else {
-                LOG_SEVERE("failed to umount: %s\n", sdcard_img_path);
-                send_gen_injector_ntf(MSG_TYPE_SDCARD, 6, 11, err_no, NULL);
-                return err_no;
-            }
-            g_free(sdcard_img_path);
-        } else if (ret == '1') {
-            /* mount sdcard */
-            LOG_INFO("datalen: %d\n", dataLen);
-            char* sdcard_img_path = get_sdcard_img_path(dataBuf + 2, dataLen);
-            if ( !is_sdcard_attached() && make_sdcard_lock_os(sdcard_img_path)) {
-                do_hotplug(ATTACH_SDCARD, sdcard_img_path, strlen(sdcard_img_path) + 1);
-                send_gen_injector_ntf(MSG_TYPE_SDCARD, 6, 11, 0, NULL);
-            } else {
-                send_gen_injector_ntf(MSG_TYPE_SDCARD, 6, 11, 5, NULL);
-                return ERR_LCK;
-            }
-            g_free(sdcard_img_path);
-
-        } else if (ret == '2') {
-            LOG_TRACE("sdcard status 2 bypass\n" );
-        } else {
-            LOG_SEVERE("!!! unknown command : %c\n", ret);
-            return ret;
-        }
-    } else {
-        LOG_SEVERE("!!! unknown data : %c\n", ret);
-        return ret;
-    }
-    return ERR_SUCCESS;
-}
-
-static bool injector_req_sdcard(ECS_Client* ccli, ECS__InjectorReq* msg, char *cmd)
-{
-    if (msg->has_data) {
-        LOG_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 {
-        LOG_SEVERE("has no msg\n");
-    }
-
-    injector_send(ccli, msg, cmd);
-
-    return true;
-}
-
 static void send_status_injector_ntf(const char* cmd, int cmdlen, int act, char* on)
 {
     int msglen = 0, datalen = 0;
@@ -538,9 +242,7 @@ bool msgproc_injector_req(ECS_Client* ccli, ECS__InjectorReq* msg)
 
     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)) {
+    if (!strcmp(cmd, MSG_TYPE_SENSOR)) {
         ret = injector_req_sensor(ccli, msg, cmd);
     } else if (!strcmp(cmd, MSG_TYPE_GUEST)) {
         ret = injector_req_guest();
diff --git a/tizen/src/ecs/ecs_sdcard.c b/tizen/src/ecs/ecs_sdcard.c
new file mode 100644 (file)
index 0000000..25487d3
--- /dev/null
@@ -0,0 +1,359 @@
+/*
+ * Emulator Control Server - SD Card Handler
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * MunKyu Im       <munkyu.im@samsung.com>
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ * Sangho Park     <sangho.p@samsung.com>
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#include "ecs_sdcard.h"
+#include "ecs.h"
+#include "util/osutil.h"
+#include "util/maru_device_hotplug.h"
+#include "debug_ch.h"
+#include "emul_state.h"
+
+MULTI_DEBUG_CHANNEL(qemu, ecs);
+
+static char *get_old_tizen_sdk_data_path(void)
+{
+    char *tizen_sdk_data_path = NULL;
+
+    LOG_INFO("try to search tizen-sdk-data path in another way.\n");
+
+#ifndef CONFIG_WIN32
+    char tizen_sdk_data[] = "/tizen-sdk-data";
+    int tizen_sdk_data_len = 0;
+    char *home_dir;
+
+    home_dir = (char *)g_getenv("HOME");
+    if (!home_dir) {
+        home_dir = (char *)g_get_home_dir();
+    }
+
+    tizen_sdk_data_len = strlen(home_dir) + sizeof(tizen_sdk_data) + 1;
+    tizen_sdk_data_path = g_malloc(tizen_sdk_data_len);
+    if (!tizen_sdk_data_path) {
+        LOG_SEVERE("failed to allocate memory.\n");
+        return NULL;
+    }
+    g_strlcpy(tizen_sdk_data_path, home_dir, tizen_sdk_data_len);
+    g_strlcat(tizen_sdk_data_path, tizen_sdk_data, tizen_sdk_data_len);
+
+#else
+    char tizen_sdk_data[] = "\\tizen-sdk-data\\";
+    gint tizen_sdk_data_len = 0;
+    HKEY hKey;
+    char strLocalAppDataPath[1024] = { 0 };
+    DWORD dwBufLen = 1024;
+
+    RegOpenKeyEx(HKEY_CURRENT_USER,
+            "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
+            0, KEY_QUERY_VALUE, &hKey);
+
+    RegQueryValueEx(hKey, "Local AppData", NULL,
+            NULL, (LPBYTE)strLocalAppDataPath, &dwBufLen);
+    RegCloseKey(hKey);
+
+    tizen_sdk_data_len = strlen(strLocalAppDataPath) + sizeof(tizen_sdk_data) + 1;
+    tizen_sdk_data_path = g_malloc(tizen_sdk_data_len);
+    if (!tizen_sdk_data_path) {
+        LOG_SEVERE("failed to allocate memory.\n");
+        return NULL;
+    }
+
+    g_strlcpy(tizen_sdk_data_path, strLocalAppDataPath, tizen_sdk_data_len);
+    g_strlcat(tizen_sdk_data_path, tizen_sdk_data, tizen_sdk_data_len);
+#endif
+
+    LOG_INFO("tizen-sdk-data path: %s\n", tizen_sdk_data_path);
+    return tizen_sdk_data_path;
+}
+
+/*
+ *  get tizen-sdk-data path from sdk.info.
+ */
+char *get_tizen_sdk_data_path(void)
+{
+    char const *emul_bin_path = NULL;
+    char *sdk_info_file_path = NULL;
+    char *tizen_sdk_data_path = NULL;
+#ifndef CONFIG_WIN32
+    const char *sdk_info = "../../../../../sdk.info";
+#else
+    const char *sdk_info = "..\\..\\..\\..\\..\\sdk.info";
+#endif
+    const char sdk_data_var[] = "TIZEN_SDK_DATA_PATH";
+
+    FILE *sdk_info_fp = NULL;
+    int sdk_info_path_len = 0;
+
+    LOG_TRACE("%s\n", __func__);
+
+    emul_bin_path = get_bin_path();
+    if (!emul_bin_path) {
+        LOG_SEVERE("failed to get emulator path.\n");
+        return NULL;
+    }
+
+    sdk_info_path_len = strlen(emul_bin_path) + strlen(sdk_info) + 1;
+    sdk_info_file_path = g_malloc(sdk_info_path_len);
+    if (!sdk_info_file_path) {
+        LOG_SEVERE("failed to allocate sdk-data buffer.\n");
+        return NULL;
+    }
+
+    g_snprintf(sdk_info_file_path, sdk_info_path_len, "%s%s",
+            emul_bin_path, sdk_info);
+    LOG_INFO("sdk.info path: %s\n", sdk_info_file_path);
+
+    sdk_info_fp = fopen(sdk_info_file_path, "r");
+    g_free(sdk_info_file_path);
+
+    if (sdk_info_fp) {
+        LOG_TRACE("Succeeded to open [sdk.info].\n");
+
+        char tmp[256] = { '\0', };
+        char *tmpline = NULL;
+        while (fgets(tmp, sizeof(tmp), sdk_info_fp) != NULL) {
+            if ((tmpline = g_strstr_len(tmp, sizeof(tmp), sdk_data_var))) {
+                tmpline += strlen(sdk_data_var) + 1; // 1 for '='
+                break;
+            }
+        }
+
+        if (tmpline) {
+            if (tmpline[strlen(tmpline) - 1] == '\n') {
+                tmpline[strlen(tmpline) - 1] = '\0';
+            }
+            if (tmpline[strlen(tmpline) - 1] == '\r') {
+                tmpline[strlen(tmpline) - 1] = '\0';
+            }
+
+            tizen_sdk_data_path = g_malloc(strlen(tmpline) + 1);
+            g_strlcpy(tizen_sdk_data_path, tmpline, strlen(tmpline) + 1);
+
+            LOG_INFO("tizen-sdk-data path: %s\n", tizen_sdk_data_path);
+
+            fclose(sdk_info_fp);
+            return tizen_sdk_data_path;
+        }
+
+        fclose(sdk_info_fp);
+    }
+
+    // legacy mode
+    LOG_SEVERE("Failed to open [sdk.info].\n");
+
+    return get_old_tizen_sdk_data_path();
+}
+
+static char* get_emulator_sdcard_path(void)
+{
+    char *emulator_sdcard_path = NULL;
+    char *tizen_sdk_data = NULL;
+
+#ifndef CONFIG_WIN32
+    char emulator_sdcard[] = "/emulator/sdcard/";
+#else
+    char emulator_sdcard[] = "\\emulator\\sdcard\\";
+#endif
+
+    LOG_TRACE("emulator_sdcard: %s, %zu\n", emulator_sdcard, sizeof(emulator_sdcard));
+
+    tizen_sdk_data = get_tizen_sdk_data_path();
+    if (!tizen_sdk_data) {
+        LOG_SEVERE("failed to get tizen-sdk-data path.\n");
+        return NULL;
+    }
+
+    emulator_sdcard_path =
+        g_malloc(strlen(tizen_sdk_data) + sizeof(emulator_sdcard) + 1);
+    if (!emulator_sdcard_path) {
+        LOG_SEVERE("failed to allocate memory.\n");
+        return NULL;
+    }
+
+    g_snprintf(emulator_sdcard_path, strlen(tizen_sdk_data) + sizeof(emulator_sdcard),
+             "%s%s", tizen_sdk_data, emulator_sdcard);
+
+    g_free(tizen_sdk_data);
+    LOG_TRACE("sdcard path: %s\n", emulator_sdcard_path);
+    return emulator_sdcard_path;
+}
+
+static char* get_sdcard_img_path(char* sdcard_img_name, size_t dataLen) {
+    char* sdcard_img_path = NULL;
+    char* sdcard_path = NULL;
+    if (sdcard_img_name == NULL || dataLen < 3) {
+        return NULL;
+    }
+    dataLen = dataLen - 3;
+    if (sdcard_img_name[dataLen] == '\n') {
+        sdcard_img_name[dataLen] = '\0';
+        LOG_TRACE("sdcard_img_name: %s\n", sdcard_img_name);
+    } else {
+        LOG_SEVERE("wrong sdcard message!\n");
+        return NULL;
+    }
+
+    sdcard_path = get_emulator_sdcard_path();
+    if (sdcard_path != NULL) {
+        sdcard_img_path = g_malloc(MAXBUFLEN);
+        g_strlcpy(sdcard_img_path, sdcard_path, MAXBUFLEN);
+        g_strlcat(sdcard_img_path, sdcard_img_name, MAXBUFLEN);
+        LOG_TRACE("sdcard img path: [%s] length: %d\n", sdcard_img_path, strlen(sdcard_img_path));
+        g_free(sdcard_path);
+        return sdcard_img_path;
+    }
+    return NULL;
+}
+
+static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
+{
+    static const char suffixes[NB_SUFFIXES] = "KMGT";
+    int64_t base;
+    int i;
+
+    if (size <= 999) {
+        snprintf(buf, buf_size, "%" PRId64, size);
+    } else {
+        base = 1024;
+        for (i = 0; i < NB_SUFFIXES; i++) {
+            if (size < (10 * base)) {
+                snprintf(buf, buf_size, "%0.1f%c",
+                         (double)size / base,
+                         suffixes[i]);
+                break;
+            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
+                snprintf(buf, buf_size, "%" PRId64 "%c",
+                         ((size + (base >> 1)) / base),
+                         suffixes[i]);
+                break;
+            }
+            base = base * 1024;
+        }
+    }
+    return buf;
+}
+
+static void send_sdcard_status(void)
+{
+    BlockInfoList *block_list, *info;
+    ImageInfo *image_info;
+    char data[READ_BUF_LEN];
+    memset(data, 0, READ_BUF_LEN);
+    int length = 0;
+    char size_buf[128], dsize_buf[128];
+    memset(dsize_buf, 0, 128);
+    block_list = qmp_query_block(NULL);
+    for (info = block_list; info; info = info->next) {
+        if (g_str_has_prefix(info->value->device, SDCARD_ID_PREFIX) == false) {
+            continue;
+        }
+        if (info->value->removable) {
+            continue;
+        }
+        image_info = info->value->inserted->image;
+        if (!image_info->has_actual_size) {
+            snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
+        } else {
+            get_human_readable_size(dsize_buf, sizeof(dsize_buf),
+                    image_info->actual_size);
+        }
+        get_human_readable_size(size_buf, sizeof(size_buf), image_info->virtual_size);
+        length += sprintf(data + length,
+                "\nimage: %s\n"
+                "file format: %s\n"
+                "virtual size: %s (%" PRId64 " bytes)\n"
+                "disk size: %s\n",
+                image_info->filename, image_info->format, size_buf,
+                image_info->virtual_size,
+                dsize_buf);
+
+        if (image_info->has_encrypted && image_info->encrypted) {
+            length += sprintf(data + length, "encrypted: yes\n");
+        }
+
+        if (image_info->has_cluster_size) {
+            length += sprintf(data + length, "cluster_size: %" PRId64 "", image_info->cluster_size);
+        }
+    }
+
+    qapi_free_BlockInfoList(block_list);
+    LOG_INFO("sdcard data: %s\n", data);
+    if (data[0] == 0) {
+        make_send_device_ntf((char*)MSG_TYPE_SDCARD, GROUP_SDCARD, ACT_SDCARD_DETACH_STATUS, NULL);
+    } else {
+        make_send_device_ntf((char*)MSG_TYPE_SDCARD, GROUP_SDCARD, ACT_SDCARD_ATTACH_STATUS, data);
+    }
+}
+
+void handle_sdcard(char* dataBuf, size_t dataLen)
+{
+    int err_no = 0;
+    char ret = 0;
+    if (dataBuf != NULL && strlen(dataBuf) > 0) {
+        ret = dataBuf[0];
+
+        if (ret == '0' ) {
+            /* detach sdcard */
+            char* sdcard_img_path = get_sdcard_img_path(dataBuf + 2, dataLen);
+            LOG_TRACE("sdcard_img_path: %s\n", sdcard_img_path);
+            err_no = remove_sdcard_lock_os(sdcard_img_path);
+            if (err_no == 0 && is_sdcard_attached()) {
+                do_hotplug(DETACH_SDCARD, sdcard_img_path, strlen(sdcard_img_path) + 1);
+                make_send_device_ntf((char*)MSG_TYPE_SDCARD, GROUP_SDCARD,
+                        ACT_SDCARD_DETACH, g_path_get_basename(sdcard_img_path));
+            } else {
+                LOG_SEVERE("failed to umount: %s, err_no: %d\n", sdcard_img_path, err_no);
+                make_send_device_ntf((char*)MSG_TYPE_SDCARD, GROUP_SDCARD,
+                        err_no, g_path_get_basename(sdcard_img_path));
+            }
+            g_free(sdcard_img_path);
+        } else if (ret == '1') {
+            /* attach sdcard */
+            char* sdcard_img_path = get_sdcard_img_path(dataBuf + 2, dataLen);
+            LOG_TRACE("sdcard_img_path: %s\n", sdcard_img_path);
+            if (!is_sdcard_attached() && make_sdcard_lock_os(sdcard_img_path)) {
+                do_hotplug(ATTACH_SDCARD, sdcard_img_path, strlen(sdcard_img_path) + 1);
+                make_send_device_ntf((char*)MSG_TYPE_SDCARD, GROUP_SDCARD,
+                        ACT_SDCARD_ATTACH, g_path_get_basename(sdcard_img_path));
+            } else {
+                make_send_device_ntf((char*)MSG_TYPE_SDCARD, GROUP_SDCARD,
+                        ACT_SDCARD_ATTACH_FAIL, g_path_get_basename(sdcard_img_path));
+            }
+            g_free(sdcard_img_path);
+
+        } else if (ret == '2') {
+            send_sdcard_status();
+        } else {
+            LOG_SEVERE("!!! unknown command : %c\n", ret);
+        }
+    } else {
+        LOG_SEVERE("!!! unknown data : %c\n", ret);
+    }
+}
+
+
diff --git a/tizen/src/ecs/ecs_sdcard.h b/tizen/src/ecs/ecs_sdcard.h
new file mode 100644 (file)
index 0000000..6d3ddfe
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Emulator Control Server - SD Card Handler
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * MunKyu Im       <munkyu.im@samsung.com>
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ * Sangho Park     <sangho.p@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+#ifndef __SDCARD_H__
+#define __SDCARD_H__
+
+#include "qemu-common.h"
+#include "monitor/qdev.h"
+#include "fsdev/qemu-fsdev.h"
+#include "qmp-commands.h"
+#include "sysemu/blockdev.h"
+
+#define SDCARD_DRIVER               "virtio-blk-pci"
+#define SDCARD_ID_PREFIX            "device_id_sdcard"
+#define NB_SUFFIXES 4
+#define GROUP_SDCARD                11
+
+#define ACT_SDCARD_DETACH           0
+#define ACT_SDCARD_ATTACH           1
+#define ACT_SDCARD_DETACH_STATUS    2
+#define ACT_SDCARD_ATTACH_STATUS    3
+#define ACT_SDCARD_DETACH_FAIL      4
+#define ACT_SDCARD_ATTACH_FAIL      5
+#define ACT_SDCARD_NO_ATTACH_FOUND  6
+#define MAXBUFLEN 1024
+void handle_sdcard(char* dataBuf, size_t dataLen);
+
+#endif //__SDCARD_H__
index 94dd67e..6241880 100644 (file)
 #include "qemu/main-loop.h"
 #include "qemu/config-file.h"
 #include "hw/qdev.h"
-#include "monitor/qdev.h"
-#include "fsdev/qemu-fsdev.h"
-#include "qmp-commands.h"
-#include "sysemu/blockdev.h"
 #include "qemu/event_notifier.h"
 
 #include "emulator.h"
 #include "maru_device_hotplug.h"
 #include "ecs/ecs.h"
 #include "hds.h"
+#include "ecs/ecs_sdcard.h"
 
 #define HOST_KEYBOARD_DRIVER        "virtio-keyboard-pci"
 #define HOST_KEYBOARD_DEFAULT_ID    "HOSTKBD0"
 
-#define SDCARD_DRIVE_DEFAULT_ID     "SDCARD_DRIVE0"
-#define SDCARD_DRIVER               "virtio-blk-pci"
-#define SDCARD_DEFAULT_ID           "SDCARD0"
-
 #define FS_MOUNT_TAG                "fileshare"
 
 #include "debug_ch.h"
@@ -109,52 +102,60 @@ static bool do_sdcard_attach(const char * const file)
     QDict *qdict = qdict_new();
     QDict *qdict_file = qdict_new();
     QDict *qdict_options = qdict_new();
+    gchar *sdcard_img = g_path_get_basename(file);
+    gchar *sdcard_device_id = g_strdup_printf("device_id_%s", sdcard_img);
+    gchar *sdcard_drive_id = g_strdup_printf("drive_id_%s", sdcard_img);
+
+    g_free(sdcard_img);
 
     qdict_put(qdict_file, "driver", qstring_from_str("file"));
     qdict_put(qdict_file, "filename", qstring_from_str(file));
     qdict_put(qdict_options, "file", qdict_file);
     qdict_put(qdict_options, "driver", qstring_from_str("qcow2"));
-    qdict_put(qdict_options, "id", qstring_from_str(SDCARD_DRIVE_DEFAULT_ID));
+    qdict_put(qdict_options, "id", qstring_from_str(sdcard_device_id));
     qdict_put(qdict, "options", qdict_options);
 
-    if (qmp_marshal_input_blockdev_add(default_mon, qdict, NULL)) {
-        QDECREF(qdict);
-    }
+    qmp_marshal_input_blockdev_add(default_mon, qdict, NULL);
 
     QDECREF(qdict);
 
     qdict = qdict_new();
     qdict_put(qdict, "driver", qstring_from_str(SDCARD_DRIVER));
-    qdict_put(qdict, "drive", qstring_from_str(SDCARD_DRIVE_DEFAULT_ID));
-    qdict_put(qdict, "id", qstring_from_str(SDCARD_DEFAULT_ID));
+    qdict_put(qdict, "drive", qstring_from_str(sdcard_device_id));
+    /* set sdcard id as sdcard image file name */
+    qdict_put(qdict, "id", qstring_from_str(sdcard_drive_id));
 
     if (do_device_add(default_mon, qdict, NULL)) {
         QDECREF(qdict);
-        // TODO error reporting
+        LOG_WARNING("failed to attach sdcard: %s", file);
+        g_free(sdcard_device_id);
+        g_free(sdcard_drive_id);
         return false;
     }
 
     QDECREF(qdict);
-
+    g_free(sdcard_device_id);
+    g_free(sdcard_drive_id);
     state->sdcard_attached = true;
-
     return true;
 }
 
-static bool do_sdcard_detach(void) {
+static bool do_sdcard_detach(const char * const file)
+{
     QDict *qdict = qdict_new();
-    qdict_put(qdict, "id", qstring_from_str(SDCARD_DEFAULT_ID));
+    gchar *sdcard_drive_id = g_strdup_printf("drive_id_%s", g_path_get_basename(file));
+    qdict_put(qdict, "id", qstring_from_str(sdcard_drive_id));
 
     if (qmp_marshal_input_device_del(cur_mon, qdict, NULL)) {
         QDECREF(qdict);
-        // TODO error reporting
+        g_free(sdcard_drive_id);
+        LOG_WARNING("failed to detach sdcard: %s", file);
         return false;
     }
 
     QDECREF(qdict);
-
     state->sdcard_attached = false;
-
+    g_free(sdcard_drive_id);
     return true;
 }
 
@@ -266,7 +267,7 @@ static void device_hotplug_handler(EventNotifier *e)
         do_sdcard_attach(state->opaque);
         break;
     case DETACH_SDCARD:
-        do_sdcard_detach();
+        do_sdcard_detach(state->opaque);
         break;
     case ATTACH_HDS:
         do_hds_attach(state->opaque);
@@ -310,6 +311,6 @@ bool is_host_keyboard_attached(void)
 
 bool is_sdcard_attached(void)
 {
+    //TODO: support checking multi sdcard attached
     return state->sdcard_attached;
 }
-
index 40ce4d9..cc7f327 100644 (file)
@@ -214,7 +214,7 @@ void print_system_info_os(void)
 
 bool make_sdcard_lock_os(char *sdcard)
 {
-    char *lock_file = g_strdup_printf("%s.lck", sdcard);
+    char *lock_file = g_strdup_printf("%s-%d.lck", sdcard, getpid());
 
     HANDLE hFile = CreateFile(lock_file, GENERIC_READ,
             FILE_SHARE_READ | FILE_SHARE_DELETE,
@@ -242,7 +242,7 @@ bool make_sdcard_lock_os(char *sdcard)
 
 int remove_sdcard_lock_os(char *sdcard)
 {
-    char *lock_file = g_strdup_printf("%s.lck", sdcard);
+    char *lock_file = g_strdup_printf("%s-%d.lck", sdcard, getpid());
     HANDLE hFile2;
     hFile2 = CreateFile(lock_file, GENERIC_READ,
             0,
index 4c2501c..03832df 100644 (file)
@@ -66,13 +66,14 @@ static int fd_unlock(int fd)
     lock.l_start = 0;
     lock.l_whence = SEEK_SET;
     lock.l_len = 0;
+    lock.l_pid = getpid();
 
     return fcntl(fd, F_SETLK, &lock);
 }
 
 bool make_sdcard_lock_posix(char *sdcard)
 {
-    char *lock_file = g_strdup_printf("%s.lck", sdcard);
+    char *lock_file = g_strdup_printf("%s-%d.lck", sdcard, getpid());
     int fd = open(lock_file, O_CREAT|O_RDWR, 0666);
     if (fd == -1)
     {
@@ -93,7 +94,7 @@ bool make_sdcard_lock_posix(char *sdcard)
 int remove_sdcard_lock_posix(char *sdcard)
 {
     errno = 0;
-    char *lock_file = g_strdup_printf("%s.lck", sdcard);
+    char *lock_file = g_strdup_printf("%s-%d.lck", sdcard, getpid());
     int fd = open(lock_file, O_RDWR, 0666);
     if (fd == -1)
     {