2 * Copyright (c) 2018 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;
22 using Tizen.Applications;
24 namespace Tizen.Network.WiFi
26 static internal class Globals
28 internal const string LogTag = "Tizen.Network.WiFi";
31 internal class HandleHolder
33 private SafeWiFiManagerHandle _handle;
35 internal HandleHolder()
37 _handle = WiFiManagerImpl.Instance.Initialize();
38 Log.Info(Globals.LogTag, "Handle: " + _handle);
41 internal SafeWiFiManagerHandle GetSafeHandle()
43 Log.Debug(Globals.LogTag, "Handleholder safehandle = " + _handle);
48 internal partial class WiFiManagerImpl
50 private static readonly Lazy<WiFiManagerImpl> _instance =
51 new Lazy<WiFiManagerImpl>(() => new WiFiManagerImpl());
53 private TizenSynchronizationContext context = new TizenSynchronizationContext();
55 private Dictionary<IntPtr, Interop.WiFi.VoidCallback> _callback_map =
56 new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
58 private int _requestId = 0;
59 private string _macAddress;
61 //private string PrivilegeNetworkSet = "http://tizen.org/privilege/network.set";
62 private string PrivilegeNetworkGet = "http://tizen.org/privilege/network.get";
63 private string PrivilegeNetworkProfile = "http://tizen.org/privilege/network.get";
65 internal string MacAddress
69 if (String.IsNullOrEmpty(_macAddress))
72 int ret = Interop.WiFi.GetMacAddress(GetSafeHandle(), out address);
73 if (ret != (int)WiFiError.None)
75 Log.Error(Globals.LogTag, "Failed to get mac address, Error - " + (WiFiError)ret);
80 _macAddress = address;
87 internal string InterfaceName
92 int ret = Interop.WiFi.GetNetworkInterfaceName(GetSafeHandle(), out name);
93 if (ret != (int)WiFiError.None)
95 Log.Error(Globals.LogTag, "Failed to get interface name, Error - " + (WiFiError)ret);
102 internal WiFiConnectionState ConnectionState
107 int ret = Interop.WiFi.GetConnectionState(GetSafeHandle(), out state);
108 if (ret != (int)WiFiError.None)
110 Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
111 return WiFiConnectionState.Failure;
113 return (WiFiConnectionState)state;
117 internal bool IsActive
122 int ret = Interop.WiFi.IsActive(GetSafeHandle(), out active);
123 if (ret != (int)WiFiError.None)
125 Log.Error(Globals.LogTag, "Failed to get isActive, Error - " + (WiFiError)ret);
131 internal static WiFiManagerImpl Instance
135 return _instance.Value;
139 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
141 Log.Info(Globals.LogTag, "In threadlocal delegate");
142 return new HandleHolder();
145 private WiFiManagerImpl()
147 Log.Info(Globals.LogTag, "WiFiManagerImpl constructor");
150 internal SafeWiFiManagerHandle GetSafeHandle()
152 return s_threadName.Value.GetSafeHandle();
155 internal SafeWiFiManagerHandle Initialize()
157 SafeWiFiManagerHandle handle;
158 int tid = Thread.CurrentThread.ManagedThreadId;
159 Log.Info(Globals.LogTag, "PInvoke wifi_manager_initialize");
160 int ret = Interop.WiFi.Initialize(tid, out handle);
161 if (ret != (int)WiFiError.None)
163 Log.Error(Globals.LogTag, "Initialize Fail, Error - " + (WiFiError)ret);
164 WiFiErrorFactory.ThrowWiFiException(ret, PrivilegeNetworkGet);
170 internal IEnumerable<WiFiAP> GetFoundAPs()
172 Log.Info(Globals.LogTag, "GetFoundAPs");
173 List<WiFiAP> apList = new List<WiFiAP>();
174 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
176 if (apHandle != IntPtr.Zero)
179 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
180 WiFiAP apItem = new WiFiAP(clonedHandle);
187 int ret = Interop.WiFi.GetForeachFoundAPs(GetSafeHandle(), callback, IntPtr.Zero);
188 CheckReturnValue(ret, "GetForeachFoundAPs", PrivilegeNetworkGet);
193 internal IEnumerable<WiFiAP> GetFoundSpecificAPs()
195 Log.Info(Globals.LogTag, "GetFoundSpecificAPs");
196 List<WiFiAP> apList = new List<WiFiAP>();
197 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
199 if (apHandle != IntPtr.Zero)
202 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
203 WiFiAP apItem = new WiFiAP(clonedHandle);
211 int ret = Interop.WiFi.GetForeachFoundSpecificAPs(GetSafeHandle(), callback, IntPtr.Zero);
212 CheckReturnValue(ret, "GetForeachFoundSpecificAPs", PrivilegeNetworkGet);
217 internal IEnumerable<WiFiAP> GetFoundBssids()
219 Log.Info(Globals.LogTag, "GetFoundBssids");
220 List<WiFiAP> apList = new List<WiFiAP>();
221 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
223 if (apHandle != IntPtr.Zero)
226 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
227 WiFiAP apItem = new WiFiAP(clonedHandle);
234 int ret = Interop.WiFi.GetForeachFoundBssids(GetSafeHandle(), callback, IntPtr.Zero);
235 CheckReturnValue(ret, "GetForeachFoundBssids", PrivilegeNetworkGet);
240 internal IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
242 Log.Debug(Globals.LogTag, "GetWiFiConfigurations");
243 List<WiFiConfiguration> configList = new List<WiFiConfiguration>();
244 Interop.WiFi.HandleCallback callback = (IntPtr configHandle, IntPtr userData) =>
246 if (configHandle != IntPtr.Zero)
249 Interop.WiFi.Config.Clone(configHandle, out clonedConfig);
250 WiFiConfiguration configItem = new WiFiConfiguration(clonedConfig);
251 configList.Add(configItem);
257 int ret = Interop.WiFi.Config.GetForeachConfiguration(GetSafeHandle(), callback, IntPtr.Zero);
258 CheckReturnValue(ret, "GetForeachConfiguration", PrivilegeNetworkProfile);
262 internal void SaveWiFiNetworkConfiguration(WiFiConfiguration config)
264 Log.Debug(Globals.LogTag, "SaveWiFiNetworkConfiguration");
267 throw new ArgumentNullException("WiFi configuration is null");
270 IntPtr configHandle = config.GetHandle();
271 int ret = Interop.WiFi.Config.SaveConfiguration(GetSafeHandle(), configHandle);
272 CheckReturnValue(ret, "SaveConfiguration", PrivilegeNetworkProfile);
275 internal WiFiAP GetConnectedAP()
277 Log.Info(Globals.LogTag, "GetConnectedAP");
279 int ret = Interop.WiFi.GetConnectedAP(GetSafeHandle(), out apHandle);
280 if (ret == (int)WiFiError.NoConnectionError)
282 Log.Error(Globals.LogTag, "No connection " + (WiFiError)ret);
285 CheckReturnValue(ret, "GetConnectedAP", PrivilegeNetworkGet);
286 WiFiAP ap = new WiFiAP(apHandle);
290 internal Task ActivateAsync()
292 Log.Info(Globals.LogTag, "ActivateAsync");
293 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
297 id = (IntPtr)_requestId++;
298 _callback_map[id] = (error, key) =>
300 Log.Info(Globals.LogTag, "ActivateAsync done");
301 if (error != (int)WiFiError.None)
303 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
304 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
308 task.SetResult(true);
312 _callback_map.Remove(key);
319 Log.Info(Globals.LogTag, "Interop.WiFi.ActivateAsync");
322 int ret = Interop.WiFi.Activate(GetSafeHandle(), _callback_map[id], id);
323 CheckReturnValue(ret, "Activate", "");
327 Log.Error(Globals.LogTag, "Exception on ActivateAsync\n" + e);
328 task.SetException(e);
335 internal Task ActivateWithWiFiPickerTestedAsync()
337 Log.Info(Globals.LogTag, "ActivateWithWiFiPickerTestedAsync");
338 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
342 id = (IntPtr)_requestId++;
343 _callback_map[id] = (error, key) =>
345 Log.Info(Globals.LogTag, "ActivateWithWiFiPickerTestedAsync done");
346 if (error != (int)WiFiError.None)
348 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
349 task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
353 task.SetResult(true);
357 _callback_map.Remove(key);
364 Log.Info(Globals.LogTag, "Interop.WiFi.ActivateWithWiFiPickerTestedAsync");
367 int ret = Interop.WiFi.ActivateWithWiFiPickerTested(GetSafeHandle(), _callback_map[id], id);
368 CheckReturnValue(ret, "ActivateWithWiFiPickerTested", "");
372 Log.Error(Globals.LogTag, "Exception on ActivateWithWiFiPickerTestedAsync\n" + e);
373 task.SetException(e);
380 internal Task DeactivateAsync()
382 Log.Info(Globals.LogTag, "DeactivateAsync");
383 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
387 id = (IntPtr)_requestId++;
388 _callback_map[id] = (error, key) =>
390 Log.Info(Globals.LogTag, "DeactivateAsync done");
391 if (error != (int)WiFiError.None)
393 Log.Error(Globals.LogTag, "Error occurs during WiFi deactivating, " + (WiFiError)error);
394 task.SetException(new InvalidOperationException("Error occurs during WiFi deactivating, " + (WiFiError)error));
398 task.SetResult(true);
402 _callback_map.Remove(key);
409 Log.Info(Globals.LogTag, "Interop.WiFi.Deactivate");
412 int ret = Interop.WiFi.Deactivate(GetSafeHandle(), _callback_map[id], id);
413 CheckReturnValue(ret, "Deactivate", "");
417 Log.Error(Globals.LogTag, "Exception on Deactivate\n" + e);
418 task.SetException(e);
425 internal Task ScanAsync()
427 Log.Info(Globals.LogTag, "ScanAsync");
428 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
432 id = (IntPtr)_requestId++;
433 _callback_map[id] = (error, key) =>
435 Log.Info(Globals.LogTag, "ScanAsync done");
436 if (error != (int)WiFiError.None)
438 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
439 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
443 task.SetResult(true);
447 _callback_map.Remove(key);
454 Log.Info(Globals.LogTag, "Interop.WiFi.Scan");
457 int ret = Interop.WiFi.Scan(GetSafeHandle(), _callback_map[id], id);
458 CheckReturnValue(ret, "Scan", "");
462 Log.Error(Globals.LogTag, "Exception on Scan\n" + e);
463 task.SetException(e);
470 internal Task ScanSpecificAPAsync(string essid)
472 Log.Info(Globals.LogTag, "ScanSpecificAPAsync " + essid);
473 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
477 id = (IntPtr)_requestId++;
478 _callback_map[id] = (error, key) =>
480 Log.Info(Globals.LogTag, "ScanSpecificAPAsync Done " + essid);
481 if (error != (int)WiFiError.None)
483 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
484 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
488 task.SetResult(true);
492 _callback_map.Remove(key);
499 Log.Info(Globals.LogTag, "Interop.WiFi.ScanSpecificAPAsync");
502 int ret = Interop.WiFi.ScanSpecificAP(GetSafeHandle(), essid, _callback_map[id], id);
503 CheckReturnValue(ret, "ScanSpecificAP", "");
507 Log.Error(Globals.LogTag, "Exception on ScanSpecificAPAsync\n" + e);
508 task.SetException(e);
515 internal Task BssidScanAsync()
517 Log.Info(Globals.LogTag, "BssidScanAsync");
518 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
522 id = (IntPtr)_requestId++;
523 _callback_map[id] = (error, key) =>
525 Log.Info(Globals.LogTag, "BssidScanAsync done");
526 if (error != (int)WiFiError.None)
528 Log.Error(Globals.LogTag, "Error occurs during bssid scanning, " + (WiFiError)error);
529 task.SetException(new InvalidOperationException("Error occurs during bssid scanning, " + (WiFiError)error));
533 task.SetResult(true);
537 _callback_map.Remove(key);
544 Log.Info(Globals.LogTag, "Interop.WiFi.BssidScan");
547 int ret = Interop.WiFi.BssidScan(GetSafeHandle(), _callback_map[id], id);
548 CheckReturnValue(ret, "BssidScan", "");
552 Log.Error(Globals.LogTag, "Exception on BssidScan\n" + e);
553 task.SetException(e);
560 private void CheckReturnValue(int ret, string method, string privilege)
562 if (ret != (int)WiFiError.None)
564 Log.Error(Globals.LogTag, method + " Fail, Error - " + (WiFiError)ret);
565 if (ret == (int)WiFiError.InvalidParameterError)
567 throw new InvalidOperationException("Invalid handle");
569 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), privilege);