add2fe85fed7f30d9e0300fe612103cfd4f9be88
[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 using namespace std;
35 using namespace Tizen::App;
36 using namespace Tizen::Base;
37
38 namespace Tizen { namespace Telephony
39 {
40
41
42 _SimInfoImpl::_SimInfoImpl(void)
43 : __pTelephonyServiceProxy(null)
44 {
45
46 }
47
48 _SimInfoImpl::~_SimInfoImpl(void)
49 {
50 }
51
52 result
53 _SimInfoImpl::Construct(void)
54 {
55         SysTryReturnResult(NID_TEL, IsAvailable(), E_DEVICE_UNAVAILABLE, "The operation failed due to a missing SIM card.");
56
57         __pTelephonyServiceProxy = _TelephonyIpcProxy::GetInstance();
58
59         if (__pTelephonyServiceProxy == null)
60         {
61             SysLog(NID_TEL, "Creating an IPC instance to connect with the Connectivity service daemon has failed.");
62         }
63
64         return E_SUCCESS;
65 }
66
67
68 int
69 _SimInfoImpl::GetMnc(void) const
70 {
71         int mnc = -1;
72
73         SysTryReturn(NID_TEL, IsAvailable(), mnc, E_DEVICE_UNAVAILABLE,
74                                         "[%s] The operation failed due to a missing SIM card.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
75
76         int err = SIM_ERROR_NONE;
77         char* pTemp = null;
78
79         err = sim_get_mnc(&pTemp);
80
81         SysLog(NID_TEL, "The return value of sim_get_mnc() is 0x%x and the mnc value is %s", err, pTemp);
82         SysTryReturn(NID_TEL, err == SIM_ERROR_NONE, mnc, E_SYSTEM, "[%s] A system error has occured. Failed to get the Mobile Network Code (MNC).", GetErrorMessage(E_SYSTEM));
83         unique_ptr<char, _CharDeleter> pMnc(pTemp);
84
85         if(pMnc.get() != null)
86         {
87                 mnc = atoi(pMnc.get());
88         }
89         return mnc;
90 }
91
92 int
93 _SimInfoImpl::GetMcc(void) const
94 {
95         int mcc = -1;
96
97         SysTryReturn(NID_TEL, IsAvailable(), mcc, E_DEVICE_UNAVAILABLE,
98                                         "[%s] The operation failed due to a missing SIM card.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
99
100         int err = SIM_ERROR_NONE;
101         char* pTemp = null;
102
103         err = sim_get_mcc(&pTemp);
104         
105         SysLog(NID_TEL, "The return value of sim_get_mcc() is 0x%x,and the mcc value is %s", err, pTemp);
106         SysTryReturn(NID_TEL, err == SIM_ERROR_NONE, mcc, E_SYSTEM, "[%s] A system error has occured. Failed to get the Mobile Country Code (MCC).", GetErrorMessage(E_SYSTEM));
107         unique_ptr<char, _CharDeleter> pMcc(pTemp);
108
109         if(pMcc.get() != null)
110         {
111                 mcc = atoi(pMcc.get());
112         }
113         return mcc;
114 }
115
116 String
117 _SimInfoImpl::GetSpn(void) const
118 {
119         String spn;
120
121         SysTryReturn(NID_TEL, IsAvailable(), spn, E_DEVICE_UNAVAILABLE,
122                     "[%s] The operation failed due to a missing SIM card.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
123
124         int err = SIM_ERROR_NONE;
125         
126         char* pTemp = null;
127
128         err = sim_get_spn(&pTemp);
129         SysLog(NID_TEL, "The return value of sim_get_spn() is 0x%x and the spn value is %s", err, pTemp);
130         SysTryReturn(NID_TEL, err == SIM_ERROR_NONE, spn, E_SYSTEM, "[%s] A system error has occured. Failed to get the Service Provider Name (SPN).", GetErrorMessage(E_SYSTEM));
131         unique_ptr<char, _CharDeleter> pSpn(pTemp);
132
133         if(pSpn.get() != null)
134         {
135                 spn = String(pSpn.get());
136         }
137         return spn;
138 }
139
140 String
141 _SimInfoImpl::GetIccId(void) const
142 {
143         String iccId;
144
145         SysTryReturn(NID_TEL, IsAvailable(), iccId, E_DEVICE_UNAVAILABLE,
146                                         "[%s] The operation failed due to a missing SIM card.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
147
148         int err = SIM_ERROR_NONE;
149         char* pTemp = null;
150
151         err = sim_get_icc_id(&pTemp);
152         SysLog(NID_TEL, "The return value of sim_get_icc_id() is 0x%x and the iccid valuce is %s", err, pTemp);
153         SysTryReturn(NID_TEL, err == SIM_ERROR_NONE, iccId, E_SYSTEM, "[%s] A system error has occured. Failed to get the Integrated Circuit Card(ICC) ID.", GetErrorMessage(E_SYSTEM));
154
155         unique_ptr<char, _CharDeleter> pIccId(pTemp);
156
157         if (pIccId.get() != null)
158         {
159                 iccId = String(pIccId.get());
160         }
161
162         return iccId;
163 }
164
165 String
166 _SimInfoImpl::GetOperatorName(void) const
167 {
168         String operatorName;
169
170         SysTryReturn(NID_TEL, IsAvailable(), operatorName, E_DEVICE_UNAVAILABLE,
171                                         "[%s] The operation failed due to a missing SIM card.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
172
173         int err = SIM_ERROR_NONE;
174
175         char* pTempFullName = null;
176         char* pTempShortName = null;
177
178         err = sim_get_cphs_operator_name(&pTempFullName, &pTempShortName);
179
180         SysLog(NID_TEL, "The return value of sim_get_cphs_operator_name() is 0x%x", err);
181         SysTryReturn(NID_TEL, err == SIM_ERROR_NONE, operatorName, E_SYSTEM, "[%s] A system error has occured. Failed to get the operator name.", GetErrorMessage(E_SYSTEM));
182
183         unique_ptr<char, _CharDeleter> pFullName(pTempFullName);
184         unique_ptr<char, _CharDeleter> pShortName(pTempShortName);
185
186         operatorName = String((const char*) pFullName.get());
187         SysLog(NID_TEL, "The Operator Name is %ls", operatorName.GetPointer());
188
189         return operatorName;
190 }
191
192 String
193 _SimInfoImpl::GetPhoneNumber(void) const
194 {
195         String phoneNumber;
196
197         SysTryReturn(NID_TEL, IsAvailable(), phoneNumber, E_DEVICE_UNAVAILABLE,
198                                 "[%s] The operation failed due to a missing SIM card.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
199
200         int err = SIM_ERROR_NONE;
201         char* pTemp = null;
202
203         err = sim_get_subscriber_number(&pTemp);
204         SysLog(NID_TEL, "The return value of sim_get_subscriber_number() is 0x%x and the phone number is %s", err, pTemp);
205         SysTryReturn(NID_TEL, err == SIM_ERROR_NONE, phoneNumber, E_SYSTEM, "[%s] A system error has occured. Failed to get the phone number.", GetErrorMessage(E_SYSTEM));
206         
207         unique_ptr<char, _CharDeleter> pPhoneNumber(pTemp);
208         if (pPhoneNumber.get() != null)
209         {
210                 phoneNumber = String((const char*) pPhoneNumber.get());
211         }
212         return phoneNumber;
213 }
214
215 String
216 _SimInfoImpl::GetImsi(void) const
217 {
218         result r = E_SUCCESS;
219         String imsi;
220
221         SysTryReturn(NID_TEL, IsAvailable(), imsi, E_DEVICE_UNAVAILABLE,
222                 "[%s] The operation failed due to a missing SIM card.", GetErrorMessage(E_DEVICE_UNAVAILABLE));
223
224         SysTryReturn(NID_TEL, __pTelephonyServiceProxy != null, imsi, E_SYSTEM, "[%s] A system error has occured. IPC instance has not been constructed yet.", GetErrorMessage(E_SYSTEM));
225
226         r = __pTelephonyServiceProxy->GetImsi(imsi);
227         SysTryReturn(NID_TEL, r == E_SUCCESS, imsi, r, "[%s] Failed to get the imsi.", GetErrorMessage(r));
228
229         return imsi;
230 }
231
232 bool
233 _SimInfoImpl::IsAvailable(void) const
234 {
235         bool isAvailable = false;
236         int err = SIM_ERROR_NONE;
237         sim_state_e simState = SIM_STATE_UNKNOWN;
238
239         err = sim_get_state(&simState);
240         SysLog(NID_TEL, "The return value of sim_get_state() is 0x%x and the simState is %d", err, simState);
241
242         if (err == SIM_ERROR_NONE && simState == SIM_STATE_AVAILABLE)
243         {
244                 isAvailable = true;
245         }
246
247         return isAvailable;
248 }
249
250 _SimInfoImpl*
251 _SimInfoImpl::GetInstance(SimInfo& simInfo)
252 {
253         return simInfo.__pSimInfoImpl;
254 }
255
256 const _SimInfoImpl*
257 _SimInfoImpl::GetInstance(const SimInfo& simInfo)
258 {
259         return simInfo.__pSimInfoImpl;
260 }
261
262 }} // Tizen::Telephony