[Connection][WiFi] Remove duplicate codes (#385)
authorchleun-moon <32117100+chleun-moon@users.noreply.github.com>
Fri, 10 Aug 2018 06:12:40 +0000 (15:12 +0900)
committertaesubkim <35015408+taesubkim@users.noreply.github.com>
Fri, 10 Aug 2018 06:12:40 +0000 (15:12 +0900)
* [Connection] Improve code quality

* [WiFi] Fix typo and remove duplicate codes

* [Connection][WiFi] Replace codes to get IPAddress with ParseIPAddress

* [WiFi] Fix DhcpServerAddress

src/Tizen.Network.Connection/Interop/Interop.Libraries.cs
src/Tizen.Network.Connection/Tizen.Network.Connection/IAddressInformation.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiAP.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiAddressInformation.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiErrorFactory.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiNetworkChange.cs

index 03ee0cd..e4c5317 100644 (file)
@@ -19,12 +19,6 @@ internal static partial class Interop
     internal static partial class Libraries
     {
         public const string Connection = "libcapi-network-connection.so.1";
-        public const string WiFi = "libcapi-network-wifi.so.1";
-        public const string Bluetooth = "libcapi-network-bluetooth.so.0";
-        public const string Smartcard = "libcapi-network-smartcard.so.0";
-        public const string Nfc = "libcapi-network-nfc.so.0";
-        public const string WiFiDirect = "libwifi-direct.so.1";
-        public const string Glib = "libglib-2.0.so.0";
         public const string Libc = "libc.so.6";
     }
 }
