emulator: fix compile errors on Windows.
[sdk/emulator/qemu.git] / tizen / src / osutil-win32.c
index 02555ce..457279b 100644 (file)
@@ -1,9 +1,9 @@
-/* 
+/*
  * Emulator
  *
  * Copyright (C) 2012, 2013 Samsung Electronics Co., Ltd. All rights reserved.
  *
- * Contact: 
+ * Contact:
  * SeokYeon Hwang <syeon.hwang@samsung.com>
  * MunKyu Im <munkyu.im@samsung.com>
  * GiWoong Kim <giwoong.kim@samsung.com>
@@ -34,6 +34,7 @@
   @brief    Collection of utilities for win32
  */
 
+#include <png.h>
 #include "maru_common.h"
 #include "osutil.h"
 #include "emulator.h"
 #include "maru_err_table.h"
 #include "sdb.h"
 
+#ifndef CONFIG_WIN32
+#error
+#endif
+
 #include <windows.h>
 
 MULTI_DEBUG_CHANNEL (emulator, osutil);
 
+
+static qemu_timeval tv = { 0, 0 };
+static time_t ti;
+static char buf_time[64];
+static HANDLE g_hMapFile;
+static char *g_pBuf;
+
 extern char tizen_target_img_path[];
-extern int tizen_base_port;
+
+static const char *pactempfile = ".autoproxy";
 
 void check_vm_lock_os(void)
 {
@@ -54,6 +67,7 @@ void check_vm_lock_os(void)
     char *base_port = NULL;
     char *pBuf;
     HANDLE hMapFile;
+
     for (port = 26100; port < 26200; port += 10) {
         base_port = g_strdup_printf("%d", port);
         hMapFile = OpenFileMapping(FILE_MAP_READ, TRUE, base_port);
@@ -87,37 +101,48 @@ void check_vm_lock_os(void)
 
 void make_vm_lock_os(void)
 {
-    HANDLE hMapFile;
-    char *pBuf;
     char *port_in_use;
     char *shared_memory;
+    int base_port;
 
+    base_port = get_emul_vm_base_port();
     shared_memory = g_strdup_printf("%s", tizen_target_img_path);
-    port_in_use =  g_strdup_printf("%d", tizen_base_port);
-    hMapFile = CreateFileMapping(
+    port_in_use =  g_strdup_printf("%d", base_port);
+    g_hMapFile = CreateFileMapping(
                  INVALID_HANDLE_VALUE, /* use paging file */
                  NULL,                 /* default security */
                  PAGE_READWRITE,       /* read/write access */
                  0,                /* maximum object size (high-order DWORD) */
                  50,               /* maximum object size (low-order DWORD) */
                  port_in_use);         /* name of mapping object */
-    if (hMapFile == NULL) {
+    if (g_hMapFile == NULL) {
         ERR("Could not create file mapping object (%d).\n", GetLastError());
         return;
     }
-    pBuf = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 50);
 
-    if (pBuf == NULL) {
+    g_pBuf = MapViewOfFile(g_hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 50);
+    if (g_pBuf == NULL) {
         ERR("Could not map view of file (%d).\n", GetLastError());
-        CloseHandle(hMapFile);
+        CloseHandle(g_hMapFile);
         return;
     }
 
-    CopyMemory((PVOID)pBuf, shared_memory, strlen(shared_memory));
+    CopyMemory((PVOID)g_pBuf, shared_memory, strlen(shared_memory));
     free(port_in_use);
     free(shared_memory);
 }
 
+void remove_vm_lock_os(void)
+{
+    if (g_pBuf != NULL) {
+        UnmapViewOfFile(g_pBuf);
+    }
+    if (g_hMapFile != NULL) {
+        CloseHandle(g_hMapFile);
+    }
+}
+
+
 void set_bin_path_os(gchar * exec_argv)
 {
     gchar link_path[PATH_MAX] = { 0, };
@@ -133,10 +158,29 @@ void set_bin_path_os(gchar * exec_argv)
     g_strlcat(bin_path, "\\", PATH_MAX);
 }
 
+int get_number_of_processors(void)
+{
+    SYSTEM_INFO sysi;
+    int num_processors = 0;
+
+    GetSystemInfo(&sysi);
+    TRACE("Processor type: %d, Core number: %d\n",
+        sysi.dwProcessorType, sysi.dwNumberOfProcessors);
+
+    num_processors = sysi.dwNumberOfProcessors;
+    if (num_processors < 1) {
+        num_processors = 1;
+    }
+
+    return num_processors;
+}
+
 void print_system_info_os(void)
 {
     INFO("* Windows\n");
 
+    INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING);
+
     /* Retrieves information about the current os */
     OSVERSIONINFO osvi;
     ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
@@ -153,9 +197,12 @@ void print_system_info_os(void)
     SYSTEM_INFO sysi;
     ZeroMemory(&sysi, sizeof(SYSTEM_INFO));
 
+#if 0
     GetSystemInfo(&sysi);
     INFO("* Processor type : %d, Number of processors : %d\n",
             sysi.dwProcessorType, sysi.dwNumberOfProcessors);
+#endif
+    get_number_of_processors();
 
     MEMORYSTATUSEX memInfo;
     memInfo.dwLength = sizeof(MEMORYSTATUSEX);
@@ -163,3 +210,202 @@ void print_system_info_os(void)
     INFO("* Total Ram : %llu kB, Free: %lld kB\n",
             memInfo.ullTotalPhys / 1024, memInfo.ullAvailPhys / 1024);
 }
+
+char *get_timeofday(void)
+{
+    qemu_gettimeofday(&tv);
+    ti = tv.tv_sec;
+
+    struct tm *ptm = localtime(&ti);
+    strftime(buf_time, sizeof(buf_time),
+             "%H:%M:%S", ptm);
+
+    return buf_time;
+}
+
+static int get_auto_proxy(BYTE *url, char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
+{
+    char type[MAXLEN];
+    char proxy[MAXLEN];
+    char line[MAXLEN];
+    FILE *fp_pacfile;
+    char *p = NULL;
+
+    INFO("pac address: %s\n", (char*)url);
+    download_url((char*)url);
+
+    fp_pacfile = fopen(pactempfile, "r");
+    if(fp_pacfile != NULL) {
+        while(fgets(line, MAXLEN, fp_pacfile) != NULL) {
+            if( (strstr(line, "return") != NULL) && (strstr(line, "if") == NULL)) {
+                INFO("line found %s", line);
+                sscanf(line, "%*[^\"]\"%s %s", type, proxy);
+            }
+        }
+
+        if(g_str_has_prefix(type, DIRECT)) {
+            INFO("auto proxy is set to direct mode\n");
+            fclose(fp_pacfile);
+        }
+        else if(g_str_has_prefix(type, PROXY)) {
+            INFO("auto proxy is set to proxy mode\n");
+            INFO("type: %s, proxy: %s\n", type, proxy);
+            p = strtok(proxy, "\";");
+            if(p != NULL) {
+                INFO("auto proxy to set: %s\n",p);
+                strcpy(http_proxy, p);
+                strcpy(https_proxy, p);
+                strcpy(ftp_proxy, p);
+                strcpy(socks_proxy, p);
+            }
+            fclose(fp_pacfile);
+        }
+        else
+        {
+            ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
+            fclose(fp_pacfile);
+        }
+    }
+    else {
+        ERR("fail to get pacfile fp\n");
+        return -1;
+    }
+
+    remove(pactempfile);
+
+    return 0;
+}
+
+void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
+{
+    HKEY hKey;
+    int nRet;
+    LONG lRet;
+    BYTE *proxyenable, *proxyserver;
+    char *p;
+    char *real_proxy;
+    BYTE *url;
+
+    DWORD dwLength = 0;
+    nRet = RegOpenKeyEx(HKEY_CURRENT_USER,
+            "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
+            0, KEY_QUERY_VALUE, &hKey);
+    if (nRet != ERROR_SUCCESS) {
+        ERR("Failed to open registry from %s\n",
+                "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
+        return 0;
+    }
+    //check auto proxy key exists
+    lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, NULL, &dwLength);
+    if (lRet != ERROR_SUCCESS && dwLength == 0) {
+        ERR("Failed to query value from %s\n",
+                "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\AutoConfigURL");
+    }
+    else {
+        //if exists
+        url = (char*)malloc(dwLength);
+        if (url == NULL) {
+            ERR( "Failed to allocate a buffer\n");
+        }
+        else {
+            memset(url, 0x00, dwLength);
+            lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, url, &dwLength);
+            if (lRet == ERROR_SUCCESS && dwLength != 0) {
+                get_auto_proxy(url, http_proxy, https_proxy, ftp_proxy, socks_proxy);
+                RegCloseKey(hKey);
+                return 0;
+            }
+        }
+    }
+    //check manual proxy key exists
+    lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, NULL, &dwLength);
+    if (lRet != ERROR_SUCCESS && dwLength == 0) {
+        ERR(stderr, "Failed to query value from %s\n",
+                "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
+        RegCloseKey(hKey);
+        return 0;
+    }
+    proxyenable = (BYTE*)malloc(dwLength);
+    if (proxyenable == NULL) {
+        ERR( "Failed to allocate a buffer\n");
+        RegCloseKey(hKey);
+        return 0;
+    }
+
+    lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, proxyenable, &dwLength);
+    if (lRet != ERROR_SUCCESS) {
+        free(proxyenable);
+        ERR("Failed to query value from %s\n",
+                "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
+        RegCloseKey(hKey);
+        return 0;
+    }
+    if (*(char*)proxyenable == 0) {
+        free(proxyenable);
+        RegCloseKey(hKey);
+        return 0;
+    }
+
+    dwLength = 0;
+    lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, NULL, &dwLength);
+    if (lRet != ERROR_SUCCESS && dwLength == 0) {
+        ERR("Failed to query value from from %s\n",
+                "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
+        RegCloseKey(hKey);
+        return 0;
+    }
+
+    proxyserver = (BYTE*)malloc(dwLength);
+    if (proxyserver == NULL) {
+        ERR( "Failed to allocate a buffer\n");
+        RegCloseKey(hKey);
+        return 0;
+    }
+
+    memset(proxyserver, 0x00, dwLength);
+    lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, proxyserver, &dwLength);
+    if (lRet != ERROR_SUCCESS) {
+        free(proxyserver);
+        ERR( "Failed to query value from from %s\n",
+                "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
+        RegCloseKey(hKey);
+        return 0;
+    }
+
+    if((char*)proxyserver != NULL) {
+        INFO("proxy value: %s\n", (char*)proxyserver);
+        real_proxy = malloc(MAXLEN);
+
+        for(p = strtok((char*)proxyserver, ";"); p; p = strtok(NULL, ";")){
+            if(strstr(p, HTTP_PROTOCOL)) {
+                remove_string(p, real_proxy, HTTP_PROTOCOL);
+                strcpy(http_proxy, real_proxy);
+            }
+            else if(strstr(p, HTTPS_PROTOCOL)) {
+                remove_string(p, real_proxy, HTTPS_PROTOCOL);
+                strcpy(https_proxy, real_proxy);
+            }
+            else if(strstr(p, FTP_PROTOCOL)) {
+                remove_string(p, real_proxy, FTP_PROTOCOL);
+                strcpy(ftp_proxy, real_proxy);
+            }
+            else if(strstr(p, SOCKS_PROTOCOL)) {
+                remove_string(p, real_proxy, SOCKS_PROTOCOL);
+                strcpy(socks_proxy, real_proxy);
+            }
+            else {
+                INFO("all protocol uses the same proxy server: %s\n", p);
+                strcpy(http_proxy, p);
+                strcpy(https_proxy, p);
+                strcpy(ftp_proxy, p);
+                strcpy(socks_proxy, p);
+            }
+        }
+        free(real_proxy);
+    }
+    else {
+        INFO("proxy is null\n");
+        return 0;
+    }
+    RegCloseKey(hKey);
+}