By Jim Connors on Oct 31, 2014
One of the challenges in creating the content for the Java One 2014 Java SE Embedded Internet of Things Hands-on-Lab concerned interacting, via Java and OSGi, with a USB temperature sensor. Unfortunately a USB communications API is not part of the Java SE standard (as of this post: Halloween 2014). So the question is, how can Java/USB communication be established, and furthermore and how does this work within the OSGi framework?
In looking around at some of the available options, we chose the javahidapi as the basis for this connectivity. As a Java/JNI wrapper around the C/C++ HID API for Linux, MacOS X and Windows, the appeal behind this API is that using it does not require the use of a custom driver for each device on each platform.
In order to operate within an OSGi framework (in this case Apache Felix 4.4), javahidapi's open source code has been slightly modified/enhanced. The end result is that an OSGi bundle is available that can be dropped into standard OSGi frameworks to support USB communication for HID devices. It does contain a native component, and for the sake of simplicity, we've decided to include a separate jar file for each supported architecture. For the OSGi enthusiast, here's what the generated MANIFEST.MF file looks like for the Linux/armhf (suitable for the Raspberry Pi) architecture:
Bundle-Name: hidapi OSGi Bundle for Linux/armhf
Bundle-NativeCode: native/linux/armv6l/libhidapi-jni-32.so; osname=Linux; processor=armv6l
Created-By: Apache Maven Bundle Plugin
Here are some pre-built hidapi OSGi bundles for popular Linux platforms:
- com.codeminders.hidapi-armhf-1.0.jar (ARMv6 hard float - Suitable for Raspberry Pi)
- com.codeminders.hidapi-armv5tel-1.0.jar (ARMv5- Suitable for Plug Computers)
- com.codeminders.hidapi-x86-1.0.jar (Linux/x86 32-bit)
- com.codeminders.hidapi-x86_64-1.0.jar (Linux/x86 64-bit)
- com.codeminders.hidapi-quark-1.0.jar (Linux/x86 Intel Quark processor)
To get a feel for the changes made to the original source, here's what was done:
- A NetBeans project was created under the Maven->OSGi Bundle category
- The Java source code for javahidapi was placed in the project's src/main/java/com/codeminders/hidapi directory
- The architecture specific native library was placed in the project's src/main/resources/native/linux/architecture directory. For example, the Linux/x86 version of the project places the libhidapi-jni-32.so file in the src/main/resources/native/linux/x86 directory.
- An Activator.java class was added in the project's src/main/java/com/codeminders/hidapi directory. In OSGi, the start() method in this class gets called when this bundle is activated. It is specified in the bundle's MANIFEST.MF file.
- The original ClassPathLibraryLoader.java file was simplified and is currently only appropriate for Linux deployments.
- As this is a Maven-based project, the project's pom.xml file was edited (here's what the x86 version looks like), such that at build time it will generate a MANIFEST.MF file similar to the one referenced above.
And here are the associated NetBeans Projects which can be used to build the 4 bundles referenced above.pull down the source for hidapi and build it for inclusion into your project. If anyone is interested in going through this exercise, I'd be glad to post the fruits of their labor here.