Skip to content

Commit

Permalink
chore: Add connected device checking for HID devices
Browse files Browse the repository at this point in the history
HID devices would try to connect multiple times, as there was no
address checking at the scan level.
  • Loading branch information
qdot committed May 26, 2019
1 parent 84a7f52 commit c4fa65c
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion Buttplug.Server.Managers.HidSharpManager/HidSharpManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// </copyright>

using System;
using System.Collections.Generic;
using Buttplug.Core.Logging;
using Buttplug.Devices.Configuration;
using HidSharp;
Expand All @@ -13,6 +14,8 @@ namespace Buttplug.Server.Managers.HidSharpManager
{
public class HidSharpManager : TimedScanDeviceSubtypeManager
{
List<string> _connectedAddresses = new List<string>();

public HidSharpManager(IButtplugLogManager aLogManager)
: base(aLogManager)
{
Expand All @@ -33,7 +36,19 @@ protected override void RunScan()
continue;
}

// We're already connected, just keep going.
if (_connectedAddresses.Contains(device.DevicePath))
{
continue;
}
var bpDevice = factory.CreateDevice(LogManager, new HidSharpHidDeviceImpl(LogManager, device)).Result;
_connectedAddresses.Add(bpDevice.Identifier);
void Removed(object aObj, EventArgs aArgs)
{
_connectedAddresses.Remove(bpDevice.Identifier);
bpDevice.DeviceRemoved -= Removed;
}
bpDevice.DeviceRemoved += Removed;
InvokeDeviceAdded(new DeviceAddedEventArgs(bpDevice));
}

Expand All @@ -45,7 +60,11 @@ protected override void RunScan()
{
continue;
}

// We're already connected, just keep going.
if (_connectedAddresses.Contains(port.DevicePath))
{
continue;
}
var config = new OpenConfiguration();
config.SetOption(OpenOption.Exclusive, true);
config.SetOption(OpenOption.Interruptible, true);
Expand All @@ -65,6 +84,13 @@ protected override void RunScan()
stream.Parity = SerialParity.None;

var bpDevice = factory.CreateDevice(LogManager, new HidSharpSerialDeviceImpl(LogManager, stream)).Result;
_connectedAddresses.Add(bpDevice.Identifier);
void Removed(object aObj, EventArgs aArgs)
{
_connectedAddresses.Remove(bpDevice.Identifier);
bpDevice.DeviceRemoved -= Removed;
}
bpDevice.DeviceRemoved += Removed;
InvokeDeviceAdded(new DeviceAddedEventArgs(bpDevice));
}
}
Expand Down

0 comments on commit c4fa65c

Please sign in to comment.