2 // Open Service Platform
3 // Copyright (c) 2012-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.
18 * @file FTel_SimStateManagerImpl.cpp
19 * @brief This is the implementation file for _SimStateManagerImpl class.
22 // Todo: Remove unnecessary headers
25 #include <tapi_event.h>
26 #include <tapi_common.h>
30 #include <TapiUtility.h>
32 #include <unique_ptr.h>
33 #include <FTelITelephonySimEventListener.h>
34 #include <FTelISimStateManagerGetPinLockSettingResultListener.h>
35 #include <FTelSimStateManager.h>
36 #include <FBaseUtilStringUtil.h>
37 #include <FBaseString.h>
38 #include <FBaseResult.h>
39 #include <FBaseSysLog.h>
41 #include "FTel_SimInfoImpl.h"
42 #include "FTel_SimStateManagerImpl.h"
44 #include "FTel_SimEvent.h"
45 #include "FTel_SimEventArg.h"
46 #include "FTel_SimManagerEvent.h"
47 #include "FTel_SimManagerEventArg.h"
48 #include "FTel_TelephonyIpcProxy.h"
49 #include "FTel_TelephonyUtility.h"
52 using namespace Tizen::System;
53 using namespace Tizen::App;
54 using namespace Tizen::Base;
55 using namespace Tizen::Base::Utility;
56 using namespace Tizen::Base::Collection;
59 namespace Tizen { namespace Telephony
62 _SimStateManagerImpl::_SimStateManagerImpl(void)
64 , __simState(SIM_STATE_UNKNOWN)
65 , __isInProgress(false)
67 , __pSimManagerEvent(null)
68 , __pSimEventListener(null)
69 , __pSimStateManagerGetPinLockSettingResultListener(null)
73 _SimStateManagerImpl::~_SimStateManagerImpl(void)
75 if (__pHandle != null)
77 tel_deregister_noti_event(__pHandle, TAPI_NOTI_SIM_STATUS);
78 tel_deinit(__pHandle);
83 _SimStateManagerImpl::Construct()
86 int err = TAPI_API_SUCCESS;
87 TelSimCardStatus_t initStatus = TAPI_SIM_STATUS_UNKNOWN;
90 SysAssertf(__pHandle == null,
91 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
93 __pHandle = tel_init(null);
94 SysTryReturnResult(NID_TEL, __pHandle != null, E_SYSTEM,
95 "[%s] A system error has occurred. Failed to initialize TApi library.", GetErrorMessage(E_SYSTEM));
97 err = tel_register_noti_event(__pHandle, TAPI_NOTI_SIM_STATUS, OnSimStateChangedCallback, this);
98 SysTryCatch(NID_TEL, err == TAPI_API_SUCCESS, r = E_SYSTEM, E_SYSTEM,
99 "[%s] A system error has occurred. Failed to register the callback function.", GetErrorMessage(E_SYSTEM));
101 err = tel_get_sim_init_info(__pHandle, &initStatus, &cardChanged);
102 SysLog(NID_TEL, "Initial state of SIM card is [%d:%d]", initStatus, cardChanged);
103 SysTryCatch(NID_TEL, err == TAPI_API_SUCCESS, r = E_SYSTEM, E_SYSTEM,
104 "[%s] A system error has occurred. Failed to get initial state of SIM.", GetErrorMessage(E_SYSTEM));
108 case TAPI_SIM_STATUS_CARD_NOT_PRESENT: // fall through
109 case TAPI_SIM_STATUS_CARD_REMOVED:
110 __simState = SIM_STATE_ABSENT;
113 case TAPI_SIM_STATUS_SIM_INITIALIZING:
114 __simState = SIM_STATE_INITIALIZING;
117 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
118 __simState = SIM_STATE_READY;
121 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
122 __simState = SIM_STATE_PIN_REQUIRED;
125 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
126 __simState = SIM_STATE_PUK_REQUIRED;
129 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED: // fall through
130 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED: // fall through
131 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED: // fall through
132 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
133 __simState = SIM_STATE_NETWORK_LOCKED;
136 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
137 __simState = SIM_STATE_SIM_LOCKED;
140 case TAPI_SIM_STATUS_CARD_ERROR: // fall through
141 case TAPI_SIM_STATUS_CARD_BLOCKED: // fall through
142 case TAPI_SIM_STATUS_UNKNOWN: // fall through
144 __simState = SIM_STATE_UNKNOWN;
151 if (__pHandle != null)
153 tel_deinit(__pHandle);
161 _SimStateManagerImpl::OnSimStateChangedCallback(TapiHandle* pHandle, const char* pNotiId, void* pData, void* pUserData)
163 TelSimCardStatus_t* status = (TelSimCardStatus_t*)pData;
164 SysLog(NID_TEL, "TelSimCardStatus: [%d]", *status);
166 _SimStateManagerImpl* pSimStateManagerImpl = static_cast<_SimStateManagerImpl*>(pUserData);
168 SimState state = SIM_STATE_UNKNOWN;
172 case TAPI_SIM_STATUS_CARD_NOT_PRESENT: // fall through
173 case TAPI_SIM_STATUS_CARD_REMOVED:
174 state = SIM_STATE_ABSENT;
177 case TAPI_SIM_STATUS_SIM_INITIALIZING:
178 state = SIM_STATE_INITIALIZING;
181 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
182 state = SIM_STATE_READY;
185 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
186 state = SIM_STATE_PIN_REQUIRED;
189 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
190 state = SIM_STATE_PUK_REQUIRED;
193 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED: // fall through
194 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED: // fall through
195 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED: // fall through
196 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
197 state = SIM_STATE_NETWORK_LOCKED;
200 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
201 state = SIM_STATE_SIM_LOCKED;
204 case TAPI_SIM_STATUS_CARD_ERROR: // fall through
205 case TAPI_SIM_STATUS_CARD_BLOCKED: // fall through
206 case TAPI_SIM_STATUS_UNKNOWN: // fall through
208 state = SIM_STATE_UNKNOWN;
212 if (pSimStateManagerImpl->__simState != state)
214 pSimStateManagerImpl->__simState = state;
216 if (pSimStateManagerImpl->__pSimEvent != null)
219 _SimEventArg* pEventArg = new (std::nothrow)_SimEventArg(_SIM_EVENT_SIM_STATE_CHANGED, state);
220 SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
222 (void)pSimStateManagerImpl->__pSimEvent->Fire(*pEventArg);
228 _SimStateManagerImpl::OnGetPinLockSettingCallback(TapiHandle* pHandle, int ret, void* pData, void* pUserData)
230 result r = E_SUCCESS;
231 bool isEnabled = false;
233 TelSimPinOperationResult_t err = (TelSimPinOperationResult_t)ret;
234 SysLog(NID_TEL, "The result of PIN operation is [%d]", err);
236 if (err != TAPI_SIM_PIN_OPERATION_SUCCESS)
242 TelSimFacilityInfo_t* pFacilityInfo = (TelSimFacilityInfo_t*)pData;
244 TelSimLockType_t lockType = pFacilityInfo->type;
245 TelSimFacilityStatus_t facilityStatus = pFacilityInfo->f_status;
246 SysLog(NID_TEL, "Lock type is [%d], and facility status is [%d].", lockType, facilityStatus);
248 if (lockType == TAPI_SIM_LOCK_SC && facilityStatus == TAPI_SIM_FACILITY_DISABLED)
252 else if (lockType == TAPI_SIM_LOCK_SC && facilityStatus == TAPI_SIM_FACILITY_ENABLED)
256 else if (lockType != TAPI_SIM_LOCK_SC || facilityStatus == TAPI_SIM_FACILITY_UNKNOWN)
263 _SimStateManagerImpl* pSimStateManagerImpl = static_cast<_SimStateManagerImpl*>(pUserData);
264 pSimStateManagerImpl->__isInProgress = false;
267 if (pSimStateManagerImpl->__pSimManagerEvent != null)
269 _SimManagerEventArg* pEventArg = new (std::nothrow)_SimManagerEventArg(_SIM_MANAGER_EVENT_PIN_LOCK_SETTING_RESULT_RECEIVED, isEnabled, r);
270 SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
272 (void)pSimStateManagerImpl->__pSimManagerEvent->Fire(*pEventArg);
277 _SimStateManagerImpl::SetSimEventListener(ITelephonySimEventListener *pListener)
279 result r = E_SUCCESS;
281 if (__pSimEventListener != null)
283 __pSimEvent->RemoveListener(*__pSimEventListener);
284 __pSimEventListener = null;
287 if (__pSimEvent == null && pListener != null)
289 std::unique_ptr<_SimEvent> pSimEvent(new (std::nothrow) _SimEvent());
290 SysTryReturnResult(NID_TEL, pSimEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
292 r = pSimEvent->Construct();
293 SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
295 __pSimEvent = move(pSimEvent);
298 if (pListener != null)
300 r = __pSimEvent->AddListener(*pListener, true);
304 __pSimEventListener = pListener;
310 __pSimEvent.reset(null);
311 SysLogException(NID_TEL, r, "[%s] Propagating.", GetErrorMessage(r));
318 _SimStateManagerImpl::SetSimStateManagerGetPinLockSettingResultListener(ISimStateManagerGetPinLockSettingResultListener *pListener)
320 result r = E_SUCCESS;
322 if (__pSimStateManagerGetPinLockSettingResultListener != null)
324 __pSimManagerEvent->RemoveListener(*__pSimStateManagerGetPinLockSettingResultListener);
325 __pSimStateManagerGetPinLockSettingResultListener = null;
328 if (__pSimManagerEvent == null && pListener != null)
330 std::unique_ptr<_SimManagerEvent> pSimManagerEvent(new (std::nothrow) _SimManagerEvent());
331 SysTryReturnResult(NID_TEL, pSimManagerEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
333 r = pSimManagerEvent->Construct();
334 SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
336 __pSimManagerEvent = move(pSimManagerEvent);
339 if (pListener != null)
341 r = __pSimManagerEvent->AddListener(*pListener, true);
344 __pSimStateManagerGetPinLockSettingResultListener = pListener;
350 __pSimManagerEvent.reset(null);
351 SysLogException(NID_TEL, r, "[%s] Propagating.", GetErrorMessage(r));
358 _SimStateManagerImpl::GetSimState(void) const
366 _SimStateManagerImpl::GetPinLockSetting(ISimStateManagerGetPinLockSettingResultListener* pListener)
368 result r = E_SUCCESS;
371 SysTryReturnResult(NID_TEL, pListener != null, E_INVALID_ARG, "The specified input parameter is invalid.");
372 SysTryReturnResult(NID_TEL, !__isInProgress, E_IN_PROGRESS, "The previous request is in progress.");
374 r = SetSimStateManagerGetPinLockSettingResultListener(pListener);
375 SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to set listener.");
377 ret = tel_get_sim_facility(__pHandle, TAPI_SIM_LOCK_SC, OnGetPinLockSettingCallback, this);
379 SysTryReturnResult(NID_TEL, ret != TAPI_API_SIM_NOT_FOUND, E_DEVICE_UNAVAILABLE,
380 "The operation failed due to a missing SIM card.");
382 SysTryReturnResult(NID_TEL, ret == TAPI_API_SUCCESS || ret == TAPI_API_SIM_NOT_FOUND, E_SYSTEM,
383 "A system error has occurred. Failed to get SIM facility.");
385 __isInProgress = true;
391 _SimStateManagerImpl::GetSimInfo(SimInfo& simInfo) const
393 SysAssertf(__pHandle != null, "Not yet constructed. Construct() should be called before use.");
395 result r = E_SUCCESS;
397 _SimInfoImpl* pSimInfoImpl = _SimInfoImpl::GetInstance(simInfo);
398 if (pSimInfoImpl == null)
400 r = simInfo.Construct();
404 r = pSimInfoImpl->Construct();
407 SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to construct SimInfo.");
412 _SimStateManagerImpl*
413 _SimStateManagerImpl::GetInstance(SimStateManager& simStateManager)
415 return simStateManager.__pSimStateManagerImpl;
418 const _SimStateManagerImpl*
419 _SimStateManagerImpl::GetInstance(const SimStateManager& simStateManager)
421 return simStateManager.__pSimStateManagerImpl;
424 }} // Tizen::Telephony