Merge remote-tracking branch 'origin/API8' into tizen_6.0
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiNetworkChange.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Threading.Tasks;
19 using System.Threading;
20
21 namespace Tizen.Network.WiFi
22 {
23     internal static class EventHandlerExtension
24     {
25         internal static void SafeInvoke(this EventHandler evt, object sender, EventArgs e)
26         {
27             var handler = evt;
28             if (handler != null)
29             {
30                 handler(sender, e);
31             }
32         }
33
34         internal static void SafeInvoke<T>(this EventHandler<T> evt, object sender, T e) where T : EventArgs
35         {
36             var handler = evt;
37             if (handler != null)
38             {
39                 handler(sender, e);
40             }
41         }
42     }
43
44     internal partial class WiFiManagerImpl
45     {
46         private event EventHandler<DeviceStateChangedEventArgs> _deviceStateChanged;
47         private event EventHandler<ConnectionStateChangedEventArgs> _connectionStateChanged;
48         private event EventHandler<RssiLevelChangedEventArgs> _rssiLevelChanged;
49         private event EventHandler<ScanStateChangedEventArgs> _scanStateChanged;
50         private event EventHandler _backgroundScanFinished;
51
52         private Interop.WiFi.DeviceStateChangedCallback _deviceChangedCallback;
53         private Interop.WiFi.ConnectionStateChangedCallback _connectionChangedCallback;
54         private Interop.WiFi.RssiLevelChangedCallback _rssiChangedCallback;
55         private Interop.WiFi.ScanStateChangedCallback _scanChangedCallback;
56         private Interop.WiFi.VoidCallback _backgroundScanFinishedCallback;
57
58         internal event EventHandler<DeviceStateChangedEventArgs> DeviceStateChanged
59         {
60             add
61             {
62                 context.Post((x) =>
63                 {
64                     if (_deviceStateChanged == null)
65                     {
66                         try
67                         {
68                             RegisterDeviceStateChangedEvent();
69                         } catch (Exception e)
70                         {
71                             Log.Error(Globals.LogTag, "Exception on adding DeviceStateChanged\n" + e);
72                             return;
73                         }
74                     }
75                     _deviceStateChanged += value;
76                 }, null);
77             }
78             remove
79             {
80                 context.Post((x) =>
81                 {
82                     _deviceStateChanged -= value;
83                     if (_deviceStateChanged == null)
84                     {
85                         try
86                         {
87                             UnregisterDeviceStateChangedEvent();
88                         }
89                         catch (Exception e)
90                         {
91                             Log.Error(Globals.LogTag, "Exception on removing DeviceStateChanged\n" + e);
92                         }
93                     }
94                 }, null);
95             }
96         }
97
98         internal event EventHandler<ConnectionStateChangedEventArgs> ConnectionStateChanged
99         {
100             add
101             {
102                 context.Post((x) =>
103                 {
104                     if (_connectionStateChanged == null)
105                     {
106                         try
107                         {
108                             RegisterConnectionStateChangedEvent();
109                         }
110                         catch (Exception e)
111                         {
112                             Log.Error(Globals.LogTag, "Exception on adding ConnectionStateChanged\n" + e);
113                             return;
114                         }
115                     }
116                     _connectionStateChanged += value;
117                 }, null);
118             }
119             remove
120             {
121                 context.Post((x) =>
122                 {
123                     _connectionStateChanged -= value;
124                     if (_connectionStateChanged == null)
125                     {
126                         try
127                         {
128                             UnregisterConnectionStateChangedEvent();
129                         }
130                         catch (Exception e)
131                         {
132                             Log.Error(Globals.LogTag, "Exception on removing ConnectionStateChanged\n" + e);
133                         }
134                     }
135                 }, null);
136             }
137         }
138
139         internal event EventHandler<RssiLevelChangedEventArgs> RssiLevelChanged
140         {
141             add
142             {
143                 context.Post((x) =>
144                 {
145                     if (_rssiLevelChanged == null)
146                     {
147                         try
148                         {
149                             RegisterRssiLevelChangedEvent();
150                         }
151                         catch (Exception e)
152                         {
153                             Log.Error(Globals.LogTag, "Exception on adding RssiLevelChanged\n" + e);
154                             return;
155                         }
156                     }
157                     _rssiLevelChanged += value;
158                 }, null);
159             }
160             remove
161             {
162                 context.Post((x) =>
163                 {
164                     _rssiLevelChanged -= value;
165                     if (_rssiLevelChanged == null)
166                     {
167                         try
168                         {
169                             UnregisterRssiLevelChangedEvent();
170                         }
171                         catch (Exception e)
172                         {
173                             Log.Error(Globals.LogTag, "Exception on removing RssiLevelChanged\n" + e);
174                         }
175                     }
176                 }, null);
177             }
178         }
179
180         internal event EventHandler<ScanStateChangedEventArgs> ScanStateChanged
181         {
182             add
183             {
184                 context.Post((x) =>
185                 {
186                     if (_scanStateChanged == null)
187                     {
188                         try
189                         {
190                             RegisterScanStateChangedEvent();
191                         }
192                         catch (Exception e)
193                         {
194                             Log.Error(Globals.LogTag, "Exception on adding ScanStateChanged\n" + e);
195                             return;
196                         }
197                     }
198                     _scanStateChanged += value;
199                 }, null);
200             }
201             remove
202             {
203                 context.Post((x) =>
204                 {
205                     _scanStateChanged -= value;
206                     if (_scanStateChanged == null)
207                     {
208                         try
209                         {
210                             UnregisterScanStateChangedEvent();
211                         }
212                         catch (Exception e)
213                         {
214                             Log.Error(Globals.LogTag, "Exception on removing ScanStateChanged\n" + e);
215                         }
216                     }
217                 }, null);
218             }
219         }
220
221         internal event EventHandler BackgroundScanFinished
222         {
223             add
224             {
225                 context.Post((x) =>
226                 {
227                     if (_backgroundScanFinished == null)
228                     {
229                         try
230                         {
231                             RegisterBackgroundScanFinishedEvent();
232                         }
233                         catch (Exception e)
234                         {
235                             Log.Error(Globals.LogTag, "Exception on adding BackgroundScanFinished\n" + e);
236                             return;
237                         }
238                     }
239                     _backgroundScanFinished += value;
240                 }, null);
241             }
242             remove
243             {
244                 context.Post((x) =>
245                 {
246                     _backgroundScanFinished -= value;
247                     if (_backgroundScanFinished == null)
248                     {
249                         try
250                         {
251                             UnregisterBackgroundScanFinishedEvent();
252                         }
253                         catch (Exception e)
254                         {
255                             Log.Error(Globals.LogTag, "Exception on removing BackgroundScanFinished\n" + e);
256                         }
257                     }
258                 }, null);
259             }
260         }
261
262         private void RegisterDeviceStateChangedEvent()
263         {
264             Log.Info(Globals.LogTag, "RegisterDeviceStateChangedEvent in Thread " + Thread.CurrentThread.ManagedThreadId);
265             _deviceChangedCallback = (int deviceState, IntPtr userDate) =>
266             {
267                 WiFiDeviceState state = (WiFiDeviceState)deviceState;
268                 DeviceStateChangedEventArgs e = new DeviceStateChangedEventArgs(state);
269                 _deviceStateChanged.SafeInvoke(null, e);
270             };
271             int ret = Interop.WiFi.SetDeviceStateChangedCallback(GetSafeHandle(), _deviceChangedCallback, IntPtr.Zero);
272             if (ret != (int)WiFiError.None)
273             {
274                 Log.Error(Globals.LogTag, "Failed to set device state changed callback, Error - " + (WiFiError)ret);
275             }
276         }
277
278         private void UnregisterDeviceStateChangedEvent()
279         {
280             Log.Info(Globals.LogTag, "UnregisterDeviceStateChangedEvent in Thread " + Thread.CurrentThread.ManagedThreadId);
281             int ret = Interop.WiFi.UnsetDeviceStateChangedCallback(GetSafeHandle());
282             if (ret != (int)WiFiError.None)
283             {
284                 Log.Error(Globals.LogTag, "Failed to unset device state changed callback, Error - " + (WiFiError)ret);
285             }
286         }
287
288         private void RegisterConnectionStateChangedEvent()
289         {
290             _connectionChangedCallback = (int connectionState, IntPtr ap, IntPtr userData) =>
291             {
292                 if (ap != IntPtr.Zero)
293                 {
294                     WiFiConnectionState state = (WiFiConnectionState)connectionState;
295                     ConnectionStateChangedEventArgs e = new ConnectionStateChangedEventArgs(state, ap);
296                     _connectionStateChanged.SafeInvoke(null, e);
297                 }
298             };
299             int ret = Interop.WiFi.SetConnectionStateChangedCallback(GetSafeHandle(), _connectionChangedCallback, IntPtr.Zero);
300             if (ret != (int)WiFiError.None)
301             {
302                 Log.Error(Globals.LogTag, "Failed to set copnnection state changed callback, Error - " + (WiFiError)ret);
303             }
304         }
305
306         private void UnregisterConnectionStateChangedEvent()
307         {
308             int ret = Interop.WiFi.UnsetConnectionStateChangedCallback(GetSafeHandle());
309             if (ret != (int)WiFiError.None)
310             {
311                 Log.Error(Globals.LogTag, "Failed to unset device state changed callback, Error - " + (WiFiError)ret);
312             }
313         }
314
315         private void RegisterRssiLevelChangedEvent()
316         {
317
318             _rssiChangedCallback = (int rssiLevel, IntPtr userDate) =>
319             {
320                 WiFiRssiLevel level = (WiFiRssiLevel)rssiLevel;
321                 RssiLevelChangedEventArgs e = new RssiLevelChangedEventArgs(level);
322                 _rssiLevelChanged.SafeInvoke(null, e);
323             };
324             int ret = Interop.WiFi.SetRssiLevelchangedCallback(GetSafeHandle(), _rssiChangedCallback, IntPtr.Zero);
325             if (ret != (int)WiFiError.None)
326             {
327                 Log.Error(Globals.LogTag, "Failed to set rssi level changed callback, Error - " + (WiFiError)ret);
328             }
329         }
330
331         private void UnregisterRssiLevelChangedEvent()
332         {
333             int ret = Interop.WiFi.UnsetRssiLevelchangedCallback(GetSafeHandle());
334             if (ret != (int)WiFiError.None)
335             {
336                 Log.Error(Globals.LogTag, "Failed to unset rssi level changed callback, Error - " + (WiFiError)ret);
337             }
338         }
339
340         private void RegisterScanStateChangedEvent()
341         {
342             _scanChangedCallback = (int scanState, IntPtr userData) =>
343             {
344                 _scanStateChanged?.Invoke(null, new ScanStateChangedEventArgs((WiFiScanState)scanState));
345             };
346             int ret = Interop.WiFi.SetScanStateChangedCallback(GetSafeHandle(), _scanChangedCallback, IntPtr.Zero);
347             if (ret != (int)WiFiError.None)
348             {
349                 Log.Error(Globals.LogTag, "Failed to set scan state changed callback, Error - " + (WiFiError)ret);
350             }
351         }
352
353         private void UnregisterScanStateChangedEvent()
354         {
355             int ret = Interop.WiFi.UnsetScanStateChangedCallback(GetSafeHandle());
356             if (ret != (int)WiFiError.None)
357             {
358                 Log.Error(Globals.LogTag, "Failed to unset scan state changed callback, Error - " + (WiFiError)ret);
359             }
360         }
361
362         private void RegisterBackgroundScanFinishedEvent()
363         {
364             _backgroundScanFinishedCallback = (int result, IntPtr userDate) =>
365             {
366                 EventArgs e = new EventArgs();
367                 _backgroundScanFinished.SafeInvoke(null, e);
368             };
369             int ret = Interop.WiFi.SetBackgroundScanCallback(GetSafeHandle(), _backgroundScanFinishedCallback, IntPtr.Zero);
370             if (ret != (int)WiFiError.None)
371             {
372                 Log.Error(Globals.LogTag, "Failed to set background scan callback, Error - " + (WiFiError)ret);
373             }
374         }
375
376         private void UnregisterBackgroundScanFinishedEvent()
377         {
378             int ret = Interop.WiFi.UnsetBackgroundScanCallback(GetSafeHandle());
379             if (ret != (int)WiFiError.None)
380             {
381                 Log.Error(Globals.LogTag, "Failed to unset background scan callback, Error - " + (WiFiError)ret);
382             }
383         }
384     }
385 }