Add to read a custum feature key.
[platform/framework/native/appfw.git] / src / system / FSys_SystemInfoImpl.cpp
index 86eaf5d..8cc29b0 100644 (file)
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
  * @brief              This is the implementation file for _SystemInfoImpl class.
  */
 #include <unique_ptr.h>
+#include <unistd.h>
+
+#include <system_info.h>
+#include <ITapiModem.h>
 
 #include <FBaseBoolean.h>
 #include <FBaseInteger.h>
 #include <FBaseColHashMap.h>
+#include <FBaseUtilStringUtil.h>
+#include <FIoRegistry.h>
 
 #include <FBase_NativeError.h>
+#include <FBase_StringConverter.h>
 #include <FIo_RegistryImpl.h>
 #include <FSys_SystemInfoImpl.h>
 #include <FIo_AppServiceIpcMessages.h>
@@ -40,6 +46,7 @@ using namespace std;
 using namespace Tizen::Base;
 using namespace Tizen::Base::Collection;
 using namespace Tizen::Io;
+using namespace Tizen::Base::Utility;
 
 namespace Tizen { namespace System
 {
@@ -150,9 +157,9 @@ static const wchar_t* _SUPPORTED = L"Supported";
 static const wchar_t* _UNSUPPORTED = L"Unsupported";
 
 static bool firstRequest = false;
-static HashMap integerList;
-static HashMap boolList;
-static HashMap stringList;
+static HashMap integerList(SingleObjectDeleter);
+static HashMap boolList(SingleObjectDeleter);
+static HashMap stringList(SingleObjectDeleter);
 
 void
 PrepareCache(void)
@@ -260,17 +267,19 @@ _SystemInfoImpl::GetSysInfo(const String& key, String& value)
        result r = E_OBJ_NOT_FOUND;
        String tizenKey = ConvertToTizen(key);
 
+       SysLog(NID_SYS, "Request Key is %ls.", key.GetPointer());
+
        if (firstRequest == false)
        {
                PrepareCache();
        }
 
-       String* pValue = (String*)stringList.GetValue(key);
+       String* pValue = (String*)stringList.GetValue(tizenKey);
 
        if(pValue != null)
        {
                value = *pValue;
-               r = E_SUCCESS;
+               return E_SUCCESS;
        }
 
        if (key == _NETWORK_TYPE) //Compatibility
@@ -433,19 +442,79 @@ _SystemInfoImpl::GetSysInfo(const String& key, String& value)
        {
                r = E_OBJ_NOT_FOUND;
        }
+       else if (tizenKey == _DUID)
+       {
+               r = GetFromRegistry(tizenKey, value);
+
+               if(r != E_SUCCESS || value == L"ERROR")
+               {
+                       ArrayList requestMessage;
+                       ArrayList responseMessage;
+
+                       _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
+
+                       _IpcClient* pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
+                       SysTryReturn(NID_SYS, pIpcClient != null, E_OUT_OF_MEMORY, r, "It is failed to create IPC instance.");
+
+                       requestMessage.Construct();
+                       responseMessage.Construct();
+
+                       String serviceId(_SYSTEM_SERVICE_ID);
+                       String commandId(L"osp.system.command.get.duid.internal");
+
+                       requestMessage.Add(serviceId);
+                       requestMessage.Add(commandId);
+
+                       unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
+                       SysTryReturnResult(NID_SYS, pMsg != null, E_OUT_OF_MEMORY, "It is failed to create Ipc message");
+
+                       r = pIpcClient->SendRequest(pMsg.get());
+                       SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "It is failed to send ipc message. [%s]", GetErrorMessage(r));
+
+                       String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
+                       String* pDuid = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA+1);
+
+                       SysTryReturn(NID_SYS, pResult != null, r = E_SYSTEM, r, "It is failed to receive result on IPC response message.");
+                       SysTryReturn(NID_SYS, pDuid != null, r = E_SYSTEM, r, "It is failed to receive IMEI value on IPC response message.");
+
+                       SysTryReturn(NID_SYS, *pResult != _SYSTEM_RESULT_PRIVILEGED, r = E_PRIVILEGE_DENIED, r, "It is failed to get privilege.");
+                       SysTryReturn(NID_SYS, *pResult == _SYSTEM_RESULT_OK, r = E_SYSTEM, r, "It is failed to get DUID value.");
+
+                       value = *pDuid;
+
+                       responseMessage.RemoveAll(true);
+               }
+       }
        else
        {
                r = GetFromRegistry(tizenKey, value);
 
                if(r != E_SUCCESS)
                {
-                       _SystemClient* pSystemClient = _SystemClient::GetInstance();
-                       r = pSystemClient->GetValue(key, value);
+                       char* pStringValue = null;
+                       String tKey;
+                       tizenKey.SubString(7, tKey);
+                       SysTryReturnResult(NID_SYS, tKey.GetLength() > 0, E_OBJ_NOT_FOUND, "Required key should be longer than 0.");
+
+                       ClearLastResult();
+                       unique_ptr< char[] > systemKey(_StringConverter::CopyToCharArrayN(tKey));
+                       r = GetLastResult();
+                       SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] StringUtil::StringToUtf8N It is failed", GetErrorMessage(r));
+
+                       int ret = system_info_get_platform_string(systemKey.get(), &pStringValue);
+                       if (ret != SYSTEM_INFO_ERROR_NONE)
+                       {
+                               ret = system_info_get_custom_string(systemKey.get(), &pStringValue);
+                               SysTryReturnResult(NID_SYS, ret == SYSTEM_INFO_ERROR_NONE, E_OBJ_NOT_FOUND, "It is failed to get system information %ls from configration file.", tizenKey.GetPointer());
+                       }
+                       r = StringUtil::Utf8ToString(pStringValue, value);
+                       free(pStringValue);
+                       r = E_SUCCESS;
                }
        }
        if( r == E_SUCCESS)
        {
-               stringList.Add(new String(key), new String(value));
+               stringList.Add(new String(tizenKey), new String(value));
        }
        return r;
 }
