[Connection][WiFi] Handling exception for event and async methods (#158) 89/172489/1
authorchleun.moon <chleun.moon@samsung.com>
Wed, 14 Mar 2018 07:15:48 +0000 (16:15 +0900)
committerchleun.moon <chleun.moon@samsung.com>
Wed, 14 Mar 2018 07:15:56 +0000 (16:15 +0900)
Change-Id: I2de62dbb0fa2d7b1106d3eb27de42368ee1a8bef
Signed-off-by: Cheoleun Moon <chleun.moon@samsung.com>
src/Tizen.Network.Connection/Tizen.Network.Connection.csproj
src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs
src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionProfile.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi.csproj
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiAP.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiConfiguration.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManager.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiManagerImpl.cs
src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiNetworkChange.cs

index 7f4584ffea8f900ba1aaefe31daeae81c8dd7790..4942b308f5316b7874e38db5862b2182121060d7 100644 (file)
@@ -7,6 +7,7 @@
   <ItemGroup>
     <ProjectReference Include="..\Tizen\Tizen.csproj" />
     <ProjectReference Include="..\Tizen.Log\Tizen.Log.csproj" />
+    <ProjectReference Include="..\Tizen.Applications.Common\Tizen.Applications.Common.csproj" />
   </ItemGroup>
 
 </Project>
index a4ef319ba2e45eec11ffbc7c6bd0257467881d30..c4b254b23055faa69bf8f658a4d1ae42f5d631d2 100755 (executable)
@@ -22,19 +22,20 @@ using System.Threading.Tasks;
 using System.Runtime.InteropServices;
 using System.Collections;
 using System.Threading;
+using Tizen.Applications;
 
 namespace Tizen.Network.Connection
 {
     class HandleHolder
     {
         private IntPtr Handle;
-        private int tid;
+        private int _tid;
 
         public HandleHolder()
         {
-            tid = Thread.CurrentThread.ManagedThreadId;
-            Log.Info(Globals.LogTag, "PInvoke connection_destroy for Thread " + tid);
-            int ret = Interop.Connection.Create(tid, out Handle);
+            _tid = Thread.CurrentThread.ManagedThreadId;
+            Log.Info(Globals.LogTag, "PInvoke connection_create for Thread " + _tid);
+            int ret = Interop.Connection.Create(_tid, out Handle);
             Log.Info(Globals.LogTag, "Handle: " + Handle);
             if(ret != (int)ConnectionError.None)
             {
@@ -57,8 +58,9 @@ namespace Tizen.Network.Connection
 
         private void Destroy()
         {
-            Log.Info(Globals.LogTag, "PInvoke connection_destroy for Thread " + tid);
-            Interop.Connection.Destroy(tid, Handle);
+
+            Log.Info(Globals.LogTag, "PInvoke connection_destroy for Thread " + _tid);
+            Interop.Connection.Destroy(_tid, Handle);
             if (Handle != IntPtr.Zero)
             {
                 Handle = IntPtr.Zero;
@@ -81,6 +83,8 @@ namespace Tizen.Network.Connection
         private Interop.Connection.ConnectionAddressChangedCallback _proxyAddressChangedCallback;
         private Interop.Connection.EthernetCableStateChangedCallback _ethernetCableStateChangedCallback;
 
+        private TizenSynchronizationContext context = new TizenSynchronizationContext();
+
         internal static ConnectionInternalManager Instance
         {
             get
@@ -102,6 +106,7 @@ namespace Tizen.Network.Connection
 
         ~ConnectionInternalManager()
         {
+            UnregisterEvents();
         }
 
         internal IntPtr GetHandle()
@@ -113,25 +118,46 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                if (_ConnectionTypeChanged == null)
+                context.Post((x) =>
                 {
-                    ConnectionTypeChangedStart();
-                }
-
-                _ConnectionTypeChanged += value;
+                    if (_ConnectionTypeChanged == null)
+                    {
+                        try
+                        {
+                            ConnectionTypeChangedStart();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on adding ConnectionTypeChanged\n" + e.ToString());
+                            return;
+                        }
+                    }
+                    _ConnectionTypeChanged += value;
+                }, null);
             }
             remove
             {
-                _ConnectionTypeChanged -= value;
-                if (_ConnectionTypeChanged == null)
+                context.Post((x) =>
                 {
-                    ConnectionTypeChangedStop();
-                }
+                    _ConnectionTypeChanged -= value;
+                    if (_ConnectionTypeChanged == null)
+                    {
+                        try
+                        {
+                            ConnectionTypeChangedStop();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on removing ConnectionTypeChanged\n" + e.ToString());
+                        }
+                    }
+                }, null);
             }
         }
 
         private void ConnectionTypeChangedStart()
         {
+            Log.Info(Globals.LogTag, "Register ConnectionTypeChanged");
             _connectionTypeChangedCallback = (ConnectionType type, IntPtr user_data) =>
             {
                 if (_ConnectionTypeChanged != null)
@@ -150,6 +176,7 @@ namespace Tizen.Network.Connection
 
         private void ConnectionTypeChangedStop()
         {
+            Log.Info(Globals.LogTag, "Unregister ConnectionTypeChanged");
             int ret = Interop.Connection.UnsetTypeChangedCallback(GetHandle());
             if ((ConnectionError)ret != ConnectionError.None)
             {
@@ -162,24 +189,46 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                if (_EthernetCableStateChanged == null)
+                context.Post((x) =>
                 {
-                    EthernetCableStateChangedStart();
-                }
-                _EthernetCableStateChanged += value;
+                    if (_EthernetCableStateChanged == null)
+                    {
+                        try
+                        {
+                            EthernetCableStateChangedStart();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on adding EthernetCableStateChanged\n" + e.ToString());
+                            return;
+                        }
+                    }
+                    _EthernetCableStateChanged += value;
+                }, null);
             }
             remove
             {
-                _EthernetCableStateChanged -= value;
-                if (_EthernetCableStateChanged == null)
+                context.Post((x) =>
                 {
-                    EthernetCableStateChangedStop();
-                }
+                    _EthernetCableStateChanged -= value;
+                    if (_EthernetCableStateChanged == null)
+                    {
+                        try
+                        {
+                            EthernetCableStateChangedStop();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on removing EthernetCableStateChanged\n" + e.ToString());
+                        }
+                    }
+                }, null);
             }
         }
 
         private void EthernetCableStateChangedStart()
         {
+            Log.Info(Globals.LogTag, "Register EthernetCableStateChanged");
             _ethernetCableStateChangedCallback = (EthernetCableState state, IntPtr user_data) =>
             {
                 if (_EthernetCableStateChanged != null)
@@ -200,6 +249,7 @@ namespace Tizen.Network.Connection
 
         private void EthernetCableStateChangedStop()
         {
+            Log.Info(Globals.LogTag, "Unregister EthernetCableStateChanged");
             int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(GetHandle());
             if ((ConnectionError)ret != ConnectionError.None)
             {
@@ -214,25 +264,47 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                if (_IPAddressChanged == null)
+                context.Post((x) =>
                 {
-                    IPAddressChangedStart();
-                }
-                _IPAddressChanged += value;
+                    if (_IPAddressChanged == null)
+                    {
+                        try
+                        {
+                            IPAddressChangedStart();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on adding IPAddressChanged\n" + e.ToString());
+                            return;
+                        }
+                    }
+                    _IPAddressChanged += value;
+                }, null);
             }
 
             remove
             {
-                _IPAddressChanged -= value;
-                if (_IPAddressChanged == null)
+                context.Post((x) =>
                 {
-                    IPAddressChangedStop();
-                }
+                    _IPAddressChanged -= value;
+                    if (_IPAddressChanged == null)
+                    {
+                        try
+                        {
+                            IPAddressChangedStop();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on removing IPAddressChanged\n" + e.ToString());
+                        }
+                    }
+                }, null);
             }
         }
 
         private void IPAddressChangedStart()
         {
+            Log.Info(Globals.LogTag, "Register IPAddressChanged");
             _connectionAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
             {
                 if (_IPAddressChanged != null)
@@ -256,6 +328,7 @@ namespace Tizen.Network.Connection
 
         private void IPAddressChangedStop()
         {
+            Log.Info(Globals.LogTag, "Unregister IPAddressChanged");
             int ret = Interop.Connection.UnsetIPAddressChangedCallback(GetHandle());
             if ((ConnectionError)ret != ConnectionError.None)
             {
@@ -267,27 +340,46 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                //Console.WriteLine("ProxyAddressChanged Add **");
-                if (_ProxyAddressChanged == null)
+                context.Post((x) =>
                 {
-                    ProxyAddressChangedStart();
-                }
-
-                _ProxyAddressChanged += value;
+                    if (_ProxyAddressChanged == null)
+                    {
+                        try
+                        {
+                            ProxyAddressChangedStart();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on adding ProxyAddressChanged\n" + e.ToString());
+                            return;
+                        }
+                    }
+                    _ProxyAddressChanged += value;
+                }, null);
             }
             remove
             {
-                //Console.WriteLine("ProxyAddressChanged Remove");
-                _ProxyAddressChanged -= value;
-                if (_ProxyAddressChanged == null)
+                context.Post((x) =>
                 {
-                    ProxyAddressChangedStop();
-                }
+                    _ProxyAddressChanged -= value;
+                    if (_ProxyAddressChanged == null)
+                    {
+                        try
+                        {
+                            ProxyAddressChangedStop();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on removing ProxyAddressChanged\n" + e.ToString());
+                        }
+                    }
+                }, null);
             }
         }
 
         private void ProxyAddressChangedStart()
         {
+            Log.Info(Globals.LogTag, "Register ProxyAddressChanged");
             _proxyAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
             {
                 if (_ProxyAddressChanged != null)
@@ -311,6 +403,7 @@ namespace Tizen.Network.Connection
 
         private void ProxyAddressChangedStop()
         {
+            Log.Info(Globals.LogTag, "Unregister ProxyAddressChanged");
             int ret = Interop.Connection.UnsetProxyAddressChangedCallback(GetHandle());
             if ((ConnectionError)ret != ConnectionError.None)
             {
@@ -766,12 +859,13 @@ namespace Tizen.Network.Connection
 
         internal Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
         {
-            Log.Debug(Globals.LogTag, "SetDefaultCellularProfile");
+            Log.Info(Globals.LogTag, "SetDefaultCellularProfile");
             if (profile != null)
             {
                 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
                 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
                 {
+                    Log.Info(Globals.LogTag, "SetDefaultCellularProfile done " + profile.Name);
                     if (Result != ConnectionError.None)
                     {
                         Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + Result);
@@ -783,18 +877,29 @@ namespace Tizen.Network.Connection
                     }
                 };
 
-                int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0);
-                if ((ConnectionError)ret != ConnectionError.None)
+                context.Post((x) =>
                 {
-                    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);
-                }
+                    Log.Info(Globals.LogTag, "Interop.Connection.SetDefaultCellularServiceProfileAsync " + profile.Name);
+                    try
+                    {
+                        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);
 
                 return task.Task;
             }
-
             else
             {
                 throw new ArgumentNullException("Profile is null");
@@ -851,14 +956,14 @@ namespace Tizen.Network.Connection
 
         internal Task OpenProfileAsync(ConnectionProfile profile)
         {
-            Log.Debug(Globals.LogTag, "OpenProfileAsync");
+            Log.Info(Globals.LogTag, "OpenProfileAsync");
             if (profile != null)
             {
-                Log.Debug(Globals.LogTag, "OpenProfileAsync " + profile.Name);
+                Log.Info(Globals.LogTag, "OpenProfileAsync " + profile.Name);
                 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
                 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
                 {
-                    Log.Debug(Globals.LogTag, "Connected " + profile.Name);
+                    Log.Info(Globals.LogTag, "OpenProfileAsync done " + profile.Name);
                     if (Result != ConnectionError.None)
                     {
                         Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + Result);
@@ -870,14 +975,26 @@ namespace Tizen.Network.Connection
                     }
                 };
 
-                int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
-                if ((ConnectionError)ret != ConnectionError.None)
+                context.Post((x) =>
                 {
-                    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);
-                }
+                    Log.Info(Globals.LogTag, "Interop.Connection.OpenProfile " + profile.Name);
+                    try
+                    {
+                        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);
 
                 return task.Task;
             }
@@ -890,13 +1007,14 @@ namespace Tizen.Network.Connection
 
         internal Task CloseProfileAsync(ConnectionProfile profile)
         {
-            Log.Debug(Globals.LogTag, "CloseProfileAsync");
+            Log.Info(Globals.LogTag, "CloseProfileAsync");
             if (profile != null)
             {
-                Log.Debug(Globals.LogTag, "CloseProfileAsync " + profile.Name);
+                Log.Info(Globals.LogTag, "CloseProfileAsync " + profile.Name);
                 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
                 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
                 {
+                    Log.Info(Globals.LogTag, "CloseProfileAsync done " + profile.Name);
                     if (Result != ConnectionError.None)
                     {
                         Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + Result);
@@ -908,15 +1026,27 @@ namespace Tizen.Network.Connection
                     }
                 };
 
-                int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
-                if ((ConnectionError)ret != ConnectionError.None)
+                context.Post((x) =>
                 {
-                    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);
-                }
+                    Log.Info(Globals.LogTag, "Interop.Connection.CloseProfile " + profile.Name);
+                    try
+                    {
+                        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);
 
                 return task.Task;
             }
index 321577778898eba9ba98df0c463b1429b4a68a06..5ab232c4ec578e00e5a9a5b9c15f071add0024a0 100755 (executable)
@@ -19,6 +19,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Runtime.InteropServices;
+using Tizen.Applications;
 
 namespace Tizen.Network.Connection
 {
@@ -36,6 +37,8 @@ namespace Tizen.Network.Connection
 
         private Interop.ConnectionProfile.ProfileStateChangedCallback _profileChangedCallback;
 
+        private TizenSynchronizationContext context = new TizenSynchronizationContext();
+
         internal IntPtr GetHandle()
         {
             return ProfileHandle;
@@ -54,21 +57,27 @@ namespace Tizen.Network.Connection
         {
             add
             {
-                Log.Debug(Globals.LogTag, "ProfileStateChanged add");
-                if (_ProfileStateChanged == null)
+                Log.Info(Globals.LogTag, "ProfileStateChanged add");
+                context.Post((x) =>
                 {
-                    ProfileStateChangedStart();
-                }
-                _ProfileStateChanged += value;
+                    if (_ProfileStateChanged == null)
+                    {
+                        ProfileStateChangedStart();
+                    }
+                    _ProfileStateChanged += value;
+                }, null);
             }
             remove
             {
-                Log.Debug(Globals.LogTag, "ProfileStateChanged remove");
-                _ProfileStateChanged -= value;
-                if (_ProfileStateChanged == null)
+                Log.Info(Globals.LogTag, "ProfileStateChanged remove");
+                context.Post((x) =>
                 {
-                    ProfileStateChangedStop();
-                }
+                    _ProfileStateChanged -= value;
+                    if (_ProfileStateChanged == null)
+                    {
+                        ProfileStateChangedStop();
+                    }
+                }, null);
             }
         }
 
@@ -76,13 +85,14 @@ namespace Tizen.Network.Connection
         {
             _profileChangedCallback = (ProfileState state, IntPtr userData) =>
             {
+                Log.Info(Globals.LogTag, "***** MOON ProfileStateChanged occur");
                 if (_ProfileStateChanged != null)
                 {
                     _ProfileStateChanged(null, new ProfileStateEventArgs(state));
                 }
             };
 
-            Log.Debug(Globals.LogTag, "ProfileStateChangedStart");
+            Log.Info(Globals.LogTag, "ProfileStateChangedStart");
             int ret = Interop.ConnectionProfile.SetStateChangeCallback(ProfileHandle, _profileChangedCallback, IntPtr.Zero);
             if ((ConnectionError)ret != ConnectionError.None)
             {
@@ -92,7 +102,7 @@ namespace Tizen.Network.Connection
 
         private void ProfileStateChangedStop()
         {
-            Log.Debug(Globals.LogTag, "ProfileStateChangedStop");
+            Log.Info(Globals.LogTag, "ProfileStateChangedStop");
             int ret = Interop.ConnectionProfile.UnsetStateChangeCallback(ProfileHandle);
             if ((ConnectionError)ret != ConnectionError.None)
             {
@@ -131,12 +141,9 @@ namespace Tizen.Network.Connection
             if (disposed)
                 return;
 
-            if (disposing)
-            {
-                // Free managed objects.
-                UnregisterEvents();
-                Destroy();
-            }
+            // Free unmanaged objects
+            UnregisterEvents();
+            Destroy();
             disposed = true;
         }
 
index b3b1ca16e0962dd938063bac7b19f1873fbe2641..52d22a12c29275487f9522c67c49d5b5a1680c77 100644 (file)
@@ -6,6 +6,7 @@
 
   <ItemGroup>
     <ProjectReference Include="..\Tizen.Network.Connection\Tizen.Network.Connection.csproj" />
+    <ProjectReference Include="..\Tizen.Applications.Common\Tizen.Applications.Common.csproj" />
   </ItemGroup>
 
 </Project>
index e170b25c7c94e5472b5cc7363c5bb9d88a177eeb..cc72ad6806b83ce5a08bfd95a32426f8a01b5a9f 100755 (executable)
@@ -17,7 +17,7 @@
 using System;
 using System.Threading.Tasks;
 using System.Collections.Generic;
-using System.ComponentModel;
+using Tizen.Applications;
 
 namespace Tizen.Network.WiFi
 {
@@ -36,6 +36,9 @@ namespace Tizen.Network.WiFi
         private WiFiSecurity _security;
         private bool _disposed = false;
 
+        private TizenSynchronizationContext context = new TizenSynchronizationContext();
+        private static TizenSynchronizationContext s_context = new TizenSynchronizationContext();
+
         /// <summary>
         /// The network information of the access point (AP).
         /// </summary>
@@ -132,11 +135,8 @@ namespace Tizen.Network.WiFi
             if (_disposed)
                 return;
 
-            if (disposing)
-            {
-                Interop.WiFi.AP.Destroy(_apHandle);
-                _apHandle = IntPtr.Zero;
-            }
+            Interop.WiFi.AP.Destroy(_apHandle);
+            _apHandle = IntPtr.Zero;
             _disposed = true;
         }
 
@@ -214,7 +214,7 @@ namespace Tizen.Network.WiFi
         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
         public Task ConnectAsync()
         {
-            Log.Debug(Globals.LogTag, "ConnectAsync");
+            Log.Info(Globals.LogTag, "ConnectAsync");
             if (_disposed)
             {
                 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
@@ -226,7 +226,7 @@ namespace Tizen.Network.WiFi
                 id = (IntPtr)_requestId++;
                 _callback_map[id] = (error, key) =>
                 {
-                    Log.Debug(Globals.LogTag, "Connecting finished : " + (WiFiError)error);
+                    Log.Info(Globals.LogTag, "ConnectAsync done " + (WiFiError)error);
                     if (error != (int)WiFiError.None)
                     {
                         Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
@@ -243,12 +243,24 @@ namespace Tizen.Network.WiFi
                 };
             }
 
-            int ret = Interop.WiFi.Connect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
-            if (ret != (int)WiFiError.None)
+            context.Post((x) =>
             {
-                Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
-                WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
-            }
+                Log.Info(Globals.LogTag, "Interop.WiFi.Connect");
+                try
+                {
+                    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);
 
             return task.Task;
         }
@@ -272,7 +284,7 @@ namespace Tizen.Network.WiFi
         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
         public Task ConnectWpsAsync(WpsInfo info)
         {
-            Log.Debug(Globals.LogTag, "ConnectWpsAsync");
+            Log.Info(Globals.LogTag, "ConnectWpsAsync");
             if (_disposed)
             {
                 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
@@ -284,7 +296,7 @@ namespace Tizen.Network.WiFi
                 id = (IntPtr)_requestId++;
                 _callback_map[id] = (error, key) =>
                 {
-                    Log.Debug(Globals.LogTag, "Connecting by WPS finished");
+                    Log.Info(Globals.LogTag, "ConnectWpsAsync done");
                     if (error != (int)WiFiError.None)
                     {
                         Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
@@ -301,33 +313,45 @@ namespace Tizen.Network.WiFi
                 };
             }
 
-            int ret = -1;
-            if (info.GetType() == typeof(WpsPbcInfo))
-            {
-                ret = Interop.WiFi.ConnectByWpsPbc(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
-            }
-
-            else if (info.GetType() == typeof(WpsPinInfo))
+            context.Post((x) =>
             {
-                WpsPinInfo pinInfo = (WpsPinInfo)info;
-                if (pinInfo.GetWpsPin() == null)
+                try
                 {
-                    throw new ArgumentNullException("Wps pin should not be null");
-                }
+                    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))
+                    {
+                        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);
+                    }
 
-                if (pinInfo.GetWpsPin().Length == 0 || pinInfo.GetWpsPin().Length > 8)
+                    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)
                 {
-                    throw new ArgumentOutOfRangeException("Wps pin should not be empty or more than 7 characters");
+                    Log.Error(Globals.LogTag, "Exception on ConnectWpsAsync\n" + e.ToString());
+                    task.SetException(e);
                 }
-
-                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);
-            }
+            }, null);
 
             return task.Task;
         }
@@ -354,6 +378,7 @@ namespace Tizen.Network.WiFi
         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
         public static Task<WiFiAP> ConnectWpsWithoutSsidAsync(WpsInfo info)
         {
+            Log.Info(Globals.LogTag, "ConnectWpsWithoutSsidAsync");
             TaskCompletionSource<WiFiAP> task = new TaskCompletionSource<WiFiAP>();
             IntPtr id;
             lock (s_callbackMap)
@@ -361,7 +386,7 @@ namespace Tizen.Network.WiFi
                 id = (IntPtr)s_requestId++;
                 s_callbackMap[id] = (error, key) =>
                 {
-                    Log.Debug(Globals.LogTag, "Connecting by WPS finished");
+                    Log.Info(Globals.LogTag, "ConnectWpsWithoutSsidAsync done");
                     if (error != (int)WiFiError.None)
                     {
                         Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
@@ -379,33 +404,45 @@ namespace Tizen.Network.WiFi
                 };
             }
 
-            int ret = -1;
-            if (info.GetType() == typeof(WpsPbcInfo))
-            {
-                ret = Interop.WiFi.ConnectByWpsPbcWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), s_callbackMap[id], id);   
-            }
-
-            else if (info.GetType() == typeof(WpsPinInfo))
+            s_context.Post((x) =>
             {
-                WpsPinInfo pinInfo = (WpsPinInfo)info;
-                if (pinInfo.GetWpsPin() == null)
+                try
                 {
-                    throw new ArgumentNullException("Wps pin should not be null");
-                }
+                    int ret = -1;
+                    if (info.GetType() == typeof(WpsPbcInfo))
+                    {
+                        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)
+                        {
+                            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);
+                    }
 
-                if (pinInfo.GetWpsPin().Length != 4 && pinInfo.GetWpsPin().Length != 8)
+                    if (ret != (int)WiFiError.None)
+                    {
+                        Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
+                        WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
+                    }
+                }
+                catch (Exception e)
                 {
-                    throw new ArgumentOutOfRangeException("Wps pin should be of 4 or 8 characters long");
+                    Log.Error(Globals.LogTag, "Exception on ConnectWpsWithoutSsidAsync\n" + e.ToString());
+                    task.SetException(e);
                 }
-
-                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());
-            }
+            }, null);
 
             return task.Task;
         }
@@ -438,7 +475,7 @@ namespace Tizen.Network.WiFi
                 id = (IntPtr)_requestId++;
                 _callback_map[id] = (error, key) =>
                 {
-                    Log.Debug(Globals.LogTag, "Disconnecting finished");
+                    Log.Info(Globals.LogTag, "DisconnectAsync done");
                     if (error != (int)WiFiError.None)
                     {
                         Log.Error(Globals.LogTag, "Error occurs during WiFi disconnecting, " + (WiFiError)error);
@@ -454,12 +491,26 @@ namespace Tizen.Network.WiFi
                     }
                 };
             }
-            int ret = Interop.WiFi.Disconnect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
-            if (ret != (int)WiFiError.None)
+
+            context.Post((x) =>
             {
-                Log.Error(Globals.LogTag, "Failed to disconnect wifi, Error - " + (WiFiError)ret);
-                WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
-            }
+                Log.Info(Globals.LogTag, "Interop.WiFi.Disconnect");
+                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, "Exception on Disconnect\n" + e.ToString());
+                    task.SetException(e);
+                }
+            }, null);
+
             return task.Task;
         }
 
