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;
69 SysLog(NID_NET, "Invoke stop event, because bearer is changed.");
70 pConnection->HandleStopEvent(E_NETWORK_FAILED);
71 pConnection->UpdateConnectionInfo(null);
74 if (pConnection->GetConnectionState() == NET_CONNECTION_STATE_STARTED)
76 SysLog(NID_NET, "Ignore the event, because this is already in started state.");
80 ret = connection_get_current_profile(connectionHandle, &profileHandle);
81 SysTryReturnVoidResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
82 "[%s] A system error has been occurred. The return value from connection_get_current_profile() is %d", GetErrorMessage(E_SYSTEM), ret);
84 pConnection->UpdateConnectionInfo(profileHandle);
85 pConnection->HandleStartEvent();
87 connection_profile_destroy(profileHandle);
92 pConnection->HandleStopEvent(E_NETWORK_FAILED);
93 pConnection->UpdateConnectionInfo(null);
97 _DefaultSystemNetConnection::_DefaultSystemNetConnection(void)
98 : __pConnectionHandle(null)
102 _DefaultSystemNetConnection::~_DefaultSystemNetConnection(void)
107 _DefaultSystemNetConnection::Construct(void)
109 result r = E_SUCCESS;
110 unique_ptr<void, _ConnectionDeleter> pConnectionHandle;
111 int ret = CONNECTION_ERROR_NONE;
112 connection_h connectionHandle = null;
113 connection_type_e type = CONNECTION_TYPE_DISCONNECTED;
115 SysAssertf(__pConnectionHandle == null,
116 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
118 r = _SystemNetConnection::Initialize(L"DEFAULT");
119 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
121 ret = connection_create(&connectionHandle);
122 SysTryCatch(NID_NET, ret == CONNECTION_ERROR_NONE, r = E_SYSTEM, E_SYSTEM,
123 "[%s] A system error has been occurred. The return value from connection_create() is %d", GetErrorMessage(E_SYSTEM), ret);
124 pConnectionHandle.reset(connectionHandle);
126 ret = connection_set_type_changed_cb(connectionHandle, ActiveConnectionTypeChangedCallback, this);
127 SysTryCatch(NID_NET, ret == CONNECTION_ERROR_NONE, r = E_SYSTEM, E_SYSTEM,
128 "[%s] A system error has been occurred. The return value from connection_set_type_changed_cb() is %d", GetErrorMessage(E_SYSTEM), ret);
130 ret = connection_get_type(connectionHandle, &type);
131 SysTryCatch(NID_NET, ret == CONNECTION_ERROR_NONE, r = E_SYSTEM, E_SYSTEM,
132 "[%s] A system error has been occurred. The return value from connection_get_type() is %d", GetErrorMessage(E_SYSTEM), ret);
134 if (type != CONNECTION_TYPE_DISCONNECTED)
136 // Default connection is ON
137 SysLog(NID_NET, "Default connection is ON. type=%d", type);
139 connection_profile_h profileHandle = null;
141 ret = connection_get_current_profile(connectionHandle, &profileHandle);
142 if (ret == CONNECTION_ERROR_NONE)
144 UpdateConnectionInfo(profileHandle);
145 connection_profile_destroy(profileHandle);
149 SysLog(NID_NET, "The return value from connection_get_current_profile() is %d", ret);
150 UpdateConnectionInfo(null);
155 // Default connection is OFF
156 SysLog(NID_NET, "Default connection is OFF.");
157 UpdateConnectionInfo(null);
160 __pConnectionHandle = move(pConnectionHandle);
165 _SystemNetConnection::Deinitialize();
171 _DefaultSystemNetConnection::HandleStartEvent(void)
173 _NetConnectionEvent* pEvent = null;
174 _NetConnectionEventArg* pEventArg = null;
176 MutexGuard locked(*_pLock);
178 unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
181 while (pEnum->MoveNext() == E_SUCCESS)
183 pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
186 // Sends event which doesn't invoke start.
187 if (pEvent->GetConnectionState() != NET_CONNECTION_STATE_STARTED)
189 pEvent->SetConnectionState(NET_CONNECTION_STATE_STARTED);
190 pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, E_SUCCESS);
191 if (pEventArg != null)
193 pEvent->FireAsync(*pEventArg);
204 _DefaultSystemNetConnection::UpdateConnectionInfo(void* pData)
206 connection_profile_h profileHandle = pData;
207 connection_profile_type_e type = CONNECTION_PROFILE_TYPE_WIFI;
208 int ret = CONNECTION_ERROR_NONE;
210 MutexGuard locked(*_pLock);
214 ret = connection_profile_get_type(profileHandle, &type);
215 SysLog(NID_NET, "The return value from connection_profile_get_type() is %d, Type is %d", ret, type);
216 if (type == CONNECTION_PROFILE_TYPE_WIFI)
218 _bearerType = NET_BEARER_WIFI;
222 _bearerType = NET_BEARER_PS;
225 _connectionState = NET_CONNECTION_STATE_STARTED;
226 _pConnectionInfo->Update(pData, true);
230 _bearerType = NET_BEARER_NONE;
231 _connectionState = NET_CONNECTION_STATE_STOPPED;
232 _pConnectionInfo->Clear();
239 _DefaultSystemNetConnection::GetConnectionHandle(void) const
241 return __pConnectionHandle.get();