Skip to content

Commit

Permalink
[Tizen.Network.WiFi] Remove thread local support (#6659)
Browse files Browse the repository at this point in the history
* [WiFi] Remove TizenSynchronizationContext.post()

Signed-off-by: Akash Kumar <[email protected]>

* Remove thread local

Signed-off-by: Akash Kumar <[email protected]>

---------

Signed-off-by: Akash Kumar <[email protected]>
  • Loading branch information
akash1-kumar authored Feb 24, 2025
1 parent 52bd644 commit ea9b34b
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 374 deletions.
1 change: 0 additions & 1 deletion src/Tizen.Network.WiFi/Tizen.Network.WiFi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

<ItemGroup>
<ProjectReference Include="..\Tizen.Network.Connection\Tizen.Network.Connection.csproj" />
<ProjectReference Include="..\Tizen.Applications.Common\Tizen.Applications.Common.csproj" />
</ItemGroup>

</Project>
222 changes: 102 additions & 120 deletions src/Tizen.Network.WiFi/Tizen.Network.WiFi/WiFiAP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Tizen.Applications;

namespace Tizen.Network.WiFi
{
Expand All @@ -38,8 +37,6 @@ public class WiFiAP : IDisposable

private static TaskCompletionSource<WiFiAP> wpsWithoutSsidTask = null;
private static Dictionary<IntPtr, TaskCompletionSource<bool>> _wpsTaskMap = new Dictionary<IntPtr, TaskCompletionSource<bool>>();
private TizenSynchronizationContext context = new TizenSynchronizationContext();
private static TizenSynchronizationContext s_context = new TizenSynchronizationContext();

/// <summary>
/// The network information of the access point (AP).
Expand Down Expand Up @@ -255,32 +252,29 @@ public Task ConnectAsync()
};
}

context.Post((x) =>
Log.Info(Globals.LogTag, "Interop.WiFi.Connect");
try
{
Log.Info(Globals.LogTag, "Interop.WiFi.Connect");
try
int ret = (int)WiFiError.None;
lock (_callback_map)
{
int ret = (int)WiFiError.None;
lock (_callback_map)
{
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);
if (ret == (int)WiFiError.InvalidParameterError)
{
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
}
ret = Interop.WiFi.Connect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
}
catch (Exception e)
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Exception on ConnectAsync\n" + e);
task.SetException(e);
Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
if (ret == (int)WiFiError.InvalidParameterError)
{
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
}
}, null);
}
catch (Exception e)
{
Log.Error(Globals.LogTag, "Exception on ConnectAsync\n" + e);
task.SetException(e);
}

return task.Task;
}
Expand Down Expand Up @@ -345,47 +339,44 @@ public Task ConnectWpsAsync(WpsInfo info)
};
}

context.Post((x) =>
try
{
try
int ret = -1;
if (info.GetType() == typeof(WpsPbcInfo))
{
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))
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)
{
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);
throw new ArgumentNullException("Wps pin should not be null");
}

if (ret != (int)WiFiError.None)
if (pinInfo.GetWpsPin().Length == 0 || pinInfo.GetWpsPin().Length > 8)
{
Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
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);
}
catch (Exception e)

if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Exception on ConnectWpsAsync\n" + e);
wpsTask.SetException(e);
Log.Info(Globals.LogTag, "Remove task for ConnectWpsAsync");
_wpsTaskMap.Remove(_apHandle);
Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
}
}, null);
}
catch (Exception e)
{
Log.Error(Globals.LogTag, "Exception on ConnectWpsAsync\n" + e);
wpsTask.SetException(e);
Log.Info(Globals.LogTag, "Remove task for ConnectWpsAsync");
_wpsTaskMap.Remove(_apHandle);
}

return wpsTask.Task;
}
Expand Down Expand Up @@ -445,47 +436,44 @@ public static Task<WiFiAP> ConnectWpsWithoutSsidAsync(WpsInfo info)
};
}

s_context.Post((x) =>
try
{
try
int ret = -1;
if (info.GetType() == typeof(WpsPbcInfo))
{
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))
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)
{
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);
throw new ArgumentNullException("Wps pin should not be null");
}

if (ret != (int)WiFiError.None)
if (pinInfo.GetWpsPin().Length != 4 && pinInfo.GetWpsPin().Length != 8)
{
Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
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);
}
catch (Exception e)

if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Exception on ConnectWpsWithoutSsidAsync\n" + e);
wpsWithoutSsidTask.SetException(e);
wpsWithoutSsidTask = null;
Log.Info(Globals.LogTag, "task is null");
Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
}
}, null);
}
catch (Exception e)
{
Log.Error(Globals.LogTag, "Exception on ConnectWpsWithoutSsidAsync\n" + e);
wpsWithoutSsidTask.SetException(e);
wpsWithoutSsidTask = null;
Log.Info(Globals.LogTag, "task is null");
}

return wpsWithoutSsidTask.Task;
}
Expand Down Expand Up @@ -574,28 +562,25 @@ public Task DisconnectAsync()
};
}

context.Post((x) =>
Log.Info(Globals.LogTag, "Interop.WiFi.Disconnect");
try
{
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)
{
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);
if (ret == (int)WiFiError.InvalidParameterError)
{
Log.Error(Globals.LogTag, "Failed to disconnect wifi, Error - " + (WiFiError)ret);
if (ret == (int)WiFiError.InvalidParameterError)
{
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
}
catch (Exception e)
{
Log.Error(Globals.LogTag, "Exception on Disconnect\n" + e);
task.SetException(e);
}
}, null);
}
catch (Exception e)
{
Log.Error(Globals.LogTag, "Exception on Disconnect\n" + e);
task.SetException(e);
}

return task.Task;
}
Expand Down Expand Up @@ -679,28 +664,25 @@ public Task ForgetAPAsync()
};
}

context.Post((x) =>
Log.Info(Globals.LogTag, "Interop.WiFi.ForgetAP");
try
{
Log.Info(Globals.LogTag, "Interop.WiFi.ForgetAP");
try
int ret = Interop.WiFi.ForgetAP(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
if (ret != (int)WiFiError.None)
{
int ret = Interop.WiFi.ForgetAP(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
if (ret != (int)WiFiError.None)
Log.Error(Globals.LogTag, "Failed to forget wifi, Error - " + (WiFiError)ret);
if (ret == (int)WiFiError.InvalidParameterError)
{
Log.Error(Globals.LogTag, "Failed to forget wifi, Error - " + (WiFiError)ret);
if (ret == (int)WiFiError.InvalidParameterError)
{
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
}
catch (Exception e)
{
Log.Error(Globals.LogTag, "Exception on ForgetAPAsync\n" + e);
task.SetException(e);
}
}, null);
}
catch (Exception e)
{
Log.Error(Globals.LogTag, "Exception on ForgetAPAsync\n" + e);
task.SetException(e);
}

return task.Task;
}
Expand Down
Loading

0 comments on commit ea9b34b

Please sign in to comment.