Permalink
Browse files

Updated wrapper api to match warble's noppl branch

  • Loading branch information...
scaryghost committed Aug 16, 2018
1 parent 298394f commit 11c19bc8d3e732e464c4956e4321a19cbbf05de7
Showing with 45 additions and 13 deletions.
  1. +1 −1 Example.ReadDevInfo/ReadDevInfo.cs
  2. +3 −0 Warble.NetStandard/Bindings.cs
  3. +25 −0 Warble.NetStandard/Gatt.cs
  4. +15 −11 Warble.NetStandard/GattChar.cs
  5. +1 −1 clibs/warble
@@ -23,7 +23,7 @@ class Program {
};
foreach(var id in uuids) {
var gattchar = gatt.FindCharacteristic(id);
var gattchar = await gatt.FindCharacteristicAsync("0000180a-0000-1000-8000-00805f9b34fb", id);
Console.Write(mac + " -> ");
if (gattchar == null) {
@@ -95,6 +95,9 @@ internal struct Option {
[DllImport(WARBLE_DLL, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr warble_gatt_find_characteristic(IntPtr gatt, [MarshalAs(UnmanagedType.LPStr)] string uuid);
[DllImport(WARBLE_DLL, CallingConvention = CallingConvention.Cdecl)]
internal static extern void warble_gatt_find_characteristic_async(IntPtr gatt, [MarshalAs(UnmanagedType.LPStr)] string service, [MarshalAs(UnmanagedType.LPStr)] string uuid, IntPtr context, FnVoid_VoidP_WarbleGattCharP_CharP handler);
[DllImport(WARBLE_DLL, CallingConvention = CallingConvention.Cdecl)]
internal static extern int warble_gatt_has_service(IntPtr gatt, [MarshalAs(UnmanagedType.LPStr)] string uuid);
@@ -76,6 +76,10 @@ public class Gatt {
/// Disconnects from the remote device
/// </summary>
public void Disconnect() {
foreach (var _ in Characteristics) {
_.Value.OnNotificationReceived = null;
}
warble_gatt_disconnect(WarbleGatt);
}
@@ -97,6 +101,27 @@ public class Gatt {
return gattchar;
}
public async Task<GattChar> FindCharacteristicAsync(string service, string uuid) {
if (!Characteristics.TryGetValue(uuid, out var gattchar)) {
TaskCompletionSource<IntPtr> warbleTaskSrc = new TaskCompletionSource<IntPtr>();
var handler = new FnVoid_VoidP_WarbleGattCharP_CharP((ctx, caller, pointer) => {
if (caller == IntPtr.Zero) {
warbleTaskSrc.SetException(new WarbleException(pointer));
} else {
warbleTaskSrc.SetResult(caller);
}
});
warble_gatt_find_characteristic_async(WarbleGatt, service, uuid, IntPtr.Zero, handler);
var result = await warbleTaskSrc.Task;
gattchar = result == IntPtr.Zero ? null : new GattChar(result);
Characteristics.Add(uuid, gattchar);
}
return gattchar;
}
/// <summary>
/// Check if a GATT service with the corresponding UUID exists on the device
/// </summary>
@@ -15,22 +15,26 @@ public class GattChar {
/// <summary>
/// Handler to process characteristic notifications, <see cref="EnableNotificationsAsync"/>
/// </summary>
public Action<byte[]> OnNotificationReceived { get; set; }
public Action<byte[]> OnNotificationReceived {
set {
if (value != null) {
WarbleNotifyHandler = new FnVoid_VoidP_WarbleGattCharP_UbyteP_Ubyte((context, caller, pointer, size) => {
byte[] managedArray = new byte[size];
Marshal.Copy(pointer, managedArray, 0, size);
value.Invoke(managedArray);
});
warble_gattchar_on_notification_received(WarbleGattChar, IntPtr.Zero, WarbleNotifyHandler);
} else {
warble_gattchar_on_notification_received(WarbleGattChar, IntPtr.Zero, null);
}
}
}
private readonly IntPtr WarbleGattChar;
private readonly FnVoid_VoidP_WarbleGattCharP_UbyteP_Ubyte WarbleNotifyHandler;
private FnVoid_VoidP_WarbleGattCharP_UbyteP_Ubyte WarbleNotifyHandler;
internal GattChar(IntPtr pointer) {
WarbleGattChar = pointer;
WarbleNotifyHandler = new FnVoid_VoidP_WarbleGattCharP_UbyteP_Ubyte((context, caller, value, value_size) => {
if (OnNotificationReceived != null) {
byte[] managedArray = new byte[value_size];
Marshal.Copy(value, managedArray, 0, value_size);
OnNotificationReceived(managedArray);
}
});
warble_gattchar_on_notification_received(WarbleGattChar, IntPtr.Zero, WarbleNotifyHandler);
}
private async Task WriteAsync(Action<FnVoid_VoidP_WarbleGattCharP_CharP> fn) {

0 comments on commit 11c19bc

Please sign in to comment.