powerfun-unity/Assets/Scripts/Devices/Ble/Win/WclBleMainThread.cs

216 lines
6.7 KiB
C#

using Assets.Scripts.Ble;
using Assets.Scripts.Ble.CPPBridge;
using Assets.Scripts.Devices.Ble;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scripts.Devices.Ble.Win
{
internal class WclBleMainThread: WclBleThread
{
internal delegate void ManagerStatusChangedCallback(WclBleMainThread thread, WclBleManagerStatus status);
private WclBleMainThread.ManagerStatusChangedCallback managerStatusChanged;
public event WclBleMainThread.ManagerStatusChangedCallback ManagerStatusChanged
{
add
{
this.managerStatusChanged += value;
}
remove
{
this.managerStatusChanged -= value;
}
}
private GattThreadStopped gattThreadStopped;
public event GattThreadStopped GattThreadStopped
{
add
{
this.gattThreadStopped += value;
}
remove
{
this.gattThreadStopped -= value;
}
}
private WclBleManager wclBleManager;
private WclBleWatcher wclWatcher;
private readonly object locker = new object();
private readonly Dictionary<BlePeripheralInfo, WclBleGattThread> gattClients = new Dictionary<BlePeripheralInfo, WclBleGattThread>();
internal WclBleMainThread()
{
WclAlertableWait.SetApcSync();
}
protected override void SetUpWorkerThread()
{
wclBleManager = new WclBleManager
{
ManagerStatusChanged = new WclBleManager.ManagerStatusChangedCallback(this.OnManagerStatusChanged)
};
var num = wclBleManager.Open();
if (num != 0)
{
Debug.LogWarning(string.Format("WCL BLE open failed: 0x{0:X8}", num));
}
wclWatcher = new WclBleWatcher
{
InfoPacketReceived = new WclBleWatcher.InfoPacketReceivedEvent(OnInfoPacketReceived),
UuidPacketReceived = new WclBleWatcher.UuidPacketReceivedEvent(OnUuidPacketReceived)
};
//Debug.Log("bbb");
}
protected override void CleanUpWorkerThread()
{
this.wclWatcher.InfoPacketReceived = null;
this.wclWatcher.UuidPacketReceived = null;
if (this.wclWatcher.Active)
{
this.wclWatcher.StopScan();
}
//this.StopGattThreads();
if (this.wclBleManager.Active)
{
this.wclBleManager.ManagerStatusChanged = null;
this.wclBleManager.Close();
}
base.ProcessPendingAPCMessages();
this.wclWatcher.Dispose();
this.wclBleManager.Dispose();
this.wclBleManager = null;
this.wclWatcher = null;
}
private void OnManagerStatusChanged(WclBleManager manager, WclBleManagerStatus status)
{
//Debug.Log(status);
//WclBleMainThread.SafeInvoke(this.managerStatusChanged, this, status);
this.managerStatusChanged(this, status);
}
private void OnInfoPacketReceived(object sender, long address, long timestamp, sbyte rssi, string name, WclBleAdvertisementType packetType, byte flags)
{
this.scanInfoReceived(this, address, name, rssi, packetType, null);
}
// Token: 0x060020E7 RID: 8423 RVA: 0x000889ED File Offset: 0x00086BED
private void OnUuidPacketReceived(object sender, long address, long timestamp, sbyte rssi, Guid uuid)
{
this.scanInfoReceived(this, address, null, rssi, WclBleAdvertisementType.Unknown, new Guid?(uuid));
}
private static void SafeInvoke(WclBleMainThread.ManagerStatusChangedCallback callback, WclBleMainThread thread, WclBleManagerStatus status)
{
callback.Method.Invoke(callback.Target, new object[] {
thread,
status,
});
}
public override bool Stop()
{
Debug.Log("停止thread");
foreach (var item in this.gattClients.Values)
{
item.Stop();
}
wclBleManager.Dispose();
return true;
}
public string Test()
{
return wclBleManager.Test();
}
public WclBleGattThread GetGattThread(BlePeripheralInfo info)
{
lock (locker)
{
if (this.gattClients.ContainsKey(info))
{
return this.gattClients[info];
}
return null;
}
}
public bool StartWatcher()
{
return base.EnqueueAction(() =>
{
this.wclWatcher.StartScan(this.wclBleManager.Radio);
});
}
public WclBleGattThread CreateGattThread(BlePeripheralInfo info)
{
lock (locker)
{
if (gattClients.ContainsKey(info))
{
throw new ArgumentException("gatt thread 已经创建");
}
WclBleGattThread wclBleGattThread = new WclBleGattThread(info, this.wclBleManager.Radio, this.GattThreadStoppedCallback);
this.gattClients[info] = wclBleGattThread;
return wclBleGattThread;
}
}
private void GattThreadStoppedCallback(WclBleGattThread gattThread)
{
Debug.Log("stop callback");
bool flag = false;
object obj = this.locker;
lock (obj)
{
if (this.gattClients.ContainsKey(gattThread.Peripheral))
{
this.gattClients.Remove(gattThread.Peripheral);
flag = true;
}
}
if (flag)
{
this.gattThreadStopped?.Invoke(this, gattThread);
}
}
public event WclBleMainThread.WclAdvertisementPacketDelegate ScanInfoReceived
{
add
{
this.scanInfoReceived += value;
}
remove
{
this.scanInfoReceived -= value;
}
}
private WclBleMainThread.WclAdvertisementPacketDelegate scanInfoReceived;
public delegate void WclAdvertisementPacketDelegate(WclBleMainThread sender, long address, string name, int rssi, WclBleAdvertisementType packetType, Guid? service);
}
}