Skip to content

Commit 1d65b88

Browse files
author
Erik Werner
committed
Added hid_open_ref to open a device based on an IOHIDDeviceRef. Prevents the wrong device from being opened if the path is ambiguous.
1 parent 083223e commit 1d65b88

2 files changed

Lines changed: 104 additions & 1 deletion

File tree

‎hidapi/hidapi.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ extern "C" {
7777
* Valid on the Mac implementation if and only if the device
7878
is a USB HID device. */
7979
int interface_number;
80+
81+
/** Store an IOHIDDeviceRef to open device
82+
by reference (Mac only).*/
83+
unsigned long long dev_ref;
84+
8085

8186
/** Pointer to the next device */
8287
struct hid_device_info *next;
@@ -181,6 +186,21 @@ extern "C" {
181186
success or NULL on failure.
182187
*/
183188
HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path);
189+
190+
/** @brief Open a HID device by reference
191+
192+
The reference is determined by calling hid_enumerate().
193+
194+
This function sets the return value of hid_error().
195+
196+
@ingroup API
197+
@param dev_ref an IOHIDDeviceRef to the device being opened
198+
199+
@returns
200+
This function returns a pointer to a #hid_device object on
201+
success or NULL on failure.
202+
*/
203+
HID_API_EXPORT hid_device * HID_API_CALL hid_open_ref(const unsigned long long dev_ref);
184204

185205
/** @brief Write an Output report to a HID device.
186206

‎mac/hid.c‎

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
482482

483483
/* Release Number */
484484
cur_dev->release_number = get_int_property(dev, CFSTR(kIOHIDVersionNumberKey));
485+
486+
/* store the current IOHIDDeviceRef */
487+
cur_dev->dev_ref = dev;
485488

486489
/* We can only retrieve the interface number for USB HID devices.
487490
* IOKit always seems to return 0 when querying a standard USB device
@@ -521,6 +524,7 @@ hid_device * HID_API_EXPORT hid_open(unsigned short vendor_id, unsigned short pr
521524
/* This function is identical to the Linux version. Platform independent. */
522525
struct hid_device_info *devs, *cur_dev;
523526
const char *path_to_open = NULL;
527+
IOHIDDeviceRef dev_to_open = NULL;
524528
hid_device * handle = NULL;
525529

526530
devs = hid_enumerate(vendor_id, product_id);
@@ -531,10 +535,16 @@ hid_device * HID_API_EXPORT hid_open(unsigned short vendor_id, unsigned short pr
531535
if (serial_number) {
532536
if (wcscmp(serial_number, cur_dev->serial_number) == 0) {
533537
path_to_open = cur_dev->path;
538+
// when there is a serial number, use the reference instead of the path
539+
dev_to_open = cur_dev->dev_ref;
534540
break;
535541
}
542+
else {
543+
// serial number doesn't match, keep iterating
544+
}
536545
}
537546
else {
547+
// there was no serial number argument, open by path
538548
path_to_open = cur_dev->path;
539549
break;
540550
}
@@ -543,9 +553,12 @@ hid_device * HID_API_EXPORT hid_open(unsigned short vendor_id, unsigned short pr
543553
}
544554

545555
if (path_to_open) {
546-
/* Open the device */
556+
/* Open the device using the path*/
547557
handle = hid_open_path(path_to_open);
548558
}
559+
else if (dev_to_open) {
560+
handle = hid_open_ref(dev_to_open);
561+
}
549562

550563
hid_free_enumeration(devs);
551564

@@ -761,6 +774,76 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
761774
return NULL;
762775
}
763776

777+
/* hid_open_ref()
778+
*
779+
* ref must be a valid IOHIDDeviceRef
780+
*/
781+
hid_device * HID_API_EXPORT hid_open_ref(const unsigned long long dev_ref)
782+
{
783+
hid_device *dev = NULL;
784+
//io_registry_entry_t entry = MACH_PORT_NULL;
785+
IOReturn ret = kIOReturnInvalid;
786+
787+
/* Set up the HID Manager if it hasn't been done */
788+
if (hid_init() < 0)
789+
goto return_error;
790+
791+
dev = new_hid_device();
792+
793+
io_object_t iokit_dev = hidapi_IOHIDDeviceGetService(dev_ref);
794+
/* Create an IOHIDDevice for the entry */
795+
dev->device_handle = IOHIDDeviceCreate(kCFAllocatorDefault, iokit_dev);
796+
if (dev->device_handle == NULL) {
797+
/* Error creating the HID device */
798+
goto return_error;
799+
}
800+
801+
/* Open the IOHIDDevice */
802+
ret = IOHIDDeviceOpen(dev->device_handle, kIOHIDOptionsTypeSeizeDevice);
803+
if (ret == kIOReturnSuccess) {
804+
char str[32];
805+
806+
/* Create the buffers for receiving data */
807+
dev->max_input_report_len = (CFIndex) get_max_report_length(dev->device_handle);
808+
dev->input_report_buf = (uint8_t*) calloc(dev->max_input_report_len, sizeof(uint8_t));
809+
810+
/* Create the Run Loop Mode for this device.
811+
printing the reference seems to work. */
812+
sprintf(str, "HIDAPI_%p", dev->device_handle);
813+
dev->run_loop_mode =
814+
CFStringCreateWithCString(NULL, str, kCFStringEncodingASCII);
815+
816+
/* Attach the device to a Run Loop */
817+
IOHIDDeviceRegisterInputReportCallback(
818+
dev->device_handle, dev->input_report_buf, dev->max_input_report_len,
819+
&hid_report_callback, dev);
820+
IOHIDDeviceRegisterRemovalCallback(dev->device_handle, hid_device_removal_callback, dev);
821+
822+
/* Start the read thread */
823+
pthread_create(&dev->thread, NULL, read_thread, dev);
824+
825+
/* Wait here for the read thread to be initialized. */
826+
pthread_barrier_wait(&dev->barrier);
827+
828+
//IOObjectRelease(entry);
829+
return dev;
830+
}
831+
else {
832+
goto return_error;
833+
}
834+
835+
return_error:
836+
if (dev->device_handle != NULL)
837+
CFRelease(dev->device_handle);
838+
839+
//if (entry != MACH_PORT_NULL)
840+
// IOObjectRelease(entry);
841+
842+
free_hid_device(dev);
843+
return NULL;
844+
}
845+
846+
764847
static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char *data, size_t length)
765848
{
766849
const unsigned char *data_to_send = data;

0 commit comments

Comments
 (0)