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