2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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.
19 * @file FNet_NetConnectionImpl.cpp
20 * @brief This is the implementation for the _NetConnectionImpl class.
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"
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Base::Collection;
41 namespace Tizen { namespace Net {
43 _NetConnectionImpl::_NetConnectionImpl()
44 : __pNetConnection(null)
45 , __netAccountId(INVALID_HANDLE)
48 , __pSystemNetConnection(null)
53 _NetConnectionImpl::~_NetConnectionImpl(void)
57 __pEvent->RemoveAllNetConnectionEventListener(__pNetConnection);
59 if (__pSystemNetConnection != null)
63 __pSystemNetConnection->Stop(*__pEvent);
66 __pSystemNetConnection = null;
73 __pNetConnection = null;
76 _NetConnectionImpl::_NetConnectionImpl(const _NetConnectionImpl& value)
78 __pNetConnection = null;
79 __netAccountId = value.__netAccountId;
80 __netAccountName = value.__netAccountName;
82 __isClosed = value.__isClosed;
83 __pSystemNetConnection = value.__pSystemNetConnection;
84 __pEvent = value.__pEvent;
89 _NetConnectionImpl::Construct(NetConnection* pNetConnection, NetAccountId netAccountId)
92 _SystemNetConnection* pSystemNetConnection = null;
93 String netAccountName;
95 SysAssertf(__netAccountId == INVALID_HANDLE,
96 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
98 _SystemNetConnection::InitializeNetworkFramework();
100 unique_ptr<_NetConnectionEvent> pEvent(new (std::nothrow) _NetConnectionEvent());
101 SysTryReturnResult(NID_NET, pEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
103 r = pEvent->Construct();
104 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
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))
112 SysLog(NID_NET, "Use default network connection.");
113 pSystemNetConnection = _SystemNetConnection::GetDefaultInstance();
117 pSystemNetConnection = null;
119 SysTryReturnResult(NID_NET, pSystemNetConnection != null, E_INVALID_ACCOUNT,
120 "Invalid network account. accountId=%d", netAccountId);
122 if (netAccountId == _DEFAULT_WIFI_ACCOUNT_ID)
125 pSystemNetConnection = _SystemNetConnection::GetWifiInstance();
127 else if (netAccountId == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID)
130 pSystemNetConnection = _SystemNetConnection::GetWifiDirectInstance();
132 else if (netAccountId == _DEFAULT_USB_ACCOUNT_ID)
135 pSystemNetConnection = _SystemNetConnection::GetUsbInstance();
142 r = _NetAccountDatabase::GetAccountName(netAccountId, netAccountName);
143 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_ACCOUNT,
144 "Invalid network account. accountId=%d", netAccountId);
146 pSystemNetConnection = _SystemNetConnection::GetPsInstance(netAccountId);
149 SysTryReturnResult(NID_NET, pSystemNetConnection != null, E_SYSTEM,
150 "A system error has been occurred. pSystemNetConnection is null.");
152 r = pSystemNetConnection->AddEvent(*pEvent);
153 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
155 pEvent->SetSystemConnection(pSystemNetConnection, false);
157 __pNetConnection = pNetConnection;
158 __netAccountId = netAccountId;
159 __netAccountName = netAccountName;
162 __pSystemNetConnection = pSystemNetConnection;
163 __pEvent = pEvent.release();
169 _NetConnectionImpl::AddNetConnectionListener(INetConnectionEventListener& listener)
171 result r = E_SUCCESS;
173 SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
175 SysTryReturnResult(NID_NET, !__isClosed, E_INVALID_STATE, "The instance was closed.");
177 r = __pEvent->AddNetConnectionEventListener(listener, __pNetConnection);
183 _NetConnectionImpl::RemoveNetConnectionListener(INetConnectionEventListener& listener)
185 result r = E_SUCCESS;
187 SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
189 SysTryReturnResult(NID_NET, !__isClosed, E_INVALID_STATE, "The instance was closed.");
191 r = __pEvent->RemoveNetConnectionEventListener(listener);
197 _NetConnectionImpl::Start(void)
199 result r = E_SUCCESS;
201 SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
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.");
206 r = __pSystemNetConnection->Start(*__pEvent);
212 _NetConnectionImpl::Stop(void)
214 result r = E_SUCCESS;
216 SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
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.");
221 r = __pSystemNetConnection->Stop(*__pEvent);
227 _NetConnectionImpl::Close(void)
229 result r = E_SUCCESS;
231 SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
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.");
236 __pEvent->RemoveAllNetConnectionEventListener(__pNetConnection);
238 if (__pSystemNetConnection != null)
240 __pSystemNetConnection->Stop(*__pEvent);
241 __pSystemNetConnection = null;
253 _NetConnectionImpl::GetNetAccountId(void) const
257 SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
259 SysTryReturn(NID_NET, !__isClosed, INVALID_HANDLE, E_INVALID_STATE,
260 "[%s] The instance was closed.", GetErrorMessage(E_INVALID_STATE));
262 return __netAccountId;
265 const NetConnectionInfo*
266 _NetConnectionImpl::GetNetConnectionInfo(void)
270 NetConnectionInfo* pConnectionInfo = null;
272 SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
274 SysTryReturn(NID_NET, !__isClosed, null, E_INVALID_STATE,
275 "[%s] The instance was closed.", GetErrorMessage(E_INVALID_STATE));
277 if (__pEvent->GetConnectionState() == NET_CONNECTION_STATE_STARTED)
279 __pSystemNetConnection->GetConnectionInfo(__netConnectionInfo);
280 pConnectionInfo = &__netConnectionInfo;
283 return pConnectionInfo;
287 _NetConnectionImpl::GetNetConnectionInfoN(NetAccountId netAccountId)
291 return _NetConnectionManagerImpl::GetNetConnectionInfoN(netAccountId);
295 _NetConnectionImpl::GetAllNetConnectionInfoN(void)
299 return _NetConnectionManagerImpl::GetAllNetConnectionInfoN();
303 _NetConnectionImpl::GetConnectionState(void) const
307 SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
309 SysTryReturn(NID_NET, !__isClosed, NET_CONNECTION_STATE_NONE, E_INVALID_STATE,
310 "[%s] The instance was closed.", GetErrorMessage(E_INVALID_STATE));
312 return __pEvent->GetConnectionState();
316 _NetConnectionImpl::GetProxyAddress(void) const
320 SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
322 SysTryReturn(NID_NET, !__isClosed, L"", E_INVALID_STATE, "[%s] The instance was closed.", GetErrorMessage(E_INVALID_STATE));
324 return __pSystemNetConnection->GetProxyAddress();
328 _NetConnectionImpl::CopyInstanceN(void)
332 SysAssertf(__netAccountId != INVALID_HANDLE, "Not yet constructed. Construct() should be called before use.");
334 SysTryReturn(NID_NET, !__isClosed, null, E_INVALID_STATE, "[%s] The instance was closed.", GetErrorMessage(E_INVALID_STATE));
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));
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));
344 pConnectionImpl->__pNetConnection = pConnection.get();
345 pConnection->__pNetConnectionImpl = pConnectionImpl;
347 return pConnection.release();
351 _NetConnectionImpl::GetInstance(NetConnection& netConnection)
353 return netConnection.__pNetConnectionImpl;
356 const _NetConnectionImpl*
357 _NetConnectionImpl::GetInstance(const NetConnection& netConnection)
359 return netConnection.__pNetConnectionImpl;