merge with master
[framework/osp/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                 if (isBearerChanged)
68                 {
69                         SysLog(NID_NET, "Invoke stop event, because bearer is changed.");
70                         pConnection->HandleStopEvent(E_NETWORK_FAILED);
71                         pConnection->UpdateConnectionInfo(null);
72                 }
73
74                 if (pConnection->GetConnectionState() == NET_CONNECTION_STATE_STARTED)
75                 {
76                         SysLog(NID_NET, "Ignore the event, because this is already in started state.");
77                         return;
78                 }
79
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);
83
84                 pConnection->UpdateConnectionInfo(profileHandle);
85                 pConnection->HandleStartEvent();
86
87                 connection_profile_destroy(profileHandle);
88         }
89         else
90         {
91                 // Not connected
92                 pConnection->HandleStopEvent(E_NETWORK_FAILED);
93                 pConnection->UpdateConnectionInfo(null);
94         }
95 }
96
97 _DefaultSystemNetConnection::_DefaultSystemNetConnection(void)
98         : __pConnectionHandle(null)
99 {
100 }
101
102 _DefaultSystemNetConnection::~_DefaultSystemNetConnection(void)
103 {
104 }
105
106 result
107 _DefaultSystemNetConnection::Construct(void)
108 {
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;
114
115         SysAssertf(__pConnectionHandle == null,
116                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
117
118         r = _SystemNetConnection::Initialize(L"DEFAULT");
119         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
120
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);
125
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);
129
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);
133
134         if (type != CONNECTION_TYPE_DISCONNECTED)
135         {
136                 // Default connection is ON
137                 SysLog(NID_NET, "Default connection is ON. type=%d", type);
138
139                 connection_profile_h profileHandle = null;
140
141                 ret = connection_get_current_profile(connectionHandle, &profileHandle);
142                 if (ret == CONNECTION_ERROR_NONE)
143                 {
144                         UpdateConnectionInfo(profileHandle);
145                         connection_profile_destroy(profileHandle);
146                 }
147                 else
148                 {
149                         SysLog(NID_NET, "The return value from connection_get_current_profile() is %d", ret);
150                         UpdateConnectionInfo(null);
151                 }
152         }
153         else
154         {
155                 // Default connection is OFF
156                 SysLog(NID_NET, "Default connection is OFF.");
157                 UpdateConnectionInfo(null);
158         }
159
160         __pConnectionHandle = move(pConnectionHandle);
161
162         return r;
163
164 CATCH:
165         _SystemNetConnection::Deinitialize();
166
167         return r;
168 }
169
170 void
171 _DefaultSystemNetConnection::HandleStartEvent(void)
172 {
173         _NetConnectionEvent* pEvent = null;
174         _NetConnectionEventArg* pEventArg = null;
175
176         MutexGuard locked(*_pLock);
177
178         unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
179         if (pEnum != null)
180         {
181                 while (pEnum->MoveNext() == E_SUCCESS)
182                 {
183                         pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
184                         if (pEvent != null)
185                         {
186                                 // Sends event which doesn't invoke start.
187                                 if (pEvent->GetConnectionState() != NET_CONNECTION_STATE_STARTED)
188                                 {
189                                         pEvent->SetConnectionState(NET_CONNECTION_STATE_STARTED);
190                                         pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, E_SUCCESS);
191                                         if (pEventArg != null)
192                                         {
193                                                 pEvent->FireAsync(*pEventArg);
194                                         }
195                                 }
196                         }
197                 }
198         }
199
200         locked.Unlock();
201 }
202
203 void
204 _DefaultSystemNetConnection::UpdateConnectionInfo(void* pData)
205 {
206         connection_profile_h profileHandle = pData;
207         connection_profile_type_e type = CONNECTION_PROFILE_TYPE_WIFI;
208         int ret = CONNECTION_ERROR_NONE;
209
210         MutexGuard locked(*_pLock);
211
212         if (pData != null)
213         {
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)
217                 {
218                         _bearerType = NET_BEARER_WIFI;
219                 }
220                 else
221                 {
222                         _bearerType = NET_BEARER_PS;
223                 }
224
225                 _connectionState = NET_CONNECTION_STATE_STARTED;
226                 _pConnectionInfo->Update(pData, true);
227         }
228         else
229         {
230                 _bearerType = NET_BEARER_NONE;
231                 _connectionState = NET_CONNECTION_STATE_STOPPED;
232                 _pConnectionInfo->Clear();
233         }
234
235         locked.Unlock();
236 }
237
238 void*
239 _DefaultSystemNetConnection::GetConnectionHandle(void) const
240 {
241         return __pConnectionHandle.get();
242 }
243
244 } } // Tizen::Net