Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
Refined driver installation wrapper to check for VIDs and PIDs only a…
Browse files Browse the repository at this point in the history
…nd not the full string
  • Loading branch information
nefarius committed Aug 10, 2015
1 parent 71934eb commit 0e7f2e7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
28 changes: 27 additions & 1 deletion ScpControl/Driver/WdiWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using log4net;

namespace ScpControl.Driver
Expand Down Expand Up @@ -190,6 +191,19 @@ private static extern int wdi_read_logger(IntPtr buffer, UInt32 buffer_size,
/// <returns></returns>
public WdiErrorCode InstallWinUsbDriver(string hardwareId, string deviceGuid, string driverPath, string infName, IntPtr hwnd)
{
// regex to extract vendir ID and product ID from hardware ID string
var regex = new Regex("VID_([0-9A-Z]{4})&PID_([0-9A-Z]{4})", RegexOptions.IgnoreCase);
// matched groups
var matches = regex.Match(hardwareId).Groups;

// very basic check
if(matches.Count < 3)
throw new ArgumentException("Supplied Hardware-ID is malformed");

// get values
var vid = matches[1].Value.ToUpper();
var pid = matches[2].Value.ToUpper();

// default return value is no matching device found
var result = WdiErrorCode.WDI_ERROR_NO_DEVICE;
// pointer to write device list to
Expand Down Expand Up @@ -223,14 +237,26 @@ public WdiErrorCode InstallWinUsbDriver(string hardwareId, string deviceGuid, st
// translate device info to managed object
var info = (wdi_device_info)Marshal.PtrToStructure(pList, typeof(wdi_device_info));

// extract VID and PID
var currentMatches = regex.Match(info.hardware_id).Groups;
var currentVid = currentMatches[1].Value.ToUpper();
var currentPid = currentMatches[2].Value.ToUpper();

// does the HID of the current device match the desired HID
if (string.CompareOrdinal(info.hardware_id, hardwareId) == 0)
if (vid == currentVid && pid == currentPid)
{
Log.InfoFormat("Device with specified VID ({0}) and PID ({1}) found, preparing driver installation...",
vid, pid);

// prepare driver installation (generates the signed driver and installation helpers)
if ((result = wdi_prepare_driver(pList, driverPath, infName, ref prepOpts)) == WdiErrorCode.WDI_SUCCESS)
{
Log.InfoFormat("Driver \"{0}\" successfully created in directory \"{1}\"", infName, driverPath);

// install/replace the current devices driver
result = wdi_install_driver(pList, driverPath, infName, ref intOpts);

Log.InfoFormat("Installation result: {0}", Enum.GetName(typeof(WdiErrorCode), result));
}

break;
Expand Down
4 changes: 2 additions & 2 deletions ScpControl/ScpControl.ini
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ HardwareId = USB\VID_07D1&PID_F101
HardwareId = USB\VID_0930&PID_0214
HardwareId = USB\VID_0930&PID_0215
HardwareId = USB\VID_0A12&PID_0001
HardwareId = USB\VID_0A12&PID_0001&REV_8891
HardwareId = USB\VID_0A5C&PID_200A
HardwareId = USB\VID_0A5C&PID_2021
HardwareId = USB\VID_0A5C&PID_2100
Expand Down Expand Up @@ -101,6 +100,7 @@ HardwareId = USB\VID_413C&PID_8126
HardwareId = USB\VID_413C&PID_8197
HardwareId = USB\VID_8086&PID_0189
HardwareId = USB\VID_8087&PID_07DA
HardwareId = USB\VID_8087&PID_07DC

[DualShock 3 Controllers]
; GUID to identify supported DS3 controllers
Expand All @@ -111,8 +111,8 @@ DeviceGuid = {E2824A09-DBAA-4407-85CA-C8E8FF5F6FFA}
; EXPERIMENTAL: add you own HIDs here for tests
HardwareId = USB\VID_054C&PID_0268
HardwareId = USB\VID_054C&PID_042F
HardwareId = USB\VID_054C&PID_0268&REV_0100
HardwareId = USB\VID_0E6F&PID_0214&REV_0580
HardwareId = USB\VID_045E&PID_028E

[DualShock 4 Controllers]
; GUID to identify supported DS4 controllers
Expand Down
5 changes: 5 additions & 0 deletions ScpService/Ds3Service.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -40,6 +41,10 @@ public Ds3Service()

protected override void OnStart(string[] args)
{
#if DEBUG
Debugger.Launch();
#endif

Log.Info("Scarlet.Crush Productions DS3 Service Started");

Log.DebugFormat("++ {0} {1}", Assembly.GetExecutingAssembly().Location,
Expand Down

0 comments on commit 0e7f2e7

Please sign in to comment.