merge with master
[framework/osp/net.git] / src / wifi / FNetWifi_WifiDirectDeviceEvent.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 /**
19  * @file    FNetWifi_WifiDirectDeviceEvent.cpp
20  * @brief   This is the implementation file for the _WifiDirectDeviceEvent Class.
21  *
22  * This header file contains implementation of the _WifiDirectDeviceEvent Class.
23  */
24
25 #include <FBaseRtIEventListener.h>
26 #include <FNetWifiIWifiDirectDeviceListener.h>
27 #include <FBaseColIList.h>
28 #include <FBaseString.h>
29 #include <FNetWifiWifiDirectGroupMember.h>
30 #include <FNetWifiWifiDirectDeviceInfo.h>
31 #include <FNetWifiWifiDirectGroupInfo.h>
32 #include <FBaseSysLog.h>
33 #include "FNetWifi_WifiDirectDeviceImpl.h"
34 #include "FNetWifi_WifiDirectDeviceEvent.h"
35 #include "FNetWifi_WifiDirectEventArg.h"
36
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base;
39
40 namespace Tizen { namespace Net { namespace Wifi
41 {
42
43 ////////////////////////////////////////////////////////////////////////////
44 _WifiDirectDeviceEvent::_WifiDirectDeviceEvent(_WifiDirectDeviceImpl* pWifiDirectDeviceImpl)
45 {
46     __pWifiDirectDeviceImpl = pWifiDirectDeviceImpl;
47 }
48
49 _WifiDirectDeviceEvent::~_WifiDirectDeviceEvent(void)
50 {
51
52 }
53
54 _WifiDirectDeviceEvent::_WifiDirectDeviceEvent(void)
55     : __pWifiDirectDeviceImpl(null)
56 {
57 }
58
59
60 result
61 _WifiDirectDeviceEvent::Construct(void)
62 {
63     return _Event::Initialize();
64 }
65
66 ////////////////////////////////////////////////////////////////////////////////
67 /// Operation : Protected
68
69 void
70 _WifiDirectDeviceEvent::FireImpl(Tizen::Base::Runtime::IEventListener& listener, const Tizen::Base::Runtime::IEventArg& arg)
71 {
72         const _WifiDirectEventArg* pArg = dynamic_cast< const _WifiDirectEventArg* >(&arg);
73         SysTryReturnVoidResult(NID_NET_WIFI, pArg != null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is a null pointer.");
74
75         IWifiDirectDeviceListener* pDeviceListener = dynamic_cast< IWifiDirectDeviceListener* >(&listener);
76         SysTryReturnVoidResult(NID_NET_WIFI, pDeviceListener != null, E_INVALID_ARG,
77                                                    "[E_INVALID_ARG] The result of a dynamic_cast operation is null for external Device listener.");
78
79         _WifiDirectEventType eventType = pArg->GetEventType();
80         WifiDirectDeviceId localDeviceId = pArg->GetDeviceId();
81         result r = pArg->GetError();
82
83         switch (eventType)
84         {
85         case WIFI_DIRECT_DEVICE_EVENT_ACTIVATED:
86                 SysLog(NID_NET_WIFI, "Firing External Event (Type: WIFI_DIRECT_DEVICE_EVENT_ACTIVATED) : %s", GetErrorMessage(r));
87                 pDeviceListener->OnWifiDirectDeviceActivated(localDeviceId, r);
88                 break;
89
90         case WIFI_DIRECT_DEVICE_EVENT_DEACTIVATED:
91                 SysLog(NID_NET_WIFI, "Firing External Event (Type: WIFI_DIRECT_DEVICE_EVENT_DEACTIVATED) : %s", GetErrorMessage(r));
92                 pDeviceListener->OnWifiDirectDeviceDeactivated(localDeviceId, r);
93                 break;
94
95         case WIFI_DIRECT_DEVICE_EVENT_GROUP_CREATED:
96                 SysLog(NID_NET_WIFI, "Firing External Event (Type: WIFI_DIRECT_DEVICE_EVENT_GROUP_CREATED) : %s", GetErrorMessage(r));
97                 {
98                         WifiDirectGroupInfo* pGroupInfo = pArg->GetGroupInfo();
99                         WifiDirectDeviceInfo* pGroupOwnerInfo = pArg->GetDeviceInfo();
100                         WifiDirectGroupMember* pGroupMember = pArg->GetGroupMember();
101                         pDeviceListener->OnWifiDirectGroupCreatedN(localDeviceId, *pGroupInfo, *pGroupOwnerInfo, pGroupMember, r);
102                 }
103                 break;
104         case WIFI_DIRECT_DEVICE_EVENT_SCAN_FOUND:
105             SysLog(NID_NET_WIFI, "Firing External Event (Type: WIFI_DIRECT_DEVICE_EVENT_SCAN_FOUND)");
106             {
107                 WifiDirectDeviceInfo* pDeviceInfo = pArg->GetDeviceInfo();
108                 pDeviceListener->OnWifiDirectRemoteDeviceFound(localDeviceId, *pDeviceInfo);
109             }
110             break;
111         case WIFI_DIRECT_DEVICE_EVENT_SCAN_COMPLETED:
112                 SysLog(NID_NET_WIFI, "Firing External Event (Type: WIFI_DIRECT_DEVICE_EVENT_SCAN_COMPLETED) : %s", GetErrorMessage(r));
113                 {
114                         if (!IsFailed(r))
115                         {
116                                 ArrayList* pSrcDeviceInfoList = dynamic_cast< ArrayList* >(pArg->GetDeviceInfoList());
117                                 if (pSrcDeviceInfoList == null)
118                                 {
119                                         SysLogException(NID_NET_WIFI, E_SYSTEM, "[E_SYSTEM] Failed to dynamic casting DeviceInfo list.");
120                                         pDeviceListener->OnWifiDirectScanCompletedN(localDeviceId, null, E_SYSTEM);
121                                         return;
122                                 }
123
124                                 ArrayList* pDescDeviceInfoList = new (std::nothrow) ArrayList();
125                                 if (pSrcDeviceInfoList == null)
126                                 {
127                                         SysLogException(NID_NET_WIFI, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
128                                         pDeviceListener->OnWifiDirectScanCompletedN(localDeviceId, null, E_SYSTEM);
129                                         return;
130                                 }
131
132                                 result res = pDescDeviceInfoList->Construct(*pSrcDeviceInfoList);
133
134                                 if (IsFailed(res))
135                                 {
136                                         delete pDescDeviceInfoList;
137                                         pDescDeviceInfoList = null;
138
139                                         r = E_SYSTEM;
140                                 }
141
142                                 pDeviceListener->OnWifiDirectScanCompletedN(localDeviceId, pDescDeviceInfoList, r);
143                         }
144                         else
145                         {
146                                 pDeviceListener->OnWifiDirectScanCompletedN(localDeviceId, null, r);
147                         }
148                 }
149                 break;
150
151         case WIFI_DIRECT_DEVICE_EVENT_ASSOCIATED:
152                 SysLog(NID_NET_WIFI, "Firing External Event (Type: WIFI_DIRECT_DEVICE_EVENT_ASSOCIATED) : %s", GetErrorMessage(r));
153                 {
154                         WifiDirectDeviceInfo* pGroupOwnerInfo = pArg->GetDeviceInfo();
155                         if (pGroupOwnerInfo == null)
156                         {
157                                 pDeviceListener->OnWifiDirectAssociationCompleted(localDeviceId, WifiDirectDeviceInfo(), r);
158                         }
159                         else
160                         {
161                                 pDeviceListener->OnWifiDirectAssociationCompleted(localDeviceId, *pGroupOwnerInfo, r);
162                         }
163                 }
164                 break;
165
166         case WIFI_DIRECT_DEVICE_EVENT_CONNECTED:
167                 SysLog(NID_NET_WIFI, "Firing External Event (Type: WIFI_DIRECT_DEVICE_EVENT_CONNECTED) : %s", GetErrorMessage(r));
168                 {
169                         WifiDirectDeviceInfo* pDeviceInfo = pArg->GetDeviceInfo();
170                         if (pDeviceInfo == null)
171                         {
172                                 pDeviceListener->OnWifiDirectConnected(localDeviceId, WifiDirectDeviceInfo(), r);
173                         }
174                         else
175                         {
176                                 pDeviceListener->OnWifiDirectConnected(localDeviceId, *pDeviceInfo, r);
177                         }
178                 }
179                 break;
180
181         case WIFI_DIRECT_DEVICE_EVENT_DISCONNECTED:
182                 {
183                         SysLog(NID_NET_WIFI, "Firing External Event (Type: WIFI_DIRECT_DEVICE_EVENT_DISCONNECTED) : %s", GetErrorMessage(r));
184                         String macAddress = pArg->GetMacAddress();
185                         pDeviceListener->OnWifiDirectDisconnected(localDeviceId, macAddress, r);
186                 }
187                 break;
188
189         case WIFI_DIRECT_DEVICE_EVENT_AUTONOMOS_GROUP_CREATED:
190                 SysLog(NID_NET_WIFI, "Firing External Event (Type: WIFI_DIRECT_DEVICE_EVENT_AUTONOMOS_GROUP_CREATED) : %s", GetErrorMessage(r));
191                 pDeviceListener->OnWifiDirectAutonomousGroupCreated(localDeviceId, r);
192                 break;
193
194         case WIFI_DIRECT_DEVICE_EVENT_GROUP_LEFT:
195                 SysLog(NID_NET_WIFI, "Firing External Event (Type: WIFI_DIRECT_DEVICE_EVENT_GROUP_LEFT) : %s", GetErrorMessage(r));
196                 pDeviceListener->OnWifiDirectGroupLeft(localDeviceId, r);
197                 break;
198
199         default:
200                 SysLog(NID_NET_WIFI, "An undefined external WifiDirectDevice event occurs. (Type: %d)", eventType);
201                 SysAssert(false);
202                 break;
203         }
204 }
205
206 } } } // Tizen::Net::Wifi