index 4a2a475..176dd38 100755 (executable)
@@ -122,6 +122,8 @@ namespace Tizen.Network.Connection
     {
         private IntPtr _profileHandle;
         private AddressFamily _family;
+        private const String DefaultIPv4 = "0.0.0.0";
+        private const String DefaultIPv6 = "::";
 
         internal ConnectionAddressInformation(IntPtr handle, AddressFamily family)
         {
@@ -135,15 +137,7 @@ namespace Tizen.Network.Connection
             {
                 IntPtr Value;
                 int ret = Interop.ConnectionProfile.GetDnsAddress(_profileHandle, 1, (int)_family, out Value);
-                if ((ConnectionError)ret != ConnectionError.None)
-                {
-                    Log.Error(Globals.LogTag, "It failed to get dns1 address, " + (ConnectionError)ret);
-                }
-                string result = Marshal.PtrToStringAnsi(Value);
-                Interop.Libc.Free(Value);
-                if (result == null || result.Length == 0)
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                return System.Net.IPAddress.Parse(result);
+                return ParseIPAddress(ret, Value);
             }
 
             set
@@ -164,17 +158,8 @@ namespace Tizen.Network.Connection
             {
                 IntPtr Value;
                 int ret = Interop.ConnectionProfile.GetDnsAddress(_profileHandle, 2, (int)_family, out Value);
-                if ((ConnectionError)ret != ConnectionError.None)
-                {
-                    Log.Error(Globals.LogTag, "It failed to get dns2 address, " + (ConnectionError)ret);
-                }
-                string result = Marshal.PtrToStringAnsi(Value);
-                Interop.Libc.Free(Value);
-                if (result == null || result.Length == 0)
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                return System.Net.IPAddress.Parse(result);
+                return ParseIPAddress(ret, Value);
             }
-
             set
             {
                 int ret = Interop.ConnectionProfile.SetDnsAddress(_profileHandle, 2, (int)_family, value.ToString());
@@ -194,15 +179,7 @@ namespace Tizen.Network.Connection
             {
                 IntPtr Value;
                 int ret = Interop.ConnectionProfile.GetGatewayAddress(_profileHandle, (int)_family, out Value);
-                if ((ConnectionError)ret != ConnectionError.None)
-                {
-                    Log.Error(Globals.LogTag, "It failed to get gateway, " + (ConnectionError)ret);
-                }
-                string result = Marshal.PtrToStringAnsi(Value);
-                Interop.Libc.Free(Value);
-                if (result == null || result.Length == 0)
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                return System.Net.IPAddress.Parse(result);
+                return ParseIPAddress(ret, Value);
             }
 
             set
@@ -225,15 +202,7 @@ namespace Tizen.Network.Connection
             {
                 IntPtr Value;
                 int ret = Interop.ConnectionProfile.GetSubnetMask(_profileHandle, (int)_family, out Value);
-                if ((ConnectionError)ret != ConnectionError.None)
-                {
-                    Log.Error(Globals.LogTag, "It failed to get subnet mask, " + (ConnectionError)ret);
-                }
-                string result = Marshal.PtrToStringAnsi(Value);
-                Interop.Libc.Free(Value);
-                if (result == null || result.Length == 0)
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                return System.Net.IPAddress.Parse(result);
+                return ParseIPAddress(ret, Value);
             }
 
             set
@@ -256,15 +225,7 @@ namespace Tizen.Network.Connection
             {
                 IntPtr Value;
                 int ret = Interop.ConnectionProfile.GetIPAddress(_profileHandle, (int)_family, out Value);
-                if ((ConnectionError)ret != ConnectionError.None)
-                {
-                    Log.Error(Globals.LogTag, "It failed to get ip, " + (ConnectionError)ret);
-                }
-                string result = Marshal.PtrToStringAnsi(Value);
-                Interop.Libc.Free(Value);
-                if (result == null || result.Length == 0)
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                return System.Net.IPAddress.Parse(result);
+                return ParseIPAddress(ret, Value);
             }
 
             set
@@ -364,21 +325,37 @@ namespace Tizen.Network.Connection
             {
                 string dhcpServer;
                 int ret = Interop.ConnectionProfile.GetDhcpServerAddress(_profileHandle, _family, out dhcpServer);
-                if ((ConnectionError)ret != ConnectionError.None)
+                if ((ConnectionError)ret != ConnectionError.None || 
+                        dhcpServer == null || dhcpServer.Length == 0)
                 {
                     Log.Error(Globals.LogTag, "It failed to get the DHCP server address, " + (ConnectionError)ret);
+                    return DefaultIPAddress();
                 }
+                return System.Net.IPAddress.Parse(dhcpServer);
+            }
+        }
 
-                if (dhcpServer == null || dhcpServer.Length == 0)
-                {
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                }
-
-                else
-                {
-                    return System.Net.IPAddress.Parse(dhcpServer);
-                }
+        private System.Net.IPAddress ParseIPAddress(int ret, IntPtr addrPtr)
+        {
+            if (ret != (int)ConnectionError.None)
+            {
+                Log.Error(Globals.LogTag, "Failed to get address, Error - " + (ConnectionError)ret);
+                return DefaultIPAddress();
             }
+
+            string addr = Marshal.PtrToStringAnsi(addrPtr);
+            if (addr == null || addr.Length == 0)
+                return DefaultIPAddress();
+            Interop.Libc.Free(addrPtr);
+            return System.Net.IPAddress.Parse(addr);
+        }
+
+        private System.Net.IPAddress DefaultIPAddress()
+        {
+            if (_family == AddressFamily.IPv4)
+                return System.Net.IPAddress.Parse(DefaultIPv4);
+            else
+                return System.Net.IPAddress.Parse(DefaultIPv6);
         }
     }
 }
index 59eeef4..935d26b 100644 (file)
@@ -148,7 +148,7 @@ namespace Tizen.Network.WiFi
 
         private void createHandle(string id, bool hidden)
         {
-            int ret = -1;
+            int ret;
             if (id == null)
             {
                 throw new ArgumentNullException("Essid is null");
@@ -271,7 +271,7 @@ namespace Tizen.Network.WiFi
                 }
                 catch (Exception e)
                 {
-                    Log.Error(Globals.LogTag, "Exception on ConnectAsync\n" + e.ToString());
+                    Log.Error(Globals.LogTag, "Exception on ConnectAsync\n" + e);
                     task.SetException(e);
                 }
             }, null);
