Changed to use libusb_handle_events() instead of
libusb_handle_events_timeout() on the read thread. This increases
performance because the read thread now doesn't get woken up for no
reason.
Also, when the read thread exits, signal any waiting threads (which are
waiting in hid_read_timeout() so that they don't block forever if another
thread calls hid_close() or if the device becomes disconnected.
This was all kicked off by some suggestions by Hans Hübner
<hans.huebner@gmail.com>
0. VDR`s `Makefile` was taken as a template [1].
1. Separate variables for flags for `CC` and `CXX` were introduced. `-c` is now passed explicitly. Further flags for `CXX` could be added like in VDR’s `Makefile`.
2. Separate variable `INCLUDES` defined which is only used for the libraries(?).
[1] http://git.gekrumbel.de/vdr.git?p=vdr.git;a=blob;f=Makefile;hb=bd61fee1e9328aec470c319494cccb90e53302c2
--
Please note that this is not build tested.
Added the HIDIOCSFEATURE and HIDIOCGFEATURE ioctl macros to hid.c so that it
will compile with older kernel headers. This of course doesn't make it _run_
with older kernels. Some distros ship older kernel headers than the running
kernel.
hid_read_timeout() will execute a blocking read up until a timeout has been
reached or data has been received.
This patch implements it on all platforms and comes from Urs Fleisch.
Commit log message by Alan Ott.
Signed-off-by: Urs Fleisch <urs.fleisch@gmail.com>
Tested-by: Alan Ott <alan@signal11.us>
We first check that the HID device is managed by the kernel before
calling libusb_detach_kernel_driver(). This will avoid side effects of
libusb_detach_kernel_driver() like also doing a
libusb_release_interface()
Ludovic's original patch modified by Alan Ott <alan@signal11.us>
The interface number is added to the hid_device_info struct. Since the
Linux/libusb implementation can't put the Usage and Usage Page in the
hid_device_info struct because it requires detaching the kernel driver, the
interface number is added instead so that users with composite HID devices
can differentiate between the interfaces on the device.
had to happen:
1. Create a hid_device_info struct for each interface which
reports to be HID, rather than one for each device.
2. Parse and report the Usage Page and Usage for each.
The problem is that the file descriptor 1 used by printf() may not be
stdout but somethink else. For example a daemon may close fds 0, 1 and 2
and reuse them to open files or sockets. So using printf() may send data
on a file or socket opened by the application.
Errors will now be logged to stderr only if DEBUG_PRINTF is defined.
read_thread(): if a transfer was in progress then
libusb_cancel_transfer() will return with no error, then we must wait to
the transfer cancellation to complete. Otherwise the transfer will/may
complete after the resources have been freed and we get a crash.
Although it works without problem, it is technically illegal to call memcpy()
with a destination of NULL, even if size is zero.
Reported-by: Ludovic Rousseau <ludovic.rousseau@gmail.com>
the past, I thought it would be a good idea to move this to hid_close(), but
I can't now understand why, as freeing these objects in read_thread() makes
sense because they are created in read_thread().
This code never made it into hid_close(), so it's now re-activated in
read_thread().
Reported-by: Ludovic Rousseau <ludovic.rousseau@gmail.com>
hid_close() would hang on pthread_join() if hid_close() was called while the
reading thread was executing read_callback() _before_ the call to
libusb_submit_transfer(). This caused libusb_cancel_transfer() to fail, and
the read thread would never stop (and hid_close() would never return).
This patch simply sets the shutdown_thread flag from read_close(). This way,
even if hid_close() is called while there is no transfer pending, the loop
in read_thread() will exit, causing read_thread() to end. For this to not
leak resources, read_thread() also now has to call libusb_cancel_transfer().
Note that necessarily, one of the calls to libusb_submit_transfer() will
fail (and return an error code), but that's ok, because one of them is
guaranteed to succeed.
Reported-by: Ludovic Rousseau <ludovic.rousseau@gmail.com>
has been initialized before hid_open_path() returns. There was a race
condition if hid_close() was called immediately after hid_open(), because
hid_close() was trying to destroy objects that read_thread() had sometimes
not created yet.
Reported-by: Ludovic Rousseau <rousseau@debian.org>
iconv_open() returns a iconv_t so is always a positive value.
In case of error the returned value is (iconv_t)-1 so compare with this
specific value instead of < 0
Signed-off-by: Alan Ott <alan@signal11.us>
iconv() returns a size_t so is always a positive value.
In case of error the returned value is (size_t)-1 so compare with this
specific value instead of < 0
Signed-off-by: Alan Ott <alan@signal11.us>
hid-libusb.c:172: warning: declaration of ‘index’ shadows a global declaration
/usr/include/string.h:487: warning: shadowed declaration is here
Use idx instead of index to avoid collision with index()
Signed-off-by: Alan Ott <alan@signal11.us>
hid-libusb.c:223: warning: array subscript is above array bounds
sizeof(wbuf) gives the size of the wbuf array in char. It is also the
number of element only if wbuf is an array of char.
Here wbuf is an array of wchar_t so the last element of the array is
wbuf[sizeof(wbuf)/sizeof(wbuf[0])-1]
Signed-off-by: Alan Ott <alan@signal11.us>