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 SafeWiFiManagerHandle _handle;
34 internal HandleHolder()
36 _handle = WiFiManagerImpl.Instance.Initialize();
37 Log.Debug(Globals.LogTag, "Handle: " + _handle);
40 internal SafeWiFiManagerHandle GetSafeHandle()
42 Log.Debug(Globals.LogTag, "Handleholder safehandle = " + _handle);
47 internal partial class WiFiManagerImpl
49 private static readonly Lazy<WiFiManagerImpl> _instance =
50 new Lazy<WiFiManagerImpl>(() => new WiFiManagerImpl());
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 return _instance.Value;
129 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
131 Log.Info(Globals.LogTag, "In threadlocal delegate");
132 return new HandleHolder();
135 private WiFiManagerImpl()
137 Log.Info(Globals.LogTag, "WiFiManagerImpl constructor");
140 internal SafeWiFiManagerHandle GetSafeHandle()
142 return s_threadName.Value.GetSafeHandle();
145 internal SafeWiFiManagerHandle Initialize()
147 SafeWiFiManagerHandle handle;
148 int ret = Interop.WiFi.Initialize(out handle);
149 if (ret != (int)WiFiError.None)
151 Log.Error(Globals.LogTag, "Failed to initialize wifi, Error - " + (WiFiError)ret);
152 WiFiErrorFactory.ThrowWiFiException(ret, "http://tizen.org/privilege/network.get");
157 internal IEnumerable<WiFiAP> GetFoundAPs()
159 Log.Debug(Globals.LogTag, "GetFoundAPs");
160 List<WiFiAP> apList = new List<WiFiAP>();
161 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
163 if (apHandle != IntPtr.Zero)
166 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
167 WiFiAP apItem = new WiFiAP(clonedHandle);
174 int ret = Interop.WiFi.GetForeachFoundAPs(GetSafeHandle(), callback, IntPtr.Zero);
175 if (ret != (int)WiFiError.None)
177 Log.Error(Globals.LogTag, "Failed to get all APs, Error - " + (WiFiError)ret);
178 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.get");
184 internal IEnumerable<WiFiAP> GetFoundSpecificAPs()
186 Log.Debug(Globals.LogTag, "GetFoundSpecificAPs");
187 List<WiFiAP> apList = new List<WiFiAP>();
188 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
190 if (apHandle != IntPtr.Zero)
193 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
194 WiFiAP apItem = new WiFiAP(clonedHandle);
202 int ret = Interop.WiFi.GetForeachFoundSpecificAPs(GetSafeHandle(), callback, IntPtr.Zero);
203 if (ret != (int)WiFiError.None)
205 Log.Error(Globals.LogTag, "Failed to get specific APs, Error - " + (WiFiError)ret);
206 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.get");
212 internal IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
214 Log.Debug(Globals.LogTag, "GetWiFiConfigurations");
215 List<WiFiConfiguration> configList = new List<WiFiConfiguration>();
216 Interop.WiFi.HandleCallback callback = (IntPtr configHandle, IntPtr userData) =>
218 if (configHandle != IntPtr.Zero)
221 Interop.WiFi.Config.Clone(configHandle, out clonedConfig);
222 WiFiConfiguration configItem = new WiFiConfiguration(clonedConfig);
223 configList.Add(configItem);
229 int ret = Interop.WiFi.Config.GetForeachConfiguration(GetSafeHandle(), callback, IntPtr.Zero);
230 if (ret != (int)WiFiError.None)
232 Log.Error(Globals.LogTag, "Failed to get configurations, Error - " + (WiFiError)ret);
233 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.profile");
239 internal void SaveWiFiNetworkConfiguration(WiFiConfiguration config)
241 Log.Debug(Globals.LogTag, "SaveWiFiNetworkConfiguration");
244 throw new ArgumentNullException("WiFi configuration is null");
247 IntPtr configHandle = config.GetHandle();
248 int ret = Interop.WiFi.Config.SaveConfiguration(GetSafeHandle(), configHandle);
249 if (ret != (int)WiFiError.None)
251 Log.Error(Globals.LogTag, "Failed to save configuration, Error - " + (WiFiError)ret);
252 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.profile");
256 internal WiFiAP GetConnectedAP()
258 Log.Debug(Globals.LogTag, "GetConnectedAP");
260 int ret = Interop.WiFi.GetConnectedAP(GetSafeHandle(), out apHandle);
261 if (ret != (int)WiFiError.None)
263 if (ret == (int)WiFiError.NoConnectionError)
265 Log.Error(Globals.LogTag, "No connection " + (WiFiError)ret);
270 Log.Error(Globals.LogTag, "Failed to get connected AP, Error - " + (WiFiError)ret);
271 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.get");
274 WiFiAP ap = new WiFiAP(apHandle);
278 internal Task ActivateAsync()
280 Log.Debug(Globals.LogTag, "ActivateAsync");
281 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
285 id = (IntPtr)_requestId++;
286 _callback_map[id] = (error, key) =>
288 Log.Debug(Globals.LogTag, "wifi activated");
289 if (error != (int)WiFiError.None)
291 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
292 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
296 task.SetResult(true);
300 _callback_map.Remove(key);
304 int ret = Interop.WiFi.Activate(GetSafeHandle(), _callback_map[id], id);
305 if (ret != (int)WiFiError.None)
307 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
308 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
313 internal Task ActivateWithWiFiPickerTestedAsync()
315 Log.Debug(Globals.LogTag, "ActivateWithWiFiPickerTestedAsync");
316 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
320 id = (IntPtr)_requestId++;
321 _callback_map[id] = (error, key) =>
323 Log.Debug(Globals.LogTag, "Activation finished");
324 if (error != (int)WiFiError.None)
326 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
327 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
331 task.SetResult(true);
335 _callback_map.Remove(key);
339 int ret = Interop.WiFi.ActivateWithWiFiPickerTested(GetSafeHandle(), _callback_map[id], id);
340 if (ret != (int)WiFiError.None)
342 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
343 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
348 internal Task DeactivateAsync()
350 Log.Debug(Globals.LogTag, "DeactivateAsync");
351 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
355 id = (IntPtr)_requestId++;
356 _callback_map[id] = (error, key) =>
358 Log.Debug(Globals.LogTag, "Deactivation finished");
359 if (error != (int)WiFiError.None)
361 Log.Error(Globals.LogTag, "Error occurs during WiFi deactivating, " + (WiFiError)error);
362 task.SetException(new InvalidOperationException("Error occurs during WiFi deactivating, " + (WiFiError)error));
366 task.SetResult(true);
370 _callback_map.Remove(key);
374 int ret = Interop.WiFi.Deactivate(GetSafeHandle(), _callback_map[id], id);
375 if (ret != (int)WiFiError.None)
377 Log.Error(Globals.LogTag, "Failed to deactivate wifi, Error - " + (WiFiError)ret);
378 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
383 internal Task ScanAsync()
385 Log.Debug(Globals.LogTag, "ScanAsync");
386 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
390 id = (IntPtr)_requestId++;
391 _callback_map[id] = (error, key) =>
393 Log.Debug(Globals.LogTag, "Scanning finished");
394 if (error != (int)WiFiError.None)
396 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
397 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
401 task.SetResult(true);
405 _callback_map.Remove(key);
409 int ret = Interop.WiFi.Scan(GetSafeHandle(), _callback_map[id], id);
410 if (ret != (int)WiFiError.None)
412 Log.Error(Globals.LogTag, "Failed to scan all AP, Error - " + (WiFiError)ret);
413 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
418 internal Task ScanSpecificAPAsync(string essid)
420 Log.Debug(Globals.LogTag, "ScanSpecificAPAsync " + essid);
421 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
425 id = (IntPtr)_requestId++;
426 _callback_map[id] = (error, key) =>
428 Log.Debug(Globals.LogTag, "Scanning with specific AP finished");
429 if (error != (int)WiFiError.None)
431 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
432 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
436 task.SetResult(true);
440 _callback_map.Remove(key);
444 int ret = Interop.WiFi.ScanSpecificAP(GetSafeHandle(), essid, _callback_map[id], id);
445 if (ret != (int)WiFiError.None)
447 Log.Error(Globals.LogTag, "Failed to scan with specific AP, Error - " + (WiFiError)ret);
448 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());