/*******************************************************
HIDAPI - Multi-Platform library for
communication with HID devices.
Alan Ott
Signal 11 Software
8/22/2009
Linux Version - 6/2/2010
Libusb Version - 8/13/2010
Copyright 2009, All Rights Reserved.
At the discretion of the user of this library,
this software may be licensed under the terms of the
GNU Public License v3, a BSD-Style license, or the
original HIDAPI license as outlined in the LICENSE.txt,
LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
files located at the root of the source distribution.
These files may also be found in the public source
code repository located at:
http://github.com/signal11/hidapi .
********************************************************/
#define _GNU_SOURCE // needed for wcsdup() before glibc 2.10
/* C */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <locale.h>
#include <errno.h>
/* Unix */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <fcntl.h>
#include <pthread.h>
#include <wchar.h>
/* GNU / LibUSB */
#include <libusb.h>
#include "iconv.h"
#include "hidapi.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef DEBUG_PRINTF
#define LOG(...) fprintf(stderr, __VA_ARGS__)
#else
#define LOG(...) do {} while (0)
#endif
/* Uncomment to enable the retrieval of Usage and Usage Page in
hid_enumerate(). Warning, this is very invasive as it requires the detach
and re-attach of the kernel driver. See comments inside hid_enumerate().
Linux/libusb HIDAPI programs are encouraged to use the interface number
instead to differentiate between interfaces on a composite HID device. */
/*#define INVASIVE_GET_USAGE*/
/* Linked List of input reports received from the device. */
struct input_report {
uint8_t *data;
size_t len;
struct input_report *next;
};
struct hid_device_ {
/* Handle to the actual device. */
libusb_device_handle *device_handle;
/* Endpoint information */
int input_endpoint;
int output_endpoint;
int input_ep_max_packet_size;
/* The interface number of the HID */
int interface;
/* Indexes of Strings */
int manufacturer_index;
int product_index;
int serial_index;
/* Whether blocking reads are used */
int blocking; /* boolean */
/* Read thread objects */
pthread_t thread;
pthread_mutex_t mutex; /* Protects input_reports */
pthread_cond_t condition;
pthread_barrier_t barrier; /* Ensures correct startup sequence */
int shutdown_thread;
struct libusb_transfer *transfer;
/* List of received input reports. */
struct input_report *input_reports;
};
static int initialized = 0;
uint16_t get_usb_code_for_current_locale(void);
static int return_data(hid_device *dev, unsigned char *data, size_t length);
static hid_device *new_hid_device(void)
{
hid_device *dev = calloc(1, sizeof(hid_device));
dev->device_handle = NULL;
dev->input_endpoint = 0;
dev->output_endpoint = 0;
dev->input_ep_max_packet_size = 0;
dev->interface = 0;
dev->manufacturer_index = 0;
dev->product_index = 0;
dev->serial_index = 0;
dev->blocking = 1;
dev->shutdown_thread = 0;
dev->transfer = NULL;
dev->input_reports = NULL;
pthread_mutex_init(&dev->mutex, NULL);
pthread_cond_init(&dev->condition, NULL);
pthread_barrier_init(&dev->barrier, NULL, 2);
return dev;
}
static void free_hid_device(hid_device *dev)
{
/* Clean up the thread objects */
pthread_barrier_destroy(&dev->barrier);
pthread_cond_destroy(&dev->condition);
pthread_mutex_destroy(&dev->mutex);
/* Free the device itself */
free(dev);
}
#if 0
//TODO: Implement this funciton on Linux.
static void register_error(hid_device *device, const char *op)
{
}
#endif
#ifdef INVASIVE_GET_USAGE
/* Get bytes from a HID Report Descriptor.
Only call with a num_bytes of 0, 1, 2, or 4. */
static uint32_t get_bytes(uint8_t *rpt, size_t len, size_t num_bytes, size_t cur)
{
/* Return if there aren't enough bytes. */
if (cur + num_bytes >= len)
return 0;
if (num_bytes == 0)
return 0;
else if (num_bytes == 1) {
return rpt[cur+1];
}
else if (num_bytes == 2) {
return (rpt[cur+2] * 256 + rpt[cur+1]);
}
else if (num_bytes == 4) {
return (rpt[cur+4] * 0x01000000 +
rpt[cur+3] * 0x00010000 +
rpt[cur+2] * 0x00000100 +
rpt[cur+1] * 0x00000001);
}
else
return 0;
}
/* Retrieves the device's Usage Page and Usage from the report
descriptor. The algorithm is simple, as it just returns the first
Usage and Usage Page that it finds in the descriptor.
The return value is 0 on success and -1 on failure. */
static int get_usage(uint8_t *report_descriptor, size_t size,
unsigned short *usage_page, unsigned short *usage)
{
int i = 0;
int size_code;
int data_len, key_size;
int usage_found = 0, usage_page_found = 0;
while (i < size) {
int key = report_descriptor[i];
int key_cmd = key & 0xfc;
//printf("key: %02hhx\n", key);
if ((key & 0xf0) == 0xf0) {
/* This is a Long Item. The next byte contains the
length of the data section (value) for this key.
See the HID specification, version 1.11, section
6.2.2.3, titled "Long Items." */
if (i+1 < size)
data_len = report_descriptor[i+1];
else
data_len = 0; /* malformed report */
key_size = 3;
}
else {
/* This is a Short Item. The bottom two bits of the
key contain the size code for the data section
(value) for this key. Refer to the HID
specification, version 1.11, section 6.2.2.2,
titled "Short Items." */
size_code = key & 0x3;
switch (size_code) {
case 0:
case 1:
case 2:
data_len = size_code;
break;
case 3:
data_len = 4;
break;
default:
/* Can't ever happen since size_code is & 0x3 */
data_len = 0;
break;
};
key_size = 1;
}
if (key_cmd == 0x4) {
*usage_page = get_bytes(report_descriptor, size, data_len, i);
usage_page_found = 1;
//printf("Usage Page: %x\n", (uint32_t)*usage_page);
}
if (key_cmd == 0x8) {
*usage = get_bytes(report_descriptor, size, data_len, i);
usage_found = 1;
//printf("Usage: %x\n", (uint32_t)*usage);
}
if (usage_page_found && usage_found)
return 0; /* success */
/* Skip over this key and it's associated data */
i += data_len + key_size;
}
return -1; /* failure */
}
#endif // INVASIVE_GET_USAGE
/* Get the first language the device says it reports. This comes from
USB string #0. */
static uint16_t get_first_language(libusb_device_handle *dev)
{
uint16_t buf[32];
int len;
/* Get the string from libusb. */
len = libusb_get_string_descriptor(dev,
0x0, /* String ID */
0x0, /* Language */
(unsigned char*)buf,
sizeof(buf));
if (len < 4)
return 0x0;
return buf[1]; // First two bytes are len and descriptor type.
}
static int is_language_supported(libusb_device_handle *dev, uint16_t lang)
{
uint16_t buf[32];
int len;
int i;
/* Get the string from libusb. */
len = libusb_get_string_descriptor(dev,
0x0, /* String ID */
0x0, /* Language */
(unsigned char*)buf,
sizeof(buf));
if (len < 4)
return 0x0;
len /= 2; /* language IDs are two-bytes each. */
/* Start at index 1 because there are two bytes of protocol data. */
for (i = 1; i < len; i++) {
if (buf[i] == lang)
return 1;
}
return 0;
}
/* This function returns a newly allocated wide string containing the USB
device string numbered by the index. The returned string must be freed
by using free(). */
static wchar_t *get_usb_string(libusb_device_handle *dev, uint8_t idx)
{
char buf[512];
int len;
wchar_t *str = NULL;
wchar_t wbuf[256];
/* iconv variables */
iconv_t ic;
size_t inbytes;
size_t outbytes;
size_t res;
char *inptr;
char *outptr;
/* Determine which language to use. */
uint16_t lang;
lang = get_usb_code_for_current_locale();
if (!is_language_supported(dev, lang))
lang = get_first_language(dev);
/* Get t