Search code examples
c#.netdllhid

Why can't I connect to my USB Composite Device with HIDSharp?


I'm working on a project that emulates a USB Composite Device consisting of a HID keyboard and HID Mouse using a microprocessor. I have the device properly enumerating and functioning with both my Windows 7 x64 and Raspbian hosts and it all looks good, but where I'm running into a problem is getting my winforms application (using HidSharp) to open the connected composite device so I can get at the raw data in the keyboard endpoint.

The problem seems to be with the TryOpen() function, in that I can find the connected device by matching VID and PID, I assign the device information and report descriptor, but when I try to open a datastream via TryOpen() it fails, and I don't know why. Unfortunately the function only returns a bool, so I don't know why it's failing, just that it can't open the datastream. I'm wondering if maybe there's something funny with opening a composite device that I'm not aware of? My code for finding the device and opening the datastream is below:

/*These vars are part of the class*/
byte[] keyboardBuffer;  //EP1
HidSharp.Reports.Input.HidDeviceInputReceiver InputReceiver;
HidSharp.Reports.ReportDescriptor KeyboardRptDescriptor;
HidStream KeyboardStream;
HidDevice KeyboardDevice;

private void FindDevice()
{
    var list = DeviceList.Local;
    var stopwatch = Stopwatch.StartNew();
    var hidDeviceList = list.GetHidDevices().ToArray();

    foreach (HidDevice d in hidDeviceList)
    {

        if (d.VendorID == 0x0000 && d.ProductID == 0xA0A0)
        {
            /*Proper VID and PID Found*/
            if (d.GetProductName() == "Keyboard")
            {
                KeyboardDevice = d;
                KeyboardRptDescriptor = KeyboardDevice.GetReportDescriptor();

            }

        }

    }

    if (KeyboardDevice != null)
    {
        /*Device Found, open the datastream*/
        if (KeyboardDevice.TryOpen(out KeyboardStream))    //PROBLEM LINE - Always False?
        {
            KeyboardReport = KeyboardRptDescriptor.InputReports.FirstOrDefault();
            keyboardBuffer = new byte[KeyboardDevice.GetMaxInputReportLength()];
            InputParser = KeyboardReport.DeviceItem.CreateDeviceItemInputParser();
            InputReceiver = KeyboardRptDescriptor.CreateHidDeviceInputReceiver();
            InputReceiver.Received -= new EventHandler(HidInputReceived);
            InputReceiver.Received += new EventHandler(HidInputReceived);
            InputReceiver.Start(KeyboardStream);
        } else {
            rtb_hidLog.AppendText("Unable to connect to device\r\n");
        }


    }
    else
    {
        rtb_hidLog.AppendText("No Device Found\r\n");
    }

}

Right now I'm only attempting to read from the HID Keyboard, and will add the mouse once I sort the keyboard out. There are seemingly no issues finding the device, but why is opening it giving me such an issue? My HIDSharp library appears to be v2.0.2.0 (according to the file properties).

Thanks in advance for any suggestions!


Solution

  • So I asked around on the HIDSharp forum about this, and I got an answer from the dev:

    Turns out Windows does not allow you to open HID Keyboard devices as a security "feature", so HIDSharp will always fail to open the datastream of a HID Keyboard device.