@@ -372,7 +372,7 @@ namespace Tizen.Network.WiFi
                 }
                 catch (Exception e)
                 {
-                    Log.Error(Globals.LogTag, "Exception on ConnectWpsAsync\n" + e.ToString());
+                    Log.Error(Globals.LogTag, "Exception on ConnectWpsAsync\n" + e);
                     wpsTask.SetException(e);
                     Log.Info(Globals.LogTag, "Remove task for ConnectWpsAsync");
                     _wpsTaskMap.Remove(_apHandle);
@@ -470,7 +470,7 @@ namespace Tizen.Network.WiFi
                 }
                 catch (Exception e)
                 {
-                    Log.Error(Globals.LogTag, "Exception on ConnectWpsWithoutSsidAsync\n" + e.ToString());
+                    Log.Error(Globals.LogTag, "Exception on ConnectWpsWithoutSsidAsync\n" + e);
                     wpsWithoutSsidTask.SetException(e);
                     wpsWithoutSsidTask = null;
                     Log.Info(Globals.LogTag, "task is null");
@@ -582,7 +582,7 @@ namespace Tizen.Network.WiFi
                 }
                 catch (Exception e)
                 {
-                    Log.Error(Globals.LogTag, "Exception on Disconnect\n" + e.ToString());
+                    Log.Error(Globals.LogTag, "Exception on Disconnect\n" + e);
                     task.SetException(e);
                 }
             }, null);
@@ -687,7 +687,7 @@ namespace Tizen.Network.WiFi
                 }
                 catch (Exception e)
                 {
-                    Log.Error(Globals.LogTag, "Exception on ForgetAPAsync\n" + e.ToString());
+                    Log.Error(Globals.LogTag, "Exception on ForgetAPAsync\n" + e);
                     task.SetException(e);
                 }
             }, null);
@@ -722,7 +722,7 @@ namespace Tizen.Network.WiFi
                 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
             }
         }
-   }
+  }
 
     /// <summary>
     /// An abstract class which is used to represent the WPS information of the access point.
index 07506f0..2273f44 100755 (executable)
@@ -26,6 +26,9 @@ namespace Tizen.Network.WiFi
         private Interop.WiFi.SafeWiFiAPHandle _handle;
         private AddressFamily _family;
 