index ea34275e65ffe4d85630cfe49e8b7e818f57e70a..1fd57bc05f8452eb93ebc8632a4e9238eff74d2d 100755 (executable)
@@ -219,11 +219,8 @@ namespace Tizen.Network.WiFi
             if (_disposed)
                 return;
 
-            if (disposing)
-            {
-                Interop.WiFi.Config.Destroy(_configHandle);
-                _configHandle = IntPtr.Zero;
-            }
+            Interop.WiFi.Config.Destroy(_configHandle);
+            _configHandle = IntPtr.Zero;
             _disposed = true;
         }
 
index 96d613c7e51fcd7a456f634df42705b95be5b449..5cbdad55d36bc1d34fdb3100d0fd2335b2f39101 100755 (executable)
@@ -29,7 +29,7 @@ namespace Tizen.Network.WiFi
     [EditorBrowsable(EditorBrowsableState.Never)]
     public sealed class SafeWiFiManagerHandle : SafeHandle
     {
-        private int tid;
+        private int _tid;
 
         internal SafeWiFiManagerHandle() : base(IntPtr.Zero, true)
         {
@@ -53,18 +53,15 @@ namespace Tizen.Network.WiFi
         /// </summary>
         protected override bool ReleaseHandle()
         {
-            Interop.WiFi.Deinitialize(tid, this.handle);
+            Interop.WiFi.Deinitialize(_tid, this.handle);
             this.SetHandle(IntPtr.Zero);
             return true;
         }
 
-        internal int TID
+        internal void SetTID(int id)
         {
-            set
-            {
-                tid = value;
-                Log.Info(Globals.LogTag, "New Handle for Thread " + tid);
-            }
+            _tid = id;
+            Log.Info(Globals.LogTag, "New Handle for Thread " + _tid);
         }
     }
 
index 9349cb287bb85b5de64ee611a1889c13fd20b01d..ee356f56bdd846775ea29d2040fe8a2ab9ae33b2 100755 (executable)
@@ -19,6 +19,7 @@ using System.Collections.Generic;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Runtime.InteropServices;
+using Tizen.Applications;
 
 namespace Tizen.Network.WiFi
 {
@@ -48,7 +49,12 @@ namespace Tizen.Network.WiFi
     {
         private static readonly Lazy<WiFiManagerImpl> _instance =
             new Lazy<WiFiManagerImpl>(() => new WiFiManagerImpl());
-        private Dictionary<IntPtr, Interop.WiFi.VoidCallback> _callback_map = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
+
+        private TizenSynchronizationContext context = new TizenSynchronizationContext();
+        
+        private Dictionary<IntPtr, Interop.WiFi.VoidCallback> _callback_map =
+            new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
+        
         private int _requestId = 0;
         private string _macAddress;
 
@@ -108,6 +114,7 @@ namespace Tizen.Network.WiFi
         {
             get
             {
+                Log.Info(Globals.LogTag, "IsActive");
                 bool active;
                 int ret = Interop.WiFi.IsActive(GetSafeHandle(), out active);
                 if (ret != (int)WiFiError.None)
@@ -153,7 +160,7 @@ namespace Tizen.Network.WiFi
                 Log.Error(Globals.LogTag, "Failed to initialize wifi, Error - " + (WiFiError)ret);
                 WiFiErrorFactory.ThrowWiFiException(ret, "http://tizen.org/privilege/network.get");
             }
-            handle.TID = tid;
+            handle.SetTID(tid);
             return handle;
         }
 
@@ -288,7 +295,7 @@ namespace Tizen.Network.WiFi
                 id = (IntPtr)_requestId++;
                 _callback_map[id] = (error, key) =>
                 {
-                    Log.Info(Globals.LogTag, "wifi activated");
+                    Log.Info(Globals.LogTag, "ActivateAsync done");
                     if (error != (int)WiFiError.None)
                     {
                         Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
@@ -304,18 +311,32 @@ namespace Tizen.Network.WiFi
                     }
                 };
             }
-            int ret = Interop.WiFi.Activate(GetSafeHandle(), _callback_map[id], id);
-            if (ret != (int)WiFiError.None)
+
+            context.Post((x) =>
             {
-                Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
-                WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
-            }
+                Log.Info(Globals.LogTag, "Interop.WiFi.ActivateAsync");
+                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, "Exception on ActivateAsync\n" + e.ToString());
+                    task.SetException(e);
+                }
+            }, null);
+
             return task.Task;
         }
 
         internal Task ActivateWithWiFiPickerTestedAsync()
         {
-            Log.Debug(Globals.LogTag, "ActivateWithWiFiPickerTestedAsync");
+            Log.Info(Globals.LogTag, "ActivateWithWiFiPickerTestedAsync");
             TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
             IntPtr id;
             lock (_callback_map)
@@ -323,7 +344,7 @@ namespace Tizen.Network.WiFi
                 id = (IntPtr)_requestId++;
                 _callback_map[id] = (error, key) =>
                 {
-                    Log.Debug(Globals.LogTag, "Activation finished");
+                    Log.Info(Globals.LogTag, "ActivateWithWiFiPickerTestedAsync done");
                     if (error != (int)WiFiError.None)
                     {
                         Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
@@ -339,12 +360,26 @@ namespace Tizen.Network.WiFi
                     }
                 };
             }
