From 1c9212057ef86428592a7e93aefc31474252bc3d Mon Sep 17 00:00:00 2001 From: chleun-moon <32117100+chleun-moon@users.noreply.github.com> Date: Thu, 21 Jun 2018 13:34:30 +0900 Subject: [PATCH] [WiFi] TCSACR-149 (#292) * [WiFi] New APIs [method] public static void CancelWps() static public IEnumerable GetFoundBssidAPs() static public Task BssidScanAsync() public IEnumerable GetBssids() [property] public byte[] RawSsid public string CountryCode * Rename GetFoundBssidAPs to GetFoundBssids --- src/Tizen.Network.WiFi/Interop/Interop.WiFi.cs | 15 ++++ .../Tizen.Network.WiFi/WiFiAP.cs | 25 +++++- .../Tizen.Network.WiFi/WiFiManager.cs | 35 +++++++++ .../Tizen.Network.WiFi/WiFiManagerImpl.cs | 76 +++++++++++++++++++ .../Tizen.Network.WiFi/WiFiNetwork.cs | 88 ++++++++++++++++++++++ 5 files changed, 238 insertions(+), 1 deletion(-) diff --git a/src/Tizen.Network.WiFi/Interop/Interop.WiFi.cs b/src/Tizen.Network.WiFi/Interop/Interop.WiFi.cs index a70c6b1..a019901 100755 --- a/src/Tizen.Network.WiFi/Interop/Interop.WiFi.cs +++ b/src/Tizen.Network.WiFi/Interop/Interop.WiFi.cs @@ -60,12 +60,16 @@ internal static partial class Interop internal static extern int Scan(SafeWiFiManagerHandle wifi, VoidCallback callback, IntPtr userData); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_scan_specific_ap")] internal static extern int ScanSpecificAP(SafeWiFiManagerHandle wifi, string essid, VoidCallback callback, IntPtr userData); + [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_bssid_scan")] + internal static extern int BssidScan(SafeWiFiManagerHandle wifi, VoidCallback callback, IntPtr userData); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_get_connected_ap")] internal static extern int GetConnectedAP(SafeWiFiManagerHandle wifi, out IntPtr ap); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_foreach_found_ap")] internal static extern int GetForeachFoundAPs(SafeWiFiManagerHandle wifi, HandleCallback callback, IntPtr userData); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_foreach_found_specific_ap")] internal static extern int GetForeachFoundSpecificAPs(SafeWiFiManagerHandle wifi, HandleCallback callback, IntPtr userData); + [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_foreach_found_bssid_ap")] + internal static extern int GetForeachFoundBssids(SafeWiFiManagerHandle wifi, HandleCallback callback, IntPtr userData); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_connect")] internal static extern int Connect(SafeWiFiManagerHandle wifi, IntPtr ap, VoidCallback callback, IntPtr userData); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_disconnect")] @@ -78,6 +82,8 @@ internal static partial class Interop internal static extern int ConnectByWpsPbcWithoutSsid(SafeWiFiManagerHandle wifi, VoidCallback callback, IntPtr userData); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_connect_by_wps_pin_without_ssid")] internal static extern int ConnectByWpsPinWithoutSsid(SafeWiFiManagerHandle wifi, string pin, VoidCallback callback, IntPtr userData); + [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_cancel_wps")] + internal static extern int CancelWps(SafeWiFiManagerHandle wifi); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_forget_ap")] internal static extern int RemoveAP(SafeWiFiManagerHandle wifi, IntPtr ap); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_update_ap")] @@ -105,6 +111,9 @@ internal static partial class Interop internal static class AP { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool FoundBssidCallback(string bssid, int rssi, int freq, IntPtr userData); + [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_ap_create")] internal static extern int Create(SafeWiFiManagerHandle wifi, string essid, out IntPtr ap); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_ap_hidden_create")] @@ -119,8 +128,12 @@ internal static partial class Interop ////Network Information [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_ap_get_essid")] internal static extern int GetEssid(SafeWiFiAPHandle ap, out IntPtr essid); + [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_ap_get_raw_essid")] + internal static extern int GetRawSsid(SafeWiFiAPHandle ap, out IntPtr raw_ssid, out int length); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_ap_get_bssid")] internal static extern int GetBssid(SafeWiFiAPHandle ap, out IntPtr bssid); + [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_foreach_found_bssid")] + internal static extern int GetBssids(SafeWiFiAPHandle ap, FoundBssidCallback callback, IntPtr userData); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_ap_get_rssi")] internal static extern int GetRssi(SafeWiFiAPHandle ap, out int rssi); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_ap_get_rssi_level")] @@ -143,6 +156,8 @@ internal static partial class Interop internal static extern int GetIPAddress(SafeWiFiAPHandle ap, int addressFamily, out IntPtr ipAddress); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_ap_foreach_ipv6_address")] internal static extern int GetAllIPv6Addresses(SafeWiFiAPHandle ap, HandleCallback callback, IntPtr userData); + [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_ap_get_countrycode")] + internal static extern int GetCountryCode(SafeWiFiAPHandle ap, out IntPtr code); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_ap_set_ip_address")] internal static extern int SetIPAddress(SafeWiFiAPHandle ap, int addressFamily, string ipAddress); [DllImport(Libraries.WiFi, EntryPoint = "wifi_manager_ap_get_subnet_mask")] diff --git a/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiAP.cs b/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiAP.cs index f458c14..6715646 100644 --- a/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiAP.cs +++ b/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiAP.cs @@ -156,7 +156,6 @@ namespace Tizen.Network.WiFi { ret = Interop.WiFi.AP.CreateHiddenAP(WiFiManagerImpl.Instance.GetSafeHandle(), id, out _apHandle); } - else { ret = Interop.WiFi.AP.Create(WiFiManagerImpl.Instance.GetSafeHandle(), id, out _apHandle); @@ -459,6 +458,30 @@ namespace Tizen.Network.WiFi } /// + /// Stops ongoing WPS provisioning + /// + /// 5 + /// http://tizen.org/feature/network.wifi + /// http://tizen.org/privilege/network.set + /// http://tizen.org/privilege/network.get + /// Thrown when the Wi-Fi is not supported. + /// Thrown when permission is denied. + /// Thrown when the object instance is disposed or released. + /// Thrown when the system is out of memory. + /// Thrown when the method failed due to an invalid operation. + public static void CancelWps() + { + Log.Debug(Globals.LogTag, "CancelWps"); + int ret = Interop.WiFi.CancelWps(WiFiManagerImpl.Instance.GetSafeHandle()); + if (ret != (int)WiFiError.None) + { + Log.Error(Globals.LogTag, "Failed to cancel Wps, Error - " + (WiFiError)ret); + WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle()); + } + } + + + /// /// Disconnects the access point asynchronously. /// /// 3 diff --git a/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManager.cs b/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManager.cs index 5cbdad5..5d3ccf2 100755 --- a/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManager.cs +++ b/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManager.cs @@ -252,6 +252,22 @@ namespace Tizen.Network.WiFi } /// + /// Gets the result of the BssidScanAsync() API. + /// + /// 5 + /// A list of the WiFiAP objects. + /// http://tizen.org/feature/network.wifi + /// http://tizen.org/privilege/network.get + /// Thrown when the Wi-Fi is not supported. + /// Thrown when the permission is denied. + /// Thrown when the method failed due to an invalid parameter. + /// Thrown when the method failed due to an invalid operation. + static public IEnumerable GetFoundBssids() + { + return WiFiManagerImpl.Instance.GetFoundBssids(); + } + + /// /// Gets the list of Wi-Fi configurations. /// /// 3 @@ -387,5 +403,24 @@ namespace Tizen.Network.WiFi { return WiFiManagerImpl.Instance.ScanSpecificAPAsync(essid); } + + /// + /// Starts BSSID scan asynchronously. + /// + /// + /// This method must be called from MainThread. + /// + /// 5 + /// A task indicating whether the BssidScanAsync method is done or not. + /// http://tizen.org/feature/network.wifi + /// http://tizen.org/privilege/network.set + /// http://tizen.org/privilege/network.get + /// Thrown when the Wi-Fi is not supported. + /// Thrown when the permission is denied. + /// Thrown when the method failed due to an invalid operation. + static public Task BssidScanAsync() + { + return WiFiManagerImpl.Instance.BssidScanAsync(); + } } } diff --git a/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs b/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs index ee092aa..b4b23ac 100644 --- a/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs +++ b/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs @@ -218,6 +218,33 @@ namespace Tizen.Network.WiFi return apList; } + internal IEnumerable GetFoundBssids() + { + Log.Info(Globals.LogTag, "GetFoundBssids"); + List apList = new List(); + Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) => + { + if (apHandle != IntPtr.Zero) + { + IntPtr clonedHandle; + Interop.WiFi.AP.Clone(out clonedHandle, apHandle); + WiFiAP apItem = new WiFiAP(clonedHandle); + apList.Add(apItem); + return true; + } + return false; + }; + + int ret = Interop.WiFi.GetForeachFoundBssids(GetSafeHandle(), callback, IntPtr.Zero); + if (ret != (int)WiFiError.None) + { + Log.Error(Globals.LogTag, "Failed to get bssid APs, Error - " + (WiFiError)ret); + WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.get"); + } + + return apList; + } + internal IEnumerable GetWiFiConfigurations() { Log.Debug(Globals.LogTag, "GetWiFiConfigurations"); @@ -528,5 +555,54 @@ namespace Tizen.Network.WiFi return task.Task; } + + internal Task BssidScanAsync() + { + Log.Info(Globals.LogTag, "BssidScanAsync"); + TaskCompletionSource task = new TaskCompletionSource(); + IntPtr id; + lock (_callback_map) + { + id = (IntPtr)_requestId++; + _callback_map[id] = (error, key) => + { + Log.Info(Globals.LogTag, "BssidScanAsync done"); + if (error != (int)WiFiError.None) + { + Log.Error(Globals.LogTag, "Error occurs during bssid scanning, " + (WiFiError)error); + task.SetException(new InvalidOperationException("Error occurs during bssid scanning, " + (WiFiError)error)); + } + else + { + task.SetResult(true); + } + lock (_callback_map) + { + _callback_map.Remove(key); + } + }; + } + + context.Post((x) => + { + Log.Info(Globals.LogTag, "Interop.WiFi.BssidScan"); + try + { + int ret = Interop.WiFi.BssidScan(GetSafeHandle(), _callback_map[id], id); + if (ret != (int)WiFiError.None) + { + Log.Error(Globals.LogTag, "Failed to scan Bssid AP, Error - " + (WiFiError)ret); + WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle()); + } + } + catch (Exception e) + { + Log.Error(Globals.LogTag, "Exception on BssidScan\n" + e.ToString()); + task.SetException(e); + } + }, null); + + return task.Task; + } } } diff --git a/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiNetwork.cs b/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiNetwork.cs index e10206c..b9b3350 100755 --- a/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiNetwork.cs +++ b/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiNetwork.cs @@ -54,6 +54,7 @@ namespace Tizen.Network.WiFi else { _essid = Marshal.PtrToStringAnsi(strPtr); + Interop.Libc.Free(strPtr); } } return _essid; @@ -61,6 +62,34 @@ namespace Tizen.Network.WiFi } /// + /// The raw Service Set Identifier (SSID). + /// + /// 5 + /// Raw SSID of the Wi-Fi. + public byte[] RawSsid + { + get + { + IntPtr ptr; + byte[] rawSsid; + int length; + int ret = Interop.WiFi.AP.GetRawSsid(_apHandle, out ptr, out length); + if (ret != (int)WiFiError.None || length == 0) + { + Log.Error(Globals.LogTag, "Failed to get raw essid, Error - " + (WiFiError)ret); + rawSsid = null; + } + else + { + rawSsid = new byte[length]; + Marshal.Copy(ptr, rawSsid, 0, length); + Interop.Libc.Free(ptr); + } + return rawSsid; + } + } + + /// /// The Basic Service Set Identifier (BSSID). /// /// 3 @@ -316,6 +345,32 @@ namespace Tizen.Network.WiFi } /// + /// The raw country code + /// + /// 5 + /// Represents the raw country code of the Wi-Fi. + public string CountryCode + { + get + { + string code; + IntPtr strPtr; + int ret = Interop.WiFi.AP.GetCountryCode(_apHandle, out strPtr); + if (ret != (int)WiFiError.None) + { + Log.Error(Globals.LogTag, "Failed to get country code, Error - " + (WiFiError)ret); + code = ""; + } + else + { + code = Marshal.PtrToStringAnsi(strPtr); + Interop.Libc.Free(strPtr); + } + return code; + } + } + + /// /// Gets all IPv6 addresses of the access point. /// /// 3 @@ -352,6 +407,39 @@ namespace Tizen.Network.WiFi return ipList; } + /// + /// Gets the Bssid list + /// + /// 5 + /// A list of BSSIDs of access points with the same SSID as that of this access point. + /// http://tizen.org/feature/network.wifi + /// Thrown when the Wi-Fi is not supported. + /// Thrown when the method failed due to an invalid parameter. + /// Thrown when the method failed due to an invalid operation. + public IEnumerable GetBssids() + { + Log.Debug(Globals.LogTag, "GetBssids"); + List bssidList = new List(); + Interop.WiFi.AP.FoundBssidCallback callback = (string bssid, int rssi, int freq, IntPtr userData) => + { + if (string.IsNullOrEmpty(bssid)) + { + bssidList.Add(bssid); + return true; + } + return false; + }; + + int ret = Interop.WiFi.AP.GetBssids(_apHandle, callback, IntPtr.Zero); + if (ret != (int)WiFiError.None) + { + Log.Error(Globals.LogTag, "Failed to get BSSIDs, Error - " + (WiFiError)ret); + WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle()); + } + + return bssidList; + } + internal WiFiNetwork(Interop.WiFi.SafeWiFiAPHandle apHandle) { _apHandle = apHandle; -- 2.7.4