+        private const string DefaultIPv4 = "0.0.0.0";
+        private const string DefaultIPv6 = "::";
+
         internal WiFiAddressInformation(Interop.WiFi.SafeWiFiAPHandle handle, AddressFamily family)
         {
             _handle = handle;
@@ -38,16 +41,7 @@ namespace Tizen.Network.WiFi
             {
                 IntPtr addrPtr;
                 int ret = Interop.WiFi.AP.GetDnsAddress(_handle, 1, (int)_family, out addrPtr);
-                if (ret != (int)WiFiError.None)
-                {
-                    Log.Error(Globals.LogTag, "Failed to get first dns address, Error - " + (WiFiError)ret);
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                }
-                string addrStr = Marshal.PtrToStringAnsi(addrPtr);
-                Interop.Libc.Free(addrPtr);
-                if (addrStr == null || addrStr.Length == 0)
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                return System.Net.IPAddress.Parse(addrStr);
+                return ParseIPAddress(ret, addrPtr);
             }
             set
             {
@@ -66,17 +60,8 @@ namespace Tizen.Network.WiFi
             {
                 IntPtr addrPtr;
                 int ret = Interop.WiFi.AP.GetDnsAddress(_handle, 2, (int)_family, out addrPtr);
-                if (ret != (int)WiFiError.None)
-                {
-                    Log.Error(Globals.LogTag, "Failed to get second dns address, Error - " + (WiFiError)ret);
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                }
-                string addrStr = Marshal.PtrToStringAnsi(addrPtr);
-                Interop.Libc.Free(addrPtr);
-                if (addrStr == null || addrStr.Length == 0)
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                return System.Net.IPAddress.Parse(addrStr);
-            }
+                return ParseIPAddress(ret, addrPtr);
+           }
             set
             {
                 int ret = Interop.WiFi.AP.SetDnsAddress(_handle, 2, (int)_family, value.ToString());
@@ -94,16 +79,7 @@ namespace Tizen.Network.WiFi
             {
                 IntPtr addrPtr;
                 int ret = Interop.WiFi.AP.GetGatewayAddress(_handle, (int)_family, out addrPtr);
-                if (ret != (int)WiFiError.None)
-                {
-                    Log.Error(Globals.LogTag, "Failed to get gateway address, Error - " + (WiFiError)ret);
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                }
-                string addrStr = Marshal.PtrToStringAnsi(addrPtr);
-                Interop.Libc.Free(addrPtr);
-                if (addrStr == null || addrStr.Length == 0)
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                return System.Net.IPAddress.Parse(addrStr);
+                return ParseIPAddress(ret, addrPtr);
             }
             set
             {
@@ -122,16 +98,7 @@ namespace Tizen.Network.WiFi
             {
                 IntPtr addrPtr;
                 int ret = Interop.WiFi.AP.GetSubnetMask(_handle, (int)_family, out addrPtr);
-                if (ret != (int)WiFiError.None)
-                {
-                    Log.Error(Globals.LogTag, "Failed to get subnet mask, Error - " + (WiFiError)ret);
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                }
-                string addrStr = Marshal.PtrToStringAnsi(addrPtr);
-                Interop.Libc.Free(addrPtr);
-                if (addrStr == null || addrStr.Length == 0)
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                return System.Net.IPAddress.Parse(addrStr);
+                return ParseIPAddress(ret, addrPtr);
             }
             set
             {
@@ -150,16 +117,7 @@ namespace Tizen.Network.WiFi
             {
                 IntPtr addrPtr;
                 int ret = Interop.WiFi.AP.GetIPAddress(_handle, (int)_family, out addrPtr);
-                if (ret != (int)WiFiError.None)
-                {
-                    Log.Error(Globals.LogTag, "Failed to get ip address, Error - " + (WiFiError)ret);
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                }
-                string addrStr = Marshal.PtrToStringAnsi(addrPtr);
-                Interop.Libc.Free(addrPtr);
-                if (addrStr == null || addrStr.Length == 0)
-                    return System.Net.IPAddress.Parse("0.0.0.0");
-                return System.Net.IPAddress.Parse(addrStr);
+                return ParseIPAddress(ret, addrPtr);
             }
             set
             {
@@ -252,20 +210,38 @@ namespace Tizen.Network.WiFi
             get
             {
                 string dhcpServer;
-                int ret = Interop.WiFi.AP.GetDhcpServerAddress(_handle, AddressFamily.IPv4, out dhcpServer);
-                if (ret != (int)WiFiError.None)
+                int ret = Interop.WiFi.AP.GetDhcpServerAddress(_handle, _family, out dhcpServer);
+                if (ret != (int)WiFiError.None || dhcpServer == null || dhcpServer.Length == 0)
                 {
                     Log.Error(Globals.LogTag, "Failed to get DHCP server address, Error - " + (WiFiError)ret);
-                }
-
-                if (dhcpServer == null || dhcpServer.Length == 0)
-                {
-                    return IPAddress.Parse("0.0.0.0");
+                    return DefaultIPAddress();
                 }
 
                 return IPAddress.Parse(dhcpServer);
             }
         }
 
+        private System.Net.IPAddress ParseIPAddress(int ret, IntPtr addrPtr)
+        {
+            if (ret != (int)WiFiError.None)
+            {
+                Log.Error(Globals.LogTag, "Failed to get address, Error - " + (WiFiError)ret);
+                return DefaultIPAddress();
+            }
+
+            string addr = Marshal.PtrToStringAnsi(addrPtr);
+            if (addr == null || addr.Length == 0)
+                return DefaultIPAddress();
+            Interop.Libc.Free(addrPtr);
+            return System.Net.IPAddress.Parse(addr);
+        }
+
+        private System.Net.IPAddress DefaultIPAddress()
+        {
+            if (_family == AddressFamily.IPv4)
+                return System.Net.IPAddress.Parse(DefaultIPv4);
+            else
+                return System.Net.IPAddress.Parse(DefaultIPv6);
+        }
     }
 }
index a771486..b65ebaf 100755 (executable)
@@ -48,30 +48,30 @@ namespace Tizen.Network.WiFi
     {
         static internal void ThrowWiFiException(int e, IntPtr handle)
         {
-            ThrowExcption(e, (handle == IntPtr.Zero), false, "");
+            ThrowException(e, (handle == IntPtr.Zero), false, "");
         }
 
         static internal void ThrowWiFiException(int e, IntPtr handle1, IntPtr handle2)
         {
-            ThrowExcption(e, (handle1 == IntPtr.Zero), (handle2 == IntPtr.Zero), "");
+            ThrowException(e, (handle1 == IntPtr.Zero), (handle2 == IntPtr.Zero), "");
         }
 
         static internal void ThrowWiFiException(int e, string message)
         {
-            ThrowExcption(e, false, false, message);
+            ThrowException(e, false, false, message);
         }
 
         static internal void ThrowWiFiException(int e, IntPtr handle, string message)
         {
-            ThrowExcption(e, (handle == IntPtr.Zero), false, message);
+            ThrowException(e, (handle == IntPtr.Zero), false, message);
         }
 
         static internal void ThrowWiFiException(int e, IntPtr handle1, IntPtr handle2, string message)
         {
-            ThrowExcption(e, (handle1 == IntPtr.Zero), (handle2 == IntPtr.Zero), message);
+            ThrowException(e, (handle1 == IntPtr.Zero), (handle2 == IntPtr.Zero), message);
         }
 
-        static private void ThrowExcption(int e, bool isHandle1Null, bool isHandle2Null, string message)
+        static private void ThrowException(int e, bool isHandle1Null, bool isHandle2Null, string message)
         {
             WiFiError err = (WiFiError)e;
             if (err == WiFiError.NotSupportedError)
index be7c478..ebfce37 100644 (file)
@@ -58,6 +58,10 @@ namespace Tizen.Network.WiFi
         private int _requestId = 0;
         private string _macAddress;
 
+        //private string PrivilegeNetworkSet = "http://tizen.org/privilege/network.set";
+        private string PrivilegeNetworkGet = "http://tizen.org/privilege/network.get";
+        private string PrivilegeNetworkProfile = "http://tizen.org/privilege/network.get";
+
         internal string MacAddress
         {
             get
@@ -154,11 +158,7 @@ namespace Tizen.Network.WiFi
             int tid = Thread.CurrentThread.ManagedThreadId;
             Log.Info(Globals.LogTag, "PInvoke wifi_manager_initialize");
             int ret = Interop.WiFi.Initialize(tid, out handle);
-            if (ret != (int)WiFiError.None)
-            {
-                Log.Error(Globals.LogTag, "Failed to initialize wifi, Error - " + (WiFiError)ret);
-                WiFiErrorFactory.ThrowWiFiException(ret, "http://tizen.org/privilege/network.get");
-            }
+            CheckReturnValue(ret, "Initialize", PrivilegeNetworkGet);
             handle.SetTID(tid);
             return handle;
         }
@@ -181,15 +181,7 @@ namespace Tizen.Network.WiFi
             };
 
             int ret = Interop.WiFi.GetForeachFoundAPs(GetSafeHandle(), callback, IntPtr.Zero);
-            if (ret != (int)WiFiError.None)
-            {
-                Log.Error(Globals.LogTag, "Failed to get all APs, Error - " + (WiFiError)ret);
-                if (ret == (int)WiFiError.InvalidParameterError)
-                {
-                    throw new InvalidOperationException("Invalid handle");
-                }
-                WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.get");
-            }
+            CheckReturnValue(ret, "GetForeachFoundAPs", PrivilegeNetworkGet);
 
             return apList;
         }
@@ -213,16 +205,8 @@ namespace Tizen.Network.WiFi
             };
 
             int ret = Interop.WiFi.GetForeachFoundSpecificAPs(GetSafeHandle(), callback, IntPtr.Zero);
-            if (ret != (int)WiFiError.None)
-            {
-                Log.Error(Globals.LogTag, "Failed to get specific APs, Error - " + (WiFiError)ret);
-                if (ret == (int)WiFiError.InvalidParameterError)
-                {
-                    throw new InvalidOperationException("Invalid handle");
-                }
-                WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.get");
-            }
-
+            CheckReturnValue(ret, "GetForeachFoundSpecificAPs", PrivilegeNetworkGet);
+            
             return apList;
         }
 
