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 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 SafeWiFiManagerHandle GetSafeHandle()
146 return s_threadName.Value.GetSafeHandle();
149 internal SafeWiFiManagerHandle Initialize()
151 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, "http://tizen.org/privilege/network.get");
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, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.get");
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, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.get");
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, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.profile");
243 internal void SaveWiFiNetworkConfiguration(WiFiConfiguration config)
245 Log.Debug(Globals.LogTag, "SaveWiFiNetworkConfiguration");
248 throw new ArgumentNullException("WiFi configuration is null");
251 IntPtr configHandle = config.GetHandle();
252 int ret = Interop.WiFi.Config.SaveConfiguration(GetSafeHandle(), configHandle);
253 if (ret != (int)WiFiError.None)
255 Log.Error(Globals.LogTag, "Failed to save configuration, Error - " + (WiFiError)ret);
256 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.profile");
260 internal WiFiAP GetConnectedAP()
262 Log.Debug(Globals.LogTag, "GetConnectedAP");
264 int ret = Interop.WiFi.GetConnectedAP(GetSafeHandle(), out apHandle);
265 if (ret != (int)WiFiError.None)
267 if (ret == (int)WiFiError.NoConnectionError)
269 Log.Error(Globals.LogTag, "No connection " + (WiFiError)ret);
274 Log.Error(Globals.LogTag, "Failed to get connected AP, Error - " + (WiFiError)ret);
275 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.get");
278 WiFiAP ap = new WiFiAP(apHandle);
282 internal Task ActivateAsync()
284 Log.Debug(Globals.LogTag, "ActivateAsync");
285 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
289 id = (IntPtr)_requestId++;
290 _callback_map[id] = (error, key) =>
292 Log.Debug(Globals.LogTag, "wifi activated");
293 if (error != (int)WiFiError.None)
295 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
296 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
300 task.SetResult(true);
304 _callback_map.Remove(key);
308 int ret = Interop.WiFi.Activate(GetSafeHandle(), _callback_map[id], id);
309 if (ret != (int)WiFiError.None)
311 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
312 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
317 internal Task ActivateWithWiFiPickerTestedAsync()
319 Log.Debug(Globals.LogTag, "ActivateWithWiFiPickerTestedAsync");
320 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
324 id = (IntPtr)_requestId++;
325 _callback_map[id] = (error, key) =>
327 Log.Debug(Globals.LogTag, "Activation finished");
328 if (error != (int)WiFiError.None)
330 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
331 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
335 task.SetResult(true);
339 _callback_map.Remove(key);
343 int ret = Interop.WiFi.ActivateWithWiFiPickerTested(GetSafeHandle(), _callback_map[id], id);
344 if (ret != (int)WiFiError.None)
346 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
347 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
352 internal Task DeactivateAsync()
354 Log.Debug(Globals.LogTag, "DeactivateAsync");
355 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
359 id = (IntPtr)_requestId++;
360 _callback_map[id] = (error, key) =>
362 Log.Debug(Globals.LogTag, "Deactivation finished");
363 if (error != (int)WiFiError.None)
365 Log.Error(Globals.LogTag, "Error occurs during WiFi deactivating, " + (WiFiError)error);
366 task.SetException(new InvalidOperationException("Error occurs during WiFi deactivating, " + (WiFiError)error));
370 task.SetResult(true);
374 _callback_map.Remove(key);
378 int ret = Interop.WiFi.Deactivate(GetSafeHandle(), _callback_map[id], id);
379 if (ret != (int)WiFiError.None)
381 Log.Error(Globals.LogTag, "Failed to deactivate wifi, Error - " + (WiFiError)ret);
382 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
387 internal Task ScanAsync()
389 Log.Debug(Globals.LogTag, "ScanAsync");
390 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
394 id = (IntPtr)_requestId++;
395 _callback_map[id] = (error, key) =>
397 Log.Debug(Globals.LogTag, "Scanning finished");
398 if (error != (int)WiFiError.None)
400 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
401 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
405 task.SetResult(true);
409 _callback_map.Remove(key);
413 int ret = Interop.WiFi.Scan(GetSafeHandle(), _callback_map[id], id);
414 if (ret != (int)WiFiError.None)
416 Log.Error(Globals.LogTag, "Failed to scan all AP, Error - " + (WiFiError)ret);
417 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
422 internal Task ScanSpecificAPAsync(string essid)
424 Log.Debug(Globals.LogTag, "ScanSpecificAPAsync " + essid);
425 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
429 id = (IntPtr)_requestId++;
430 _callback_map[id] = (error, key) =>
432 Log.Debug(Globals.LogTag, "Scanning with specific AP finished");
433 if (error != (int)WiFiError.None)
435 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
436 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
440 task.SetResult(true);
444 _callback_map.Remove(key);
448 int ret = Interop.WiFi.ScanSpecificAP(GetSafeHandle(), essid, _callback_map[id], id);
449 if (ret != (int)WiFiError.None)
451 Log.Error(Globals.LogTag, "Failed to scan with specific AP, Error - " + (WiFiError)ret);
452 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
457 internal void UpdateAP(Interop.WiFi.SafeWiFiAPHandle apHandle)
459 int ret = Interop.WiFi.UpdateAP(GetSafeHandle(), apHandle.DangerousGetHandle());
460 if (ret != (int)WiFiError.None)
462 Log.Error(Globals.LogTag, "Failed to update AP, Error - " + (WiFiError)ret);
463 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), apHandle.DangerousGetHandle());