#include <IOKit/hid/IOHIDManager.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <IOKit/IOKitLib.h>
#include <CoreFoundation/CoreFoundation.h>
#include <wchar.h>
#include <locale.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include <dlfcn.h>
#include "hidapi.h"
typedef int pthread_barrierattr_t;
typedef struct pthread_barrier {
pthread_mutex_t mutex;
pthread_cond_t cond;
int count;
int trip_count;
} pthread_barrier_t;
static int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count)
{
if(count == 0) {
errno = EINVAL;
return -1;
}
if(pthread_mutex_init(&barrier->mutex, 0) < 0) {
return -1;
}
if(pthread_cond_init(&barrier->cond, 0) < 0) {
pthread_mutex_destroy(&barrier->mutex);
return -1;
}
barrier->trip_count = count;
barrier->count = 0;
return 0;
}
static int pthread_barrier_destroy(pthread_barrier_t *barrier)
{
pthread_cond_destroy(&barrier->cond);
pthread_mutex_destroy(&barrier->mutex);
return 0;
}
static int pthread_barrier_wait(pthread_barrier_t *barrier)
{
pthread_mutex_lock(&barrier->mutex);
++(barrier->count);
if(barrier->count >= barrier->trip_count)
{
barrier->count = 0;
pthread_cond_broadcast(&barrier->cond);
pthread_mutex_unlock(&barrier->mutex);
return 1;
}
else
{
pthread_cond_wait(&barrier->cond, &(barrier->mutex));
pthread_mutex_unlock(&barrier->mutex);
return 0;
}
}
static int return_data(hid_device *dev, unsigned char *data, size_t length);
struct input_report {
uint8_t *data;
size_t len;
struct input_report *next;
};
struct hid_device_ {
IOHIDDeviceRef device_handle;
int blocking;
int uses_numbered_reports;
int disconnected;
CFStringRef run_loop_mode;
CFRunLoopRef run_loop;
CFRunLoopSourceRef source;
uint8_t *input_report_buf;
CFIndex max_input_report_len;
struct input_report *input_reports;
pthread_t thread;
pthread_mutex_t mutex;
pthread_cond_t condition;
pthread_barrier_t barrier;
pthread_barrier_t shutdown_barrier;
int shutdown_thread;
};
static hid_device *new_hid_device(void)
{
hid_device *dev = calloc(1, sizeof(hid_device));
dev->device_handle = NULL;
dev->blocking = 1;
dev->uses_numbered_reports = 0;
dev->disconnected = 0;
dev->run_loop_mode = NULL;
dev->run_loop = NULL;
dev->source = NULL;
dev->input_report_buf = NULL;
dev->input_reports = NULL;
dev->shutdown_thread = 0;
pthread_mutex_init(&dev->mutex, NULL);
pthread_cond_init(&dev->condition, NULL);
pthread_barrier_init(&dev->barrier, NULL, 2);
pthread_barrier_init(&dev->shutdown_barrier, NULL, 2);
return dev;
}
static void free_hid_device(hid_device *dev)
{
if (!dev)
return;
struct input_report *rpt = dev->input_reports;
while (rpt) {
struct input_report *next = rpt->next;
free(rpt->data);
free(rpt);
rpt = next;
}
if (dev->run_loop_mode)
CFRelease(dev->run_loop_mode);
if (dev->source)
CFRelease(dev->source);
free(dev->input_report_buf);
pthread_barrier_destroy(&dev->shutdown_barrier);
pthread_barrier_destroy(&dev->barrier);
pthread_cond_destroy(&dev->condition);
pthread_mutex_destroy(&dev->mutex);
free(dev);
}
static IOHIDManagerRef hid_mgr = 0x0;
#if 0#endif
static int32_t get_int_property(IOHIDDeviceRef device, CFStringRef key)
{
CFTypeRef ref;
int32_t value;
ref = IOHIDDeviceGetProperty(device, key);
if (ref) {
if (CFGetTypeID(ref) == CFNumberGetTypeID()) {
CFNumberGetValue((CFNumberRef) ref, kCFNumberSInt32Type, &value);
return value;
}
}
return 0;
}
static unsigned short get_vendor_id(IOHIDDeviceRef device)
{
return get_int_property(device, CFSTR(kIOHIDVendorIDKey));
}
static unsigned short get_product_id(IOHIDDeviceRef device)
{
return get_int_property(device, CFSTR(kIOHIDProductIDKey));
}
static int32_t get_max_report_length(IOHIDDeviceRef device)
{
return get_int_property(device, CFSTR(kIOHIDMaxInputReportSizeKey));
}
static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t *buf, size_t len)
{
CFStringRef str;
if (!len)
return 0;
str = IOHIDDeviceGetProperty(device, prop);
buf[0] = 0;
if (str) {
CFIndex str_len = CFStringGetLength(str);
CFRange range;
CFIndex used_buf_len;
CFIndex chars_copied;
len --;
range.location = 0;
range.length = ((size_t)str_len > len)? len: (size_t)str_len;
chars_copied = CFStringGetBytes(str,
range,
kCFStringEncodingUTF32LE,
(char)'?',
FALSE,
(UInt8*)buf,
len * sizeof(wchar_t),
&used_buf_len);
if (chars_copied == len)
buf[len] = 0;
else
buf[chars_copied] = 0;
return 0;
}
else
return -1;
}
static int get_serial_number(IOHIDDeviceRef device, wchar_t *buf, size_t len)
{
return get_string_property(device, CFSTR(kIOHIDSerialNumberKey), buf, len);
}
static int get_manufacturer_string(IOHIDDeviceRef device, wchar_t *buf, size_t len)
{
return get_string_property(device, CFSTR(kIOHIDManufacturerKey), buf, len);
}
static int get_product_string(IOHIDDeviceRef device, wchar_t *buf, size_t len)
{
return get_string_property(device, CFSTR(kIOHIDProductKey), buf, len);
}
static wchar_t *dup_wcs(const wchar_t *s)
{
size_t len = wcslen(s);
wchar_t *ret = malloc((len+1)*sizeof(wchar_t));
wcscpy(ret, s);
return ret;
}
static io_service_t hidapi_IOHIDDeviceGetService(IOHIDDeviceRef device)
{
static void *iokit_framework = NULL;
static io_service_t (*dynamic_IOHIDDeviceGetService)(IOHIDDeviceRef device) = NULL;
if (iokit_framework == NULL) {
iokit_framework = dlopen("/System/Library/IOKit.framework/IOKit", RTLD_LAZY);
if (iokit_framework != NULL)
dynamic_IOHIDDeviceGetService = dlsym(iokit_framework, "IOHIDDeviceGetService");
}
if (dynamic_IOHIDDeviceGetService != NULL) {
return dynamic_IOHIDDeviceGetService(device);
}
else
{
struct IOHIDDevice_internal {
uintptr_t a;
uint8_t b[4];
#if __LP64__
uint32_t c;
#endif
io_service_t service;
};
struct IOHIDDevice_internal *tmp = (struct IOHIDDevice_internal *)device;
return tmp->service;
}
}
static int init_hid_manager(void)
{
hid_mgr = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (hid_mgr) {
IOHIDManagerSetDeviceMatching(hid_mgr, NULL);
IOHIDManagerScheduleWithRunLoop(hid_mgr, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
return 0;
}
return -1;
}
int HID_API_EXPORT hid_init(void)
{
if (!hid_mgr) {
return init_hid_manager();
}
return 0;
}
int HID_API_EXPORT hid_exit(void)
{
if (hid_mgr) {
IOHIDManagerClose(hid_mgr, kIOHIDOptionsTypeNone);
CFRelease(hid_mgr);
hid_mgr = NULL;
}
return 0;
}
static void process_pending_events(void) {
SInt32 res;
do {
res = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.001, FALSE);
} while(res != kCFRunLoopRunFinished && res != kCFRunLoopRunTimedOut);
}
struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
{
struct hid_device_info *root = NULL;
struct hid_device_info *cur_dev = NULL;
CFIndex num_devices;
int i;
if (hid_init() < 0)
return NULL;
process_pending_events();
IOHIDManagerSetDeviceMatching(hid_mgr, NULL);
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++) {
unsigned short dev_vid;
unsigned short dev_pid;
#define BUF_LEN 256
wchar_t buf[BUF_LEN];
IOHIDDeviceRef dev = device_array[i];
if (!dev) {
continue;
}
dev_vid = get_vendor_id(dev);
dev_pid = get_product_id(dev);
if ((vendor_id == 0x0 || vendor_id == dev_vid) &&
(product_id == 0x0 || product_id == dev_pid)) {
struct hid_device_info *tmp;
io_object_t iokit_dev;
kern_return_t res;
io_string_t path;
tmp = malloc(sizeof(struct hid_device_info));
if (cur_dev) {
cur_dev->next = tmp;
}
else {
root = tmp;
}
cur_dev = tmp;
cur_dev->usage_page = get_int_property(dev, CFSTR(kIOHIDPrimaryUsagePageKey));
cur_dev->usage = get_int_property(dev, CFSTR(kIOHIDPrimaryUsageKey));
cur_dev->next = NULL;
iokit_dev = hidapi_IOHIDDeviceGetService(dev);
res = IORegistryEntryGetPath(iokit_dev, kIOServicePlane, path);
if (res == KERN_SUCCESS)
cur_dev->path = strdup(path);
else
cur_dev->path = strdup("");
get_serial_number(dev, buf, BUF_LEN);
cur_dev->serial_number = dup_wcs(buf);
get_manufacturer_string(dev, buf, BUF_LEN);
cur_dev->manufacturer_string = dup_wcs(buf);
get_product_string(dev, buf, BUF_LEN);
cur_dev->product_string = dup_wcs(buf);
cur_dev->vendor_id = dev_vid;
cur_dev->product_id = dev_pid;
cur_dev->release_number = get_int_property(dev, CFSTR(kIOHIDVersionNumberKey));
cur_dev->interface_number = -1;
}
}
free(device_array);
CFRelease(device_set);
return root;
}
void HID_API_EXPORT hid_free_enumeration(struct hid_device_info *devs)
{
struct hid_device_info *d = devs;
while (d) {
struct hid_device_info *next = d->next;
free(d->path);
free(d->serial_number);
free(d->manufacturer_string);
free(d->product_string);
free(d);
d = next;
}
}
hid_device * HID_API_EXPORT hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
{
struct hid_device_info *devs, *cur_dev;
const char *path_to_open = NULL;
hid_device * handle = NULL;
devs = hid_enumerate(vendor_id, product_id);
cur_dev = devs;
while (cur_dev) {
if (cur_dev->vendor_id == vendor_id &&
cur_dev->product_id == product_id) {
if (serial_number) {
if (wcscmp(serial_number, cur_dev->serial_number) == 0) {
path_to_open = cur_dev->path;
break;
}
}
else {
path_to_open = cur_dev->path;
break;
}
}
cur_dev = cur_dev->next;
}
if (path_to_open) {
handle = hid_open_path(path_to_open);
}
hid_free_enumeration(devs);
return handle;
}
static void hid_device_removal_callback(void *context, IOReturn result,
void *sender)
{
hid_device *d = context;
d->disconnected = 1;
CFRunLoopStop(d->run_loop);
}
static void hid_report_callback(void *context, IOReturn result, void *sender,
IOHIDReportType report_type, uint32_t report_id,
uint8_t *report, CFIndex report_length)
{
struct input_report *rpt;
hid_device *dev = context;
rpt = calloc(1, sizeof(struct input_report));
rpt->data = calloc(1, report_length);
memcpy(rpt->data, report, report_length);
rpt->len = report_length;
rpt->next = NULL;
pthread_mutex_lock(&dev->mutex);
if (dev->input_reports == NULL) {
dev->input_reports = rpt;
}
else {
struct input_report *cur = dev->input_reports;
int num_queued = 0;
while (cur->next != NULL) {
cur = cur->next;
num_queued++;
}
cur->next = rpt;
if (num_queued > 30) {
return_data(dev, NULL, 0);
}
}
pthread_cond_signal(&dev->condition);
pthread_mutex_unlock(&dev->mutex);
}
static void perform_signal_callback(void *context)
{
hid_device *dev = context;
CFRunLoopStop(dev->run_loop);
}
static void *read_thread(void *param)
{
hid_device *dev = param;
SInt32 code;
IOHIDDeviceScheduleWithRunLoop(dev->device_handle, CFRunLoopGetCurrent(), dev->run_loop_mode);
CFRunLoopSourceContext ctx;
memset(&ctx, 0, sizeof(ctx));
ctx.version = 0;
ctx.info = dev;
ctx.perform = &perform_signal_callback;
dev->source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &ctx);
CFRunLoopAddSource(CFRunLoopGetCurrent(), dev->source, dev->run_loop_mode);
dev->run_loop = CFRunLoopGetCurrent();
pthread_barrier_wait(&dev->barrier);
while (!dev->shutdown_thread && !dev->disconnected) {
code = CFRunLoopRunInMode(dev->run_loop_mode, 1000, FALSE);
if (code == kCFRunLoopRunFinished) {
dev->disconnected = 1;
break;
}
if (code != kCFRunLoopRunTimedOut &&
code != kCFRunLoopRunHandledSource) {
dev->shutdown_thread = 1;
break;
}
}
pthread_mutex_lock(&dev->mutex);
pthread_cond_broadcast(&dev->condition);
pthread_mutex_unlock(&dev->mutex);
pthread_barrier_wait(&dev->shutdown_barrier);
return NULL;
}
hid_device * HID_API_EXPORT hid_open_path(const char *path)
{
hid_device *dev = NULL;
io_registry_entry_t entry = MACH_PORT_NULL;
dev = new_hid_device();
if (hid_init() < 0)
return NULL;
entry = IORegistryEntryFromPath(kIOMasterPortDefault, path);
if (entry == MACH_PORT_NULL) {
goto return_error;
}
dev->device_handle = IOHIDDeviceCreate(kCFAllocatorDefault, entry);
if (dev->device_handle == NULL) {
goto return_error;
}
IOReturn ret = IOHIDDeviceOpen(dev->device_handle, kIOHIDOptionsTypeSeizeDevice);
if (ret == kIOReturnSuccess) {
char str[32];
dev->max_input_report_len = (CFIndex) get_max_report_length(dev->device_handle);
dev->input_report_buf = calloc(dev->max_input_report_len, sizeof(uint8_t));
sprintf(str, "HIDAPI_%p", dev->device_handle);
dev->run_loop_mode =
CFStringCreateWithCString(NULL, str, kCFStringEncodingASCII);
IOHIDDeviceRegisterInputReportCallback(
dev->device_handle, dev->input_report_buf, dev->max_input_report_len,
&hid_report_callback, dev);
IOHIDDeviceRegisterRemovalCallback(dev->device_handle, hid_device_removal_callback, dev);
pthread_create(&dev->thread, NULL, read_thread, dev);
pthread_barrier_wait(&dev->barrier);
IOObjectRelease(entry);
return dev;
}
else {
goto return_error;
}
return_error:
if (dev->device_handle != NULL)
CFRelease(dev->device_handle);
if (entry != MACH_PORT_NULL)
IOObjectRelease(entry);
free_hid_device(dev);
return NULL;
}
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;
IOReturn res;
if (dev->disconnected)
return -1;
if (data[0] == 0x0) {
data_to_send = data+1;
length_to_send = length-1;
}
else {
data_to_send = data;
length_to_send = length;
}
if (!dev->disconnected) {
res = IOHIDDeviceSetReport(dev->device_handle,
type,
data[0],
data_to_send, length_to_send);
if (res == kIOReturnSuccess) {
return length;
}
else
return -1;
}
return -1;
}
int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
{
return set_report(dev, kIOHIDReportTypeOutput, data, length);
}
static int return_data(hid_device *dev, unsigned char *data, size_t length)
{
struct input_report *rpt = dev->input_reports;
size_t len = (length < rpt->len)? length: rpt->len;
memcpy(data, rpt->data, len);
dev->input_reports = rpt->next;
free(rpt->data);
free(rpt);
return len;
}
static int cond_wait(const hid_device *dev, pthread_cond_t *cond, pthread_mutex_t *mutex)
{
while (!dev->input_reports) {
int res = pthread_cond_wait(cond, mutex);
if (res != 0)
return res;
if (dev->shutdown_thread || dev->disconnected)
return -1;
}
return 0;
}
static int cond_timedwait(const hid_device *dev, pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
{
while (!dev->input_reports) {
int res = pthread_cond_timedwait(cond, mutex, abstime);
if (res != 0)
return res;
if (dev->shutdown_thread || dev->disconnected)
return -1;
}
return 0;
}
int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
{
int bytes_read = -1;
pthread_mutex_lock(&dev->mutex);
if (dev->input_reports) {
bytes_read = return_data(dev, data, length);
goto ret;
}
if (dev->disconnected) {
bytes_read = -1;
goto ret;
}
if (dev->shutdown_thread) {
bytes_read = -1;
goto ret;
}
if (milliseconds == -1) {
int res;
res = cond_wait(dev, &dev->condition, &dev->mutex);
if (res == 0)
bytes_read = return_data(dev, data, length);
else {
bytes_read = -1;
}
}
else if (milliseconds > 0) {
int res;
struct timespec ts;
struct timeval tv;
gettimeofday(&tv, NULL);
TIMEVAL_TO_TIMESPEC(&tv, &ts);
ts.tv_sec += milliseconds / 1000;
ts.tv_nsec += (milliseconds % 1000) * 1000000;
if (ts.tv_nsec >= 1000000000L) {
ts.tv_sec++;
ts.tv_nsec -= 1000000000L;
}
res = cond_timedwait(dev, &dev->condition, &dev->mutex, &ts);
if (res == 0)
bytes_read = return_data(dev, data, length);
else if (res == ETIMEDOUT)
bytes_read = 0;
else
bytes_read = -1;
}
else {
bytes_read = 0;
}
ret:
pthread_mutex_unlock(&dev->mutex);
return bytes_read;
}
int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
{
return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
}
int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
{
dev->blocking = !nonblock;
return 0;
}
int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
{
return set_report(dev, kIOHIDReportTypeFeature, data, length);
}
int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
{
CFIndex len = length;
IOReturn res;
if (dev->disconnected)
return -1;
res = IOHIDDeviceGetReport(dev->device_handle,
kIOHIDReportTypeFeature,
data[0],
data, &len);
if (res == kIOReturnSuccess)
return len;
else
return -1;
}
void HID_API_EXPORT hid_close(hid_device *dev)
{
if (!dev)
return;
if (!dev->disconnected) {
IOHIDDeviceRegisterInputReportCallback(
dev->device_handle, dev->input_report_buf, dev->max_input_report_len,
NULL, dev);
IOHIDDeviceRegisterRemovalCallback(dev->device_handle, NULL, dev);
IOHIDDeviceUnscheduleFromRunLoop(dev->device_handle, dev->run_loop, dev->run_loop_mode);
IOHIDDeviceScheduleWithRunLoop(dev->device_handle, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
}
dev->shutdown_thread = 1;
CFRunLoopSourceSignal(dev->source);
CFRunLoopWakeUp(dev->run_loop);
pthread_barrier_wait(&dev->shutdown_barrier);
pthread_join(dev->thread, NULL);
if (!dev->disconnected) {
IOHIDDeviceClose(dev->device_handle, kIOHIDOptionsTypeSeizeDevice);
}
pthread_mutex_lock(&dev->mutex);
while (dev->input_reports) {
return_data(dev, NULL, 0);
}
pthread_mutex_unlock(&dev->mutex);
CFRelease(dev->device_handle);
free_hid_device(dev);
}
int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
{
return get_manufacturer_string(dev->device_handle, string, maxlen);
}
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
{
return get_product_string(dev->device_handle, string, maxlen);
}
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
{
return get_serial_number(dev->device_handle, string, maxlen);
}
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
{
return 0;
}
HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev)
{
return NULL;
}
#if 0#endif