@@ -244,15 +228,7 @@ namespace Tizen.Network.WiFi
             };
 
             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);
-                if (ret == (int)WiFiError.InvalidParameterError)
-                {
-                    throw new InvalidOperationException("Invalid handle");
-                }
-                WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.get");
-            }
+            CheckReturnValue(ret, "GetForeachFoundBssids", PrivilegeNetworkGet);
 
             return apList;
         }
@@ -275,16 +251,7 @@ namespace Tizen.Network.WiFi
             };
 
             int ret = Interop.WiFi.Config.GetForeachConfiguration(GetSafeHandle(), callback, IntPtr.Zero);
-            if (ret != (int)WiFiError.None)
-            {
-                Log.Error(Globals.LogTag, "Failed to get configurations, Error - " + (WiFiError)ret);
-                if (ret == (int)WiFiError.InvalidParameterError)
-                {
-                    throw new InvalidOperationException("Invalid handle");
-                }
-                WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.profile");
-            }
-
+            CheckReturnValue(ret, "GetForeachConfiguration", PrivilegeNetworkProfile);
             return configList;
         }
 
@@ -298,35 +265,20 @@ namespace Tizen.Network.WiFi
 
             IntPtr configHandle = config.GetHandle();
             int ret = Interop.WiFi.Config.SaveConfiguration(GetSafeHandle(), configHandle);
