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 _handle = WiFiManagerImpl.Instance.Initialize();
37 Log.Debug(Globals.LogTag, "Handle: " + _handle);
40 internal Interop.WiFi.SafeWiFiManagerHandle GetSafeHandle()
42 Log.Debug(Globals.LogTag, "Handleholder safehandle = " + _handle);
47 internal partial class WiFiManagerImpl
49 private static WiFiManagerImpl _instance = null;
50 private Dictionary<IntPtr, Interop.WiFi.VoidCallback> _callback_map = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
51 private int _requestId = 0;
52 private string _macAddress;
54 internal string MacAddress
58 if (String.IsNullOrEmpty(_macAddress))
61 int ret = Interop.WiFi.GetMacAddress(GetSafeHandle(), out address);
62 if (ret != (int)WiFiError.None)
64 Log.Error(Globals.LogTag, "Failed to get mac address, Error - " + (WiFiError)ret);
69 _macAddress = address;
76 internal string InterfaceName
81 int ret = Interop.WiFi.GetNetworkInterfaceName(GetSafeHandle(), out name);
82 if (ret != (int)WiFiError.None)
84 Log.Error(Globals.LogTag, "Failed to get interface name, Error - " + (WiFiError)ret);
91 internal WiFiConnectionState ConnectionState
96 int ret = Interop.WiFi.GetConnectionState(GetSafeHandle(), out state);
97 if (ret != (int)WiFiError.None)
99 Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
100 return WiFiConnectionState.Failure;
102 return (WiFiConnectionState)state;
106 internal bool IsActive
111 int ret = Interop.WiFi.IsActive(GetSafeHandle(), out active);
112 if (ret != (int)WiFiError.None)
114 Log.Error(Globals.LogTag, "Failed to get isActive, Error - " + (WiFiError)ret);
120 internal static WiFiManagerImpl Instance
124 if (_instance == null)
126 Log.Debug(Globals.LogTag, "Instance is null");
127 _instance = new WiFiManagerImpl();
134 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
136 Log.Info(Globals.LogTag, "In threadlocal delegate");
137 return new HandleHolder();
140 private WiFiManagerImpl()
144 internal Interop.WiFi.SafeWiFiManagerHandle GetSafeHandle()
146 return s_threadName.Value.GetSafeHandle();
149 internal Interop.WiFi.SafeWiFiManagerHandle Initialize()
151 Interop.WiFi.SafeWiFiManagerHandle handle;
152 int ret = Interop.WiFi.Initialize(out handle);
153 if (ret != (int)WiFiError.None)
155 Log.Error(Globals.LogTag, "Failed to initialize wifi, Error - " + (WiFiError)ret);
156 WiFiErrorFactory.ThrowWiFiException(ret);
161 internal IEnumerable<WiFiAP> GetFoundAPs()
163 Log.Debug(Globals.LogTag, "GetFoundAPs");
164 List<WiFiAP> apList = new List<WiFiAP>();
165 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
167 if (apHandle != IntPtr.Zero)
170 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
171 WiFiAP apItem = new WiFiAP(clonedHandle);
178 int ret = Interop.WiFi.GetForeachFoundAPs(GetSafeHandle(), callback, IntPtr.Zero);
179 if (ret != (int)WiFiError.None)
181 Log.Error(Globals.LogTag, "Failed to get all APs, Error - " + (WiFiError)ret);
182 WiFiErrorFactory.ThrowWiFiException(ret);
188 internal IEnumerable<WiFiAP> GetFoundSpecificAPs()
190 Log.Debug(Globals.LogTag, "GetFoundSpecificAPs");
191 List<WiFiAP> apList = new List<WiFiAP>();
192 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
194 if (apHandle != IntPtr.Zero)
197 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
198 WiFiAP apItem = new WiFiAP(clonedHandle);
206 int ret = Interop.WiFi.GetForeachFoundSpecificAPs(GetSafeHandle(), callback, IntPtr.Zero);
207 if (ret != (int)WiFiError.None)
209 Log.Error(Globals.LogTag, "Failed to get specific APs, Error - " + (WiFiError)ret);
210 WiFiErrorFactory.ThrowWiFiException(ret);
216 internal IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
218 Log.Debug(Globals.LogTag, "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 Log.Debug(Globals.LogTag, "SaveWiFiNetworkConfiguration");
246 IntPtr configHandle = config.GetHandle();
247 int ret = Interop.WiFi.Config.SaveConfiguration(GetSafeHandle(), configHandle);
248 if (ret != (int)WiFiError.None)
250 Log.Error(Globals.LogTag, "Failed to save configuration, Error - " + (WiFiError)ret);
251 WiFiErrorFactory.ThrowWiFiException(ret);
255 internal WiFiAP GetConnectedAP()
257 Log.Debug(Globals.LogTag, "GetConnectedAP");
259 int ret = Interop.WiFi.GetConnectedAP(GetSafeHandle(), out apHandle);
260 if (ret != (int)WiFiError.None)
262 Log.Error(Globals.LogTag, "Failed to connect with AP, Error - " + (WiFiError)ret);
263 WiFiErrorFactory.ThrowWiFiException(ret);
265 WiFiAP ap = new WiFiAP(apHandle);
269 internal Task ActivateAsync()
271 Log.Debug(Globals.LogTag, "ActivateAsync");
272 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
276 id = (IntPtr)_requestId++;
277 _callback_map[id] = (error, key) =>
279 Log.Debug(Globals.LogTag, "wifi activated");
280 if (error != (int)WiFiError.None)
282 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
283 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
285 task.SetResult(true);
288 _callback_map.Remove(key);
292 int ret = Interop.WiFi.Activate(GetSafeHandle(), _callback_map[id], id);
293 if (ret != (int)WiFiError.None)
295 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
296 WiFiErrorFactory.ThrowWiFiException(ret);
301 internal Task ActivateWithWiFiPickerTestedAsync()
303 Log.Debug(Globals.LogTag, "ActivateWithWiFiPickerTestedAsync");
304 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
308 id = (IntPtr)_requestId++;
309 _callback_map[id] = (error, key) =>
311 Log.Debug(Globals.LogTag, "Activation finished");
312 if (error != (int)WiFiError.None)
314 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
315 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
317 task.SetResult(true);
320 _callback_map.Remove(key);
324 int ret = Interop.WiFi.ActivateWithWiFiPickerTested(GetSafeHandle(), _callback_map[id], id);
325 if (ret != (int)WiFiError.None)
327 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
328 WiFiErrorFactory.ThrowWiFiException(ret);
333 internal Task DeactivateAsync()
335 Log.Debug(Globals.LogTag, "DeactivateAsync");
336 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
340 id = (IntPtr)_requestId++;
341 _callback_map[id] = (error, key) =>
343 Log.Debug(Globals.LogTag, "Deactivation finished");
344 if (error != (int)WiFiError.None)
346 Log.Error(Globals.LogTag, "Error occurs during WiFi deactivating, " + (WiFiError)error);
347 task.SetException(new InvalidOperationException("Error occurs during WiFi deactivating, " + (WiFiError)error));
349 task.SetResult(true);
352 _callback_map.Remove(key);
356 int ret = Interop.WiFi.Deactivate(GetSafeHandle(), _callback_map[id], id);
357 if (ret != (int)WiFiError.None)
359 Log.Error(Globals.LogTag, "Failed to deactivate wifi, Error - " + (WiFiError)ret);
360 WiFiErrorFactory.ThrowWiFiException(ret);
365 internal Task ScanAsync()
367 Log.Debug(Globals.LogTag, "ScanAsync");
368 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
372 id = (IntPtr)_requestId++;
373 _callback_map[id] = (error, key) =>
375 Log.Debug(Globals.LogTag, "Scanning finished");
376 if (error != (int)WiFiError.None)
378 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
379 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
381 task.SetResult(true);
384 _callback_map.Remove(key);
388 int ret = Interop.WiFi.Scan(GetSafeHandle(), _callback_map[id], id);
389 if (ret != (int)WiFiError.None)
391 Log.Error(Globals.LogTag, "Failed to scan all AP, Error - " + (WiFiError)ret);
392 WiFiErrorFactory.ThrowWiFiException(ret);
397 internal Task ScanSpecificAPAsync(string essid)
399 Log.Debug(Globals.LogTag, "ScanSpecificAPAsync " + essid);
400 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
404 id = (IntPtr)_requestId++;
405 _callback_map[id] = (error, key) =>
407 Log.Debug(Globals.LogTag, "Scanning with specific AP finished");
408 if (error != (int)WiFiError.None)
410 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
411 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
413 task.SetResult(true);
416 _callback_map.Remove(key);
420 int ret = Interop.WiFi.ScanSpecificAP(GetSafeHandle(), essid, _callback_map[id], id);
421 if (ret != (int)WiFiError.None)
423 Log.Error(Globals.LogTag, "Failed to scan with specific AP, Error - " + (WiFiError)ret);
424 WiFiErrorFactory.ThrowWiFiException(ret);