From 47b042c3b4142cb796f172315a78a3ff74f8d478 Mon Sep 17 00:00:00 2001 From: Ihor Dutchak Date: Mon, 2 Sep 2019 12:39:12 +0300 Subject: [PATCH 1/2] macOS: send report id conditionally for hid_get_feature_report same as in hid_send_feature_report, and Windows/Linux --- mac/hid.c | 84 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/mac/hid.c b/mac/hid.c index 77308d189..7dea2e22d 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -763,38 +763,61 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char *data, size_t length) { - const unsigned char *data_to_send; - size_t length_to_send; + const unsigned char *data_to_send = data; + CFIndex length_to_send = length; IOReturn res; + const unsigned char report_id = data[0]; - /* Return if the device has been disconnected. */ - if (dev->disconnected) - return -1; - - if (data[0] == 0x0) { + if (report_id == 0x0) { /* Not using numbered Reports. Don't send the report number. */ data_to_send = data+1; length_to_send = length-1; } - else { - /* Using numbered Reports. - Send the Report Number */ - data_to_send = data; - length_to_send = length; + + /* Avoid crash if the device has been unplugged. */ + if (dev->disconnected) { + return -1; } - if (!dev->disconnected) { - res = IOHIDDeviceSetReport(dev->device_handle, - type, - data[0], /* Report ID*/ - data_to_send, length_to_send); + res = IOHIDDeviceSetReport(dev->device_handle, + type, + report_id, + data_to_send, length_to_send); - if (res == kIOReturnSuccess) { - return length; - } - else - return -1; + if (res == kIOReturnSuccess) { + return length; + } + + return -1; +} + +static int get_report(hid_device *dev, IOHIDReportType type, unsigned char *data, size_t length) +{ + unsigned char *report = data; + CFIndex report_length = length; + IOReturn res = kIOReturnSuccess; + const unsigned char report_id = data[0]; + + if (report_id == 0x0) { + /* Not using numbered Reports. + Don't send the report number. */ + report = data+1; + report_length = length-1; + } + + /* Avoid crash if the device has been unplugged. */ + if (dev->disconnected) { + return -1; + } + + res = IOHIDDeviceGetReport(dev->device_handle, + type, + report_id, + report, &report_length); + + if (res == kIOReturnSuccess) { + return length; } return -1; @@ -954,24 +977,9 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length) { - CFIndex len = length - 1; - IOReturn res; - - /* Return if the device has been unplugged. */ - if (dev->disconnected) - return -1; - - res = IOHIDDeviceGetReport(dev->device_handle, - kIOHIDReportTypeFeature, - data[0], /* Report ID */ - data + 1, &len); - if (res == kIOReturnSuccess) - return len + 1; - else - return -1; + return get_report(dev, kIOHIDReportTypeFeature, data, length); } - void HID_API_EXPORT hid_close(hid_device *dev) { if (!dev) From c44c5ea1f81d1799f788e133766e4f6e2fad80f6 Mon Sep 17 00:00:00 2001 From: Ihor Dutchak Date: Mon, 23 Sep 2019 14:51:12 +0300 Subject: [PATCH 2/2] return actual report length --- mac/hid.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mac/hid.c b/mac/hid.c index 7dea2e22d..a2b624df8 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -817,7 +817,10 @@ static int get_report(hid_device *dev, IOHIDReportType type, unsigned char *data report, &report_length); if (res == kIOReturnSuccess) { - return length; + if (report_id == 0x0) { // 0 report number still present at the beginning + report_length++; + } + return report_length; } return -1;