Skip to content

Commit

Permalink
feat: Add WinUSB Manager and Rez TranceVibrator support
Browse files Browse the repository at this point in the history
Add WinUSB Device manager for handling devices that require
WinUSB (Zadig) drivers. Add Rez TranceVibrator support on Windows
7 (Won't work on win 10 due to USB descriptor issues).

Fixes #235
  • Loading branch information
qdot committed Mar 7, 2018
1 parent ac56389 commit 1e6a035
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@
<Project>{223ea194-8318-4189-bec8-73734c3f4a78}</Project>
<Name>Buttplug.Server.Managers.UWPBluetoothManager</Name>
</ProjectReference>
<ProjectReference Include="..\Buttplug.Server.Managers.WinUSBManager\Buttplug.Server.Managers.WinUSBManager.csproj">
<Project>{0f5dab4b-184b-4b7e-9f40-339c7f5e216f}</Project>
<Name>Buttplug.Server.Managers.WinUSBManager</Name>
</ProjectReference>
<ProjectReference Include="..\Buttplug.Server.Managers.XInputGamepadManager\Buttplug.Server.Managers.XInputGamepadManager.csproj">
<Project>{c7d040e1-e18e-4f51-859f-cff8aa57bd64}</Project>
<Name>Buttplug.Server.Managers.XInputGamepadManager</Name>
Expand Down
3 changes: 2 additions & 1 deletion Buttplug.Components.Controls/ButtplugTabControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Buttplug.Server.Managers.ETSerialManager;
using Buttplug.Server.Managers.SimulatorManager;
using Buttplug.Server.Managers.UWPBluetoothManager;
using Buttplug.Server.Managers.WinUSBManager;
using Buttplug.Server.Managers.XInputGamepadManager;
using JetBrains.Annotations;
using Microsoft.Win32;
Expand Down Expand Up @@ -139,7 +140,7 @@ private ButtplugServer InitializeButtplugServer(string aServerName, uint aMaxPin

bpServer.AddDeviceSubtypeManager(aLogger => new XInputGamepadManager(aLogger));
bpServer.AddDeviceSubtypeManager(aLogger => new ETSerialManager(aLogger));

bpServer.AddDeviceSubtypeManager(aLogger => new WinUSBManager(aLogger));
#if DEBUG
bpServer.AddDeviceSubtypeManager(aLogger => new SimulatorManager(aLogger));
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0F5DAB4B-184B-4B7E-9F40-339C7F5E216F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Buttplug.Server.Managers.WinUSBManager</RootNamespace>
<AssemblyName>Buttplug.Server.Managers.WinUSBManager</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>..\Buttplug.ProjectFiles\ButtplugCodeAnalysisRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="WinUSBNet, Version=1.0.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\WinUSBNet.1.0.3\lib\net35\WinUSBNet.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="RezTranceVibratorDevice.cs" />
<Compile Include="WinUSBManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Buttplug.Core\Buttplug.Core.csproj">
<Project>{fc4af8fe-8c21-4e07-8d24-7fb7791149d0}</Project>
<Name>Buttplug.Core</Name>
</ProjectReference>
<ProjectReference Include="..\Buttplug.Server\Buttplug.Server.csproj">
<Project>{23d9ec18-2dba-4149-a951-b84786d4bfa6}</Project>
<Name>Buttplug.Server</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\Buttplug.ProjectFiles\stylecop.json" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\StyleCop.Analyzers.1.1.0-beta004\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />
<Analyzer Include="..\packages\StyleCop.Analyzers.1.1.0-beta004\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
39 changes: 39 additions & 0 deletions Buttplug.Server.Managers.WinUSBManager/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Buttplug.Core;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Buttplug.Server.Managers.WinUSBManager")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Buttplug.Server.Managers.WinUSBManager")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0f5dab4b-184b-4b7e-9f40-339c7f5e216f")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0.0-dev")]
[assembly: AssemblyGitVersion("")]
90 changes: 90 additions & 0 deletions Buttplug.Server.Managers.WinUSBManager/RezTranceVibratorDevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Buttplug.Core;
using Buttplug.Core.Messages;
using MadWizard.WinUSBNet;

