2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Threading.Tasks;
19 using System.Threading;
21 namespace Tizen.Network.WiFi
23 internal static class EventHandlerExtension
25 internal static void SafeInvoke(this EventHandler evt, object sender, EventArgs e)
34 internal static void SafeInvoke<T>(this EventHandler<T> evt, object sender, T e) where T : EventArgs
44 internal partial class WiFiManagerImpl
46 private event EventHandler<DeviceStateChangedEventArgs> _deviceStateChanged;
47 private event EventHandler<ConnectionStateChangedEventArgs> _connectionStateChanged;
48 private event EventHandler<RssiLevelChangedEventArgs> _rssiLevelChanged;
49 private event EventHandler _backgroundScanFinished;
51 private Interop.WiFi.DeviceStateChangedCallback _deviceChangedCallback;
52 private Interop.WiFi.ConnectionStateChangedCallback _connectionChangedCallback;
53 private Interop.WiFi.RssiLevelChangedCallback _rssiChangedCallback;
54 private Interop.WiFi.VoidCallback _backgroundScanFinishedCallback;
56 internal event EventHandler<DeviceStateChangedEventArgs> DeviceStateChanged
62 if (_deviceStateChanged == null)
66 RegisterDeviceStateChangedEvent();
69 Log.Error(Globals.LogTag, "Exception on adding DeviceStateChanged\n" + e.ToString());
73 _deviceStateChanged += value;
80 _deviceStateChanged -= value;
81 if (_deviceStateChanged == null)
85 UnregisterDeviceStateChangedEvent();
89 Log.Error(Globals.LogTag, "Exception on removing DeviceStateChanged\n" + e.ToString());
96 internal event EventHandler<ConnectionStateChangedEventArgs> ConnectionStateChanged
102 if (_connectionStateChanged == null)
106 RegisterConnectionStateChangedEvent();
110 Log.Error(Globals.LogTag, "Exception on adding ConnectionStateChanged\n" + e.ToString());
114 _connectionStateChanged += value;
121 _connectionStateChanged -= value;
122 if (_connectionStateChanged == null)
126 UnregisterConnectionStateChangedEvent();
130 Log.Error(Globals.LogTag, "Exception on removing ConnectionStateChanged\n" + e.ToString());
137 internal event EventHandler<RssiLevelChangedEventArgs> RssiLevelChanged
143 if (_rssiLevelChanged == null)
147 RegisterRssiLevelChangedEvent();
151 Log.Error(Globals.LogTag, "Exception on adding RssiLevelChanged\n" + e.ToString());
155 _rssiLevelChanged += value;
162 _rssiLevelChanged -= value;
163 if (_rssiLevelChanged == null)
167 UnregisterRssiLevelChangedEvent();
171 Log.Error(Globals.LogTag, "Exception on removing RssiLevelChanged\n" + e.ToString());
178 internal event EventHandler BackgroundScanFinished
184 if (_backgroundScanFinished == null)
188 RegisterBackgroundScanFinishedEvent();
192 Log.Error(Globals.LogTag, "Exception on adding BackgroundScanFinished\n" + e.ToString());
196 _backgroundScanFinished += value;
203 _backgroundScanFinished -= value;
204 if (_backgroundScanFinished == null)
208 UnregisterBackgroundScanFinishedEvent();
212 Log.Error(Globals.LogTag, "Exception on removing BackgroundScanFinished\n" + e.ToString());
219 private void RegisterDeviceStateChangedEvent()
221 Log.Info(Globals.LogTag, "RegisterDeviceStateChangedEvent in Thread " + Thread.CurrentThread.ManagedThreadId);
222 _deviceChangedCallback = (int deviceState, IntPtr userDate) =>
224 WiFiDeviceState state = (WiFiDeviceState)deviceState;
225 DeviceStateChangedEventArgs e = new DeviceStateChangedEventArgs(state);
226 _deviceStateChanged.SafeInvoke(null, e);
228 int ret = Interop.WiFi.SetDeviceStateChangedCallback(GetSafeHandle(), _deviceChangedCallback, IntPtr.Zero);
229 if (ret != (int)WiFiError.None)
231 Log.Error(Globals.LogTag, "Failed to set device state changed callback, Error - " + (WiFiError)ret);
235 private void UnregisterDeviceStateChangedEvent()
237 Log.Info(Globals.LogTag, "UnregisterDeviceStateChangedEvent in Thread " + Thread.CurrentThread.ManagedThreadId);
238 int ret = Interop.WiFi.UnsetDeviceStateChangedCallback(GetSafeHandle());
239 if (ret != (int)WiFiError.None)
241 Log.Error(Globals.LogTag, "Failed to unset device state changed callback, Error - " + (WiFiError)ret);
245 private void RegisterConnectionStateChangedEvent()
247 _connectionChangedCallback = (int connectionState, IntPtr ap, IntPtr userData) =>
249 if (ap != IntPtr.Zero)
251 WiFiConnectionState state = (WiFiConnectionState)connectionState;
252 ConnectionStateChangedEventArgs e = new ConnectionStateChangedEventArgs(state, ap);
253 _connectionStateChanged.SafeInvoke(null, e);
256 int ret = Interop.WiFi.SetConnectionStateChangedCallback(GetSafeHandle(), _connectionChangedCallback, IntPtr.Zero);
257 if (ret != (int)WiFiError.None)
259 Log.Error(Globals.LogTag, "Failed to set copnnection state changed callback, Error - " + (WiFiError)ret);
263 private void UnregisterConnectionStateChangedEvent()
265 int ret = Interop.WiFi.UnsetConnectionStateChangedCallback(GetSafeHandle());
266 if (ret != (int)WiFiError.None)
268 Log.Error(Globals.LogTag, "Failed to unset device state changed callback, Error - " + (WiFiError)ret);
272 private void RegisterRssiLevelChangedEvent()
275 _rssiChangedCallback = (int rssiLevel, IntPtr userDate) =>
277 WiFiRssiLevel level = (WiFiRssiLevel)rssiLevel;
278 RssiLevelChangedEventArgs e = new RssiLevelChangedEventArgs(level);
279 _rssiLevelChanged.SafeInvoke(null, e);
281 int ret = Interop.WiFi.SetRssiLevelchangedCallback(GetSafeHandle(), _rssiChangedCallback, IntPtr.Zero);
282 if (ret != (int)WiFiError.None)
284 Log.Error(Globals.LogTag, "Failed to set rssi level changed callback, Error - " + (WiFiError)ret);
288 private void UnregisterRssiLevelChangedEvent()
290 int ret = Interop.WiFi.UnsetRssiLevelchangedCallback(GetSafeHandle());
291 if (ret != (int)WiFiError.None)
293 Log.Error(Globals.LogTag, "Failed to unset rssi level changed callback, Error - " + (WiFiError)ret);
297 private void RegisterBackgroundScanFinishedEvent()
299 _backgroundScanFinishedCallback = (int result, IntPtr userDate) =>
301 EventArgs e = new EventArgs();
302 _backgroundScanFinished.SafeInvoke(null, e);
304 int ret = Interop.WiFi.SetBackgroundScanCallback(GetSafeHandle(), _backgroundScanFinishedCallback, IntPtr.Zero);
305 if (ret != (int)WiFiError.None)
307 Log.Error(Globals.LogTag, "Failed to set background scan callback, Error - " + (WiFiError)ret);
311 private void UnregisterBackgroundScanFinishedEvent()
313 int ret = Interop.WiFi.UnsetBackgroundScanCallback(GetSafeHandle());
314 if (ret != (int)WiFiError.None)
316 Log.Error(Globals.LogTag, "Failed to unset background scan callback, Error - " + (WiFiError)ret);