2
votes

When I do a ReadFile/WriteFile on a handle provided by CreateFile(HID_DEVICE_NAME,...), what happens in a terms of HID operations?

Does it issues a direct write/read request to HID device (USB, in my case), or is it transformed somewhere in underlying drivers to read last cached HID report with such ID?


ReadFile call:

syncDevice.OutputReportBuffer[0] = 0;
syncDevice.OutputReportBuffer[1] = reportID;
HANDLE writeHandle = CreateFile(pDevice->DevicePath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
HANDLE readHandle = CreateFile(pDevice->DevicePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
success = WriteFile(writeHandle, (void*) syncDevice.OutputReportBuffer, syncDevice.Caps.OutputReportByteLength, &bytecnt, 0);
success = ReadFile(readHandle, syncDevice.InputReportBuffer, syncDevice.Caps.InputReportByteLength, &bytecnt, 0);
1
I would expect the CreateFile call to fail; it is my understanding that Windows keeps HID devices open for exclusive access. At any rate this is unlikely to do anything sensible.Harry Johnston
@HarryJohnston but it workskagali-san
Is this a mouse, a keyboard, or something else?Harry Johnston
@HarryJohnston its an AVR-based device which is visible as HID device.kagali-san

1 Answers

4
votes

From USB Complete by Jan Axelson:

The Windows HID driver causes the host controller to request Input reports. The driver stores received reports in a buffer. ReadFile retrieves one or more reports from the buffer. If the buffer is empty, ReadFile waits for a report to arrive. In other words, ReadFile doesn’t cause a device to send a report but just reads reports that the driver has requested.

WriteFile sends an Output report. The function uses an interrupt transfer if the HID has an interrupt OUT endpoint and the operating system is later than Windows 98 Gold. Otherwise, WriteFile uses a control transfer with a Set Report request. If using interrupt transfers, WriteFile will wait if the device NAKs. If using control transfers, WriteFile returns with an error code on failure or a timeout.