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;
21 using System.Runtime.InteropServices;
23 namespace Tizen.Network.WiFi
25 static internal class Globals
27 internal const string LogTag = "Tizen.Network.WiFi";
30 internal class HandleHolder
32 private Interop.WiFi.SafeWiFiManagerHandle _handle;
34 internal HandleHolder()
36 Log.Debug(Globals.LogTag, "HandleHolder() Constructor");
37 _handle = WiFiManagerImpl.Instance.Initialize();
38 Log.Debug(Globals.LogTag, "Handle: " + _handle);
41 internal Interop.WiFi.SafeWiFiManagerHandle GetSafeHandle()
43 Log.Debug(Globals.LogTag, "Handleholder safehandle = " + _handle);
48 internal partial class WiFiManagerImpl
50 private static WiFiManagerImpl _instance = null;
51 private Dictionary<IntPtr, Interop.WiFi.VoidCallback> _callback_map = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
52 private int _requestId = 0;
53 private string _macAddress;
55 internal string MacAddress
59 if (String.IsNullOrEmpty(_macAddress))
62 int ret = Interop.WiFi.GetMacAddress(GetSafeHandle(), out address);
63 if (ret != (int)WiFiError.None)
65 Log.Error(Globals.LogTag, "Failed to get mac address, Error - " + (WiFiError)ret);
70 _macAddress = address;
77 internal string InterfaceName
82 int ret = Interop.WiFi.GetNetworkInterfaceName(GetSafeHandle(), out name);
83 if (ret != (int)WiFiError.None)
85 Log.Error(Globals.LogTag, "Failed to get interface name, Error - " + (WiFiError)ret);
92 internal WiFiConnectionState ConnectionState
97 int ret = Interop.WiFi.GetConnectionState(GetSafeHandle(), out state);
98 if (ret != (int)WiFiError.None)
100 Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
101 return WiFiConnectionState.Failure;
103 return (WiFiConnectionState)state;
107 internal bool IsActive
112 int ret = Interop.WiFi.IsActive(GetSafeHandle(), out active);
113 if (ret != (int)WiFiError.None)
115 Log.Error(Globals.LogTag, "Failed to get isActive, Error - " + (WiFiError)ret);
121 internal static WiFiManagerImpl Instance
125 Log.Debug(Globals.LogTag, "Instance getter");
126 if (_instance == null)
128 Log.Debug(Globals.LogTag, "Instance is null");
129 _instance = new WiFiManagerImpl();
136 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
138 Log.Info(Globals.LogTag, "In threadlocal delegate");
139 return new HandleHolder();
142 private WiFiManagerImpl()
146 internal Interop.WiFi.SafeWiFiManagerHandle GetSafeHandle()
148 Log.Debug(Globals.LogTag, "GetHandle, Thread Id = " + Thread.CurrentThread.ManagedThreadId);
149 return s_threadName.Value.GetSafeHandle();
152 internal Interop.WiFi.SafeWiFiManagerHandle Initialize()
154 Interop.WiFi.SafeWiFiManagerHandle handle = new Interop.WiFi.SafeWiFiManagerHandle();
155 int ret = Interop.WiFi.Initialize(out handle);
156 if (ret != (int)WiFiError.None)
158 Log.Error(Globals.LogTag, "Failed to initialize wifi, Error - " + (WiFiError)ret);
159 WiFiErrorFactory.ThrowWiFiException(ret);
164 internal IEnumerable<WiFiAP> GetFoundAPs()
166 List<WiFiAP> apList = new List<WiFiAP>();
167 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
169 if (apHandle != IntPtr.Zero)
172 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
173 WiFiAP apItem = new WiFiAP(clonedHandle);
180 int ret = Interop.WiFi.GetForeachFoundAPs(GetSafeHandle(), callback, IntPtr.Zero);
181 if (ret != (int)WiFiError.None)
183 Log.Error(Globals.LogTag, "Failed to get all APs, Error - " + (WiFiError)ret);
184 WiFiErrorFactory.ThrowWiFiException(ret);
190 internal IEnumerable<WiFiAP> GetFoundSpecificAPs()
192 List<WiFiAP> apList = new List<WiFiAP>();
193 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
195 if (apHandle != IntPtr.Zero)
198 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
199 WiFiAP apItem = new WiFiAP(clonedHandle);
207 int ret = Interop.WiFi.GetForeachFoundSpecificAPs(GetSafeHandle(), callback, IntPtr.Zero);
208 if (ret != (int)WiFiError.None)
210 Log.Error(Globals.LogTag, "Failed to get specific APs, Error - " + (WiFiError)ret);
211 WiFiErrorFactory.ThrowWiFiException(ret);
217 internal IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
219 List<WiFiConfiguration> configList = new List<WiFiConfiguration>();
220 Interop.WiFi.HandleCallback callback = (IntPtr configHandle, IntPtr userData) =>
222 if (configHandle != IntPtr.Zero)
225 Interop.WiFi.Config.Clone(configHandle, out clonedConfig);
226 WiFiConfiguration configItem = new WiFiConfiguration(clonedConfig);
227 configList.Add(configItem);
233 int ret = Interop.WiFi.Config.GetForeachConfiguration(GetSafeHandle(), callback, IntPtr.Zero);
234 if (ret != (int)WiFiError.None)
236 Log.Error(Globals.LogTag, "Failed to get configurations, Error - " + (WiFiError)ret);
237 WiFiErrorFactory.ThrowWiFiException(ret);
243 internal void SaveWiFiNetworkConfiguration(WiFiConfiguration config)
245 IntPtr configHandle = config.GetHandle();
246 int ret = Interop.WiFi.Config.SaveConfiguration(GetSafeHandle(), configHandle);
247 if (ret != (int)WiFiError.None)
249 Log.Error(Globals.LogTag, "Failed to save configuration, Error - " + (WiFiError)ret);
250 WiFiErrorFactory.ThrowWiFiException(ret);
254 internal WiFiAP GetConnectedAP()
258 int ret = Interop.WiFi.GetConnectedAP(GetSafeHandle(), out apHandle);
259 if (ret != (int)WiFiError.None)
261 Log.Error(Globals.LogTag, "Failed to connect with AP, Error - " + (WiFiError)ret);
262 WiFiErrorFactory.ThrowWiFiException(ret);
264 WiFiAP ap = new WiFiAP(apHandle);
268 internal Task ActivateAsync()
270 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
274 id = (IntPtr)_requestId++;
275 _callback_map[id] = (error, key) =>
277 Log.Debug(Globals.LogTag, "wifi activated");
278 if (error != (int)WiFiError.None)
280 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
281 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
283 task.SetResult(true);
286 _callback_map.Remove(key);
290 int ret = Interop.WiFi.Activate(GetSafeHandle(), _callback_map[id], id);
291 if (ret != (int)WiFiError.None)
293 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
294 WiFiErrorFactory.ThrowWiFiException(ret);
299 internal Task ActivateWithWiFiPickerTestedAsync()
301 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
305 id = (IntPtr)_requestId++;
306 _callback_map[id] = (error, key) =>
308 Log.Debug(Globals.LogTag, "Activation finished");
309 if (error != (int)WiFiError.None)
311 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
312 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
314 task.SetResult(true);
317 _callback_map.Remove(key);
321 int ret = Interop.WiFi.ActivateWithWiFiPickerTested(GetSafeHandle(), _callback_map[id], id);
322 if (ret != (int)WiFiError.None)
324 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
325 WiFiErrorFactory.ThrowWiFiException(ret);
330 internal Task DeactivateAsync()
332 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
336 id = (IntPtr)_requestId++;
337 _callback_map[id] = (error, key) =>
339 Log.Debug(Globals.LogTag, "Deactivation finished");
340 if (error != (int)WiFiError.None)
342 Log.Error(Globals.LogTag, "Error occurs during WiFi deactivating, " + (WiFiError)error);
343 task.SetException(new InvalidOperationException("Error occurs during WiFi deactivating, " + (WiFiError)error));
345 task.SetResult(true);
348 _callback_map.Remove(key);
352 int ret = Interop.WiFi.Deactivate(GetSafeHandle(), _callback_map[id], id);
353 if (ret != (int)WiFiError.None)
355 Log.Error(Globals.LogTag, "Failed to deactivate wifi, Error - " + (WiFiError)ret);
356 WiFiErrorFactory.ThrowWiFiException(ret);
361 internal Task ScanAsync()
363 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
367 id = (IntPtr)_requestId++;
368 _callback_map[id] = (error, key) =>
370 Log.Debug(Globals.LogTag, "Scanning finished");
371 if (error != (int)WiFiError.None)
373 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
374 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
376 task.SetResult(true);
379 _callback_map.Remove(key);
383 int ret = Interop.WiFi.Scan(GetSafeHandle(), _callback_map[id], id);
384 if (ret != (int)WiFiError.None)
386 Log.Error(Globals.LogTag, "Failed to scan all AP, Error - " + (WiFiError)ret);
387 WiFiErrorFactory.ThrowWiFiException(ret);
392 internal Task ScanSpecificAPAsync(string essid)
394 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
398 id = (IntPtr)_requestId++;
399 _callback_map[id] = (error, key) =>
401 Log.Debug(Globals.LogTag, "Scanning with specific AP finished");
402 if (error != (int)WiFiError.None)
404 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
405 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
407 task.SetResult(true);
410 _callback_map.Remove(key);
414 int ret = Interop.WiFi.ScanSpecificAP(GetSafeHandle(), essid, _callback_map[id], id);
415 if (ret != (int)WiFiError.None)
417 Log.Error(Globals.LogTag, "Failed to scan with specific AP, Error - " + (WiFiError)ret);
418 WiFiErrorFactory.ThrowWiFiException(ret);