-            int ret = Interop.WiFi.ActivateWithWiFiPickerTested(GetSafeHandle(), _callback_map[id], id);
-            if (ret != (int)WiFiError.None)
+
+            context.Post((x) =>
             {
-                Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
-                WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
-            }
+                Log.Info(Globals.LogTag, "Interop.WiFi.ActivateWithWiFiPickerTestedAsync");
+                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);
+                        WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
+                    }
+                }
+                catch (Exception e)
+                {
+                    Log.Error(Globals.LogTag, "Exception on ActivateWithWiFiPickerTestedAsync\n" + e.ToString());
+                    task.SetException(e);
+                }
+            }, null);
+            
             return task.Task;
         }
 
@@ -358,7 +393,7 @@ namespace Tizen.Network.WiFi
                 id = (IntPtr)_requestId++;
                 _callback_map[id] = (error, key) =>
                 {
-                    Log.Info(Globals.LogTag, "Deactivation finished");
+                    Log.Info(Globals.LogTag, "DeactivateAsync done");
                     if (error != (int)WiFiError.None)
                     {
                         Log.Error(Globals.LogTag, "Error occurs during WiFi deactivating, " + (WiFiError)error);
@@ -374,13 +409,27 @@ namespace Tizen.Network.WiFi
                     }
                 };
             }