@@ -456,12 +525,14 @@ _SystemInfoImpl::GetSysInfo(const String& key, int& value)
        result r = E_SUCCESS;
        String tizenKey = ConvertToTizen(key);
 
+       SysLog(NID_SYS, "Request Key is %ls.", key.GetPointer());
+
        if (firstRequest == false)
        {
                PrepareCache();
        }
 
-       Integer* pValue = (Integer*)integerList.GetValue(key);
+       Integer* pValue = (Integer*)integerList.GetValue(tizenKey);
 
        if(pValue != null)
        {
@@ -487,16 +558,33 @@ _SystemInfoImpl::GetSysInfo(const String& key, int& value)
        else
        {
                r = GetFromRegistry(tizenKey, value);
-               if(r != E_SUCCESS)
+               if(r != E_SUCCESS || tizenKey == _SCREEN_WIDTH || tizenKey == _SCREEN_HEIGHT || tizenKey == _SCREEN_DPI)
                {
-                       _SystemClient* pSystemClient = _SystemClient::GetInstance();
-                       r = pSystemClient->GetValue(key, value);
+                       int ret = 0;
+                       String tKey;
+
+                       tizenKey.SubString(7, tKey);
+                       SysTryReturnResult(NID_SYS, tKey.GetLength() > 0, E_OBJ_NOT_FOUND, "Required key should be longer than 0.");
+
+                       ClearLastResult();
+                       unique_ptr< char[] > systemKey(_StringConverter::CopyToCharArrayN(tKey));
+                       r = GetLastResult(); 
+                       SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] StringUtil::StringToUtf8N It is failed", GetErrorMessage(r));
+
+                       ret = system_info_get_platform_int(systemKey.get(), &value);
+                       if (ret != SYSTEM_INFO_ERROR_NONE)
+                       {
+                               ret = system_info_get_custom_int(systemKey.get(), &value);
+                               SysTryReturnResult(NID_SYS, ret == SYSTEM_INFO_ERROR_NONE, E_OBJ_NOT_FOUND,
+                                       "It is failed to get system information %ls from configration file.", tizenKey.GetPointer());
+                       }
+                       r = E_SUCCESS;
                }
        }
 
        if(r == E_SUCCESS)
        {
-               integerList.Add(new String(key), new Integer(value));
+               integerList.Add(new String(tizenKey), new Integer(value));
        }
 
        return r;
@@ -520,12 +608,14 @@ _SystemInfoImpl::GetSysInfo(const String& key, bool& value)
        result r = E_SUCCESS;
        String tizenKey = ConvertToTizen(key);
 
