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_NetworkManagerImpl.cpp
19 * @brief This is the implementation file for _NetworkManagerImpl class.
23 #include <net_connection.h>
24 #include <telephony_network.h>
25 #include <telephony_network_type.h>
26 #include <FTelITelephonyNetworkEventListener.h>
27 #include <FTelITelephonyNetworkSettingListener.h>
28 #include <FTelNetworkInfo.h>
29 #include <FTelNetworkStatus.h>
30 #include <FTelNetworkManager.h>
31 #include <FSysSettingInfo.h>
32 #include <FBaseUtilStringUtil.h>
33 #include <FBaseSysLog.h>
34 #include "FApp_AppInfo.h"
35 #include "FTel_NetworkManagerImpl.h"
36 #include "FTel_NetworkStatusImpl.h"
37 #include "FTel_NetworkInfoImpl.h"
38 #include "FTel_NetworkManagerEvent.h"
39 #include "FTel_NetworkManagerEventArg.h"
40 #include "FTel_NetworkSettingEvent.h"
41 #include "FTel_NetworkSettingEventArg.h"
42 #include "FTel_TelephonyUtility.h"
43 #include "FTel_TelephonyIpcProxy.h"
46 using namespace Tizen::System;
47 using namespace Tizen::App;
48 using namespace Tizen::Base;
49 using namespace Tizen::Base::Utility;
50 using namespace Tizen::Base::Collection;
53 namespace Tizen { namespace Telephony
56 _NetworkManagerImpl::_NetworkManagerImpl(void)
57 : __pNetworkManagerEvent(null)
58 , __pNetworkSettingEvent(null)
59 , __pTelephonyServiceProxy(null)
60 , __pSettingListener(null)
61 , __isEventAdded(false)
65 _NetworkManagerImpl::~_NetworkManagerImpl(void)
67 if ((__pNetworkManagerEvent != null) && __isEventAdded && (__pTelephonyServiceProxy != null))
69 __pTelephonyServiceProxy->RemoveNetworkEvent(__pNetworkManagerEvent.get());
74 _NetworkManagerImpl::Construct(ITelephonyNetworkEventListener* pListener)
76 __pTelephonyServiceProxy = _TelephonyIpcProxy::GetInstance();
78 if (__pTelephonyServiceProxy == null)
80 SysLog(NID_TEL, "Creating an IPC instance to connect with the Connectivity service daemon has failed.");
83 if (pListener != null)
85 unique_ptr<_NetworkManagerEvent> pNetworkManagerEvent(new (std::nothrow) _NetworkManagerEvent);
86 SysTryReturnResult(NID_TEL, pNetworkManagerEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
88 result r = pNetworkManagerEvent->Construct();
89 SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
90 SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to construct of the _NetworkManagerEvent.");
92 SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "Failed to initialize the telephony ipc client.");
93 r = __pTelephonyServiceProxy->AddNetworkEvent(pNetworkManagerEvent.get());
94 SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
96 __isEventAdded = true;
98 pNetworkManagerEvent->AddListener(*pListener, true);
100 __pNetworkManagerEvent = move(pNetworkManagerEvent);
107 _NetworkManagerImpl::GetNetworkStatus(NetworkStatus& networkStatus) const
109 bool isRoaming = false;
110 bool isDataServiceAvailable = false;
111 bool isCallServiceAvailable = false;
113 SysTryReturnResult(NID_TEL, IsNetworkAvailable(), E_NETWORK_UNAVAILABLE, "The operation has failed because the device is in the offline mode.");
116 isRoaming = IsRoaming();
118 // Get the isCallServiceAvailable
119 SysTryReturnResult(NID_TEL, IsServiceAvailable(), E_SERVICE_UNAVAILABLE, "The operation failed because the device is out of the coverage area or in the emergency mode.")
120 isCallServiceAvailable = true;
122 isDataServiceAvailable = IsDataAvailable();
124 _NetworkStatusImpl::GetInstance(networkStatus)->SetIsRoaming(isRoaming);
125 _NetworkStatusImpl::GetInstance(networkStatus)->SetIsCallServiceAvailable(isCallServiceAvailable);
126 _NetworkStatusImpl::GetInstance(networkStatus)->SetIsDataServiceAvailable(isDataServiceAvailable);
132 _NetworkManagerImpl::GetNetworkInfo(NetworkInfo& networkInfo) const
134 result r = E_SUCCESS;
136 SysTryReturnResult(NID_TEL, IsNetworkAvailable(), E_NETWORK_UNAVAILABLE,
137 "The operation has failed because the device is in the offline mode.");
138 SysTryReturnResult(NID_TEL, IsServiceAvailable(), E_SERVICE_UNAVAILABLE,
139 "The operation failed because the device is out of the coverage area or in the emergency mode.");
141 int err = NETWORK_INFO_ERROR_NONE;
142 _ApiVersion apiVersion = _AppInfo::GetApiVersion();
146 err = network_info_get_cell_id(&cellId);
147 SysLog(NID_TEL, "The return value of network_info_get_cell_id() is %d and the cellId value is %d", err, cellId);
148 SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the cell ID.");
152 err = network_info_get_lac(&lac);
153 SysLog(NID_TEL, "The return value of network_info_get_lac() is %d and the lac value is %d", err, lac);
154 SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the lac.");
158 // Get the operator name
159 err = network_info_get_provider_name(&pTemp);
160 SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the plmn.");
161 unique_ptr<char[],_CharDeleter> pOperatorName(pTemp);
163 r = (StringUtil::Utf8ToString(pOperatorName.get(), operatorName));
164 SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
165 SysLog(NID_TEL, "The operatorname is %ls", operatorName.GetPointer());
168 err = network_info_get_mcc(&pTemp);
169 SysLog(NID_TEL, "The return value of network_info_get_mcc() is %d and the mcc value is %s", err, pTemp);
170 SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the mcc.");
171 unique_ptr<char[],_CharDeleter> pMcc(pTemp);
174 err = network_info_get_mnc(&pTemp);
175 SysLog(NID_TEL, "The return value of network_info_get_mnc() is %d and the mnc value is %s", err, pTemp);
176 SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the mnc.");
177 unique_ptr<char[],_CharDeleter> pMnc(pTemp);
180 r = plmn.Append(pMcc.get());
181 SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to set plmn.");
183 r = plmn.Append(pMnc.get());
184 SysLog(NID_TEL, "The plmn value is %ls", plmn.GetPointer());
185 SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to set plmn.");
187 _NetworkInfoImpl::GetInstance(networkInfo)->SetCellId(cellId);
188 _NetworkInfoImpl::GetInstance(networkInfo)->SetLac(lac);
189 _NetworkInfoImpl::GetInstance(networkInfo)->SetOperatorName(operatorName);
190 _NetworkInfoImpl::GetInstance(networkInfo)->SetMcc(atoi(pMcc.get()));
191 _NetworkInfoImpl::GetInstance(networkInfo)->SetMnc(atoi(pMnc.get()));
193 if (apiVersion == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
195 _NetworkInfoImpl::GetInstance(networkInfo)->SetPlmn(operatorName);
199 _NetworkInfoImpl::GetInstance(networkInfo)->SetPlmn(plmn);
206 _NetworkManagerImpl::SetNetworkSettingListener(ITelephonyNetworkSettingListener* pListener)
208 SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "A system error has occurred. IPC instance has not been constructed yet.");
210 result r = __pTelephonyServiceProxy->HasSystemPrivilege();
211 SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
213 if (__pSettingListener != null)
215 __pNetworkSettingEvent->RemoveListener(*__pSettingListener);
216 __pSettingListener = null;
219 // Creates event object
220 if (__pNetworkSettingEvent == null && pListener != null)
222 std::unique_ptr<_NetworkSettingEvent> pNetworkSettingEvent(new (std::nothrow) _NetworkSettingEvent());
224 SysTryReturnResult(NID_TEL, pNetworkSettingEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
226 r = pNetworkSettingEvent->Construct();
227 SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
229 __pNetworkSettingEvent = move(pNetworkSettingEvent);
233 if (pListener != null)
235 __pSettingListener = pListener;
236 r = __pNetworkSettingEvent->AddListener(*__pSettingListener, true);
241 __pNetworkSettingEvent.reset(null);
242 SysLogException(NID_TEL, r, "[%s] Propagating.", GetErrorMessage(r));
249 _NetworkManagerImpl::GetNetworkSelectionMode(void)
251 SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "A system error has occurred. IPC instance has not been constructed yet.");
253 result r = __pTelephonyServiceProxy->GetNetworkSelectionMode(this);
254 SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
255 SysTryReturnResult(NID_TEL, r != E_PRIVILEGE_DENIED, r, "Propagating.");
256 SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_OPERATION_FAILED, "Failed to get selection mode.");
262 _NetworkManagerImpl::SelectNetwork(const NetworkInfo & networkInfo)
264 SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "IPC instance has not been constructed yet.");
266 const _NetworkInfoImpl* pNetworkInfoImpl = _NetworkInfoImpl::GetInstance(networkInfo);
267 TelNetworkSystemType_t networkSystemType = _TelephonyUtility::ConvertNetworkType(pNetworkInfoImpl->GetNetworkType());
269 result r = __pTelephonyServiceProxy->SelectNetwork(this, pNetworkInfoImpl->GetPlmn(), (int)networkSystemType);
270 SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
271 SysTryReturnResult(NID_TEL, r != E_PRIVILEGE_DENIED, r, "Propagating.");
272 SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_OPERATION_FAILED, "Failed to select network manually.");
278 _NetworkManagerImpl::SelectNetwork(void)
280 SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "A system error has occurred. IPC instance has not been constructed yet.");
282 result r = __pTelephonyServiceProxy->SelectNetwork(this);
283 SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
284 SysTryReturnResult(NID_TEL, r != E_PRIVILEGE_DENIED, r, "Propagating.");
285 SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_OPERATION_FAILED, "Failed to select network automatically.");
291 _NetworkManagerImpl::SearchNetwork(void)
293 SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "A system error has occurred. IPC instance has not been constructed yet.");
295 result r = __pTelephonyServiceProxy->SearchNetwork(this);
296 SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
297 SysTryReturnResult(NID_TEL, r != E_PRIVILEGE_DENIED, r, "Propagating.");
298 SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_OPERATION_FAILED, "Failed to search network automatic.");
304 _NetworkManagerImpl::IsDataAvailable(void) const
306 connection_h pConnHandle = null;
307 connection_cellular_state_e dataServiceState = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
308 bool isDataAvailable = false;
309 int err = connection_create(&pConnHandle);
310 SysLog(NID_TEL, "The return value of connection_create() is %d", err);
311 SysTryReturn(NID_TEL, err == CONNECTION_ERROR_NONE, false, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get the connection handle.");
312 std::unique_ptr<void, _ConnectionDeleter> pConnectionHandle(pConnHandle);
314 err = connection_get_cellular_state(pConnHandle, &dataServiceState);
316 SysLog(NID_TEL, "The return value of connection_get_cellular_state() is 0x%x and the dataServiceState is %d", err, dataServiceState);
318 if (dataServiceState == CONNECTION_CELLULAR_STATE_AVAILABLE || dataServiceState == CONNECTION_CELLULAR_STATE_CONNECTED)
320 isDataAvailable = true;
324 isDataAvailable = false;
327 return isDataAvailable;
331 _NetworkManagerImpl::IsRoaming(void) const
333 int err = NETWORK_INFO_ERROR_NONE;
334 bool isRoaming = false;
336 err = network_info_is_roaming(&isRoaming);
337 SysLog(NID_TEL, "The return value of network_info_is_roaming() is 0x%x and the roming state is %s", err, isRoaming ? "True" : "False");
343 _NetworkManagerImpl::IsServiceAvailable(bool checkEmergency /* = false */)
345 bool isAvailable = false;
346 int err = NETWORK_INFO_ERROR_NONE;
347 network_info_service_state_e state;
349 err = network_info_get_service_state(&state);
350 SysLog(NID_TEL, "The return value of network_info_get_service_state() is 0x%x and the state is %d and checkEmergency is %s",
351 err, state, checkEmergency ? "True" : "False");
353 if (!checkEmergency && state == NETWORK_INFO_SERVICE_STATE_IN_SERVICE)
357 else if (checkEmergency && (state == NETWORK_INFO_SERVICE_STATE_IN_SERVICE ||
358 state == NETWORK_INFO_SERVICE_STATE_EMERGENCY_ONLY))
367 _NetworkManagerImpl::IsNetworkAvailable(void)
369 bool isFlightModeEnabled = false;
370 result r = SettingInfo::GetValue(L"http://tizen.org/setting/network.flight_mode", isFlightModeEnabled);
371 SysLog(NID_TEL, "isFlightModeEnabled is %s", isFlightModeEnabled ? "True":"False");
372 SysLog(NID_TEL, "The return value of SettingInfo::GetValue() is %s", GetErrorMessage(r));
379 return !isFlightModeEnabled;
383 _NetworkManagerImpl::GetInstance(NetworkManager& networkManager)
385 return networkManager.__pNetworkManagerImpl;
388 const _NetworkManagerImpl*
389 _NetworkManagerImpl::GetInstance(const NetworkManager& networkManager)
391 return networkManager.__pNetworkManagerImpl;
396 _NetworkManagerImpl::OnTelephonyNetworkSelectionModeReceived(bool isManual, result r)
398 SysLog(NID_TEL, "The mode is %s and the result is %s", isManual ? "Manual":"Automatic", GetErrorMessage(r));
400 if (__pNetworkSettingEvent != null)
402 _NetworkSettingEventArg* pEventArg = new (std::nothrow)_NetworkSettingEventArg(_NETWORK_EVENT_GET_SELECTION_MODE, isManual, r);
403 SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
404 (void)__pNetworkSettingEvent->Fire(*pEventArg);
409 _NetworkManagerImpl::OnTelephonyNetworkSelectionCompleted(result r)
411 SysLog(NID_TEL, "The listener has called with the result %s", GetErrorMessage(r));
413 if (__pNetworkSettingEvent != null)
415 _NetworkSettingEventArg* pEventArg = new (std::nothrow)_NetworkSettingEventArg(_NETWORK_EVENT_SELECT_NETWORK, r);
416 SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
417 (void)__pNetworkSettingEvent->Fire(*pEventArg);
422 _NetworkManagerImpl::OnTelephonyNetworkSearchCompleted(String message, result r)
424 SysLog(NID_TEL, "The listener has called with the result %s", GetErrorMessage(r));
426 if (__pNetworkSettingEvent != null)
428 IList* pNetworkInfoList = null;
432 pNetworkInfoList = ParsingMessageN(message);
434 if (pNetworkInfoList == null)
436 SysLog(NID_TEL, "ParsingMessageN() has failed ");
441 _NetworkSettingEventArg* pEventArg = new (std::nothrow)_NetworkSettingEventArg(_NETWORK_EVENT_SEARCH_COMPLETED, pNetworkInfoList, r);
442 SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
444 (void)__pNetworkSettingEvent->Fire(*pEventArg);
449 _NetworkManagerImpl::ParsingMessageN(const String& message)
451 result r = E_SUCCESS;
452 int currentIndex = 0;
455 NetworkInfo* pNetworkInfo = null;
457 int networkInfoCount = 0;
459 r = message.SubString(0, 1, temp);
460 SysTryReturn(NID_TEL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));
464 r = Integer::Decode(temp, networkInfoCount);
465 SysTryReturn(NID_TEL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));
467 ArrayList* pNetworkInfoList = null;
469 pNetworkInfoList = new (std::nothrow) ArrayList();
470 SysTryReturn(NID_TEL, pNetworkInfoList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
472 r = pNetworkInfoList->Construct(networkInfoCount);
473 SysTryReturn(NID_TEL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
475 for (int i = 0 ; i < networkInfoCount ; i++)
477 pNetworkInfo = GenerateNetworkInfoN(message, currentIndex);
479 pNetworkInfoList->Add(*pNetworkInfo);
482 return pNetworkInfoList;
487 _NetworkManagerImpl::GenerateNetworkInfoN(const String& message, int& currentIndex)
489 NetworkInfo* pNetworkInfo = null;
490 _NetworkInfoImpl* pNetworkInfoImpl = null;
495 pNetworkInfo = new (std::nothrow) NetworkInfo();
496 SysTryReturn(NID_TEL, pNetworkInfo != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
498 pNetworkInfoImpl = _NetworkInfoImpl::GetInstance(*pNetworkInfo);
503 tempString = MessageWithLengthToString(message, currentIndex);
504 _TelephonyUtility::GetMccMncFromPlmn(tempString, mcc, mnc);
505 pNetworkInfoImpl->SetPlmn(tempString);
506 pNetworkInfoImpl->SetMcc(mcc);
507 pNetworkInfoImpl->SetMnc(mnc);
508 SysLog(NID_TEL, "The plmn value is %ls, the mcc value is %d and the mnc is %d",tempString.GetPointer(), mcc, mnc);
511 tempString = MessageWithLengthToString(message, currentIndex);
512 pNetworkInfoImpl->SetOperatorName(tempString);
513 SysLog(NID_TEL, "The Operator Name is %ls", tempString.GetPointer());
516 tempString = MessageToString(message, currentIndex);
517 Tizen::Base::Integer::Parse(tempString, temp);
519 _NetworkType networkType = _TelephonyUtility::ConvertNetworkType(static_cast<TelNetworkSystemType_t>(temp));
520 pNetworkInfoImpl->SetNetworkType(networkType);
521 SysLog(NID_TEL, "The NetworkType is %d", networkType);
528 _NetworkManagerImpl::MessageToString(const String& message, int& currentIndex)
532 result r = E_SUCCESS;
534 r = message.IndexOf(":", currentIndex, nextIndex);
538 SysLog(NID_TEL, "The result of message.IndexOf is %s", GetErrorMessage(r));
541 currentIndex = nextIndex + 1;
543 r = message.IndexOf(":", currentIndex, nextIndex);
547 SysLog(NID_TEL, "The result of message.IndexOf is %s", GetErrorMessage(r));
550 r = message.SubString(currentIndex, nextIndex-currentIndex, tempString);
554 SysLog(NID_TEL, "The result of message.SubString is %s", GetErrorMessage(r));
561 _NetworkManagerImpl::MessageWithLengthToString(const String& message, int& currentIndex)
563 // e.g. plmn or network name -> "05Olleh",
565 String tempString("");
566 result r = E_SUCCESS;
569 r = message.IndexOf(":", currentIndex, nextIndex);
573 SysLog(NID_TEL, "The result of message.IndexOf is %s", GetErrorMessage(r));
576 currentIndex = nextIndex + 1;
578 message.SubString(currentIndex, 2, tempString);
581 Tizen::Base::Integer::Decode(tempString, static_cast<int&>(len));
589 r = message.SubString(currentIndex, len, tempString);
593 SysLog(NID_TEL, "The result of message.IndexOf is %s", GetErrorMessage(r));
602 }} // Tizen::Telephony