namespace Buttplug.Server.Managers.WinUSBManager
{
internal class RezTranceVibratorDevice : ButtplugDevice
{
private static uint _vibratorCount = 1;
private USBDevice _device;

public RezTranceVibratorDevice(IButtplugLogManager aLogManager, USBDevice aDevice, uint aIndex)
: base(aLogManager, "Trancevibrator " + aIndex, "Trancevibrator " + aIndex)
{
_device = aDevice;
MsgFuncs.Add(typeof(SingleMotorVibrateCmd), new ButtplugDeviceWrapper(HandleSingleMotorVibrateCmd));
MsgFuncs.Add(typeof(StopDeviceCmd), new ButtplugDeviceWrapper(HandleStopDeviceCmd));
MsgFuncs.Add(typeof(VibrateCmd), new ButtplugDeviceWrapper(HandleVibrateCmd));
}

public override void Disconnect()
{
}

private Task<ButtplugMessage> HandleStopDeviceCmd(ButtplugDeviceMessage aMsg)
{
BpLogger.Debug("Stopping Device " + Name);
return HandleSingleMotorVibrateCmd(new SingleMotorVibrateCmd(aMsg.DeviceIndex, 0, aMsg.Id));
}

private Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg)
{
if (!(aMsg is VibrateCmd cmdMsg))
{
return Task.FromResult<ButtplugMessage>(BpLogger.LogErrorMsg(aMsg.Id, Error.ErrorClass.ERROR_DEVICE, "Wrong Handler"));
}

if (cmdMsg.Speeds.Count == 0 || cmdMsg.Speeds.Count > _vibratorCount)
{
return Task.FromResult<ButtplugMessage>(new Error("VibrateCmd requires 1 speed for this device.",
Error.ErrorClass.ERROR_DEVICE,
cmdMsg.Id));
}

foreach (var v in cmdMsg.Speeds)
{
if (v.Index >= _vibratorCount)
{
return Task.FromResult<ButtplugMessage>(new Error(
$"Index {v.Index} is out of bounds for VibrateCmd for this device.",
Error.ErrorClass.ERROR_DEVICE,
cmdMsg.Id));
}

var speed = (byte)Math.Floor(v.Speed * 255);
_device.ControlOut(
0x02 << 5 | // Vendor Type
0x01 | // Interface Recipient
0x00, // Out Enpoint
1,
speed,
0);
}

return Task.FromResult<ButtplugMessage>(new Ok(aMsg.Id));
}

private Task<ButtplugMessage> HandleSingleMotorVibrateCmd(ButtplugDeviceMessage aMsg)
{
var cmdMsg = aMsg as SingleMotorVibrateCmd;
if (cmdMsg is null)
{
return Task.FromResult<ButtplugMessage>(BpLogger.LogErrorMsg(aMsg.Id, Error.ErrorClass.ERROR_DEVICE, "Wrong Handler"));
}

var speeds = new List<VibrateCmd.VibrateSubcommand>();
for (uint i = 0; i < _vibratorCount; i++)
{
speeds.Add(new VibrateCmd.VibrateSubcommand(i, cmdMsg.Speed));
}

return HandleVibrateCmd(new VibrateCmd(aMsg.DeviceIndex, speeds, aMsg.Id));
}
}
}
93 changes: 93 additions & 0 deletions Buttplug.Server.Managers.WinUSBManager/WinUSBManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using Buttplug.Core;
using MadWizard.WinUSBNet;
using Microsoft.Win32;