+       SysLog(NID_SYS, "Request Key is %ls.", key.GetPointer());
+
        if (firstRequest == false)
        {
                PrepareCache();
        }
 
-       Boolean* pValue = (Boolean*)boolList.GetValue(key);
+       Boolean* pValue = (Boolean*)boolList.GetValue(tizenKey);
 
        if(pValue != null)
        {
@@ -536,14 +626,41 @@ _SystemInfoImpl::GetSysInfo(const String& key, bool& value)
        r = GetFromRegistry(tizenKey, value);
        if(r != E_SUCCESS)
        {
-               _SystemClient* pSystemClient = _SystemClient::GetInstance();
-               r = pSystemClient->GetValue(key, value);
+               String tKey;
+               tizenKey.SubString(7, tKey);
+               SysTryReturnResult(NID_SYS, tKey.GetLength() > 0, E_OBJ_NOT_FOUND, "Required key should be longer than 0.");
+
+               ClearLastResult();
+               unique_ptr< char[] > systemKey(_StringConverter::CopyToCharArrayN(tKey));
+               r = GetLastResult();
+               SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] StringUtil::StringToUtf8N It is failed", GetErrorMessage(r));
+
+               bool supported = false;
+               int ret = system_info_get_platform_bool(systemKey.get(), &supported);
+               if(ret != SYSTEM_INFO_ERROR_NONE)
+               {
+                       ret = system_info_get_custom_bool(systemKey.get(), &supported);
+                       if (ret != SYSTEM_INFO_ERROR_NONE)
+                       {
+                               String screenKey(L"http://tizen.org/feature/screen.size");
+                               String requiredKey;
+                               tizenKey.SubString(0, screenKey.GetLength(), requiredKey);
+
+                               if(requiredKey == screenKey)
+                               {
+                                       value = false;
+                               }
+                       }
+               }
+
+               SysTryReturnResult(NID_SYS, ret == SYSTEM_INFO_ERROR_NONE, E_OBJ_NOT_FOUND, "It is failed to get system information %ls from configration file.", tizenKey.GetPointer());
+               value = supported;
+               r = E_SUCCESS;
        }
-       SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "It is failed to get the key[%ls]", tizenKey.GetPointer());
 
        if(r == E_SUCCESS)
        {
-               boolList.Add(new String(key), new Boolean(value));
+               boolList.Add(new String(tizenKey), new Boolean(value));
        }
 
        return r;
@@ -569,7 +686,26 @@ _SystemInfoImpl::GetPlatformVersion(String& platformVersion)
        String tizenKey(_PLATFORM_VERSION);
 
        r = GetFromRegistry(tizenKey, platformVersion);
-       SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get the key[%ls]", tizenKey.GetPointer());
+
+       if(r != E_SUCCESS)
+       {
+               char* pStringValue = null;
+               String tKey;
+               tizenKey.SubString(7, tKey);
+               SysTryReturnResult(NID_SYS, tKey.GetLength() > 0, E_SYSTEM, "Required key should be longer than 0.");
+
+               ClearLastResult();
+               unique_ptr< char[] > systemKey(_StringConverter::CopyToCharArrayN(tKey));
+               r = GetLastResult();
+               SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] StringUtil::StringToUtf8N It is failed", GetErrorMessage(r));
+
+               int ret = system_info_get_platform_string(systemKey.get(), &pStringValue);
+               SysTryReturnResult(NID_SYS, ret == SYSTEM_INFO_ERROR_NONE, E_OBJ_NOT_FOUND, "It is failed to get system information %ls from configration file.", tizenKey.GetPointer());
+
+               r = StringUtil::Utf8ToString(pStringValue, platformVersion);
+               free(pStringValue);
+               r = E_SUCCESS;
+       }
        return r;
 }
 
@@ -580,7 +716,26 @@ _SystemInfoImpl::GetBuildInfo(String& buildInfo)
        String tizenKey(_BUILD_STRING);
 
        r = GetFromRegistry(tizenKey, buildInfo);
