Skip to content

Commit

Permalink
Add C# internal APIs (#6064)
Browse files Browse the repository at this point in the history
* Add C# internal APIs

API List:
bt_adapter_set_authentication_req_cb
bt_adapter_unset_authentication_req_cb
bt_adapter_passkey_confirmation_reply

Signed-off-by: Anuj Kumar Singh <anujk.singh@samsung.com>

* Add C# internal APIs

API List:
bt_adapter_set_authentication_req_cb
bt_adapter_unset_authentication_req_cb
bt_adapter_passkey_confirmation_reply

Signed-off-by: Anuj Kumar Singh <anujk.singh@samsung.com>

* add invisible tag to function the public class AuthenticationRequestedEventArgs

Signed-off-by: anujk-singh <anujk.singh@samsung.com>

---------

Signed-off-by: Anuj Kumar Singh <anujk.singh@samsung.com>
Signed-off-by: anujk-singh <anujk.singh@samsung.com>
  • Loading branch information
anujk-singh committed Apr 29, 2024
1 parent 8e0e95a commit c82878e
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Tizen.Network.Bluetooth/Interop/Interop.Bluetooth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ internal static partial class Bluetooth
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void StateChangedCallback(int result, int state, IntPtr userData);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void AuthenticationRequestedCallback(int result, AuthenticationInfoType authType, string deviceName, string remoteAddr, string passKey, IntPtr userData);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void NameChangedCallback(string deviceName, IntPtr userData);
Expand Down Expand Up @@ -168,6 +170,12 @@ internal static partial class Bluetooth
internal static extern int SetDiscoveryStateChangedCallback(DiscoveryStateChangedCallback discoveryChangedCb, IntPtr userData);
[DllImport(Libraries.Bluetooth, EntryPoint = "bt_adapter_unset_device_discovery_state_changed_cb")]
internal static extern int UnsetDiscoveryStateChangedCallback();
[DllImport(Libraries.Bluetooth, EntryPoint = "bt_adapter_passkey_confirmation_reply")]
internal static extern int PasskeyConfirmationReply(bool confirmationReply);
[DllImport(Libraries.Bluetooth, EntryPoint = "bt_adapter_set_authentication_req_cb")]
internal static extern int SetAuthenticationRequestedCallback(AuthenticationRequestedCallback stateChangedCb, IntPtr userData);
[DllImport(Libraries.Bluetooth, EntryPoint = "bt_adapter_unset_authentication_req_cb")]
internal static extern int UnsetAuthenticationRequestedCallback();

//Bluetooth Device
[DllImport(Libraries.Bluetooth, EntryPoint = "bt_device_create_bond")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,39 @@ static public int RemainingTimeAsVisible
}
}

/// <summary>
/// The AuthenticationChanged event is raised when the Bluetooth adapter authentication is changed.
/// </summary>
/// <exception cref="NotSupportedException">Thrown when the Bluetooth is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown when the Bluetooth is not enabled.</exception>
/// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
static public event EventHandler<AuthenticationRequestedEventArgs> AuthenticationChanged
{
add
{
try
{
BluetoothAdapterImpl.Instance.AuthenticationChanged += value;
}
catch (TypeInitializationException e)
{
throw e.InnerException;
}
}
remove
{
try
{
BluetoothAdapterImpl.Instance.AuthenticationChanged -= value;
}
catch (TypeInitializationException e)
{
throw e.InnerException;
}
}
}

/// <summary>
/// The NameChanged event is raised when the Bluetooth adapter name is changed.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ internal partial class BluetoothAdapterImpl : IDisposable
private event EventHandler<VisibilityModeChangedEventArgs> _visibilityModeChanged;
private event EventHandler<VisibilityDurationChangedEventArgs> _visibilityDurationChanged;
private event EventHandler<DiscoveryStateChangedEventArgs> _discoveryStateChanged;
private event EventHandler<AuthenticationRequestedEventArgs> _authenticationRequested;

private Interop.Bluetooth.StateChangedCallback _stateChangedCallback;
private Interop.Bluetooth.NameChangedCallback _nameChangedCallback;
private Interop.Bluetooth.VisibilityModeChangedCallback _visibilityChangedCallback;
private Interop.Bluetooth.VisibilityDurationChangedCallback _visibilitydurationChangedCallback;
private Interop.Bluetooth.DiscoveryStateChangedCallback _discoveryStateChangedCallback;
private Interop.Bluetooth.BondedDeviceCallback _bondedDeviceCallback;
private Interop.Bluetooth.AuthenticationRequestedCallback _authenticationRequestedCallback;

