diff --git a/hidapi/hid.cpp b/hidapi/hid.cpp index 6c78048..a7139ff 100644 --- a/hidapi/hid.cpp +++ b/hidapi/hid.cpp @@ -451,6 +451,47 @@ int HID_API_EXPORT hid_set_nonblocking(int device, int nonblock) return 0; /* Success */ } +int HID_API_EXPORT hid_send_feature_report(int device, const unsigned char *data, size_t length) +{ + Device *dev = NULL; + + // Get the handle + if (device < 0 || device >= MAX_DEVICES) + return -1; + if (devices[device].valid == 0) + return -1; + dev = &devices[device]; + + BOOL res = HidD_SetFeature(dev->device_handle, (PVOID)data, length); + if (!res) { + register_error(dev, "HidD_SetFeature"); + return -1; + } + + return length; +} + + +int HID_API_EXPORT hid_get_feature_report(int device, unsigned char *data, size_t length) +{ + Device *dev = NULL; + + // Get the handle + if (device < 0 || device >= MAX_DEVICES) + return -1; + if (devices[device].valid == 0) + return -1; + dev = &devices[device]; + + BOOL res = HidD_GetFeature(dev->device_handle, data, length); + if (!res) { + register_error(dev, "HidD_GetFeature"); + return -1; + } + + return 0; /* Windows doesn't give us an actual length, unfortunately */ +} + void HID_API_EXPORT hid_close(int device) { Device *dev = NULL; diff --git a/hidapi/hidapi.def b/hidapi/hidapi.def index 088ff7c..05e35af 100644 --- a/hidapi/hidapi.def +++ b/hidapi/hidapi.def @@ -12,3 +12,6 @@ EXPORTS hid_set_nonblocking @10 hid_enumerate @11 hid_open_path @12 + hid_send_feature_report @13 + hid_get_feature_report @14 + \ No newline at end of file diff --git a/hidapi/hidapi.h b/hidapi/hidapi.h index bba30ca..04b2e5a 100644 --- a/hidapi/hidapi.h +++ b/hidapi/hidapi.h @@ -132,8 +132,9 @@ extern "C" { */ int HID_API_EXPORT HID_API_CALL hid_write(int device, const unsigned char *data, size_t length); - /** Read a report from a HID device. The first byte will contain the - Report number, if multiple reports are supported by the device. + /** Read an Input report from a HID device. Input reports are returned + to the host through the INTERRUPT IN endpoint. The first byte will + contain the Report number if the device uses numbered reports. Params: device: A device handle returned from hid_open(). @@ -165,6 +166,49 @@ extern "C" { */ int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(int device, int nonblock); + /** Send a Feature report to the device. Feature reports are sent + over the Control endpoint as a Set_Report transfer. The first + byte of data[] must contain the Report ID. For devices which only + support a single report, this must be set to 0x0. The remaining + bytes contain the report data. Since the Report ID is mandatory, + calls to hid_send_feature_report() will always contain one more + byte than the report contains. For example, if a hid report is 16 + bytes long, 17 bytes must be passed to hid_send_feature_report(): + the Report ID (or 0x0, for devices which do not use numbered + reports), followed by the report data (16 bytes). In this example, + the length passed in would be 17. + + Params: + device: A device handle returned from hid_open(). + data: The data to send, including the report number as + the first byte. + length: The length in bytes of the data to send, including + the report number. + + Return Value: + This function returns the actual number of bytes written and + -1 on error. + */ + int HID_API_EXPORT HID_API_CALL hid_send_feature_report(int device, const unsigned char *data, size_t length); + + /** Get a feature report from a HID device. Make sure to set the + first byte of data[] to the Report ID of the report to be read. + Make sure to allow space for this extra byte in data[]. + + Params: + device: A device handle returned from hid_open(). + data: A buffer to put the read data into, including the + Report ID. Set the first byte of data[] to the Report + ID of the report to be read. + length: The number of bytes to read, including an extra byte + for the report ID. The buffer can be longer than the + actual report. + + Return Value: + This function returns 0 on success and -1 on error. + */ + int HID_API_EXPORT HID_API_CALL hid_get_feature_report(int device, unsigned char *data, size_t length); + /** Close a HID device. Params: diff --git a/hidapi/objfre_wxp_x86/i386/hidapi.dll b/hidapi/objfre_wxp_x86/i386/hidapi.dll index 112b3a4..eef8adb 100644 Binary files a/hidapi/objfre_wxp_x86/i386/hidapi.dll and b/hidapi/objfre_wxp_x86/i386/hidapi.dll differ diff --git a/hidapi/objfre_wxp_x86/i386/hidapi.lib b/hidapi/objfre_wxp_x86/i386/hidapi.lib index 3055554..7473cda 100644 Binary files a/hidapi/objfre_wxp_x86/i386/hidapi.lib and b/hidapi/objfre_wxp_x86/i386/hidapi.lib differ diff --git a/hidtest/Debug/hidtest.exe b/hidtest/Debug/hidtest.exe index 5336e34..8b8e42b 100644 Binary files a/hidtest/Debug/hidtest.exe and b/hidtest/Debug/hidtest.exe differ diff --git a/hidtest/Release/hidtest.exe b/hidtest/Release/hidtest.exe index 7de9df8..e3d2dd8 100644 Binary files a/hidtest/Release/hidtest.exe and b/hidtest/Release/hidtest.exe differ diff --git a/hidtest/hidtest.cpp b/hidtest/hidtest.cpp index 63b6ce2..73c471c 100644 --- a/hidtest/hidtest.cpp +++ b/hidtest/hidtest.cpp @@ -97,6 +97,33 @@ int main(int argc, char* argv[]) // data here, but execution should not block. res = hid_read(handle, buf, 17); + // Send a Feature Report to the device + buf[0] = 0x2; + buf[1] = 0xa0; + buf[2] = 0x0a; + buf[3] = 0x00; + buf[4] = 0x00; + res = hid_send_feature_report(handle, buf, 17); + if (res < 0) { + printf("Unable to send a feature report.\n"); + } + + memset(buf,0,sizeof(buf)); + + // Read a Feature Report from the device + buf[0] = 0x2; + res = hid_get_feature_report(handle, buf, sizeof(buf)); + if (res < 0) { + printf("Unable to get a feature report.\n"); + } + + // Print out the returned buffer. + printf("Feature Report\n "); + for (i = 0; i < 17; i++) + printf("%02hhx ", buf[i]); + printf("\n"); + + // Toggle LED (cmd 0x80). The first byte is the report number (0x1). buf[0] = 0x1; buf[1] = 0x80; @@ -126,6 +153,7 @@ int main(int argc, char* argv[]) printf("Unable to read()\n"); } + printf("Data read:\n "); // Print out the returned buffer. for (i = 0; i < res; i++) printf("%02hhx ", buf[i]);