-       SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get the key[%ls]", tizenKey.GetPointer());
+
+       if(r != E_SUCCESS)
+       {
+               char* pStringValue = null;
+               String tKey;
+               tizenKey.SubString(7, tKey);
+               SysTryReturnResult(NID_SYS, tKey.GetLength() > 0, E_SYSTEM, "Required key should be longer than 0.");
+
+               ClearLastResult();
+               unique_ptr< char[] > systemKey(_StringConverter::CopyToCharArrayN(tKey));
+               r = GetLastResult();
+               SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] StringUtil::StringToUtf8N It is failed", GetErrorMessage(r));
+
+               int ret = system_info_get_platform_string(systemKey.get(), &pStringValue);
+               SysTryReturnResult(NID_SYS, ret == SYSTEM_INFO_ERROR_NONE, E_OBJ_NOT_FOUND, "It is failed to get system information %ls from configration file.", tizenKey.GetPointer());
+
+               r = StringUtil::Utf8ToString(pStringValue, buildInfo);
+               free(pStringValue);
+               r = E_SUCCESS;
+       }
        return r;
 }
 
@@ -591,7 +746,26 @@ _SystemInfoImpl::GetNativeApiVersion(String& nativeApiVersion)
        String tizenKey(_PLATFORM_NATIVE_API_VERSION);
 
        r = GetFromRegistry(tizenKey, nativeApiVersion);
-       SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get the key[%ls]", tizenKey.GetPointer());
+
+       if(r != E_SUCCESS)
+       {
+               char* pStringValue = null;
+               String tKey;
+               tizenKey.SubString(7, tKey);
+               SysTryReturnResult(NID_SYS, tKey.GetLength() > 0, E_SYSTEM, "Required key should be longer than 0.");
+
+               ClearLastResult();
+               unique_ptr< char[] > systemKey(_StringConverter::CopyToCharArrayN(tKey));
+               r = GetLastResult();
+               SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] StringUtil::StringToUtf8N It is failed", GetErrorMessage(r));
+
+               int ret = system_info_get_platform_string(systemKey.get(), &pStringValue);
+               SysTryReturnResult(NID_SYS, ret == SYSTEM_INFO_ERROR_NONE, E_OBJ_NOT_FOUND, "It is failed to get system information %ls from configration file.", tizenKey.GetPointer());
+
+               r = StringUtil::Utf8ToString(pStringValue, nativeApiVersion);
+               free(pStringValue);
+               r = E_SUCCESS;
+       }
        return r;
 }
 
@@ -602,7 +776,26 @@ _SystemInfoImpl::GetWebApiVersion(String& webApiVersion)
        String tizenKey(_PLATFORM_WEB_API_VERSION);
 
        r = GetFromRegistry(tizenKey, webApiVersion);
-       SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get the key[%ls]", tizenKey.GetPointer());
+
+       if(r != E_SUCCESS)
+       {
+               char* pStringValue = null;
+               String tKey;
+               tizenKey.SubString(7, tKey);
+               SysTryReturnResult(NID_SYS, tKey.GetLength() > 0, E_SYSTEM, "Required key should be longer than 0.");
+
+               ClearLastResult();
+               unique_ptr< char[] > systemKey(_StringConverter::CopyToCharArrayN(tKey));
+               r = GetLastResult();
+               SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] StringUtil::StringToUtf8N It is failed", GetErrorMessage(r));
+
+               int ret = system_info_get_platform_string(systemKey.get(), &pStringValue);
+               SysTryReturnResult(NID_SYS, ret == SYSTEM_INFO_ERROR_NONE, E_OBJ_NOT_FOUND, "It is failed to get system information %ls from configration file.", tizenKey.GetPointer());
+
+               r = StringUtil::Utf8ToString(pStringValue, webApiVersion);
+               free(pStringValue);
+               r = E_SUCCESS;
+       }
        return r;
 }
 
