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 SysLog(NID_TEL, "Enter"); // Todo: Remove temporary log
165 TelSimCardStatus_t* status = (TelSimCardStatus_t*)pData;
166 SysLog(NID_TEL, "TelSimCardStatus: [%d]", *status);
168 _SimStateManagerImpl* pSimStateManagerImpl = static_cast<_SimStateManagerImpl*>(pUserData);
170 SimState state = SIM_STATE_UNKNOWN;
174 case TAPI_SIM_STATUS_CARD_NOT_PRESENT: // fall through
175 case TAPI_SIM_STATUS_CARD_REMOVED:
176 state = SIM_STATE_ABSENT;
179 case TAPI_SIM_STATUS_SIM_INITIALIZING:
180 state = SIM_STATE_INITIALIZING;
183 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
184 state = SIM_STATE_READY;
187 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
188 state = SIM_STATE_PIN_REQUIRED;
191 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
192 state = SIM_STATE_PUK_REQUIRED;
195 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED: // fall through
196 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED: // fall through
197 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED: // fall through
198 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
199 state = SIM_STATE_NETWORK_LOCKED;
202 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
203 state = SIM_STATE_SIM_LOCKED;
206 case TAPI_SIM_STATUS_CARD_ERROR: // fall through
207 case TAPI_SIM_STATUS_CARD_BLOCKED: // fall through
208 case TAPI_SIM_STATUS_UNKNOWN: // fall through
210 state = SIM_STATE_UNKNOWN;
214 if (pSimStateManagerImpl->__simState != state)
216 pSimStateManagerImpl->__simState = state;
218 _SimEventArg* pEventArg = new (std::nothrow)_SimEventArg(_SIM_EVENT_SIM_STATE_CHANGED, state);
219 SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
221 (void)pSimStateManagerImpl->__pSimEvent->Fire(*pEventArg);
224 SysLog(NID_TEL, "Exit"); // Todo: Remove temporary log
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;
266 _SimManagerEventArg* pEventArg = new (std::nothrow)_SimManagerEventArg(_SIM_MANAGER_EVENT_PIN_LOCK_SETTING_RESULT_RECEIVED, isEnabled, r);
267 SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
269 (void)pSimStateManagerImpl->__pSimManagerEvent->Fire(*pEventArg);
271 SysLog(NID_TEL, "Exit"); // Todo: Remove temporary log
275 _SimStateManagerImpl::SetSimEventListener(ITelephonySimEventListener *pListener)
277 result r = E_SUCCESS;
279 if (__pSimEventListener != null)
281 __pSimEvent->RemoveListener(*__pSimEventListener);
282 __pSimEventListener = null;
285 if (__pSimEvent == null && pListener != null)
287 std::unique_ptr<_SimEvent> pSimEvent(new (std::nothrow) _SimEvent());
288 SysTryReturnResult(NID_TEL, pSimEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
290 r = pSimEvent->Construct();
291 SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
293 __pSimEvent = move(pSimEvent);
296 if (pListener != null)
298 r = __pSimEvent->AddListener(*pListener, true);
302 __pSimEventListener = pListener;
308 __pSimEvent.reset(null);
309 SysLogException(NID_TEL, r, "[%s] Propagating.", GetErrorMessage(r));
316 _SimStateManagerImpl::SetSimStateManagerGetPinLockSettingResultListener(ISimStateManagerGetPinLockSettingResultListener *pListener)
318 result r = E_SUCCESS;
320 if (__pSimStateManagerGetPinLockSettingResultListener != null)
322 __pSimManagerEvent->RemoveListener(*__pSimStateManagerGetPinLockSettingResultListener);
323 __pSimStateManagerGetPinLockSettingResultListener = null;
326 if (__pSimManagerEvent == null && pListener != null)
328 std::unique_ptr<_SimManagerEvent> pSimManagerEvent(new (std::nothrow) _SimManagerEvent());
329 SysTryReturnResult(NID_TEL, pSimManagerEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
331 r = pSimManagerEvent->Construct();
332 SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
334 __pSimManagerEvent = move(pSimManagerEvent);
337 if (pListener != null)
339 r = __pSimManagerEvent->AddListener(*pListener, true);
342 __pSimStateManagerGetPinLockSettingResultListener = pListener;
348 __pSimManagerEvent.reset(null);
349 SysLogException(NID_TEL, r, "[%s] Propagating.", GetErrorMessage(r));
356 _SimStateManagerImpl::GetSimState(void) const
362 _SimStateManagerImpl::GetPinLockSetting(ISimStateManagerGetPinLockSettingResultListener* pListener)
364 result r = E_SUCCESS;
367 SysTryReturnResult(NID_TEL, pListener != null, E_INVALID_ARG, "The specified input parameter is invalid.");
368 SysTryReturnResult(NID_TEL, !__isInProgress, E_IN_PROGRESS, "The previous request is in progress.");
370 r = SetSimStateManagerGetPinLockSettingResultListener(pListener);
371 SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to set listener.");
373 ret = tel_get_sim_facility(__pHandle, TAPI_SIM_LOCK_SC, OnGetPinLockSettingCallback, this);
375 SysTryReturnResult(NID_TEL, ret != TAPI_API_SIM_NOT_FOUND, E_DEVICE_UNAVAILABLE,
376 "The operation failed due to a missing SIM card.");
378 SysTryReturnResult(NID_TEL, ret == TAPI_API_SUCCESS || ret == TAPI_API_SIM_NOT_FOUND, E_SYSTEM,
379 "A system error has occurred. Failed to get SIM facility.");
381 __isInProgress = true;
387 _SimStateManagerImpl::GetSimInfo(SimInfo& simInfo) const
389 SysAssertf(__pHandle != null, "Not yet constructed. Construct() should be called before use.");
391 result r = E_SUCCESS;
393 _SimInfoImpl* pSimInfoImpl = _SimInfoImpl::GetInstance(simInfo);
394 if (pSimInfoImpl == null)
396 r = simInfo.Construct();
400 r = pSimInfoImpl->Construct();
403 SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to construct SimInfo.");
408 _SimStateManagerImpl*
409 _SimStateManagerImpl::GetInstance(SimStateManager& simStateManager)
411 return simStateManager.__pSimStateManagerImpl;
414 const _SimStateManagerImpl*
415 _SimStateManagerImpl::GetInstance(const SimStateManager& simStateManager)
417 return simStateManager.__pSimStateManagerImpl;
420 }} // Tizen::Telephony