Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNet_NetConnectionImpl.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_NetConnectionImpl.cpp
20  * @brief               This is the implementation for the _NetConnectionImpl class.
21  */
22
23 #include <unique_ptr.h>
24 #include <FNetNetTypes.h>
25 #include <FNetNetConnectionInfo.h>
26 #include <FNetNetConnection.h>
27 #include <FBaseSysLog.h>
28 #include "FNet_NetTypes.h"
29 #include "FNet_NetConnectionImpl.h"
30 #include "FNet_NetConnectionManagerImpl.h"
31 #include "FNet_SystemNetConnection.h"
32 #include "FNet_NetAccountDatabase.h"
33 #include "FNet_NetConnectionEvent.h"
34 #include "FNet_NetUtility.h"
35
36 using namespace std;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Base::Collection;
40
41 namespace Tizen { namespace Net {
42
43 _NetConnectionImpl::_NetConnectionImpl()
44         : __pNetConnection(null)
45         , __netAccountId(INVALID_HANDLE)
46         , __isOwner(false)
47         , __isClosed(false)
48         , __pSystemNetConnection(null)
49         , __pEvent(null)
50 {
51 }
52
53 _NetConnectionImpl::~_NetConnectionImpl(void)
54 {
55         if (__pEvent != null)
56         {
57                 __pEvent->RemoveAllNetConnectionEventListener(__pNetConnection);
58
59                 if (__pSystemNetConnection != null)
60                 {
61                         if (__isOwner)
62                         {
63                                 __pSystemNetConnection->Stop(*__pEvent);
64                         }
65
66                         __pSystemNetConnection = null;
67                 }
68
69                 __pEvent->Release();
70                 __pEvent = null;
71         }
72
73         __pNetConnection = null;
74 }
75
76 _NetConnectionImpl::_NetConnectionImpl(const _NetConnectionImpl& value)
77 {
78         __pNetConnection = null;
79         __netAccountId = value.__netAccountId;
80         __netAccountName = value.__netAccountName;
81         __isOwner = false;
82         __isClosed = value.__isClosed;
83         __pSystemNetConnection = value.__pSystemNetConnection;
84         __pEvent = value.__pEvent;
85         __pEvent->AddRef();
86 }
87
88 result
89 _NetConnectionImpl::Construct(NetConnection* pNetConnection, NetAccountId netAccountId)
90 {
91         result r = E_SUCCESS;
92         _SystemNetConnection* pSystemNetConnection = null;
93         String netAccountName;
94
95         SysAssertf(__netAccountId == INVALID_HANDLE,
96                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
97
98         _SystemNetConnection::InitializeNetworkFramework();
99
100         unique_ptr<_NetConnectionEvent> pEvent(new (std::nothrow) _NetConnectionEvent());
101         SysTryReturnResult(NID_NET, pEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
102
103         r = pEvent->Construct();
104         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
105
106 #ifdef _OSP_EMUL_
107         if ((netAccountId == _DEFAULT_WIFI_ACCOUNT_ID) ||
108                 (netAccountId == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID) ||
109                 (netAccountId == _DEFAULT_USB_ACCOUNT_ID) ||
110                 (_NetAccountDatabase::GetAccountName(netAccountId, netAccountName) == E_SUCCESS))
111         {
112                 SysLog(NID_NET, "Use default network connection.");
113                 pSystemNetConnection = _SystemNetConnection::GetDefaultInstance();
114         }
115         else
116         {
117                 pSystemNetConnection = null;
118         }
119         SysTryReturnResult(NID_NET, pSystemNetConnection != null, E_INVALID_ACCOUNT,
120                         "Invalid network account. accountId=%d", netAccountId);
121 #else // _OSP_EMUL_
122         if (netAccountId == _DEFAULT_WIFI_ACCOUNT_ID)
123         {
124                 // WiFi
125                 pSystemNetConnection = _SystemNetConnection::GetWifiInstance();
126         }
127         else if (netAccountId == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID)
128         {
129                 // WiFi-Direct
130                 pSystemNetConnection = _SystemNetConnection::GetWifiDirectInstance();
131         }
132         else if (netAccountId == _DEFAULT_USB_ACCOUNT_ID)
133         {
134                 // USB
135                 pSystemNetConnection = _SystemNetConnection::GetUsbInstance();
136         }
137         else
138         {
139                 // PS
140                 String profileName;
141
142                 r = _NetAccountDatabase::GetAccountName(netAccountId, netAccountName);
143                 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_ACCOUNT,
144                                 "Invalid network account. accountId=%d", netAccountId);
145
146                 pSystemNetConnection = _SystemNetConnection::GetPsInstance(netAccountId);
147         }
148 #endif // _OSP_EMUL_
149         SysTryReturnResult(NID_NET, pSystemNetConnection != null, E_SYSTEM,
150                         "A system error has been occurred. pSystemNetConnection is null.");
151
152         r = pSystemNetConnection->AddEvent(*pEvent);
153         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
154
155         pEvent->SetSystemConnection(pSystemNetConnection, false);
156
157         __pNetConnection = pNetConnection;
158         __netAccountId = netAccountId;
159         __netAccountName = netAccountName;
160         __isOwner = true;
161         __isClosed = false;
162         __pSystemNetConnection = pSystemNetConnection;
163         __pEvent = pEvent.release();
164
165         return r;
166 }
167
168 result
169 _NetConnectionImpl::AddNetConnectionListener(INetConnectionEventListener& listener)
170 {
171         result r = E_SUCCESS;
172
173         SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
174
175         SysTryReturnResult(NID_NET, !__isClosed, E_INVALID_STATE, "The instance was closed.");
176
177         r = __pEvent->AddNetConnectionEventListener(listener, __pNetConnection);
178
179         return r;
180 }
181
182 result
183 _NetConnectionImpl::RemoveNetConnectionListener(INetConnectionEventListener& listener)
184 {
185         result r = E_SUCCESS;
186
187         SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
188
189         SysTryReturnResult(NID_NET, !__isClosed, E_INVALID_STATE, "The instance was closed.");
190
191         r = __pEvent->RemoveNetConnectionEventListener(listener);
192
193         return r;
194 }
195
196 result
197 _NetConnectionImpl::Start(void)
198 {
199         result r = E_SUCCESS;
200
201         SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
202
203         SysTryReturnResult(NID_NET, !__isClosed, E_INVALID_STATE, "The instance was closed.");
204         SysTryReturnResult(NID_NET, __isOwner, E_INVALID_OPERATION, "No right to control the connection.");
205
206         r = __pSystemNetConnection->Start(*__pEvent);
207
208         return r;
209 }
210
211 result
212 _NetConnectionImpl::Stop(void)
213 {
214         result r = E_SUCCESS;
215
216         SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
217
218         SysTryReturnResult(NID_NET, !__isClosed, E_INVALID_STATE, "The instance was closed.");
219         SysTryReturnResult(NID_NET, __isOwner, E_INVALID_OPERATION, "No right to control the connection.");
220
221         r = __pSystemNetConnection->Stop(*__pEvent);
222
223         return r;
224 }
225
226 result
227 _NetConnectionImpl::Close(void)
228 {
229         result r = E_SUCCESS;
230
231         SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
232
233         SysTryReturnResult(NID_NET, !__isClosed, E_INVALID_STATE, "The instance was closed.");
234         SysTryReturnResult(NID_NET, __isOwner, E_INVALID_OPERATION, "No right to control the connection.");
235
236         __pEvent->RemoveAllNetConnectionEventListener(__pNetConnection);
237
238         if (__pSystemNetConnection != null)
239         {
240                 __pSystemNetConnection->Stop(*__pEvent);
241                 __pSystemNetConnection = null;
242
243                 __pEvent->Release();
244                 __pEvent = null;
245         }
246
247         __isClosed = true;
248
249         return r;
250 }
251
252 NetAccountId
253 _NetConnectionImpl::GetNetAccountId(void) const
254 {
255         ClearLastResult();
256
257         SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
258
259         SysTryReturn(NID_NET, !__isClosed, INVALID_HANDLE, E_INVALID_STATE,
260                         "[%s] The instance was closed.", GetErrorMessage(E_INVALID_STATE));
261
262         return __netAccountId;
263 }
264
265 const NetConnectionInfo*
266 _NetConnectionImpl::GetNetConnectionInfo(void)
267 {
268         ClearLastResult();
269
270         NetConnectionInfo* pConnectionInfo = null;
271
272         SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
273
274         SysTryReturn(NID_NET, !__isClosed, null, E_INVALID_STATE,
275                         "[%s] The instance was closed.", GetErrorMessage(E_INVALID_STATE));
276
277         if (__pEvent->GetConnectionState() == NET_CONNECTION_STATE_STARTED)
278         {
279                 __pSystemNetConnection->GetConnectionInfo(__netConnectionInfo);
280                 pConnectionInfo = &__netConnectionInfo;
281         }
282
283         return pConnectionInfo;
284 }
285
286 NetConnectionInfo*
287 _NetConnectionImpl::GetNetConnectionInfoN(NetAccountId netAccountId)
288 {
289         ClearLastResult();
290
291         return _NetConnectionManagerImpl::GetNetConnectionInfoN(netAccountId);
292 }
293
294 IList*
295 _NetConnectionImpl::GetAllNetConnectionInfoN(void)
296 {
297         ClearLastResult();
298
299         return _NetConnectionManagerImpl::GetAllNetConnectionInfoN();
300 }
301
302 NetConnectionState
303 _NetConnectionImpl::GetConnectionState(void) const
304 {
305         ClearLastResult();
306
307         SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
308
309         SysTryReturn(NID_NET, !__isClosed, NET_CONNECTION_STATE_NONE, E_INVALID_STATE,
310                         "[%s] The instance was closed.", GetErrorMessage(E_INVALID_STATE));
311
312         return __pEvent->GetConnectionState();
313 }
314
315 String
316 _NetConnectionImpl::GetProxyAddress(void) const
317 {
318         ClearLastResult();
319
320         SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
321
322         SysTryReturn(NID_NET, !__isClosed, L"", E_INVALID_STATE, "[%s] The instance was closed.", GetErrorMessage(E_INVALID_STATE));
323
324         return __pSystemNetConnection->GetProxyAddress();
325 }
326
327 NetConnection*
328 _NetConnectionImpl::CopyInstanceN(void)
329 {
330         ClearLastResult();
331
332         SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
333
334         SysTryReturn(NID_NET, !__isClosed, null, E_INVALID_STATE, "[%s] The instance was closed.", GetErrorMessage(E_INVALID_STATE));
335
336         unique_ptr<NetConnection> pConnection(new (std::nothrow) NetConnection());
337         SysTryReturn(NID_NET, pConnection != null, null, E_OUT_OF_MEMORY,
338                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
339
340         _NetConnectionImpl* pConnectionImpl = new (std::nothrow) _NetConnectionImpl(*this);
341         SysTryReturn(NID_NET, pConnectionImpl != null, null, E_OUT_OF_MEMORY,
342                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
343
344         pConnectionImpl->__pNetConnection = pConnection.get();
345         pConnection->__pNetConnectionImpl = pConnectionImpl;
346
347         return pConnection.release();
348 }
349
350 _NetConnectionImpl*
351 _NetConnectionImpl::GetInstance(NetConnection& netConnection)
352 {
353         return netConnection.__pNetConnectionImpl;
354 }
355
356 const _NetConnectionImpl*
357 _NetConnectionImpl::GetInstance(const NetConnection& netConnection)
358 {
359         return netConnection.__pNetConnectionImpl;
360 }
361
362 } } // Tizen::Net
363