Merge from 2.2
[platform/framework/native/telephony.git] / src / FTel_SimInfoImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        FTel_SimInfoImpl.cpp
19  * @brief       This is the implementation file for _SimInfoImpl class.
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sim.h>
25 #include <FBaseString.h>
26 #include <FBaseResult.h>
27 #include <unique_ptr.h>
28 #include <FTelSimInfo.h>
29 #include <FBaseSysLog.h>
30 #include "FTel_SimInfoImpl.h"
31 #include "FTel_TelephonyIpcProxy.h"
32 #include "FTel_TelephonyUtility.h"
33
34 #include <ITapiSim.h>
35
36
37 using namespace std;
38 using namespace Tizen::App;
39 using namespace Tizen::Base;
40
41 namespace Tizen { namespace Telephony
42 {
43
44
45 _SimInfoImpl::_SimInfoImpl(void)
46         : __isAvailable(false)
47 {
48 }
49
50 _SimInfoImpl::_SimInfoImpl(const _SimInfoImpl& rhs)
51         : __isAvailable(rhs.__isAvailable)
52 {
53 }
54
55 _SimInfoImpl::~_SimInfoImpl(void)
56 {
57 }
58
59 result
60 _SimInfoImpl::Construct(void)
61 {
62         int err = SIM_ERROR_NONE;
63         sim_state_e simState = SIM_STATE_UNAVAILABLE;
64
65         err = sim_get_state(&simState);
66         SysLog(NID_TEL, "The return value of sim_get_state() is [0x%x] and the simState is [%d]", err, simState);
67
68         if (err == SIM_ERROR_NONE && simState == SIM_STATE_AVAILABLE)
69         {
70                 __isAvailable = true;
71         }
72
73         SysTryReturnResult(NID_TEL, __isAvailable, E_DEVICE_UNAVAILABLE, "The operation failed because the SIM card is not ready.");
74
75         return E_SUCCESS;
76 }
77
78 int
79 _SimInfoImpl::GetMnc(void) const
80 {
81         int err = SIM_ERROR_NONE;
82         char* pMnc = null;
83         int mnc = -1;
84
85         SysTryReturn(NID_TEL, __isAvailable, mnc, E_DEVICE_UNAVAILABLE,
86                 "[%s] The operation failed because the SIM card is not ready.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
87
88         err = sim_get_mnc(&pMnc);
89         SysLog(NID_TEL, "The return value of sim_get_mnc() is [0x%x] and the mnc value is [%s]", err, pMnc);
90         SysTryReturn(NID_TEL, err == SIM_ERROR_NONE && pMnc != null, mnc, E_DEVICE_UNAVAILABLE,
91                 "[%s] The operation failed.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
92
93         mnc = atoi(pMnc);
94         free(pMnc);
95
96         return mnc;
97 }
98
99 int
100 _SimInfoImpl::GetMcc(void) const
101 {
102         int err = SIM_ERROR_NONE;
103         char* pMcc = null;
104         int mcc = -1;
105
106         SysTryReturn(NID_TEL, __isAvailable, mcc, E_DEVICE_UNAVAILABLE,
107                 "[%s] The operation failed because the SIM card is not ready.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
108
109         err = sim_get_mcc(&pMcc);
110         SysLog(NID_TEL, "The return value of sim_get_mcc() is [0x%x] and the mcc value is [%s]", err, pMcc);
111         SysTryReturn(NID_TEL, err == SIM_ERROR_NONE && pMcc != null, mcc, E_DEVICE_UNAVAILABLE,
112                 "[%s] The operation failed.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
113
114         mcc = atoi(pMcc);
115         free(pMcc);
116
117         return mcc;
118 }
119
120 String
121 _SimInfoImpl::GetSpn(void) const
122 {
123         int err = SIM_ERROR_NONE;
124         char* pSpn = null;
125         String spn;
126
127         SysTryReturn(NID_TEL, __isAvailable, spn, E_DEVICE_UNAVAILABLE,
128                 "[%s] The operation failed because the SIM card is not ready.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
129
130         err = sim_get_spn(&pSpn);
131         SysLog(NID_TEL, "The return value of sim_get_spn() is [0x%x] and the spn value is [%s]", err, pSpn);
132         SysTryReturn(NID_TEL, err == SIM_ERROR_NONE, spn, E_DEVICE_UNAVAILABLE,
133                 "[%s] The operation failed.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
134
135         spn = String(pSpn);
136
137         if (pSpn != null)
138         {
139                 free(pSpn);
140         }
141
142         return spn;
143 }
144
145 String
146 _SimInfoImpl::GetIccId(void) const
147 {
148         int err = SIM_ERROR_NONE;
149         char* pIccId = null;
150         String iccId;
151
152         SysTryReturn(NID_TEL, __isAvailable, iccId, E_DEVICE_UNAVAILABLE,
153                 "[%s] The operation failed because the SIM card is not ready.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
154
155         err = sim_get_icc_id(&pIccId);
156         SysSecureLog(NID_TEL, "The return value of sim_get_icc_id() is [0x%x] and the iccid value is [%s]", err, pIccId);
157         SysTryReturn(NID_TEL, err == SIM_ERROR_NONE, iccId, E_DEVICE_UNAVAILABLE,
158                 "[%s] The operation failed.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
159
160         iccId = String(pIccId);
161
162         if (pIccId != null)
163         {
164                 free(pIccId);
165         }
166
167         return iccId;
168 }
169
170 String
171 _SimInfoImpl::GetOperatorName(void) const
172 {
173         int err = SIM_ERROR_NONE;
174         char* pFullName = null;
175         char* pShortName = null;
176         String operatorName;
177
178         SysTryReturn(NID_TEL, __isAvailable, operatorName, E_DEVICE_UNAVAILABLE,
179                 "[%s] The operation failed because the SIM card is not ready.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
180
181         err = sim_get_cphs_operator_name(&pFullName, &pShortName);
182         SysSecureLog(NID_TEL, "The return value of sim_get_cphs_operator_name() is [0x%x]. full[%s] short[%s]", err, pFullName, pShortName);
183         SysTryReturn(NID_TEL, err == SIM_ERROR_NONE, operatorName, E_DEVICE_UNAVAILABLE,
184                 "[%s] The operation failed.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
185
186         if ((pFullName != null) && (strlen(pFullName) > 0))
187         {
188                 operatorName = String(pFullName);
189         }
190         else if ((pShortName != null) && (strlen(pShortName) > 0))
191         {
192                 operatorName = String(pShortName);
193         }
194
195         if (pFullName != null)
196         {
197                 free(pFullName);
198         }
199         if (pShortName != null)
200         {
201                 free(pShortName);
202         }
203
204         return operatorName;
205 }
206
207 String
208 _SimInfoImpl::GetPhoneNumber(void) const
209 {
210         int err = SIM_ERROR_NONE;
211         char* pPhoneNumber = null;
212         String phoneNumber;
213
214         SysTryReturn(NID_TEL, __isAvailable, phoneNumber, E_DEVICE_UNAVAILABLE,
215                 "[%s] The operation failed because the SIM card is not ready.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
216
217         err = sim_get_subscriber_number(&pPhoneNumber);
218         SysLog(NID_TEL, "The return value of sim_get_subscriber_number() is [0x%x] and the phone number is [%s]", err, pPhoneNumber);
219         SysTryReturn(NID_TEL, err == SIM_ERROR_NONE, phoneNumber, E_DEVICE_UNAVAILABLE,
220                 "[%s] The operation failed.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
221
222         phoneNumber = String(pPhoneNumber);
223
224         if (pPhoneNumber != null)
225         {
226                 free(pPhoneNumber);
227         }
228
229         return phoneNumber;
230 }
231
232 String
233 _SimInfoImpl::GetImsi(void) const
234 {
235         result r = E_SUCCESS;
236         String imsi;
237
238         SysTryReturn(NID_TEL, __isAvailable, imsi, E_DEVICE_UNAVAILABLE,
239                 "[%s] The operation failed due to a missing SIM card.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
240
241         _TelephonyIpcProxy* pTelephonyServiceProxy = _TelephonyIpcProxy::GetInstance();
242         SysTryReturn(NID_TEL, pTelephonyServiceProxy != null, imsi, E_SYSTEM, "[%s] A system error has occured. IPC instance has not been constructed yet.", GetErrorMessage(E_SYSTEM));
243
244         r = pTelephonyServiceProxy->GetImsi(imsi);
245         SysTryReturn(NID_TEL, r == E_SUCCESS, imsi, r, "[%s] Failed to get the imsi.", GetErrorMessage(r));
246
247         return imsi;
248 }
249
250 SimType
251 _SimInfoImpl::GetSimType(void) const
252 {
253         TapiHandle* pHandle = null;
254         int err = SIM_ERROR_NONE;
255         TelSimCardType_t simCardType = TAPI_SIM_CARD_TYPE_UNKNOWN;
256         SimType simType = SIM_TYPE_UNKNOWN;
257
258         SysTryReturn(NID_TEL, __isAvailable, SIM_TYPE_UNKNOWN, E_DEVICE_UNAVAILABLE,
259                 "[%s] The operation failed because the SIM card is not ready.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
260
261         pHandle = tel_init(null);
262         SysTryReturn(NID_TEL, pHandle != null, SIM_TYPE_UNKNOWN, E_SYSTEM,
263                 "[%s] A system error has occurred. Failed to initialize TAPI library.", GetErrorMessage(E_SYSTEM));
264
265         err = tel_get_sim_type(pHandle, &simCardType);
266         SysLog(NID_TEL, "The return value of tel_get_sim_type() is [0x%x] and the SIM type is [%d]", err, simCardType);
267
268         switch(simCardType)
269         {
270         case TAPI_SIM_CARD_TYPE_GSM:
271                 simType = SIM_TYPE_GSM;
272                 break;
273         case TAPI_SIM_CARD_TYPE_USIM:
274                 simType = SIM_TYPE_USIM;
275                 break;
276         case TAPI_SIM_CARD_TYPE_RUIM:
277                 simType = SIM_TYPE_RUIM;
278                 break;
279         case TAPI_SIM_CARD_TYPE_IMS:
280                 simType = SIM_TYPE_ISIM;
281                 break;
282         case TAPI_SIM_CARD_TYPE_UNKNOWN:        // Fall through
283         default:
284                 simType = SIM_TYPE_UNKNOWN;
285                 break;
286         }
287
288         tel_deinit(pHandle);
289
290         return simType;
291 }
292
293 bool
294 _SimInfoImpl::IsAvailable(void) const
295 {
296         return __isAvailable;
297 }
298
299 _SimInfoImpl&
300 _SimInfoImpl::operator =(const _SimInfoImpl& rhs)
301 {
302         if (this != &rhs)
303         {
304                 __isAvailable = rhs.__isAvailable;
305         }
306
307         return *this;
308 }
309
310 bool
311 _SimInfoImpl::Equals(const Tizen::Base::Object& rhs) const
312 {
313         const _SimInfoImpl* pRhs = dynamic_cast <const _SimInfoImpl*>(&rhs);
314
315         if (pRhs == null)
316         {
317                 return false;
318         }
319
320         if (__isAvailable != pRhs->__isAvailable)
321         {
322                 return false;
323         }
324
325         return true;
326 }
327
328 int
329 _SimInfoImpl::GetHashCode(void) const
330 {
331         int hashCode = _HASH_CODE_INITIAL_VALUE;
332
333         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + (__isAvailable ? 0 : 1);
334
335         return hashCode;
336 }
337
338 _SimInfoImpl*
339 _SimInfoImpl::GetInstance(SimInfo& simInfo)
340 {
341         return simInfo.__pSimInfoImpl;
342 }
343
344 const _SimInfoImpl*
345 _SimInfoImpl::GetInstance(const SimInfo& simInfo)
346 {
347         return simInfo.__pSimInfoImpl;
348 }
349
350 }} // Tizen::Telephony