4c39c98d53d30ac93d121c000b6cc27add169d56
[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 using Tizen.Applications;
21
22 namespace Tizen.Network.WiFi
23 {
24     internal static class EventHandlerExtension
25     {
26         internal static void SafeInvoke(this EventHandler evt, object sender, EventArgs e)
27         {
28             var handler = evt;
29             if (handler != null)
30             {
31                 handler(sender, e);
32             }
33         }
34
35         internal static void SafeInvoke<T>(this EventHandler<T> evt, object sender, T e) where T : EventArgs
36         {
37             var handler = evt;
38             if (handler != null)
39             {
40                 handler(sender, e);
41             }
42         }
43     }
44
45     internal partial class WiFiManagerImpl
46     {
47         private event EventHandler<DeviceStateChangedEventArgs> _deviceStateChanged;
48         private event EventHandler<ConnectionStateChangedEventArgs> _connectionStateChanged;
49         private event EventHandler<RssiLevelChangedEventArgs> _rssiLevelChanged;
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.VoidCallback _backgroundScanFinishedCallback;
56
57         internal event EventHandler<DeviceStateChangedEventArgs> DeviceStateChanged
58         {
59             add
60             {
61                 context.Post((x) =>
62                 {
63                     if (_deviceStateChanged == null)
64                     {
65                         try
66                         {
67                             RegisterDeviceStateChangedEvent();
68                         } catch (Exception e)
69                         {
70                             Log.Error(Globals.LogTag, "Exception on adding DeviceStateChanged\n" + e.ToString());
71                             return;
72                         }
73                     }
74                     _deviceStateChanged += value;
75                 }, null);
76             }
77             remove
78             {
79                 context.Post((x) =>
80                 {
81                     _deviceStateChanged -= value;
82                     if (_deviceStateChanged == null)
83                     {
84                         try
85                         {
86                             UnregisterDeviceStateChangedEvent();
87                         }
88                         catch (Exception e)
89                         {
90                             Log.Error(Globals.LogTag, "Exception on removing DeviceStateChanged\n" + e.ToString());
91                         }
92                     }
93                 }, null);
94             }
95         }
96
97         internal event EventHandler<ConnectionStateChangedEventArgs> ConnectionStateChanged
98         {
99             add
100             {
101                 context.Post((x) =>
102                 {
103                     if (_connectionStateChanged == null)
104                     {
105                         try
106                         {
107                             RegisterConnectionStateChangedEvent();
108                         }
109                         catch (Exception e)
110                         {
111                             Log.Error(Globals.LogTag, "Exception on adding ConnectionStateChanged\n" + e.ToString());
112                             return;
113                         }
114                     }
115                     _connectionStateChanged += value;
116                 }, null);
117             }
118             remove
119             {
120                 context.Post((x) =>
121                 {
122                     _connectionStateChanged -= value;
123                     if (_connectionStateChanged == null)
124                     {
125                         try
126                         {
127                             UnregisterConnectionStateChangedEvent();
128                         }
129                         catch (Exception e)
130                         {
131                             Log.Error(Globals.LogTag, "Exception on removing ConnectionStateChanged\n" + e.ToString());
132                         }
133                     }
134                 }, null);
135             }
136         }
137
138         internal event EventHandler<RssiLevelChangedEventArgs> RssiLevelChanged
139         {
140             add
141             {
142                 context.Post((x) =>
143                 {
144                     if (_rssiLevelChanged == null)
145                     {
146                         try
147                         {
148                             RegisterRssiLevelChangedEvent();
149                         }
150                         catch (Exception e)
151                         {
152                             Log.Error(Globals.LogTag, "Exception on adding RssiLevelChanged\n" + e.ToString());
153                             return;
154                         }
155                     }
156                     _rssiLevelChanged += value;
157                 }, null);
158             }
159             remove
160             {
161                 context.Post((x) =>
162                 {
163                     _rssiLevelChanged -= value;
164                     if (_rssiLevelChanged == null)
165                     {
166                         try
167                         {
168                             UnregisterRssiLevelChangedEvent();
169                         }
170                         catch (Exception e)
171                         {
172                             Log.Error(Globals.LogTag, "Exception on removing RssiLevelChanged\n" + e.ToString());
173                         }
174                     }
175                 }, null);
176             }
177         }
178
179         internal event EventHandler BackgroundScanFinished
180         {
181             add
182             {
183                 context.Post((x) =>
184                 {
185                     if (_backgroundScanFinished == null)
186                     {
187                         try
188                         {
189                             RegisterBackgroundScanFinishedEvent();
190                         }
191                         catch (Exception e)
192                         {
193                             Log.Error(Globals.LogTag, "Exception on adding BackgroundScanFinished\n" + e.ToString());
194                             return;
195                         }
196                     }
197                     _backgroundScanFinished += value;
198                 }, null);
199             }
200             remove
201             {
202                 context.Post((x) =>
203                 {
204                     _backgroundScanFinished -= value;
205                     if (_backgroundScanFinished == null)
206                     {
207                         try
208                         {
209                             UnregisterBackgroundScanFinishedEvent();
210                         }
211                         catch (Exception e)
212                         {
213                             Log.Error(Globals.LogTag, "Exception on removing BackgroundScanFinished\n" + e.ToString());
214                         }
215                     }
216                 }, null);
217             }
218         }
219
220         private void RegisterDeviceStateChangedEvent()
221         {
222             Log.Info(Globals.LogTag, "RegisterDeviceStateChangedEvent in Thread " + Thread.CurrentThread.ManagedThreadId);
223             _deviceChangedCallback = (int deviceState, IntPtr userDate) =>
224             {
225                 WiFiDeviceState state = (WiFiDeviceState)deviceState;
226                 DeviceStateChangedEventArgs e = new DeviceStateChangedEventArgs(state);
227                 _deviceStateChanged.SafeInvoke(null, e);
228             };
229             int ret = Interop.WiFi.SetDeviceStateChangedCallback(GetSafeHandle(), _deviceChangedCallback, IntPtr.Zero);
230             if (ret != (int)WiFiError.None)
231             {
232                 Log.Error(Globals.LogTag, "Failed to set device state changed callback, Error - " + (WiFiError)ret);
233             }
234         }
235
236         private void UnregisterDeviceStateChangedEvent()
237         {
238             Log.Info(Globals.LogTag, "UnregisterDeviceStateChangedEvent in Thread " + Thread.CurrentThread.ManagedThreadId);
239             int ret = Interop.WiFi.UnsetDeviceStateChangedCallback(GetSafeHandle());
240             if (ret != (int)WiFiError.None)
241             {
242                 Log.Error(Globals.LogTag, "Failed to unset device state changed callback, Error - " + (WiFiError)ret);
243             }
244         }
245
246         private void RegisterConnectionStateChangedEvent()
247         {
248             _connectionChangedCallback = (int connectionState, IntPtr ap, IntPtr userData) =>
249             {
250                 if (ap != IntPtr.Zero)
251                 {
252                     WiFiConnectionState state = (WiFiConnectionState)connectionState;
253                     ConnectionStateChangedEventArgs e = new ConnectionStateChangedEventArgs(state, ap);
254                     _connectionStateChanged.SafeInvoke(null, e);
255                 }
256             };
257             int ret = Interop.WiFi.SetConnectionStateChangedCallback(GetSafeHandle(), _connectionChangedCallback, IntPtr.Zero);
258             if (ret != (int)WiFiError.None)
259             {
260                 Log.Error(Globals.LogTag, "Failed to set copnnection state changed callback, Error - " + (WiFiError)ret);
261             }
262         }
263
264         private void UnregisterConnectionStateChangedEvent()
265         {
266             int ret = Interop.WiFi.UnsetConnectionStateChangedCallback(GetSafeHandle());
267             if (ret != (int)WiFiError.None)
268             {
269                 Log.Error(Globals.LogTag, "Failed to unset device state changed callback, Error - " + (WiFiError)ret);
270             }
271         }
272
273         private void RegisterRssiLevelChangedEvent()
274         {
275
276             _rssiChangedCallback = (int rssiLevel, IntPtr userDate) =>
277             {
278                 WiFiRssiLevel level = (WiFiRssiLevel)rssiLevel;
279                 RssiLevelChangedEventArgs e = new RssiLevelChangedEventArgs(level);
280                 _rssiLevelChanged.SafeInvoke(null, e);
281             };
282             int ret = Interop.WiFi.SetRssiLevelchangedCallback(GetSafeHandle(), _rssiChangedCallback, IntPtr.Zero);
283             if (ret != (int)WiFiError.None)
284             {
285                 Log.Error(Globals.LogTag, "Failed to set rssi level changed callback, Error - " + (WiFiError)ret);
286             }
287         }
288
289         private void UnregisterRssiLevelChangedEvent()
290         {
291             int ret = Interop.WiFi.UnsetRssiLevelchangedCallback(GetSafeHandle());
292             if (ret != (int)WiFiError.None)
293             {
294                 Log.Error(Globals.LogTag, "Failed to unset rssi level changed callback, Error - " + (WiFiError)ret);
295             }
296         }
297
298         private void RegisterBackgroundScanFinishedEvent()
299         {
300             _backgroundScanFinishedCallback = (int result, IntPtr userDate) =>
301             {
302                 EventArgs e = new EventArgs();
303                 _backgroundScanFinished.SafeInvoke(null, e);
304             };
305             int ret = Interop.WiFi.SetBackgroundScanCallback(GetSafeHandle(), _backgroundScanFinishedCallback, IntPtr.Zero);
306             if (ret != (int)WiFiError.None)
307             {
308                 Log.Error(Globals.LogTag, "Failed to set background scan callback, Error - " + (WiFiError)ret);
309             }
310         }
311
312         private void UnregisterBackgroundScanFinishedEvent()
313         {
314             int ret = Interop.WiFi.UnsetBackgroundScanCallback(GetSafeHandle());
315             if (ret != (int)WiFiError.None)
316             {
317                 Log.Error(Globals.LogTag, "Failed to unset background scan callback, Error - " + (WiFiError)ret);
318             }
319         }
320     }
321 }