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 FNet_SystemNetConnection.cpp
19 * @brief This is the implementation file for _SystemNetConnection class.
22 * This file contains the implementation of _SystemNetConnection class.
26 #include <net_connection.h>
27 #include <FNetNetConnectionInfo.h>
28 #include <FBaseRtMutexGuard.h>
29 #include <FBaseSysLog.h>
30 #include <FBase_StringConverter.h>
31 #include <FIo_DirectoryImpl.h>
32 #include "FNet_NetTypes.h"
33 #include "FNet_SystemNetConnection.h"
34 #include "FNet_NetConnectionInfoImpl.h"
35 #include "FNet_DefaultSystemNetConnection.h"
36 #include "FNet_PsSystemNetConnection.h"
37 #include "FNet_WifiSystemNetConnection.h"
38 #include "FNet_WifiDirectSystemNetConnection.h"
39 #include "FNet_UsbSystemNetConnection.h"
40 #include "FNet_NetAccountDatabase.h"
41 #include "FNet_NetAccountManagerImpl.h"
42 #include "FNet_NetConnectionEvent.h"
43 #include "FNet_NetConnectionEventArg.h"
44 #include "FNet_NetUtility.h"
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Collection;
49 using namespace Tizen::Base::Runtime;
50 using namespace Tizen::Io;
52 namespace Tizen { namespace Net {
54 bool _SystemNetConnection::__isInitialized = false;
55 HashMap* _SystemNetConnection::__pPsSystemConnectionMap = null;
56 _DefaultSystemNetConnection* _SystemNetConnection::__pDefaultConnection = null;
57 _WifiSystemNetConnection* _SystemNetConnection::__pWifiConnection = null;
58 _WifiDirectSystemNetConnection* _SystemNetConnection::__pWifiDirectConnection = null;
59 _UsbSystemNetConnection* _SystemNetConnection::__pUsbConnection = null;
61 _SystemNetConnection::_SystemNetConnection(void)
62 : _bearerType(NET_BEARER_NONE)
63 , _connectionState(NET_CONNECTION_STATE_NONE)
64 , _pConnectionInfo(null)
71 _SystemNetConnection::~_SystemNetConnection(void)
76 _SystemNetConnection::Initialize(const String& name)
80 unique_ptr<_NetConnectionInfoImpl> pConnectionInfo(new (std::nothrow) _NetConnectionInfoImpl());
81 SysTryReturnResult(NID_NET, pConnectionInfo != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
83 unique_ptr<ArrayList> pEventList(new (std::nothrow) ArrayList());
84 SysTryReturnResult(NID_NET, pEventList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
86 r = pEventList->Construct();
87 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
89 unique_ptr<Mutex> pLock(new (std::nothrow) Mutex());
90 SysTryReturnResult(NID_NET, pLock != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
92 r = pLock->Create(name);
93 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
96 _pConnectionInfo = move(pConnectionInfo);
97 _pEventList = move(pEventList);
104 _SystemNetConnection::Deinitialize(void)
106 _bearerType = NET_BEARER_NONE;
107 _connectionState = NET_CONNECTION_STATE_NONE;
109 _pConnectionInfo.reset(null);
110 _pEventList.reset(null);
116 _SystemNetConnection::AddEvent(_NetConnectionEvent& event)
118 result r = E_SUCCESS;
119 bool isFound = false;
121 SysAssertf(_pEventList != null, "Not yet constructed. Construct() should be called before use.");
123 MutexGuard locked(*_pLock);
125 isFound = _pEventList->Contains(event);
128 r = _pEventList->Add(event);
137 _SystemNetConnection::RemoveEvent(_NetConnectionEvent& event)
139 result r = E_SUCCESS;
140 bool isFound = false;
142 SysAssertf(_pEventList != null, "Not yet constructed. Construct() should be called before use.");
144 MutexGuard locked(*_pLock);
146 isFound = _pEventList->Contains(event);
149 event.SetConnectionState(NET_CONNECTION_STATE_NONE);
150 r = _pEventList->Remove(event, false);
159 _SystemNetConnection::Start(_NetConnectionEvent& event)
161 result r = E_SUCCESS;
163 SysAssertf(_pEventList != null, "Not yet constructed. Construct() should be called before use.");
165 MutexGuard locked(*_pLock);
167 if (_connectionState == NET_CONNECTION_STATE_STARTED)
169 SysLog(NID_NET, "[%ls] is already connected.", _name.GetPointer());
171 if (event.GetConnectionState() != NET_CONNECTION_STATE_STARTED)
173 unique_ptr<_NetConnectionEventArg> pEventArg(new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, E_SUCCESS));
174 SysTryReturnResult(NID_NET, pEventArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
176 event.SetConnectionState(NET_CONNECTION_STATE_STARTED);
177 r = event.FireAsync(*pEventArg);
178 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
186 SysLogException(NID_NET, E_INVALID_CONNECTION,
187 "[%s] %ls is NOT connected.", GetErrorMessage(E_INVALID_CONNECTION), _name.GetPointer());
188 r = E_INVALID_CONNECTION;
197 _SystemNetConnection::Stop(_NetConnectionEvent& event, bool waitingEvent)
199 result r = E_SUCCESS;
201 SysAssertf(_pEventList != null, "Not yet constructed. Construct() should be called before use.");
203 MutexGuard locked(*_pLock);
205 if ((event.GetConnectionState() != NET_CONNECTION_STATE_STOPPED) &&
206 (event.GetConnectionState() != NET_CONNECTION_STATE_NONE))
210 unique_ptr<_NetConnectionEventArg> pEventArg(new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, E_SUCCESS));
211 SysTryReturnResult(NID_NET, pEventArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
213 event.SetConnectionState(NET_CONNECTION_STATE_STOPPED);
214 r = event.FireAsync(*pEventArg);
215 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
229 _SystemNetConnection::HandleStartResponse(result error, void* pData)
231 SysLog(NID_NET, "[%ls] Ignore HandleStartResponse(%s,0x%x).", _name.GetPointer(), GetErrorMessage(error), pData);
235 _SystemNetConnection::HandleStopResponse(void)
237 SysLog(NID_NET, "[%ls] Ignore HandleStopResponse.", _name.GetPointer());
241 _SystemNetConnection::HandleStartEvent(void)
243 _NetConnectionEvent* pEvent = null;
244 _NetConnectionEventArg* pEventArg = null;
246 if (_connectionState == NET_CONNECTION_STATE_STARTED)
248 SysLog(NID_NET, "[%ls] Ignore HandleStartEvent() because this instance is in started state.", _name.GetPointer());
252 SysLog(NID_NET, "[%ls] Notify started event.", _name.GetPointer());
254 MutexGuard locked(*_pLock);
256 unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
259 while (pEnum->MoveNext() == E_SUCCESS)
261 pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
264 if (pEvent->GetConnectionState() == NET_CONNECTION_STATE_STARTING)
266 pEvent->SetConnectionState(NET_CONNECTION_STATE_STARTED);
267 pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, E_SUCCESS);
268 if (pEventArg != null)
270 pEvent->FireAsync(*pEventArg);
281 _SystemNetConnection::HandleStopEvent(result error)
283 _NetConnectionEvent* pEvent = null;
284 _NetConnectionEventArg* pEventArg = null;
286 if ((_connectionState == NET_CONNECTION_STATE_NONE) || (_connectionState == NET_CONNECTION_STATE_STOPPED))
288 SysLog(NID_NET, "[%ls] Ignore HandleStopEvent(%s) because this instance is in stopped state.", _name.GetPointer(), GetErrorMessage(error));
292 SysLog(NID_NET, "[%ls] Notify stopped event.", _name.GetPointer());
294 MutexGuard locked(*_pLock);
298 unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
301 while (pEnum->MoveNext() == E_SUCCESS)
303 pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
306 if ((pEvent->GetConnectionState() != NET_CONNECTION_STATE_NONE) &&
307 (pEvent->GetConnectionState() != NET_CONNECTION_STATE_STOPPED))
309 pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
310 pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, error);
311 if (pEventArg != null)
313 pEvent->FireAsync(*pEventArg);
324 _SystemNetConnection::QueryConnectionState(String& devName) const
326 if (_pConnectionInfo != null)
328 devName = _pConnectionInfo->GetDeviceName();
331 SysLog(NID_NET, "QueryConnectionState() has done with state:%d, devName:%ls", _connectionState, devName.GetPointer());
333 return _connectionState;
337 _SystemNetConnection::GetBearerType(void) const
343 _SystemNetConnection::GetConnectionState(void) const
345 return _connectionState;
349 _SystemNetConnection::GetProxyAddress(void) const
353 if (_pConnectionInfo != null)
355 proxyAddress = _pConnectionInfo->GetProxyAddress();
358 SysLog(NID_NET, "GetProxyAddress() has done with proxy:%ls", proxyAddress.GetPointer());
364 _SystemNetConnection::GetConnectionInfo(NetConnectionInfo& netConnectionInfo)
366 _NetConnectionInfoImpl* pSource = _pConnectionInfo.get();
367 _NetConnectionInfoImpl* pDestnation = _NetConnectionInfoImpl::GetInstance(netConnectionInfo);
369 SysTryReturnVoidResult(NID_NET, ((pSource != null) && (pDestnation != null)), E_INVALID_ARG,
370 "[%s] Invalid argument is used. The impl instance is null. [0x%x] [0x%x]", GetErrorMessage(E_INVALID_ARG), pSource, pDestnation);
372 MutexGuard locked(*_pLock);
374 pDestnation->CopyFrom(pSource);
380 _SystemNetConnection::InitializeNetworkFramework(void)
382 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
384 if (!__isInitialized)
387 pthread_once(&onceBlock, InitializeNetworkFrameworkOnce);
388 result r = GetLastResult();
391 onceBlock = PTHREAD_ONCE_INIT;
397 _SystemNetConnection::InitializeNetworkFrameworkOnce(void)
399 result r = E_SUCCESS;
401 __pPsSystemConnectionMap = new (std::nothrow) HashMap();
402 SysTryReturnVoidResult(NID_NET, __pPsSystemConnectionMap != null, E_OUT_OF_MEMORY,
403 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
405 r = __pPsSystemConnectionMap->Construct();
406 r = TransExceptionsExclusive(r, E_SYSTEM, E_OUT_OF_MEMORY);
407 SysTryReturnVoidResult(NID_NET, r == E_SUCCESS, r,
408 "[%s] A system error has been occurred. Failed to initialize the HashMap.", GetErrorMessage(r));
410 r = _NetAccountDatabase::InitializeRepository();
411 r = TransExceptionsExclusive(r, E_SYSTEM, E_OUT_OF_MEMORY);
412 SysTryReturnVoidResult(NID_NET, r == E_SUCCESS, r,
413 "[%s] A system error has been occurred. Failed to initialize the network database.", GetErrorMessage(r));
415 __isInitialized = true;
418 _SystemNetConnection*
419 _SystemNetConnection::GetPsInstance(NetAccountId netAccountId)
421 result r = E_SUCCESS;
422 _PsSystemNetConnection* pConnection = null;
425 r = _NetAccountDatabase::GetProfileName(netAccountId, profileName);
426 SysTryReturn(NID_NET, r == E_SUCCESS, null, E_INVALID_ACCOUNT,
427 "[%s] Invalid network account. accountId=%d", GetErrorMessage(E_INVALID_ACCOUNT), netAccountId);
429 Mutex* pLock = _NetUtility::GetLock();
430 MutexGuard locked(*pLock);
432 pConnection = dynamic_cast<_PsSystemNetConnection*>(__pPsSystemConnectionMap->GetValue(profileName));
433 if (pConnection == null)
435 pConnection = new (std::nothrow) _PsSystemNetConnection();
436 SysTryReturn(NID_NET, pConnection != null, null, E_OUT_OF_MEMORY,
437 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
439 r = pConnection->Construct(profileName);
442 SysLogException(NID_NET, r, "[%s] Propagating.", GetErrorMessage(r));
449 r = __pPsSystemConnectionMap->Add(pConnection->__profileName, *pConnection);
452 SysLogException(NID_NET, r, "[%s] Propagating.", GetErrorMessage(r));
465 _SystemNetConnection*
466 _SystemNetConnection::GetDefaultInstance(void)
468 result r = E_SUCCESS;
470 Mutex* pLock = _NetUtility::GetLock();
471 MutexGuard locked(*pLock);
473 if (__pDefaultConnection == null)
475 unique_ptr<_DefaultSystemNetConnection> pDefaultConnection(new (std::nothrow) _DefaultSystemNetConnection());
476 SysTryReturn(NID_NET, pDefaultConnection != null, null, E_OUT_OF_MEMORY,
477 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
479 r = pDefaultConnection->Construct();
480 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
482 __pDefaultConnection = pDefaultConnection.release();
487 return __pDefaultConnection;
490 _SystemNetConnection*
491 _SystemNetConnection::GetWifiInstance(void)
493 result r = E_SUCCESS;
495 Mutex* pLock = _NetUtility::GetLock();
496 MutexGuard locked(*pLock);
498 if (__pWifiConnection == null)
500 unique_ptr<_WifiSystemNetConnection> pWifiConnection(new (std::nothrow) _WifiSystemNetConnection());
501 SysTryReturn(NID_NET, pWifiConnection != null, null, E_OUT_OF_MEMORY,
502 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
504 r = pWifiConnection->Construct();
505 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
507 __pWifiConnection = pWifiConnection.release();
512 return __pWifiConnection;
515 _SystemNetConnection*
516 _SystemNetConnection::GetWifiDirectInstance(void)
518 result r = E_SUCCESS;
520 Mutex* pLock = _NetUtility::GetLock();
521 MutexGuard locked(*pLock);
523 // ToDo - Should be changed similar with other instance. (After resolving circular dependency.)
524 if (__pWifiDirectConnection == null)
526 __pWifiDirectConnection = new (std::nothrow) _WifiDirectSystemNetConnection();
527 SysTryReturn(NID_NET, __pWifiDirectConnection != null, null, E_OUT_OF_MEMORY,
528 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
530 r = __pWifiDirectConnection->Construct();
533 SysLogException(NID_NET, r, "[%s] Propagating.", GetErrorMessage(r));
535 delete __pWifiDirectConnection;
536 __pWifiDirectConnection = null;
542 return __pWifiDirectConnection;
545 _SystemNetConnection*
546 _SystemNetConnection::GetUsbInstance(void)
548 result r = E_SUCCESS;
550 Mutex* pLock = _NetUtility::GetLock();
551 MutexGuard locked(*pLock);
553 if (__pUsbConnection == null)
555 unique_ptr<_UsbSystemNetConnection> pUsbConnection(new (std::nothrow) _UsbSystemNetConnection());
556 SysTryReturn(NID_NET, pUsbConnection != null, null, E_OUT_OF_MEMORY,
557 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
559 r = pUsbConnection->Construct();
560 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
562 __pUsbConnection = pUsbConnection.release();
567 return __pUsbConnection;
572 _SystemNetConnection::ConvertError(int ret)
574 result r = E_SUCCESS;
582 case NET_ERR_TIME_OUT:
586 case NET_ERR_NO_SERVICE:
587 r = E_NETWORK_UNAVAILABLE;
590 case NET_ERR_NO_ACTIVE_CONNECTIONS:
591 r = E_SERVICE_UNAVAILABLE;
594 case NET_ERR_CONNECTION_LOGIN_FAILED:
595 case NET_ERR_CONNECTION_AUTH_FAILED:
596 case NET_ERR_CONNECTION_INVALID_KEY:
597 r = E_AUTHENTICATION;
600 case NET_ERR_NOT_SUPPORTED:
601 case NET_ERR_SECURITY_RESTRICTED:
602 r = E_UNSUPPORTED_OPERATION;
605 case NET_ERR_CONNECTION_OUT_OF_RANGE:
606 case NET_ERR_CONNECTION_PIN_MISSING:
607 case NET_ERR_CONNECTION_CONNECT_FAILED:
608 case NET_ERR_OPERATION_ABORTED:
609 case NET_ERR_ACCESS_DENIED:
610 r = E_NETWORK_FAILED;
613 case NET_ERR_CONNECTION_DHCP_FAILED:
617 case NET_ERR_WIFI_DRIVER_FAILURE: