Update code of system and text for reporting Klocwork.
[platform/framework/native/appfw.git] / src / system / FSysSystemInfo.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                FSysSystemInfo.cpp
19  * @brief               This is the implementation file for SystemInfo class.
20  */
21
22 #include <unique_ptr.h>
23 #include <FSysSystemInfo.h>
24
25 #include <FBaseSysLog.h>
26 #include <FIo_AppServiceIpcMessages.h>
27 #include <FIo_IpcClient.h>
28 #include <FSys_SystemInfoImpl.h>
29 #include <FSec_AccessController.h>
30 #include <FSys_Types.h>
31
32 using namespace std;
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Io;
37 using namespace Tizen::Security;
38
39 namespace Tizen { namespace System
40 {
41
42 SystemInfo::SystemInfo(void)
43         :__pSystemInfoImpl(null)
44 {
45 }
46
47 SystemInfo::~SystemInfo(void)
48 {
49 }
50
51 result
52 SystemInfo::GetValue(const Tizen::Base::String& key, bool& value)
53 {
54         SysTryReturn(NID_SYS, key.GetLength() > 0, E_INVALID_ARG, E_INVALID_ARG, ("Key length should be greater than 0."));
55
56         return _SystemInfoImpl::GetSysInfo(key, value);
57 }
58
59 result
60 SystemInfo::GetValue(const String& key, String& value)
61 {
62         SysTryReturn(NID_SYS, key.GetLength() > 0, E_INVALID_ARG, E_INVALID_ARG, ("Key length should be greater than 0."));
63
64         return _SystemInfoImpl::GetSysInfo(key, value);
65 }
66
67 result
68 SystemInfo::GetValue(const String& key, int& value)
69 {
70         SysTryReturn(NID_SYS, key.GetLength() > 0, E_INVALID_ARG, E_INVALID_ARG, ("Key length should be greater than 0."));
71
72         return _SystemInfoImpl::GetSysInfo(key, value);
73 }
74
75 result
76 SystemInfo::GetValue(const String& key, double& value)
77 {
78         SysTryReturn(NID_SYS, key.GetLength() > 0, E_INVALID_ARG, E_INVALID_ARG, ("Key length should be greater than 0."));
79
80         return _SystemInfoImpl::GetSysInfo(key, value);
81 }
82
83 result
84 SystemInfo::GetValue(const String& key, UuId& value)
85 {
86         SysTryReturn(NID_SYS, key.GetLength() > 0, E_INVALID_ARG, E_INVALID_ARG, ("Key length should be greater than 0."));
87
88         return _SystemInfoImpl::GetSysInfo(key, value);
89 }
90
91 IList*
92 SystemInfo::GetValuesN(const String& key, SystemInfoValueType type)
93 {
94         result r = E_SUCCESS;
95         IList* pValues = null;  
96         SysTryCatch(NID_SYS, key.GetLength() > 0, r = E_INVALID_ARG, E_INVALID_ARG, ("Key length should be greater than 0."));
97
98         pValues = _SystemInfoImpl::GetSysInfoValuesN(key, type);
99         if(pValues == null)
100         {
101                 r = E_OBJ_NOT_FOUND;
102         }
103 CATCH:
104         SetLastResult(r);
105         return pValues;
106 }
107
108 result
109 SystemInfo::GetPlatformVersion(String& platformVersion)
110 {
111         result r = _AccessController::CheckUserPrivilege(_PRV_PLATFORMINFO, _PRV_SYSTEM);
112         SysTryReturn(NID_SYS, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, ("[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method."));
113         return _SystemInfoImpl::GetPlatformVersion(platformVersion);
114 }
115
116 result
117 SystemInfo::GetBuildInfo(String& buildInfo)
118 {
119         result r = _AccessController::CheckUserPrivilege(_PRV_PLATFORMINFO, _PRV_SYSTEM);
120         SysTryReturn(NID_SYS, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, ("[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method."));
121         return _SystemInfoImpl::GetBuildInfo(buildInfo);
122 }
123
124 result
125 SystemInfo::GetNativeApiVersion(String& nativeApiVersion)
126 {
127         result r = _AccessController::CheckUserPrivilege(_PRV_PLATFORMINFO, _PRV_SYSTEM);
128         SysTryReturn(NID_SYS, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, ("[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method."));
129         return _SystemInfoImpl::GetNativeApiVersion(nativeApiVersion);
130 }
131
132 result
133 SystemInfo::GetWebApiVersion(String& webApiVersion)
134 {
135         result r = _AccessController::CheckUserPrivilege(_PRV_PLATFORMINFO, _PRV_SYSTEM);
136         SysTryReturn(NID_SYS, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, ("[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method."));
137         return _SystemInfoImpl::GetWebApiVersion(webApiVersion);
138 }
139
140 result
141 SystemInfo::GetImei(String& imei)
142 {
143         result r = E_SUCCESS;
144
145         ArrayList requestMessage;
146         ArrayList responseMessage;
147
148         unique_ptr<_IpcClient> pIpcClient (new (std::nothrow) _IpcClient());
149         SysTryReturn(NID_SYS, pIpcClient != null, E_OUT_OF_MEMORY, r, "It is failed to create IPC instance.");
150
151         r = pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID);
152         SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "It is failed to create IpcClient. [%s]", GetErrorMessage(r));
153
154         requestMessage.Construct();
155         responseMessage.Construct();
156
157         String serviceId = _SYSTEM_SERVICE_ID;
158         String commandId = _SYSTEM_COMMAND_GET_IMEI;
159
160         requestMessage.Add(serviceId);
161         requestMessage.Add(commandId);
162
163         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
164         SysTryReturnResult(NID_SYS, pMsg != null, E_OUT_OF_MEMORY, "It is failed to create Ipc message");
165
166         r = pIpcClient->SendRequest(pMsg.get());
167         SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "It is failed to send ipc message. [%s]", GetErrorMessage(r));
168
169         String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
170         String* pImei = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA+1);
171
172         SysTryCatch(NID_SYS, pResult != null, r = E_SYSTEM, r, "It is failed to receive result on IPC response message.");
173         SysTryCatch(NID_SYS, pImei != null, r = E_SYSTEM, r, "It is failed to receive IMEI value on IPC response message.");
174
175         SysTryCatch(NID_SYS, *pResult != _SYSTEM_RESULT_PRIVILEGED, r = E_PRIVILEGE_DENIED, r, "It is failed to get privilege.");
176         SysTryCatch(NID_SYS, *pResult == _SYSTEM_RESULT_OK, r = E_SYSTEM, r, "It is failed to get IMEI value.");
177
178 CATCH:
179         if(r == E_SUCCESS)
180         {
181                 imei = *pImei;
182         }
183
184         responseMessage.RemoveAll(true);
185         return r;
186 }
187
188 }}  // Tizen::System