Skip to content

Commit d64f6c6

Browse files
authored
macOS: send report id conditionally for hid_get_feature_report (libusb#70)
same as in hid_send_feature_report, and Windows/Linux
1 parent d8b6588 commit d64f6c6

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed

mac/hid.c

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -763,38 +763,64 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
763763

764764
static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char *data, size_t length)
765765
{
766-
const unsigned char *data_to_send;
767-
size_t length_to_send;
766+
const unsigned char *data_to_send = data;
767+
CFIndex length_to_send = length;
768768
IOReturn res;
769+
const unsigned char report_id = data[0];
769770

770-
/* Return if the device has been disconnected. */
771-
if (dev->disconnected)
772-
return -1;
773-
774-
if (data[0] == 0x0) {
771+
if (report_id == 0x0) {
775772
/* Not using numbered Reports.
776773
Don't send the report number. */
777774
data_to_send = data+1;
778775
length_to_send = length-1;
779776
}
780-
else {
781-
/* Using numbered Reports.
782-
Send the Report Number */
783-
data_to_send = data;
784-
length_to_send = length;
777+
778+
/* Avoid crash if the device has been unplugged. */
779+
if (dev->disconnected) {
780+
return -1;
785781
}
786782

787-
if (!dev->disconnected) {
788-
res = IOHIDDeviceSetReport(dev->device_handle,
789-
type,
790-
data[0], /* Report ID*/
791-
data_to_send, length_to_send);
783+
res = IOHIDDeviceSetReport(dev->device_handle,
784+
type,
785+
report_id,
786+
data_to_send, length_to_send);
787+
788+
if (res == kIOReturnSuccess) {
789+
return length;
790+
}
792791

793-
if (res == kIOReturnSuccess) {
794-
return length;
792+
return -1;
793+
}
794+
795+
static int get_report(hid_device *dev, IOHIDReportType type, unsigned char *data, size_t length)
796+
{
797+
unsigned char *report = data;
798+
CFIndex report_length = length;
799+
IOReturn res = kIOReturnSuccess;
800+
const unsigned char report_id = data[0];
801+
802+
if (report_id == 0x0) {
803+
/* Not using numbered Reports.
804+
Don't send the report number. */
805+
report = data+1;
806+
report_length = length-1;
807+
}
808+
809+
/* Avoid crash if the device has been unplugged. */
810+
if (dev->disconnected) {
811+
return -1;
812+
}
813+
814+
res = IOHIDDeviceGetReport(dev->device_handle,
815+
type,
816+
report_id,
817+
report, &report_length);
818+
819+
if (res == kIOReturnSuccess) {
820+
if (report_id == 0x0) { // 0 report number still present at the beginning
821+
report_length++;
795822
}
796-
else
797-
return -1;
823+
return report_length;
798824
}
799825

800826
return -1;
@@ -954,24 +980,9 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char
954980

955981
int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
956982
{
957-
CFIndex len = length - 1;
958-
IOReturn res;
959-
960-
/* Return if the device has been unplugged. */
961-
if (dev->disconnected)
962-
return -1;
963-
964-
res = IOHIDDeviceGetReport(dev->device_handle,
965-
kIOHIDReportTypeFeature,
966-
data[0], /* Report ID */
967-
data + 1, &len);
968-
if (res == kIOReturnSuccess)
969-
return len + 1;
970-
else
971-
return -1;
983+
return get_report(dev, kIOHIDReportTypeFeature, data, length);
972984
}
973985

974-
975986
void HID_API_EXPORT hid_close(hid_device *dev)
976987
{
977988
if (!dev)

0 commit comments

Comments
 (0)