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_WifiDirectSystemNetConnection.cpp
19 * @brief This is the implementation file for _WifiDirectSystemNetConnection class.
22 * This file contains the implementation of _WifiDirectSystemNetConnection class.
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"
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Base::Runtime;
47 using namespace Tizen::Net::Wifi;
49 namespace Tizen { namespace Net {
51 static const char* _WIFI_DIRECT_LIBRARY_NAME = "libosp-wifi.so";
53 _WifiDirectSystemNetConnection::_WifiDirectSystemNetConnection(void)
59 _WifiDirectSystemNetConnection::~_WifiDirectSystemNetConnection(void)
61 if (__pDllHandle != null)
63 if (__pDevice != null)
65 void(*pDeleteFunction)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener) = null;
67 pDeleteFunction = reinterpret_cast<void(*)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener)>(dlsym(__pDllHandle, "_WifiDirectDeviceImpl_DeleteWifiDirectDevice"));
68 if (pDeleteFunction != null)
70 pDeleteFunction(__pDevice, this, this, this);
75 dlclose(__pDllHandle);
80 _WifiDirectSystemNetConnection::Construct(void)
83 WifiDirectDevice*(*pCreateFunction)(IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener) = null;
84 bool(*pIsGroupMemberFunction)(void) = null;
86 SysAssertf(__pDevice == null,
87 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
89 r = _SystemNetConnection::Initialize(L"WIFI-DIRECT");
90 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
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));
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));
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));
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));
108 if (pIsGroupMemberFunction())
110 UpdateConnectionInfo(true);
114 UpdateConnectionInfo(false);
120 _SystemNetConnection::Deinitialize();
122 if (__pDllHandle != null)
124 if (__pDevice != null)
126 void(*pDeleteFunction)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener) = null;
128 pDeleteFunction = reinterpret_cast<void(*)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener)>(dlsym(__pDllHandle, "_WifiDirectDeviceImpl_DeleteWifiDirectDevice"));
129 if (pDeleteFunction != null)
131 pDeleteFunction(__pDevice, this, this, this);
136 dlclose(__pDllHandle);
143 _WifiDirectSystemNetConnection::Start(_NetConnectionEvent& event)
145 result r = E_SUCCESS;
147 SysAssertf(_pEventList != null, "Not yet constructed. Construct() should be called before use.");
149 if ((_connectionState != NET_CONNECTION_STATE_STARTED) && (__pDllHandle != null))
151 bool(*pIsGroupMemberFunction)(void) = null;
153 pIsGroupMemberFunction = reinterpret_cast<bool(*)(void)>(dlsym(__pDllHandle, "_WifiDirectDeviceImpl_IsGroupMember"));
154 if (pIsGroupMemberFunction != null)
156 if (pIsGroupMemberFunction())
158 SysLog(NID_NET, "[%ls] Update to started state.", _name.GetPointer());
159 UpdateConnectionInfo(true);
164 MutexGuard locked(*_pLock);
166 if (_connectionState == NET_CONNECTION_STATE_STARTED)
168 SysLog(NID_NET, "[%ls] is already connected.", _name.GetPointer());
170 if (event.GetConnectionState() != NET_CONNECTION_STATE_STARTED)
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.");
175 event.SetConnectionState(NET_CONNECTION_STATE_STARTED);
176 r = event.FireAsync(*pEventArg);
177 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
185 SysLogException(NID_NET, E_INVALID_CONNECTION,
186 "[%s] %ls is NOT connected.", GetErrorMessage(E_INVALID_CONNECTION), _name.GetPointer());
187 r = E_INVALID_CONNECTION;
196 _WifiDirectSystemNetConnection::OnWifiDirectDeviceActivated(WifiDirectDeviceId localDeviceId, result r)
198 SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectDeviceActivated event with the result %s (Ignore)", GetErrorMessage(r));
202 _WifiDirectSystemNetConnection::OnWifiDirectDeviceDeactivated(WifiDirectDeviceId localDeviceId, result r)
204 SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectDeviceDeactivated event with the result %s", GetErrorMessage(r));
207 UpdateConnectionInfo(false);
211 _WifiDirectSystemNetConnection::OnWifiDirectGroupCreatedN(WifiDirectDeviceId localDeviceId,
212 const WifiDirectGroupInfo& wifiDirectGroupInfo, const WifiDirectDeviceInfo& wifiDirectGroupOwnerDeviceInfo,
213 WifiDirectGroupMember* pWifiDirectMember, result r)
215 SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectGroupCreatedN event with the result %s", GetErrorMessage(r));
220 UpdateConnectionInfo(true);
225 UpdateConnectionInfo(false);
228 delete pWifiDirectMember;
232 _WifiDirectSystemNetConnection::OnWifiDirectScanCompletedN(WifiDirectDeviceId localDeviceId, IList* pWifiDirectDeviceInfoList,
235 SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectScanCompletedN event with the result %s (Ignore)", GetErrorMessage(r));
237 if (pWifiDirectDeviceInfoList != null)
239 pWifiDirectDeviceInfoList->RemoveAll(true);
240 delete pWifiDirectDeviceInfoList;
245 _WifiDirectSystemNetConnection::OnWifiDirectAssociationCompleted(WifiDirectDeviceId localDeviceId,
246 const WifiDirectDeviceInfo& wifiDirectGroupOwnerDeviceInfo, result r)
248 SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectAssociationCompleted event with the result %s", GetErrorMessage(r));
253 UpdateConnectionInfo(true);
258 UpdateConnectionInfo(false);
263 _WifiDirectSystemNetConnection::OnWifiDirectClientAssociated(WifiDirectDeviceId localDeviceId, const WifiDirectDeviceInfo& wifiDirectClientInfo)
265 SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectClientAssociated event (Ignore)");
269 _WifiDirectSystemNetConnection::OnWifiDirectClientDisassociated(WifiDirectDeviceId localDeviceId, const WifiDirectDeviceInfo& wifiDirectClientInfo, WifiDirectAssociationTerminationReason reason)
271 SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectClientDisassociated event (Ignore)");
275 _WifiDirectSystemNetConnection::OnWifiDirectGroupDestroyed(WifiDirectDeviceId localDeviceId, result r)
277 SysLog(NID_NET, "Enter with Result[%s].", GetErrorMessage(r));
280 UpdateConnectionInfo(false);
282 SysLog(NID_NET, "Exit.");
286 _WifiDirectSystemNetConnection::OnWifiDirectGroupMemberInfoServiceStarted(WifiDirectDeviceId localDeviceId, const NetConnection* pNetConnection, result r)
288 SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectGroupMemberInfoServiceStarted event with the result %s (Ignore)", GetErrorMessage(r));
292 _WifiDirectSystemNetConnection::OnWifiDirectGroupMemberInfoServiceStopped(WifiDirectDeviceId localDeviceId, result r)
294 SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectGroupMemberInfoServiceStopped event with the result %s (Ignore)", GetErrorMessage(r));
298 _WifiDirectSystemNetConnection::OnWifiDirectAssociationTerminated(WifiDirectDeviceId localDeviceId, WifiDirectAssociationTerminationReason reason, result r)
300 SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectAssociationTerminated event with the result %s", GetErrorMessage(r));
303 UpdateConnectionInfo(false);
307 _WifiDirectSystemNetConnection::OnWifiDirectGroupOwnerInfoReceived(WifiDirectDeviceId localDeviceId, const WifiDirectDeviceInfo& wifiDirectGroupOwnerDeviceInfo, result r)
309 SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectGroupOwnerInfoReceived event with the result %s (Ignore)", GetErrorMessage(r));
313 _WifiDirectSystemNetConnection::OnWifiDirectAllGroupMemberInfoReceivedN(WifiDirectDeviceId localDeviceId, IList* pWifiDirectDeviceInfoList, result r)
315 SysLog(NID_NET, "WifiDirectSystemNetConnection received OnWifiDirectAllGroupMemberInfoReceivedN event with the result %s (Ignore)", GetErrorMessage(r));
317 if (pWifiDirectDeviceInfoList != null)
319 pWifiDirectDeviceInfoList->RemoveAll(true);
320 delete pWifiDirectDeviceInfoList;
325 _WifiDirectSystemNetConnection::UpdateConnectionInfo(bool isStarted)
327 MutexGuard locked(*_pLock);
331 int ret = WIFI_DIRECT_ERROR_NONE;
332 char* pDeviceName = null;
333 char* pLocalAddress = null;
334 char* pSubnetMaskAddress = null;
335 char* pGatewayAddress = null;
338 String subnetMaskAddress;
339 String gatewayAddress;
341 ret = wifi_direct_get_network_interface_name(&pDeviceName);
342 if ((ret == WIFI_DIRECT_ERROR_NONE) && (pDeviceName != null))
344 SysSecureLog(NID_NET, "DeviceName is %s", pDeviceName);
346 deviceName = String(pDeviceName);
351 SysLog(NID_NET, "Failed result from wifi_direct_get_network_interface_name() is %d", ret);
356 ret = wifi_direct_get_ip_address(&pLocalAddress);
357 if ((ret == WIFI_DIRECT_ERROR_NONE) && (pLocalAddress != null))
359 SysSecureLog(NID_NET, "LocalAddress is %s", pLocalAddress);
361 localAddress = String(pLocalAddress);
366 SysLog(NID_NET, "Failed result from wifi_direct_get_ip_address() is %d", ret);
369 ret = wifi_direct_get_subnet_mask(&pSubnetMaskAddress);
370 if ((ret == WIFI_DIRECT_ERROR_NONE) && (pSubnetMaskAddress != null))
372 SysSecureLog(NID_NET, "SubnetMaskAddress is %s", pSubnetMaskAddress);
374 subnetMaskAddress = String(pSubnetMaskAddress);
375 free(pSubnetMaskAddress);
379 SysLog(NID_NET, "Failed result from wifi_direct_get_subnet_mask() is %d", ret);
382 ret = wifi_direct_get_gateway_address(&pGatewayAddress);
383 if ((ret == WIFI_DIRECT_ERROR_NONE) && (pGatewayAddress != null))
385 SysSecureLog(NID_NET, "GatewayAddress is %s", pGatewayAddress);
387 gatewayAddress = String(pGatewayAddress);
388 free(pGatewayAddress);
392 SysLog(NID_NET, "Failed result from wifi_direct_get_gateway_address() is %d", ret);
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);
407 _bearerType = NET_BEARER_NONE;
408 _connectionState = NET_CONNECTION_STATE_STOPPED;
409 _pConnectionInfo->Clear();