2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
19 * @file FNet_NetConnectionManagerImpl.cpp
20 * @brief This is the implementation for the _NetConnectionManagerImpl class.
24 #include <net_connection.h>
25 #include <unique_ptr.h>
26 #include <FNetNetConnection.h>
27 #include <FNetIManagedNetConnectionEventListener.h>
28 #include <FNetNetConnectionManager.h>
29 #include <FBaseRtMutexGuard.h>
30 #include <FBaseSysLog.h>
31 #include <FBase_StringConverter.h>
32 #include "FNet_NetTypes.h"
33 #include "FNet_ManagedNetConnectionImpl.h"
34 #include "FNet_NetConnectionManagerImpl.h"
35 #include "FNet_NetConnectionInfoImpl.h"
36 #include "FNet_SystemNetConnection.h"
37 #include "FNet_NetAccountDatabase.h"
38 #include "FNet_NetAccountManagerImpl.h"
39 #include "FNet_NetConnectionEvent.h"
40 #include "FNet_NetUtility.h"
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Collection;
45 using namespace Tizen::Base::Runtime;
47 namespace Tizen { namespace Net
50 static const int _NET_CONNECTION_LINGER_TIMER_VALUE = 10000; // 10sec
52 _NetConnectionManagerImpl* _NetConnectionManagerImpl::__pInstance = null;
54 _NetConnectionManagerImpl::_NetConnectionManagerImpl()
55 : __managedNetPreference(NET_WIFI_FIRST)
56 , __managedNetAccountId(_DEFAULT_PS_ACCOUNT_ID)
57 , __managedNetRefCount(0)
58 , __isManagedNetDefaultMode(true)
59 , __pManagedSystemNetConnection(null)
60 , __pManagedNetConnectionEvent(null)
61 , __pManagedNetConnectionTimer(null)
62 , __pDefaultConnection(null)
63 , __pWifiConnection(null)
64 , __pPsConnection(null)
68 _NetConnectionManagerImpl::~_NetConnectionManagerImpl()
70 if (__pManagedNetConnectionTimer != null)
72 delete __pManagedNetConnectionTimer;
73 __pManagedNetConnectionTimer = null;
76 if (__pManagedNetConnectionEvent != null)
78 if (__pManagedSystemNetConnection != null)
80 __pManagedSystemNetConnection->Stop(*__pManagedNetConnectionEvent, false);
81 __pManagedSystemNetConnection = null;
84 __pManagedNetConnectionEvent->Release();
85 __pManagedNetConnectionEvent = null;
90 _NetConnectionManagerImpl::Construct()
93 _SystemNetConnection* pDefaultConnection = null;
94 _SystemNetConnection* pWifiConnection = null;
95 _SystemNetConnection* pManagedSystemNetConnection = null;
97 _SystemNetConnection::InitializeNetworkFramework();
99 unique_ptr<Timer> pManagedNetConnectionTimer(new (std::nothrow) Timer());
100 SysTryReturnResult(NID_NET, pManagedNetConnectionTimer != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
102 r = pManagedNetConnectionTimer->Construct(*this);
103 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
105 unique_ptr<_NetConnectionEvent> pManagedNetConnectionEvent(new (std::nothrow) _NetConnectionEvent());
106 SysTryReturnResult(NID_NET, pManagedNetConnectionEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
108 r = pManagedNetConnectionEvent->Construct();
109 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
111 pDefaultConnection = _SystemNetConnection::GetDefaultInstance();
112 SysTryReturnResult(NID_NET, pDefaultConnection != null, E_SYSTEM, "Failed to get a default connection.");
115 pWifiConnection = _SystemNetConnection::GetWifiInstance();
116 SysTryReturnResult(NID_NET, pWifiConnection != null, E_SYSTEM, "Failed to get a wifi connection.");
117 #endif // !(_OSP_EMUL_)
119 pManagedSystemNetConnection = pDefaultConnection;
121 r = pManagedSystemNetConnection->AddEvent(*pManagedNetConnectionEvent);
122 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
124 pManagedNetConnectionEvent->SetSystemConnection(pManagedSystemNetConnection);
126 __pManagedSystemNetConnection = pManagedSystemNetConnection;
127 __pManagedNetConnectionEvent = pManagedNetConnectionEvent.release();
128 __pManagedNetConnectionTimer = pManagedNetConnectionTimer.release();
129 __pDefaultConnection = pDefaultConnection;
130 __pWifiConnection = pWifiConnection;
136 _NetConnectionManagerImpl::OnTimerExpired(Timer& timer)
138 if ((__pManagedSystemNetConnection != null) &&(__managedNetRefCount == 0))
140 __pManagedSystemNetConnection->Stop(*__pManagedNetConnectionEvent);
145 _NetConnectionManagerImpl::CreateNetConnectionN(NetAccountId netAccountId)
147 SysLog(NID_NET, "CreateNetConnectionN() has been called with accountId:%d", netAccountId);
149 result r = E_SUCCESS;
151 SysTryReturn(NID_NET, ((netAccountId == _DEFAULT_WIFI_ACCOUNT_ID) ||
152 (netAccountId == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID) ||
153 (netAccountId == _DEFAULT_USB_ACCOUNT_ID) ||
154 (netAccountId >= _PS_ACCOUNT_ID_START)),
155 null, E_INVALID_ACCOUNT, "[%s] Invalid network account. accountId=%d",
156 GetErrorMessage(E_INVALID_ACCOUNT), netAccountId);
158 unique_ptr<NetConnection> pConnection(new (std::nothrow) NetConnection());
159 SysTryReturn(NID_NET, pConnection != null, null, E_OUT_OF_MEMORY,
160 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
162 r = pConnection->Construct(netAccountId);
163 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
167 return pConnection.release();
170 ManagedNetConnection*
171 _NetConnectionManagerImpl::GetManagedNetConnectionN(void) const
175 ManagedNetConnection* pConnection = null;
177 pConnection = _ManagedNetConnectionImpl::CreateInstanceN();
183 _NetConnectionManagerImpl::GetNetPreference(void) const
187 return __managedNetPreference;
191 _NetConnectionManagerImpl::SetNetPreference(NetPreferenceType netPreference)
193 result r = E_SUCCESS;
195 _SystemNetConnection* pNewConnection = null;
196 #endif // !_OSP_EMUL_
198 SysLog(NID_NET, "SetNetPreference() has been called with preference:%d", netPreference);
200 SysTryReturnResult(NID_NET, __managedNetRefCount == 0, E_INVALID_OPERATION, "The managed network connection is used.");
202 if (netPreference == NET_PS_ONLY)
204 if (__managedNetAccountId == _DEFAULT_PS_ACCOUNT_ID)
206 __pPsConnection = _SystemNetConnection::GetPsInstance(_NetAccountManagerImpl::GetInternetAccountId());
210 __pPsConnection = _SystemNetConnection::GetPsInstance(__managedNetAccountId);
214 Mutex* pLock = _NetUtility::GetLock();
215 MutexGuard locked(*pLock);
217 __managedNetPreference = netPreference;
219 if ((__managedNetPreference == NET_WIFI_FIRST) && (__managedNetAccountId == _DEFAULT_PS_ACCOUNT_ID))
221 __isManagedNetDefaultMode = true;
225 __isManagedNetDefaultMode = false;
228 __pManagedNetConnectionTimer->Cancel();
231 if (__isManagedNetDefaultMode)
233 pNewConnection = __pDefaultConnection;
235 else if (__managedNetPreference == NET_WIFI_ONLY)
237 pNewConnection = __pWifiConnection;
239 else if (__managedNetPreference == NET_PS_ONLY)
241 pNewConnection = __pPsConnection;
243 else // NET_WIFI_FIRST
245 if (__pWifiConnection->GetConnectionState() == NET_CONNECTION_STATE_STARTED)
247 pNewConnection = __pWifiConnection;
251 pNewConnection = __pPsConnection;
255 if (pNewConnection != __pManagedSystemNetConnection)
257 SysLog(NID_NET, "ManagedNetConnection should be changed. [0x%x] -> [0x%x]",
258 __pManagedSystemNetConnection, pNewConnection);
259 if (__pManagedSystemNetConnection != null)
261 __pManagedSystemNetConnection->Stop(*__pManagedNetConnectionEvent, false);
262 __pManagedSystemNetConnection->RemoveEvent(*__pManagedNetConnectionEvent);
263 __pManagedSystemNetConnection = null;
264 __pManagedNetConnectionEvent->SetConnectionState(NET_CONNECTION_STATE_NONE);
265 __pManagedNetConnectionEvent->SetSystemConnection(null);
268 if (pNewConnection != null)
270 __pManagedSystemNetConnection = pNewConnection;
271 __pManagedSystemNetConnection->AddEvent(*__pManagedNetConnectionEvent);
272 __pManagedNetConnectionEvent->SetSystemConnection(__pManagedSystemNetConnection);
275 #endif // !_OSP_EMUL_
283 _NetConnectionManagerImpl::GetNetConnectionInfoN(NetAccountId netAccountId)
285 _NetConnectionManagerImpl* pThis = _NetConnectionManagerImpl::GetInstance();
289 result r = E_SUCCESS;
290 NetConnectionInfo* pInfo = null;
291 _SystemNetConnection* pSystemNetConnection = null;
293 SysTryReturn(NID_NET, pThis != null, null, E_SYSTEM,
294 "[%s] A system error has been occurred. System is not ready.", GetErrorMessage(E_SYSTEM));
296 if (netAccountId == _DEFAULT_WIFI_ACCOUNT_ID)
298 pSystemNetConnection = _SystemNetConnection::GetWifiInstance();
300 else if (netAccountId == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID)
302 pSystemNetConnection = _SystemNetConnection::GetWifiDirectInstance();
304 else if (netAccountId == _DEFAULT_USB_ACCOUNT_ID)
306 pSystemNetConnection = _SystemNetConnection::GetUsbInstance();
309 if (pSystemNetConnection != null)
311 pInfo = new (std::nothrow) NetConnectionInfo();
312 SysTryReturn(NID_NET, pInfo != null, null,
313 E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
315 pSystemNetConnection->GetConnectionInfo(*pInfo);
320 connection_profile_h profileHandle = null;
322 r = _NetAccountDatabase::GetProfileName(netAccountId, profileName);
323 SysTryReturn(NID_NET, r == E_SUCCESS, null, E_INVALID_ACCOUNT,
324 "[%s] Invalid network account. accountId:%d", GetErrorMessage(E_INVALID_ACCOUNT), netAccountId);
326 profileHandle = (void*)_NetAccountManagerImpl::GetPsProfileHandleN(profileName);
327 SysTryReturn(NID_NET, profileHandle != null, null, E_INVALID_ACCOUNT,
328 "[%s] Invalid network account. accountId:%d", GetErrorMessage(E_INVALID_ACCOUNT), netAccountId);
330 pInfo = _NetConnectionInfoImpl::CreateNetConnectionInfoN(profileHandle);
331 connection_profile_destroy(profileHandle);
338 _NetConnectionManagerImpl::GetAllNetConnectionInfoN(void)
340 _NetConnectionManagerImpl* pThis = _NetConnectionManagerImpl::GetInstance();
341 NetConnectionInfo* pInfo = null;
345 SysTryReturn(NID_NET, pThis != null, null, E_SYSTEM,
346 "[%s] Asystem error has been occurred. System is not ready.", GetErrorMessage(E_SYSTEM));
348 unique_ptr<ArrayList, _CollectionDeleter> pList(new (std::nothrow) ArrayList());
349 SysTryReturn(NID_NET, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
352 const NetConnectionInfo* pManagedNetConnectionInfo = pThis->GetManagedNetConnectionInfo();
353 pInfo = new (std::nothrow) NetConnectionInfo(*pManagedNetConnectionInfo);
359 int ret = CONNECTION_ERROR_NONE;
360 connection_h connectionHandle = null;
361 connection_profile_iterator_h iterator = null;
362 connection_profile_h profileHandle = null;
364 ret = connection_create(&connectionHandle);
365 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
366 "[%s] A system error has been occurred. The return value from connection_create() is %d",
367 GetErrorMessage(E_SYSTEM), ret);
368 unique_ptr<void, _ConnectionDeleter> pConnectionHandle(connectionHandle);
370 ret = connection_get_profile_iterator(connectionHandle, CONNECTION_ITERATOR_TYPE_CONNECTED, &iterator);
371 SysTryReturn(NID_NET, ret == CONNECTION_ERROR_NONE, null, E_SYSTEM,
372 "[%s] A system error has been occurred. The return value from connection_get_profile_iterator() is %d",
373 GetErrorMessage(E_SYSTEM), ret);
374 unique_ptr<void, _ProfileIteratorDeleter> pIterator(iterator);
376 ret = connection_profile_iterator_next(iterator, &profileHandle);
377 while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
379 pInfo = _NetConnectionInfoImpl::CreateNetConnectionInfoN(profileHandle);
385 profileHandle = null;
386 ret = connection_profile_iterator_next(iterator, &profileHandle);
390 SysLog(NID_NET, "GetAllNetConnectionInfoN() has been succeeded with result:%s, count:%d",
391 GetErrorMessage(GetLastResult()), pList->GetCount());
393 return pList.release();
397 _NetConnectionManagerImpl::IsDefaultMode(void)
400 _NetConnectionManagerImpl* pThis = _NetConnectionManagerImpl::GetInstance();
402 SysLog(NID_NET, "Preference:%d, AccountId:%d", pThis->__managedNetPreference, pThis->__managedNetAccountId);
404 ret = pThis->__isManagedNetDefaultMode;
406 SysLog(NID_NET, "IsDefaultMode() has done with ret(%d).", ret);
412 _NetConnectionManagerImpl::GetProxyAddress(void)
414 _NetConnectionManagerImpl* pThis = _NetConnectionManagerImpl::GetInstance();
417 proxyAddress.Clear();
421 if (pThis->__pManagedSystemNetConnection != null)
423 proxyAddress = pThis->__pManagedSystemNetConnection->GetProxyAddress();
427 SysLog(NID_NET, "The current value of ProxyAddress is %ls", proxyAddress.GetPointer());
432 _NetConnectionManagerImpl*
433 _NetConnectionManagerImpl::GetInstance(void)
435 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
437 if (__pInstance == null)
440 pthread_once(&onceBlock, InitSingleton);
441 result r = GetLastResult();
444 onceBlock = PTHREAD_ONCE_INIT;
452 _NetConnectionManagerImpl::InitSingleton(void)
454 result r = E_SUCCESS;
455 static _NetConnectionManagerImpl instance;
457 r = instance.Construct();
458 SysTryReturnVoidResult(NID_NET, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
461 __pInstance = &instance;
465 _NetConnectionManagerImpl::SetManagedNetAccountId(NetAccountId netAccountId)
467 result r = E_SUCCESS;
470 SysTryReturnResult(NID_NET, __managedNetRefCount == 0, E_INVALID_OPERATION, "The managed network connection is used.");
471 SysTryReturnResult(NID_NET, netAccountId >= _PS_ACCOUNT_ID_START,
472 E_INVALID_ARG, "Invalid argument is used. accountId=%d", netAccountId);
474 r = _NetAccountDatabase::GetAccountName(netAccountId, accountName);
475 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is used. accountId=%d", netAccountId);
477 __pPsConnection = _SystemNetConnection::GetPsInstance(netAccountId);
478 SysTryReturnResult(NID_NET, __pPsConnection != null, E_INVALID_ARG, "Invalid argument is used. accountId=%d", netAccountId);
480 Mutex* pLock = _NetUtility::GetLock();
481 MutexGuard locked(*pLock);
483 __managedNetAccountId = netAccountId;
485 if ((__managedNetPreference == NET_WIFI_FIRST) && (__managedNetAccountId == _DEFAULT_PS_ACCOUNT_ID))
487 __isManagedNetDefaultMode = true;
491 __isManagedNetDefaultMode = false;
500 _NetConnectionManagerImpl::GetManagedNetAccountId(void) const
502 NetAccountId accountId = INVALID_HANDLE;
504 SysTryReturn(NID_NET, __pManagedSystemNetConnection != null, INVALID_HANDLE,
505 E_INVALID_STATE, "[%s] Managed network connection is inactive.", GetErrorMessage(E_INVALID_STATE));
507 SysTryReturn(NID_NET, GetManagedNetConnectionState() == NET_CONNECTION_STATE_STARTED, INVALID_HANDLE,
508 E_INVALID_STATE, "[%s] Managed network connection is inactive.",GetErrorMessage(E_INVALID_STATE));
510 if (__pManagedSystemNetConnection->GetBearerType() == NET_BEARER_WIFI)
512 accountId = _DEFAULT_WIFI_ACCOUNT_ID;
516 if (__managedNetAccountId == _DEFAULT_PS_ACCOUNT_ID)
518 accountId = _NetAccountManagerImpl::GetInternetAccountId();
522 accountId = __managedNetAccountId;
532 _NetConnectionManagerImpl::GetManagedNetConnectionState(void) const
534 if ((__isManagedNetDefaultMode) && (__pManagedSystemNetConnection != null))
536 return __pManagedSystemNetConnection->GetConnectionState();
540 return __pManagedNetConnectionEvent->GetConnectionState();
545 _NetConnectionManagerImpl::QueryManagedNetConnectionState(String& devName) const
547 if (__pManagedSystemNetConnection != null)
549 return __pManagedSystemNetConnection->QueryConnectionState(devName);
553 return __pManagedNetConnectionEvent->GetConnectionState();
557 const NetConnectionInfo*
558 _NetConnectionManagerImpl::GetManagedNetConnectionInfo(void)
560 _NetConnectionInfoImpl* pInfoImpl = null;
562 if ((__pManagedSystemNetConnection != null) &&
563 (GetManagedNetConnectionState() == NET_CONNECTION_STATE_STARTED))
565 __pManagedSystemNetConnection->GetConnectionInfo(__managedNetConnectionInfo);
569 pInfoImpl = _NetConnectionInfoImpl::GetInstance(__managedNetConnectionInfo);
570 if (pInfoImpl != null)
576 return &__managedNetConnectionInfo;
580 _NetConnectionManagerImpl::AddManagedNetConnectionEventListener(IManagedNetConnectionEventListener& listener,
581 const ManagedNetConnection* pConnection)
583 result r = E_SUCCESS;
585 r = __pManagedNetConnectionEvent->AddNetConnectionEventListener(listener, pConnection);
587 SysLog(NID_NET, "AddManagedNetConnectionEventListener() has done with result:%s", GetErrorMessage(r));
593 _NetConnectionManagerImpl::RemoveManagedNetConnectionEventListener(IManagedNetConnectionEventListener& listener)
595 result r = E_SUCCESS;
597 r = __pManagedNetConnectionEvent->RemoveNetConnectionEventListener(listener);
599 SysLog(NID_NET, "RemoveManagedNetConnectionEventListener() has done with result:%s", GetErrorMessage(r));
605 _NetConnectionManagerImpl::AddRefManagedNetConnection(void)
607 result r = E_SUCCESS;
609 _SystemNetConnection* pNewConnection = null;
610 #endif // !_OSP_EMUL_
612 Mutex* pLock = _NetUtility::GetLock();
613 MutexGuard locked(*pLock);
616 r = __pManagedSystemNetConnection->Start(*__pManagedNetConnectionEvent);
618 if (__managedNetRefCount == 0)
620 __pManagedNetConnectionTimer->Cancel();
624 pNewConnection = __pDefaultConnection;
626 else if (__managedNetPreference == NET_WIFI_ONLY)
628 pNewConnection = __pWifiConnection;
630 else if (__managedNetPreference == NET_PS_ONLY)
632 pNewConnection = __pPsConnection;
634 else // NET_WIFI_FIRST
636 if (__pWifiConnection->GetConnectionState() == NET_CONNECTION_STATE_STARTED)
638 pNewConnection = __pWifiConnection;
642 pNewConnection = __pPsConnection;
648 pNewConnection = __pManagedSystemNetConnection;
651 if (pNewConnection != __pManagedSystemNetConnection)
653 SysLog(NID_NET, "ManagedNetConnection should be changed. [0x%x] -> [0x%x]",
654 __pManagedSystemNetConnection, pNewConnection);
655 if (__pManagedSystemNetConnection != null)
657 __pManagedSystemNetConnection->Stop(*__pManagedNetConnectionEvent, false);
658 __pManagedSystemNetConnection->RemoveEvent(*__pManagedNetConnectionEvent);
659 __pManagedSystemNetConnection = null;
660 __pManagedNetConnectionEvent->SetConnectionState(NET_CONNECTION_STATE_NONE);
661 __pManagedNetConnectionEvent->SetSystemConnection(null);
664 if (pNewConnection != null)
666 __pManagedSystemNetConnection = pNewConnection;
667 __pManagedSystemNetConnection->AddEvent(*__pManagedNetConnectionEvent);
668 __pManagedNetConnectionEvent->SetSystemConnection(__pManagedSystemNetConnection);
672 if (__pManagedSystemNetConnection != null)
674 SysLog(NID_NET, "Start a ManagedNetConnection.");
675 r = __pManagedSystemNetConnection->Start(*__pManagedNetConnectionEvent);
679 r = E_CONNECTION_FAILED;
680 SysLogException(NID_NET, r, "[%s] Network is unavailable. preference[%d], accountId[%d]",
681 GetErrorMessage(E_CONNECTION_FAILED), __managedNetPreference, __managedNetAccountId);
683 #endif // !_OSP_EMUL_
687 __managedNetRefCount++;
692 SysLog(NID_NET, "AddRefManagedNetConnection() has done with result:%s", GetErrorMessage(r));
698 _NetConnectionManagerImpl::ReleaseManagedNetConnection(void)
700 result r = E_SUCCESS;
702 Mutex* pLock = _NetUtility::GetLock();
703 MutexGuard locked(*pLock);
705 __managedNetRefCount--;
707 if (__managedNetRefCount == 0)
709 SysLog(NID_NET, "ManagedNetConnection is not used any more, so start a linger timer.");
710 r = __pManagedNetConnectionTimer->Cancel();
711 r = __pManagedNetConnectionTimer->Start(_NET_CONNECTION_LINGER_TIMER_VALUE);
717 _NetConnectionManagerImpl*
718 _NetConnectionManagerImpl::GetInstance(NetConnectionManager& netConnectionManager)
720 return netConnectionManager.__pNetConnectionManagerImpl;
723 const _NetConnectionManagerImpl*
724 _NetConnectionManagerImpl::GetInstance(const NetConnectionManager& netConnectionManager)
726 return netConnectionManager.__pNetConnectionManagerImpl;