-            if (ret != (int)WiFiError.None)
-            {
-                Log.Error(Globals.LogTag, "Failed to save configuration, Error - " + (WiFiError)ret);
-                WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.profile");
-            }
-        }
+            CheckReturnValue(ret, "SaveConfiguration", PrivilegeNetworkProfile);
+       }
 
         internal WiFiAP GetConnectedAP()
         {
             Log.Info(Globals.LogTag, "GetConnectedAP");
             IntPtr apHandle;
             int ret = Interop.WiFi.GetConnectedAP(GetSafeHandle(), out apHandle);
-            if (ret != (int)WiFiError.None)
+            if (ret == (int)WiFiError.NoConnectionError)
             {
-                if (ret == (int)WiFiError.NoConnectionError)
-                {
-                    Log.Error(Globals.LogTag, "No connection " + (WiFiError)ret);
-                    return null;
-                }
-                else if (ret == (int)WiFiError.InvalidParameterError)
-                {
-                    throw new InvalidOperationException("Invalid handle");
-                }
-                else
-                {
-                    Log.Error(Globals.LogTag, "Failed to get connected AP, Error - " + (WiFiError)ret);
-                    WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), "http://tizen.org/privilege/network.get");
-                }
+                Log.Error(Globals.LogTag, "No connection " + (WiFiError)ret);
+                return null;
             }
+            CheckReturnValue(ret, "GetConnectedAP", PrivilegeNetworkGet);
             WiFiAP ap = new WiFiAP(apHandle);
             return ap;
         }
@@ -364,19 +316,11 @@ namespace Tizen.Network.WiFi
                 try
                 {
                     int ret = Interop.WiFi.Activate(GetSafeHandle(), _callback_map[id], id);
-                    if (ret != (int)WiFiError.None)
-                    {
-                        Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
-                        if (ret == (int)WiFiError.InvalidParameterError)
-                        {
-                            throw new InvalidOperationException("Invalid handle");
-                        }
-                        WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
-                    }
+                    CheckReturnValue(ret, "Activate", "");
                 }
                 catch (Exception e)
                 {
-                    Log.Error(Globals.LogTag, "Exception on ActivateAsync\n" + e.ToString());
+                    Log.Error(Globals.LogTag, "Exception on ActivateAsync\n" + e);
                     task.SetException(e);
                 }
             }, null);