@@ -611,6 +804,34 @@ _SystemInfoImpl::GetImei(String& imei)
 {
        result r = E_SUCCESS;
 
+       int ret = 0;
+       TapiHandle* handle = null;
+       int time_count = 0;
+       int status = 0;
+
+       handle = tel_init(null);
+       SysTryReturnResult(NID_SYS, handle != null, E_SYSTEM, "It is failed to get handle of telephony.");
+       while(time_count < 30) //Wait 30 second.
+       {
+               ret = tel_check_modem_power_status(handle, &status);
+               SysLog(NID_SYS, "Modem check result is %d, status is %d.", handle, status);
+               if(ret != TAPI_API_SUCCESS)
+               {
+                       SysLogException(NID_SYS, E_SYSTEM, "It is failed to get Modem status.");
+                       tel_deinit(handle);
+                       return E_SYSTEM;
+               }
+
+               if(status == 0)
+               {
+                       break;
+               }
+
+               usleep(1000000);
+               time_count++;
+       }
+       tel_deinit(handle);
+
        ArrayList requestMessage;
        ArrayList responseMessage;
 
@@ -634,16 +855,18 @@ _SystemInfoImpl::GetImei(String& imei)
        r = pIpcClient->SendRequest(pMsg.get());
        SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "It is failed to send ipc message. [%s]", GetErrorMessage(r));
 
-       unique_ptr<String> pResult((String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA));
-       unique_ptr<String> pImei((String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA+1));
+       String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
+       String* pImei = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA+1);
 
-       SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "It is failed to receive result on IPC response message.");
-       SysTryReturnResult(NID_SYS, pImei != null, E_SYSTEM, "It is failed to receive IMEI value on IPC response message.");
+       SysTryReturn(NID_SYS, pResult != null, r = E_SYSTEM, r, "It is failed to receive result on IPC response message.");
+       SysTryReturn(NID_SYS, pImei != null, r = E_SYSTEM, r, "It is failed to receive IMEI value on IPC response message.");
 
-       SysTryReturnResult(NID_SYS, *pResult != _SYSTEM_RESULT_PRIVILEGED, E_PRIVILEGE_DENIED, "It is failed to get privilege.");
-       SysTryReturnResult(NID_SYS, *pResult == _SYSTEM_RESULT_OK, E_SYSTEM, "It is failed to get IMEI value.");
+       SysTryReturn(NID_SYS, *pResult != _SYSTEM_RESULT_PRIVILEGED, r = E_PRIVILEGE_DENIED, r, "It is failed to get privilege.");
+       SysTryReturn(NID_SYS, *pResult == _SYSTEM_RESULT_OK, r = E_SYSTEM, r, "It is failed to get IMEI value.");
 
        imei = *pImei;
+       responseMessage.RemoveAll(true);
+
        return r;
 }
 
@@ -658,7 +881,7 @@ _SystemInfoImpl::GetFromRegistry(const String& key, String& value)
        SysTryReturnResult(NID_SYS, r == E_SUCCESS , r, " RegistryImpl construct is failed");
 
        r = _reg.GetValue(_SYSTEM_INFO_SESSION, key, valStr);
-       SysTryReturnResult(NID_SYS, r == E_SUCCESS , r, " Registry GetValue is failed");
+       SysTryReturnResult(NID_SYS, r == E_SUCCESS , r, " Registry GetValue is failed, %ls", key.GetPointer());
 
        if(valStr == L"true" || valStr == L"false")
        {
@@ -678,12 +901,12 @@ _SystemInfoImpl::GetFromRegistry(const String& key, int& value)
        result r = E_SUCCESS;
        _RegistryImpl _reg;
        String valStr;
+
        r = _reg.Construct(_REGISTRY_PATH, REG_OPEN_READ_ONLY, null);
        SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, " RegistryImpl construct is failed");
 
        r = _reg.GetValue(_SYSTEM_INFO_SESSION, key, valStr);
-       SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, " Registry GetValue is failed");
-       SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, " Registry GetValue is failed");
+       SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, " Registry GetValue is failed, %ls", key.GetPointer());
        r = Integer::Parse(valStr, value);
        SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, " Integer::Parse() is failed");
        return E_SUCCESS;
@@ -695,12 +918,12 @@ _SystemInfoImpl::GetFromRegistry(const String& key, bool& value)
        result r = E_SUCCESS;
        _RegistryImpl _reg;
        String valStr;
+
        r = _reg.Construct(_REGISTRY_PATH, REG_OPEN_READ_ONLY, null);
        SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, " RegistryImpl construct is failed");
 
        r = _reg.GetValue(_SYSTEM_INFO_SESSION, key, valStr);
-       SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, " Registry GetValue is failed");
-       SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, " Registry GetValue is failed");
+       SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, " Registry GetValue is failed, [%ls]", key.GetPointer());
        if(valStr == L"true")
        {
                value = true;