Beispiel #1
0
hid_device * HID_API_EXPORT hid_open_path(const char *path)
{
    int i;
    hid_device *dev = NULL;
    CFIndex num_devices;

    dev = new_hid_device();

    /* Set up the HID Manager if it hasn't been done */
    if (!hid_mgr)
        init_hid_manager();

    CFSetRef device_set = IOHIDManagerCopyDevices(hid_mgr);

    num_devices = CFSetGetCount(device_set);
    IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef));
    CFSetGetValues(device_set, (const void **) device_array);
    for (i = 0; i < num_devices; i++)
    {
        char cbuf[BUF_LEN];
        size_t len;
        IOHIDDeviceRef os_dev = device_array[i];

        len = make_path(os_dev, cbuf, sizeof(cbuf));
        if (!strcmp(cbuf, path))
        {
            // Matched Paths. Open this Device.
            IOReturn ret = IOHIDDeviceOpen(os_dev, kIOHIDOptionsTypeNone);
            if (ret == kIOReturnSuccess)
            {
                char str[32];
                CFIndex max_input_report_len;

                free(device_array);
                CFRelease(device_set);
                dev->device_handle = os_dev;

                /* Create the buffers for receiving data */
                max_input_report_len = (CFIndex) get_max_report_length(os_dev);
                dev->input_report_buf = calloc(max_input_report_len, sizeof(uint8_t));

                /* Create the Run Loop Mode for this device.
                   printing the reference seems to work. */
                sprintf(str, "%p", os_dev);
                dev->run_loop_mode =
                    CFStringCreateWithCString(NULL, str, kCFStringEncodingASCII);

                /* Attach the device to a Run Loop */
                IOHIDDeviceScheduleWithRunLoop(os_dev, CFRunLoopGetCurrent(), dev->run_loop_mode);
                IOHIDDeviceRegisterInputReportCallback(
                    os_dev, dev->input_report_buf, max_input_report_len,
                    &hid_report_callback, dev);

                pthread_mutex_init(&dev->mutex, NULL);

                return dev;
            }
            else
            {
                goto return_error;
            }
        }
    }

return_error:
    free(device_array);
    CFRelease(device_set);
    free(dev);
    return NULL;
}
Beispiel #2
0
hid_device * HID_API_EXPORT hid_open_path(const char *path)
{
    int i;
    hid_device *dev = NULL;
    CFIndex num_devices;

    dev = new_hid_device();

    /* Set up the HID Manager if it hasn't been done */
    if (hid_init() < 0)
        return NULL;

    /* give the IOHIDManager a chance to update itself */
    process_pending_events();

    CFSetRef device_set = IOHIDManagerCopyDevices(hid_mgr);

    num_devices = CFSetGetCount(device_set);
    IOHIDDeviceRef *device_array = (IOHIDDeviceRef *)calloc(num_devices, sizeof(IOHIDDeviceRef));
    CFSetGetValues(device_set, (const void **) device_array);
    for (i = 0; i < num_devices; i++) {
        char cbuf[BUF_LEN];
        size_t len;
        IOHIDDeviceRef os_dev = device_array[i];

        len = make_path(os_dev, cbuf, sizeof(cbuf));
        if (!strcmp(cbuf, path)) {
            /* Matched Paths. Open this Device. */
            IOReturn ret = IOHIDDeviceOpen(os_dev, kIOHIDOptionsTypeSeizeDevice);
            if (ret == kIOReturnSuccess) {
                char str[32];

                free(device_array);
                CFRetain(os_dev);
                CFRelease(device_set);
                dev->device_handle = os_dev;

                /* Create the buffers for receiving data */
                dev->max_input_report_len = (CFIndex) get_max_report_length(os_dev);
                dev->input_report_buf = (uint8_t *)calloc(dev->max_input_report_len, sizeof(uint8_t));

                /* Create the Run Loop Mode for this device.
                   printing the reference seems to work. */
                sprintf(str, "HIDAPI_%p", os_dev);
                dev->run_loop_mode =
                    CFStringCreateWithCString(NULL, str, kCFStringEncodingASCII);

                /* Attach the device to a Run Loop */
                IOHIDDeviceRegisterInputReportCallback(
                    os_dev, dev->input_report_buf, dev->max_input_report_len,
                    &hid_report_callback, dev);
                IOHIDDeviceRegisterRemovalCallback(dev->device_handle, hid_device_removal_callback, dev);

                /* Start the read thread */
                pthread_create(&dev->thread, NULL, read_thread, dev);

                /* Wait here for the read thread to be initialized. */
                pthread_barrier_wait(&dev->barrier);

                return dev;
            }
            else {
                goto return_error;
            }
        }
    }

return_error:
    free(device_array);
    CFRelease(device_set);
    free_hid_device(dev);
    return NULL;
}