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 WiFiScanState ScanState
136 int ret = Interop.WiFi.GetScanState(GetSafeHandle(), out state);
137 if (ret != (int)WiFiError.None)
139 Log.Error(Globals.LogTag, "Failed to get scan state, Error - " + (WiFiError)ret);
140 return WiFiScanState.NotScanning;
142 return (WiFiScanState)state;
146 internal static WiFiManagerImpl Instance
150 return _instance.Value;
154 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
156 Log.Info(Globals.LogTag, "In threadlocal delegate");
157 return new HandleHolder();
160 private WiFiManagerImpl()
162 Log.Info(Globals.LogTag, "WiFiManagerImpl constructor");
165 internal SafeWiFiManagerHandle GetSafeHandle()
167 return s_threadName.Value.GetSafeHandle();
170 internal SafeWiFiManagerHandle Initialize()
172 SafeWiFiManagerHandle handle;
173 int tid = Thread.CurrentThread.ManagedThreadId;
174 Log.Info(Globals.LogTag, "PInvoke wifi_manager_initialize");
175 int ret = Interop.WiFi.Initialize(tid, out handle);
176 if (ret != (int)WiFiError.None)
178 Log.Error(Globals.LogTag, "Initialize Fail, Error - " + (WiFiError)ret);
179 WiFiErrorFactory.ThrowWiFiException(ret, PrivilegeNetworkGet);
185 internal IEnumerable<WiFiAP> GetFoundAPs()
187 Log.Info(Globals.LogTag, "GetFoundAPs");
188 List<WiFiAP> apList = new List<WiFiAP>();
189 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
191 if (apHandle != IntPtr.Zero)
194 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
195 WiFiAP apItem = new WiFiAP(clonedHandle);
202 int ret = Interop.WiFi.GetForeachFoundAPs(GetSafeHandle(), callback, IntPtr.Zero);
203 CheckReturnValue(ret, "GetForeachFoundAPs", PrivilegeNetworkGet);
208 internal IEnumerable<WiFiAP> GetFoundSpecificAPs()
210 Log.Info(Globals.LogTag, "GetFoundSpecificAPs");
211 List<WiFiAP> apList = new List<WiFiAP>();
212 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
214 if (apHandle != IntPtr.Zero)
217 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
218 WiFiAP apItem = new WiFiAP(clonedHandle);
226 int ret = Interop.WiFi.GetForeachFoundSpecificAPs(GetSafeHandle(), callback, IntPtr.Zero);
227 CheckReturnValue(ret, "GetForeachFoundSpecificAPs", PrivilegeNetworkGet);
232 internal IEnumerable<WiFiAP> GetFoundBssids()
234 Log.Info(Globals.LogTag, "GetFoundBssids");
235 List<WiFiAP> apList = new List<WiFiAP>();
236 Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
238 if (apHandle != IntPtr.Zero)
241 Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
242 WiFiAP apItem = new WiFiAP(clonedHandle);
249 int ret = Interop.WiFi.GetForeachFoundBssids(GetSafeHandle(), callback, IntPtr.Zero);
250 CheckReturnValue(ret, "GetForeachFoundBssids", PrivilegeNetworkGet);
255 internal IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
257 Log.Debug(Globals.LogTag, "GetWiFiConfigurations");
258 List<WiFiConfiguration> configList = new List<WiFiConfiguration>();
259 Interop.WiFi.HandleCallback callback = (IntPtr configHandle, IntPtr userData) =>
261 if (configHandle != IntPtr.Zero)
264 Interop.WiFi.Config.Clone(configHandle, out clonedConfig);
265 WiFiConfiguration configItem = new WiFiConfiguration(clonedConfig);
266 configList.Add(configItem);
272 int ret = Interop.WiFi.Config.GetForeachConfiguration(GetSafeHandle(), callback, IntPtr.Zero);
273 CheckReturnValue(ret, "GetForeachConfiguration", PrivilegeNetworkProfile);
277 internal void SaveWiFiNetworkConfiguration(WiFiConfiguration config)
279 Log.Debug(Globals.LogTag, "SaveWiFiNetworkConfiguration");
282 throw new ArgumentNullException("WiFi configuration is null");
285 IntPtr configHandle = config.GetHandle();
286 int ret = Interop.WiFi.Config.SaveConfiguration(GetSafeHandle(), configHandle);
287 CheckReturnValue(ret, "SaveConfiguration", PrivilegeNetworkProfile);
290 internal WiFiAP GetConnectedAP()
292 Log.Info(Globals.LogTag, "GetConnectedAP");
294 int ret = Interop.WiFi.GetConnectedAP(GetSafeHandle(), out apHandle);
295 if (ret == (int)WiFiError.NoConnectionError)
297 Log.Error(Globals.LogTag, "No connection " + (WiFiError)ret);
300 CheckReturnValue(ret, "GetConnectedAP", PrivilegeNetworkGet);
301 WiFiAP ap = new WiFiAP(apHandle);
305 internal Task ActivateAsync()
307 Log.Info(Globals.LogTag, "ActivateAsync");
308 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
312 id = (IntPtr)_requestId++;
313 _callback_map[id] = (error, key) =>
315 Log.Info(Globals.LogTag, "ActivateAsync done");
316 if (error != (int)WiFiError.None)
318 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
319 task.SetException(WiFiErrorFactory.GetException(error, "Error occurs during WiFi activating"));
323 task.SetResult(true);
327 _callback_map.Remove(key);
334 Log.Info(Globals.LogTag, "Interop.WiFi.ActivateAsync");
337 int ret = (int)WiFiError.None;
340 ret = Interop.WiFi.Activate(GetSafeHandle(), _callback_map[id], id);
342 CheckReturnValue(ret, "Activate", "");
346 Log.Error(Globals.LogTag, "Exception on ActivateAsync\n" + e);
347 task.SetException(e);
354 internal Task ActivateWithWiFiPickerTestedAsync()
356 Log.Info(Globals.LogTag, "ActivateWithWiFiPickerTestedAsync");
357 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
361 id = (IntPtr)_requestId++;
362 _callback_map[id] = (error, key) =>
364 Log.Info(Globals.LogTag, "ActivateWithWiFiPickerTestedAsync done");
365 if (error != (int)WiFiError.None)
367 Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
368 task.SetException(WiFiErrorFactory.GetException(error, "Error occurs during WiFi activating"));
372 task.SetResult(true);
376 _callback_map.Remove(key);
383 Log.Info(Globals.LogTag, "Interop.WiFi.ActivateWithWiFiPickerTestedAsync");
386 int ret = (int)WiFiError.None;
389 ret = Interop.WiFi.ActivateWithWiFiPickerTested(GetSafeHandle(), _callback_map[id], id);
391 CheckReturnValue(ret, "ActivateWithWiFiPickerTested", "");
395 Log.Error(Globals.LogTag, "Exception on ActivateWithWiFiPickerTestedAsync\n" + e);
396 task.SetException(e);
403 internal Task DeactivateAsync()
405 Log.Info(Globals.LogTag, "DeactivateAsync");
406 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
410 id = (IntPtr)_requestId++;
411 _callback_map[id] = (error, key) =>
413 Log.Info(Globals.LogTag, "DeactivateAsync done");
414 if (error != (int)WiFiError.None)
416 Log.Error(Globals.LogTag, "Error occurs during WiFi deactivating, " + (WiFiError)error);
417 task.SetException(WiFiErrorFactory.GetException(error, "Error occurs during WiFi deactivating"));
421 task.SetResult(true);
425 _callback_map.Remove(key);
432 Log.Info(Globals.LogTag, "Interop.WiFi.Deactivate");
435 int ret = Interop.WiFi.Deactivate(GetSafeHandle(), _callback_map[id], id);
436 CheckReturnValue(ret, "Deactivate", "");
440 Log.Error(Globals.LogTag, "Exception on Deactivate\n" + e);
441 task.SetException(e);
448 internal Task ScanAsync()
450 Log.Info(Globals.LogTag, "ScanAsync");
451 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
455 id = (IntPtr)_requestId++;
456 _callback_map[id] = (error, key) =>
458 Log.Info(Globals.LogTag, "ScanAsync done");
459 if (error != (int)WiFiError.None)
461 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
462 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
466 task.SetResult(true);
470 _callback_map.Remove(key);
477 Log.Info(Globals.LogTag, "Interop.WiFi.Scan");
480 int ret = Interop.WiFi.Scan(GetSafeHandle(), _callback_map[id], id);
481 CheckReturnValue(ret, "Scan", "");
485 Log.Error(Globals.LogTag, "Exception on Scan\n" + e);
486 task.SetException(e);
493 internal Task ScanSpecificAPAsync(string essid)
495 Log.Info(Globals.LogTag, "ScanSpecificAPAsync " + essid);
496 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
500 id = (IntPtr)_requestId++;
501 _callback_map[id] = (error, key) =>
503 Log.Info(Globals.LogTag, "ScanSpecificAPAsync Done " + essid);
504 if (error != (int)WiFiError.None)
506 Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
507 task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
511 task.SetResult(true);
515 _callback_map.Remove(key);
522 Log.Info(Globals.LogTag, "Interop.WiFi.ScanSpecificAPAsync");
525 int ret = Interop.WiFi.ScanSpecificAP(GetSafeHandle(), essid, _callback_map[id], id);
526 CheckReturnValue(ret, "ScanSpecificAP", "");
530 Log.Error(Globals.LogTag, "Exception on ScanSpecificAPAsync\n" + e);
531 task.SetException(e);
538 internal Task BssidScanAsync()
540 Log.Info(Globals.LogTag, "BssidScanAsync");
541 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
545 id = (IntPtr)_requestId++;
546 _callback_map[id] = (error, key) =>
548 Log.Info(Globals.LogTag, "BssidScanAsync done");
549 if (error != (int)WiFiError.None)
551 Log.Error(Globals.LogTag, "Error occurs during bssid scanning, " + (WiFiError)error);
552 task.SetException(new InvalidOperationException("Error occurs during bssid scanning, " + (WiFiError)error));
556 task.SetResult(true);
560 _callback_map.Remove(key);
567 Log.Info(Globals.LogTag, "Interop.WiFi.BssidScan");
570 int ret = Interop.WiFi.BssidScan(GetSafeHandle(), _callback_map[id], id);
571 CheckReturnValue(ret, "BssidScan", "");
575 Log.Error(Globals.LogTag, "Exception on BssidScan\n" + e);
576 task.SetException(e);
583 private void CheckReturnValue(int ret, string method, string privilege)
585 if (ret != (int)WiFiError.None)
587 Log.Error(Globals.LogTag, method + " Fail, Error - " + (WiFiError)ret);
588 if (ret == (int)WiFiError.InvalidParameterError)
590 throw new InvalidOperationException("Invalid handle");
592 WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), privilege);