From c82878e0403be66710746c97656f20b8850c5c34 Mon Sep 17 00:00:00 2001 From: anujk-singh <115205617+anujk-singh@users.noreply.github.com> Date: Mon, 29 Apr 2024 12:45:38 +0530 Subject: [PATCH] Add C# internal APIs (#6064) * 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 * 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 * add invisible tag to function the public class AuthenticationRequestedEventArgs Signed-off-by: anujk-singh --------- Signed-off-by: Anuj Kumar Singh Signed-off-by: anujk-singh --- .../Interop/Interop.Bluetooth.cs | 8 +++ .../Tizen.Network.Bluetooth/BluetoothAdapter.cs | 33 +++++++++ .../BluetoothAdapterImpl.cs | 59 +++++++++++++++ .../BluetoothEnumerations.cs | 21 ++++++ .../Tizen.Network.Bluetooth/BluetoothEventArgs.cs | 83 ++++++++++++++++++++++ 5 files changed, 204 insertions(+) diff --git a/src/Tizen.Network.Bluetooth/Interop/Interop.Bluetooth.cs b/src/Tizen.Network.Bluetooth/Interop/Interop.Bluetooth.cs index 52a5a97..78c549b 100644 --- a/src/Tizen.Network.Bluetooth/Interop/Interop.Bluetooth.cs +++ b/src/Tizen.Network.Bluetooth/Interop/Interop.Bluetooth.cs @@ -24,6 +24,8 @@ internal static partial class Interop { [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); @@ -168,6 +170,12 @@ internal static partial class Interop 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")] diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapter.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapter.cs index dd1f4bb..5ec27d9 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapter.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapter.cs @@ -213,6 +213,39 @@ namespace Tizen.Network.Bluetooth } /// + /// The AuthenticationChanged event is raised when the Bluetooth adapter authentication is changed. + /// + /// Thrown when the Bluetooth is not supported. + /// Thrown when the Bluetooth is not enabled. + /// 9 + [EditorBrowsable(EditorBrowsableState.Never)] + static public event EventHandler 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; + } + } + } + + /// /// The NameChanged event is raised when the Bluetooth adapter name is changed. /// /// Thrown when the Bluetooth is not supported. diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs index d74b045..198bb14 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs @@ -37,6 +37,7 @@ namespace Tizen.Network.Bluetooth private event EventHandler _visibilityModeChanged; private event EventHandler _visibilityDurationChanged; private event EventHandler _discoveryStateChanged; + private event EventHandler _authenticationRequested; private Interop.Bluetooth.StateChangedCallback _stateChangedCallback; private Interop.Bluetooth.NameChangedCallback _nameChangedCallback; @@ -44,6 +45,7 @@ namespace Tizen.Network.Bluetooth 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; @@ -68,6 +70,26 @@ namespace Tizen.Network.Bluetooth } } + internal event EventHandler AuthenticationChanged + { + add + { + if (_authenticationRequested == null) + { + RegisterAuthenticationRequestedEvent(); + } + _authenticationRequested += value; + } + remove + { + _authenticationRequested -= value; + if (_stateChanged == null) + { + UnregisterAuthenticationRequestedEvent(); + } + } + } + internal event EventHandler NameChanged { add @@ -175,6 +197,33 @@ namespace Tizen.Network.Bluetooth } } + 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) => @@ -577,6 +626,16 @@ namespace Tizen.Network.Bluetooth } } + 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; diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEnumerations.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEnumerations.cs index 3f070d4..8efab31 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEnumerations.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEnumerations.cs @@ -1384,4 +1384,25 @@ namespace Tizen.Network.Bluetooth /// Output } + + /// + /// Enumeration for the authentication info type. + /// + /// 9 + [EditorBrowsable(EditorBrowsableState.Never)] + public enum AuthenticationInfoType + { + /// + /// PIN display event to user for entering PIN in keyboard + /// + AuthKeyboardPasskeyDisplay = 0, + /// + /// Legacy PIN or PASSKEY request event + /// + AuthPinRequest, + /// + /// PASSKEY confirmation event to match PASSKEY in remote device + /// + AuthPasskeyConfirmRequest, + } } diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEventArgs.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEventArgs.cs index ec87eaa..fe4f095 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEventArgs.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEventArgs.cs @@ -61,6 +61,89 @@ namespace Tizen.Network.Bluetooth } /// + /// An extended EventArgs class contains the changed Bluetooth authentication. + /// + /// 9 + [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; + } + + /// + /// The authentication of Bluetooth. + /// + /// 9 + public AuthenticationInfoType Authentication + { + get + { + return _authType; + } + } + + /// + /// The BluetoothError result. + /// + /// 9 + public BluetoothError Result + { + get + { + return _result; + } + } + + /// + /// The name of the device. + /// + /// 9 + public string DeviceName + { + get + { + return _deviceName; + } + } + + /// + /// The Remote address of the device. + /// + /// 9 + public string RemoteAddr + { + get + { + return _remoteAddr; + } + } + + /// + /// The pass key of the device. + /// + /// 9 + public string PassKey + { + get + { + return _passKey; + } + } + } + + /// /// An extended EventArgs class contains the changed Bluetooth name. /// /// 3 -- 2.7.4