Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNet_NetConnectionManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://floralicense.org/license/
10 //
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.
16 //
17
18 /**
19  * @file                FNet_NetConnectionManagerImpl.cpp
20  * @brief               This is the implementation for the _NetConnectionManagerImpl class.
21  */
22
23 #include <pthread.h>
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"
41
42 using namespace std;
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Collection;
45 using namespace Tizen::Base::Runtime;
46
47 namespace Tizen { namespace Net
48 {
49
50 static const int _NET_CONNECTION_LINGER_TIMER_VALUE = 10000;    // 10sec
51
52 _NetConnectionManagerImpl* _NetConnectionManagerImpl::__pInstance = null;
53
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)
65 {
66 }
67
68 _NetConnectionManagerImpl::~_NetConnectionManagerImpl()
69 {
70         if (__pManagedNetConnectionTimer != null)
71         {
72                 delete __pManagedNetConnectionTimer;
73                 __pManagedNetConnectionTimer = null;
74         }
75
76         if (__pManagedNetConnectionEvent != null)
77         {
78                 if (__pManagedSystemNetConnection != null)
79                 {
80                         __pManagedSystemNetConnection->Stop(*__pManagedNetConnectionEvent, false);
81                         __pManagedSystemNetConnection = null;
82                 }
83
84                 __pManagedNetConnectionEvent->Release();
85                 __pManagedNetConnectionEvent = null;
86         }
87 }
88
89 result
90 _NetConnectionManagerImpl::Construct()
91 {
92         result r = E_SUCCESS;
93         _SystemNetConnection* pDefaultConnection = null;
94         _SystemNetConnection* pWifiConnection = null;
95         _SystemNetConnection* pManagedSystemNetConnection = null;
96
97         _SystemNetConnection::InitializeNetworkFramework();
98
99         unique_ptr<Timer> pManagedNetConnectionTimer(new (std::nothrow) Timer());
100         SysTryReturnResult(NID_NET, pManagedNetConnectionTimer != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
101
102         r = pManagedNetConnectionTimer->Construct(*this);
103         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
104
105         unique_ptr<_NetConnectionEvent> pManagedNetConnectionEvent(new (std::nothrow) _NetConnectionEvent());
106         SysTryReturnResult(NID_NET, pManagedNetConnectionEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
107
108         r = pManagedNetConnectionEvent->Construct();
109         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
110
111         pDefaultConnection = _SystemNetConnection::GetDefaultInstance();
112         SysTryReturnResult(NID_NET, pDefaultConnection != null, E_SYSTEM, "Failed to get a default connection.");
113
114 #ifndef _OSP_EMUL_
115                 pWifiConnection = _SystemNetConnection::GetWifiInstance();
116                 SysTryReturnResult(NID_NET, pWifiConnection != null, E_SYSTEM, "Failed to get a wifi connection.");
117 #endif // !(_OSP_EMUL_)
118
119         pManagedSystemNetConnection = pDefaultConnection;
120
121         r = pManagedSystemNetConnection->AddEvent(*pManagedNetConnectionEvent);
122         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
123
124         pManagedNetConnectionEvent->SetSystemConnection(pManagedSystemNetConnection);
125
126         __pManagedSystemNetConnection = pManagedSystemNetConnection;
127         __pManagedNetConnectionEvent = pManagedNetConnectionEvent.release();
128         __pManagedNetConnectionTimer = pManagedNetConnectionTimer.release();
129         __pDefaultConnection = pDefaultConnection;
130         __pWifiConnection = pWifiConnection;
131
132         return r;
133 }
134
135 void
136 _NetConnectionManagerImpl::OnTimerExpired(Timer& timer)
137 {
138         if ((__pManagedSystemNetConnection != null) &&(__managedNetRefCount == 0))
139         {
140                 __pManagedSystemNetConnection->Stop(*__pManagedNetConnectionEvent);
141         }
142 }
143
144 NetConnection*
145 _NetConnectionManagerImpl::CreateNetConnectionN(NetAccountId netAccountId)
146 {
147         SysLog(NID_NET, "CreateNetConnectionN() has been called with accountId:%d", netAccountId);
148
149         result r = E_SUCCESS;
150
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);
157
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));
161
162         r = pConnection->Construct(netAccountId);
163         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
164
165         ClearLastResult();
166
167         return pConnection.release();
168 }
169
170 ManagedNetConnection*
171 _NetConnectionManagerImpl::GetManagedNetConnectionN(void) const
172 {
173         ClearLastResult();
174
175         ManagedNetConnection* pConnection = null;
176
177         pConnection = _ManagedNetConnectionImpl::CreateInstanceN();
178
179         return pConnection;
180 }
181
182 NetPreferenceType
183 _NetConnectionManagerImpl::GetNetPreference(void) const
184 {
185         ClearLastResult();
186
187         return __managedNetPreference;
188 }
189
190 result
191 _NetConnectionManagerImpl::SetNetPreference(NetPreferenceType netPreference)
192 {
193         result r = E_SUCCESS;
194 #ifndef _OSP_EMUL_
195         _SystemNetConnection* pNewConnection = null;
196 #endif // !_OSP_EMUL_
197
198         SysLog(NID_NET, "SetNetPreference() has been called with preference:%d", netPreference);
199
200         SysTryReturnResult(NID_NET, __managedNetRefCount == 0, E_INVALID_OPERATION, "The managed network connection is used.");
201
202         if (netPreference == NET_PS_ONLY)
203         {
204                 if (__managedNetAccountId == _DEFAULT_PS_ACCOUNT_ID)
205                 {
206                         __pPsConnection = _SystemNetConnection::GetPsInstance(_NetAccountManagerImpl::GetInternetAccountId());
207                 }
208                 else
209                 {
210                         __pPsConnection = _SystemNetConnection::GetPsInstance(__managedNetAccountId);
211                 }
212         }
213
214         Mutex* pLock = _NetUtility::GetLock();
215         MutexGuard locked(*pLock);
216
217         __managedNetPreference = netPreference;
218
219         if ((__managedNetPreference == NET_WIFI_FIRST) && (__managedNetAccountId == _DEFAULT_PS_ACCOUNT_ID))
220         {
221                 __isManagedNetDefaultMode = true;
222         }
223         else
224         {
225                 __isManagedNetDefaultMode = false;
226         }
227
228         __pManagedNetConnectionTimer->Cancel();
229
230 #ifndef _OSP_EMUL_
231         if (__isManagedNetDefaultMode)
232         {
233                 pNewConnection = __pDefaultConnection;
234         }
235         else if (__managedNetPreference == NET_WIFI_ONLY)
236         {
237                 pNewConnection = __pWifiConnection;
238         }
239         else if (__managedNetPreference == NET_PS_ONLY)
240         {
241                 pNewConnection = __pPsConnection;
242         }
243         else // NET_WIFI_FIRST
244         {
245                 if (__pWifiConnection->GetConnectionState() == NET_CONNECTION_STATE_STARTED)
246                 {
247                         pNewConnection = __pWifiConnection;
248                 }
249                 else
250                 {
251                         pNewConnection = __pPsConnection;
252                 }
253         }
254
255         if (pNewConnection != __pManagedSystemNetConnection)
256         {
257                 SysLog(NID_NET, "ManagedNetConnection should be changed. [0x%x] -> [0x%x]",
258                                 __pManagedSystemNetConnection, pNewConnection);
259                 if (__pManagedSystemNetConnection != null)
260                 {
261                         __pManagedSystemNetConnection->Stop(*__pManagedNetConnectionEvent, false);
262                         __pManagedSystemNetConnection->RemoveEvent(*__pManagedNetConnectionEvent);
263                         __pManagedSystemNetConnection = null;
264                         __pManagedNetConnectionEvent->SetConnectionState(NET_CONNECTION_STATE_NONE);
265                         __pManagedNetConnectionEvent->SetSystemConnection(null);
266                 }
267
268                 if (pNewConnection != null)
269                 {
270                         __pManagedSystemNetConnection = pNewConnection;
271                         __pManagedSystemNetConnection->AddEvent(*__pManagedNetConnectionEvent);
272                         __pManagedNetConnectionEvent->SetSystemConnection(__pManagedSystemNetConnection);
273                 }
274         }
275 #endif // !_OSP_EMUL_
276
277         locked.Unlock();
278
279         return r;
280 }
281
282 NetConnectionInfo*
283 _NetConnectionManagerImpl::GetNetConnectionInfoN(NetAccountId netAccountId)
284 {
285         _NetConnectionManagerImpl* pThis = _NetConnectionManagerImpl::GetInstance();
286
287         ClearLastResult();
288
289         result r = E_SUCCESS;
290         NetConnectionInfo* pInfo = null;
291         _SystemNetConnection* pSystemNetConnection = null;
292
293         SysTryReturn(NID_NET, pThis != null, null, E_SYSTEM,
294                         "[%s] A system error has been occurred. System is not ready.", GetErrorMessage(E_SYSTEM));
295
296         if (netAccountId == _DEFAULT_WIFI_ACCOUNT_ID)
297         {
298                 pSystemNetConnection = _SystemNetConnection::GetWifiInstance();
299         }
300         else if (netAccountId == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID)
301         {
302                 pSystemNetConnection = _SystemNetConnection::GetWifiDirectInstance();
303         }
304         else if (netAccountId == _DEFAULT_USB_ACCOUNT_ID)
305         {
306                 pSystemNetConnection = _SystemNetConnection::GetUsbInstance();
307         }
308
309         if (pSystemNetConnection != null)
310         {
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));
314
315                 pSystemNetConnection->GetConnectionInfo(*pInfo);
316         }
317         else
318         {
319                 String profileName;
320                 connection_profile_h profileHandle = null;
321
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);
325
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);
329
330                 pInfo = _NetConnectionInfoImpl::CreateNetConnectionInfoN(profileHandle);
331                 connection_profile_destroy(profileHandle);
332         }
333
334         return pInfo;
335 }
336
337 IList*
338 _NetConnectionManagerImpl::GetAllNetConnectionInfoN(void)
339 {
340         _NetConnectionManagerImpl* pThis = _NetConnectionManagerImpl::GetInstance();
341         NetConnectionInfo* pInfo = null;
342
343         ClearLastResult();
344
345         SysTryReturn(NID_NET, pThis != null, null, E_SYSTEM,
346                         "[%s] Asystem error has been occurred. System is not ready.", GetErrorMessage(E_SYSTEM));
347
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));
350
351 #ifdef _OSP_EMUL_
352         const NetConnectionInfo* pManagedNetConnectionInfo = pThis->GetManagedNetConnectionInfo();
353         pInfo = new (std::nothrow) NetConnectionInfo(*pManagedNetConnectionInfo);
354         if (pInfo != null)
355         {
356                 pList->Add(*pInfo);
357         }
358 #else // _OSP_EMUL_
359         int ret = CONNECTION_ERROR_NONE;
360         connection_h connectionHandle = null;
361         connection_profile_iterator_h iterator = null;
362         connection_profile_h profileHandle = null;
363
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);
369
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);
375
376         ret = connection_profile_iterator_next(iterator, &profileHandle);
377         while ((ret == CONNECTION_ERROR_NONE) && (profileHandle != null))
378         {
379                 pInfo = _NetConnectionInfoImpl::CreateNetConnectionInfoN(profileHandle);
380                 if (pInfo != null)
381                 {
382                         pList->Add(*pInfo);
383                 }
384
385                 profileHandle = null;
386                 ret = connection_profile_iterator_next(iterator, &profileHandle);
387         }
388 #endif // _OSP_EMUL_
389
390         SysLog(NID_NET, "GetAllNetConnectionInfoN() has been succeeded with result:%s, count:%d",
391                         GetErrorMessage(GetLastResult()), pList->GetCount());
392
393         return pList.release();
394 }
395
396 bool
397 _NetConnectionManagerImpl::IsDefaultMode(void)
398 {
399         bool ret = false;
400         _NetConnectionManagerImpl* pThis = _NetConnectionManagerImpl::GetInstance();
401
402         SysLog(NID_NET, "Preference:%d, AccountId:%d", pThis->__managedNetPreference, pThis->__managedNetAccountId);
403
404         ret = pThis->__isManagedNetDefaultMode;
405
406         SysLog(NID_NET, "IsDefaultMode() has done with ret(%d).", ret);
407
408         return ret;
409 }
410
411 String
412 _NetConnectionManagerImpl::GetProxyAddress(void)
413 {
414         _NetConnectionManagerImpl* pThis = _NetConnectionManagerImpl::GetInstance();
415         String proxyAddress;
416
417         proxyAddress.Clear();
418
419         if (pThis != null)
420         {
421                 if (pThis->__pManagedSystemNetConnection != null)
422                 {
423                         proxyAddress = pThis->__pManagedSystemNetConnection->GetProxyAddress();
424                 }
425         }
426
427         SysLog(NID_NET, "The current value of ProxyAddress is %ls", proxyAddress.GetPointer());
428
429         return proxyAddress;
430 }
431
432 _NetConnectionManagerImpl*
433 _NetConnectionManagerImpl::GetInstance(void)
434 {
435         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
436
437         if (__pInstance == null)
438         {
439                 ClearLastResult();
440                 pthread_once(&onceBlock, InitSingleton);
441                 result r = GetLastResult();
442                 if (r != E_SUCCESS)
443                 {
444                         onceBlock = PTHREAD_ONCE_INIT;
445                 }
446         }
447
448         return __pInstance;
449 }
450
451 void
452 _NetConnectionManagerImpl::InitSingleton(void)
453 {
454     result r = E_SUCCESS;
455     static _NetConnectionManagerImpl instance;
456
457     r = instance.Construct();
458         SysTryReturnVoidResult(NID_NET, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
459
460         ClearLastResult();
461         __pInstance = &instance;
462 }
463
464 result
465 _NetConnectionManagerImpl::SetManagedNetAccountId(NetAccountId netAccountId)
466 {
467         result r = E_SUCCESS;
468         String accountName;
469
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);
473
474         r = _NetAccountDatabase::GetAccountName(netAccountId, accountName);
475         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is used. accountId=%d", netAccountId);
476
477         __pPsConnection = _SystemNetConnection::GetPsInstance(netAccountId);
478         SysTryReturnResult(NID_NET, __pPsConnection != null, E_INVALID_ARG, "Invalid argument is used. accountId=%d", netAccountId);
479
480         Mutex* pLock = _NetUtility::GetLock();
481         MutexGuard locked(*pLock);
482
483         __managedNetAccountId = netAccountId;
484
485         if ((__managedNetPreference == NET_WIFI_FIRST) && (__managedNetAccountId == _DEFAULT_PS_ACCOUNT_ID))
486         {
487                 __isManagedNetDefaultMode = true;
488         }
489         else
490         {
491                 __isManagedNetDefaultMode = false;
492         }
493
494         locked.Unlock();
495
496         return r;
497 }
498
499 NetAccountId
500 _NetConnectionManagerImpl::GetManagedNetAccountId(void) const
501 {
502         NetAccountId accountId = INVALID_HANDLE;
503
504         SysTryReturn(NID_NET, __pManagedSystemNetConnection != null, INVALID_HANDLE,
505                         E_INVALID_STATE, "[%s] Managed network connection is inactive.", GetErrorMessage(E_INVALID_STATE));
506
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));
509
510         if (__pManagedSystemNetConnection->GetBearerType() == NET_BEARER_WIFI)
511         {
512                 accountId = _DEFAULT_WIFI_ACCOUNT_ID;
513         }
514         else
515         {
516                 if (__managedNetAccountId == _DEFAULT_PS_ACCOUNT_ID)
517                 {
518                         accountId = _NetAccountManagerImpl::GetInternetAccountId();
519                 }
520                 else
521                 {
522                         accountId = __managedNetAccountId;
523                 }
524         }
525
526         ClearLastResult();
527
528         return accountId;
529 }
530
531 NetConnectionState
532 _NetConnectionManagerImpl::GetManagedNetConnectionState(void) const
533 {
534         if ((__isManagedNetDefaultMode) && (__pManagedSystemNetConnection != null))
535         {
536                 return __pManagedSystemNetConnection->GetConnectionState();
537         }
538         else
539         {
540                 return __pManagedNetConnectionEvent->GetConnectionState();
541         }
542 }
543
544 NetConnectionState
545 _NetConnectionManagerImpl::QueryManagedNetConnectionState(String& devName) const
546 {
547         if (__pManagedSystemNetConnection != null)
548         {
549                 return __pManagedSystemNetConnection->QueryConnectionState(devName);
550         }
551         else
552         {
553                 return __pManagedNetConnectionEvent->GetConnectionState();
554         }
555 }
556
557 const NetConnectionInfo*
558 _NetConnectionManagerImpl::GetManagedNetConnectionInfo(void)
559 {
560         _NetConnectionInfoImpl* pInfoImpl = null;
561
562         if ((__pManagedSystemNetConnection != null) &&
563                 (GetManagedNetConnectionState() == NET_CONNECTION_STATE_STARTED))
564         {
565                 __pManagedSystemNetConnection->GetConnectionInfo(__managedNetConnectionInfo);
566         }
567         else
568         {
569                 pInfoImpl = _NetConnectionInfoImpl::GetInstance(__managedNetConnectionInfo);
570                 if (pInfoImpl != null)
571                 {
572                         pInfoImpl->Clear();
573                 }
574         }
575
576         return &__managedNetConnectionInfo;
577 }
578
579 result
580 _NetConnectionManagerImpl::AddManagedNetConnectionEventListener(IManagedNetConnectionEventListener& listener,
581                 const ManagedNetConnection* pConnection)
582 {
583         result r = E_SUCCESS;
584
585         r = __pManagedNetConnectionEvent->AddNetConnectionEventListener(listener, pConnection);
586
587         SysLog(NID_NET, "AddManagedNetConnectionEventListener() has done with result:%s", GetErrorMessage(r));
588
589         return r;
590 }
591
592 result
593 _NetConnectionManagerImpl::RemoveManagedNetConnectionEventListener(IManagedNetConnectionEventListener& listener)
594 {
595         result r = E_SUCCESS;
596
597         r = __pManagedNetConnectionEvent->RemoveNetConnectionEventListener(listener);
598
599         SysLog(NID_NET, "RemoveManagedNetConnectionEventListener() has done with result:%s", GetErrorMessage(r));
600
601         return r;
602 }
603
604 result
605 _NetConnectionManagerImpl::AddRefManagedNetConnection(void)
606 {
607         result r = E_SUCCESS;
608 #ifndef _OSP_EMUL_
609         _SystemNetConnection* pNewConnection = null;
610 #endif // !_OSP_EMUL_
611
612         Mutex* pLock = _NetUtility::GetLock();
613         MutexGuard locked(*pLock);
614
615 #ifdef _OSP_EMUL_
616         r = __pManagedSystemNetConnection->Start(*__pManagedNetConnectionEvent);
617 #else // _OSP_EMUL_
618         if (__managedNetRefCount == 0)
619         {
620                 __pManagedNetConnectionTimer->Cancel();
621
622                 if (IsDefaultMode())
623                 {
624                         pNewConnection = __pDefaultConnection;
625                 }
626                 else if (__managedNetPreference == NET_WIFI_ONLY)
627                 {
628                         pNewConnection = __pWifiConnection;
629                 }
630                 else if (__managedNetPreference == NET_PS_ONLY)
631                 {
632                         pNewConnection = __pPsConnection;
633                 }
634                 else // NET_WIFI_FIRST
635                 {
636                         if (__pWifiConnection->GetConnectionState() == NET_CONNECTION_STATE_STARTED)
637                         {
638                                 pNewConnection = __pWifiConnection;
639                         }
640                         else
641                         {
642                                 pNewConnection = __pPsConnection;
643                         }
644                 }
645         }
646         else
647         {
648                 pNewConnection = __pManagedSystemNetConnection;
649         }
650
651         if (pNewConnection != __pManagedSystemNetConnection)
652         {
653                 SysLog(NID_NET, "ManagedNetConnection should be changed. [0x%x] -> [0x%x]",
654                                 __pManagedSystemNetConnection, pNewConnection);
655                 if (__pManagedSystemNetConnection != null)
656                 {
657                         __pManagedSystemNetConnection->Stop(*__pManagedNetConnectionEvent, false);
658                         __pManagedSystemNetConnection->RemoveEvent(*__pManagedNetConnectionEvent);
659                         __pManagedSystemNetConnection = null;
660                         __pManagedNetConnectionEvent->SetConnectionState(NET_CONNECTION_STATE_NONE);
661                         __pManagedNetConnectionEvent->SetSystemConnection(null);
662                 }
663
664                 if (pNewConnection != null)
665                 {
666                         __pManagedSystemNetConnection = pNewConnection;
667                         __pManagedSystemNetConnection->AddEvent(*__pManagedNetConnectionEvent);
668                         __pManagedNetConnectionEvent->SetSystemConnection(__pManagedSystemNetConnection);
669                 }
670         }
671
672         if (__pManagedSystemNetConnection != null)
673         {
674                 SysLog(NID_NET, "Start a ManagedNetConnection.");
675                 r = __pManagedSystemNetConnection->Start(*__pManagedNetConnectionEvent);
676         }
677         else
678         {
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);
682         }
683 #endif  // !_OSP_EMUL_
684
685         if (r == E_SUCCESS)
686         {
687                 __managedNetRefCount++;
688         }
689
690         locked.Unlock();
691
692         SysLog(NID_NET, "AddRefManagedNetConnection() has done with result:%s", GetErrorMessage(r));
693
694         return r;
695 }
696
697 void
698 _NetConnectionManagerImpl::ReleaseManagedNetConnection(void)
699 {
700         result r = E_SUCCESS;
701
702         Mutex* pLock = _NetUtility::GetLock();
703         MutexGuard locked(*pLock);
704
705         __managedNetRefCount--;
706
707         if (__managedNetRefCount == 0)
708         {
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);
712         }
713
714         locked.Unlock();
715 }
716
717 _NetConnectionManagerImpl*
718 _NetConnectionManagerImpl::GetInstance(NetConnectionManager& netConnectionManager)
719 {
720         return netConnectionManager.__pNetConnectionManagerImpl;
721 }
722
723 const _NetConnectionManagerImpl*
724 _NetConnectionManagerImpl::GetInstance(const NetConnectionManager& netConnectionManager)
725 {
726         return netConnectionManager.__pNetConnectionManagerImpl;
727 }
728
729 } } // Tizen::Net