private static readonly BluetoothAdapterImpl _instance = new BluetoothAdapterImpl();
private bool disposed = false;
Expand All @@ -68,6 +70,26 @@ internal partial class BluetoothAdapterImpl : IDisposable
}
}

internal event EventHandler<AuthenticationRequestedEventArgs> AuthenticationChanged
{
add
{
if (_authenticationRequested == null)
{
RegisterAuthenticationRequestedEvent();
}
_authenticationRequested += value;
}
remove
{
_authenticationRequested -= value;
if (_stateChanged == null)
{
UnregisterAuthenticationRequestedEvent();
}
}
}

internal event EventHandler<NameChangedEventArgs> NameChanged
{
add
Expand Down Expand Up @@ -175,6 +197,33 @@ private void UnregisterStateChangedEvent()
}
}

private void RegisterAuthenticationRequestedEvent()
{
_authenticationRequestedCallback = (int result, AuthenticationInfoType authType, string deviceName, string remoteAddr, string passKey, IntPtr userData) =>
{
if (_authenticationRequested != null)
{
AuthenticationInfoType at = authType;
BluetoothError res = (BluetoothError)result;
_authenticationRequested(null, new AuthenticationRequestedEventArgs(res, at, deviceName, remoteAddr, passKey));
}
};
int ret = Interop.Bluetooth.SetAuthenticationRequestedCallback(_authenticationRequestedCallback, IntPtr.Zero);
if (ret != (int)BluetoothError.None)
{
Log.Error(Globals.LogTag, "Failed to set authentication request callback, Error - " + (BluetoothError)ret);
}
}

private void UnregisterAuthenticationRequestedEvent()
{
int ret = Interop.Bluetooth.UnsetAuthenticationRequestedCallback();
if (ret != (int)BluetoothError.None)
{
Log.Error(Globals.LogTag, "Failed to unset authentication request callback, Error - " + (BluetoothError)ret);
}
}

private void RegisterNameChangedEvent()
{
_nameChangedCallback = (string deviceName, IntPtr userData) =>
Expand Down Expand Up @@ -577,6 +626,16 @@ internal void RemoveRemoteOobData(string deviceAddress)
}
}

internal void PasskeyConfirmationReply(bool confirmationReply)
{
int ret = Interop.Bluetooth.PasskeyConfirmationReply(confirmationReply);
if (ret != (int)BluetoothError.None)
{
Log.Error(Globals.LogTag, "Failed to passkey confirmation reply, Error - " + (BluetoothError)ret);
BluetoothErrorFactory.ThrowBluetoothException(ret);
}
}

internal BluetoothServerSocket CreateServerSocket(string serviceUuid)
{
int socketFd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1384,4 +1384,25 @@ public enum BluetoothHidParamType
/// </summary>
Output
}

/// <summary>
/// Enumeration for the authentication info type.
/// </summary>
/// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public enum AuthenticationInfoType
{
/// <summary>
/// PIN display event to user for entering PIN in keyboard
/// </summary>
AuthKeyboardPasskeyDisplay = 0,
/// <summary>
/// Legacy PIN or PASSKEY request event
/// </summary>
AuthPinRequest,
/// <summary>
/// PASSKEY confirmation event to match PASSKEY in remote device
/// </summary>
AuthPasskeyConfirmRequest,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,89 @@ public BluetoothError Result
}
}

/// <summary>
/// An extended EventArgs class contains the changed Bluetooth authentication.
/// </summary>
/// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public class AuthenticationRequestedEventArgs : EventArgs
{
private AuthenticationInfoType _authType;
private BluetoothError _result;
private string _deviceName;
private string _remoteAddr;
private string _passKey;

internal AuthenticationRequestedEventArgs(BluetoothError result, AuthenticationInfoType type, string deviceName, string remoteAddr, string passKey)
{
_authType = type;
_result = result;
_deviceName = deviceName;
_remoteAddr = remoteAddr;
_passKey = passKey;
}

/// <summary>
/// The authentication of Bluetooth.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public AuthenticationInfoType Authentication
{
get
{
return _authType;
}
}

/// <summary>
/// The BluetoothError result.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public BluetoothError Result
{
get
{
return _result;
}
}

/// <summary>
/// The name of the device.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public string DeviceName
{
get
{
return _deviceName;
}
}

/// <summary>
/// The Remote address of the device.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public string RemoteAddr
{
get
{
return _remoteAddr;
}
}

/// <summary>
/// The pass key of the device.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public string PassKey
{
get
{
return _passKey;
}
}
}

/// <summary>
/// An extended EventArgs class contains the changed Bluetooth name.
/// </summary>
Expand Down

0 comments on commit c82878e

Please sign in to comment.