tizen 2.3 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Systeminfo / SystemInfoSIM.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 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 #include <string>
19 #include <Logger.h>
20 #include <PlatformException.h>
21
22 #include "SystemInfoUtil.h"
23 #include "SystemInfoSIM.h"
24 #include "SystemInfo.h"
25
26 using namespace DeviceAPI::Common;
27
28 namespace DeviceAPI {
29 namespace SystemInfo {
30
31 namespace {
32 const char* SIM_STATUS_ABSENT = "ABSENT";
33 const char* SIM_STATUS_INITIALIZING = "INITIALIZING";
34 const char* SIM_STATUS_READY = "READY";
35 const char* SIM_STATUS_PIN_REQUIRED = "PIN_REQUIRED";
36 const char* SIM_STATUS_PUK_REQUIRED = "PUK_REQUIRED";
37 const char* SIM_STATUS_SIM_LOCKED = "SIM_LOCKED";
38 const char* SIM_STATUS_NETWORK_LOCKED = "NETWORK_LOCKED";
39 const char* SIM_STATUS_UNKNOWN = "UNKNOWN";
40 }
41
42 SystemInfoSIM::SystemInfoSIM(TapiHandle *tapi_handle) :
43     m_ready(false),
44     m_properties_to_process(0),
45     m_mcc(0),
46     m_mnc(0),
47     m_state(""),
48     m_msisdn(""),
49     m_iccid(""),
50     m_msin(""),
51     m_spn("")
52 {
53     LOGD("Entered");
54     if (NULL == tapi_handle) {
55         LOGE("Tapi handle is null");
56         m_ready = true;
57         m_state = SIM_STATUS_UNKNOWN;
58     } else {
59         int card_changed = 0;
60         TelSimCardStatus_t sim_card_state;
61         int ret = tel_get_sim_init_info(tapi_handle, &sim_card_state, &card_changed);
62         if (ret == TAPI_API_SUCCESS) {
63             switch (sim_card_state) {
64                 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
65                 case TAPI_SIM_STATUS_CARD_REMOVED:
66                     m_state = SIM_STATUS_ABSENT;
67                     break;
68                 case TAPI_SIM_STATUS_SIM_INITIALIZING:
69                     m_state = SIM_STATUS_INITIALIZING;
70                     break;
71                 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
72                     m_state = SIM_STATUS_READY;
73                     break;
74                 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
75                     m_state = SIM_STATUS_PIN_REQUIRED;
76                     break;
77                 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
78                     m_state = SIM_STATUS_PUK_REQUIRED;
79                     break;
80                 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
81                 case TAPI_SIM_STATUS_CARD_BLOCKED:
82                     m_state = SIM_STATUS_SIM_LOCKED;
83                     break;
84                 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
85                 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
86                     m_state = SIM_STATUS_NETWORK_LOCKED;
87                     break;
88                 default:
89                     m_state = SIM_STATUS_UNKNOWN;
90                     break;
91             }
92             LOGD("state : %s", m_state.c_str());
93             if (SIM_STATUS_READY != m_state) {
94                 //sim is not ready, we can't get other properties
95                 //set property ready
96                 m_ready = true;
97             } else {
98                 TelSimImsiInfo_t imsi;
99                 ret = tel_get_sim_imsi(tapi_handle, &imsi);
100                 if (ret == TAPI_API_SUCCESS) {
101                     LOGD("mcc: %s, mnc: %s, msin: %s", imsi.szMcc, imsi.szMnc, imsi.szMsin);
102                     m_mcc = std::stoul(imsi.szMcc);
103                     m_mnc = std::stoul(imsi.szMnc);
104                     m_msin = imsi.szMsin;
105                 } else {
106                     std::string log_msg = "Failed to get sim imsi";
107                     LOGE("$s, %d, %s", log_msg.c_str(), ret,
108                             SystemInfoUtil::getTapiErrorMessage(ret).c_str());
109                     SystemInfoUtil::throwTapiException(ret, log_msg);
110                 }
111
112                 ret = tel_get_sim_msisdn(tapi_handle, msisdnValueCallback, this);
113                 if (ret == TAPI_API_SUCCESS) {
114                     ++m_properties_to_process;
115                 } else {
116                     std::string log_msg = "Failed to get msisdn info";
117                     LOGE("$s, %d, %s", log_msg.c_str(), ret,
118                             SystemInfoUtil::getTapiErrorMessage(ret).c_str());
119                     SystemInfoUtil::throwTapiException(ret, log_msg);
120                 }
121
122                 ret = tel_get_sim_iccid(tapi_handle, iccidValueCallback, this);
123                 if (ret == TAPI_API_SUCCESS) {
124                     ++m_properties_to_process;
125                 } else {
126                     std::string log_msg = "Failed to get iccid info";
127                     LOGE("$s, %d, %s", log_msg.c_str(), ret,
128                             SystemInfoUtil::getTapiErrorMessage(ret).c_str());
129                     SystemInfoUtil::throwTapiException(ret, log_msg);
130                 }
131
132                 ret = tel_get_sim_spn(tapi_handle, spnValueCallback, this);
133                 if (ret == TAPI_API_SUCCESS) {
134                     ++m_properties_to_process;
135                 } else {
136                     std::string log_msg = "Failed to get spn info";
137                     LOGE("$s, %d, %s", log_msg.c_str(), ret,
138                             SystemInfoUtil::getTapiErrorMessage(ret).c_str());
139                     SystemInfoUtil::throwTapiException(ret, log_msg);
140                 }
141
142                 ret = tel_get_sim_cphs_netname(tapi_handle, cphsValueCallback, this);
143                 if (ret == TAPI_API_SUCCESS) {
144                     ++m_properties_to_process;
145                 } else {
146                     std::string log_msg = "Failed to get cphs info";
147                     LOGE("$s, %d, %s", log_msg.c_str(), ret,
148                             SystemInfoUtil::getTapiErrorMessage(ret).c_str());
149                     SystemInfoUtil::throwTapiException(ret, log_msg);
150                 }
151             }
152         } else {
153             std::string log_msg = "Failed to initialize sim info";
154             LOGE("$s, %d, %s", log_msg.c_str(), ret,
155                     SystemInfoUtil::getTapiErrorMessage(ret).c_str());
156             SystemInfoUtil::throwTapiException(ret, log_msg);
157         }
158     }
159
160 }
161
162 SystemInfoSIM::~SystemInfoSIM()
163 {
164     LOGD("Entered");
165 }
166
167 bool SystemInfoSIM::isReady() const
168 {
169     return m_ready;
170 }
171
172 std::string SystemInfoSIM::getState() const
173 {
174     return m_state;
175 }
176
177 std::string SystemInfoSIM::getOperatorName() const
178 {
179     return m_operator_name;
180 }
181
182 std::string SystemInfoSIM::getMsisdn() const
183 {
184     return m_msisdn;
185 }
186
187 std::string SystemInfoSIM::getIccid() const
188 {
189     return m_iccid;
190 }
191
192 unsigned short SystemInfoSIM::getMcc() const
193 {
194     return m_mcc;;
195 }
196
197 unsigned short SystemInfoSIM::getMnc() const
198 {
199     return m_mnc;
200 }
201
202 std::string SystemInfoSIM::getMsin() const
203 {
204     return m_msin;
205 }
206
207 std::string SystemInfoSIM::getSpn() const
208 {
209     return m_spn;
210 }
211
212 void SystemInfoSIM::checkIfReady()
213 {
214     --m_properties_to_process;
215     if (0 == m_properties_to_process) {
216         m_ready = true;
217         SystemInfo::getInstance().notifyGetPropertyValueReady();
218     }
219 }
220
221 void SystemInfoSIM::cphsValueCallback(TapiHandle *handle, int result, void *data, void *user_data)
222 {
223     LOGD("Entered");
224
225     TelSimAccessResult_t access_rt = static_cast<TelSimAccessResult_t>(result);
226     TelSimCphsNetName_t *cphs_info = static_cast<TelSimCphsNetName_t*>(data);
227
228     SystemInfoSIM* property = static_cast<SystemInfoSIM*>(user_data);
229     if (NULL == property) {
230         LOGE("property is NULL");
231         return;
232     }
233
234     if (access_rt == TAPI_SIM_ACCESS_SUCCESS) {
235         std::stringstream s;
236         s << cphs_info->full_name;
237         if (s.str().empty()) {
238             s << cphs_info->short_name;
239         }
240         property->m_operator_name = s.str();
241         LOGD("Operator name: %s", property->m_operator_name.c_str());
242     } else {
243         LOGW("Failed to retrieve cphs_info: %d", access_rt);
244     }
245     property->checkIfReady();
246 }
247
248 void SystemInfoSIM::msisdnValueCallback(TapiHandle *handle, int result, void *data, void *user_data)
249 {
250     LOGD("Entered");
251
252     TelSimAccessResult_t access_rt = static_cast<TelSimAccessResult_t>(result);
253     TelSimMsisdnList_t *msisdn_info = static_cast<TelSimMsisdnList_t*>(data);
254
255     SystemInfoSIM* property = static_cast<SystemInfoSIM*>(user_data);
256     if (NULL == property) {
257         LOGE("property is NULL");
258         return;
259     }
260
261     if (access_rt == TAPI_SIM_ACCESS_SUCCESS) {
262         if (msisdn_info->count > 0) {
263             if ('\0' != msisdn_info->list[0].num[0]) {
264                 property->m_msisdn = msisdn_info->list[0].num;
265                 LOGD("MSISDN number: %s", property->m_msisdn.c_str());
266             } else {
267                 LOGW("MSISDN number empty");
268             }
269         } else {
270             LOGW("msisdn_info list empty");
271         }
272     } else {
273         LOGW("Failed to retrieve msisdn: %d", access_rt);
274     }
275     property->checkIfReady();
276 }
277
278 void SystemInfoSIM::iccidValueCallback(TapiHandle *handle, int result, void *data, void *user_data)
279 {
280     LOGD("Entered");
281
282     TelSimAccessResult_t access_rt = static_cast<TelSimAccessResult_t>(result);
283     TelSimIccIdInfo_t *iccid_info = static_cast<TelSimIccIdInfo_t*>(data);
284
285     SystemInfoSIM* property = static_cast<SystemInfoSIM*>(user_data);
286     if (NULL == property) {
287         LOGE("property is NULL");
288         return;
289     }
290
291     if (access_rt == TAPI_SIM_ACCESS_SUCCESS) {
292         if (iccid_info->icc_length > 0) {
293             property->m_iccid = iccid_info->icc_num;
294             LOGD("iccid value: %s", property->m_iccid.c_str());
295         } else {
296             LOGW("iccid_info is empty");
297         }
298     } else {
299         LOGW("Failed to retrieve iccid: %d", access_rt);
300     }
301     property->checkIfReady();
302 }
303
304 void SystemInfoSIM::spnValueCallback(TapiHandle *handle, int result, void *data, void *user_data)
305 {
306     LOGD("Entered");
307
308     TelSimAccessResult_t access_rt = static_cast<TelSimAccessResult_t>(result);
309     TelSimSpn_t *spn_info = static_cast<TelSimSpn_t*>(data);
310
311     SystemInfoSIM* property = static_cast<SystemInfoSIM*>(user_data);
312     if (NULL == property) {
313         LOGE("property is NULL");
314         return;
315     }
316
317     if (access_rt == TAPI_SIM_ACCESS_SUCCESS) {
318         std::stringstream s;
319         s << spn_info->spn;
320         property->m_spn = s.str();
321         LOGD("spn value: %s", property->m_spn.c_str());
322     } else {
323         LOGW("Failed to retrieve spn: %d", access_rt);
324     }
325     property->checkIfReady();
326 }
327
328 } // SystemInfo
329 } // DeviceAPI