[Bluetooth][TCSACR-437] Add GATT related APIs (#3377)
authorWootak Jung <wootak.jung@samsung.com>
Sun, 29 Aug 2021 23:10:37 +0000 (08:10 +0900)
committerGitHub <noreply@github.com>
Sun, 29 Aug 2021 23:10:37 +0000 (08:10 +0900)
BluetoothGattServer.GetAttMtu(string clientAddress)
BluetoothGattClient.ServiceChanged
ServiceChangedEventArgs
BluetoothGattServiceChangeType

Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
src/Tizen.Network.Bluetooth/Interop/Interop.Bluetooth.cs
src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEnumerations.cs
src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEventArgs.cs
src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothGatt.cs
src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothGattImpl.cs

index e87209e..52a5a97 100644 (file)
@@ -638,6 +638,9 @@ internal static partial class Interop
         [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
         internal delegate void BtGattServerAttMtuChangedCallback(IntPtr clientHandle, ref AttMtuInfoStruct mtuInfo, IntPtr userData);
 
+        [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
+        internal delegate void BtClientServiceChangedCallback(IntPtr clientHandle, BluetoothGattServiceChangeType changeType, string serviceUuid, IntPtr userData);
+
         // Gatt Attribute
 
         [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_destroy")]
@@ -704,6 +707,12 @@ internal static partial class Interop
         [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_characteristic_foreach_descriptors")]
         internal static extern int BtGattCharacteristicForeachDescriptors(BluetoothGattAttributeHandle characteristicHandle, BtGattForeachCallback callback, IntPtr userData);
 
+        [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_client_set_service_changed_cb")]
+        internal static extern int BtGattClientSetServiceChangedCallback(BluetoothGattClientHandle clientHandle, BtClientServiceChangedCallback cb, IntPtr userData);
+
+        [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_client_unset_service_changed_cb")]
+        internal static extern int BtGattClientUnsetServiceChangedCallback(BluetoothGattClientHandle clientHandle);
+
         [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_client_set_characteristic_value_changed_cb")]
         internal static extern int BtGattClientSetCharacteristicValueChangedCallback(BluetoothGattAttributeHandle characteristicHandle, BtClientCharacteristicValueChangedCallback cb, IntPtr userData);
 
@@ -820,6 +829,9 @@ internal static partial class Interop
 
         [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_server_notify_characteristic_changed_value")]
         internal static extern int BtGattServerNotify(BluetoothGattAttributeHandle characteristicHandle, BtGattServerNotificationSentCallback callback, string clientAddress, IntPtr userData);
+
+        [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_server_get_device_mtu")]
+        internal static extern int BtGattServerGetDeviceMtu(string remoteAddress, out int mtu);
     }
 }
 
index 217e03f..70c504e 100644 (file)
@@ -1308,6 +1308,22 @@ namespace Tizen.Network.Bluetooth
     }
 
     /// <summary>
+    /// Enumeration for the GATT service change type.
+    /// </summary>
+    /// <since_tizen> 9 </since_tizen>
+    public enum BluetoothGattServiceChangeType
+    {
+        /// <summary>
+        /// Service added.
+        /// </summary>
+        Added = 0,
+        /// <summary>
+        /// Service removed.
+        /// </summary>
+        Removed,
+    }
+
+    /// <summary>
     /// Enumeration for the Bluetooth HID header type.
     /// </summary>
     /// <since_tizen> 6 </since_tizen>
index b728454..ec87eaa 100644 (file)
@@ -1241,6 +1241,31 @@ namespace Tizen.Network.Bluetooth
     }
 
     /// <summary>
+    /// An extended EventArgs class contains the service changed information.
+    /// </summary>
+    /// <since_tizen> 9 </since_tizen>
+    public class ServiceChangedEventArgs : EventArgs
+    {
+        internal ServiceChangedEventArgs(BluetoothGattServiceChangeType changeType, string serviceUuid)
+        {
+            ChangeType = changeType;
+            ServiceUuid = serviceUuid;
+        }
+
+        /// <summary>
+        /// The service changed type.
+        /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        public BluetoothGattServiceChangeType ChangeType { get; }
+
+        /// <summary>
+        /// The service UUID.
+        /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        public string ServiceUuid { get; }
+    }
+
+    /// <summary>
     /// An extended EventArgs class contains the changed attribute value.
     /// </summary>
     /// <since_tizen> 3 </since_tizen>
index 56dfb9d..166e6ac 100644 (file)
@@ -217,6 +217,20 @@ namespace Tizen.Network.Bluetooth
         }
 
         /// <summary>
+        /// Gets the value of the ATT MTU(Maximum Transmission Unit) for the connection.
+        /// </summary>
+        /// <param name="clientAddress">The remote device address.</param>
+        /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.server</feature>
+        /// <returns>The MTU value</returns>
+        /// <exception cref="NotSupportedException">Thrown when the BT/BLE is not supported.</exception>
+        /// <exception cref="InvalidOperationException">Thrown when the BT/BLE is not enabled
+        /// or when the remote device is disconnected, or when other specific error occurs.</exception>
+        /// <since_tizen> 9 </since_tizen>
+        public int GetAttMtu(string clientAddress)
+        {
+            return _impl.GetAttMtu(clientAddress);
+        }
+
         /// The AttMtuChanged event is raised when the MTU value changed.
         /// </summary>
         /// <since_tizen> 9 </since_tizen>
@@ -281,6 +295,7 @@ namespace Tizen.Network.Bluetooth
             _remoteAddress = remoteAddress;
             StaticConnectionStateChanged += OnConnectionStateChanged;
             _impl.AttMtuChanged += OnAttMtuChanged;
+            _impl.ServiceChanged += OnServiceChanged;
         }
 
         private void OnAttMtuChanged(object s, AttMtuChangedEventArgs e)
@@ -288,6 +303,11 @@ namespace Tizen.Network.Bluetooth
             AttMtuChanged?.Invoke(this, e);
         }
 
+        private void OnServiceChanged(object s, ServiceChangedEventArgs e)
+        {
+            ServiceChanged?.Invoke(this, e);
+        }
+
         /// <summary>
         /// Creates the Bluetooth GATT client.
         /// </summary>
@@ -569,6 +589,13 @@ namespace Tizen.Network.Bluetooth
         [EditorBrowsable(EditorBrowsableState.Never)]
         public event EventHandler<AttMtuChangedEventArgs> AttMtuChanged;
 
+        /// <summary>
+        /// The ServiceChanged event is raised when the service is changed from the remote device(GATT server).
+        /// </summary>
+        /// <feature>http://tizen.org/feature/network.bluetooth.le.gatt.client</feature>
+        /// <since_tizen> 9 </since_tizen>
+        public event EventHandler<ServiceChangedEventArgs> ServiceChanged;
+
         internal bool Isvalid()
         {
             return _impl.GetHandle().IsInvalid == false;
index 7a623bc..42ce160 100644 (file)
@@ -155,6 +155,17 @@ namespace Tizen.Network.Bluetooth
             return task.Task;
         }
 
+        internal int GetAttMtu(string clientAddress)
+        {
+            int err = Interop.Bluetooth.BtGattServerGetDeviceMtu(clientAddress, out int mtu);
+            if (err.IsFailed())
+            {
+                GattUtil.Error(err, "Failed to get MTU value");
+                BluetoothErrorFactory.ThrowBluetoothException(err);
+            }
+            return mtu;
+        }
+
         internal event EventHandler<AttMtuChangedEventArgs> AttMtuChanged
         {
             add
@@ -214,6 +225,8 @@ namespace Tizen.Network.Bluetooth
         private Interop.Bluetooth.BtGattForeachCallback _serviceForeachCallback;
         private Interop.Bluetooth.BtGattClientAttMtuChangedCallback _attMtuChangedCallback;
         private event EventHandler<AttMtuChangedEventArgs> _attMtuChanged;
+        private Interop.Bluetooth.BtClientServiceChangedCallback _serviceChangedCallback;
+        private event EventHandler<ServiceChangedEventArgs> _serviceChanged;
 
         internal BluetoothGattClientImpl(string remoteAddress)
         {
@@ -429,6 +442,48 @@ namespace Tizen.Network.Bluetooth
             }
         }
 
+        internal event EventHandler<ServiceChangedEventArgs> ServiceChanged
+        {
+            add
+            {
+                if (_serviceChanged == null)
+                {
+                    RegisterServiceChangedEvent();
+                }
+                _serviceChanged += value;
+            }
+            remove
+            {
+                _serviceChanged -= value;
+                if (_serviceChanged == null)
+                {
+                    UnregisterServiceChangedEvent();
+                }
+            }
+        }
+
+        private void RegisterServiceChangedEvent()
+        {
+            _serviceChangedCallback = (IntPtr clientHandle, BluetoothGattServiceChangeType changeType, string serviceUuid, IntPtr userData) =>
+            {
+                _serviceChanged?.Invoke(null, new ServiceChangedEventArgs(changeType, serviceUuid));
+            };
+            int ret = Interop.Bluetooth.BtGattClientSetServiceChangedCallback(_handle, _serviceChangedCallback, IntPtr.Zero);
+            if (ret != (int)BluetoothError.None)
+            {
+                Log.Error(Globals.LogTag, "Failed to set service changed callback, Error - " + (BluetoothError)ret);
+            }
+        }
+
+        private void UnregisterServiceChangedEvent()
+        {
+            int ret = Interop.Bluetooth.BtGattClientUnsetServiceChangedCallback(_handle);
+            if (ret != (int)BluetoothError.None)
+            {
+                Log.Error(Globals.LogTag, "Failed to unset service changed callback, Error - " + (BluetoothError)ret);
+            }
+        }
+
         internal BluetoothGattClientHandle GetHandle()
         {
             return _handle;