-            int ret = Interop.WiFi.Deactivate(GetSafeHandle(), _callback_map[id], id);
-            if (ret != (int)WiFiError.None)
+
+            context.Post((x) =>
             {
-                Log.Error(Globals.LogTag, "Failed to deactivate wifi, Error - " + (WiFiError)ret);
-                WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
-            }
-            return task.Task;
+                Log.Info(Globals.LogTag, "Interop.WiFi.Deactivate");
+                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, "Exception on Deactivate\n" + e.ToString());
+                    task.SetException(e);
+                }
+            }, null);
+
+            return task.Task;           
         }
 
         internal Task ScanAsync()
@@ -393,7 +442,7 @@ namespace Tizen.Network.WiFi
                 id = (IntPtr)_requestId++;
                 _callback_map[id] = (error, key) =>
                 {
-                    Log.Info(Globals.LogTag, "Scanning finished");
+                    Log.Info(Globals.LogTag, "ScanAsync done");
                     if (error != (int)WiFiError.None)
                     {
                         Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
@@ -409,12 +458,26 @@ namespace Tizen.Network.WiFi
                     }
                 };
             }
-            int ret = Interop.WiFi.Scan(GetSafeHandle(), _callback_map[id], id);
-            if (ret != (int)WiFiError.None)
+
+            context.Post((x) =>
             {
-                Log.Error(Globals.LogTag, "Failed to scan all AP, Error - " + (WiFiError)ret);
-                WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
-            }
+                Log.Info(Globals.LogTag, "Interop.WiFi.Scan");
+                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);
+                        WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
+                    }
+                }
+                catch (Exception e)
+                {
+                    Log.Error(Globals.LogTag, "Exception on Scan\n" + e.ToString());
+                    task.SetException(e);
+                }
+            }, null);
+
             return task.Task;
         }
 
