From: chleun-moon <32117100+chleun-moon@users.noreply.github.com> Date: Tue, 13 Mar 2018 08:13:32 +0000 (+0900) Subject: [Connection][WiFi] Fix exception handling in async methods (#154) X-Git-Tag: 5.0.0.14283^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b151970b553ea0091f1e6553a40987c8f6e41343;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [Connection][WiFi] Fix exception handling in async methods (#154) --- diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs old mode 100755 new mode 100644 index b68723c..7f3718a --- a/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs @@ -880,13 +880,21 @@ namespace Tizen.Network.Connection context.Post((x) => { Log.Info(Globals.LogTag, "Interop.Connection.SetDefaultCellularServiceProfileAsync " + profile.Name); - int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0); - if ((ConnectionError)ret != ConnectionError.None) + try { - Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret); - ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony"); - ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released"); - ConnectionErrorFactory.ThrowConnectionException(ret); + int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0); + + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret); + ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony"); + ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released"); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } catch (Exception e) + { + Log.Error(Globals.LogTag, "Exception on SetDefaultCellularServiceProfileAsync\n" + e.ToString()); + task.SetException(e); } }, null); @@ -970,13 +978,21 @@ namespace Tizen.Network.Connection context.Post((x) => { Log.Info(Globals.LogTag, "Interop.Connection.OpenProfile " + profile.Name); - int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero); - if ((ConnectionError)ret != ConnectionError.None) + try { - Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret); - ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth"); - ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released"); - ConnectionErrorFactory.ThrowConnectionException(ret); + int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret); + ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth"); + ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released"); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + catch (Exception e) + { + Log.Error(Globals.LogTag, "Exception on OpenProfile\n" + e.ToString()); + task.SetException(e); } }, null); @@ -1013,14 +1029,22 @@ namespace Tizen.Network.Connection context.Post((x) => { Log.Info(Globals.LogTag, "Interop.Connection.CloseProfile " + profile.Name); - int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero); - if ((ConnectionError)ret != ConnectionError.None) + try { - Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret); - ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth"); - ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)"); - ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released"); - ConnectionErrorFactory.ThrowConnectionException(ret); + int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret); + ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth"); + ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)"); + ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released"); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + catch (Exception e) + { + Log.Error(Globals.LogTag, "Exception on CloseProfile\n" + e.ToString()); + task.SetException(e); } }, null); diff --git a/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiAP.cs b/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiAP.cs old mode 100755 new mode 100644 index 46166ee..01c3156 --- a/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiAP.cs +++ b/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiAP.cs @@ -246,11 +246,19 @@ namespace Tizen.Network.WiFi context.Post((x) => { Log.Info(Globals.LogTag, "Interop.WiFi.Connect"); - int ret = Interop.WiFi.Connect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id); - if (ret != (int)WiFiError.None) + try { - Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret); - WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle); + int ret = Interop.WiFi.Connect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id); + if (ret != (int)WiFiError.None) + { + Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret); + WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle); + } + } + catch (Exception e) + { + Log.Error(Globals.LogTag, "Exception on ConnectAsync\n" + e.ToString()); + task.SetException(e); } }, null); @@ -307,33 +315,41 @@ namespace Tizen.Network.WiFi context.Post((x) => { - int ret = -1; - if (info.GetType() == typeof(WpsPbcInfo)) - { - Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPb"); - ret = Interop.WiFi.ConnectByWpsPbc(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id); - } - else if (info.GetType() == typeof(WpsPinInfo)) + try { - WpsPinInfo pinInfo = (WpsPinInfo)info; - if (pinInfo.GetWpsPin() == null) + int ret = -1; + if (info.GetType() == typeof(WpsPbcInfo)) { - throw new ArgumentNullException("Wps pin should not be null"); + Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPb"); + ret = Interop.WiFi.ConnectByWpsPbc(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id); } - - if (pinInfo.GetWpsPin().Length == 0 || pinInfo.GetWpsPin().Length > 8) + else if (info.GetType() == typeof(WpsPinInfo)) { - throw new ArgumentOutOfRangeException("Wps pin should not be empty or more than 7 characters"); + WpsPinInfo pinInfo = (WpsPinInfo)info; + if (pinInfo.GetWpsPin() == null) + { + throw new ArgumentNullException("Wps pin should not be null"); + } + + if (pinInfo.GetWpsPin().Length == 0 || pinInfo.GetWpsPin().Length > 8) + { + throw new ArgumentOutOfRangeException("Wps pin should not be empty or more than 7 characters"); + } + + Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPin"); + ret = Interop.WiFi.ConnectByWpsPin(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, pinInfo.GetWpsPin(), _callback_map[id], id); } - Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPin"); - ret = Interop.WiFi.ConnectByWpsPin(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, pinInfo.GetWpsPin(), _callback_map[id], id); + if (ret != (int)WiFiError.None) + { + Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret); + WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle); + } } - - if (ret != (int)WiFiError.None) + catch (Exception e) { - Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret); - WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle); + Log.Error(Globals.LogTag, "Exception on ConnectWpsAsync\n" + e.ToString()); + task.SetException(e); } }, null); @@ -390,33 +406,41 @@ namespace Tizen.Network.WiFi s_context.Post((x) => { - int ret = -1; - if (info.GetType() == typeof(WpsPbcInfo)) + try { - Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPbcWithoutSsid"); - ret = Interop.WiFi.ConnectByWpsPbcWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), s_callbackMap[id], id); - } - else if (info.GetType() == typeof(WpsPinInfo)) - { - WpsPinInfo pinInfo = (WpsPinInfo)info; - if (pinInfo.GetWpsPin() == null) + int ret = -1; + if (info.GetType() == typeof(WpsPbcInfo)) { - throw new ArgumentNullException("Wps pin should not be null"); + Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPbcWithoutSsid"); + ret = Interop.WiFi.ConnectByWpsPbcWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), s_callbackMap[id], id); } - - if (pinInfo.GetWpsPin().Length != 4 && pinInfo.GetWpsPin().Length != 8) + else if (info.GetType() == typeof(WpsPinInfo)) { - throw new ArgumentOutOfRangeException("Wps pin should be of 4 or 8 characters long"); + WpsPinInfo pinInfo = (WpsPinInfo)info; + if (pinInfo.GetWpsPin() == null) + { + throw new ArgumentNullException("Wps pin should not be null"); + } + + if (pinInfo.GetWpsPin().Length != 4 && pinInfo.GetWpsPin().Length != 8) + { + throw new ArgumentOutOfRangeException("Wps pin should be of 4 or 8 characters long"); + } + + Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPinWithoutSsid"); + ret = Interop.WiFi.ConnectByWpsPinWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), pinInfo.GetWpsPin(), s_callbackMap[id], id); } - Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPinWithoutSsid"); - ret = Interop.WiFi.ConnectByWpsPinWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), pinInfo.GetWpsPin(), s_callbackMap[id], id); + if (ret != (int)WiFiError.None) + { + Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret); + WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle()); + } } - - if (ret != (int)WiFiError.None) + catch (Exception e) { - Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret); - WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle()); + Log.Error(Globals.LogTag, "Exception on ConnectWpsWithoutSsidAsync\n" + e.ToString()); + task.SetException(e); } }, null); @@ -471,11 +495,19 @@ namespace Tizen.Network.WiFi context.Post((x) => { Log.Info(Globals.LogTag, "Interop.WiFi.Disconnect"); - int ret = Interop.WiFi.Disconnect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id); - if (ret != (int)WiFiError.None) + try + { + int ret = Interop.WiFi.Disconnect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id); + if (ret != (int)WiFiError.None) + { + Log.Error(Globals.LogTag, "Failed to disconnect wifi, Error - " + (WiFiError)ret); + WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle); + } + } + catch (Exception e) { - Log.Error(Globals.LogTag, "Failed to disconnect wifi, Error - " + (WiFiError)ret); - WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle); + Log.Error(Globals.LogTag, "Exception on Disconnect\n" + e.ToString()); + task.SetException(e); } }, null); diff --git a/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs b/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs old mode 100755 new mode 100644 index b896fad..ee092aa --- a/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs +++ b/src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs @@ -314,11 +314,19 @@ namespace Tizen.Network.WiFi context.Post((x) => { Log.Info(Globals.LogTag, "Interop.WiFi.ActivateAsync"); - int ret = Interop.WiFi.Activate(GetSafeHandle(), _callback_map[id], id); - if (ret != (int)WiFiError.None) + 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); + WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle()); + } + } + catch (Exception e) { - Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret); - WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle()); + Log.Error(Globals.LogTag, "Exception on ActivateAsync\n" + e.ToString()); + task.SetException(e); } }, null); @@ -355,11 +363,19 @@ namespace Tizen.Network.WiFi context.Post((x) => { Log.Info(Globals.LogTag, "Interop.WiFi.ActivateWithWiFiPickerTestedAsync"); - int ret = Interop.WiFi.ActivateWithWiFiPickerTested(GetSafeHandle(), _callback_map[id], id); - if (ret != (int)WiFiError.None) + try { - Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret); - WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle()); + 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); + WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle()); + } + } + catch (Exception e) + { + Log.Error(Globals.LogTag, "Exception on ActivateWithWiFiPickerTestedAsync\n" + e.ToString()); + task.SetException(e); } }, null); @@ -396,11 +412,19 @@ namespace Tizen.Network.WiFi context.Post((x) => { Log.Info(Globals.LogTag, "Interop.WiFi.Deactivate"); - int ret = Interop.WiFi.Deactivate(GetSafeHandle(), _callback_map[id], id); - if (ret != (int)WiFiError.None) + 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); + WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle()); + } + } + catch (Exception e) { - Log.Error(Globals.LogTag, "Failed to deactivate wifi, Error - " + (WiFiError)ret); - WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle()); + Log.Error(Globals.LogTag, "Exception on Deactivate\n" + e.ToString()); + task.SetException(e); } }, null); @@ -437,11 +461,19 @@ namespace Tizen.Network.WiFi context.Post((x) => { Log.Info(Globals.LogTag, "Interop.WiFi.Scan"); - int ret = Interop.WiFi.Scan(GetSafeHandle(), _callback_map[id], id); - if (ret != (int)WiFiError.None) + try { - Log.Error(Globals.LogTag, "Failed to scan all AP, Error - " + (WiFiError)ret); - WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle()); + 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); + WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle()); + } + } + catch (Exception e) + { + Log.Error(Globals.LogTag, "Exception on Scan\n" + e.ToString()); + task.SetException(e); } }, null); @@ -478,11 +510,19 @@ namespace Tizen.Network.WiFi context.Post((x) => { Log.Info(Globals.LogTag, "Interop.WiFi.ScanSpecificAPAsync"); - int ret = Interop.WiFi.ScanSpecificAP(GetSafeHandle(), essid, _callback_map[id], id); - if (ret != (int)WiFiError.None) + 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()); + } + } + catch (Exception e) { - Log.Error(Globals.LogTag, "Failed to scan with specific AP, Error - " + (WiFiError)ret); - WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle()); + Log.Error(Globals.LogTag, "Exception on ScanSpecificAPAsync\n" + e.ToString()); + task.SetException(e); } }, null);