merge with master
[framework/osp/net.git] / src / FNet_SystemNetConnection.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
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  * @file                        FNet_SystemNetConnection.cpp
19  * @brief               This is the implementation file for _SystemNetConnection class.
20  * @version     3.0
21  *
22  * This file contains the implementation of _SystemNetConnection class.
23  */
24
25 #include <pthread.h>
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"
45
46 using namespace std;
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Collection;
49 using namespace Tizen::Base::Runtime;
50 using namespace Tizen::Io;
51
52 namespace Tizen { namespace Net {
53
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;
60
61 _SystemNetConnection::_SystemNetConnection(void)
62         : _bearerType(NET_BEARER_NONE)
63         , _connectionState(NET_CONNECTION_STATE_NONE)
64         , _pConnectionInfo(null)
65         , _pEventList(null)
66         , _pLock(null)
67         , _refCount(0)
68 {
69 }
70
71 _SystemNetConnection::~_SystemNetConnection(void)
72 {
73 }
74
75 result
76 _SystemNetConnection::Initialize(const String& name)
77 {
78         result r = E_SUCCESS;
79
80         unique_ptr<_NetConnectionInfoImpl> pConnectionInfo(new (std::nothrow) _NetConnectionInfoImpl());
81         SysTryReturnResult(NID_NET, pConnectionInfo != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
82
83         unique_ptr<ArrayList> pEventList(new (std::nothrow) ArrayList());
84         SysTryReturnResult(NID_NET, pEventList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
85
86         r = pEventList->Construct();
87         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
88
89         unique_ptr<Mutex> pLock(new (std::nothrow) Mutex());
90         SysTryReturnResult(NID_NET, pLock != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
91
92         r = pLock->Create(name);
93         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
94
95         _name = name;
96         _pConnectionInfo = move(pConnectionInfo);
97         _pEventList = move(pEventList);
98         _pLock = move(pLock);
99
100         return r;
101 }
102
103 void
104 _SystemNetConnection::Deinitialize(void)
105 {
106         _bearerType = NET_BEARER_NONE;
107         _connectionState = NET_CONNECTION_STATE_NONE;
108         _name.Clear();
109         _pConnectionInfo.reset(null);
110         _pEventList.reset(null);
111         _pLock.reset(null);
112         _refCount = 0;
113 }
114
115 result
116 _SystemNetConnection::AddEvent(_NetConnectionEvent& event)
117 {
118         result r = E_SUCCESS;
119         bool isFound = false;
120
121         SysAssertf(_pEventList != null, "Not yet constructed. Construct() should be called before use.");
122
123         MutexGuard locked(*_pLock);
124
125         isFound = _pEventList->Contains(event);
126         if (!isFound)
127         {
128                 r = _pEventList->Add(event);
129         }
130
131         locked.Unlock();
132
133         return r;
134 }
135
136 result
137 _SystemNetConnection::RemoveEvent(_NetConnectionEvent& event)
138 {
139         result r = E_SUCCESS;
140         bool isFound = false;
141
142         SysAssertf(_pEventList != null, "Not yet constructed. Construct() should be called before use.");
143
144         MutexGuard locked(*_pLock);
145
146         isFound = _pEventList->Contains(event);
147         if (isFound)
148         {
149                 event.SetConnectionState(NET_CONNECTION_STATE_NONE);
150                 r = _pEventList->Remove(event, false);
151         }
152
153         locked.Unlock();
154
155         return r;
156 }
157
158 result
159 _SystemNetConnection::Start(_NetConnectionEvent& event)
160 {
161         result r = E_SUCCESS;
162
163         SysAssertf(_pEventList != null, "Not yet constructed. Construct() should be called before use.");
164
165         MutexGuard locked(*_pLock);
166
167         if (_connectionState == NET_CONNECTION_STATE_STARTED)
168         {
169                 SysLog(NID_NET, "[%ls] is already connected.", _name.GetPointer());
170
171                 if (event.GetConnectionState() != NET_CONNECTION_STATE_STARTED)
172                 {
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.");
175
176                         event.SetConnectionState(NET_CONNECTION_STATE_STARTED);
177                         r = event.FireAsync(*pEventArg);
178                         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.", GetErrorMessage(r));
179
180                         _refCount++;
181                         pEventArg.release();
182                 }
183         }
184         else
185         {
186                 SysLogException(NID_NET, E_INVALID_CONNECTION,
187                                 "[%s] %ls is NOT connected.", GetErrorMessage(E_INVALID_CONNECTION), _name.GetPointer());
188                 r = E_INVALID_CONNECTION;
189         }
190
191         locked.Unlock();
192
193         return r;
194 }
195
196 result
197 _SystemNetConnection::Stop(_NetConnectionEvent& event, bool waitingEvent)
198 {
199         result r = E_SUCCESS;
200
201         SysAssertf(_pEventList != null, "Not yet constructed. Construct() should be called before use.");
202
203         MutexGuard locked(*_pLock);
204
205         if ((event.GetConnectionState() != NET_CONNECTION_STATE_STOPPED) &&
206                 (event.GetConnectionState() != NET_CONNECTION_STATE_NONE))
207         {
208                 if (waitingEvent)
209                 {
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.");
212
213                         event.SetConnectionState(NET_CONNECTION_STATE_STOPPED);
214                         r = event.FireAsync(*pEventArg);
215                         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
216
217                         pEventArg.release();
218                 }
219
220                 _refCount--;
221         }
222
223         locked.Unlock();
224
225         return r;
226 }
227
228 void
229 _SystemNetConnection::HandleStartResponse(result error, void* pData)
230 {
231         SysLog(NID_NET, "[%ls] Ignore HandleStartResponse(%s,0x%x).", _name.GetPointer(), GetErrorMessage(error), pData);
232 }
233
234 void
235 _SystemNetConnection::HandleStopResponse(void)
236 {
237         SysLog(NID_NET, "[%ls] Ignore HandleStopResponse.", _name.GetPointer());
238 }
239
240 void
241 _SystemNetConnection::HandleStartEvent(void)
242 {
243         _NetConnectionEvent* pEvent = null;
244         _NetConnectionEventArg* pEventArg = null;
245
246         if (_connectionState == NET_CONNECTION_STATE_STARTED)
247         {
248                 SysLog(NID_NET, "[%ls] Ignore HandleStartEvent() because this instance is in started state.", _name.GetPointer());
249                 return;
250         }
251
252         SysLog(NID_NET, "[%ls] Notify started event.", _name.GetPointer());
253
254         MutexGuard locked(*_pLock);
255
256         unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
257         if (pEnum != null)
258         {
259                 while (pEnum->MoveNext() == E_SUCCESS)
260                 {
261                         pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
262                         if (pEvent != null)
263                         {
264                                 if (pEvent->GetConnectionState() == NET_CONNECTION_STATE_STARTING)
265                                 {
266                                         pEvent->SetConnectionState(NET_CONNECTION_STATE_STARTED);
267                                         pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, E_SUCCESS);
268                                         if (pEventArg != null)
269                                         {
270                                                 pEvent->FireAsync(*pEventArg);
271                                         }
272                                 }
273                         }
274                 }
275         }
276
277         locked.Unlock();
278 }
279
280 void
281 _SystemNetConnection::HandleStopEvent(result error)
282 {
283         _NetConnectionEvent* pEvent = null;
284         _NetConnectionEventArg* pEventArg = null;
285
286         if ((_connectionState == NET_CONNECTION_STATE_NONE) || (_connectionState == NET_CONNECTION_STATE_STOPPED))
287         {
288                 SysLog(NID_NET, "[%ls] Ignore HandleStopEvent(%s) because this instance is in stopped state.", _name.GetPointer(), GetErrorMessage(error));
289                 return;
290         }
291
292         SysLog(NID_NET, "[%ls] Notify stopped event.", _name.GetPointer());
293
294         MutexGuard locked(*_pLock);
295
296         _refCount = 0;
297
298         unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
299         if (pEnum != null)
300         {
301                 while (pEnum->MoveNext() == E_SUCCESS)
302                 {
303                         pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
304                         if (pEvent != null)
305                         {
306                                 if ((pEvent->GetConnectionState() != NET_CONNECTION_STATE_NONE) &&
307                                         (pEvent->GetConnectionState() != NET_CONNECTION_STATE_STOPPED))
308                                 {
309                                         pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
310                                         pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, error);
311                                         if (pEventArg != null)
312                                         {
313                                                 pEvent->FireAsync(*pEventArg);
314                                         }
315                                 }
316                         }
317                 }
318         }
319
320         locked.Unlock();
321 }
322
323 NetConnectionState
324 _SystemNetConnection::QueryConnectionState(String& devName) const
325 {
326         if (_pConnectionInfo != null)
327         {
328                 devName = _pConnectionInfo->GetDeviceName();
329         }
330
331         SysLog(NID_NET, "QueryConnectionState() has done with state:%d, devName:%ls", _connectionState, devName.GetPointer());
332
333         return _connectionState;
334 }
335
336 NetBearerType
337 _SystemNetConnection::GetBearerType(void) const
338 {
339         return _bearerType;
340 }
341
342 NetConnectionState
343 _SystemNetConnection::GetConnectionState(void) const
344 {
345         return _connectionState;
346 }
347
348 String
349 _SystemNetConnection::GetProxyAddress(void) const
350 {
351         String proxyAddress;
352
353         if (_pConnectionInfo != null)
354         {
355                 proxyAddress = _pConnectionInfo->GetProxyAddress();
356         }
357
358         SysLog(NID_NET, "GetProxyAddress() has done with proxy:%ls", proxyAddress.GetPointer());
359
360         return proxyAddress;
361 }
362
363 void
364 _SystemNetConnection::GetConnectionInfo(NetConnectionInfo& netConnectionInfo)
365 {
366         _NetConnectionInfoImpl* pSource = _pConnectionInfo.get();
367         _NetConnectionInfoImpl* pDestnation = _NetConnectionInfoImpl::GetInstance(netConnectionInfo);
368
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);
371
372         MutexGuard locked(*_pLock);
373
374         pDestnation->CopyFrom(pSource);
375
376         locked.Unlock();
377 }
378
379 void
380 _SystemNetConnection::InitializeNetworkFramework(void)
381 {
382         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
383
384         if (!__isInitialized)
385         {
386                 ClearLastResult();
387                 pthread_once(&onceBlock, InitializeNetworkFrameworkOnce);
388                 result r = GetLastResult();
389                 if (r != E_SUCCESS)
390                 {
391                         onceBlock = PTHREAD_ONCE_INIT;
392                 }
393         }
394 }
395
396 void
397 _SystemNetConnection::InitializeNetworkFrameworkOnce(void)
398 {
399         result r = E_SUCCESS;
400         NetAccountId accountId = INVALID_HANDLE;
401         String accountName;
402
403         __pPsSystemConnectionMap = new (std::nothrow) HashMap();
404         SysTryReturnVoidResult(NID_NET, __pPsSystemConnectionMap != null, E_OUT_OF_MEMORY,
405                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
406
407         r = __pPsSystemConnectionMap->Construct();
408         r = TransExceptionsExclusive(r, E_SYSTEM, E_OUT_OF_MEMORY);
409         SysTryReturnVoidResult(NID_NET, r == E_SUCCESS, r,
410                         "[%s] A system error has been occurred. Failed to initialize the HashMap.", GetErrorMessage(r));
411
412         r = _NetAccountDatabase::InitializeRepository();
413         r = TransExceptionsExclusive(r, E_SYSTEM, E_OUT_OF_MEMORY);
414         SysTryReturnVoidResult(NID_NET, r == E_SUCCESS, r,
415                         "[%s] A system error has been occurred. Failed to initialize the network database.", GetErrorMessage(r));
416
417         _NetProfileInfo internetProfileInfo = _NetAccountManagerImpl::GetInternetProfileInfo();
418         _NetProfileInfo mmsProfileInfo = _NetAccountManagerImpl::GetMmsProfileInfo();
419         String profileName;
420
421         r = _NetAccountDatabase::GetProfileName(_DEFAULT_PS_ACCOUNT_ID, profileName);
422         if (internetProfileInfo.GetProfileName().IsEmpty())
423         {
424                 SysLog(NID_NET, "Internet profile is not found.");
425
426                 if (!profileName.IsEmpty())
427                 {
428                         SysLog(NID_NET, "Remove Internet profile from database.");
429                         r = _NetAccountDatabase::RemoveAccountByAccountId(_DEFAULT_PS_ACCOUNT_ID);
430                 }
431         }
432         else
433         {
434                 if (internetProfileInfo.GetProfileName() != profileName)
435                 {
436                         SysLog(NID_NET, "Internet profile is changed. [%ls] -> [%ls]", profileName.GetPointer(), internetProfileInfo.GetProfileName().GetPointer());
437
438                         if (!profileName.IsEmpty())
439                         {
440                                 r = _NetAccountDatabase::RemoveAccountByAccountId(_DEFAULT_PS_ACCOUNT_ID);
441                         }
442
443                         r = _NetAccountDatabase::RemoveAccountByProfileName(internetProfileInfo.GetProfileName());
444
445                         accountName.Clear();
446                         accountName.Append(internetProfileInfo.GetProfileDisplayName());
447                         accountName.Append(internetProfileInfo.GetProfileName());
448
449                         r = _NetAccountDatabase::AddAccount(accountName, internetProfileInfo.GetProfileName(), _NET_ACCOUNT_OWNER_SYSTEM_INTERNET, accountId);
450                 }
451         }
452
453         profileName.Clear();
454
455         r = _NetAccountDatabase::GetProfileName(_DEFAULT_MMS_ACCOUNT_ID, profileName);
456         if (mmsProfileInfo.GetProfileName().IsEmpty())
457         {
458                 SysLog(NID_NET, "MMS profile is not found.");
459
460                 if (!profileName.IsEmpty())
461                 {
462                         SysLog(NID_NET, "Remove MMS profile from database.");
463                         r = _NetAccountDatabase::RemoveAccountByAccountId(_DEFAULT_MMS_ACCOUNT_ID);
464                 }
465         }
466         else
467         {
468                 if (mmsProfileInfo.GetProfileName() != profileName)
469                 {
470                         SysLog(NID_NET, "MMS profile is changed. [%ls] -> [%ls]", profileName.GetPointer(), mmsProfileInfo.GetProfileName().GetPointer());
471
472                         if (!profileName.IsEmpty())
473                         {
474                                 r = _NetAccountDatabase::RemoveAccountByAccountId(_DEFAULT_MMS_ACCOUNT_ID);
475                         }
476
477                         r = _NetAccountDatabase::RemoveAccountByProfileName(mmsProfileInfo.GetProfileName());
478
479                         accountName.Clear();
480                         accountName.Append(mmsProfileInfo.GetProfileDisplayName());
481                         accountName.Append(mmsProfileInfo.GetProfileName());
482
483                         r = _NetAccountDatabase::AddAccount(accountName, mmsProfileInfo.GetProfileName(), _NET_ACCOUNT_OWNER_SYSTEM_MMS, accountId);
484                 }
485         }
486
487         unique_ptr<IList, _CollectionDeleter> pAccountList(_NetAccountDatabase::GetProfileNamesN());
488         unique_ptr<IList, _CollectionDeleter> pProfileList(_NetAccountManagerImpl::GetAllProfileInfosN());
489
490         if (pProfileList != null)
491         {
492                 if (pProfileList->GetCount() > 0)
493                 {
494                         SysLog(NID_NET, "[%d] profiles are found.", pProfileList->GetCount());
495
496                         unique_ptr<IEnumerator> pEnum(pProfileList->GetEnumeratorN());
497                         if (pEnum != null)
498                         {
499                                 _NetProfileInfo* pProfileInfo = null;
500                                 while (pEnum->MoveNext() == E_SUCCESS)
501                                 {
502                                         pProfileInfo = dynamic_cast<_NetProfileInfo*>(pEnum->GetCurrent());
503                                         if (pProfileInfo != null)
504                                         {
505                                                 if (pAccountList->Contains(pProfileInfo->GetProfileName()))
506                                                 {
507                                                         SysLog(NID_NET, "ProfileName[%ls] is already on DB.", pProfileInfo->GetProfileName().GetPointer());
508                                                         pAccountList->Remove(pProfileInfo->GetProfileName(), true);
509                                                 }
510                                                 else
511                                                 {
512                                                         // OSP or Non-default account
513                                                         accountName.Clear();
514                                                         accountName.Append(pProfileInfo->GetProfileDisplayName());
515                                                         accountName.Append(pProfileInfo->GetProfileName());
516
517                                                         SysLog(NID_NET, "ProfileName[%ls] is not found on DB, so add it.", pProfileInfo->GetProfileName().GetPointer());
518                                                         r = _NetAccountDatabase::AddAccount(accountName, pProfileInfo->GetProfileName(), _NET_ACCOUNT_OWNER_OTHER, accountId);
519                                                 }
520                                         }
521                                 }
522                         }
523                 }
524         }
525
526         if (pAccountList != null)
527         {
528                 if (pAccountList->GetCount() > 0)
529                 {
530                         SysLog(NID_NET, "[%d] zombie accounts are on DB.", pAccountList->GetCount());
531
532                         unique_ptr<IEnumerator> pEnum(pAccountList->GetEnumeratorN());
533                         if (pEnum != null)
534                         {
535                                 String* pProfileName = null;
536                                 while (pEnum->MoveNext() == E_SUCCESS)
537                                 {
538                                         pProfileName = dynamic_cast<String*>(pEnum->GetCurrent());
539                                         if (pProfileName != null)
540                                         {
541                                                 r = _NetAccountDatabase::RemoveAccountByProfileName(*pProfileName);
542                                         }
543                                 }
544                         }
545                 }
546         }
547
548 #ifdef _OSP_EMUL_
549         static const wchar_t _DEFAULT_PS_ACCOUNT_NAME_FOR_EMULATOR[] = L"Tizen_Internet_1";
550         static const wchar_t _DEFAULT_MMS_ACCOUNT_NAME_FOR_EMULATOR[] = L"Tizen_Mms_2";
551
552         String defaultAccountName;
553         int defaultAccountId;
554
555         r = _NetAccountDatabase::GetAccountName(_DEFAULT_PS_ACCOUNT_ID, defaultAccountName);
556         if (r != E_SUCCESS)
557         {
558                 SysLog(NID_NET, "Default PS account is not found, so add it now.");
559                 r = _NetAccountDatabase::AddAccount(String(_DEFAULT_PS_ACCOUNT_NAME_FOR_EMULATOR),
560                                 String(_DEFAULT_PS_ACCOUNT_NAME_FOR_EMULATOR), _NET_ACCOUNT_OWNER_SYSTEM_INTERNET, defaultAccountId);
561                 r = _NetAccountDatabase::AddAccount(String(_DEFAULT_MMS_ACCOUNT_NAME_FOR_EMULATOR),
562                                 String(_DEFAULT_MMS_ACCOUNT_NAME_FOR_EMULATOR), _NET_ACCOUNT_OWNER_SYSTEM_MMS, defaultAccountId);
563         }
564 #endif // _OSP_EMUL_
565
566         __isInitialized = true;
567 }
568
569 _SystemNetConnection*
570 _SystemNetConnection::GetPsInstance(NetAccountId netAccountId)
571 {
572         result r = E_SUCCESS;
573         _PsSystemNetConnection* pConnection = null;
574         String profileName;
575
576         r = _NetAccountDatabase::GetProfileName(netAccountId, profileName);
577         SysTryReturn(NID_NET, r == E_SUCCESS, null, E_INVALID_ACCOUNT,
578                         "[%s] Invalid network account. accountId=%d", GetErrorMessage(E_INVALID_ACCOUNT), netAccountId);
579
580         Mutex* pLock = _NetUtility::GetLock();
581         MutexGuard locked(*pLock);
582
583         pConnection = dynamic_cast<_PsSystemNetConnection*>(__pPsSystemConnectionMap->GetValue(profileName));
584         if (pConnection == null)
585         {
586                 pConnection = new (std::nothrow) _PsSystemNetConnection();
587                 SysTryReturn(NID_NET, pConnection != null, null, E_OUT_OF_MEMORY,
588                                 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
589
590                 r = pConnection->Construct(profileName);
591                 if (r != E_SUCCESS)
592                 {
593                         SysLogException(NID_NET, r, "[%s] Propagating.", GetErrorMessage(r));
594
595                         delete pConnection;
596                         pConnection = null;
597                 }
598                 else
599                 {
600                         r = __pPsSystemConnectionMap->Add(pConnection->__profileName, *pConnection);
601                         if (r != E_SUCCESS)
602                         {
603                                 SysLogException(NID_NET, r, "[%s] Propagating.", GetErrorMessage(r));
604
605                                 delete pConnection;
606                                 pConnection = null;
607                         }
608                 }
609         }
610
611         locked.Unlock();
612
613         return pConnection;
614 }
615
616 _SystemNetConnection*
617 _SystemNetConnection::GetDefaultInstance(void)
618 {
619         result r = E_SUCCESS;
620
621         Mutex* pLock = _NetUtility::GetLock();
622         MutexGuard locked(*pLock);
623
624         if (__pDefaultConnection == null)
625         {
626                 unique_ptr<_DefaultSystemNetConnection> pDefaultConnection(new (std::nothrow) _DefaultSystemNetConnection());
627                 SysTryReturn(NID_NET, pDefaultConnection != null, null, E_OUT_OF_MEMORY,
628                                 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
629
630                 r = pDefaultConnection->Construct();
631                 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
632
633                 __pDefaultConnection = pDefaultConnection.release();
634         }
635
636         locked.Unlock();
637
638         return __pDefaultConnection;
639 }
640
641 _SystemNetConnection*
642 _SystemNetConnection::GetWifiInstance(void)
643 {
644         result r = E_SUCCESS;
645
646         Mutex* pLock = _NetUtility::GetLock();
647         MutexGuard locked(*pLock);
648
649         if (__pWifiConnection == null)
650         {
651                 unique_ptr<_WifiSystemNetConnection> pWifiConnection(new (std::nothrow) _WifiSystemNetConnection());
652                 SysTryReturn(NID_NET, pWifiConnection != null, null, E_OUT_OF_MEMORY,
653                                 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
654
655                 r = pWifiConnection->Construct();
656                 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
657
658                 __pWifiConnection = pWifiConnection.release();
659         }
660
661         locked.Unlock();
662
663         return __pWifiConnection;
664 }
665
666 _SystemNetConnection*
667 _SystemNetConnection::GetWifiDirectInstance(void)
668 {
669         result r = E_SUCCESS;
670
671         Mutex* pLock = _NetUtility::GetLock();
672         MutexGuard locked(*pLock);
673
674         // ToDo - Should be changed similar with other instance. (After resolving circular dependency.)
675         if (__pWifiDirectConnection == null)
676         {
677                 __pWifiDirectConnection = new (std::nothrow) _WifiDirectSystemNetConnection();
678                 SysTryReturn(NID_NET, __pWifiDirectConnection != null, null, E_OUT_OF_MEMORY,
679                                 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
680
681                 r = __pWifiDirectConnection->Construct();
682                 if (r != E_SUCCESS)
683                 {
684                         SysLogException(NID_NET, r, "[%s] Propagating.", GetErrorMessage(r));
685
686                         delete __pWifiDirectConnection;
687                         __pWifiDirectConnection = null;
688                 }
689         }
690
691         locked.Unlock();
692
693         return __pWifiDirectConnection;
694 }
695
696 _SystemNetConnection*
697 _SystemNetConnection::GetUsbInstance(void)
698 {
699         result r = E_SUCCESS;
700
701         Mutex* pLock = _NetUtility::GetLock();
702         MutexGuard locked(*pLock);
703
704         if (__pUsbConnection == null)
705         {
706                 unique_ptr<_UsbSystemNetConnection> pUsbConnection(new (std::nothrow) _UsbSystemNetConnection());
707                 SysTryReturn(NID_NET, pUsbConnection != null, null, E_OUT_OF_MEMORY,
708                                 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
709
710                 r = pUsbConnection->Construct();
711                 SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
712
713                 __pUsbConnection = pUsbConnection.release();
714         }
715
716         locked.Unlock();
717
718         return __pUsbConnection;
719 }
720
721 #if 0
722 result
723 _SystemNetConnection::ConvertError(int ret)
724 {
725         result r = E_SUCCESS;
726
727         switch (ret)
728         {
729         case NET_ERR_NONE:
730                 r = E_SUCCESS;
731                 break;
732
733         case NET_ERR_TIME_OUT:
734                 r = E_TIMEOUT;
735                 break;
736
737         case NET_ERR_NO_SERVICE:
738                 r = E_NETWORK_UNAVAILABLE;
739                 break;
740
741         case NET_ERR_NO_ACTIVE_CONNECTIONS:
742                 r = E_SERVICE_UNAVAILABLE;
743                 break;
744
745         case NET_ERR_CONNECTION_LOGIN_FAILED:
746         case NET_ERR_CONNECTION_AUTH_FAILED:
747         case NET_ERR_CONNECTION_INVALID_KEY:
748                 r = E_AUTHENTICATION;
749                 break;
750
751         case NET_ERR_NOT_SUPPORTED:
752         case NET_ERR_SECURITY_RESTRICTED:
753                 r = E_UNSUPPORTED_OPERATION;
754                 break;
755
756         case NET_ERR_CONNECTION_OUT_OF_RANGE:
757         case NET_ERR_CONNECTION_PIN_MISSING:
758         case NET_ERR_CONNECTION_CONNECT_FAILED:
759         case NET_ERR_OPERATION_ABORTED:
760         case NET_ERR_ACCESS_DENIED:
761                 r = E_NETWORK_FAILED;
762                 break;
763
764         case NET_ERR_CONNECTION_DHCP_FAILED:
765                 r = E_DHCP;
766                 break;
767
768         case NET_ERR_WIFI_DRIVER_FAILURE:
769                 r = E_LINK;
770                 break;
771
772         default:
773                 r = E_SYSTEM;
774                 break;
775         }
776
777         return r;
778 }
779 #endif
780
781 } } // Tizen::Net