@@ -428,7 +491,7 @@ namespace Tizen.Network.WiFi
                 id = (IntPtr)_requestId++;
                 _callback_map[id] = (error, key) =>
                 {
-                    Log.Info(Globals.LogTag, "Scanning with specific AP finished");
+                    Log.Info(Globals.LogTag, "ScanSpecificAPAsync Done " + essid);
                     if (error != (int)WiFiError.None)
                     {
                         Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
@@ -444,12 +507,26 @@ namespace Tizen.Network.WiFi
                     }
                 };
             }
-            int ret = Interop.WiFi.ScanSpecificAP(GetSafeHandle(), essid, _callback_map[id], id);
-            if (ret != (int)WiFiError.None)
+
+            context.Post((x) =>
             {
-                Log.Error(Globals.LogTag, "Failed to scan with specific AP, Error - " + (WiFiError)ret);
-                WiFiErrorFactory.ThrowWiFiException(ret, GetSafeHandle().DangerousGetHandle());
-            }
+                Log.Info(Globals.LogTag, "Interop.WiFi.ScanSpecificAPAsync");
+                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, "Exception on ScanSpecificAPAsync\n" + e.ToString());
+                    task.SetException(e);
+                }
+            }, null);
+
             return task.Task;
         }
 
index ac1eabf382a43f1aad2118c565550d82f5d45697..4c39c98d53d30ac93d121c000b6cc27add169d56 100755 (executable)
@@ -16,6 +16,8 @@
 
 using System;
 using System.Threading.Tasks;
