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.Collections.Generic;
19 using System.Threading;
20 using System.Threading.Tasks;
22 namespace Tizen.Network.WiFi
24 static internal class Globals
26 internal const string LogTag = "Tizen.Network.WiFi";
29 internal class HandleHolder : IDisposable
31 private IntPtr _handle = IntPtr.Zero;
32 private bool disposed = false;
34 internal HandleHolder()
36 Log.Debug(Globals.LogTag, "HandleHolder() Constructor");
37 _handle = WiFiManagerImpl.Instance.Initialize();
38 Log.Debug(Globals.LogTag, "Handle: " + _handle);
46 internal IntPtr GetHandle()
48 Log.Debug(Globals.LogTag, "Handleholder handle = " + _handle);
54 GC.SuppressFinalize(this);
57 private void Dispose(bool disposing)
59 Log.Debug(Globals.LogTag, ">>> HandleHolder Dispose with " + disposing);
60 Log.Debug(Globals.LogTag, ">>> Handle: " + _handle);
66 // Free managed objects.
67 WiFiManagerImpl.Instance.Deinitialize(_handle);
73 internal partial class WiFiManagerImpl : IDisposable
75 private static WiFiManagerImpl _instance = null;
76 private Dictionary<IntPtr, Interop.WiFi.VoidCallback> _callback_map = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
77 private int _requestId = 0;
78 private string _macAddress;
79 private bool disposed = false;
81 internal string MacAddress
85 if (String.IsNullOrEmpty(_macAddress))
88 int ret = Interop.WiFi.GetMacAddress(GetHandle(), out address);
89 if (ret != (int)WiFiError.None)
91 Log.Error(Globals.LogTag, "Failed to get mac address, Error - " + (WiFiError)ret);
96 _macAddress = address;
103 internal string InterfaceName
108 int ret = Interop.WiFi.GetNetworkInterfaceName(GetHandle(), out name);
109 if (ret != (int)WiFiError.None)
111 Log.Error(Globals.LogTag, "Failed to get interface name, Error - " + (WiFiError)ret);
118 internal WiFiConnectionState ConnectionState
123 int ret = Interop.WiFi.GetConnectionState(GetHandle(), out state);
124 if (ret != (int)WiFiError.None)
126 Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
127 return WiFiConnectionState.Failure;
129 return (WiFiConnectionState)state;
133 internal bool IsActive
138 int ret = Interop.WiFi.IsActive(GetHandle(), out active);
139 if (ret != (int)WiFiError.None)
141 Log.Error(Globals.LogTag, "Failed to get isActive, Error - " + (WiFiError)ret);
147 internal static WiFiManagerImpl Instance
151 Log.Debug(Globals.LogTag, "Instance getter");
152 if (_instance == null)
154 Log.Debug(Globals.LogTag, "Instance is null");
155 _instance = new WiFiManagerImpl();
162 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
164 Log.Info(Globals.LogTag, "In threadlocal delegate");
165 return new HandleHolder();
168 private WiFiManagerImpl()
177 public void Dispose()
180 GC.SuppressFinalize(this);
183 private void Dispose(bool disposing)
190 // Free managed objects.
192 //Free unmanaged objects
193 RemoveAllRegisteredEvent();
197 internal IntPtr GetHandle()
199 Log.Debug(Globals.LogTag, "GetHandle, Thread Id = " + Thread.CurrentThread.ManagedThreadId);
200 return s_threadName.Value.GetHandle();
203 internal IntPtr Initialize()
206 int ret = Interop.WiFi.Initialize(out handle);
207 if (ret != (int)WiFiError.None)
209 Log.Error(Globals.LogTag, "Failed to initialize wifi, Error - " + (WiFiError)ret);
210 WiFiErrorFactory.ThrowWiFiException(ret);
215 internal void Deinitialize(IntPtr handle)
217 int ret = Interop.WiFi.Deinitialize(handle);
218 if (ret != (int)WiFiError.None)
220 Log.Error(Globals.LogTag, "Failed to deinitialize wifi, Error - " + (WiFiError)ret);
221 WiFiErrorFactory.ThrowWiFiException(ret);
225 internal IEnumerable<WiFiAP> GetFoundAPs()
227 List<WiFiAP> apList = new List<WiFiAP>();
228 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
230 if (apHandle != IntPtr.Zero)
233 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
234 WiFiAP apItem = new WiFiAP(clonedHandle);
241 int ret = Interop.WiFi.GetForeachFoundAPs(GetHandle(), callback, IntPtr.Zero);
242 if (ret != (int)WiFiError.None)
244 Log.Error(Globals.LogTag, "Failed to get all APs, Error - " + (WiFiError)ret);
245 WiFiErrorFactory.ThrowWiFiException(ret);
251 internal IEnumerable<WiFiAP> GetFoundSpecificAPs()
253 List<WiFiAP> apList = new List<WiFiAP>();
254 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
256 if (apHandle != IntPtr.Zero)
259 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
260 WiFiAP apItem = new WiFiAP(clonedHandle);
268 int ret = Interop.WiFi.GetForeachFoundSpecificAPs(GetHandle(), callback, IntPtr.Zero);
269 if (ret != (int)WiFiError.None)
271 Log.Error(Globals.LogTag, "Failed to get specific APs, Error - " + (WiFiError)ret);
272 WiFiErrorFactory.ThrowWiFiException(ret);
278 internal IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
280 List<WiFiConfiguration> configList = new List<WiFiConfiguration>();
281 Interop.WiFi.HandleCallback callback = (IntPtr configHandle, IntPtr userData) =>
283 if (configHandle != IntPtr.Zero)
286 Interop.WiFi.Config.Clone(configHandle, out clonedConfig);
287 WiFiConfiguration configItem = new WiFiConfiguration(clonedConfig);
288 configList.Add(configItem);
294 int ret = Interop.WiFi.Config.GetForeachConfiguration(GetHandle(), callback, IntPtr.Zero);
295 if (ret != (int)WiFiError.None)
297 Log.Error(Globals.LogTag, "Failed to get configurations, Error - " + (WiFiError)ret);
298 WiFiErrorFactory.ThrowWiFiException(ret);
304 internal void SaveWiFiNetworkConfiguration(WiFiConfiguration config)
306 IntPtr configHandle = config.GetHandle();
307 int ret = Interop.WiFi.Config.SaveConfiguration(GetHandle(), configHandle);
308 if (ret != (int)WiFiError.None)
310 Log.Error(Globals.LogTag, "Failed to save configuration, Error - " + (WiFiError)ret);
311 WiFiErrorFactory.ThrowWiFiException(ret);
315 internal WiFiAP GetConnectedAP()
319 int ret = Interop.WiFi.GetConnectedAP(GetHandle(), out apHandle);
320 if (ret != (int)WiFiError.None)
322 Log.Error(Globals.LogTag, "Failed to connect with AP, Error - " + (WiFiError)ret);
323 WiFiErrorFactory.ThrowWiFiException(ret);
325 WiFiAP ap = new WiFiAP(apHandle);
329 internal Task ActivateAsync()
331 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
335 id = (IntPtr)_requestId++;
336 _callback_map[id] = (error, key) =>
338 Log.Debug(Globals.LogTag, "wifi activated");
339 if (error != (int)WiFiError.None)
341 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
342 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
344 task.SetResult(true);
347 _callback_map.Remove(key);
351 int ret = Interop.WiFi.Activate(GetHandle(), _callback_map[id], id);
352 if (ret != (int)WiFiError.None)
354 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
355 WiFiErrorFactory.ThrowWiFiException(ret);
360 internal Task ActivateWithWiFiPickerTestedAsync()
362 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
366 id = (IntPtr)_requestId++;
367 _callback_map[id] = (error, key) =>
369 Log.Debug(Globals.LogTag, "Activation finished");
370 if (error != (int)WiFiError.None)
372 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
373 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
375 task.SetResult(true);
378 _callback_map.Remove(key);
382 int ret = Interop.WiFi.ActivateWithWiFiPickerTested(GetHandle(), _callback_map[id], id);
383 if (ret != (int)WiFiError.None)
385 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
386 WiFiErrorFactory.ThrowWiFiException(ret);
391 internal Task DeactivateAsync()
393 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
397 id = (IntPtr)_requestId++;
398 _callback_map[id] = (error, key) =>
400 Log.Debug(Globals.LogTag, "Deactivation finished");
401 if (error != (int)WiFiError.None)
403 Log.Error(Globals.LogTag, "Error occurs during WiFi deactivating, " + (WiFiError)error);
404 task.SetException(new InvalidOperationException("Error occurs during WiFi deactivating, " + (WiFiError)error));
406 task.SetResult(true);
409 _callback_map.Remove(key);
413 int ret = Interop.WiFi.Deactivate(GetHandle(), _callback_map[id], id);
414 if (ret != (int)WiFiError.None)
416 Log.Error(Globals.LogTag, "Failed to deactivate wifi, Error - " + (WiFiError)ret);
417 WiFiErrorFactory.ThrowWiFiException(ret);
422 internal Task ScanAsync()
424 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
428 id = (IntPtr)_requestId++;
429 _callback_map[id] = (error, key) =>
431 Log.Debug(Globals.LogTag, "Scanning finished");
432 if (error != (int)WiFiError.None)
434 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
435 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
437 task.SetResult(true);
440 _callback_map.Remove(key);
444 int ret = Interop.WiFi.Scan(GetHandle(), _callback_map[id], id);
445 if (ret != (int)WiFiError.None)
447 Log.Error(Globals.LogTag, "Failed to scan all AP, Error - " + (WiFiError)ret);
448 WiFiErrorFactory.ThrowWiFiException(ret);
453 internal Task ScanSpecificAPAsync(string essid)
455 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
459 id = (IntPtr)_requestId++;
460 _callback_map[id] = (error, key) =>
462 Log.Debug(Globals.LogTag, "Scanning with specific AP finished");
463 if (error != (int)WiFiError.None)
465 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
466 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
468 task.SetResult(true);
471 _callback_map.Remove(key);
475 int ret = Interop.WiFi.ScanSpecificAP(GetHandle(), essid, _callback_map[id], id);
476 if (ret != (int)WiFiError.None)
478 Log.Error(Globals.LogTag, "Failed to scan with specific AP, Error - " + (WiFiError)ret);
479 WiFiErrorFactory.ThrowWiFiException(ret);