2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FNet_DefaultSystemNetConnection.cpp
19 * @brief This is the implementation file for _DefaultSystemNetConnection class.
22 * This file contains the implementation of _DefaultSystemNetConnection class.
25 #include <net_connection.h>
26 #include <FNetNetConnectionInfo.h>
27 #include <FBaseRtMutexGuard.h>
28 #include <FBaseSysLog.h>
29 #include <FBase_StringConverter.h>
30 #include "FNet_NetTypes.h"
31 #include "FNet_NetConnectionInfoImpl.h"
32 #include "FNet_DefaultSystemNetConnection.h"
33 #include "FNet_NetConnectionEvent.h"
34 #include "FNet_NetConnectionEventArg.h"
35 #include "FNet_NetUtility.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Runtime;
42 namespace Tizen { namespace Net {
45 ActiveConnectionTypeChangedCallback(connection_type_e type, void* pUserData)
47 _DefaultSystemNetConnection* pConnection = static_cast<_DefaultSystemNetConnection*>(pUserData);
48 bool isBearerChanged = false;
49 int ret = CONNECTION_ERROR_NONE;
50 connection_h connectionHandle = pConnection->GetConnectionHandle();
51 connection_profile_h profileHandle = null;
53 SysLog(NID_NET, "ActiveConnectionTypeChangedCallback() has been called with type : %d", type);
55 if (type != CONNECTION_TYPE_DISCONNECTED)
58 if ((type == CONNECTION_TYPE_WIFI) && (pConnection->GetBearerType() != NET_BEARER_WIFI))
60 isBearerChanged = true;
62 else if ((type == CONNECTION_TYPE_CELLULAR) && (pConnection->GetBearerType() != NET_BEARER_PS))
64 isBearerChanged = true;
67 NetConnectionState oldState = pConnection->GetConnectionState();
69 ret = connection_get_current_profile(connectionHandle, &profileHandle);
70 SysTryReturnVoidResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
71 "[%s] A system error has been occurred. The return value from connection_get_current_profile() is %d", GetErrorMessage(E_SYSTEM), ret);
73 pConnection->UpdateConnectionInfo(profileHandle);
74 connection_profile_destroy(profileHandle);
76 if (oldState != NET_CONNECTION_STATE_STARTED)
78 pConnection->HandleStartEvent();
80 else if (isBearerChanged)
82 pConnection->HandleChangedEvent();
86 SysLog(NID_NET, "Ignore the event, because this is already in started state.");
92 if ((pConnection->GetConnectionState() != NET_CONNECTION_STATE_NONE) && (pConnection->GetConnectionState() != NET_CONNECTION_STATE_STOPPED))
94 pConnection->UpdateConnectionInfo(null);
95 pConnection->HandleStopEvent(E_NETWORK_FAILED);
99 SysLog(NID_NET, "Ignore the event, because this is already in stopped state.");
104 _DefaultSystemNetConnection::_DefaultSystemNetConnection(void)
105 : __pConnectionHandle(null)
109 _DefaultSystemNetConnection::~_DefaultSystemNetConnection(void)
114 _DefaultSystemNetConnection::Construct(void)
116 result r = E_SUCCESS;
117 unique_ptr<void, _ConnectionDeleter> pConnectionHandle;
118 int ret = CONNECTION_ERROR_NONE;
119 connection_h connectionHandle = null;
120 connection_type_e type = CONNECTION_TYPE_DISCONNECTED;
122 SysAssertf(__pConnectionHandle == null,
123 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
125 r = _SystemNetConnection::Initialize(L"DEFAULT");
126 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
128 ret = connection_create(&connectionHandle);
129 SysTryCatch(NID_NET, ret == CONNECTION_ERROR_NONE, r = E_SYSTEM, E_SYSTEM,
130 "[%s] A system error has been occurred. The return value from connection_create() is %d", GetErrorMessage(E_SYSTEM), ret);
131 pConnectionHandle.reset(connectionHandle);
133 ret = connection_set_type_changed_cb(connectionHandle, ActiveConnectionTypeChangedCallback, this);
134 SysTryCatch(NID_NET, ret == CONNECTION_ERROR_NONE, r = E_SYSTEM, E_SYSTEM,
135 "[%s] A system error has been occurred. The return value from connection_set_type_changed_cb() is %d", GetErrorMessage(E_SYSTEM), ret);
137 ret = connection_get_type(connectionHandle, &type);
138 SysTryCatch(NID_NET, ret == CONNECTION_ERROR_NONE, r = E_SYSTEM, E_SYSTEM,
139 "[%s] A system error has been occurred. The return value from connection_get_type() is %d", GetErrorMessage(E_SYSTEM), ret);
141 if (type != CONNECTION_TYPE_DISCONNECTED)
143 // Default connection is ON
144 SysLog(NID_NET, "Default connection is ON. type=%d", type);
146 connection_profile_h profileHandle = null;
148 ret = connection_get_current_profile(connectionHandle, &profileHandle);
149 if (ret == CONNECTION_ERROR_NONE)
151 UpdateConnectionInfo(profileHandle);
152 connection_profile_destroy(profileHandle);
156 SysLog(NID_NET, "The return value from connection_get_current_profile() is %d", ret);
157 UpdateConnectionInfo(null);
162 // Default connection is OFF
163 SysLog(NID_NET, "Default connection is OFF.");
164 UpdateConnectionInfo(null);
167 __pConnectionHandle = move(pConnectionHandle);
172 _SystemNetConnection::Deinitialize();
178 _DefaultSystemNetConnection::HandleStartEvent(void)
180 _NetConnectionEvent* pEvent = null;
181 _NetConnectionEventArg* pEventArg = null;
183 SysLog(NID_NET, "[%ls] Notify started event.", _name.GetPointer());
185 MutexGuard locked(*_pLock);
187 unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
190 while (pEnum->MoveNext() == E_SUCCESS)
192 pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
195 // Sends event which doesn't invoke start.
196 if (pEvent->GetConnectionState() != NET_CONNECTION_STATE_STARTED)
198 pEvent->SetConnectionState(NET_CONNECTION_STATE_STARTED);
199 pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, E_SUCCESS);
200 if (pEventArg != null)
202 pEvent->FireAsync(*pEventArg);
213 _DefaultSystemNetConnection::HandleStopEvent(result error)
215 _NetConnectionEvent* pEvent = null;
216 _NetConnectionEventArg* pEventArg = null;
218 SysLog(NID_NET, "[%ls] Notify stopped event.", _name.GetPointer());
220 MutexGuard locked(*_pLock);
224 unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
227 while (pEnum->MoveNext() == E_SUCCESS)
229 pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
232 pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
233 pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, error);
234 if (pEventArg != null)
236 pEvent->FireAsync(*pEventArg);
246 _DefaultSystemNetConnection::HandleChangedEvent(void)
248 _NetConnectionEvent* pEvent = null;
249 _NetConnectionEventArg* pEventArg = null;
251 SysLog(NID_NET, "[%ls] Notify bearer changed event.", _name.GetPointer());
253 MutexGuard locked(*_pLock);
255 unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
258 while (pEnum->MoveNext() == E_SUCCESS)
260 pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
263 pEvent->SetConnectionState(NET_CONNECTION_STATE_STARTED);
264 pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_CHANGED, E_SUCCESS);
265 if (pEventArg != null)
267 pEvent->FireAsync(*pEventArg);
277 _DefaultSystemNetConnection::UpdateConnectionInfo(void* pData)
279 connection_profile_h profileHandle = pData;
280 connection_profile_type_e type = CONNECTION_PROFILE_TYPE_WIFI;
281 int ret = CONNECTION_ERROR_NONE;
283 MutexGuard locked(*_pLock);
287 ret = connection_profile_get_type(profileHandle, &type);
288 SysLog(NID_NET, "The return value from connection_profile_get_type() is %d, Type is %d", ret, type);
289 if (type == CONNECTION_PROFILE_TYPE_WIFI)
291 _bearerType = NET_BEARER_WIFI;
295 _bearerType = NET_BEARER_PS;
298 _connectionState = NET_CONNECTION_STATE_STARTED;
299 _pConnectionInfo->Update(pData, true);
303 _bearerType = NET_BEARER_NONE;
304 _connectionState = NET_CONNECTION_STATE_STOPPED;
305 _pConnectionInfo->Clear();
312 _DefaultSystemNetConnection::GetConnectionHandle(void) const
314 return __pConnectionHandle.get();