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