Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNet_PsSystemNetConnection.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_PsSystemNetConnection.cpp
19  * @brief               This is the implementation file for _PsSystemNetConnection class.
20  * @version     3.0
21  *
22  * This file contains the implementation of _PsSystemNetConnection class.
23  */
24
25 #include <net_connection.h>
26 #include <FNetNetConnectionInfo.h>
27 #include <FBaseSysLog.h>
28 #include <FBase_StringConverter.h>
29 #include "FNet_NetTypes.h"
30 #include "FNet_NetConnectionInfoImpl.h"
31 #include "FNet_PsSystemNetConnection.h"
32 #include "FNet_NetAccountDatabase.h"
33 #include "FNet_NetAccountManagerImpl.h"
34 #include "FNet_NetConnectionEvent.h"
35 #include "FNet_NetConnectionEventArg.h"
36 #include "FNet_NetUtility.h"
37
38 using namespace std;
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Base::Runtime;
42
43 namespace Tizen { namespace Net {
44
45 void
46 PsOpenCallback(connection_error_e res, void* pUserData)
47 {
48         _PsSystemNetConnection* pConnection = static_cast<_PsSystemNetConnection*>(pUserData);
49         NetConnectionState connectionState = pConnection->GetConnectionState();
50         bool isConnected = false;
51         char* pProfileName = null;
52         char* pProfileDisplayName = null;
53         int ret = CONNECTION_ERROR_NONE;
54         connection_profile_h profileHandle = pConnection->GetProfileHandle();
55         connection_profile_state_e state = CONNECTION_PROFILE_STATE_DISCONNECTED;
56
57         ret = connection_profile_refresh(profileHandle);
58         SysLog(NID_NET, "The return value from connection_profile_refresh() is %d", ret);
59         ret = connection_profile_get_id(profileHandle, &pProfileName);
60         SysLog(NID_NET, "The return value from connection_profile_get_id() is %d", ret);
61         ret = connection_profile_get_name(profileHandle, &pProfileDisplayName);
62         SysLog(NID_NET, "The return value from connection_profile_get_name() is %d", ret);
63         ret = connection_profile_get_state(profileHandle, &state);
64         SysLog(NID_NET, "The return value from connection_profile_get_state() is %d", ret);
65
66         SysSecureLog(NID_NET, "PsOpenCallback() has been called with res:%d, state:%d, name:[%s][%s]", res, state, pProfileDisplayName, pProfileName);
67
68         if ((res == CONNECTION_ERROR_NONE) && (state == CONNECTION_PROFILE_STATE_CONNECTED))
69         {
70                 SysLog(NID_NET, "Profile[%s] is connected.", pProfileName);
71                 isConnected = true;
72         }
73         else
74         {
75                 SysLog(NID_NET, "Profile[%s] is disconnected.", pProfileName);
76                 isConnected = false;
77         }
78
79         free(pProfileName);
80         free(pProfileDisplayName);
81
82         if (connectionState != NET_CONNECTION_STATE_STARTING)
83         {
84                 SysLog(NID_NET, "Ignore open response because it is in state[%d].", connectionState);
85                 return;
86         }
87
88         if (isConnected)
89         {
90                 pConnection->HandleStartResponse(E_SUCCESS, profileHandle);
91         }
92         else
93         {
94                 pConnection->HandleStartResponse(E_NETWORK_FAILED, null);
95         }
96 }
97
98 void
99 PsCloseCallback(connection_error_e res, void* pUserData)
100 {
101         _PsSystemNetConnection* pConnection = static_cast<_PsSystemNetConnection*>(pUserData);
102         NetConnectionState connectionState = pConnection->GetConnectionState();
103         bool isConnected = false;
104         char* pProfileName = null;
105         char* pProfileDisplayName = null;
106         int ret = CONNECTION_ERROR_NONE;
107         connection_profile_h profileHandle = pConnection->GetProfileHandle();
108         connection_profile_state_e state = CONNECTION_PROFILE_STATE_DISCONNECTED;
109
110         ret = connection_profile_refresh(profileHandle);
111         SysLog(NID_NET, "The return value from connection_profile_refresh() is %d", ret);
112         ret = connection_profile_get_id(profileHandle, &pProfileName);
113         SysLog(NID_NET, "The return value from connection_profile_get_id() is %d", ret);
114         ret = connection_profile_get_name(profileHandle, &pProfileDisplayName);
115         SysLog(NID_NET, "The return value from connection_profile_get_name() is %d", ret);
116         ret = connection_profile_get_state(profileHandle, &state);
117         SysLog(NID_NET, "The return value from connection_profile_get_state() is %d", ret);
118
119         SysSecureLog(NID_NET, "PsCloseCallback() has been called with res:%d, state:%d, name:[%s][%s]", res, state, pProfileDisplayName, pProfileName);
120
121         if ((res == CONNECTION_ERROR_NONE) && (state == CONNECTION_PROFILE_STATE_CONNECTED))
122         {
123                 SysLog(NID_NET, "Profile[%s] is connected.", pProfileName);
124                 isConnected = true;
125         }
126         else
127         {
128                 SysLog(NID_NET, "Profile[%s] is disconnected.", pProfileName);
129                 isConnected = false;
130         }
131
132         free(pProfileName);
133         free(pProfileDisplayName);
134
135         if (connectionState != NET_CONNECTION_STATE_STOPPING)
136         {
137                 SysLog(NID_NET, "Ignore close response because it is in state[%d].", connectionState);
138                 return;
139         }
140
141         pConnection->HandleStopResponse();
142 }
143
144 void
145 PsStateChangedCallback(connection_profile_state_e state, void* pUserData)
146 {
147         _PsSystemNetConnection* pConnection = static_cast<_PsSystemNetConnection*>(pUserData);
148         NetConnectionState connectionState = pConnection->GetConnectionState();
149         bool isConnected = false;
150         char* pProfileName = null;
151         char* pProfileDisplayName = null;
152         int ret = CONNECTION_ERROR_NONE;
153         connection_profile_h profileHandle = pConnection->GetProfileHandle();
154
155         ret = connection_profile_refresh(profileHandle);
156         SysLog(NID_NET, "The return value from connection_profile_refresh() is %d", ret);
157         ret = connection_profile_get_id(profileHandle, &pProfileName);
158         SysLog(NID_NET, "The return value from connection_profile_get_id() is %d", ret);
159         ret = connection_profile_get_name(profileHandle, &pProfileDisplayName);
160         SysLog(NID_NET, "The return value from connection_profile_get_name() is %d", ret);
161
162         SysSecureLog(NID_NET, "PsStateChangedCallback() has been called with state:%d, name:[%s][%s]", state, pProfileDisplayName, pProfileName);
163
164         if (state == CONNECTION_PROFILE_STATE_CONNECTED)
165         {
166                 SysLog(NID_NET, "Profile[%s] is connected.", pProfileName);
167                 isConnected = true;
168         }
169         else
170         {
171                 SysLog(NID_NET, "Profile[%s] is disconnected.", pProfileName);
172                 isConnected = false;
173         }
174
175         free(pProfileName);
176         free(pProfileDisplayName);
177
178         if ((connectionState == NET_CONNECTION_STATE_STARTING) || (connectionState == NET_CONNECTION_STATE_STOPPING))
179         {
180                 SysLog(NID_NET, "Ignore (not requested) event because it is in state[%d].", connectionState);
181                 return;
182         }
183
184         if (isConnected)
185         {
186                 // Start event
187                 SysLog(NID_NET, "Ignore started event.");
188         }
189         else
190         {
191                 // Stop event
192                 pConnection->HandleStopEvent(E_NETWORK_FAILED);
193         }
194 }
195
196 _PsSystemNetConnection::_PsSystemNetConnection(void)
197         : __pConnectionHandle(null)
198         , __pProfileHandle(null)
199 {
200 }
201
202 _PsSystemNetConnection::~_PsSystemNetConnection(void)
203 {
204 }
205
206 result
207 _PsSystemNetConnection::Construct(const String& profileName)
208 {
209         SysLog(NID_NET, "Construct()has been called with profileName:%ls", profileName.GetPointer());
210
211         result r = E_SUCCESS;
212         unique_ptr<void, _ConnectionDeleter> pConnectionHandle;
213         unique_ptr<void, _ProfileDeleter> pProfileHandle;
214         int ret = CONNECTION_ERROR_NONE;
215         connection_h connectionHandle = null;
216         connection_profile_h profileHandle = null;
217
218         SysAssertf(__pConnectionHandle == null,
219                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
220
221         r = _SystemNetConnection::Initialize(L"PS");
222         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
223
224         ret = connection_create(&connectionHandle);
225         SysTryCatch(NID_NET, ret == CONNECTION_ERROR_NONE, r = E_SYSTEM, E_SYSTEM,
226                         "[%s] A system error has been occurred. The return value from connection_create() is %d", GetErrorMessage(E_SYSTEM), ret);
227         pConnectionHandle.reset(connectionHandle);
228
229         profileHandle = _NetAccountManagerImpl::GetPsProfileHandleN(profileName);
230         SysTryCatch(NID_NET, profileHandle != null, r = GetLastResult(), GetLastResult(),
231                         "[%s] Propagating.", GetErrorMessage(GetLastResult()));
232         pProfileHandle.reset(profileHandle);
233
234         ret = connection_profile_set_state_changed_cb(profileHandle, PsStateChangedCallback, this);
235         SysTryCatch(NID_NET, ret == CONNECTION_ERROR_NONE, r = E_SYSTEM, E_SYSTEM,
236                         "[%s] A system error has been occurred. The return value from connection_profile_set_state_changed_cb() is %d", GetErrorMessage(E_SYSTEM), ret);
237
238         __profileName = profileName;
239         __pConnectionHandle = move(pConnectionHandle);
240         __pProfileHandle = move(pProfileHandle);
241
242         return r;
243
244 CATCH:
245         _SystemNetConnection::Deinitialize();
246
247         return r;
248 }
249
250 result
251 _PsSystemNetConnection::Start(_NetConnectionEvent& event)
252 {
253         result r = E_SUCCESS;
254         _NetConnectionEventArg* pEventArg = null;
255
256         SysAssertf(__pConnectionHandle != null,
257                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
258
259         MutexGuard locked(*_pLock);
260
261         if (_connectionState == NET_CONNECTION_STATE_STARTED)
262         {
263                 // Already Started
264                 SysLog(NID_NET, "PS connection[%ls] is already started.", __profileName.GetPointer());
265
266                 if (event.GetConnectionState() != NET_CONNECTION_STATE_STARTED)
267                 {
268                         pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, E_SUCCESS);
269                         SysTryReturnResult(NID_NET, pEventArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
270
271                         event.SetConnectionState(NET_CONNECTION_STATE_STARTED);
272                         event.FireAsync(*pEventArg);
273
274                         _refCount++;
275                 }
276         }
277         else if ((_connectionState == NET_CONNECTION_STATE_STARTING) || (_connectionState == NET_CONNECTION_STATE_STOPPING))
278         {
279                 // Waiting Response
280                 SysLog(NID_NET, "PS connection[%ls] is waiting response.", __profileName.GetPointer());
281
282                 if (event.GetConnectionState() != NET_CONNECTION_STATE_STARTING)
283                 {
284                         event.SetConnectionState(NET_CONNECTION_STATE_STARTING);
285                         _refCount++;
286                 }
287         }
288         else
289         {
290                 // None or Stopped
291                 SysLog(NID_NET, "PS connection[%ls] is not active, so start now.", __profileName.GetPointer());
292
293                 int ret = CONNECTION_ERROR_NONE;
294
295                 ret = connection_open_profile(__pConnectionHandle.get(), __pProfileHandle.get(), PsOpenCallback, this);
296                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
297                                 "A system error has been occurred. The return value from connection_open_profile() is %d", ret);
298
299                 _connectionState = NET_CONNECTION_STATE_STARTING;
300                 event.SetConnectionState(NET_CONNECTION_STATE_STARTING);
301                 _refCount++;
302         }
303
304         locked.Unlock();
305
306         return r;
307 }
308
309 result
310 _PsSystemNetConnection::Stop(_NetConnectionEvent& event, bool waitingEvent)
311 {
312         result r = E_SUCCESS;
313         _NetConnectionEventArg* pEventArg = null;
314
315         SysAssertf(__pConnectionHandle != null,
316                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
317
318         MutexGuard locked(*_pLock);
319
320         if ((event.GetConnectionState() == NET_CONNECTION_STATE_STARTED) ||
321                 (event.GetConnectionState() == NET_CONNECTION_STATE_STARTING))
322         {
323                 if (_connectionState == NET_CONNECTION_STATE_STARTED)
324                 {
325                         // Started
326                         if (_refCount > 1)
327                         {
328                                 SysLog(NID_NET, "PS connection[%ls] is used by others[%d].", __profileName.GetPointer(), _refCount-1);
329
330                                 if (waitingEvent)
331                                 {
332                                         pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, E_SUCCESS);
333                                         SysTryReturnResult(NID_NET, pEventArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
334
335                                         event.SetConnectionState(NET_CONNECTION_STATE_STOPPED);
336                                         event.FireAsync(*pEventArg);
337                                 }
338
339                                 _refCount--;
340                         }
341                         else
342                         {
343                                 SysLog(NID_NET, "PS connection[%ls] is not used any longer, so stop it.", __profileName.GetPointer());
344
345                                 int ret = CONNECTION_ERROR_NONE;
346
347                                 ret = connection_close_profile(__pConnectionHandle.get(), __pProfileHandle.get(), PsCloseCallback, this);
348                                 SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
349                                                 "A system error has been occurred. The return value from connection_close_profile() is %d", ret);
350
351                                 _connectionState = NET_CONNECTION_STATE_STOPPING;
352                                 _refCount--;
353
354                                 event.SetConnectionState(NET_CONNECTION_STATE_STOPPING);
355                         }
356                 }
357                 else if ((_connectionState == NET_CONNECTION_STATE_STOPPING) ||
358                                 (_connectionState == NET_CONNECTION_STATE_STARTING))
359                 {
360                         // Waiting Response
361                         _refCount--;
362                         event.SetConnectionState(NET_CONNECTION_STATE_STOPPING);
363                 }
364         }
365         else
366         {
367                 // None or Stopped
368                 SysLog(NID_NET, "PS connection[%ls] is already stopped.", __profileName.GetPointer());
369
370                 if (waitingEvent)
371                 {
372                         pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, E_SUCCESS);
373                         SysTryReturnResult(NID_NET, pEventArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
374
375                         event.SetConnectionState(NET_CONNECTION_STATE_STOPPED);
376                         event.FireAsync(*pEventArg);
377                 }
378         }
379
380         locked.Unlock();
381
382         return r;
383 }
384
385 void
386 _PsSystemNetConnection::HandleStartResponse(result error, void* pData)
387 {
388         IEnumerator* pEnum = null;
389         _NetConnectionEvent* pEvent = null;
390         _NetConnectionEventArg* pStartEventArg = null;
391         _NetConnectionEventArg* pStopEventArg = null;
392
393         if (_connectionState != NET_CONNECTION_STATE_STARTING)
394         {
395                 SysLog(NID_NET, "Ignore _PsSystemNetConnection::HandleStartResponse() because this is NOT in starting state.");
396                 return;
397         }
398
399         MutexGuard locked(*_pLock);
400
401         if (error == E_SUCCESS)
402         {
403                 _bearerType = NET_BEARER_PS;
404                 _connectionState = NET_CONNECTION_STATE_STARTED;
405                 _pConnectionInfo->Update(pData);
406         }
407         else
408         {
409                 _bearerType = NET_BEARER_NONE;
410                 _connectionState = NET_CONNECTION_STATE_STOPPED;
411                 _pConnectionInfo->Clear();
412                 _refCount = 0;
413         }
414
415         pEnum = _pEventList->GetEnumeratorN();
416         if (pEnum != null)
417         {
418                 while (pEnum->MoveNext() == E_SUCCESS)
419                 {
420                         pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
421                         if (pEvent != null)
422                         {
423                                 if (pEvent->GetConnectionState() == NET_CONNECTION_STATE_STARTING)
424                                 {
425                                         if (error == E_SUCCESS)
426                                         {
427                                                 pEvent->SetConnectionState(NET_CONNECTION_STATE_STARTED);
428                                         }
429                                         else
430                                         {
431                                                 pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
432                                         }
433
434                                         pStartEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, error);
435                                         if (pStartEventArg != null)
436                                         {
437                                                 pEvent->FireAsync(*pStartEventArg);
438                                                 SysLog(NID_NET, "Fire Start event on event[0x%x].", pEvent);
439                                         }
440                                 }
441                                 else if (pEvent->GetConnectionState() == NET_CONNECTION_STATE_STOPPING)
442                                 {
443                                         if (error != E_SUCCESS)
444                                         {
445                                                 pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
446                                                 pStopEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, E_SUCCESS);
447                                                 if (pStopEventArg != null)
448                                                 {
449                                                                 pEvent->FireAsync(*pStopEventArg);
450                                                 }
451                                         }
452                                 }
453                         }
454                 }
455
456                 delete pEnum;
457         }
458
459         if ((_connectionState == NET_CONNECTION_STATE_STARTED) && (_refCount == 0))
460         {
461                 SysLog(NID_NET, "Connection is started, but not used any more, so stop it.");
462
463                 int ret = CONNECTION_ERROR_NONE;
464                 bool isSuccess = false;
465
466                 ret = connection_close_profile(__pConnectionHandle.get(), __pProfileHandle.get(), PsCloseCallback, this);
467                 if (ret == CONNECTION_ERROR_NONE)
468                 {
469                         _connectionState = NET_CONNECTION_STATE_STOPPING;
470                         isSuccess = true;
471                 }
472
473                 if (isSuccess == false)
474                 {
475                         _connectionState = NET_CONNECTION_STATE_STOPPED;
476
477                         pEnum = _pEventList->GetEnumeratorN();
478                         if (pEnum != null)
479                         {
480                                 while (pEnum->MoveNext() == E_SUCCESS)
481                                 {
482                                         pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
483                                         if (pEvent != null)
484                                         {
485                                                 if (pEvent->GetConnectionState() == NET_CONNECTION_STATE_STOPPING)
486                                                 {
487                                                         pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
488                                                         pStopEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, E_SUCCESS);
489                                                         if (pStopEventArg != null)
490                                                         {
491                                                                 pEvent->FireAsync(*pStopEventArg);
492                                                         }
493                                                 }
494                                         }
495                                 }
496
497                                 delete pEnum;
498                         }
499                 }
500         }
501
502         locked.Unlock();
503 }
504
505 void
506 _PsSystemNetConnection::HandleStopResponse(void)
507 {
508         IEnumerator* pEnum = null;
509         _NetConnectionEvent* pEvent = null;
510         _NetConnectionEventArg* pEventArg = null;
511
512         if (_connectionState != NET_CONNECTION_STATE_STOPPING)
513         {
514                 SysLog(NID_NET, "Ignore _PsSystemNetConnection::HandleStopResponse() because this is NOT in stopping state.");
515                 return;
516         }
517
518         MutexGuard locked(*_pLock);
519
520         _bearerType = NET_BEARER_NONE;
521         _connectionState = NET_CONNECTION_STATE_STOPPED;
522         _pConnectionInfo->Clear();
523
524         pEnum = _pEventList->GetEnumeratorN();
525         if (pEnum != null)
526         {
527                 while (pEnum->MoveNext() == E_SUCCESS)
528                 {
529                         pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
530                         if (pEvent != null)
531                         {
532                                 if (pEvent->GetConnectionState() == NET_CONNECTION_STATE_STOPPING)
533                                 {
534                                         pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
535                                         pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, E_SUCCESS);
536                                         if (pEventArg != null)
537                                         {
538                                                 pEvent->FireAsync(*pEventArg);
539                                         }
540                                 }
541                         }
542                 }
543
544                 delete pEnum;
545         }
546
547         if (_refCount > 0)
548         {
549                 SysLog(NID_NET, "Connection is stopped, but another start request is found, so start the connection.");
550
551                 int ret = CONNECTION_ERROR_NONE;
552                 bool isSuccess = false;
553
554                 ret = connection_open_profile(__pConnectionHandle.get(), __pProfileHandle.get(), PsOpenCallback, this);
555                 SysLog(NID_NET, "Invoke connection_open_profile() ret[%d].", ret);
556                 if (ret == CONNECTION_ERROR_NONE)
557                 {
558                         _connectionState = NET_CONNECTION_STATE_STARTING;
559                         isSuccess = true;
560                 }
561
562                 if (isSuccess == false)
563                 {
564                         _refCount = 0;
565
566                         pEnum = _pEventList->GetEnumeratorN();
567                         if (pEnum != null)
568                         {
569                                 while (pEnum->MoveNext() == E_SUCCESS)
570                                 {
571                                         pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
572                                         if (pEvent != null)
573                                         {
574                                                 if (pEvent->GetConnectionState() == NET_CONNECTION_STATE_STARTING)
575                                                 {
576                                                         pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
577                                                         pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, E_NETWORK_FAILED);
578                                                         if (pEventArg != null)
579                                                         {
580                                                                 pEvent->FireAsync(*pEventArg);
581                                                         }
582                                                 }
583                                         }
584                                 }
585
586                                 delete pEnum;
587                         }
588                 }
589         }
590
591         locked.Unlock();
592 }
593
594 void
595 _PsSystemNetConnection::HandleStartEvent(void)
596 {
597         SysLogException(NID_NET, E_INVALID_OPERATION,
598                         "[%s] _PsSystemNetConnection.HandleStartEvent() is NOT allowed.", GetErrorMessage(E_INVALID_OPERATION));
599 }
600
601 void
602 _PsSystemNetConnection::HandleStopEvent(result error)
603 {
604         IEnumerator* pEnum = null;
605         _NetConnectionEvent* pEvent = null;
606         _NetConnectionEventArg* pStartEventArg = null;
607         _NetConnectionEventArg* pStopEventArg = null;
608         _NetConnectionEventArg* pStopEventArg2 = null;
609
610         if ((_connectionState == NET_CONNECTION_STATE_NONE) || (_connectionState == NET_CONNECTION_STATE_STOPPED))
611         {
612                 SysLog(NID_NET, "Ignore _PsSystemNetConnection::HandleStopEvent() because this is already in stopped state.");
613                 return;
614         }
615
616         MutexGuard locked(*_pLock);
617
618         _bearerType = NET_BEARER_NONE;
619         _connectionState = NET_CONNECTION_STATE_STOPPED;
620         _pConnectionInfo->Clear();
621         _refCount = 0;
622
623         pEnum = _pEventList->GetEnumeratorN();
624         if (pEnum != null)
625         {
626                 while (pEnum->MoveNext() == E_SUCCESS)
627                 {
628                         pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
629                         if (pEvent != null)
630                         {
631                                 if (pEvent->GetConnectionState() == NET_CONNECTION_STATE_STARTING)
632                                 {
633                                         pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
634                                         pStartEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, error);
635                                         if (pStartEventArg != null)
636                                         {
637                                                 pEvent->FireAsync(*pStartEventArg);
638                                         }
639                                 }
640                                 else if (pEvent->GetConnectionState() == NET_CONNECTION_STATE_STARTED)
641                                 {
642                                         pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
643                                         pStopEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, error);
644                                         if (pStopEventArg != null)
645                                         {
646                                                 pEvent->FireAsync(*pStopEventArg);
647                                         }
648                                 }
649                                 else if (pEvent->GetConnectionState() == NET_CONNECTION_STATE_STOPPING)
650                                 {
651                                         pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
652                                         pStopEventArg2 = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, E_SUCCESS);
653                                         if (pStopEventArg2 != null)
654                                         {
655                                                 pEvent->FireAsync(*pStopEventArg2);
656                                         }
657                                 }
658                         }
659                 }
660
661                 delete pEnum;
662         }
663
664         locked.Unlock();
665 }
666
667 NetConnectionState
668 _PsSystemNetConnection::QueryConnectionState(String& devName) const
669 {
670         NetConnectionState state = _connectionState;
671         int ret = CONNECTION_ERROR_NONE;
672         connection_profile_h profileHandle = _NetAccountManagerImpl::GetPsProfileHandleN(__profileName);
673         connection_profile_state_e profileState = CONNECTION_PROFILE_STATE_DISCONNECTED;
674
675         if (profileHandle != null)
676         {
677                 ret = connection_profile_get_state(profileHandle, &profileState);
678                 if ((ret == CONNECTION_ERROR_NONE) && (profileState == CONNECTION_PROFILE_STATE_CONNECTED))
679                 {
680                         char* pDevName = null;
681
682                         ret = connection_profile_get_network_interface_name(profileHandle, &pDevName);
683                         if ((ret == CONNECTION_ERROR_NONE) && (pDevName != null))
684                         {
685                                 devName = String(pDevName);
686                                 free(pDevName);
687                         }
688
689                         state = NET_CONNECTION_STATE_STARTED;
690                 }
691
692                 connection_profile_destroy(profileHandle);
693         }
694
695         SysLog(NID_NET, "QueryConnectionState() has done with state:%d, devName:%ls", state, devName.GetPointer());
696
697         return state;
698 }
699
700 void*
701 _PsSystemNetConnection::GetProfileHandle(void)
702 {
703         return __pProfileHandle.get();
704 }
705
706 } } // Tizen::Net