internal delegate bool BtGattForeachCallback(int total, int index, IntPtr gattHandle, IntPtr userData);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
- internal delegate void BtGattServerReadValueRequestedCallback(string clientAddress, int requestId, BluetoothGattServerHandle serverHandle, BluetoothGattAttributeHandle gattHandle, int offset, IntPtr userData);
+ internal delegate void BtGattServerReadValueRequestedCallback(string clientAddress, int requestId, IntPtr serverHandle, IntPtr gattHandle, int offset, IntPtr userData);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
- internal delegate void BtGattServerWriteValueRequestedCallback(string clientAddress, int requestId, BluetoothGattServerHandle serverHandle, BluetoothGattAttributeHandle gattHandle, int offset, byte[] value, int len, IntPtr userData);
+ internal delegate void BtGattServerWriteValueRequestedCallback(string clientAddress, int requestId, IntPtr serverHandle, IntPtr gattHandle, int offset, bool response_needed, byte[] value, int len, IntPtr userData);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
- internal delegate void BtClientCharacteristicValueChangedCallback(BluetoothGattAttributeHandle characteristicHandle, byte[] value, int len, IntPtr userData);
+ internal delegate void BtClientCharacteristicValueChangedCallback(IntPtr characteristicHandle, byte[] value, int len, IntPtr userData);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
- internal delegate void BtGattServerNotificationStateChangeCallback(bool notify, BluetoothGattServerHandle serverHandle, BluetoothGattAttributeHandle characteristicHandle, IntPtr userData);
+ internal delegate void BtGattServerNotificationStateChangeCallback(bool notify, IntPtr serverHandle, IntPtr characteristicHandle, IntPtr userData);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
- internal delegate void BtGattServerNotificationSentCallback(int result, string clientAddress, BluetoothGattServerHandle serverHandle, BluetoothGattAttributeHandle characteristicHandle, bool completed, IntPtr userData);
+ internal delegate void BtGattServerNotificationSentCallback(int result, string clientAddress, IntPtr serverHandle, IntPtr characteristicHandle, bool completed, IntPtr userData);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
internal delegate void BtGattClientRequestCompletedCallback(int result, IntPtr requestHandle, IntPtr userData);
[DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_server_set_read_value_requested_cb")]
internal static extern int BtGattServerSetReadValueRequestedCallback(BluetoothGattAttributeHandle gattHandle, BtGattServerReadValueRequestedCallback callback, IntPtr userData);
- [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_server_set_read_value_requested_cb")]
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_server_set_write_value_requested_cb")]
internal static extern int BtGattServerSetWriteValueRequestedCallback(BluetoothGattAttributeHandle gattHandle, BtGattServerWriteValueRequestedCallback callback, IntPtr userData);
- [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_server_set_notification_state_change_cb")]
+ [DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_server_set_characteristic_notification_state_change_cb")]
internal static extern int BtGattServeSetNotificationStateChangeCallback(BluetoothGattAttributeHandle characteristicHandle, BtGattServerNotificationStateChangeCallback callback, IntPtr userData);
[DllImport(Libraries.Bluetooth, EntryPoint = "bt_gatt_server_start")]
/// </summary>
WriteWithResponse
}
+
+ /// <summary>
+ /// Enumerations of the remote device request types for attributes
+ /// </summary>
+ public enum BluetoothGattRequestType
+ {
+ /// <summary>
+ /// Read Requested.
+ /// </summary>
+ Read = 0,
+ /// <summary>
+ /// Write Requested.
+ /// </summary>
+ Write = 1,
+ }
}
-/*
+/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
using System;
using System.Collections.Generic;
+using System.Runtime.InteropServices;
+using System.Text;
using System.Threading.Tasks;
namespace Tizen.Network.Bluetooth
{
private static BluetoothGattServer _instance;
private BluetoothGattServerImpl _impl;
-
private BluetoothGattServer()
{
_impl = new BluetoothGattServerImpl();
/// Sends a response to the remote device as a result of a read/ write request
/// </summary>
/// <param name="requestId">The identification of a read/ write request</param>
+ /// <param name="type">The request type for read/write</param>
/// <param name="status">error value in case of failure, 0 for success</param>
/// <param name="value">Value to be sent</param>
/// <param name="offset">Fffset from where the value is read</param>
- public void SendResponse(int requestId, int status, byte[] value, int offset)
+ public void SendResponse(int requestId, BluetoothGattRequestType type, int status, byte[] value, int offset)
{
- _impl.SendResponse(requestId, status, value, offset);
+ _impl.SendResponse(requestId, (int)type, status, value, offset);
}
internal bool IsValid()
return client.Isvalid() ? client : null;
}
+ public void DestroyClient()
+ {
+ _impl.GetHandle().Dispose();
+ }
+
/// <summary>
/// Address of remote device.
/// </summary>
if (Server == null) return;
if (_writeValueRequested == null)
{
- _writeValueRequestedCallback = (clientAddress, requestId, serverHandle, gattHandle, offset, valueToWrite, len, userData) =>
+ _writeValueRequestedCallback = (clientAddress, requestId, serverHandle, gattHandle, offset, response_needed, valueToWrite, len, userData) =>
{
- _writeValueRequested?.Invoke(this, new WriteRequestedEventArgs(Server, clientAddress, requestId, valueToWrite, offset));
+ _writeValueRequested?.Invoke(this, new WriteRequestedEventArgs(Server, clientAddress, requestId, valueToWrite, offset, response_needed));
};
Impl.SetWriteValueRequestedEventCallback(_writeValueRequestedCallback);
}
/// Sets string value as specified offset
/// </summary>
/// <param name="value">value to set</param>
- /// <param name="offset">offset in the attribute value buffer</param>
- /// <exception cref="InvalidOperationException">Throws excetion if (offset + size of string value) is greater then length of value buffer</exception>
- public void SetValue(string value, int offset)
+ /// <exception cref="InvalidOperationException">Throws excetion if value is null</exception>
+ public void SetValue(string value)
{
- Impl.SetValue(value, offset);
+ if (value.Equals(null))
+ GattUtil.ThrowForError((int)BluetoothError.InvalidParameter, "value should not be null");
+
+ byte[] val = Encoding.UTF8.GetBytes(value);
+ Impl.SetValue(val);
}
/// <summary>
return attribututeList;
}
- internal void SendResponse(int requestId, int status, byte[] value, int offset)
+ internal void SendResponse(int requestId, int request_type, int status, byte[] value, int offset)
{
- int err = Interop.Bluetooth.BtGattServerSendResponse(requestId, 0, offset, status, value, value.Length);
+ int err = Interop.Bluetooth.BtGattServerSendResponse(requestId, request_type, offset, status, value, value.Length);
GattUtil.ThrowForError(err, string.Format("Failed to send response for request Id {0}", requestId));
}
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
Interop.Bluetooth.BtGattClientRequestCompletedCallback cb = (result, requestHandle, userData) =>
{
- tcs.SetResult(true);
+ if (result == (int)BluetoothError.None)
+ tcs.SetResult(true);
+ else
+ tcs.SetResult(false);
};
int err = Interop.Bluetooth.BtGattClientReadValue(handle, cb, IntPtr.Zero);
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
Interop.Bluetooth.BtGattClientRequestCompletedCallback cb = (result, requestHandle, userData) =>
{
- tcs.SetResult(true);
+ if (result == (int)BluetoothError.None)
+ tcs.SetResult(true);
+ else
+ tcs.SetResult(false);
};
int err = Interop.Bluetooth.BtGattClientWriteValue(handle, cb, IntPtr.Zero);
return strValue;
}
- internal void SetValue(string value, int offset)
- {
- byte[] byteValue = GetValue();
- byte[] strValue = Encoding.UTF8.GetBytes(value);
- int length = offset + strValue.Length;
- if (length >= byteValue.Length)
- {
- GattUtil.ThrowForError((int)BluetoothError.InvalidParameter, "Can not fit value to buffer");
- }
-
- Buffer.BlockCopy(strValue, 0, byteValue, offset, strValue.Length);
- byteValue[length] = (byte)'\0';
- SetValue(byteValue);
- }
-
internal int GetValue(IntDataType type, int offset)
{
int value;
if (_hasOwnership == true)
{
Interop.Bluetooth.BtGattClientDestroy(handle);
- Interop.Bluetooth.BtGattServerDeinitialize();
}
SetHandle(IntPtr.Zero);
return true;
{
if (_hasOwnership == true)
{
+ Interop.Bluetooth.BtGattServerDeinitialize();
Interop.Bluetooth.BtGattServerDestroy(handle);
}
SetHandle(IntPtr.Zero);