powerfun-unity/Assets/Scripts/Devices/Ble/Win/WclBleGattClient.cs
2021-06-08 10:30:26 +08:00

304 lines
12 KiB
C#

using Assets.Scripts.Ble.Win.CPPBridge;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scripts.Ble.Win
{
internal class WclBleGattClient
{
public WclBleGattClient(long address, IntPtr radio)
{
this.gattAddress = address;
this.rPtr = radio;
this.CConnectEvent = new WclBleGattClient.GATTCLIENT_CONNECT(this.OnConnected);
this.CDisconnectEvent = new WclBleGattClient.GATTCLIENT_DISCONNECT(this.OnDisconnected);
this.FCharacteristicChangedEvent = new WclBleGattClient.GATTCLIENT_ONCHANGED(this.OnCharacteristicChanged);
this.mPtr = WclBleGattClient.WCLGattClientCreate(this.CConnectEvent, this.CDisconnectEvent, this.FCharacteristicChangedEvent);
}
// Token: 0x06002086 RID: 8326
[DllImport("WclBlePluginCPP.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.I4)]
private static extern int WCLGattClientConnect([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Client, [MarshalAs(UnmanagedType.SysInt)][In] IntPtr Radio, [MarshalAs(UnmanagedType.I8)][In] long Address);
// Token: 0x06002087 RID: 8327
[DllImport("WclBlePluginCPP.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.SysInt)]
private static extern IntPtr WCLGattClientCreate([MarshalAs(UnmanagedType.FunctionPtr)][In] WclBleGattClient.GATTCLIENT_CONNECT OnConnect, [MarshalAs(UnmanagedType.FunctionPtr)][In] WclBleGattClient.GATTCLIENT_DISCONNECT OnDisconnect, [MarshalAs(UnmanagedType.FunctionPtr)][In] WclBleGattClient.GATTCLIENT_ONCHANGED OnChanged);
// Token: 0x06002088 RID: 8328
[DllImport("WclBlePluginCPP.dll", CallingConvention = CallingConvention.StdCall)]
private static extern void WCLGattClientDestroy([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Client);
// Token: 0x06002089 RID: 8329
[DllImport("WclBlePluginCPP.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.I4)]
private static extern int WCLGattClientDisconnect([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Client);
// Token: 0x0600208A RID: 8330
[DllImport("WclBlePluginCPP.dll", CallingConvention = CallingConvention.StdCall)]
private static extern void WCLGattClientFreeMem([MarshalAs(UnmanagedType.SysInt)][In] IntPtr pMem);
// Token: 0x0600208B RID: 8331
[DllImport("WclBlePluginCPP.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.I4)]
private static extern int WCLGattClientGetCharacteristics([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Client, [In] ref GattService Service, [In][Out] ref GattCharacteristics Chars);
// Token: 0x0600208C RID: 8332
[DllImport("WclBlePluginCPP.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.I4)]
private static extern int WCLGattClientGetServices([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Client, [In][Out] ref GattServices Services);
// Token: 0x0600208D RID: 8333
[DllImport("WclBlePluginCPP.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.U4)]
private static extern WclBleGattClientState WCLGattClientGetState([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Client);
// Token: 0x0600208E RID: 8334
[DllImport("WclBlePluginCPP.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.I4)]
private static extern int WCLGattClientReadCharacteristicValue([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Client, [In] ref GattCharacteristic Char, [MarshalAs(UnmanagedType.SysInt)][In][Out] ref IntPtr ppValue, [MarshalAs(UnmanagedType.U4)][In][Out] ref uint pSize);
// Token: 0x0600208F RID: 8335
[DllImport("WclBlePluginCPP.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.I4)]
private static extern int WCLGattClientSubscribeCharacteristic([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Client, [In] ref GattCharacteristic Char);
// Token: 0x06002090 RID: 8336
[DllImport("WclBlePluginCPP.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.I4)]
private static extern int WCLGattClientUnsubscribeCharacteristic([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Client, [In] ref GattCharacteristic Char);
// Token: 0x06002091 RID: 8337
[DllImport("WclBlePluginCPP.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.I4)]
private static extern int WCLGattClientWriteCharacteristicValue([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Client, [In] ref GattCharacteristic Char, [MarshalAs(UnmanagedType.SysInt)][In] IntPtr pValue, [MarshalAs(UnmanagedType.U4)][In] uint Size);
// Token: 0x170005D4 RID: 1492
// (get) Token: 0x06002092 RID: 8338 RVA: 0x00087790 File Offset: 0x00085990
public WclBleGattClientState State
{
get
{
return WclBleGattClient.WCLGattClientGetState(this.mPtr);
}
}
// Token: 0x06002093 RID: 8339 RVA: 0x000877A4 File Offset: 0x000859A4
public void Connect()
{
if (this.mPtr != IntPtr.Zero)
{
int num = WclBleGattClient.WCLGattClientConnect(this.mPtr, this.rPtr, this.gattAddress);
if (num != 0)
{
this.OnConnected(this.mPtr, num);
}
}
}
// Token: 0x06002094 RID: 8340 RVA: 0x000877F4 File Offset: 0x000859F4
public void Disconnect()
{
if (this.mPtr != IntPtr.Zero)
{
//WclBleGattClient.log.Info("Disconnect called", Array.Empty<object>());
int num = WclBleGattClient.WCLGattClientDisconnect(this.mPtr);
//WclBleGattClient.log.Info(string.Format("Disconnect {0:X} response {1}", this.gattAddress, num), Array.Empty<object>());
}
}
// Token: 0x06002095 RID: 8341 RVA: 0x00087864 File Offset: 0x00085A64
public int DiscoverCharacteristics(GattService service, out GattCharacteristics characteristics)
{
characteristics = new GattCharacteristics
{
Count = 0,
Chars = new GattCharacteristic[255]
};
return WclBleGattClient.WCLGattClientGetCharacteristics(this.mPtr, ref service, ref characteristics);
}
// Token: 0x06002096 RID: 8342 RVA: 0x000878B0 File Offset: 0x00085AB0
public int DiscoverServices(out GattServices services)
{
services = new GattServices
{
Count = 0,
Services = new GattService[255]
};
return WclBleGattClient.WCLGattClientGetServices(this.mPtr, ref services);
}
// Token: 0x06002097 RID: 8343 RVA: 0x000878F8 File Offset: 0x00085AF8
public int ReadCharacteristicValue(GattCharacteristic characteristic, out byte[] value)
{
value = null;
IntPtr zero = IntPtr.Zero;
uint num = 0U;
int num2 = WclBleGattClient.WCLGattClientReadCharacteristicValue(this.mPtr, ref characteristic, ref zero, ref num);
if (num2 == 0 && zero != IntPtr.Zero && num > 0U)
{
value = new byte[num];
Marshal.Copy(zero, value, 0, (int)num);
WclBleGattClient.WCLGattClientFreeMem(zero);
}
return num2;
}
// Token: 0x06002098 RID: 8344 RVA: 0x00087953 File Offset: 0x00085B53
public int SubscribeCharacteristic(GattCharacteristic characteristic)
{
return WclBleGattClient.WCLGattClientSubscribeCharacteristic(this.mPtr, ref characteristic);
}
// Token: 0x06002099 RID: 8345 RVA: 0x00087968 File Offset: 0x00085B68
public int WriteCharacteristic(GattCharacteristic characteristic, byte[] value)
{
IntPtr intPtr;
try
{
intPtr = Marshal.AllocHGlobal(value.Length);
}
catch
{
intPtr = IntPtr.Zero;
}
if (intPtr == IntPtr.Zero)
{
return 65537;
}
Marshal.Copy(value, 0, intPtr, value.Length);
int result = WclBleGattClient.WCLGattClientWriteCharacteristicValue(this.mPtr, ref characteristic, intPtr, (uint)value.Length);
Marshal.FreeHGlobal(intPtr);
return result;
}
// Token: 0x0600209A RID: 8346 RVA: 0x000879D4 File Offset: 0x00085BD4
public void Dispose()
{
WclBleGattClient.WCLGattClientDestroy(this.mPtr);
this.mPtr = IntPtr.Zero;
this.rPtr = IntPtr.Zero;
this.CConnectEvent = null;
this.CDisconnectEvent = null;
this.FCharacteristicChangedEvent = null;
}
// Token: 0x0600209B RID: 8347 RVA: 0x00087A0C File Offset: 0x00085C0C
private void OnCharacteristicChanged(IntPtr sender, ushort handle, IntPtr pValue, uint valueLen)
{
//Debug.Log("char changed");
if (pValue != IntPtr.Zero && valueLen > 0U)
{
byte[] array = new byte[valueLen];
Marshal.Copy(pValue, array, 0, (int)valueLen);
try
{
WclBleGattClient.GattCharacteristicChanged characteristicChanged = this.CharacteristicChanged;
if (characteristicChanged != null)
{
characteristicChanged(this, handle, array);
}
}
catch (Exception arg)
{
//WclBleGattClient.log.Error(string.Format("GattConnection characteristic changed callback processing error - {0}", arg), Array.Empty<object>());
}
}
}
// Token: 0x0600209C RID: 8348 RVA: 0x00087A80 File Offset: 0x00085C80
private void OnConnected(IntPtr client, int error)
{
try
{
Debug.Log("onconnected");
WclBleGattClient.GattConnectionChanged connected = this.Connected;
if (connected != null)
{
connected(this, error);
}
}
catch (Exception arg)
{
//WclBleGattClient.log.Error(string.Format("GattConnection connect callback processing error - {0}", arg), Array.Empty<object>());
Debug.LogError(arg);
}
}
// Token: 0x0600209D RID: 8349 RVA: 0x00087AD0 File Offset: 0x00085CD0
private void OnDisconnected(IntPtr client, int reason)
{
try
{
WclBleGattClient.GattConnectionChanged disconnected = this.Disconnected;
if (disconnected != null)
{
disconnected(this, reason);
}
}
catch (Exception arg)
{
//WclBleGattClient.log.Error(string.Format("GattConnection disconnect callback processing error - {0}", arg), Array.Empty<object>());
}
}
// Token: 0x0400130B RID: 4875
public WclBleGattClient.GattCharacteristicChanged CharacteristicChanged;
// Token: 0x0400130C RID: 4876
public WclBleGattClient.GattConnectionChanged Connected;
// Token: 0x0400130D RID: 4877
public WclBleGattClient.GattConnectionChanged Disconnected;
// Token: 0x0400130F RID: 4879
private WclBleGattClient.GATTCLIENT_CONNECT CConnectEvent;
// Token: 0x04001310 RID: 4880
private WclBleGattClient.GATTCLIENT_DISCONNECT CDisconnectEvent;
// Token: 0x04001311 RID: 4881
private WclBleGattClient.GATTCLIENT_ONCHANGED FCharacteristicChangedEvent;
// Token: 0x04001312 RID: 4882
private readonly long gattAddress;
// Token: 0x04001313 RID: 4883
private IntPtr mPtr;
// Token: 0x04001314 RID: 4884
private IntPtr rPtr;
// Token: 0x020008FB RID: 2299
// (Invoke) Token: 0x06003F5A RID: 16218
public delegate void GattCharacteristicChanged(WclBleGattClient connection, ushort handle, byte[] value);
// Token: 0x020008FC RID: 2300
// (Invoke) Token: 0x06003F5E RID: 16222
public delegate void GattConnectionChanged(WclBleGattClient connection, int error);
// Token: 0x020008FD RID: 2301
// (Invoke) Token: 0x06003F62 RID: 16226
[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = false)]
private delegate void GATTCLIENT_CONNECT([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Sender, [MarshalAs(UnmanagedType.I4)][In] int Error);
// Token: 0x020008FE RID: 2302
// (Invoke) Token: 0x06003F66 RID: 16230
[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = false)]
private delegate void GATTCLIENT_DISCONNECT([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Sender, [MarshalAs(UnmanagedType.I4)][In] int Reason);
// Token: 0x020008FF RID: 2303
// (Invoke) Token: 0x06003F6A RID: 16234
[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = false)]
private delegate void GATTCLIENT_ONCHANGED([MarshalAs(UnmanagedType.SysInt)][In] IntPtr Sender, [MarshalAs(UnmanagedType.U2)][In] ushort Handle, [MarshalAs(UnmanagedType.SysInt)][In] IntPtr Value, [MarshalAs(UnmanagedType.U4)][In] uint ValueLen);
}
}