namespace Buttplug.Server.Managers.WinUSBManager
{
public class WinUSBManager : DeviceSubtypeManager
{
public WinUSBManager(IButtplugLogManager aLogManager)
: base(aLogManager)
{
BpLogger.Info("Loading WinUSB Manager");
}

public override void StartScanning()
{
BpLogger.Info("WinUSBManager start scanning");
var deviceKey =
Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\USB\VID_0B49&PID_064F");
if (deviceKey == null)
{
BpLogger.Debug("No TranceVibrator Devices found in registry.");
InvokeScanningFinished();
return;
}

var deviceKeyNames = deviceKey.GetSubKeyNames();
if (deviceKeyNames.Length == 0)
{
BpLogger.Debug("No TranceVibrator Devices with drivers found in registry.");
InvokeScanningFinished();
return;
}

var deviceSubKey = deviceKey.OpenSubKey(deviceKeyNames[0]);
if (deviceSubKey == null)
{
BpLogger.Debug("No TranceVibrator Devices with drivers subkeys found in registry.");
InvokeScanningFinished();
return;
}

var deviceParameters = deviceSubKey.OpenSubKey("Device Parameters");
if (deviceParameters == null)
{
BpLogger.Debug("No TranceVibrator Devices with drivers parameters found in registry.");
InvokeScanningFinished();
return;
}

var deviceGUID = (string[])deviceParameters.GetValue("DeviceInterfaceGUIDs", string.Empty);
if (deviceGUID == null || deviceGUID.ToString().Length == 0)
{
BpLogger.Debug("No TranceVibrator Devices Driver GUIDs found in registry.");
InvokeScanningFinished();
return;
}

// Only valid for our Trancevibrator install
var devices = USBDevice.GetDevices(deviceGUID[0]);
if (devices == null || devices.Length == 0)
{
BpLogger.Error("No USB Device found!");
InvokeScanningFinished();
return;
}

uint index = 0;
foreach (var deviceinfo in devices)
{
var device = new USBDevice(deviceinfo);
BpLogger.Debug($"Found TranceVibrator Device");
var tvDevice = new RezTranceVibratorDevice(LogManager, device, index);
index += 1;
InvokeDeviceAdded(new DeviceAddedEventArgs(tvDevice));
}

InvokeScanningFinished();
}

public override void StopScanning()
{
// noop
}

public override bool IsScanning()
{
// noop
return false;
}
}
}
11 changes: 11 additions & 0 deletions Buttplug.Server.Managers.WinUSBManager/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
5 changes: 5 additions & 0 deletions Buttplug.Server.Managers.WinUSBManager/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="WinUSBNet" version="1.0.3" targetFramework="net452" />
<package id="StyleCop.Analyzers" version="1.1.0-beta004" targetFramework="net452" developmentDependency="true" />
</packages>
8 changes: 7 additions & 1 deletion Buttplug.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.15
VisualStudioVersion = 15.0.27004.2009
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Buttplug.Core", "Buttplug.Core\Buttplug.Core.csproj", "{FC4AF8FE-8C21-4E07-8D24-7FB7791149D0}"
EndProject
Expand Down Expand Up @@ -47,6 +47,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Buttplug.Server.Managers.Si
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Buttplug.Server.Managers.ETSerialManager", "Buttplug.Server.Managers.ErosTek\Buttplug.Server.Managers.ETSerialManager.csproj", "{436CF4FF-AE10-45DA-8909-9AACF1ED6425}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Buttplug.Server.Managers.WinUSBManager", "Buttplug.Server.Managers.WinUSBManager\Buttplug.Server.Managers.WinUSBManager.csproj", "{0F5DAB4B-184B-4B7E-9F40-339C7F5E216F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -139,6 +141,10 @@ Global
{436CF4FF-AE10-45DA-8909-9AACF1ED6425}.Debug|Any CPU.Build.0 = Debug|Any CPU
{436CF4FF-AE10-45DA-8909-9AACF1ED6425}.Release|Any CPU.ActiveCfg = Release|Any CPU
{436CF4FF-AE10-45DA-8909-9AACF1ED6425}.Release|Any CPU.Build.0 = Release|Any CPU
{0F5DAB4B-184B-4B7E-9F40-339C7F5E216F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0F5DAB4B-184B-4B7E-9F40-339C7F5E216F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F5DAB4B-184B-4B7E-9F40-339C7F5E216F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F5DAB4B-184B-4B7E-9F40-339C7F5E216F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 1e6a035

Please sign in to comment.