Apply SecureLog.
[platform/framework/native/net.git] / src / FNet_WifiDirectSystemNetConnection.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_WifiDirectSystemNetConnection.cpp
19  * @brief               This is the implementation file for _WifiDirectSystemNetConnection class.
20  * @version     3.0
21  *
22  * This file contains the implementation of _WifiDirectSystemNetConnection class.
23  */
24
25 #include <dlfcn.h>
26 #include <wifi-direct.h>
27 #include <FNetIp4Address.h>
28 #include <FNetNetConnection.h>
29 #include <FNetNetConnectionInfo.h>
30 #include <FNetWifiWifiDirectDevice.h>
31 #include <FNetWifiWifiDirectGroupMember.h>
32 #include <FNetWifiWifiDirectDeviceManager.h>
33 #include <FBaseRtMutexGuard.h>
34 #include <FBaseSysLog.h>
35 #include <FBase_StringConverter.h>
36 #include "FNet_NetTypes.h"
37 #include "FNet_NetConnectionInfoImpl.h"
38 #include "FNet_WifiDirectSystemNetConnection.h"
39 #include "FNet_NetConnectionEvent.h"
40 #include "FNet_NetConnectionEventArg.h"
41 #include "FNet_NetUtility.h"
42
43 using namespace std;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Base::Runtime;
47 using namespace Tizen::Net::Wifi;
48
49 namespace Tizen { namespace Net {
50
51 static const char* _WIFI_DIRECT_LIBRARY_NAME = "libosp-wifi.so";
52
53 _WifiDirectSystemNetConnection::_WifiDirectSystemNetConnection(void)
54         : __pDllHandle(null)
55         , __pDevice(null)
56 {
57 }
58
59 _WifiDirectSystemNetConnection::~_WifiDirectSystemNetConnection(void)
60 {
61         if (__pDllHandle != null)
62         {
63                 if (__pDevice != null)
64                 {
65                         void(*pDeleteFunction)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener) = null;
66
67                         pDeleteFunction = reinterpret_cast<void(*)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener)>(dlsym(__pDllHandle, "_WifiDirectDeviceImpl_DeleteWifiDirectDevice"));
68                         if (pDeleteFunction != null)
69                         {
70                                 pDeleteFunction(__pDevice, this, this, this);
71                                 __pDevice = null;
72                         }
73                 }
74
75                 dlclose(__pDllHandle);
76         }
77 }
78
79 result
80 _WifiDirectSystemNetConnection::Construct(void)
81 {
82         result r = E_SUCCESS;
83         WifiDirectDevice*(*pCreateFunction)(IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener) = null;
84         bool(*pIsGroupMemberFunction)(void) = null;
85
86         SysAssertf(__pDevice == null,
87                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
88
89         r = _SystemNetConnection::Initialize(L"WIFI-DIRECT");
90         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
91
92         __pDllHandle = dlopen(_WIFI_DIRECT_LIBRARY_NAME, RTLD_LAZY);
93         SysTryCatch(NID_NET, __pDllHandle != null, r = E_SYSTEM, E_SYSTEM,
94                         "[%s] A system error has been occurred. Failed to open wifi library.", GetErrorMessage(E_SYSTEM));
95
96         pCreateFunction = reinterpret_cast<WifiDirectDevice*(*)(IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener)>(dlsym(__pDllHandle, "_WifiDirectDeviceManagerImpl_GetWifiDirectDeviceN"));
97         SysTryCatch(NID_NET, pCreateFunction != null, r = E_SYSTEM, E_SYSTEM,
98                         "[%s] A system error has been occurred. Failed to get a function pointer.", GetErrorMessage(E_SYSTEM));
99
100         __pDevice = pCreateFunction(this, this, this);
101         SysTryCatch(NID_NET, __pDevice != null, r = E_SYSTEM, E_SYSTEM,
102                         "[%s] A system error has been occurred. Failed to get a WifiDirectDevice instance.", GetErrorMessage(E_SYSTEM));
103
104         pIsGroupMemberFunction = reinterpret_cast<bool(*)(void)>(dlsym(__pDllHandle, "_WifiDirectDeviceImpl_IsGroupMember"));
105         SysTryCatch(NID_NET, pIsGroupMemberFunction != null, r = E_SYSTEM, E_SYSTEM,
106                         "[%s] A system error has been occurred. Failed to get a function pointer.", GetErrorMessage(E_SYSTEM));
107
108         if (pIsGroupMemberFunction())
109         {
110                 UpdateConnectionInfo(true);
111         }
112         else
113         {
114                 UpdateConnectionInfo(false);
115         }
116
117         return r;
118
119 CATCH:
120         _SystemNetConnection::Deinitialize();
121
122         if (__pDllHandle != null)
123         {
124                 if (__pDevice != null)
125                 {
126                         void(*pDeleteFunction)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener) = null;
127
128                         pDeleteFunction = reinterpret_cast<void(*)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener)>(dlsym(__pDllHandle, "_WifiDirectDeviceImpl_DeleteWifiDirectDevice"));
129                         if (pDeleteFunction != null)
130                         {
131                                 pDeleteFunction(__pDevice, this, this, this);
132                                 __pDevice = null;
133                         }
134                 }
135
136                 dlclose(__pDllHandle);
137         }
138
139         return r;
140 }
141
142 result
143 _WifiDirectSystemNetConnection::Start(_NetConnectionEvent& event)
144 {
145         result r = E_SUCCESS;
146
147         SysAssertf(_pEventList != null, "Not yet constructed. Construct() should be called before use.");
148
149         if ((_connectionState != NET_CONNECTION_STATE_STARTED) && (__pDllHandle != null))
150         {
151                 bool(*pIsGroupMemberFunction)(void) = null;
152
153                 pIsGroupMemberFunction = reinterpret_cast<bool(*)(void)>(dlsym(__pDllHandle, "_WifiDirectDeviceImpl_IsGroupMember"));
154                 if (pIsGroupMemberFunction != null)
155                 {
156                         if (pIsGroupMemberFunction())
157                         {
158                                 SysLog(NID_NET, "[%ls] Update to started state.", _name.GetPointer());
159                                 UpdateConnectionInfo(true);
160                         }
161                 }
162         }
163
164         MutexGuard locked(*_pLock);
165
166         if (_connectionState == NET_CONNECTION_STATE_STARTED)
167         {
168                 SysLog(NID_NET, "[%ls] is already connected.", _name.GetPointer());
169
170                 if (event.GetConnectionState() != NET_CONNECTION_STATE_STARTED)
171                 {
172                         unique_ptr<_NetConnectionEventArg> pEventArg(new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, E_SUCCESS));
173                         SysTryReturnResult(NID_NET, pEventArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
174
175                         event.SetConnectionState(NET_CONNECTION_STATE_STARTED);
176                         r = event.FireAsync(*pEventArg);
177                         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
178
179                         _refCount++;
180                         pEventArg.release();
181                 }
182         }
183         else
184         {
185                 SysLogException(NID_NET, E_INVALID_CONNECTION,
186                                 "[%s] %ls is NOT connected.", GetErrorMessage(E_INVALID_CONNECTION), _name.GetPointer());
187                 r = E_INVALID_CONNECTION;
188         }
189
190         locked.Unlock();
191
192         return r;
193 }
194
195 void
196 _WifiDirectSystemNetConnection::OnWifiDirectDeviceActivated(WifiDirectDeviceId localDeviceId, result r)
197 {
198         SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectDeviceActivated event with the result %s (Ignore)", GetErrorMessage(r));
199 }
200
201 void
202 _WifiDirectSystemNetConnection::OnWifiDirectDeviceDeactivated(WifiDirectDeviceId localDeviceId, result r)
203 {
204         SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectDeviceDeactivated event with the result %s", GetErrorMessage(r));
205
206         HandleStopEvent(r);
207         UpdateConnectionInfo(false);
208 }
209
210 void
211 _WifiDirectSystemNetConnection::OnWifiDirectGroupCreatedN(WifiDirectDeviceId localDeviceId,
212                 const WifiDirectGroupInfo& wifiDirectGroupInfo, const WifiDirectDeviceInfo& wifiDirectGroupOwnerDeviceInfo,
213                 WifiDirectGroupMember* pWifiDirectMember, result r)
214 {
215         SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectGroupCreatedN event with the result %s", GetErrorMessage(r));
216
217         if (r == E_SUCCESS)
218         {
219                 HandleStartEvent();
220                 UpdateConnectionInfo(true);
221         }
222         else
223         {
224                 HandleStopEvent(r);
225                 UpdateConnectionInfo(false);
226         }
227
228         delete pWifiDirectMember;
229 }
230
231 void
232 _WifiDirectSystemNetConnection::OnWifiDirectScanCompletedN(WifiDirectDeviceId localDeviceId, IList* pWifiDirectDeviceInfoList,
233                 result r)
234 {
235         SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectScanCompletedN event with the result %s (Ignore)", GetErrorMessage(r));
236
237         if (pWifiDirectDeviceInfoList != null)
238         {
239                 pWifiDirectDeviceInfoList->RemoveAll(true);
240                 delete pWifiDirectDeviceInfoList;
241         }
242 }
243
244 void
245 _WifiDirectSystemNetConnection::OnWifiDirectAssociationCompleted(WifiDirectDeviceId localDeviceId,
246                 const WifiDirectDeviceInfo& wifiDirectGroupOwnerDeviceInfo, result r)
247 {
248         SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectAssociationCompleted event with the result %s", GetErrorMessage(r));
249
250         if (r == E_SUCCESS)
251         {
252                 HandleStartEvent();
253                 UpdateConnectionInfo(true);
254         }
255         else
256         {
257                 HandleStopEvent(r);
258                 UpdateConnectionInfo(false);
259         }
260 }
261
262 void
263 _WifiDirectSystemNetConnection::OnWifiDirectClientAssociated(WifiDirectDeviceId localDeviceId, const WifiDirectDeviceInfo& wifiDirectClientInfo)
264 {
265         SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectClientAssociated event (Ignore)");
266 }
267
268 void
269 _WifiDirectSystemNetConnection::OnWifiDirectClientDisassociated(WifiDirectDeviceId localDeviceId, const WifiDirectDeviceInfo& wifiDirectClientInfo, WifiDirectAssociationTerminationReason reason)
270 {
271         SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectClientDisassociated event (Ignore)");
272 }
273
274 void
275 _WifiDirectSystemNetConnection::OnWifiDirectGroupDestroyed(WifiDirectDeviceId localDeviceId, result r)
276 {
277         SysLog(NID_NET, "Enter with Result[%s].", GetErrorMessage(r));
278
279         HandleStopEvent(r);
280         UpdateConnectionInfo(false);
281
282         SysLog(NID_NET, "Exit.");
283 }
284
285 void
286 _WifiDirectSystemNetConnection::OnWifiDirectGroupMemberInfoServiceStarted(WifiDirectDeviceId localDeviceId, const NetConnection* pNetConnection, result r)
287 {
288         SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectGroupMemberInfoServiceStarted event with the result %s (Ignore)", GetErrorMessage(r));
289 }
290
291 void
292 _WifiDirectSystemNetConnection::OnWifiDirectGroupMemberInfoServiceStopped(WifiDirectDeviceId localDeviceId, result r)
293 {
294         SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectGroupMemberInfoServiceStopped event with the result %s (Ignore)", GetErrorMessage(r));
295 }
296
297 void
298 _WifiDirectSystemNetConnection::OnWifiDirectAssociationTerminated(WifiDirectDeviceId localDeviceId, WifiDirectAssociationTerminationReason reason, result r)
299 {
300         SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectAssociationTerminated event with the result %s", GetErrorMessage(r));
301
302         HandleStopEvent(r);
303         UpdateConnectionInfo(false);
304 }
305
306 void
307 _WifiDirectSystemNetConnection::OnWifiDirectGroupOwnerInfoReceived(WifiDirectDeviceId localDeviceId, const WifiDirectDeviceInfo& wifiDirectGroupOwnerDeviceInfo, result r)
308 {
309         SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectGroupOwnerInfoReceived event with the result %s (Ignore)", GetErrorMessage(r));
310 }
311
312 void
313 _WifiDirectSystemNetConnection::OnWifiDirectAllGroupMemberInfoReceivedN(WifiDirectDeviceId localDeviceId, IList* pWifiDirectDeviceInfoList, result r)
314 {
315         SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectAllGroupMemberInfoReceivedN event with the result %s (Ignore)", GetErrorMessage(r));
316
317         if (pWifiDirectDeviceInfoList != null)
318         {
319                 pWifiDirectDeviceInfoList->RemoveAll(true);
320                 delete pWifiDirectDeviceInfoList;
321         }
322 }
323
324 void
325 _WifiDirectSystemNetConnection::UpdateConnectionInfo(bool isStarted)
326 {
327         MutexGuard locked(*_pLock);
328
329         if (isStarted)
330         {
331                 int ret = WIFI_DIRECT_ERROR_NONE;
332                 char* pDeviceName = null;
333                 char* pLocalAddress = null;
334                 char* pSubnetMaskAddress = null;
335                 char* pGatewayAddress = null;
336                 String deviceName;
337                 String localAddress;
338                 String subnetMaskAddress;
339                 String gatewayAddress;
340
341                 ret = wifi_direct_get_network_interface_name(&pDeviceName);
342                 if ((ret == WIFI_DIRECT_ERROR_NONE) && (pDeviceName != null))
343                 {
344                         SysSecureLog(NID_NET, "DeviceName is %s", pDeviceName);
345
346                         deviceName = String(pDeviceName);
347                         free(pDeviceName);
348                 }
349                 else
350                 {
351                         SysLog(NID_NET, "Failed result from wifi_direct_get_network_interface_name() is %d", ret);
352
353                         deviceName.Clear();
354                 }
355
356                 ret = wifi_direct_get_ip_address(&pLocalAddress);
357                 if ((ret == WIFI_DIRECT_ERROR_NONE) && (pLocalAddress != null))
358                 {
359                         SysSecureLog(NID_NET, "LocalAddress is %s", pLocalAddress);
360
361                         localAddress = String(pLocalAddress);
362                         free(pLocalAddress);
363                 }
364                 else
365                 {
366                         SysLog(NID_NET, "Failed result from wifi_direct_get_ip_address() is %d", ret);
367                 }
368
369                 ret = wifi_direct_get_subnet_mask(&pSubnetMaskAddress);
370                 if ((ret == WIFI_DIRECT_ERROR_NONE) && (pSubnetMaskAddress != null))
371                 {
372                         SysSecureLog(NID_NET, "SubnetMaskAddress is %s", pSubnetMaskAddress);
373
374                         subnetMaskAddress = String(pSubnetMaskAddress);
375                         free(pSubnetMaskAddress);
376                 }
377                 else
378                 {
379                         SysLog(NID_NET, "Failed result from wifi_direct_get_subnet_mask() is %d", ret);
380                 }
381
382                 ret = wifi_direct_get_gateway_address(&pGatewayAddress);
383                 if ((ret == WIFI_DIRECT_ERROR_NONE) && (pGatewayAddress != null))
384                 {
385                         SysSecureLog(NID_NET, "GatewayAddress is %s", pGatewayAddress);
386
387                         gatewayAddress = String(pGatewayAddress);
388                         free(pGatewayAddress);
389                 }
390                 else
391                 {
392                         SysLog(NID_NET, "Failed result from wifi_direct_get_gateway_address() is %d", ret);
393                 }
394
395                 _bearerType = NET_BEARER_WIFI_DIRECT;
396                 _connectionState = NET_CONNECTION_STATE_STARTED;
397                 _pConnectionInfo->SetBearerType(NET_BEARER_WIFI_DIRECT);
398                 _pConnectionInfo->SetProtocolType(NET_PROTO_TYPE_IPV4);
399                 _pConnectionInfo->SetLocalAddressScheme(NET_ADDRESS_SCHEME_STATIC);
400                 _pConnectionInfo->SetLocalAddress(localAddress);
401                 _pConnectionInfo->SetSubnetMaskAddress(subnetMaskAddress);
402                 _pConnectionInfo->SetDefaultGatewayAddress(gatewayAddress);
403                 _pConnectionInfo->SetDeviceName(deviceName);
404         }
405         else
406         {
407                 _bearerType = NET_BEARER_NONE;
408                 _connectionState = NET_CONNECTION_STATE_STOPPED;
409                 _pConnectionInfo->Clear();
410         }
411
412         locked.Unlock();
413 }
414
415 } } // Tizen::Net