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;
102 internal string InterfaceName
107 int ret = Interop.WiFi.GetNetworkInterfaceName(GetHandle(), out name);
108 if (ret != (int)WiFiError.None)
110 Log.Error(Globals.LogTag, "Failed to get interface name, Error - " + (WiFiError)ret);
116 internal WiFiConnectionState ConnectionState
121 int ret = Interop.WiFi.GetConnectionState(GetHandle(), out state);
122 if (ret != (int)WiFiError.None)
124 Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
125 return WiFiConnectionState.Failure;
127 return (WiFiConnectionState)state;
130 internal bool IsActivated
135 int ret = Interop.WiFi.IsActivated(GetHandle(), out active);
136 if (ret != (int)WiFiError.None)
138 Log.Error(Globals.LogTag, "Failed to get isActive, Error - " + (WiFiError)ret);
144 internal static WiFiManagerImpl Instance
148 Log.Debug(Globals.LogTag, "Instance getter");
149 if (_instance == null)
151 Log.Debug(Globals.LogTag, "Instance is null");
152 _instance = new WiFiManagerImpl();
159 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
161 Log.Info(Globals.LogTag, "In threadlocal delegate");
162 return new HandleHolder();
165 private WiFiManagerImpl()
174 public void Dispose()
177 GC.SuppressFinalize(this);
180 private void Dispose(bool disposing)
187 // Free managed objects.
189 //Free unmanaged objects
190 RemoveAllRegisteredEvent();
194 internal IntPtr GetHandle()
196 Log.Debug(Globals.LogTag, "GetHandle, Thread Id = " + Thread.CurrentThread.ManagedThreadId);
197 return s_threadName.Value.GetHandle();
200 internal IntPtr Initialize()
203 int ret = Interop.WiFi.Initialize(out handle);
204 if (ret != (int)WiFiError.None)
206 Log.Error(Globals.LogTag, "Failed to initialize wifi, Error - " + (WiFiError)ret);
207 WiFiErrorFactory.ThrowWiFiException(ret);
212 internal void Deinitialize(IntPtr handle)
214 int ret = Interop.WiFi.Deinitialize(handle);
215 if (ret != (int)WiFiError.None)
217 Log.Error(Globals.LogTag, "Failed to deinitialize wifi, Error - " + (WiFiError)ret);
218 WiFiErrorFactory.ThrowWiFiException(ret);
222 internal IEnumerable<WiFiAp> GetFoundAps()
224 List<WiFiAp> apList = new List<WiFiAp>();
225 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
227 if (apHandle != IntPtr.Zero)
230 Interop.WiFi.Ap.Clone(out clonedHandle, apHandle);
231 WiFiAp apItem = new WiFiAp(clonedHandle);
238 int ret = Interop.WiFi.GetForeachFoundAps(GetHandle(), callback, IntPtr.Zero);
239 if (ret != (int)WiFiError.None)
241 Log.Error(Globals.LogTag, "Failed to get all APs, Error - " + (WiFiError)ret);
242 WiFiErrorFactory.ThrowWiFiException(ret);
248 internal IEnumerable<WiFiAp> GetFoundSpecificAps()
250 List<WiFiAp> apList = new List<WiFiAp>();
251 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
253 if (apHandle != IntPtr.Zero)
256 Interop.WiFi.Ap.Clone(out clonedHandle, apHandle);
257 WiFiAp apItem = new WiFiAp(clonedHandle);
265 int ret = Interop.WiFi.GetForeachFoundSpecificAps(GetHandle(), callback, IntPtr.Zero);
266 if (ret != (int)WiFiError.None)
268 Log.Error(Globals.LogTag, "Failed to get specific APs, Error - " + (WiFiError)ret);
269 WiFiErrorFactory.ThrowWiFiException(ret);
275 internal IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
277 List<WiFiConfiguration> configList = new List<WiFiConfiguration>();
278 Interop.WiFi.HandleCallback callback = (IntPtr configHandle, IntPtr userData) =>
280 if (configHandle != IntPtr.Zero)
283 Interop.WiFi.Config.Clone(configHandle, out clonedConfig);
284 WiFiConfiguration configItem = new WiFiConfiguration(clonedConfig);
285 configList.Add(configItem);
291 int ret = Interop.WiFi.Config.GetForeachConfiguration(GetHandle(), callback, IntPtr.Zero);
292 if (ret != (int)WiFiError.None)
294 Log.Error(Globals.LogTag, "Failed to get configurations, Error - " + (WiFiError)ret);
295 WiFiErrorFactory.ThrowWiFiException(ret);
301 internal void SaveWiFiNetworkConfiguration(WiFiConfiguration config)
303 IntPtr configHandle = config.GetHandle();
304 int ret = Interop.WiFi.Config.SaveConfiguration(GetHandle(), configHandle);
305 if (ret != (int)WiFiError.None)
307 Log.Error(Globals.LogTag, "Failed to save configuration, Error - " + (WiFiError)ret);
308 WiFiErrorFactory.ThrowWiFiException(ret);
312 internal WiFiAp GetConnectedAp()
316 int ret = Interop.WiFi.GetConnectedAp(GetHandle(), out apHandle);
317 if (ret != (int)WiFiError.None)
319 Log.Error(Globals.LogTag, "Failed to connect with AP, Error - " + (WiFiError)ret);
320 WiFiErrorFactory.ThrowWiFiException(ret);
322 WiFiAp ap = new WiFiAp(apHandle);
326 internal void RemoveAp(WiFiAp ap)
328 IntPtr apHandle = ap.GetHandle();
329 int ret = Interop.WiFi.RemoveAp(GetHandle(), apHandle);
330 if (ret != (int)WiFiError.None)
332 Log.Error(Globals.LogTag, "Failed to remove with AP, Error - " + (WiFiError)ret);
333 WiFiErrorFactory.ThrowWiFiException(ret);
337 internal Task ActivateAsync()
339 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
343 id = (IntPtr)_requestId++;
344 _callback_map[id] = (error, key) =>
346 Log.Debug(Globals.LogTag, "wifi activated");
347 if (error != (int)WiFiError.None)
349 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
350 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
352 task.SetResult(true);
355 _callback_map.Remove(key);
359 int ret = Interop.WiFi.Activate(GetHandle(), _callback_map[id], id);
360 if (ret != (int)WiFiError.None)
362 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
363 WiFiErrorFactory.ThrowWiFiException(ret);
368 internal Task ActivateWithWiFiPickerTestedAsync()
370 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
374 id = (IntPtr)_requestId++;
375 _callback_map[id] = (error, key) =>
377 Log.Debug(Globals.LogTag, "Activation finished");
378 if (error != (int)WiFiError.None)
380 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
381 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
383 task.SetResult(true);
386 _callback_map.Remove(key);
390 int ret = Interop.WiFi.ActivateWithWiFiPickerTested(GetHandle(), _callback_map[id], id);
391 if (ret != (int)WiFiError.None)
393 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
394 WiFiErrorFactory.ThrowWiFiException(ret);
399 internal Task DeactivateAsync()
401 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
405 id = (IntPtr)_requestId++;
406 _callback_map[id] = (error, key) =>
408 Log.Debug(Globals.LogTag, "Deactivation finished");
409 if (error != (int)WiFiError.None)
411 Log.Error(Globals.LogTag, "Error occurs during WiFi deactivating, " + (WiFiError)error);
412 task.SetException(new InvalidOperationException("Error occurs during WiFi deactivating, " + (WiFiError)error));
414 task.SetResult(true);
417 _callback_map.Remove(key);
421 int ret = Interop.WiFi.Deactivate(GetHandle(), _callback_map[id], id);
422 if (ret != (int)WiFiError.None)
424 Log.Error(Globals.LogTag, "Failed to deactivate wifi, Error - " + (WiFiError)ret);
425 WiFiErrorFactory.ThrowWiFiException(ret);
430 internal Task ScanAsync()
432 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
436 id = (IntPtr)_requestId++;
437 _callback_map[id] = (error, key) =>
439 Log.Debug(Globals.LogTag, "Scanning finished");
440 if (error != (int)WiFiError.None)
442 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
443 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
445 task.SetResult(true);
448 _callback_map.Remove(key);
452 int ret = Interop.WiFi.Scan(GetHandle(), _callback_map[id], id);
453 if (ret != (int)WiFiError.None)
455 Log.Error(Globals.LogTag, "Failed to scan all AP, Error - " + (WiFiError)ret);
456 WiFiErrorFactory.ThrowWiFiException(ret);
461 internal Task ScanSpecificApAsync(string essid)
463 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
467 id = (IntPtr)_requestId++;
468 _callback_map[id] = (error, key) =>
470 Log.Debug(Globals.LogTag, "Scanning with specific AP finished");
471 if (error != (int)WiFiError.None)
473 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
474 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
476 task.SetResult(true);
479 _callback_map.Remove(key);
483 int ret = Interop.WiFi.ScanSpecificAp(GetHandle(), essid, _callback_map[id], id);
484 if (ret != (int)WiFiError.None)
486 Log.Error(Globals.LogTag, "Failed to scan with specific AP, Error - " + (WiFiError)ret);
487 WiFiErrorFactory.ThrowWiFiException(ret);
492 internal Task ConnectAsync(WiFiAp ap)
494 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
498 id = (IntPtr)_requestId++;
499 _callback_map[id] = (error, key) =>
501 Log.Debug(Globals.LogTag, "Connecting finished : " + (WiFiError)error);
502 if (error != (int)WiFiError.None)
504 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
505 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
507 task.SetResult(true);
510 _callback_map.Remove(key);
514 IntPtr apHandle = ap.GetHandle();
515 int ret = Interop.WiFi.Connect(GetHandle(), apHandle, _callback_map[id], id);
516 if (ret != (int)WiFiError.None)
518 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
519 WiFiErrorFactory.ThrowWiFiException(ret);
524 internal Task DisconnectAsync(WiFiAp ap)
526 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
530 id = (IntPtr)_requestId++;
531 _callback_map[id] = (error, key) =>
533 Log.Debug(Globals.LogTag, "Disconnecting finished");
534 if (error != (int)WiFiError.None)
536 Log.Error(Globals.LogTag, "Error occurs during WiFi disconnecting, " + (WiFiError)error);
537 task.SetException(new InvalidOperationException("Error occurs during WiFi disconnecting, " + (WiFiError)error));
539 task.SetResult(true);
542 _callback_map.Remove(key);
546 IntPtr apHandle = ap.GetHandle();
547 int ret = Interop.WiFi.Disconnect(GetHandle(), apHandle, _callback_map[id], id);
548 if (ret != (int)WiFiError.None)
550 Log.Error(Globals.LogTag, "Failed to disconnect wifi, Error - " + (WiFiError)ret);
551 WiFiErrorFactory.ThrowWiFiException(ret);
556 internal Task ConnectByWpsPbcAsync(WiFiAp ap)
558 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
562 id = (IntPtr)_requestId++;
563 _callback_map[id] = (error, key) =>
565 Log.Debug(Globals.LogTag, "Connecting by WPS PBC finished");
566 if (error != (int)WiFiError.None)
568 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
569 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
571 task.SetResult(true);
574 _callback_map.Remove(key);
578 IntPtr apHandle = ap.GetHandle();
579 int ret = Interop.WiFi.ConnectByWpsPbc(GetHandle(), apHandle, _callback_map[id], id);
580 if (ret != (int)WiFiError.None)
582 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
583 WiFiErrorFactory.ThrowWiFiException(ret);
588 internal Task ConnectByWpsPinAsync(WiFiAp ap, string pin)
590 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
594 id = (IntPtr)_requestId++;
595 _callback_map[id] = (error, key) =>
597 Log.Debug(Globals.LogTag, "Connecting by WPS PIN finished");
598 if (error != (int)WiFiError.None)
600 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
601 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
603 task.SetResult(true);
606 _callback_map.Remove(key);
610 IntPtr apHandle = ap.GetHandle();
611 int ret = Interop.WiFi.ConnectByWpsPin(GetHandle(), apHandle, pin, _callback_map[id], id);
612 if (ret != (int)WiFiError.None)
614 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
615 WiFiErrorFactory.ThrowWiFiException(ret);