change version to 1.2.2.0
[platform/framework/native/net.git] / src / FNet_DefaultSystemNetConnection.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_DefaultSystemNetConnection.cpp
19  * @brief               This is the implementation file for _DefaultSystemNetConnection class.
20  * @version     3.0
21  *
22  * This file contains the implementation of _DefaultSystemNetConnection class.
23  */
24
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"
36
37 using namespace std;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Runtime;
41
42 namespace Tizen { namespace Net {
43
44 void
45 ActiveConnectionTypeChangedCallback(connection_type_e type, void* pUserData)
46 {
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;
52
53         SysLog(NID_NET, "ActiveConnectionTypeChangedCallback() has been called with type : %d", type);
54
55         if (type != CONNECTION_TYPE_DISCONNECTED)
56         {
57                 // Connected
58                 if ((type == CONNECTION_TYPE_WIFI) && (pConnection->GetBearerType() != NET_BEARER_WIFI))
59                 {
60                         isBearerChanged = true;
61                 }
62                 else if ((type == CONNECTION_TYPE_CELLULAR) && (pConnection->GetBearerType() != NET_BEARER_PS))
63                 {
64                         isBearerChanged = true;
65                 }
66
67                 NetConnectionState oldState = pConnection->GetConnectionState();
68
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);
72
73                 pConnection->UpdateConnectionInfo(profileHandle);
74                 connection_profile_destroy(profileHandle);
75
76                 if (oldState != NET_CONNECTION_STATE_STARTED)
77                 {
78                         pConnection->HandleStartEvent();
79                 }
80                 else if (isBearerChanged)
81                 {
82                         pConnection->HandleChangedEvent();
83                 }
84                 else
85                 {
86                         SysLog(NID_NET, "Ignore the event, because this is already in started state.");
87                 }
88         }
89         else
90         {
91                 // Not connected
92                 if ((pConnection->GetConnectionState() != NET_CONNECTION_STATE_NONE) && (pConnection->GetConnectionState() != NET_CONNECTION_STATE_STOPPED))
93                 {
94                         pConnection->UpdateConnectionInfo(null);
95                         pConnection->HandleStopEvent(E_NETWORK_FAILED);
96                 }
97                 else
98                 {
99                         SysLog(NID_NET, "Ignore the event, because this is already in stopped state.");
100                 }
101         }
102 }
103
104 _DefaultSystemNetConnection::_DefaultSystemNetConnection(void)
105         : __pConnectionHandle(null)
106 {
107 }
108
109 _DefaultSystemNetConnection::~_DefaultSystemNetConnection(void)
110 {
111 }
112
113 result
114 _DefaultSystemNetConnection::Construct(void)
115 {
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;
121
122         SysAssertf(__pConnectionHandle == null,
123                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
124
125         r = _SystemNetConnection::Initialize(L"DEFAULT");
126         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
127
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);
132
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);
136
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);
140
141         if (type != CONNECTION_TYPE_DISCONNECTED)
142         {
143                 // Default connection is ON
144                 SysLog(NID_NET, "Default connection is ON. type=%d", type);
145
146                 connection_profile_h profileHandle = null;
147
148                 ret = connection_get_current_profile(connectionHandle, &profileHandle);
149                 if (ret == CONNECTION_ERROR_NONE)
150                 {
151                         UpdateConnectionInfo(profileHandle);
152                         connection_profile_destroy(profileHandle);
153                 }
154                 else
155                 {
156                         SysLog(NID_NET, "The return value from connection_get_current_profile() is %d", ret);
157                         UpdateConnectionInfo(null);
158                 }
159         }
160         else
161         {
162                 // Default connection is OFF
163                 SysLog(NID_NET, "Default connection is OFF.");
164                 UpdateConnectionInfo(null);
165         }
166
167         __pConnectionHandle = move(pConnectionHandle);
168
169         return r;
170
171 CATCH:
172         _SystemNetConnection::Deinitialize();
173
174         return r;
175 }
176
177 void
178 _DefaultSystemNetConnection::HandleStartEvent(void)
179 {
180         _NetConnectionEvent* pEvent = null;
181         _NetConnectionEventArg* pEventArg = null;
182
183         SysLog(NID_NET, "[%ls] Notify started event.", _name.GetPointer());
184
185         MutexGuard locked(*_pLock);
186
187         unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
188         if (pEnum != null)
189         {
190                 while (pEnum->MoveNext() == E_SUCCESS)
191                 {
192                         pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
193                         if (pEvent != null)
194                         {
195                                 // Sends event which doesn't invoke start.
196                                 if (pEvent->GetConnectionState() != NET_CONNECTION_STATE_STARTED)
197                                 {
198                                         pEvent->SetConnectionState(NET_CONNECTION_STATE_STARTED);
199                                         pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, E_SUCCESS);
200                                         if (pEventArg != null)
201                                         {
202                                                 pEvent->FireAsync(*pEventArg);
203                                         }
204                                 }
205                         }
206                 }
207         }
208
209         locked.Unlock();
210 }
211
212 void
213 _DefaultSystemNetConnection::HandleStopEvent(result error)
214 {
215         _NetConnectionEvent* pEvent = null;
216         _NetConnectionEventArg* pEventArg = null;
217
218         SysLog(NID_NET, "[%ls] Notify stopped event.", _name.GetPointer());
219
220         MutexGuard locked(*_pLock);
221
222         _refCount = 0;
223
224         unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
225         if (pEnum != null)
226         {
227                 while (pEnum->MoveNext() == E_SUCCESS)
228                 {
229                         pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
230                         if (pEvent != null)
231                         {
232                                 pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
233                                 pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, error);
234                                 if (pEventArg != null)
235                                 {
236                                         pEvent->FireAsync(*pEventArg);
237                                 }
238                         }
239                 }
240         }
241
242         locked.Unlock();
243 }
244
245 void
246 _DefaultSystemNetConnection::HandleChangedEvent(void)
247 {
248         _NetConnectionEvent* pEvent = null;
249         _NetConnectionEventArg* pEventArg = null;
250
251         SysLog(NID_NET, "[%ls] Notify bearer changed event.", _name.GetPointer());
252
253         MutexGuard locked(*_pLock);
254
255         unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
256         if (pEnum != null)
257         {
258                 while (pEnum->MoveNext() == E_SUCCESS)
259                 {
260                         pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
261                         if (pEvent != null)
262                         {
263                                 pEvent->SetConnectionState(NET_CONNECTION_STATE_STARTED);
264                                 pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_CHANGED, E_SUCCESS);
265                                 if (pEventArg != null)
266                                 {
267                                         pEvent->FireAsync(*pEventArg);
268                                 }
269                         }
270                 }
271         }
272
273         locked.Unlock();
274 }
275
276 void
277 _DefaultSystemNetConnection::UpdateConnectionInfo(void* pData)
278 {
279         connection_profile_h profileHandle = pData;
280         connection_profile_type_e type = CONNECTION_PROFILE_TYPE_WIFI;
281         int ret = CONNECTION_ERROR_NONE;
282
283         MutexGuard locked(*_pLock);
284
285         if (pData != null)
286         {
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)
290                 {
291                         _bearerType = NET_BEARER_WIFI;
292                 }
293                 else
294                 {
295                         _bearerType = NET_BEARER_PS;
296                 }
297
298                 _connectionState = NET_CONNECTION_STATE_STARTED;
299                 _pConnectionInfo->Update(pData, true);
300         }
301         else
302         {
303                 _bearerType = NET_BEARER_NONE;
304                 _connectionState = NET_CONNECTION_STATE_STOPPED;
305                 _pConnectionInfo->Clear();
306         }
307
308         locked.Unlock();
309 }
310
311 void*
312 _DefaultSystemNetConnection::GetConnectionHandle(void) const
313 {
314         return __pConnectionHandle.get();
315 }
316
317 } } // Tizen::Net