|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using SharpBrick.PoweredUp.Bluetooth; |
| 8 | +using SharpBrick.PoweredUp.BlueZ.Utilities; |
| 9 | +using Tmds.DBus; |
| 10 | + |
| 11 | +namespace SharpBrick.PoweredUp.BlueZ |
| 12 | +{ |
| 13 | + public class BlueZPoweredUpBluetoothAdapter : IPoweredUpBluetoothAdapter |
| 14 | + { |
| 15 | + private readonly ILogger<BlueZPoweredUpBluetoothAdapter> _logger; |
| 16 | + private readonly string _adapterObjectPath; |
| 17 | + private readonly Dictionary<ulong, IPoweredUpBluetoothDevice> _devices = new Dictionary<ulong, IPoweredUpBluetoothDevice>(); |
| 18 | + private IAdapter1 _adapter; |
| 19 | + |
| 20 | + public bool Discovering { get; set; } = false; |
| 21 | + |
| 22 | + public BlueZPoweredUpBluetoothAdapter( |
| 23 | + ILogger<BlueZPoweredUpBluetoothAdapter> logger, |
| 24 | + string adapterObjectPath = null) //"/org/bluez/hci0") |
| 25 | + { |
| 26 | + _logger = logger; |
| 27 | + _adapterObjectPath = adapterObjectPath; |
| 28 | + } |
| 29 | + |
| 30 | + private async Task<IAdapter1> GetAdapterAsync() |
| 31 | + { |
| 32 | + var adapter = !string.IsNullOrEmpty(_adapterObjectPath) ? Connection.System.CreateProxy<IAdapter1>(BlueZConstants.BlueZDBusServiceName, _adapterObjectPath) : await FindFirstAdapter(); |
| 33 | + |
| 34 | + // validate the adapter |
| 35 | + await adapter.GetAliasAsync(); |
| 36 | + |
| 37 | + // make sure it is powered on |
| 38 | + if (!await adapter.GetPoweredAsync()) |
| 39 | + { |
| 40 | + await adapter.SetPoweredAsync(true); |
| 41 | + } |
| 42 | + |
| 43 | + await adapter.WatchPropertiesAsync(AdapterPropertyChangedHandler); |
| 44 | + |
| 45 | + return adapter; |
| 46 | + } |
| 47 | + |
| 48 | + private async Task<IAdapter1> FindFirstAdapter() |
| 49 | + { |
| 50 | + var adapters = await Connection.System.FindProxies<IAdapter1>(); |
| 51 | + return adapters.FirstOrDefault(); |
| 52 | + } |
| 53 | + |
| 54 | + private void AdapterPropertyChangedHandler(PropertyChanges changes) |
| 55 | + { |
| 56 | + _logger.LogDebug("Property changed {ChangedProperties}", changes.Changed); |
| 57 | + |
| 58 | + foreach (var propertyChanged in changes.Changed) |
| 59 | + { |
| 60 | + switch (propertyChanged.Key) |
| 61 | + { |
| 62 | + case "Discovering": |
| 63 | + Discovering = (bool)propertyChanged.Value; |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private async Task<ICollection<IDevice1>> GetExistingDevicesAsync() |
| 70 | + => await Connection.System.FindProxies<IDevice1>(); |
| 71 | + |
| 72 | + private IDevice1 GetSpecificDeviceAsync(ObjectPath objectPath) |
| 73 | + => Connection.System.CreateProxy<IDevice1>(BlueZConstants.BlueZDBusServiceName, objectPath); |
| 74 | + |
| 75 | + private async Task<bool> IsLegoWirelessProcotolDevice(IDevice1 device) |
| 76 | + => (await device.GetUUIDsAsync()).NullToEmpty().Any(x => x.ToUpperInvariant() == PoweredUpBluetoothConstants.LegoHubService); |
| 77 | + |
| 78 | + public async void Discover(Func<PoweredUpBluetoothDeviceInfo, Task> discoveryHandler, CancellationToken cancellationToken = default) |
| 79 | + { |
| 80 | + _adapter ??= await GetAdapterAsync(); |
| 81 | + |
| 82 | + var existingDevices = await GetExistingDevicesAsync(); |
| 83 | + |
| 84 | + foreach (var device in existingDevices) |
| 85 | + { |
| 86 | + if (await IsLegoWirelessProcotolDevice(device)) |
| 87 | + { |
| 88 | + var poweredUpDevice = new BlueZPoweredUpBluetoothDevice(device, discoveryHandler); |
| 89 | + await poweredUpDevice.Initialize(); |
| 90 | + |
| 91 | + _devices.Add(poweredUpDevice.DeviceInfo.BluetoothAddress, poweredUpDevice); |
| 92 | + |
| 93 | + await poweredUpDevice.TryGetManufacturerDataAsync(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + await Connection.System.WatchInterfacesAdded(NewDeviceAddedHandler); |
| 98 | + |
| 99 | + await _adapter.SetDiscoveryFilterAsync(new Dictionary<string,object>() |
| 100 | + { |
| 101 | + { "UUIDs", new string[] { PoweredUpBluetoothConstants.LegoHubService } } |
| 102 | + }); |
| 103 | + |
| 104 | + cancellationToken.Register(async () => |
| 105 | + { |
| 106 | + if (Discovering) |
| 107 | + { |
| 108 | + await _adapter.StopDiscoveryAsync(); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + await _adapter.StartDiscoveryAsync(); |
| 113 | + |
| 114 | + async void NewDeviceAddedHandler((ObjectPath objectPath, IDictionary<string, IDictionary<string, object>> interfaces) args) |
| 115 | + { |
| 116 | + if (!args.interfaces.ContainsKey("org.bluez.Device1")) |
| 117 | + { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + var device = GetSpecificDeviceAsync(args.objectPath); |
| 122 | + var poweredUpDevice = new BlueZPoweredUpBluetoothDevice(device, discoveryHandler); |
| 123 | + |
| 124 | + await poweredUpDevice.Initialize(); |
| 125 | + |
| 126 | + _devices.Add(poweredUpDevice.DeviceInfo.BluetoothAddress, poweredUpDevice); |
| 127 | + |
| 128 | + await poweredUpDevice.TryGetManufacturerDataAsync(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public Task<IPoweredUpBluetoothDevice> GetDeviceAsync(ulong bluetoothAddress) |
| 133 | + { |
| 134 | + if (!_devices.ContainsKey(bluetoothAddress)) |
| 135 | + { |
| 136 | + throw new ArgumentOutOfRangeException("Requested bluetooth device is not available from this adapter"); |
| 137 | + } |
| 138 | + |
| 139 | + return Task.FromResult<IPoweredUpBluetoothDevice>(_devices[bluetoothAddress]); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments