2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <PlatformException.h>
22 #include "SystemInfoUtil.h"
23 #include "SystemInfoSIM.h"
24 #include "SystemInfo.h"
26 using namespace DeviceAPI::Common;
29 namespace SystemInfo {
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";
42 SystemInfoSIM::SystemInfoSIM(TapiHandle *tapi_handle) :
44 m_properties_to_process(0),
54 if (NULL == tapi_handle) {
55 LOGE("Tapi handle is null");
57 m_state = SIM_STATUS_UNKNOWN;
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;
68 case TAPI_SIM_STATUS_SIM_INITIALIZING:
69 m_state = SIM_STATUS_INITIALIZING;
71 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
72 m_state = SIM_STATUS_READY;
74 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
75 m_state = SIM_STATUS_PIN_REQUIRED;
77 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
78 m_state = SIM_STATUS_PUK_REQUIRED;
80 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
81 case TAPI_SIM_STATUS_CARD_BLOCKED:
82 m_state = SIM_STATUS_SIM_LOCKED;
84 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
85 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
86 m_state = SIM_STATUS_NETWORK_LOCKED;
89 m_state = SIM_STATUS_UNKNOWN;
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
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;
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);
112 ret = tel_get_sim_msisdn(tapi_handle, msisdnValueCallback, this);
113 if (ret == TAPI_API_SUCCESS) {
114 ++m_properties_to_process;
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);
122 ret = tel_get_sim_iccid(tapi_handle, iccidValueCallback, this);
123 if (ret == TAPI_API_SUCCESS) {
124 ++m_properties_to_process;
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);
132 ret = tel_get_sim_spn(tapi_handle, spnValueCallback, this);
133 if (ret == TAPI_API_SUCCESS) {
134 ++m_properties_to_process;
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);
142 ret = tel_get_sim_cphs_netname(tapi_handle, cphsValueCallback, this);
143 if (ret == TAPI_API_SUCCESS) {
144 ++m_properties_to_process;
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);
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);
162 SystemInfoSIM::~SystemInfoSIM()
167 bool SystemInfoSIM::isReady() const
172 std::string SystemInfoSIM::getState() const
177 std::string SystemInfoSIM::getOperatorName() const
179 return m_operator_name;
182 std::string SystemInfoSIM::getMsisdn() const
187 std::string SystemInfoSIM::getIccid() const
192 unsigned short SystemInfoSIM::getMcc() const
197 unsigned short SystemInfoSIM::getMnc() const
202 std::string SystemInfoSIM::getMsin() const
207 std::string SystemInfoSIM::getSpn() const
212 void SystemInfoSIM::checkIfReady()
214 --m_properties_to_process;
215 if (0 == m_properties_to_process) {
217 SystemInfo::getInstance().notifyGetPropertyValueReady();
221 void SystemInfoSIM::cphsValueCallback(TapiHandle *handle, int result, void *data, void *user_data)
225 TelSimAccessResult_t access_rt = static_cast<TelSimAccessResult_t>(result);
226 TelSimCphsNetName_t *cphs_info = static_cast<TelSimCphsNetName_t*>(data);
228 SystemInfoSIM* property = static_cast<SystemInfoSIM*>(user_data);
229 if (NULL == property) {
230 LOGE("property is NULL");
234 if (access_rt == TAPI_SIM_ACCESS_SUCCESS) {
236 s << cphs_info->full_name;
237 if (s.str().empty()) {
238 s << cphs_info->short_name;
240 property->m_operator_name = s.str();
241 LOGD("Operator name: %s", property->m_operator_name.c_str());
243 LOGW("Failed to retrieve cphs_info: %d", access_rt);
245 property->checkIfReady();
248 void SystemInfoSIM::msisdnValueCallback(TapiHandle *handle, int result, void *data, void *user_data)
252 TelSimAccessResult_t access_rt = static_cast<TelSimAccessResult_t>(result);
253 TelSimMsisdnList_t *msisdn_info = static_cast<TelSimMsisdnList_t*>(data);
255 SystemInfoSIM* property = static_cast<SystemInfoSIM*>(user_data);
256 if (NULL == property) {
257 LOGE("property is NULL");
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());
267 LOGW("MSISDN number empty");
270 LOGW("msisdn_info list empty");
273 LOGW("Failed to retrieve msisdn: %d", access_rt);
275 property->checkIfReady();
278 void SystemInfoSIM::iccidValueCallback(TapiHandle *handle, int result, void *data, void *user_data)
282 TelSimAccessResult_t access_rt = static_cast<TelSimAccessResult_t>(result);
283 TelSimIccIdInfo_t *iccid_info = static_cast<TelSimIccIdInfo_t*>(data);
285 SystemInfoSIM* property = static_cast<SystemInfoSIM*>(user_data);
286 if (NULL == property) {
287 LOGE("property is NULL");
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());
296 LOGW("iccid_info is empty");
299 LOGW("Failed to retrieve iccid: %d", access_rt);
301 property->checkIfReady();
304 void SystemInfoSIM::spnValueCallback(TapiHandle *handle, int result, void *data, void *user_data)
308 TelSimAccessResult_t access_rt = static_cast<TelSimAccessResult_t>(result);
309 TelSimSpn_t *spn_info = static_cast<TelSimSpn_t*>(data);
311 SystemInfoSIM* property = static_cast<SystemInfoSIM*>(user_data);
312 if (NULL == property) {
313 LOGE("property is NULL");
317 if (access_rt == TAPI_SIM_ACCESS_SUCCESS) {
320 property->m_spn = s.str();
321 LOGD("spn value: %s", property->m_spn.c_str());
323 LOGW("Failed to retrieve spn: %d", access_rt);
325 property->checkIfReady();