From 76d76db54b34292fe226c3483f3549897770aea4 Mon Sep 17 00:00:00 2001 From: "adhavan.m" Date: Wed, 1 Feb 2017 18:31:07 +0530 Subject: [PATCH] [WiFi] Fix for wifi initialization problem to avoid block TCT issues in emulator Signed-off-by: adhavan.m Change-Id: I09e8c54ff9f166afcc00ab8524dc660b814ff1c4 --- .../Tizen.Network.WiFi/WiFiManager.cs | 170 +++++++++++++++++---- .../Tizen.Network.WiFi/WiFiManagerImpl.cs | 25 ++- 2 files changed, 159 insertions(+), 36 deletions(-) mode change 100644 => 100755 Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManager.cs mode change 100644 => 100755 Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs diff --git a/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManager.cs b/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManager.cs old mode 100644 new mode 100755 index dd8b65a..d19ba8a --- a/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManager.cs +++ b/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManager.cs @@ -33,7 +33,11 @@ namespace Tizen.Network.WiFi { get { - return WiFiManagerImpl.Instance.MacAddress; + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.MacAddress; + } + return null; } } /// @@ -43,7 +47,11 @@ namespace Tizen.Network.WiFi { get { - return WiFiManagerImpl.Instance.InterfaceName; + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.InterfaceName; + } + return null; } } /// @@ -53,7 +61,11 @@ namespace Tizen.Network.WiFi { get { - return WiFiManagerImpl.Instance.ConnectionState; + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.ConnectionState; + } + return default(WiFiConnectionState); } } /// @@ -63,7 +75,11 @@ namespace Tizen.Network.WiFi { get { - return WiFiManagerImpl.Instance.IsActivated; + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.IsActivated; + } + return false; } } @@ -74,11 +90,17 @@ namespace Tizen.Network.WiFi { add { - WiFiManagerImpl.Instance.DeviceStateChanged += value; + if (Globals.IsInitialize) + { + WiFiManagerImpl.Instance.DeviceStateChanged += value; + } } remove { - WiFiManagerImpl.Instance.DeviceStateChanged -= value; + if (Globals.IsInitialize) + { + WiFiManagerImpl.Instance.DeviceStateChanged -= value; + } } } /// @@ -88,11 +110,17 @@ namespace Tizen.Network.WiFi { add { - WiFiManagerImpl.Instance.ConnectionStateChanged += value; + if (Globals.IsInitialize) + { + WiFiManagerImpl.Instance.ConnectionStateChanged += value; + } } remove { - WiFiManagerImpl.Instance.ConnectionStateChanged -= value; + if (Globals.IsInitialize) + { + WiFiManagerImpl.Instance.ConnectionStateChanged -= value; + } } } /// @@ -102,11 +130,17 @@ namespace Tizen.Network.WiFi { add { - WiFiManagerImpl.Instance.RssiLevelChanged += value; + if (Globals.IsInitialize) + { + WiFiManagerImpl.Instance.RssiLevelChanged += value; + } } remove { - WiFiManagerImpl.Instance.RssiLevelChanged -= value; + if (Globals.IsInitialize) + { + WiFiManagerImpl.Instance.RssiLevelChanged -= value; + } } } /// @@ -117,11 +151,17 @@ namespace Tizen.Network.WiFi { add { - WiFiManagerImpl.Instance.BackgroundScanFinished += value; + if (Globals.IsInitialize) + { + WiFiManagerImpl.Instance.BackgroundScanFinished += value; + } } remove { - WiFiManagerImpl.Instance.BackgroundScanFinished -= value; + if (Globals.IsInitialize) + { + WiFiManagerImpl.Instance.BackgroundScanFinished -= value; + } } } /// @@ -130,7 +170,11 @@ namespace Tizen.Network.WiFi /// A task contains the lisf for WiFiApInformation objects. static public IEnumerable GetFoundAps() { - return WiFiManagerImpl.Instance.GetFoundAps(); + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.GetFoundAps(); + } + throw new InvalidOperationException("Not initialized"); } /// /// Gets the result of specific ap scan asynchronously. @@ -138,7 +182,11 @@ namespace Tizen.Network.WiFi /// A task contains the WiFiApInformation object. static public IEnumerable GetFoundSpecificAps() { - return WiFiManagerImpl.Instance.GetFoundSpecificAps(); + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.GetFoundSpecificAps(); + } + throw new InvalidOperationException("Not initialized"); } /// /// Gets the list of wifi configuration. @@ -146,7 +194,11 @@ namespace Tizen.Network.WiFi /// A task contains the lisf for WiFiConfiguration objects. static public IEnumerable GetWiFiConfigurations() { - return WiFiManagerImpl.Instance.GetWiFiConfigurations(); + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.GetWiFiConfigurations(); + } + throw new InvalidOperationException("Not initialized"); } /// /// Saves Wi-Fi configuration of access point. @@ -154,7 +206,14 @@ namespace Tizen.Network.WiFi /// The configuration to be stored static public void SaveWiFiNetworkConfiguration(WiFiConfiguration configuration) { - WiFiManagerImpl.Instance.SaveWiFiNetworkConfiguration(configuration); + if (Globals.IsInitialize) + { + WiFiManagerImpl.Instance.SaveWiFiNetworkConfiguration(configuration); + } + else + { + throw new InvalidOperationException("Not initialized"); + } } /// /// Gets the handle of the connected access point. @@ -162,8 +221,11 @@ namespace Tizen.Network.WiFi /// The connected wifi access point(AP) information. static public WiFiAp GetConnectedAp() { - return WiFiManagerImpl.Instance.GetConnectedAp(); - + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.GetConnectedAp(); + } + throw new InvalidOperationException("Not initialized"); } /// /// Deletes the information of stored access point and disconnects it when it connected.
@@ -172,7 +234,14 @@ namespace Tizen.Network.WiFi /// The access point to be removed static public void RemoveAP(WiFiAp ap) { - WiFiManagerImpl.Instance.RemoveAp(ap); + if (Globals.IsInitialize) + { + WiFiManagerImpl.Instance.RemoveAp(ap); + } + else + { + throw new InvalidOperationException("Not initialized"); + } } /// /// Activates Wi-Fi asynchronously. @@ -180,8 +249,11 @@ namespace Tizen.Network.WiFi /// A task indicates whether the Activate method is done or not. static public Task ActivateAsync() { - - return WiFiManagerImpl.Instance.ActivateAsync(); + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.ActivateAsync(); + } + throw new InvalidOperationException("Not initialized"); } /// /// Activates Wi-Fi asynchronously and displays Wi-Fi picker (popup) when Wi-Fi is not automatically connected. @@ -189,7 +261,11 @@ namespace Tizen.Network.WiFi /// A task indicates whether the ActivateWithPickerTeated method is done or not. static public Task ActivateWithPickerTeatedAsync() { - return WiFiManagerImpl.Instance.ActivateWithWiFiPickerTestedAsync(); + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.ActivateWithWiFiPickerTestedAsync(); + } + throw new InvalidOperationException("Not initialized"); } /// /// Deactivates Wi-Fi asynchronously. @@ -197,7 +273,11 @@ namespace Tizen.Network.WiFi /// A task indicates whether the Deactivate method is done or not. static public Task DeactivateAsync() { - return WiFiManagerImpl.Instance.DeactivateAsync(); + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.DeactivateAsync(); + } + throw new InvalidOperationException("Not initialized"); } /// /// Starts scan asynchronously. @@ -205,8 +285,12 @@ namespace Tizen.Network.WiFi /// A task indicates whether the Scan method is done or not. static public Task ScanAsync() { - Log.Debug(Globals.LogTag, "ScanAsync"); - return WiFiManagerImpl.Instance.ScanAsync(); + if (Globals.IsInitialize) + { + Log.Debug(Globals.LogTag, "ScanAsync"); + return WiFiManagerImpl.Instance.ScanAsync(); + } + throw new InvalidOperationException("Not initialized"); } /// /// Starts specific ap scan, asynchronously. @@ -215,7 +299,11 @@ namespace Tizen.Network.WiFi /// The essid of hidden ap static public Task ScanSpecificApAsync(string essid) { - return WiFiManagerImpl.Instance.ScanSpecificApAsync(essid); + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.ScanSpecificApAsync(essid); + } + throw new InvalidOperationException("Not initialized"); } /// /// Connects the access point asynchronously. @@ -224,8 +312,12 @@ namespace Tizen.Network.WiFi /// A task indicates whether the Connect method is done or not. static public Task ConnectAsync(WiFiAp ap) { - Log.Debug(Globals.LogTag, "ConnectAsync"); - return WiFiManagerImpl.Instance.ConnectAsync(ap); + if (Globals.IsInitialize) + { + Log.Debug(Globals.LogTag, "ConnectAsync"); + return WiFiManagerImpl.Instance.ConnectAsync(ap); + } + throw new InvalidOperationException("Not initialized"); } /// /// Connects the access point with WPS PBC asynchronously. @@ -234,8 +326,12 @@ namespace Tizen.Network.WiFi /// The access point(AP) static public Task ConnectByWpsPbcAsync(WiFiAp ap) { - Log.Debug(Globals.LogTag, "ConnectByWpsPbcAsync"); - return WiFiManagerImpl.Instance.ConnectByWpsPbcAsync(ap); + if (Globals.IsInitialize) + { + Log.Debug(Globals.LogTag, "ConnectByWpsPbcAsync"); + return WiFiManagerImpl.Instance.ConnectByWpsPbcAsync(ap); + } + throw new InvalidOperationException("Not initialized"); } /// /// Connects the access point with WPS PIN asynchronously. @@ -245,8 +341,12 @@ namespace Tizen.Network.WiFi /// The WPS PIN is a non-NULL string with length greater than 0 and less than or equal to 8. static public Task ConnectByWpsPinAsync(WiFiAp ap, string pin) { - Log.Debug(Globals.LogTag, "ConnectByWpsPinAsync"); - return WiFiManagerImpl.Instance.ConnectByWpsPinAsync(ap, pin); + if (Globals.IsInitialize) + { + Log.Debug(Globals.LogTag, "ConnectByWpsPinAsync"); + return WiFiManagerImpl.Instance.ConnectByWpsPinAsync(ap, pin); + } + throw new InvalidOperationException("Not initialized"); } /// /// Disconnects the access point asynchronously. @@ -254,7 +354,11 @@ namespace Tizen.Network.WiFi /// A task indicates whether the Disconnect method is done or not. static public Task DisconnectAsync(WiFiAp ap) { - return WiFiManagerImpl.Instance.DisconnectAsync(ap); + if (Globals.IsInitialize) + { + return WiFiManagerImpl.Instance.DisconnectAsync(ap); + } + throw new InvalidOperationException("Not initialized"); } } } diff --git a/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs b/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs old mode 100644 new mode 100755 index 7ea0a01..f64c917 --- a/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs +++ b/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs @@ -23,11 +23,24 @@ namespace Tizen.Network.WiFi static internal class Globals { internal const string LogTag = "Tizen.Network.WiFi"; + internal static bool s_isInitialize = false; + + internal static bool IsInitialize + { + get + { + if (!Globals.s_isInitialize) + { + WiFiManagerImpl.Instance.Initialize(); + } + return Globals.s_isInitialize; + } + } } internal partial class WiFiManagerImpl : IDisposable { - private static readonly WiFiManagerImpl _instance = new WiFiManagerImpl(); + private static WiFiManagerImpl _instance; private Dictionary _callback_map = new Dictionary(); private int _requestId = 0; private string _macAddress; @@ -100,13 +113,17 @@ namespace Tizen.Network.WiFi { get { + if (_instance == null) + { + _instance = new WiFiManagerImpl(); + } + return _instance; } } private WiFiManagerImpl() { - initialize(); } ~WiFiManagerImpl() @@ -135,7 +152,7 @@ namespace Tizen.Network.WiFi disposed = true; } - private void initialize() + internal void Initialize() { int ret = Interop.WiFi.Initialize(); if (ret != (int)WiFiError.None) @@ -143,6 +160,7 @@ namespace Tizen.Network.WiFi Log.Error(Globals.LogTag, "Failed to initialize wifi, Error - " + (WiFiError)ret); WiFiErrorFactory.ThrowWiFiException(ret); } + Globals.s_isInitialize = true; string address; ret = Interop.WiFi.GetMacAddress(out address); if (ret != (int)WiFiError.None) @@ -161,6 +179,7 @@ namespace Tizen.Network.WiFi Log.Error(Globals.LogTag, "Failed to deinitialize wifi, Error - " + (WiFiError)ret); WiFiErrorFactory.ThrowWiFiException(ret); } + Globals.s_isInitialize = false; } internal IEnumerable GetFoundAps() -- 2.7.4