@@ -417,19 +361,11 @@ namespace Tizen.Network.WiFi
                 try
                 {
                     int ret = Interop.WiFi.ActivateWithWiFiPickerTested(GetSafeHandle(), _callback_map[id], id);
-                    if (ret != (int)WiFiError.None)
-                    {
-                        Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
-                        if (ret == (int)WiFiError.InvalidParameterError)
-                        {
-                            throw new InvalidOperationException("Invalid handle");
-                        }
-                        WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
-                    }
+                    CheckReturnValue(ret, "ActivateWithWiFiPickerTested", "");
                 }
                 catch (Exception e)
                 {
-                    Log.Error(Globals.LogTag, "Exception on ActivateWithWiFiPickerTestedAsync\n" + e.ToString());
+                    Log.Error(Globals.LogTag, "Exception on ActivateWithWiFiPickerTestedAsync\n" + e);
                     task.SetException(e);
                 }
             }, null);
@@ -470,19 +406,11 @@ namespace Tizen.Network.WiFi
                 try
                 {
                     int ret = Interop.WiFi.Deactivate(GetSafeHandle(), _callback_map[id], id);
-                    if (ret != (int)WiFiError.None)
-                    {
-                        Log.Error(Globals.LogTag, "Failed to deactivate wifi, Error - " + (WiFiError)ret);
-                        if (ret == (int)WiFiError.InvalidParameterError)
-                        {
-                            throw new InvalidOperationException("Invalid handle");
-                        }
-                        WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
-                    }
+                    CheckReturnValue(ret, "Deactivate", "");
                 }
                 catch (Exception e)
                 {
-                    Log.Error(Globals.LogTag, "Exception on Deactivate\n" + e.ToString());
+                    Log.Error(Globals.LogTag, "Exception on Deactivate\n" + e);
                     task.SetException(e);
                 }
             }, null);
@@ -523,19 +451,11 @@ namespace Tizen.Network.WiFi
                 try
                 {
                     int ret = Interop.WiFi.Scan(GetSafeHandle(), _callback_map[id], id);
-                    if (ret != (int)WiFiError.None)
-                    {
-                        Log.Error(Globals.LogTag, "Failed to scan all AP, Error - " + (WiFiError)ret);
-                        if (ret == (int)WiFiError.InvalidParameterError)
-                        {
-                            throw new InvalidOperationException("Invalid handle");
-                        }
-                        WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
-                    }
+                    CheckReturnValue(ret, "Scan", "");
                 }
                 catch (Exception e)
                 {
-                    Log.Error(Globals.LogTag, "Exception on Scan\n" + e.ToString());
+                    Log.Error(Globals.LogTag, "Exception on Scan\n" + e);
                     task.SetException(e);
                 }
             }, null);
@@ -576,15 +496,11 @@ namespace Tizen.Network.WiFi
                 try
                 {
                     int ret = Interop.WiFi.ScanSpecificAP(GetSafeHandle(), essid, _callback_map[id], id);
-                    if (ret != (int)WiFiError.None)
-                    {
-                        Log.Error(Globals.LogTag, "Failed to scan with specific AP, Error - " + (WiFiError)ret);
-                        WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
-                    }
+                    CheckReturnValue(ret, "ScanSpecificAP", "");
                 }
                 catch (Exception e)
                 {
-                    Log.Error(Globals.LogTag, "Exception on ScanSpecificAPAsync\n" + e.ToString());
+                    Log.Error(Globals.LogTag, "Exception on ScanSpecificAPAsync\n" + e);
                     task.SetException(e);
                 }
             }, null);
@@ -625,24 +541,29 @@ namespace Tizen.Network.WiFi
                 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);