+using System.Threading;
+using Tizen.Applications;
 
 namespace Tizen.Network.WiFi
 {
@@ -56,19 +58,39 @@ namespace Tizen.Network.WiFi
         {
             add
             {
-                if (_deviceStateChanged == null)
+                context.Post((x) =>
                 {
-                    RegisterDeviceStateChangedEvent();
-                }
-                _deviceStateChanged += value;
+                    if (_deviceStateChanged == null)
+                    {
+                        try
+                        {
+                            RegisterDeviceStateChangedEvent();
+                        } catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on adding DeviceStateChanged\n" + e.ToString());
+                            return;
+                        }
+                    }
+                    _deviceStateChanged += value;
+                }, null);
             }
             remove
             {
-                _deviceStateChanged -= value;
-                if (_deviceStateChanged == null)
+                context.Post((x) =>
                 {
-                    UnregisterDeviceStateChangedEvent();
-                }
+                    _deviceStateChanged -= value;
+                    if (_deviceStateChanged == null)
+                    {
+                        try
+                        {
+                            UnregisterDeviceStateChangedEvent();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on removing DeviceStateChanged\n" + e.ToString());
+                        }
+                    }
+                }, null);
             }
         }
 
@@ -76,19 +98,40 @@ namespace Tizen.Network.WiFi
         {
             add
             {
-                if (_connectionStateChanged == null)
+                context.Post((x) =>
                 {
-                    RegisterConnectionStateChangedEvent();
-                }
-                _connectionStateChanged += value;
+                    if (_connectionStateChanged == null)
+                    {
+                        try
+                        {
+                            RegisterConnectionStateChangedEvent();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on adding ConnectionStateChanged\n" + e.ToString());
+                            return;
+                        }
+                    }
+                    _connectionStateChanged += value;
+                }, null);
             }
             remove
             {
-                _connectionStateChanged -= value;
-                if (_connectionStateChanged == null)
+                context.Post((x) =>
                 {
-                    UnregisterConnectionStateChangedEvent();
-                }
+                    _connectionStateChanged -= value;
+                    if (_connectionStateChanged == null)
+                    {
+                        try
+                        {
+                            UnregisterConnectionStateChangedEvent();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on removing ConnectionStateChanged\n" + e.ToString());
+                        }
+                    }
+                }, null);
             }
         }
 
