kagali-san
kagali-san

Reputation: 3082

What happens when I do ReadFile() on a HID device on Windows?

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);

Upvotes: 2

Views: 4403

Answers (1)

Preston
Preston

Reputation: 2653

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.

Upvotes: 6

Related Questions