-                        if (ret == (int)WiFiError.InvalidParameterError)
-                        {
-                            throw new InvalidOperationException("Invalid handle");
-                        }
-                        WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
-                    }
+                    CheckReturnValue(ret, "BssidScan", "");
                 }
                 catch (Exception e)
                 {
-                    Log.Error(Globals.LogTag, "Exception on BssidScan\n" + e.ToString());
+                    Log.Error(Globals.LogTag, "Exception on BssidScan\n" + e);
                     task.SetException(e);
                 }
             }, null);
 
             return task.Task;
         }
+
+        private void CheckReturnValue(int ret, string method, string privilege)
+        {
+            if (ret != (int)WiFiError.None)
+            {
+                Log.Error(Globals.LogTag, method + " Fail, Error - " + (WiFiError)ret);
+                if (ret == (int)WiFiError.InvalidParameterError)
+                {
+                    throw new InvalidOperationException("Invalid handle");
+                }
+                WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle(), privilege);
+            }
+        }
     }
 }
index c684a62..76edc7b 100755 (executable)
@@ -66,7 +66,7 @@ namespace Tizen.Network.WiFi
                             RegisterDeviceStateChangedEvent();
                         } catch (Exception e)
                         {
-                            Log.Error(Globals.LogTag, "Exception on adding DeviceStateChanged\n" + e.ToString());
+                            Log.Error(Globals.LogTag, "Exception on adding DeviceStateChanged\n" + e);
                             return;
                         }
                     }
@@ -86,7 +86,7 @@ namespace Tizen.Network.WiFi
                         }
                         catch (Exception e)
                         {
-                            Log.Error(Globals.LogTag, "Exception on removing DeviceStateChanged\n" + e.ToString());
+                            Log.Error(Globals.LogTag, "Exception on removing DeviceStateChanged\n" + e);
                         }
                     }
                 }, null);
@@ -107,7 +107,7 @@ namespace Tizen.Network.WiFi
                         }
                         catch (Exception e)
                         {
-                            Log.Error(Globals.LogTag, "Exception on adding ConnectionStateChanged\n" + e.ToString());
+                            Log.Error(Globals.LogTag, "Exception on adding ConnectionStateChanged\n" + e);
                             return;
                         }
                     }
@@ -127,7 +127,7 @@ namespace Tizen.Network.WiFi
                         }
                         catch (Exception e)
                         {
-                            Log.Error(Globals.LogTag, "Exception on removing ConnectionStateChanged\n" + e.ToString());
+                            Log.Error(Globals.LogTag, "Exception on removing ConnectionStateChanged\n" + e);
                         }
                     }
                 }, null);
@@ -148,7 +148,7 @@ namespace Tizen.Network.WiFi
                         }
                         catch (Exception e)
                         {
-                            Log.Error(Globals.LogTag, "Exception on adding RssiLevelChanged\n" + e.ToString());
+                            Log.Error(Globals.LogTag, "Exception on adding RssiLevelChanged\n" + e);
                             return;
                         }
                     }
@@ -168,7 +168,7 @@ namespace Tizen.Network.WiFi
                         }
                         catch (Exception e)
                         {
-                            Log.Error(Globals.LogTag, "Exception on removing RssiLevelChanged\n" + e.ToString());
+                            Log.Error(Globals.LogTag, "Exception on removing RssiLevelChanged\n" + e);
                         }
                     }
                 }, null);
@@ -189,7 +189,7 @@ namespace Tizen.Network.WiFi
                         }
                         catch (Exception e)
                         {
-                            Log.Error(Globals.LogTag, "Exception on adding BackgroundScanFinished\n" + e.ToString());
+                            Log.Error(Globals.LogTag, "Exception on adding BackgroundScanFinished\n" + e);
                             return;
                         }
                     }
@@ -209,7 +209,7 @@ namespace Tizen.Network.WiFi
                         }
                         catch (Exception e)
                         {
-                            Log.Error(Globals.LogTag, "Exception on removing BackgroundScanFinished\n" + e.ToString());
+                            Log.Error(Globals.LogTag, "Exception on removing BackgroundScanFinished\n" + e);
                         }
                     }
                 }, null);