Commit Graph

486 Commits

Author SHA1 Message Date
Luca Longinotti
b239bfdedd Make timeouts long: they are unsigned ints in C, and only a Java long can properly represent the full range.
It would be counter-intuitive to have to set negative timeouts in Java to get longer timeouts in C...
Also reorder getVersion() and getStringDescriptorAscii() to their proper places, so they are in the same order
as they appear in the libusbx documentation.
2013-06-16 17:20:19 +02:00
Luca Longinotti
58a2494e4c Use C99-style for-loops, much more readable. 2013-06-14 18:14:53 +02:00
Luca Longinotti
3d939da41f Bump version for testing. 2013-06-14 17:04:15 +02:00
Luca Longinotti
eb5b993eb2 Add updated libraries for Windows and Linux x86_64. 2013-06-13 14:31:21 +02:00
Luca Longinotti
ccdc2f6022 Don't increase refcount on the device returned by getParent() or getDevice().
It's better to be like libusbx itself here and not change the behavior.
But I've added a note to explain to never unref such a device.
2013-06-13 14:26:36 +02:00
Luca Longinotti
266ab299cc Wcast-qual was useuful to discover those errors in extra(): the returned ByteBuffer is fully writable, but the backing memory isn't really intended to be written to (and you never should modify the descriptors in memory).
So fixed by getting a read-only ByteBuffer and passing that back to Java instead.
2013-06-13 13:43:27 +02:00
Luca Longinotti
0f6031ab88 Revert TransferTest to original, since null is now supported again. 2013-06-13 13:40:06 +02:00
Luca Longinotti
95ee591d46 Remove memcpy wrap stuff to support old, broken systems... memcpy is never used here, only libusb uses it and there they don't support it in any particular way, so I don't believe there should be a workaround for this that forces memcpy to be slower on all x86_64 linux systems.
Enable silent building and C99 support.
Enable lots of warnings and don't enable Werror, as there are warnings in libusb.h.
Fix all warnings. Add const to exceptions.
Make it possible to pass null to setDevHandle and setBuffer in Transfer now.
2013-06-13 12:59:42 +02:00
Luca Longinotti
b3e261f050 Fix tests: use direct int buffers where needed, it's Implicit and not Explicit, the class 5 is known.
Setting devHandle or buffer to NULL in Transfer is not supported (doesn't make sense at all...).
2013-06-12 23:18:39 +02:00
Luca Longinotti
8967434994 Fix descriptor dumps. Forgot to shift the masked values, other minor changes. 2013-06-12 23:09:58 +02:00
Luca Longinotti
5b58cb0302 Issue expected exception when default context refcount is zero or smaller.
Make sure that returning a new reference always leads to a new Java object being created, else
freeing/unref'ing that one would lead to the reset of the original object too.
Furthermore, ensure that getDevice also increments the reference count, since if you then free it,
which is usual and not forbidden (libusbx docs just tell you are not obligated to unref it), the
reference count would be wrong and lead to a segfault. This way it would lead at most to keeping a
device around, instead of segfaulting. Still, always free your references!!!
2013-06-12 22:22:58 +02:00
Luca Longinotti
a4d3ab69da Destroying the default context twice, even if it is refcounted, results in a segfault.
Fix submitted to libusbx as pull request #116.
Also fix this inside the Java wrapper, by adding our own refcount (similar to what was in original usb4java,
but allowing multiple calls with the default context), and making the access to said variable thread-safe.
Once libusbx fixes this, the above code can be removed.
2013-06-12 21:39:02 +02:00
Luca Longinotti
0ba3d8869f Fixed test to also use the pointer instead of the Context hashCode. 2013-06-12 18:45:31 +02:00
Luca Longinotti
ea680a9be7 Add linux and windows 64bit updated builds for more testing (linux works at a basic level). 2013-06-12 14:45:08 +02:00
Luca Longinotti
032b7487d4 Add a few capability constants that were missing, from libusbx-1.0.15. 2013-06-12 14:44:24 +02:00
Luca Longinotti
81eea49213 Change to long, this makes sure there will be no collisions or problems in the HashMap even on 64bits.
On 32bits the pointer will only have at most 32bits set, so converting it to a void * (which also is 32bits) will not actually loose any information.
2013-06-12 14:07:42 +02:00
Luca Longinotti
275bc37a77 Right now if you add different PollfdListeners to different contexts, only the last one will be remembered, and subsequent callbacks, even from other contexts, will always use that last one.
This is obviously unexpected and incorrect, yet cannot be fixed by just adding more data on the C side: you'd have to remember the callback object at the very least, which you could in the user_data
field, but you'd have to make it a global reference. Yet, there would be no easy way to later correctly delete that global reference, since to unset the pollfdNotifiers you just pass NULL to the
same function again, and you don't get back any data on what was there before.
The easiest way to fix this was thus to change to a HashMap, Concurrent since access can be from random threads.
The key was chosen to be the Context.hashCode(), as Integer lookup and comparison is fast, and the hashCode depends directly on the contextPointer stored in the Context (which identifies it).
This works for sure on 32bit systems, but I'm still concerned about 64bit systems, where a long would probably make more sense, I need to think about it some more.
2013-06-12 13:38:10 +02:00
Luca Longinotti
a47e3cd3c3 Implement the remaining Control Transfer fill functions and the ControlSetup object.
Everything is done in Java here as it's much easier and cleaner, since those are only convenience functions/structs.
Byte order conversion is conveniently handled by ByteBuffer too, and using slice() we access the same memory all around.
Added some final keywords to LibUsb and BufferUtils.
2013-06-11 18:08:51 +02:00
Luca Longinotti
13e83fd59e Add isochronous transfer functions and async transfer fill functions. They are implemented directly in Java since they are static inline convenience functions in libusb.h and don't really need to call into C via JNI at all to work, this keeps the JNI layer simple and minimal.
Only fill_control/get_control is still outstanding.
getDescriptor() and getStringDescriptor() were removed from native JNI, since they are static inline convenience functions in libusb.h and don't do anything more than calling the appropriate real function.
2013-06-11 15:48:16 +02:00
Luca Longinotti
db62b8496b Add IsoPacketDescriptor.c to Makefile. 2013-06-11 13:14:04 +02:00
Luca Longinotti
95534657c9 Transfer constructor also package private, since you never create them but only get them through allocTransfer(). 2013-06-11 13:06:02 +02:00
Luca Longinotti
51c07e1dbc Add IsoPacketDescriptor objects and get them from the Transfer.
Also add equals/hashcode for Transfer and IsoPacketDescriptor.
2013-06-11 10:32:43 +02:00
Luca Longinotti
47543fa352 Constify wrapper functions (set and wrap). 2013-06-11 09:37:45 +02:00
Luca Longinotti
fddb83de2f Add submitTransfer and cancelTransfer. Rudimentary async I/O should now work (not tested yet!). 2013-06-10 23:32:51 +02:00
Luca Longinotti
d26a0e0d62 Fix NULL deref in InterfaceDescriptor too. 2013-06-10 20:25:17 +02:00
Luca Longinotti
09cf25cfae Fix NULL dereferences. Using the result of unwrap*() directly can lead to dereferencing NULL, for example if called on an already freed object.
InterfaceDescriptor still to do.
2013-06-10 17:42:27 +02:00
Luca Longinotti
cac3297458 Add BufferUtils to centralize creation of appropriate Buffers for LibUsb (various types, directly allocated). 2013-06-10 16:58:41 +02:00
Luca Longinotti
dca1ed7693 Finish work on Transfer implementation. Everything is consistent, fast callbacks are provided, support for FREE_TRANSFER is there and fully cleans up resources.
To support FREE_TRANSFER in the case the user doesn't set (or explicitely sets) a callback of null on the Java side, a small C-callback was created that just takes care of cleaning up the Java objects and resources.
2013-06-10 16:00:17 +02:00
Luca Longinotti
f04e3e4381 Rename trans to transfer inside LibUsb functions, like in Transfer.c.
Add methodID field to transfer_data structure, so that it can be cached at set() time and the callback is faster.
Also update the length when setting a new buffer!
2013-06-10 14:37:30 +02:00
Luca Longinotti
c1c32c3faf Add new transfer_data structure (Java and other objects). Update transfer functions accordingly.
Check all variable names for consistency.
Fix memory leak on failed getDeviceDescriptor.
Use result == LIBUSB_SUCCESS instead of just !result, it is much clearer what the integer that you get as result actually means that way.
2013-06-10 11:39:20 +02:00
Luca Longinotti
1b93df20ca Transfer.java: rename get methods to just be the name of the field with the appropriate type, like in the other classes (Descriptors etc.). This is consistent and easy to use.
Move callback and maxNumIsoPacketSize to C, minimizing what is kept in Java: only the pointer and the transferBuffer. setLength() is still checked here, as that's easiest.
Update the tests to match the changes and add documentation on the new methods.
2013-06-10 10:16:47 +02:00
Luca Longinotti
34262a1b00 Add work on async transfers, not yet completed, I may yet change where certain data is kept and how it is
checked.
2013-06-09 23:55:34 +02:00
Luca Longinotti
2ee0b2b7fd Also do a deep-equals of the arrays and ByteBuffers. 2013-06-09 23:35:20 +02:00
Luca Longinotti
dbd2a4cf55 Version.java: add nano().
*.java: Fix code and comment formatting to be equal everywhere and conform to what I could see as being the
expected usb4java standard.
*.java: Remove Apache-Commons dependency for extremely simple HashCode/Equals involving only the pointer
variable, instead use the Eclipse-generated methods.
2013-06-09 23:03:01 +02:00
Luca Longinotti
68125967a4 LibUsb.java: add missing MASKs, precise TRANSFER_FREE_BUFFER comment, only update references after native
calls return without throwing exceptions.
DescriptorUtils.java: update to use the correct masks and variables, always return "Unknown" on a not-handled
value.
*Descriptor.java: update equals/hashcode to use all fields in the same order as they are defined (makes
checking this much easier in the future!). For example bRefresh was missing or some extra/extraLength checks.
InterfaceDescriptor: there is no need for a dump(DeviceHandle) here, so removed it and updated the call in
Interface.java.
2013-06-09 16:36:31 +02:00
Luca Longinotti
bda2fdab54 Use correct call methods, if it returns void: callVoid, else if it returns object: callObject.
Also move JVM reference to be available to all C files (needed for Transfer callback later) and initialize it
once at startup.
2013-06-09 15:16:04 +02:00
Luca Longinotti
1efc33b0f2 Add resetDevice() to also reset the Java Device object after unref, as it really shouldn't be used anymore
since you don't know if the refcount reached zero and it was deallocated. Every new reference means the
refcount gets increased AND a new reference indeed exits, so this always works, unless you let references go
out of scope or overwrite them, which is clearly incorrect in any case.
Also add missing checks for setDeviceHandle() in Transfer.
2013-06-08 00:20:12 +02:00
Luca Longinotti
00670bb48d Implement Transfer callback fully. Transfer cleanup via FREE_TRANSFER also fully guaranteed. 2013-06-07 23:11:09 +02:00
Luca Longinotti
61756ed405 Add basic callback support, still work in progress. 2013-06-07 18:45:53 +02:00
Luca Longinotti
41c92e07c6 Add support for direct allocTransfer(), without isoPackets argument.
Start working on callback support.
2013-06-07 18:24:07 +02:00
Luca Longinotti
817175b686 Only get JVM if not set already (it doesn't change). 2013-06-07 18:09:13 +02:00
Luca Longinotti
8c140fd201 Fix spacing. 2013-06-07 11:52:07 +02:00
Luca Longinotti
66017bfa88 Add new NOT_SET macro to call after NOT_NULL for functions that call set*() functions.
Right now for example, getting a new config descriptor re-using an old ConfigDescriptor object just overwrites the old pointer, without freeing it or telling the user, resulting in a memory leak.
This way, the user gets notified when he's re-using an object that he hasn't cleaned up yet, and he can react and call the appropriate free/cleanup function.
2013-06-07 11:20:58 +02:00
Luca Longinotti
6f542a6228 Separate input checking from function content by a newline, improves readability by a lot!
Add missing NOT_NULL checks.
Simplify getPortPath() ifdef.
Check for result value in libusb_init and only setContext if the call succeeded.
Use always same style of NULL checks in usb4java.h.
2013-06-07 10:51:23 +02:00
Luca Longinotti
1794bbe71d Fix handleEventsTimeoutCompleted and handleEventsCompleted, they require access to a variable representing an integer, that gets set from somewhere outside, like a transfer callback.
The only way to do this in Java is by direct buffers, which IntBuffers can be if allocated correctly, as explained in the JavaDoc.
2013-06-06 15:24:33 +02:00
Luca Longinotti
bcfca40d56 Fix signature comments. 2013-06-06 14:48:35 +02:00
Luca Longinotti
69cecdb21a Fix memory leak: if you malloc, you must also free, JNI never touches manually allocated memory.
Also, I don't see why there is any need to deviate from using a pointer to using a ByteBuffer here, it will not make memory management automatic, and just makes the DeviceDescriptor "different" for no apparent reason. So changed back to pointer.
2013-06-06 14:08:27 +02:00
Luca Longinotti
6d2afe79ac Don't create new scopes in macros, it only makes sense if you have frequent name collisions, and worsens performance.
Change DIRECT_BUFFER to not call the isDirect() method (expensive!), but instead to check the return value of getDirectBufferAddress, which is always used anyway.
It also detects cases where the JVM doesn't support direct buffers and other error conditions this way.
2013-06-06 12:04:50 +02:00
Luca Longinotti
2d5ecef390 Fix signature comments. 2013-06-06 11:32:44 +02:00
Luca Longinotti
36f14cf52d I see now why ints were used here, because all the constants are ints and as such you don't have to do annoying casts and conversions.
I agree with this on set*() calls, but I argue that it is more correct to return the appropriate, smaller type on get*() calls, the same as with the other classes: the values you can read are of appropriate, original size.
2013-06-06 10:36:49 +02:00