summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorIlkka Tuohela <hile@iki.fi>2014-03-10 07:15:32 +0200
committerIlkka Tuohela <hile@iki.fi>2014-03-10 07:15:32 +0200
commit96b0abfcddfdf25f4f9a0e6013a8b79a8faf821f (patch)
tree232e4c3488c433e200435ef48a96974d1d195181 /lib
parent84646eddd46b16de0ea8db9f6c16ef6cbf078820 (diff)
Import files from hidapi git trunk
Diffstat (limited to 'lib')
-rw-r--r--lib/hidapi-0.8.0-pre/LICENSE.txt2
-rw-r--r--lib/hidapi-0.8.0-pre/README.txt278
-rw-r--r--lib/hidapi-0.8.0-pre/hidapi/hidapi.h12
-rw-r--r--lib/hidapi-0.8.0-pre/linux/hid.c508
-rw-r--r--lib/hidapi-0.8.0-pre/mac/hid.c264
-rw-r--r--lib/hidapi-0.8.0-pre/windows/Makefile.mingw7
-rwxr-xr-x[-rw-r--r--]lib/hidapi-0.8.0-pre/windows/hid.c224
7 files changed, 837 insertions, 458 deletions
diff --git a/lib/hidapi-0.8.0-pre/LICENSE.txt b/lib/hidapi-0.8.0-pre/LICENSE.txt
index 3bbd95eb4e..e1676d4c42 100644
--- a/lib/hidapi-0.8.0-pre/LICENSE.txt
+++ b/lib/hidapi-0.8.0-pre/LICENSE.txt
@@ -1,6 +1,6 @@
HIDAPI can be used under one of three licenses.
-1. The GNU Public License, version 3.0, in LICENSE-gpl3.txt
+1. The GNU General Public License, version 3.0, in LICENSE-gpl3.txt
2. A BSD-Style License, in LICENSE-bsd.txt.
3. The more liberal original HIDAPI license. LICENSE-orig.txt
diff --git a/lib/hidapi-0.8.0-pre/README.txt b/lib/hidapi-0.8.0-pre/README.txt
index 2d14317d4a..b4e7c0e0da 100644
--- a/lib/hidapi-0.8.0-pre/README.txt
+++ b/lib/hidapi-0.8.0-pre/README.txt
@@ -1,17 +1,20 @@
-HID API for Windows, Linux, and Mac OS X
+ HIDAPI library for Windows, Linux, FreeBSD and Mac OS X
+ =========================================================
About
-------
+======
HIDAPI is a multi-platform library which allows an application to interface
-with USB and Bluetooth HID-Class devices on Windows, Linux, and Mac OS X.
-On Windows, a DLL is built. On other platforms (and optionally on Windows),
-the single source file can simply be dropped into a target application.
+with USB and Bluetooth HID-Class devices on Windows, Linux, FreeBSD, and Mac
+OS X. HIDAPI can be either built as a shared library (.so or .dll) or
+can be embedded directly into a target application by adding a single source
+file (per platform) and a single header.
HIDAPI has four back-ends:
* Windows (using hid.dll)
* Linux/hidraw (using the Kernel's hidraw driver)
* Linux/libusb (using libusb-1.0)
+ * FreeBSD (using libusb-1.0)
* Mac (using IOHidManager)
On Linux, either the hidraw or the libusb back-end can be used. There are
@@ -26,21 +29,26 @@ hidraw nodes associated with them. Keyboards, mice, and some other devices
which are blacklisted from having hidraw nodes will not work. Fortunately,
for nearly all the uses of hidraw, this is not a problem.
-Linux/libusb (linux/hid-libusb.c):
+Linux/FreeBSD/libusb (libusb/hid-libusb.c):
This back-end uses libusb-1.0 to communicate directly to a USB device. This
back-end will of course not work with Bluetooth devices.
+HIDAPI also comes with a Test GUI. The Test GUI is cross-platform and uses
+Fox Toolkit (http://www.fox-toolkit.org). It will build on every platform
+which HIDAPI supports. Since it relies on a 3rd party library, building it
+is optional but recommended because it is so useful when debugging hardware.
+
What Does the API Look Like?
------------------------------
+=============================
The API provides the the most commonly used HID functions including sending
and receiving of input, output, and feature reports. The sample program,
-which communicates with a heavily modified version the USB Generic HID
-sample which is part of the Microchip Application Library (in folder
-"Microchip Solutions\USB Device - HID - Custom Demos\Generic HID - Firmware"
-when the Microchip Application Framework is installed), looks like this
-(with error checking removed for simplicity):
+which communicates with a heavily hacked up version of the Microchip USB
+Generic HID sample looks like this (with error checking removed for
+simplicity):
+#ifdef WIN32
#include <windows.h>
+#endif
#include <stdio.h>
#include <stdlib.h>
#include "hidapi.h"
@@ -55,6 +63,9 @@ int main(int argc, char* argv[])
hid_device *handle;
int i;
+ // Initialize the hidapi library
+ res = hid_init();
+
// Open the device using the VID, PID,
// and optionally the Serial number.
handle = hid_open(0x4d8, 0x3f, NULL);
@@ -86,54 +97,184 @@ int main(int argc, char* argv[])
res = hid_write(handle, buf, 65);
// Read requested state
- hid_read(handle, buf, 65);
+ res = hid_read(handle, buf, 65);
// Print out the returned buffer.
for (i = 0; i < 4; i++)
printf("buf[%d]: %d\n", i, buf[i]);
+ // Finalize the hidapi library
+ res = hid_exit();
+
return 0;
}
+If you have your own simple test programs which communicate with standard
+hardware development boards (such as those from Microchip, TI, Atmel,
+FreeScale and others), please consider sending me something like the above
+for inclusion into the HIDAPI source. This will help others who have the
+same hardware as you do.
+
License
---------
+========
HIDAPI may be used by one of three licenses as outlined in LICENSE.txt.
Download
----------
-It can be downloaded from github
+=========
+HIDAPI can be downloaded from github
git clone git://github.com/signal11/hidapi.git
Build Instructions
--------------------
-To build the console test program:
- Windows:
- Build the .sln file in the windows/ directory.
- Linux:
- cd to the linux/ directory and run make.
- Mac OS X:
- cd to the mac/ directory and run make.
-
-To build the Test GUI:
- The test GUI uses Fox toolkit, available from www.fox-toolkit.org.
- On Debian-based systems such as Ubuntu, install Fox using the following:
- sudo apt-get install libfox-1.6-dev
- On Mac OSX, install Fox from ports:
- sudo port install fox
- On Windows, download the hidapi-externals.zip file from the main download
- site and extract it just outside of hidapi, so that hidapi-externals and
- hidapi are on the same level, as shown:
-
- Parent_Folder
- |
- +hidapi
- +hidapi-externals
-
- Then to build:
- On Windows, build the .sln file in the testgui/ directory.
- On Linux and Mac, run make from the testgui/ directory.
-
-To build using the DDK (old method):
+===================
+
+This section is long. Don't be put off by this. It's not long because it's
+complicated to build HIDAPI; it's quite the opposite. This section is long
+because of the flexibility of HIDAPI and the large number of ways in which
+it can be built and used. You will likely pick a single build method.
+
+HIDAPI can be built in several different ways. If you elect to build a
+shared library, you will need to build it from the HIDAPI source
+distribution. If you choose instead to embed HIDAPI directly into your
+application, you can skip the building and look at the provided platform
+Makefiles for guidance. These platform Makefiles are located in linux/
+libusb/ mac/ and windows/ and are called Makefile-manual. In addition,
+Visual Studio projects are provided. Even if you're going to embed HIDAPI
+into your project, it is still beneficial to build the example programs.
+
+
+Prerequisites:
+---------------
+
+ Linux:
+ -------
+ On Linux, you will need to install development packages for libudev,
+ libusb and optionally Fox-toolkit (for the test GUI). On
+ Debian/Ubuntu systems these can be installed by running:
+ sudo apt-get install libudev-dev libusb-1.0-0-dev libfox-1.6-dev
+
+ If you downloaded the source directly from the git repository (using
+ git clone), you'll need Autotools:
+ sudo apt-get install autotools-dev autoconf automake libtool
+
+ FreeBSD:
+ ---------
+ On FreeBSD you will need to install GNU make, libiconv, and
+ optionally Fox-Toolkit (for the test GUI). This is done by running
+ the following:
+ pkg_add -r gmake libiconv fox16
+
+ If you downloaded the source directly from the git repository (using
+ git clone), you'll need Autotools:
+ pkg_add -r autotools
+
+ Mac:
+ -----
+ On Mac, you will need to install Fox-Toolkit if you wish to build
+ the Test GUI. There are two ways to do this, and each has a slight
+ complication. Which method you use depends on your use case.
+
+ If you wish to build the Test GUI just for your own testing on your
+ own computer, then the easiest method is to install Fox-Toolkit
+ using ports:
+ sudo port install fox
+
+ If you wish to build the TestGUI app bundle to redistribute to
+ others, you will need to install Fox-toolkit from source. This is
+ because the version of fox that gets installed using ports uses the
+ ports X11 libraries which are not compatible with the Apple X11
+ libraries. If you install Fox with ports and then try to distribute
+ your built app bundle, it will simply fail to run on other systems.
+ To install Fox-Toolkit manually, download the source package from
+ http://www.fox-toolkit.org, extract it, and run the following from
+ within the extracted source:
+ ./configure && make && make install
+
+ Windows:
+ ---------
+ On Windows, if you want to build the test GUI, you will need to get
+ the hidapi-externals.zip package from the download site. This
+ contains pre-built binaries for Fox-toolkit. Extract
+ hidapi-externals.zip just outside of hidapi, so that
+ hidapi-externals and hidapi are on the same level, as shown:
+
+ Parent_Folder
+ |
+ +hidapi
+ +hidapi-externals
+
+ Again, this step is not required if you do not wish to build the
+ test GUI.
+
+
+Building HIDAPI into a shared library on Unix Platforms:
+---------------------------------------------------------
+
+On Unix-like systems such as Linux, FreeBSD, Mac, and even Windows, using
+Mingw or Cygwin, the easiest way to build a standard system-installed shared
+library is to use the GNU Autotools build system. If you checked out the
+source from the git repository, run the following:
+
+ ./bootstrap
+ ./configure
+ make
+ make install <----- as root, or using sudo
+
+If you downloaded a source package (ie: if you did not run git clone), you
+can skip the ./bootstrap step.
+
+./configure can take several arguments which control the build. The two most
+likely to be used are:
+ --enable-testgui
+ Enable build of the Test GUI. This requires Fox toolkit to
+ be installed. Instructions for installing Fox-Toolkit on
+ each platform are in the Prerequisites section above.
+
+ --prefix=/usr
+ Specify where you want the output headers and libraries to
+ be installed. The example above will put the headers in
+ /usr/include and the binaries in /usr/lib. The default is to
+ install into /usr/local which is fine on most systems.
+
+Building the manual way on Unix platforms:
+-------------------------------------------
+
+Manual Makefiles are provided mostly to give the user and idea what it takes
+to build a program which embeds HIDAPI directly inside of it. These should
+really be used as examples only. If you want to build a system-wide shared
+library, use the Autotools method described above.
+
+ To build HIDAPI using the manual makefiles, change to the directory
+ of your platform and run make. For example, on Linux run:
+ cd linux/
+ make -f Makefile-manual
+
+ To build the Test GUI using the manual makefiles:
+ cd testgui/
+ make -f Makefile-manual
+
+Building on Windows:
+---------------------
+
+To build the HIDAPI DLL on Windows using Visual Studio, build the .sln file
+in the windows/ directory.
+
+To build the Test GUI on windows using Visual Studio, build the .sln file in
+the testgui/ directory.
+
+To build HIDAPI using MinGW or Cygwin using Autotools, use the instructions
+in the section titled "Building HIDAPI into a shared library on Unix
+Platforms" above. Note that building the Test GUI with MinGW or Cygwin will
+require the Windows procedure in the Prerequisites section above (ie:
+hidapi-externals.zip).
+
+To build HIDAPI using MinGW using the Manual Makefiles, see the section
+"Building the manual way on Unix platforms" above.
+
+HIDAPI can also be built using the Windows DDK (now also called the Windows
+Driver Kit or WDK). This method was originally required for the HIDAPI build
+but not anymore. However, some users still prefer this method. It is not as
+well supported anymore but should still work. Patches are welcome if it does
+not. To build using the DDK:
1. Install the Windows Driver Kit (WDK) from Microsoft.
2. From the Start menu, in the Windows Driver Kits folder, select Build
@@ -146,8 +287,53 @@ To build using the DDK (old method):
by the build system which is appropriate for your environment. On
Windows XP, this directory is objfre_wxp_x86/i386.
---------------------------------
+Cross Compiling
+================
+
+This section talks about cross compiling HIDAPI for Linux using autotools.
+This is useful for using HIDAPI on embedded Linux targets. These
+instructions assume the most raw kind of embedded Linux build, where all
+prerequisites will need to be built first. This process will of course vary
+based on your embedded Linux build system if you are using one, such as
+OpenEmbedded or Buildroot.
+
+For the purpose of this section, it will be assumed that the following
+environment variables are exported.
+
+ $ export STAGING=$HOME/out
+ $ export HOST=arm-linux
+
+STAGING and HOST can be modified to suit your setup.
+
+Prerequisites
+--------------
+
+Note that the build of libudev is the very basic configuration.
+
+Build Libusb. From the libusb source directory, run:
+ ./configure --host=$HOST --prefix=$STAGING
+ make
+ make install
+
+Build libudev. From the libudev source directory, run:
+ ./configure --disable-gudev --disable-introspection --disable-hwdb \
+ --host=$HOST --prefix=$STAGING
+ make
+ make install
+
+Building HIDAPI
+----------------
+
+Build HIDAPI:
+
+ PKG_CONFIG_DIR= \
+ PKG_CONFIG_LIBDIR=$STAGING/lib/pkgconfig:$STAGING/share/pkgconfig \
+ PKG_CONFIG_SYSROOT_DIR=$STAGING \
+ ./configure --host=$HOST --prefix=$STAGING
+
Signal 11 Software - 2010-04-11
2010-07-28
2011-09-10
+ 2012-05-01
+ 2012-07-03
diff --git a/lib/hidapi-0.8.0-pre/hidapi/hidapi.h b/lib/hidapi-0.8.0-pre/hidapi/hidapi.h
index 6c8c483964..5730f86769 100644
--- a/lib/hidapi-0.8.0-pre/hidapi/hidapi.h
+++ b/lib/hidapi-0.8.0-pre/hidapi/hidapi.h
@@ -11,7 +11,7 @@
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
+ GNU General 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.
@@ -112,6 +112,8 @@ extern "C" {
This function returns a linked list of all the HID devices
attached to the system which match vendor_id and product_id.
+ If @p vendor_id is set to 0 then any vendor matches.
+ If @p product_id is set to 0 then any product matches.
If @p vendor_id and @p product_id are both set to 0, then
all HID devices will be returned.
@@ -155,7 +157,7 @@ extern "C" {
This function returns a pointer to a #hid_device object on
success or NULL on failure.
*/
- HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, wchar_t *serial_number);
+ HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number);
/** @brief Open a HID device by its path name.
@@ -216,7 +218,8 @@ extern "C" {
@returns
This function returns the actual number of bytes read and
- -1 on error.
+ -1 on error. If no packet was available to be read within
+ the timeout period, this function returns 0.
*/
int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds);
@@ -235,7 +238,8 @@ extern "C" {
@returns
This function returns the actual number of bytes read and
- -1 on error.
+ -1 on error. If no packet was available to be read and
+ the handle is in non-blocking mode, this function returns 0.
*/
int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length);
diff --git a/lib/hidapi-0.8.0-pre/linux/hid.c b/lib/hidapi-0.8.0-pre/linux/hid.c
index 4e3e5c81ae..bb206e8b37 100644
--- a/lib/hidapi-0.8.0-pre/linux/hid.c
+++ b/lib/hidapi-0.8.0-pre/linux/hid.c
@@ -9,10 +9,10 @@
Linux Version - 6/2/2009
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
+ GNU General 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.
@@ -40,6 +40,7 @@
/* Linux */
#include <linux/hidraw.h>
#include <linux/version.h>
+#include <linux/input.h>
#include <libudev.h>
#include "hidapi.h"
@@ -53,6 +54,23 @@
#define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len)
#endif
+
+/* USB HID device property names */
+const char *device_string_names[] = {
+ "manufacturer",
+ "product",
+ "serial",
+};
+
+/* Symbolic names for the properties above */
+enum device_string_id {
+ DEVICE_STRING_MANUFACTURER,
+ DEVICE_STRING_PRODUCT,
+ DEVICE_STRING_SERIAL,
+
+ DEVICE_STRING_COUNT,
+};
+
struct hid_device_ {
int device_handle;
int blocking;
@@ -62,7 +80,28 @@ struct hid_device_ {
static __u32 kernel_version = 0;
-hid_device *new_hid_device()
+static __u32 detect_kernel_version(void)
+{
+ struct utsname name;
+ int major, minor, release;
+ int ret;
+
+ uname(&name);
+ ret = sscanf(name.release, "%d.%d.%d", &major, &minor, &release);
+ if (ret == 3) {
+ return KERNEL_VERSION(major, minor, release);
+ }
+
+ ret = sscanf(name.release, "%d.%d", &major, &minor);
+ if (ret == 2) {
+ return KERNEL_VERSION(major, minor, 0);
+ }
+
+ printf("Couldn't determine kernel version from version string \"%s\"\n", name.release);
+ return 0;
+}
+
+static hid_device *new_hid_device(void)
{
hid_device *dev = calloc(1, sizeof(hid_device));
dev->device_handle = -1;
@@ -72,36 +111,39 @@ hid_device *new_hid_device()
return dev;
}
-static void register_error(hid_device *device, const char *op)
+
+/* The caller must free the returned string with free(). */
+static wchar_t *utf8_to_wchar_t(const char *utf8)
{
+ wchar_t *ret = NULL;
+
+ if (utf8) {
+ size_t wlen = mbstowcs(NULL, utf8, 0);
+ if ((size_t) -1 == wlen) {
+ return wcsdup(L"");
+ }
+ ret = calloc(wlen+1, sizeof(wchar_t));
+ mbstowcs(ret, utf8, wlen+1);
+ ret[wlen] = 0x0000;
+ }
+ return ret;
}
/* Get an attribute value from a udev_device and return it as a whar_t
string. The returned string must be freed with free() when done.*/
static wchar_t *copy_udev_string(struct udev_device *dev, const char *udev_name)
{
- const char *str;
- wchar_t *ret = NULL;
- str = udev_device_get_sysattr_value(dev, udev_name);
- if (str) {
- /* Convert the string from UTF-8 to wchar_t */
- size_t wlen = mbstowcs(NULL, str, 0);
- ret = calloc(wlen+1, sizeof(wchar_t));
- mbstowcs(ret, str, wlen+1);
- ret[wlen] = 0x0000;
- }
-
- return ret;
+ return utf8_to_wchar_t(udev_device_get_sysattr_value(dev, udev_name));
}
/* uses_numbered_reports() returns 1 if report_descriptor describes a device
- which contains numbered reports. */
+ which contains numbered reports. */
static int uses_numbered_reports(__u8 *report_descriptor, __u32 size) {
- int i = 0;
+ unsigned int i = 0;
int size_code;
int data_len, key_size;
-
+
while (i < size) {
int key = report_descriptor[i];
@@ -111,9 +153,9 @@ static int uses_numbered_reports(__u8 *report_descriptor, __u32 size) {
numbered reports. */
return 1;
}
-
+
//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.
@@ -148,22 +190,82 @@ static int uses_numbered_reports(__u8 *report_descriptor, __u32 size) {
};
key_size = 1;
}
-
+
/* Skip over this key and it's associated data */
i += data_len + key_size;
}
-
+
/* Didn't find a Report ID key. Device doesn't use numbered reports. */
return 0;
}
-static int get_device_string(hid_device *dev, const char *key, wchar_t *string, size_t maxlen)
+/*
+ * The caller is responsible for free()ing the (newly-allocated) character
+ * strings pointed to by serial_number_utf8 and product_name_utf8 after use.
+ */
+static int
+parse_uevent_info(const char *uevent, int *bus_type,
+ unsigned short *vendor_id, unsigned short *product_id,
+ char **serial_number_utf8, char **product_name_utf8)
+{
+ char *tmp = strdup(uevent);
+ char *saveptr = NULL;
+ char *line;
+ char *key;
+ char *value;
+
+ int found_id = 0;
+ int found_serial = 0;
+ int found_name = 0;
+
+ line = strtok_r(tmp, "\n", &saveptr);
+ while (line != NULL) {
+ /* line: "KEY=value" */
+ key = line;
+ value = strchr(line, '=');
+ if (!value) {
+ goto next_line;
+ }
+ *value = '\0';
+ value++;
+
+ if (strcmp(key, "HID_ID") == 0) {
+ /**
+ * type vendor product
+ * HID_ID=0003:000005AC:00008242
+ **/
+ int ret = sscanf(value, "%x:%hx:%hx", bus_type, vendor_id, product_id);
+ if (ret == 3) {
+ found_id = 1;
+ }
+ } else if (strcmp(key, "HID_NAME") == 0) {
+ /* The caller has to free the product name */
+ *product_name_utf8 = strdup(value);
+ found_name = 1;
+ } else if (strcmp(key, "HID_UNIQ") == 0) {
+ /* The caller has to free the serial number */
+ *serial_number_utf8 = strdup(value);
+ found_serial = 1;
+ }
+
+next_line:
+ line = strtok_r(NULL, "\n", &saveptr);
+ }
+
+ free(tmp);
+ return (found_id && found_name && found_serial);
+}
+
+
+static int get_device_string(hid_device *dev, enum device_string_id key, wchar_t *string, size_t maxlen)
{
struct udev *udev;
- struct udev_device *udev_dev, *parent;
+ struct udev_device *udev_dev, *parent, *hid_dev;
struct stat s;
int ret = -1;
-
+ char *serial_number_utf8 = NULL;
+ char *product_name_utf8 = NULL;
+
/* Create the udev object */
udev = udev_new();
if (!udev) {
@@ -176,26 +278,80 @@ static int get_device_string(hid_device *dev, const char *key, wchar_t *string,
/* Open a udev device from the dev_t. 'c' means character device. */
udev_dev = udev_device_new_from_devnum(udev, 'c', s.st_rdev);
if (udev_dev) {
- const char *str;
- /* Find the parent USB Device */
- parent = udev_device_get_parent_with_subsystem_devtype(
- udev_dev,
- "usb",
- "usb_device");
- if (parent) {
- str = udev_device_get_sysattr_value(parent, key);
- if (str) {
- /* Convert the string from UTF-8 to wchar_t */
- ret = (mbstowcs(string, str, maxlen) < 0)? -1: 0;
- goto end;
+ hid_dev = udev_device_get_parent_with_subsystem_devtype(
+ udev_dev,
+ "hid",
+ NULL);
+ if (hid_dev) {
+ unsigned short dev_vid;
+ unsigned short dev_pid;
+ int bus_type;
+ size_t retm;
+
+ ret = parse_uevent_info(
+ udev_device_get_sysattr_value(hid_dev, "uevent"),
+ &bus_type,
+ &dev_vid,
+ &dev_pid,
+ &serial_number_utf8,
+ &product_name_utf8);
+
+ if (bus_type == BUS_BLUETOOTH) {
+ switch (key) {
+ case DEVICE_STRING_MANUFACTURER:
+ wcsncpy(string, L"", maxlen);
+ ret = 0;
+ break;
+ case DEVICE_STRING_PRODUCT:
+ retm = mbstowcs(string, product_name_utf8, maxlen);
+ ret = (retm == (size_t)-1)? -1: 0;
+ break;
+ case DEVICE_STRING_SERIAL:
+ retm = mbstowcs(string, serial_number_utf8, maxlen);
+ ret = (retm == (size_t)-1)? -1: 0;
+ break;
+ case DEVICE_STRING_COUNT:
+ default:
+ ret = -1;
+ break;
+ }
+ }
+ else {
+ /* This is a USB device. Find its parent USB Device node. */
+ parent = udev_device_get_parent_with_subsystem_devtype(
+ udev_dev,
+ "usb",
+ "usb_device");
+ if (parent) {
+ const char *str;
+ const char *key_str = NULL;
+
+ if (key >= 0 && key < DEVICE_STRING_COUNT) {
+ key_str = device_string_names[key];
+ } else {
+ ret = -1;
+ goto end;
+ }
+
+ str = udev_device_get_sysattr_value(parent, key_str);
+ if (str) {
+ /* Convert the string from UTF-8 to wchar_t */
+ retm = mbstowcs(string, str, maxlen);
+ ret = (retm == (size_t)-1)? -1: 0;
+ goto end;
+ }
+ }
}
}
}
end:
+ free(serial_number_utf8);
+ free(product_name_utf8);
+
udev_device_unref(udev_dev);
- // parent doesn't need to be (and can't be) unref'd.
- // I'm not sure why, but it'll throw double-free() errors.
+ /* parent and hid_dev don't need to be (and can't be) unref'd.
+ I'm not sure why, but they'll throw double-free() errors. */
udev_unref(udev);
return ret;
@@ -210,6 +366,8 @@ int HID_API_EXPORT hid_init(void)
if (!locale)
setlocale(LC_CTYPE, "");
+ kernel_version = detect_kernel_version();
+
return 0;
}
@@ -219,14 +377,16 @@ int HID_API_EXPORT hid_exit(void)
return 0;
}
+
struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
{
struct udev *udev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
-
- struct hid_device_info *root = NULL; // return object
+
+ struct hid_device_info *root = NULL; /* return object */
struct hid_device_info *cur_dev = NULL;
+ struct hid_device_info *prev_dev = NULL; /* previous device */
hid_init();
@@ -248,46 +408,57 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
const char *sysfs_path;
const char *dev_path;
const char *str;
- struct udev_device *hid_dev; // The device's HID udev node.
- struct udev_device *dev; // The actual hardware device.
- struct udev_device *intf_dev; // The device's interface (in the USB sense).
+ struct udev_device *raw_dev; /* The device's hidraw udev node. */
+ struct udev_device *hid_dev; /* The device's HID udev node. */
+ struct udev_device *usb_dev; /* The device's USB udev node. */
+ struct udev_device *intf_dev; /* The device's interface (in the USB sense). */
unsigned short dev_vid;
unsigned short dev_pid;
-
+ char *serial_number_utf8 = NULL;
+ char *product_name_utf8 = NULL;
+ int bus_type;
+ int result;
+
/* Get the filename of the /sys entry for the device
and create a udev_device object (dev) representing it */
sysfs_path = udev_list_entry_get_name(dev_list_entry);
- hid_dev = udev_device_new_from_syspath(udev, sysfs_path);
- dev_path = udev_device_get_devnode(hid_dev);
-
- /* The device pointed to by hid_dev contains information about
- the hidraw device. In order to get information about the
- USB device, get the parent device with the
- subsystem/devtype pair of "usb"/"usb_device". This will
- be several levels up the tree, but the function will find
- it.*/
- dev = udev_device_get_parent_with_subsystem_devtype(
- hid_dev,
- "usb",
- "usb_device");
- if (!dev) {
- /* Unable to find parent usb device. */
+ raw_dev = udev_device_new_from_syspath(udev, sysfs_path);
+ dev_path = udev_device_get_devnode(raw_dev);
+
+ hid_dev = udev_device_get_parent_with_subsystem_devtype(
+ raw_dev,
+ "hid",
+ NULL);
+
+ if (!hid_dev) {
+ /* Unable to find parent hid device. */
goto next;
}
- /* Get the VID/PID of the device */
- str = udev_device_get_sysattr_value(dev,"idVendor");
- dev_vid = (str)? strtol(str, NULL, 16): 0x0;
- str = udev_device_get_sysattr_value(dev, "idProduct");
- dev_pid = (str)? strtol(str, NULL, 16): 0x0;
+ result = parse_uevent_info(
+ udev_device_get_sysattr_value(hid_dev, "uevent"),
+ &bus_type,
+ &dev_vid,
+ &dev_pid,
+ &serial_number_utf8,
+ &product_name_utf8);
+
+ if (!result) {
+ /* parse_uevent_info() failed for at least one field. */
+ goto next;
+ }
+
+ if (bus_type != BUS_USB && bus_type != BUS_BLUETOOTH) {
+ /* We only know how to handle USB and BT devices. */
+ goto next;
+ }
/* Check the VID/PID against the arguments */
- if ((vendor_id == 0x0 && product_id == 0x0) ||
- (vendor_id == dev_vid && product_id == dev_pid)) {
+ if ((vendor_id == 0x0 || vendor_id == dev_vid) &&
+ (product_id == 0x0 || product_id == dev_pid)) {
struct hid_device_info *tmp;
- size_t len;
- /* VID/PID match. Create the record. */
+ /* VID/PID match. Create the record. */
tmp = malloc(sizeof(struct hid_device_info));
if (cur_dev) {
cur_dev->next = tmp;
@@ -295,63 +466,103 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
else {
root = tmp;
}
+ prev_dev = cur_dev;
cur_dev = tmp;
-
+
/* Fill out the record */
cur_dev->next = NULL;
- str = dev_path;
- if (str) {
- len = strlen(str);
- cur_dev->path = calloc(len+1, sizeof(char));
- strncpy(cur_dev->path, str, len+1);
- cur_dev->path[len] = '\0';
- }
- else
- cur_dev->path = NULL;
-
- /* Serial Number */
- cur_dev->serial_number
- = copy_udev_string(dev, "serial");
-
- /* Manufacturer and Product strings */
- cur_dev->manufacturer_string
- = copy_udev_string(dev, "manufacturer");
- cur_dev->product_string
- = copy_udev_string(dev, "product");
-
+ cur_dev->path = dev_path? strdup(dev_path): NULL;
+
/* VID/PID */
cur_dev->vendor_id = dev_vid;
cur_dev->product_id = dev_pid;
+ /* Serial Number */
+ cur_dev->serial_number = utf8_to_wchar_t(serial_number_utf8);
+
/* Release Number */
- str = udev_device_get_sysattr_value(dev, "bcdDevice");
- cur_dev->release_number = (str)? strtol(str, NULL, 16): 0x0;
-
+ cur_dev->release_number = 0x0;
+
/* Interface Number */
cur_dev->interface_number = -1;
- /* Get a handle to the interface's udev node. */
- intf_dev = udev_device_get_parent_with_subsystem_devtype(
- hid_dev,
- "usb",
- "usb_interface");
- if (intf_dev) {
- str = udev_device_get_sysattr_value(intf_dev, "bInterfaceNumber");
- cur_dev->interface_number = (str)? strtol(str, NULL, 16): -1;
+
+ switch (bus_type) {
+ case BUS_USB:
+ /* The device pointed to by raw_dev contains information about
+ the hidraw device. In order to get information about the
+ USB device, get the parent device with the
+ subsystem/devtype pair of "usb"/"usb_device". This will
+ be several levels up the tree, but the function will find
+ it. */
+ usb_dev = udev_device_get_parent_with_subsystem_devtype(
+ raw_dev,
+ "usb",
+ "usb_device");
+
+ if (!usb_dev) {
+ /* Free this device */
+ free(cur_dev->serial_number);
+ free(cur_dev->path);
+ free(cur_dev);
+
+ /* Take it off the device list. */
+ if (prev_dev) {
+ prev_dev->next = NULL;
+ cur_dev = prev_dev;
+ }
+ else {
+ cur_dev = root = NULL;
+ }
+
+ goto next;
+ }
+
+ /* Manufacturer and Product strings */
+ cur_dev->manufacturer_string = copy_udev_string(usb_dev, device_string_names[DEVICE_STRING_MANUFACTURER]);
+ cur_dev->product_string = copy_udev_string(usb_dev, device_string_names[DEVICE_STRING_PRODUCT]);
+
+ /* Release Number */
+ str = udev_device_get_sysattr_value(usb_dev, "bcdDevice");
+ cur_dev->release_number = (str)? strtol(str, NULL, 16): 0x0;
+
+ /* Get a handle to the interface's udev node. */
+ intf_dev = udev_device_get_parent_with_subsystem_devtype(
+ raw_dev,
+ "usb",
+ "usb_interface");
+ if (intf_dev) {
+ str = udev_device_get_sysattr_value(intf_dev, "bInterfaceNumber");
+ cur_dev->interface_number = (str)? strtol(str, NULL, 16): -1;
+ }
+
+ break;
+
+ case BUS_BLUETOOTH:
+ /* Manufacturer and Product strings */
+ cur_dev->manufacturer_string = wcsdup(L"");
+ cur_dev->product_string = utf8_to_wchar_t(product_name_utf8);
+
+ break;
+
+ default:
+ /* Unknown device type - this should never happen, as we
+ * check for USB and Bluetooth devices above */
+ break;
}
}
- else
- goto next;
next:
- udev_device_unref(hid_dev);
- /* dev and intf_dev don't need to be (and can't be)
+ free(serial_number_utf8);
+ free(product_name_utf8);
+ udev_device_unref(raw_dev);
+ /* hid_dev, usb_dev and intf_dev don't need to be (and can't be)
unref()d. It will cause a double-free() error. I'm not
sure why. */
}
/* Free the enumerator and udev objects. */
udev_enumerate_unref(enumerate);
udev_unref(udev);
-
+
return root;
}
@@ -369,12 +580,12 @@ void HID_API_EXPORT hid_free_enumeration(struct hid_device_info *devs)
}
}
-hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, wchar_t *serial_number)
+hid_device * 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) {
@@ -400,7 +611,7 @@ hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, wchar
}