merge with master
[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         : __mcc(-1)
47         , __mnc(-1)
48         , __simType(SIM_TYPE_UNKNOWN)
49         , __isAvailable(false)
50         , __isIccIdValid(false)
51         , __isMccValid(false)
52         , __isMncValid(false)
53         , __isOperatorNameValid(false)
54         , __isPhoneNumberValid(false)
55         , __isSpnValid(false)
56         , __isSimTypeValid(false)
57 {
58 }
59
60 _SimInfoImpl::_SimInfoImpl(const _SimInfoImpl& rhs)
61         : __mcc(rhs.__mcc)
62         , __mnc(rhs.__mnc)
63         , __iccId(rhs.__iccId)
64         , __imsi(rhs.__imsi)
65         , __operatorName(rhs.__operatorName)
66         , __phoneNumber(rhs.__phoneNumber)
67         , __spn(rhs.__spn)
68         , __simType(rhs.__simType)
69         , __isAvailable(rhs.__isAvailable)
70         , __isIccIdValid(rhs.__isIccIdValid)
71         , __isMccValid(rhs.__isMccValid)
72         , __isMncValid(rhs.__isMncValid)
73         , __isOperatorNameValid(rhs.__isOperatorNameValid)
74         , __isPhoneNumberValid(rhs.__isPhoneNumberValid)
75         , __isSpnValid(rhs.__isSpnValid)
76         , __isSimTypeValid(rhs.__isSimTypeValid)
77 {
78 }
79
80 _SimInfoImpl::~_SimInfoImpl(void)
81 {
82 }
83
84 result
85 _SimInfoImpl::Construct(void)
86 {
87         TapiHandle* pHandle = null;
88         int err = SIM_ERROR_NONE;
89         sim_state_e simState = SIM_STATE_UNAVAILABLE;
90
91         int mnc = -1;
92         int mcc = -1;
93
94         String spn;
95         String iccId;
96         String operatorName;
97         String phoneNumber;
98
99         char* pTemp = null;
100         char* pTempFullName = null;
101         char* pTempShortName = null;
102
103         // Checking availability
104         err = sim_get_state(&simState);
105         SysLog(NID_TEL, "The return value of sim_get_state() is 0x%x and the simState is %d", err, simState);
106
107         if (err == SIM_ERROR_NONE && simState == SIM_STATE_AVAILABLE)
108         {
109                 __isAvailable = true;
110         }
111         SysTryReturnResult(NID_TEL, __isAvailable, E_DEVICE_UNAVAILABLE, "The operation failed due to a missing SIM card.");
112
113         // Getting mnc
114         err = sim_get_mnc(&pTemp);
115         SysLog(NID_TEL, "The return value of sim_get_mnc() is 0x%x and the mnc value is %s", err, pTemp);
116
117         if (err != SIM_ERROR_NONE)
118         {
119                 SysLog(NID_TEL, "Failed to get the Mobile Network Code (MNC).");
120                 __isMncValid = false;
121         }
122         else
123         {
124                 unique_ptr<char, _CharDeleter> pMnc(pTemp);
125
126                 if(pMnc.get() != null)
127                 {
128                         mnc = atoi(pMnc.get());
129                 }
130                 __isMncValid = true;
131         }
132         __mnc = mnc;
133
134         // Getting mcc
135         err = sim_get_mcc(&pTemp);
136         SysLog(NID_TEL, "The return value of sim_get_mcc() is 0x%x,and the mcc value is %s", err, pTemp);
137
138         if (err != SIM_ERROR_NONE)
139         {
140                 SysLog(NID_TEL, "Failed to get the Mobile Country Code (MCC).");
141                 __isMccValid = false;
142         }
143         else
144         {
145                 unique_ptr<char, _CharDeleter> pMcc(pTemp);
146
147                 if(pMcc.get() != null)
148                 {
149                         mcc = atoi(pMcc.get());
150                 }
151                 __isMccValid = true;
152         }
153         __mcc = mcc;
154
155         // Getting spn
156         err = sim_get_spn(&pTemp);
157         SysLog(NID_TEL, "The return value of sim_get_spn() is 0x%x and the spn value is %s", err, pTemp);
158
159         if (err != SIM_ERROR_NONE)
160         {
161                 SysLog(NID_TEL, "Failed to get the Service Provider Name (SPN).");
162                 __isSpnValid = false;
163         }
164         else
165         {
166                 unique_ptr<char, _CharDeleter> pSpn(pTemp);
167
168                 if(pSpn.get() != null)
169                 {
170                         spn = String(pSpn.get());
171                 }
172                 __isSpnValid = true;
173         }
174
175         __spn = spn;
176
177         // Getting icc id
178         err = sim_get_icc_id(&pTemp);
179         SysLog(NID_TEL, "The return value of sim_get_icc_id() is 0x%x and the iccid value is %s", err, pTemp);
180
181         if (err != SIM_ERROR_NONE)
182         {
183                 SysLog(NID_TEL, "Failed to get the Integrated Circuit Card(ICC) ID.");
184                 __isIccIdValid = false;
185         }
186         else
187         {
188                 unique_ptr<char, _CharDeleter> pIccId(pTemp);
189
190                 if (pIccId.get() != null)
191                 {
192                         iccId = String(pIccId.get());
193                 }
194                 __isIccIdValid = true;
195         }
196         __iccId = iccId;
197
198         // Getting operator name
199         err = sim_get_cphs_operator_name(&pTempFullName, &pTempShortName);
200
201         SysLog(NID_TEL, "The return value of sim_get_cphs_operator_name() is 0x%x. full[%s] short[%s]", err, pTempFullName, pTempShortName);
202         if (err != SIM_ERROR_NONE)
203         {
204                 SysLog(NID_TEL, "Failed to get the operator name.");
205                 __isOperatorNameValid = false;
206         }
207         else
208         {
209                 unique_ptr<char, _CharDeleter> pFullName(pTempFullName);
210                 unique_ptr<char, _CharDeleter> pShortName(pTempShortName);
211
212                 operatorName = String((const char*) pFullName.get());
213                 SysLog(NID_TEL, "The Operator Name is [%ls]", operatorName.GetPointer());
214                 __isOperatorNameValid = true;
215         }
216         __operatorName = operatorName;
217
218         // Getting phone number
219         err = sim_get_subscriber_number(&pTemp);
220         SysLog(NID_TEL, "The return value of sim_get_subscriber_number() is 0x%x and the phone number is [%s]", err, pTemp);
221         if (err != SIM_ERROR_NONE)
222         {
223                 SysLog(NID_TEL, "Failed to get the phone number.");
224                 __isPhoneNumberValid = false;
225         }
226         else
227         {
228                 unique_ptr<char, _CharDeleter> pPhoneNumber(pTemp);
229                 if (pPhoneNumber.get() != null)
230                 {
231                         phoneNumber = String((const char*) pPhoneNumber.get());
232                 }
233                 __isPhoneNumberValid = true;
234         }
235         __phoneNumber = phoneNumber;
236
237         // Getting SIM type
238         pHandle = tel_init(null);
239         SysTryReturnResult(NID_TEL, pHandle != null, E_SYSTEM,
240                         "[%s] A system error has occurred. Failed to initialize TApi library.", GetErrorMessage(E_SYSTEM));
241
242         TelSimCardType_t simType = TAPI_SIM_CARD_TYPE_UNKNOWN;
243         err = tel_get_sim_type(pHandle, &simType);
244         SysLog(NID_TEL, "The return value of tel_get_sim_type() is 0x%x and the SIM type is %d", err, simType);
245         if (err != SIM_ERROR_NONE)
246         {
247                 SysLog(NID_TEL, "Failed to get the SIM type.");
248                 __isSimTypeValid = false;
249         }
250         else
251         {
252                 __isSimTypeValid = true;
253         }
254
255         switch(simType)
256         {
257         case TAPI_SIM_CARD_TYPE_GSM:
258                 __simType = SIM_TYPE_GSM;
259                 break;
260         case TAPI_SIM_CARD_TYPE_USIM:
261                 __simType = SIM_TYPE_USIM;
262                 break;
263         case TAPI_SIM_CARD_TYPE_RUIM:
264                 __simType = SIM_TYPE_RUIM;
265                 break;
266         case TAPI_SIM_CARD_TYPE_IMS:
267                 __simType = SIM_TYPE_ISIM;
268                 break;
269         case TAPI_SIM_CARD_TYPE_UNKNOWN:        // Fall through
270         default:
271                 __simType = SIM_TYPE_UNKNOWN;
272                 break;
273         }
274
275         tel_deinit(pHandle);
276
277         return E_SUCCESS;
278 }
279
280 int
281 _SimInfoImpl::GetMnc(void) const
282 {
283         return __mnc;
284 }
285
286 int
287 _SimInfoImpl::GetMcc(void) const
288 {
289         return __mcc;
290 }
291
292 String
293 _SimInfoImpl::GetSpn(void) const
294 {
295         return __spn;
296 }
297
298 String
299 _SimInfoImpl::GetIccId(void) const
300 {
301         return __iccId;
302 }
303
304 String
305 _SimInfoImpl::GetOperatorName(void) const
306 {
307         return __operatorName;
308 }
309
310 String
311 _SimInfoImpl::GetPhoneNumber(void) const
312 {
313         return __phoneNumber;
314 }
315
316 String
317 _SimInfoImpl::GetImsi(void) const
318 {
319         result r = E_SUCCESS;
320         String imsi;
321
322         SysTryReturn(NID_TEL, __isAvailable, imsi, E_DEVICE_UNAVAILABLE,
323                 "[%s] The operation failed due to a missing SIM card.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
324
325         _TelephonyIpcProxy* pTelephonyServiceProxy = _TelephonyIpcProxy::GetInstance();
326         SysTryReturn(NID_TEL, pTelephonyServiceProxy != null, imsi, E_SYSTEM, "[%s] A system error has occured. IPC instance has not been constructed yet.", GetErrorMessage(E_SYSTEM));
327
328         r = pTelephonyServiceProxy->GetImsi(imsi);
329         SysTryReturn(NID_TEL, r == E_SUCCESS, imsi, r, "[%s] Failed to get the imsi.", GetErrorMessage(r));
330
331         return imsi;
332 }
333
334 SimType
335 _SimInfoImpl::GetSimType(void) const
336 {
337         return __simType;
338 }
339
340 bool
341 _SimInfoImpl::IsAvailable(void) const
342 {
343         return __isAvailable;
344 }
345
346 _SimInfoImpl&
347 _SimInfoImpl::operator =(const _SimInfoImpl& rhs)
348 {
349         if (this != &rhs)
350         {
351                 __mcc = rhs.__mcc;
352                 __mnc = rhs.__mnc;
353                 __iccId = rhs.__iccId;
354                 __imsi = rhs.__imsi;
355                 __operatorName = rhs.__operatorName;
356                 __phoneNumber = rhs.__phoneNumber;
357                 __spn = rhs.__spn;
358                 __simType = rhs.__simType;
359                 __isAvailable = rhs.__isAvailable;
360                 __isIccIdValid = rhs.__isIccIdValid;
361                 __isMccValid = rhs.__isMccValid;
362                 __isMncValid = rhs.__isMncValid;
363                 __isOperatorNameValid = rhs.__isOperatorNameValid;
364                 __isPhoneNumberValid = rhs.__isPhoneNumberValid;
365                 __isSpnValid = rhs.__isSpnValid;
366                 __isSimTypeValid = rhs.__isSimTypeValid;
367         }
368
369         return *this;
370 }
371
372 bool
373 _SimInfoImpl::Equals(const Tizen::Base::Object& rhs) const
374 {
375         const _SimInfoImpl* pRhs = dynamic_cast <const _SimInfoImpl*>(&rhs);
376
377         if (pRhs == null)
378         {
379                 return false;
380         }
381
382         if (__mcc != pRhs->__mcc ||
383                 __mnc != pRhs->__mnc ||
384                 __iccId != pRhs->__iccId ||
385                 __imsi != pRhs->__imsi ||
386                 __operatorName != pRhs->__operatorName ||
387                 __phoneNumber != pRhs->__phoneNumber ||
388                 __spn != pRhs->__spn ||
389                 __simType != pRhs->__simType ||
390                 __isAvailable != pRhs->__isAvailable ||
391                 __isIccIdValid != pRhs->__isIccIdValid ||
392                 __isMccValid != pRhs->__isMccValid ||
393                 __isMncValid != pRhs->__isMncValid ||
394                 __isOperatorNameValid != pRhs->__isOperatorNameValid ||
395                 __isPhoneNumberValid != pRhs->__isPhoneNumberValid ||
396                 __isSpnValid != pRhs->__isSpnValid ||
397                 __isSimTypeValid != pRhs->__isSimTypeValid)
398         {
399                 return false;
400         }
401
402         return true;
403 }
404
405 int
406 _SimInfoImpl::GetHashCode(void) const
407 {
408         int hashCode = _HASH_CODE_INITIAL_VALUE;
409
410         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __mcc;
411         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __mnc;
412         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __iccId.GetHashCode();
413         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __imsi.GetHashCode();
414         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __operatorName.GetHashCode();
415         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __phoneNumber.GetHashCode();
416         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __spn.GetHashCode();
417         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __simType;
418         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + (__isAvailable ? 0 : 1);
419         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + (__isIccIdValid ? 0 : 1);
420         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + (__isMccValid ? 0 : 1);
421         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + (__isMncValid ? 0 : 1);
422         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + (__isOperatorNameValid ? 0 : 1);
423         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + (__isPhoneNumberValid ? 0 : 1);
424         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + (__isSpnValid ? 0 : 1);
425         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + (__isSimTypeValid ? 0 : 1);
426
427         return hashCode;
428 }
429
430 _SimInfoImpl*
431 _SimInfoImpl::GetInstance(SimInfo& simInfo)
432 {
433         return simInfo.__pSimInfoImpl;
434 }
435
436 const _SimInfoImpl*
437 _SimInfoImpl::GetInstance(const SimInfo& simInfo)
438 {
439         return simInfo.__pSimInfoImpl;
440 }
441
442 }} // Tizen::Telephony