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_DhcpServerEvent.cpp
19 * @brief This is the implementation file for the _LocalDhcpServerEvent Class.
21 * This file contains the implementation of the _LocalDhcpServerEvent Class.
24 #include <FNetNetTypes.h>
25 #include <FNetDhcpClientInfo.h>
26 #include <FNetILocalDhcpServerEventListener.h>
27 #include <FBaseSysLog.h>
28 #include "FNet_NetTypes.h"
29 #include "FNet_LocalDhcpServerImpl.h"
30 #include "FNet_LocalDhcpServerEvent.h"
31 #include "FNet_LocalDhcpServerEventArg.h"
32 #include "FNet_DhcpClientInfoImpl.h"
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Base::Collection;
38 namespace Tizen { namespace Net
41 _LocalDhcpServerEvent::_LocalDhcpServerEvent()
42 : __pLocalDhcpServer(null)
43 , __pLocalDhcpServerEventListener(null)
47 _LocalDhcpServerEvent::~_LocalDhcpServerEvent(void)
52 _LocalDhcpServerEvent::Construct(const LocalDhcpServer& localDhcpServer)
56 SysAssertf(__pLocalDhcpServer == null,
57 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
59 r = _Event::Initialize();
60 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
61 "A system error has been occurred. Failed to initialize the event.");
63 __pLocalDhcpServer = const_cast <LocalDhcpServer*>(&localDhcpServer);
69 _LocalDhcpServerEvent::SetEventListener(ILocalDhcpServerEventListener* pListener)
73 SysAssertf(__pLocalDhcpServer != null, "Not yet constructed. Construct() should be called before use.");
75 if (__pLocalDhcpServerEventListener == null)
77 if (pListener != null)
79 __pLocalDhcpServerEventListener = pListener;
80 r = this->AddListener(*pListener, true);
84 __pLocalDhcpServerEventListener = null;
89 if (pListener != null)
91 this->RemoveListener(*__pLocalDhcpServerEventListener);
92 __pLocalDhcpServerEventListener = pListener;
93 r = this->AddListener(*pListener, true);
97 this->RemoveListener(*__pLocalDhcpServerEventListener);
98 __pLocalDhcpServerEventListener = null;
106 _LocalDhcpServerEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
108 result r = E_SUCCESS;
110 ILocalDhcpServerEventListener* pLocalDhcpServerEventListener = null;
111 pLocalDhcpServerEventListener = dynamic_cast < ILocalDhcpServerEventListener* >(&listener);
112 SysTryReturnVoidResult(NID_NET, pLocalDhcpServerEventListener != null, E_SYSTEM,
113 "A system error has been occurred. Listener argument is invalid.");
115 IEventArg* pEventArg = const_cast < IEventArg* >(&arg);
116 _LocalDhcpServerEventArg* pLocalDhcpServerEventArg = dynamic_cast < _LocalDhcpServerEventArg* >(pEventArg);
117 SysTryReturnVoidResult(NID_NET, pLocalDhcpServerEventArg != null, E_SYSTEM,
118 "A system error has been occurred. Event argument is invalid type.");
121 DhcpClientInfo* pDhcpClientInfo = pLocalDhcpServerEventArg->GetDhcpClientInfo();
122 SysTryReturnVoidResult(NID_NET, pDhcpClientInfo != null, E_SYSTEM,
123 "A system error has been occurred. Failed to get dhcp client information.");
125 // copy info and send to app.
126 DhcpClientInfo* pCallbackDhcpClientInfo = null;
127 pCallbackDhcpClientInfo = _DhcpClientInfoImpl::CloneDhcpClientInfoN(*pDhcpClientInfo);
128 SysTryReturnVoidResult(NID_NET, pCallbackDhcpClientInfo != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
130 switch (pLocalDhcpServerEventArg->GetEventType())
132 case NET_DHCP_SERVER_EVENT_CONNECT:
133 SysSecureLog(NID_NET, "Calling OnDhcpClientConnectedN() with Client name:%ls, Mac address:%ls",
134 (pCallbackDhcpClientInfo->GetName()).GetPointer(),
135 (pCallbackDhcpClientInfo->GetMacAddress()).GetPointer());
137 pLocalDhcpServerEventListener->OnDhcpClientConnectedN(*GetLocalDhcpServer(),
138 *pCallbackDhcpClientInfo);
142 case NET_DHCP_SERVER_EVENT_DISCONNECT:
143 SysSecureLog(NID_NET, "Calling OnDhcpClientDisconnectedN() with Client name:%ls, Mac address:%ls",
144 (pCallbackDhcpClientInfo->GetName()).GetPointer(),
145 (pCallbackDhcpClientInfo->GetMacAddress()).GetPointer());
147 pLocalDhcpServerEventListener->OnDhcpClientDisconnectedN(*GetLocalDhcpServer(),
148 *pCallbackDhcpClientInfo);
153 delete pCallbackDhcpClientInfo;
156 SysLogException(NID_NET, r, "Event[%d] is invalid.", pLocalDhcpServerEventArg->GetEventType());
165 _LocalDhcpServerEvent::GetLocalDhcpServer(void) const
169 SysTryReturn(NID_NET, __pLocalDhcpServer != null, null, E_INVALID_STATE, "__pLocalDhcpServer is null.");
171 return __pLocalDhcpServer;