@@ -96,19 +139,40 @@ namespace Tizen.Network.WiFi
         {
             add
             {
-                if (_rssiLevelChanged == null)
+                context.Post((x) =>
                 {
-                    RegisterRssiLevelChangedEvent();
-                }
-                _rssiLevelChanged += value;
+                    if (_rssiLevelChanged == null)
+                    {
+                        try
+                        {
+                            RegisterRssiLevelChangedEvent();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on adding RssiLevelChanged\n" + e.ToString());
+                            return;
+                        }
+                    }
+                    _rssiLevelChanged += value;
+                }, null);
             }
             remove
             {
-                _rssiLevelChanged -= value;
-                if (_rssiLevelChanged == null)
+                context.Post((x) =>
                 {
-                    UnregisterRssiLevelChangedEvent();
-                }
+                    _rssiLevelChanged -= value;
+                    if (_rssiLevelChanged == null)
+                    {
+                        try
+                        {
+                            UnregisterRssiLevelChangedEvent();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on removing RssiLevelChanged\n" + e.ToString());
+                        }
+                    }
+                }, null);
             }
         }
 
@@ -116,24 +180,46 @@ namespace Tizen.Network.WiFi
         {
             add
             {
-                if (_backgroundScanFinished == null)
+                context.Post((x) =>
                 {
-                    RegisterBackgroundScanFinishedEvent();
-                }
-                _backgroundScanFinished += value;
+                    if (_backgroundScanFinished == null)
+                    {
+                        try
+                        {
+                            RegisterBackgroundScanFinishedEvent();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on adding BackgroundScanFinished\n" + e.ToString());
+                            return;
+                        }
+                    }
+                    _backgroundScanFinished += value;
+                }, null);
             }
             remove
             {
-                _backgroundScanFinished -= value;
-                if (_backgroundScanFinished == null)
+                context.Post((x) =>
                 {
-                    UnregisterBackgroundScanFinishedEvent();
-                }
+                    _backgroundScanFinished -= value;
+                    if (_backgroundScanFinished == null)
+                    {
+                        try
+                        {
+                            UnregisterBackgroundScanFinishedEvent();
+                        }
+                        catch (Exception e)
+                        {
+                            Log.Error(Globals.LogTag, "Exception on removing BackgroundScanFinished\n" + e.ToString());
+                        }
+                    }
+                }, null);
             }
         }
 
         private void RegisterDeviceStateChangedEvent()
         {
+            Log.Info(Globals.LogTag, "RegisterDeviceStateChangedEvent in Thread " + Thread.CurrentThread.ManagedThreadId);
             _deviceChangedCallback = (int deviceState, IntPtr userDate) =>
             {
                 WiFiDeviceState state = (WiFiDeviceState)deviceState;
@@ -149,6 +235,7 @@ namespace Tizen.Network.WiFi
 
         private void UnregisterDeviceStateChangedEvent()
         {
+            Log.Info(Globals.LogTag, "UnregisterDeviceStateChangedEvent in Thread " + Thread.CurrentThread.ManagedThreadId);
             int ret = Interop.WiFi.UnsetDeviceStateChangedCallback(GetSafeHandle());
             if (ret != (int)WiFiError.None)
             {