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_LocalDhcpServerImpl.cpp
19 * @brief This is the implementation file for the __LocalDhcpServerImpl Class.
21 * This header file contains implementation of the __LocalDhcpServerImpl Class.
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
31 #include <arpa/inet.h>
33 #include <FBaseObject.h>
34 #include <FBaseResult.h>
35 #include <FBaseString.h>
36 #include <FNetNetTypes.h>
37 #include <FNetIp4Address.h>
38 #include <FNetNetConnectionInfo.h>
39 #include <FNetNetConnection.h>
40 #include <FNetDhcpClientInfo.h>
41 #include <FNetLocalDhcpServer.h>
42 #include <FBaseSysLog.h>
43 #include "FNet_NetTypes.h"
44 #include <FNetWifiWifiDirectDevice.h>
45 #include <FNetWifiWifiDirectDeviceInfo.h>
46 #include "FNet_NetConnectionImpl.h"
47 #include "FNet_LocalDhcpServerImpl.h"
48 #include "FNet_DhcpClientInfoImpl.h"
49 #include "FNet_LocalDhcpServerEvent.h"
50 #include "FNet_LocalDhcpServerEventArg.h"
53 using namespace Tizen::Base;
54 using namespace Tizen::Base::Collection;
55 using namespace Tizen::Net::Wifi;
57 namespace Tizen { namespace Net
60 static const int _MAX_DHCP_CLIENT_COUNT = 8; //dependent on DHCP server config
62 static const char _PLATFORM_MAC_ADDRESS_DELIMITER = ':';
63 static const char _MAC_ADDRESS_DELIMITER = '-';
64 static const char* _WIFI_DIRECT_LIBRARY_NAME = "libosp-wifi.so";
67 * The _DhcpNetConnectionEventListenerImpl class can call listener's method. So, when an NetConnection event occurs,
68 * application can handle it appropriately.
70 class _DhcpNetConnectionEventListenerImpl
71 : public INetConnectionEventListener
75 _DhcpNetConnectionEventListenerImpl(void);
77 virtual ~_DhcpNetConnectionEventListenerImpl(void);
80 void SetConstructParams(_LocalDhcpServerEvent* pLocalDhcpServerEvent, tethering_h pUsbTetheringHandle);
84 void OnNetConnectionStarted(NetConnection& netConnection, result r);
86 void OnNetConnectionStopped(NetConnection& netConnection, result r);
88 void OnNetConnectionSuspended(NetConnection& netConnection);
90 void OnNetConnectionResumed(NetConnection& netConnection);
94 _DhcpNetConnectionEventListenerImpl(const _DhcpNetConnectionEventListenerImpl& value);
96 _DhcpNetConnectionEventListenerImpl& operator =(const _DhcpNetConnectionEventListenerImpl& rhs);
100 _LocalDhcpServerEvent* __pLocalDhcpServerEvent;// reference, no ownership
101 tethering_h __pUsbTetheringHandle;// no ownership
105 _DhcpNetConnectionEventListenerImpl::_DhcpNetConnectionEventListenerImpl()
106 : __pLocalDhcpServerEvent(null)
107 , __pUsbTetheringHandle(null)
112 _DhcpNetConnectionEventListenerImpl::~_DhcpNetConnectionEventListenerImpl(void)
117 _DhcpNetConnectionEventListenerImpl::OnNetConnectionStarted(NetConnection& netConnection, result r)
119 SysLog(NID_NET, "DhcpNetConnectionEventListener received an event, OnNetConnectionStarted.");
121 // check, if listener is null
122 if (__pLocalDhcpServerEvent == null)
125 SysLogException(NID_NET, E_SYSTEM,
126 "__DhcpNetConnectionEventListenerImpl::OnNetConnectionStarted Invalid state.");
131 // process connected event
132 _LocalDhcpServerImpl* pLocalDhcpServerImpl = null;
133 pLocalDhcpServerImpl = _LocalDhcpServerImpl::GetInstance(*__pLocalDhcpServerEvent->GetLocalDhcpServer());
134 SysTryReturnVoidResult(NID_NET, pLocalDhcpServerImpl != null, E_SYSTEM,
135 "[%s] A system error has been occurred. Failed to retrieve LocalDhcpServer instance.", GetErrorMessage(E_SYSTEM));
138 pLocalDhcpServerImpl->__isConnectedNetwork = true;
140 pLocalDhcpServerImpl->SetDhcpConnectedPeerInfoList(pLocalDhcpServerImpl->GetDhcpClientInfoListN(false));
142 if (netConnection.GetNetAccountId() == _DEFAULT_USB_ACCOUNT_ID)
144 SysLog(NID_NET, "OnNetConnectionStarted, Get DHCP Client info for USB");
146 ArrayList* pDhcpClientInfoList = null;
147 DhcpClientInfo* pDhcpClientInfo = null;
148 _LocalDhcpServerEventArg* pDhcpServerEventArg = null;
149 _NetDhcpServerEventType eventCode = NET_DHCP_SERVER_EVENT_CONNECT;
151 pDhcpClientInfoList = dynamic_cast<ArrayList*>(pLocalDhcpServerImpl->GetDhcpConnectedPeerInfoList());
152 SysTryReturnVoidResult(NID_NET, pDhcpClientInfoList != null, E_SYSTEM,
153 "[%s] A system error has been occurred. Failed to retrieve DhcpClientInfoList.", GetErrorMessage(E_SYSTEM));
155 // we know USB case only one entry exists
156 pDhcpClientInfo = dynamic_cast<DhcpClientInfo*>(pDhcpClientInfoList->GetAt(0));
157 SysTryReturnVoidResult(NID_NET, pDhcpClientInfo != null, E_SYSTEM,
158 "[%s] A system error has been occurred. Failed to retrieve DhcpClientInfo.", GetErrorMessage(E_SYSTEM));
160 pDhcpServerEventArg = new (std::nothrow) _LocalDhcpServerEventArg(eventCode, *pDhcpClientInfo);
161 SysTryReturnVoidResult(NID_NET, pDhcpServerEventArg != null, E_OUT_OF_MEMORY,
162 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
164 __pLocalDhcpServerEvent->Fire(*pDhcpServerEventArg);
166 else if (netConnection.GetNetAccountId() == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID)
168 SysLog(NID_NET, "OnNetConnectionStarted, Get DHCP Client info for WIFI-DIRECT");
170 //register for events
172 WifiDirectDevice*(*pRegisterListenerFunction)(IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener,
173 IWifiDirectGroupClientListener* pClientListener) = null;
175 pLocalDhcpServerImpl->__pDllHandle = dlopen(_WIFI_DIRECT_LIBRARY_NAME, RTLD_LAZY);
176 SysTryReturnVoidResult(NID_NET, pLocalDhcpServerImpl->__pDllHandle != null, E_SYSTEM,
177 "[%s] A system error has been occurred. Failed to open wifi library.", GetErrorMessage(E_SYSTEM));
179 pRegisterListenerFunction = reinterpret_cast<WifiDirectDevice*(*)(IWifiDirectDeviceListener* pDeviceListener, IWifiDirectGroupOwnerListener* pOwnerListener,
180 IWifiDirectGroupClientListener* pClientListener)>( dlsym(pLocalDhcpServerImpl->__pDllHandle, "_WifiDirectDeviceManagerImpl_GetWifiDirectDeviceN"));
181 SysTryReturnVoidResult(NID_NET, pRegisterListenerFunction != null, E_SYSTEM,
182 "[%s] A system error has been occurred. Failed to get a function pointer.", GetErrorMessage(E_SYSTEM));
184 pLocalDhcpServerImpl->__pDevice = pRegisterListenerFunction(null, pLocalDhcpServerImpl, null);
185 SysTryReturnVoidResult(NID_NET, pLocalDhcpServerImpl->__pDevice != null, E_SYSTEM,
186 "[%s] A system error has been occurred. Failed to get a WifiDirectDevice instance.", GetErrorMessage(E_SYSTEM));
190 // should not reach here.
191 SysAssertf(false, "OnNetConnectionStarted, Bearer not supported for DHCP");
199 _DhcpNetConnectionEventListenerImpl::OnNetConnectionStopped(NetConnection& netConnection, result r)
201 SysLog(NID_NET, "DhcpNetConnectionEventListener received an event, OnNetConnectionStopped.");
203 result error = E_SUCCESS;
204 _LocalDhcpServerEventArg* pDhcpServerEventArg = null;
205 _NetDhcpServerEventType eventCode = NET_DHCP_SERVER_EVENT_DISCONNECT;
208 if (__pLocalDhcpServerEvent == null)
211 SysLogException(NID_NET, E_SYSTEM,
212 "__DhcpNetConnectionEventListenerImpl::OnNetConnectionStopped pDhcpServerEvent is null.");
217 // process disconnect event
218 ArrayList* pDhcpConnectedPeerInfoList = null;
219 _LocalDhcpServerImpl* pLocalDhcpServerImpl = null;
221 pLocalDhcpServerImpl = _LocalDhcpServerImpl::GetInstance(*__pLocalDhcpServerEvent->GetLocalDhcpServer());
222 SysTryReturnVoidResult(NID_NET, pLocalDhcpServerImpl != null, E_SYSTEM,
223 "[%s] A system error has been occurred. Failed to retrieve LocalDhcpServer instance.", GetErrorMessage(E_SYSTEM));
225 if (netConnection.GetNetAccountId() == _DEFAULT_USB_ACCOUNT_ID)
227 SysLog(NID_NET, "OnNetConnectionStopped event received for USB");
229 else if (netConnection.GetNetAccountId() == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID)
231 SysLog(NID_NET, "OnNetConnectionStopped event received for WIFI-DIRECT");
233 //un-register for events
234 if (pLocalDhcpServerImpl->__pDllHandle != null)
236 if (pLocalDhcpServerImpl->__pDevice != null)
238 void(*pUnRegisterListenerFunction)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener,
239 IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener) = null;
241 pUnRegisterListenerFunction = reinterpret_cast<void(*)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener,
242 IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener)>(
243 dlsym(pLocalDhcpServerImpl->__pDllHandle, "_WifiDirectDeviceImpl_DeleteWifiDirectDevice"));
244 if (pUnRegisterListenerFunction != null)
246 pUnRegisterListenerFunction(pLocalDhcpServerImpl->__pDevice, null, pLocalDhcpServerImpl, null);
247 pLocalDhcpServerImpl->__pDevice = null;
248 SysLog(NID_NET, "Deleted an instance of WifiDirectDevice.");
251 dlclose(pLocalDhcpServerImpl->__pDllHandle);
252 pLocalDhcpServerImpl->__pDllHandle = null;
257 // should not reach here.
258 SysAssertf(false, "OnNetConnectionStopped, Bearer not supported for DHCP");
263 // notify user about disconnect, call for each peer info
264 pDhcpConnectedPeerInfoList = dynamic_cast<ArrayList*>(pLocalDhcpServerImpl->GetDhcpConnectedPeerInfoList());
265 SysTryReturnVoidResult(NID_NET, pDhcpConnectedPeerInfoList != null, E_SYSTEM,
266 "[%s] A system error has been occurred. Failed to retrieve DhcpConnectedPeerInfoList.", GetErrorMessage(E_SYSTEM));
268 unique_ptr<IEnumerator> pListEnumerator(pDhcpConnectedPeerInfoList->GetEnumeratorN());
270 SysTryReturnVoidResult(NID_NET, pListEnumerator != null, E_SYSTEM,
271 "[%s] A system error has been occurred. pListEnumerator is null.", GetErrorMessage(E_SYSTEM));
273 // send notify to user about disconnect
274 while (pListEnumerator->MoveNext() == E_SUCCESS)
276 DhcpClientInfo* pDhcpClientInfo = null;
277 pDhcpClientInfo = dynamic_cast<DhcpClientInfo*>(pListEnumerator->GetCurrent());
279 if (pDhcpClientInfo != null)
281 pDhcpServerEventArg = new (std::nothrow) _LocalDhcpServerEventArg(eventCode, *pDhcpClientInfo);
282 SysTryReturnVoidResult(NID_NET, pDhcpServerEventArg != null, E_OUT_OF_MEMORY,
283 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
285 __pLocalDhcpServerEvent->Fire(*pDhcpServerEventArg);
288 // Operation is complete, delete the elements
289 pDhcpConnectedPeerInfoList->RemoveAll(true);
291 // Remove NetConnectionListener
292 pLocalDhcpServerImpl->__isConnectedNetwork = false;
294 error = pLocalDhcpServerImpl->__pNetConnectionClone->RemoveNetConnectionListener(*pLocalDhcpServerImpl->__pNetConnectionListener);
295 SysTryReturnVoidResult(NID_NET, error == E_SUCCESS, E_SYSTEM,
296 "[%s] A system error has been occurred. Failed to remove the NetConnection listener.", GetErrorMessage(E_SYSTEM));
302 _DhcpNetConnectionEventListenerImpl::OnNetConnectionSuspended(NetConnection& netConnection)
304 SysLog(NID_NET, "DhcpNetConnectionEventListener received an event, OnNetConnectionSuspended.");
308 _DhcpNetConnectionEventListenerImpl::OnNetConnectionResumed(NetConnection& netConnection)
310 SysLog(NID_NET, "DhcpNetConnectionEventListener received an event, OnNetConnectionResumed.");
314 _DhcpNetConnectionEventListenerImpl::SetConstructParams(_LocalDhcpServerEvent* pLocalDhcpServerEvent,
315 tethering_h pUsbTetheringHandle)
317 __pLocalDhcpServerEvent = pLocalDhcpServerEvent;
318 __pUsbTetheringHandle = pUsbTetheringHandle;
322 //LocalDhcpServerImpl Implementation
324 _LocalDhcpServerImpl::_LocalDhcpServerImpl(void)
325 : __pLocalDhcpServerEvent(null)
326 , __pNetConnection(null)
327 , __pNetConnectionClone(null)
328 , __pNetConnectionListener(null)
329 , __pUsbTetheringHandle(null)
330 , __pDhcpConnectedPeerInfoList(null)
333 , __isConstructed(false)
334 , __isConnectedNetwork(false)
338 _LocalDhcpServerImpl::~_LocalDhcpServerImpl(void)
340 // deallocate memory for handle
341 if (__pUsbTetheringHandle != null)
343 tethering_destroy(__pUsbTetheringHandle);
344 __pUsbTetheringHandle = null;
347 if (__pDllHandle != null)
349 if (__pDevice != null)
351 void(*pUnRegisterListenerFunction)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener,
352 IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener) = null;
354 pUnRegisterListenerFunction = reinterpret_cast<void(*)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener,
355 IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener)>
356 (dlsym(__pDllHandle, "_WifiDirectDeviceImpl_DeleteWifiDirectDevice"));
357 if (pUnRegisterListenerFunction != null)
359 pUnRegisterListenerFunction(__pDevice, null, this, null);
364 dlclose(__pDllHandle);
369 _LocalDhcpServerImpl::Construct(const LocalDhcpServer* pLocalDhcpServer, const NetConnection& netConnection)
371 result r = E_SUCCESS;
372 _NetConnectionImpl* pNetConnectionImpl = null;
373 NetConnectionState netConnectionState = NET_CONNECTION_STATE_NONE;
374 unique_ptr<_DhcpNetConnectionEventListenerImpl> pNetConnectionListener(null);
375 unique_ptr<NetConnection> pNetConnectionClone(null);
377 SysAssertf(__isConstructed == false,
378 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
381 NetAccountId netAccountId = netConnection.GetNetAccountId();
382 SysTryReturnResult(NID_NET, (netAccountId == _DEFAULT_USB_ACCOUNT_ID || netAccountId == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID),
383 E_INVALID_ARG, "Invalid argument is used. The bearer type is not supported. bearerType=%d", netAccountId);
385 __pNetConnection = const_cast<NetConnection*>(&netConnection);
388 unique_ptr<_LocalDhcpServerEvent> pLocalDhcpServerEvent(new (std::nothrow) _LocalDhcpServerEvent());
389 SysTryReturnResult(NID_NET, pLocalDhcpServerEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
391 r = pLocalDhcpServerEvent->Construct(*(const_cast < const LocalDhcpServer* >(pLocalDhcpServer)));
392 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
393 "A system error has been occurred. Failed to construct LocalDhcpServerEvent instance.");
395 // we have to listen to connection events, so clone NetConnection and set listener
396 pNetConnectionImpl = const_cast<_NetConnectionImpl*>(_NetConnectionImpl::GetInstance(netConnection));
397 SysTryReturnResult(NID_NET, pNetConnectionImpl != null, E_SYSTEM,
398 "A system error has been occurred. Failed to retrieve NetConnection instance.");
400 // Creates new NetConnection for receiving the network event.
401 pNetConnectionClone.reset(pNetConnectionImpl->CopyInstanceN());
402 SysTryReturnResult(NID_NET, pNetConnectionClone != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
404 //set connection state
405 netConnectionState = __pNetConnection->GetConnectionState();
407 if (netConnectionState == NET_CONNECTION_STATE_STARTED ||
408 netConnectionState == NET_CONNECTION_STATE_RESUMED ||
409 netConnectionState == NET_CONNECTION_STATE_SUSPENDED)
411 __isConnectedNetwork = true;
414 // New CustomNetConnectionEventListener
415 pNetConnectionListener.reset(new (std::nothrow) _DhcpNetConnectionEventListenerImpl());
416 SysTryReturnResult(NID_NET, pNetConnectionListener != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
418 // Add NetConnectionListener
419 r = pNetConnectionClone->AddNetConnectionListener(*pNetConnectionListener);
420 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred. Failed to add the NetConnection listener.");
422 //check for connection type - USB or Wifi-Direct
423 if (__pNetConnection->GetNetAccountId() == _DEFAULT_USB_ACCOUNT_ID)
425 // create usb tethering handle
426 int err = TETHERING_ERROR_NONE;
428 err = tethering_create(&__pUsbTetheringHandle);
429 SysLog(NID_NET, "[0x%x] Return value of tethering_create", err);
431 SysTryReturnResult(NID_NET, err == TETHERING_ERROR_NONE, E_SYSTEM,
432 "A system error has been occurred. Failed to create tethering handle.");
434 SysTryReturnResult(NID_NET, __pUsbTetheringHandle != null, E_SYSTEM,
435 "A system error has been occurred. Failed to initialize Usb Tethering Handle.");
437 //if already connected
438 if (__isConnectedNetwork)
440 //get already connected client information, and store locally
441 SetDhcpConnectedPeerInfoList(GetDhcpClientInfoListN(false));
444 else if (__pNetConnection->GetNetAccountId() == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID)
446 //if already connected, Register callbacks
447 if (__isConnectedNetwork)
449 //register for events
451 WifiDirectDevice*(*pRegisterListenerFunction)(IWifiDirectDeviceListener* pDeviceListener,
452 IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener) = null;
454 __pDllHandle = dlopen(_WIFI_DIRECT_LIBRARY_NAME, RTLD_LAZY);
455 SysTryCatch(NID_NET, __pDllHandle != null, r = E_SYSTEM, E_SYSTEM,
456 "[%s] A system error has been occurred. Failed to open wifi library.", GetErrorMessage(E_SYSTEM));
458 pRegisterListenerFunction = reinterpret_cast<WifiDirectDevice*(*)(IWifiDirectDeviceListener* pDeviceListener,
459 IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener)>
460 (dlsym(__pDllHandle, "_WifiDirectDeviceManagerImpl_GetWifiDirectDeviceN"));
461 SysTryCatch(NID_NET, pRegisterListenerFunction != null, r = E_SYSTEM, E_SYSTEM,
462 "[%s] A system error has been occurred. Failed to get a function pointer.", GetErrorMessage(E_SYSTEM));
464 __pDevice = pRegisterListenerFunction(null, this, null);
465 SysTryCatch(NID_NET, __pDevice != null, r = E_SYSTEM, E_SYSTEM,
466 "[%s] A system error has been occurred. Failed to get a WifiDirectDevice instance.", GetErrorMessage(E_SYSTEM));
468 //get already connected client information, and store locally
469 SetDhcpConnectedPeerInfoList(GetDhcpClientInfoListN(false));
474 SysLogException(NID_NET, E_SYSTEM, "Bearer not supported for DHCP");
476 SysTryReturnResult(NID_NET, false, E_SYSTEM, "A system error has been occurred. Bearer not supported for DHCP");
479 __isConstructed = true;
481 __pNetConnectionListener = move(pNetConnectionListener);
482 __pLocalDhcpServerEvent = move(pLocalDhcpServerEvent);
483 __pNetConnectionClone = move(pNetConnectionClone);
488 if (__pDllHandle != null)
490 if (__pDevice != null)
492 void(*pUnRegisterListenerFunction)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener,
493 IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener) = null;
495 pUnRegisterListenerFunction = reinterpret_cast<void(*)(WifiDirectDevice* pWifiDirectDevice, IWifiDirectDeviceListener* pDeviceListener,
496 IWifiDirectGroupOwnerListener* pOwnerListener, IWifiDirectGroupClientListener* pClientListener)>
497 (dlsym(__pDllHandle, "_WifiDirectDeviceImpl_DeleteWifiDirectDevice"));
499 if (pUnRegisterListenerFunction != null)
501 pUnRegisterListenerFunction(__pDevice, null, this, null);
506 dlclose(__pDllHandle);
512 _LocalDhcpServerImpl::GetLocalDhcpServer(const IList* pLocalDhcpServerList, const NetConnection& netConnection)
514 LocalDhcpServer* pLocalDhcpServer = null;
518 int count = pLocalDhcpServerList->GetCount();
520 for (int index = 0; index < count; index++)
522 pLocalDhcpServer = (LocalDhcpServer*)(pLocalDhcpServerList->GetAt(index));
523 if (pLocalDhcpServer != null)
525 _LocalDhcpServerImpl* pLocalDhcpServerImpl = _LocalDhcpServerImpl::GetInstance(*pLocalDhcpServer);
527 //check if dhcp server is for same connection as the argument netConnection.
528 // and return the instance
529 if (pLocalDhcpServerImpl != null && pLocalDhcpServerImpl->__pNetConnection == &netConnection)
531 return pLocalDhcpServer;
540 _LocalDhcpServerImpl::SetLocalDhcpServerEventListener(ILocalDhcpServerEventListener* pListener)
542 result r = E_SUCCESS;
544 SysAssertf(__isConstructed == true, "Not yet constructed. Construct() should be called before use.");
546 r = __pLocalDhcpServerEvent->SetEventListener(pListener);
550 //register for event callbacks
551 __pNetConnectionListener->SetConstructParams(__pLocalDhcpServerEvent.get(), __pUsbTetheringHandle);
555 //unregister for event callback
556 __pNetConnectionListener->SetConstructParams(null, null);
563 _LocalDhcpServerImpl::GetDhcpClientInfoListN(bool all)
565 result r = E_SUCCESS;
569 ArrayList* pDhcpClientInfoList = null;
570 DhcpClientInfo* pDhcpClientInfo = null;
572 //check netconnection state before proceeding
573 SysTryReturn(NID_NET, __isConnectedNetwork == true, null, E_INVALID_STATE,
574 "[%s] NET Connection already stopped.", GetErrorMessage(E_INVALID_STATE));
577 pDhcpClientInfoList = new (std::nothrow) ArrayList();
578 SysTryReturn(NID_NET, pDhcpClientInfoList != null, null, E_OUT_OF_MEMORY,
579 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
581 r = pDhcpClientInfoList->Construct(_MAX_DHCP_CLIENT_COUNT);
582 SysTryCatch(NID_NET, r == E_SUCCESS, r = E_SYSTEM, r,
583 "[%s] A system error has been occurred. Failed to construct DhcpClientInfoList.", GetErrorMessage(E_SYSTEM));
585 //check for connection type - USB or Wifi-Direct
586 if (__pNetConnection->GetNetAccountId() == _DEFAULT_USB_ACCOUNT_ID)
588 SysLog(NID_NET, "Connection type is NET_BEARER_USB.");
590 int err = TETHERING_ERROR_NONE;
592 // get USB DHCP client info
593 err = tethering_foreach_connected_clients(__pUsbTetheringHandle, TETHERING_TYPE_USB, OnUsbPeerConnected, pDhcpClientInfoList);
594 SysLog(NID_NET, "The return value from tethering_for_each_connecetd_clients() is 0x%x", err);
596 SysTryCatch(NID_NET, err == TETHERING_ERROR_NONE, r = E_SYSTEM, r,
597 "[%s] A system error has been occurred getting DHCP USB Client info.", GetErrorMessage(E_SYSTEM));
600 // if all is true add local server details to list
603 pDhcpClientInfo = _DhcpClientInfoImpl::CreateDhcpClientInfoN();
605 SysTryCatch(NID_NET, pDhcpClientInfo != null, r = E_OUT_OF_MEMORY, r,
606 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
608 r = GetLocalDhcpServerInfo(*pDhcpClientInfo);
609 SysTryCatch(NID_NET, r == E_SUCCESS, , r, "[%s] GetDhcpUsbInterfaceInfo() failed.", GetErrorMessage(r));
611 r = pDhcpClientInfoList->Add(*pDhcpClientInfo);
612 SysTryCatch(NID_NET, r == E_SUCCESS, r = E_SYSTEM, r,
613 "[%s] A system error has been occurred. Failed to add DhcpClientInnfo to the list.", GetErrorMessage(E_SYSTEM));
615 // trim arrayList to current size
616 pDhcpClientInfoList->Trim();
619 else if (__pNetConnection->GetNetAccountId() == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID)
621 SysLog(NID_NET, "Connection type is NET_BEARER_WIFI_DIRECT.");
622 int err = WIFI_DIRECT_ERROR_NONE;
624 // get WIFI Direct DHCP client info
625 err = wifi_direct_foreach_connected_peers(OnWifiDirectPeerConnected, pDhcpClientInfoList);
626 SysLog(NID_NET, "The return value from wifi_direct_foreach_connected_peers() is 0x%x", err);
628 SysTryCatch(NID_NET, err == WIFI_DIRECT_ERROR_NONE, r = E_SYSTEM, r,
629 "[%s] A system error has been occurred getting DHCP WIfi-Direct Client info.", GetErrorMessage(E_SYSTEM));
631 //check if group owner, just for debugging
632 bool isOwner = false;
633 wifi_direct_is_group_owner(&isOwner);
636 SysLog(NID_NET, "The device is group owner of wifi-direct.");
640 SysLog(NID_NET, "The device is group memeber of wifi-direct");
643 // if all is true add local server details to list
646 // add local server details to the list
647 pDhcpClientInfo = _DhcpClientInfoImpl::CreateDhcpClientInfoN();
649 SysTryCatch(NID_NET, pDhcpClientInfo != null, r = E_OUT_OF_MEMORY, r,
650 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
652 r = GetLocalDhcpServerInfo(*pDhcpClientInfo);
653 SysTryCatch(NID_NET, r == E_SUCCESS, , r, "[%s] GetLocalDhcpServerInfo() failed.", GetErrorMessage(r));
655 r = pDhcpClientInfoList->Add(*pDhcpClientInfo);
656 SysTryCatch(NID_NET, r == E_SUCCESS, r = E_SYSTEM, r,
657 "[%s] A system error has been occurred. Failed to add DhcpClientInfo to the list.", GetErrorMessage(E_SYSTEM));
659 // trim arrayList to current size
660 pDhcpClientInfoList->Trim();
665 SysLogException(NID_NET, E_SYSTEM, "Bearer not supported for DHCP");
671 return pDhcpClientInfoList;
676 delete pDhcpClientInfo;
677 pDhcpClientInfo = null;
679 if (pDhcpClientInfoList)
681 pDhcpClientInfoList->RemoveAll(true);
682 delete pDhcpClientInfoList;
688 _LocalDhcpServerImpl::ConvertDhcpClientInfo(tethering_client_h clientHandle, DhcpClientInfo& dhcpClientInfo)
690 result r = E_SUCCESS;
692 _DhcpClientInfoImpl* pDhcpClientInfoImpl = null;
693 int err = TETHERING_ERROR_NONE;
694 char *pClientName = null;
695 char *pMacAddress = null;
696 char *pIpAddress = null;
698 pDhcpClientInfoImpl = _DhcpClientInfoImpl::GetInstance(dhcpClientInfo);
699 SysTryReturnResult(NID_NET, pDhcpClientInfoImpl != null, E_SYSTEM,
700 "A system error has been occurred. Failed to get DhcpClientInfo instance.");
703 err = tethering_client_get_name(clientHandle, &pClientName);
704 SysLog(NID_NET, "Error value from tethering_client_get_name() is 0x%x", err);
705 SysSecureLog(NID_NET, "Network interface name is %s", pClientName);
707 SysTryReturnResult(NID_NET, err == TETHERING_ERROR_NONE, E_SYSTEM,
708 "A system error has been occurred. Failed to get tethering client name.");
710 pDhcpClientInfoImpl->__dhcpClientName = String(pClientName);
713 err = tethering_client_get_mac_address(clientHandle, &pMacAddress);
714 SysLog(NID_NET, "Error value from tethering_client_get_mac_address() is 0x%x", err);
715 SysSecureLog(NID_NET, "Mac address is %s", pMacAddress);
717 SysTryCatch(NID_NET, err == TETHERING_ERROR_NONE, r = E_SYSTEM, r,
718 "[%s] A system error has been occurred. tethering_client_get_mac_address() returned error.", GetErrorMessage(E_SYSTEM));
720 pDhcpClientInfoImpl->__macAddress = ConvertMacAddress(pMacAddress);
723 err = tethering_client_get_ip_address(clientHandle, TETHERING_ADDRESS_FAMILY_IPV4, &pIpAddress);
724 SysLog(NID_NET, "Error value from tethering_client_get_ip_address() is 0x%x", err);
725 SysSecureLog(NID_NET, "Ip address is %s", pIpAddress);
727 SysTryCatch(NID_NET, err == TETHERING_ERROR_NONE, r = E_SYSTEM, r,
728 "[%s] A system error has been occurred. tethering_client_get_ip_address() returned error.", GetErrorMessage(E_SYSTEM));
730 pDhcpClientInfoImpl->__pLocalAddress.reset(new (std::nothrow) Ip4Address(pIpAddress));
732 SysTryCatch(NID_NET, pDhcpClientInfoImpl->__pLocalAddress != null, r = E_OUT_OF_MEMORY, r,
733 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
737 if (pClientName != null)
742 if (pIpAddress != null)
747 if (pMacAddress != null)
756 _LocalDhcpServerImpl::GetLocalDhcpServerInfo(DhcpClientInfo& dhcpClientInfo)
758 result r = E_SUCCESS;
760 _DhcpClientInfoImpl* pDhcpClientInfoImpl = null;
762 char *pInterfaceName = null;
763 char *pMacAddress = null;
764 char *pIpAddress = null;
766 pDhcpClientInfoImpl = _DhcpClientInfoImpl::GetInstance(dhcpClientInfo);
767 SysTryReturnResult(NID_NET, pDhcpClientInfoImpl != null, E_SYSTEM, "A system error has been occurred. Failed to get DhcpClientInfo instance.");
769 //check for connection type - USB or Wifi-Direct
770 if (__pNetConnection->GetNetAccountId() == _DEFAULT_USB_ACCOUNT_ID)
772 int err = TETHERING_ERROR_NONE;
774 err = tethering_get_network_interface_name(__pUsbTetheringHandle, TETHERING_TYPE_USB, &pInterfaceName);
775 SysLog(NID_NET, "Error value from tethering_get_name() is 0x%x", err);
776 SysSecureLog(NID_NET, "Network interface name is %s", pInterfaceName);
778 SysTryReturnResult(NID_NET, err == TETHERING_ERROR_NONE, E_SYSTEM,
779 "A system error has been occurred. tethering_get_name() returned error.");
782 err = tethering_get_mac_address(__pUsbTetheringHandle, TETHERING_TYPE_USB, &pMacAddress);
783 SysLog(NID_NET, "Error value from tethering_get_mac_address() is 0x%x", err);
784 SysSecureLog(NID_NET, "Mac address is %s", pMacAddress);
786 SysTryCatch(NID_NET, err == TETHERING_ERROR_NONE, r = E_SYSTEM, r,
787 "[%s] A system error has been occurred. tethering_get_mac_address returned error.", GetErrorMessage(E_SYSTEM));
790 err = tethering_get_ip_address(__pUsbTetheringHandle, TETHERING_TYPE_USB, TETHERING_ADDRESS_FAMILY_IPV4, &pIpAddress);
791 SysLog(NID_NET, "Error value from tethering_get_ip_address() is 0x%x", err);
792 SysSecureLog(NID_NET, "Ip address is %s", pIpAddress);
794 SysTryCatch(NID_NET, err == TETHERING_ERROR_NONE, r = E_SYSTEM, r,
795 "[%s] A system error has been occurred. tethering_get_ip_address() returned error.", GetErrorMessage(E_SYSTEM));
797 else if (__pNetConnection->GetNetAccountId() == _DEFAULT_WIFI_DIRECT_ACCOUNT_ID)
799 int err = WIFI_DIRECT_ERROR_NONE;
800 // set interface name
801 err = wifi_direct_get_network_interface_name(&pInterfaceName);
802 SysLog(NID_NET, "Error value from wifi_direct_get_network_interface_name() is 0x%x", err);
803 SysSecureLog(NID_NET, "Network interface name is %s", pInterfaceName);
805 SysTryReturnResult(NID_NET, err == WIFI_DIRECT_ERROR_NONE, E_SYSTEM,
806 "A system error has been occurred. wifi_direct_get_network_interface_name() returned error.");
809 err = wifi_direct_get_mac_address(&pMacAddress);
810 SysLog(NID_NET, "Error value from wifi_direct_get_mac_address() is 0x%x", err);
811 SysSecureLog(NID_NET, "Mac address is %s", pMacAddress);
813 SysTryCatch(NID_NET, err == WIFI_DIRECT_ERROR_NONE, r = E_SYSTEM, r,
814 "[%s] A system error has been occurred. wifi_direct_get_mac_address() returned error.", GetErrorMessage(E_SUCCESS));
817 err = wifi_direct_get_ip_address(&pIpAddress);
818 SysLog(NID_NET, "Error value from wifi_direct_get_ip_address() is 0x%x", err);
819 SysSecureLog(NID_NET, "Ip address is %s", pIpAddress);
821 SysTryCatch(NID_NET, err == WIFI_DIRECT_ERROR_NONE, r = E_SYSTEM, r,
822 "[%s] A system error has been occurred. wifi_direct_get_ip_address() returned error.", GetErrorMessage(E_SYSTEM));
826 SysLogException(NID_NET, r = E_SYSTEM, "Bearer not supported for DHCP");
830 pDhcpClientInfoImpl->__dhcpClientName = String(pInterfaceName);
832 pDhcpClientInfoImpl->__macAddress = ConvertMacAddress(pMacAddress);
834 pDhcpClientInfoImpl->__pLocalAddress.reset(new (std::nothrow) Ip4Address(pIpAddress));
836 SysTryCatch(NID_NET, pDhcpClientInfoImpl->__pLocalAddress != null, r = E_OUT_OF_MEMORY, r,
837 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
841 if (pInterfaceName != null)
843 free(pInterfaceName);
846 if (pIpAddress != null)
851 if (pMacAddress != null)
860 _LocalDhcpServerImpl::ConvertMacAddress(char* pMacAddress)
868 char* pTempMacAddress = pMacAddress;
870 while (*pTempMacAddress != '\0')
872 if (*pTempMacAddress == _PLATFORM_MAC_ADDRESS_DELIMITER)
874 *pTempMacAddress = _MAC_ADDRESS_DELIMITER;
879 return String(pMacAddress);
883 _LocalDhcpServerImpl::OnWifiDirectClientAssociated(WifiDirectDeviceId localDeviceId, const WifiDirectDeviceInfo& wifiDirectClientInfo)
885 SysLog(NID_NET, "LocalDhcpServer received an event, OnWifiDirectClientAssociated.");
887 result r = E_SUCCESS;
889 _LocalDhcpServerEventArg* pDhcpServerEventArg = null;
890 _NetDhcpServerEventType eventCode = NET_DHCP_SERVER_EVENT_CONNECT;
892 String(*pGetMacAddressFunction)(const WifiDirectDeviceInfo& deviceinfo) = null;
894 // Get MAC address of the client
895 pGetMacAddressFunction = reinterpret_cast<String(*)(const WifiDirectDeviceInfo& deviceinfo)>(dlsym(__pDllHandle, "_WifiDirectDeviceInfoImpl_GetMacAddress"));
896 SysTryReturnVoidResult(NID_NET, pGetMacAddressFunction != null, E_SYSTEM,
897 "[%s] A system error has been occurred. Failed to get a function pointer.", GetErrorMessage(E_SYSTEM));
899 String macAddress = pGetMacAddressFunction(wifiDirectClientInfo);
901 //check if we have already the info in our local list
903 if (GetDhcpConnectedPeerInfo(macAddress) == null)
905 // not found, create DhcpClientInfo
906 unique_ptr<DhcpClientInfo> pDhcpClientInfo(_DhcpClientInfoImpl::CreateDhcpClientInfoN());
907 SysTryReturnVoidResult(NID_NET, pDhcpClientInfo != null, E_OUT_OF_MEMORY,
908 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
910 r = ConvertDhcpClientInfo(wifiDirectClientInfo,*pDhcpClientInfo);
911 SysTryReturnVoidResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
912 "[%s] A system error has been occurred. Failed to retrieve DhcpClientInfo.", GetErrorMessage(E_SYSTEM));
914 //add the info into local list
915 r = AddDhcpConnectedPeerInfo(*pDhcpClientInfo);
916 SysTryReturnVoidResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
917 "[%s] A system error has been occurred. Failed to add DhcpConnectedPeerInfo.", GetErrorMessage(E_SYSTEM));
920 pDhcpServerEventArg = new (std::nothrow) _LocalDhcpServerEventArg(eventCode, *pDhcpClientInfo);
921 SysTryReturnVoidResult(NID_NET, pDhcpServerEventArg != null, E_OUT_OF_MEMORY,
922 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
924 pDhcpClientInfo.release();
926 __pLocalDhcpServerEvent->Fire(*pDhcpServerEventArg);
933 _LocalDhcpServerImpl::OnWifiDirectClientDisassociated(WifiDirectDeviceId localDeviceId, const WifiDirectDeviceInfo& wifiDirectClientInfo, WifiDirectAssociationTerminationReason reason)
935 SysLog(NID_NET, "LocalDhcpServer received an event, OnWifiDirectClientDisassociated.");
937 _LocalDhcpServerEventArg* pDhcpServerEventArg = null;
938 _NetDhcpServerEventType eventCode = NET_DHCP_SERVER_EVENT_DISCONNECT;
939 String(*pGetMacAddressFunction)(const WifiDirectDeviceInfo& deviceinfo) = null;
941 // Get MAC address of the client
942 pGetMacAddressFunction = reinterpret_cast<String(*)(const WifiDirectDeviceInfo& deviceinfo)>(dlsym(__pDllHandle, "_WifiDirectDeviceInfoImpl_GetMacAddress"));
943 SysTryReturnVoidResult(NID_NET, pGetMacAddressFunction != null, E_SYSTEM,
944 "[%s] A system error has been occurred. Failed to get a function pointer.", GetErrorMessage(E_SYSTEM));
946 String macAddress = pGetMacAddressFunction(wifiDirectClientInfo);
948 //check if we have already the info in our local list
949 unique_ptr<DhcpClientInfo> pDhcpClientInfo(GetDhcpConnectedPeerInfo(macAddress, true));
950 SysTryReturnVoidResult(NID_NET, pDhcpClientInfo != null, E_SYSTEM,
951 "[%s] A system error has been occurred. Failed to retrieve DhcpClientInfo.", GetErrorMessage(E_SYSTEM));
954 pDhcpServerEventArg = new (std::nothrow) _LocalDhcpServerEventArg(eventCode, *pDhcpClientInfo);
955 SysTryReturnVoidResult(NID_NET, pDhcpServerEventArg != null, E_OUT_OF_MEMORY,
956 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
958 __pLocalDhcpServerEvent->Fire(*pDhcpServerEventArg);
962 _LocalDhcpServerImpl::OnWifiDirectGroupDestroyed(WifiDirectDeviceId localDeviceId, result r)
964 SysLog(NID_NET, "LocalDhcpServer received an event, OnWifiDirectGroupDestroyed.");
968 _LocalDhcpServerImpl::OnWifiDirectGroupMemberInfoServiceStarted(WifiDirectDeviceId localDeviceId, const NetConnection* pNetConnection, result r)
970 SysLog(NID_NET, "LocalDhcpServer received an event, OnWifiDirectGroupMemberInfoServiceStarted.");
974 _LocalDhcpServerImpl::OnWifiDirectGroupMemberInfoServiceStopped(WifiDirectDeviceId localDeviceId, result r)
976 SysLog(NID_NET, "LocalDhcpServer received an event, OnWifiDirectGroupMemberInfoServiceStopped.");
980 _LocalDhcpServerImpl::ConvertDhcpClientInfo(const WifiDirectDeviceInfo& wifiDirectdeviceInfo, DhcpClientInfo& dhcpClientInfo)
982 result r = E_SUCCESS;
983 _DhcpClientInfoImpl* pDhcpClientInfoImpl = null;
985 String(*pGetDeviceNameFunction)(const WifiDirectDeviceInfo& deviceinfo) = null;
986 String(*pGetMacAddressFunction)(const WifiDirectDeviceInfo& deviceinfo) = null;
987 IpAddress*(*pGetIpAddressFunction)(const WifiDirectDeviceInfo& deviceinfo) = null;
989 pGetDeviceNameFunction = reinterpret_cast<String(*)(const WifiDirectDeviceInfo& deviceinfo)>
990 (dlsym(__pDllHandle, "_WifiDirectDeviceInfoImpl_GetDeviceName"));
991 SysTryReturnResult(NID_NET, pGetDeviceNameFunction != null, E_SYSTEM,
992 "A system error has been occurred. Failed to get a function pointer.");
994 pGetMacAddressFunction = reinterpret_cast<String(*)(const WifiDirectDeviceInfo& deviceinfo)>
995 (dlsym(__pDllHandle, "_WifiDirectDeviceInfoImpl_GetMacAddress"));
996 SysTryReturnResult(NID_NET, pGetMacAddressFunction != null, E_SYSTEM,
997 "A system error has been occurred. Failed to get a function pointer.");
999 pGetIpAddressFunction = reinterpret_cast<IpAddress*(*)(const WifiDirectDeviceInfo& deviceinfo)>
1000 (dlsym(__pDllHandle, "_WifiDirectDeviceInfoImpl_GetIpAddress"));
1001 SysTryReturnResult(NID_NET, pGetIpAddressFunction != null, E_SYSTEM,
1002 "A system error has been occurred. Failed to get a function pointer.");
1004 pDhcpClientInfoImpl = _DhcpClientInfoImpl::GetInstance(dhcpClientInfo);
1005 SysTryReturnResult(NID_NET, pDhcpClientInfoImpl != null, E_SYSTEM,
1006 "A system error has been occurred. Failed to get DhcpClientInfo instance.");
1008 pDhcpClientInfoImpl->__dhcpClientName = pGetDeviceNameFunction(wifiDirectdeviceInfo);
1009 pDhcpClientInfoImpl->__macAddress = pGetMacAddressFunction(wifiDirectdeviceInfo);
1011 const Ip4Address* ipAddress = dynamic_cast<const Ip4Address*>(pGetIpAddressFunction(wifiDirectdeviceInfo));
1012 SysTryReturnResult(NID_NET, ipAddress != null, E_SYSTEM, "A system error has been occurred. IP Address is null.");
1014 pDhcpClientInfoImpl->__pLocalAddress.reset(new (std::nothrow) Ip4Address(*ipAddress));
1016 SysTryReturnResult(NID_NET, pDhcpClientInfoImpl->__pLocalAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1022 _LocalDhcpServerImpl::OnWifiDirectPeerConnected(wifi_direct_connected_peer_info_s* pPeerInfo, void* pUserData)
1024 result r = E_SUCCESS;
1026 ArrayList* pDhcpclientInfoList = static_cast<ArrayList*>(pUserData);
1029 SysTryReturn(NID_NET, pDhcpclientInfoList != null, false, E_SYSTEM,
1030 "[%s] A system error has been occurred. pDhcpclientInfoList is null.", GetErrorMessage(E_SYSTEM));
1031 SysTryReturn(NID_NET, pPeerInfo != null, false, E_SYSTEM,
1032 "[%s] A system error has been occurred. pPeerInfo is null.", GetErrorMessage(E_SYSTEM));
1034 // create DhcpClientInfo and add to local list
1035 unique_ptr<DhcpClientInfo> pDhcpClientInfo(_DhcpClientInfoImpl::CreateDhcpClientInfoN());
1037 SysTryReturn(NID_NET, pDhcpClientInfo != null, false, E_OUT_OF_MEMORY,
1038 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1040 r = ConvertDhcpClientInfo(pPeerInfo, *pDhcpClientInfo);
1041 SysTryReturn(NID_NET, r == E_SUCCESS, false, r, "[%s] Failed to convert DhcpWifiDirectClientInfo.", GetErrorMessage(r));
1043 r = pDhcpclientInfoList->Add(*pDhcpClientInfo);
1044 SysTryReturn(NID_NET, r == E_SUCCESS, false, r, "[%s] Failed to add DhcpClientInfo to the list.", GetErrorMessage(r));
1046 pDhcpClientInfo.release();
1052 _LocalDhcpServerImpl::OnUsbPeerConnected(tethering_client_h clientHandle, void* pUserData)
1054 result r = E_SUCCESS;
1056 ArrayList* pDhcpclientInfoList = static_cast<ArrayList*>(pUserData);
1059 SysTryReturn(NID_NET, pDhcpclientInfoList != null, false, E_SYSTEM,
1060 "[%s] A system error has been occurred. pDhcpclientInfoList is null.", GetErrorMessage(E_SYSTEM));
1061 SysTryReturn(NID_NET, clientHandle != null, false, E_SYSTEM,
1062 "[%s] A system error has been occurred. clientHandle is null.", GetErrorMessage(E_SYSTEM));
1064 // create DhcpClientInfo and add to local list
1065 unique_ptr<DhcpClientInfo> pDhcpClientInfo(_DhcpClientInfoImpl::CreateDhcpClientInfoN());
1067 SysTryReturn(NID_NET, pDhcpClientInfo != null, false, E_OUT_OF_MEMORY,
1068 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1070 r = _LocalDhcpServerImpl::ConvertDhcpClientInfo(clientHandle, *pDhcpClientInfo);
1071 SysTryReturn(NID_NET, r == E_SUCCESS, false, r, "[%s] Failed to convert DhcpClientInfo.", GetErrorMessage(r));
1073 r = pDhcpclientInfoList->Add(*pDhcpClientInfo);
1074 SysTryReturn(NID_NET, r == E_SUCCESS, false, r, "[%s] Failed to add DhcpClientInfo to the list.", GetErrorMessage(r));
1076 pDhcpClientInfo.release();
1082 _LocalDhcpServerImpl::ConvertDhcpClientInfo(const wifi_direct_connected_peer_info_s* pPeerInfo, DhcpClientInfo& dhcpClientInfo)
1084 result r = E_SUCCESS;
1086 _DhcpClientInfoImpl* pDhcpClientInfoImpl = null;
1088 pDhcpClientInfoImpl = _DhcpClientInfoImpl::GetInstance(dhcpClientInfo);
1089 SysTryReturnResult(NID_NET, pDhcpClientInfoImpl != null, E_SYSTEM,
1090 "A system error has been occurred. Failed to get DhcpClientInfo instance.");
1092 pDhcpClientInfoImpl->__dhcpClientName = String(pPeerInfo->device_name);
1093 pDhcpClientInfoImpl->__macAddress = ConvertMacAddress((char*)pPeerInfo->mac_address);
1095 pDhcpClientInfoImpl->__pLocalAddress.reset(new (std::nothrow) Ip4Address(pPeerInfo->ip_address));
1096 SysTryReturnResult(NID_NET, pDhcpClientInfoImpl->__pLocalAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed");
1102 _LocalDhcpServerImpl::GetDhcpConnectedPeerInfo(const String& macAddress, bool remove)
1104 DhcpClientInfo* pDhcpClientInfo = null;
1108 SysTryReturn(NID_NET, __pDhcpConnectedPeerInfoList != null, null, E_SYSTEM,
1109 "[%s] A system error has been occurred. Client Info List is null.", GetErrorMessage(E_SYSTEM));
1111 //search for the given mac-address
1112 for (int count = 0; count < __pDhcpConnectedPeerInfoList->GetCount(); count++)
1114 pDhcpClientInfo = dynamic_cast<DhcpClientInfo*>(__pDhcpConnectedPeerInfoList->GetAt(count));
1115 if (pDhcpClientInfo != null && pDhcpClientInfo->GetMacAddress() == macAddress)
1119 __pDhcpConnectedPeerInfoList->Remove(*pDhcpClientInfo, false);
1121 return pDhcpClientInfo;
1128 _LocalDhcpServerImpl::AddDhcpConnectedPeerInfo(const DhcpClientInfo& dhcpClientInfo)
1130 result r = E_SUCCESS;
1132 // create list for dhcp client info, if not created
1133 if (__pDhcpConnectedPeerInfoList == null)
1135 unique_ptr<Tizen::Base::Collection::ArrayList, _CollectionDeleter> pDhcpConnectedPeerInfoList(new (std::nothrow) ArrayList());
1136 SysTryReturnResult(NID_NET, pDhcpConnectedPeerInfoList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1138 r = pDhcpConnectedPeerInfoList->Construct(_MAX_DHCP_CLIENT_COUNT);
1139 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
1140 "A system error has been occurred. Failed to construct DhcpConnectedPeerInfoList.");
1141 __pDhcpConnectedPeerInfoList = std::move(pDhcpConnectedPeerInfoList);
1144 r = __pDhcpConnectedPeerInfoList->Add(dhcpClientInfo);
1145 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
1146 "A system error has been occurred. Failed to add DhcpClientInfo to the list.");
1152 _LocalDhcpServerImpl::GetDhcpConnectedPeerInfoList()
1154 return __pDhcpConnectedPeerInfoList.get();
1158 _LocalDhcpServerImpl::SetDhcpConnectedPeerInfoList(IList* pList)
1160 __pDhcpConnectedPeerInfoList.reset(dynamic_cast<ArrayList*>(pList));
1163 _LocalDhcpServerImpl*
1164 _LocalDhcpServerImpl::GetInstance(LocalDhcpServer& localDhcpServer)
1166 return localDhcpServer.__pLocalDhcpServerImpl;
1169 const _LocalDhcpServerImpl*
1170 _LocalDhcpServerImpl::GetInstance(const LocalDhcpServer& localDhcpServer)
1172 return localDhcpServer.__pLocalDhcpServerImpl;