These tutorials explains how to use foreign (as in non-Java) code in your Reactive Block application. Another term for foreign is native, because embedding non-Java code usually introduces platform and portability issues.
Make sure you have read the intro foreign-code-intro to prepare what you need for this tutorial.
Download the tutorial named “(Level 3) Foreign code v3: JNA”
test-wd-jna. Note that you may need to set
The functions we are going to make available from Java are:
void setWdDeviceName(const char *deviceName);: Set filename of watchdog device.
const char *getWdErrorMessage();: Gets most recent error message. The message is allocated in C heap and a pointer is returned to Java application.
void clearWdErrorMessage();: Clear last error message and release memory.
int enableWd(int timeout);: Enable watchdog, parameter is timeout value.
int getWdRemainingTime();: Returns remaining time until watchdog fires, this is similar to the stupid countdown timer on all atomic bombs in old James Bond movies.
int getWdTimeout();: Return current timeout value.
int disableWd();: Disable watchdog
int heartbeatWd();: Send heartbeat to reset watchdog counter.
void closeWd();: Close files and release memory.
All int return values < 0 indicates error.
Note that even if this is compiled as a C-program, you can use C++ from the different methods as long as you dont expose that in the header files.
Download and launch JNAeratorStudio from this page and launch it. It is (still) written in Java and some browsers (eg. Chrome) may refuse to download it.
Generate Java class to interface shared library like this:
wd-jna.hinto work window
The generated file will appear to the right. You can also do this process manually, but using this tool gives you a good starting point.
In the project you downloaded earlier you will see the generated file (
src/com/bitreactive/tutorial/foreigncode/jna/wdjna/WatchdogInterface.java) is already in place. You can replace it with the one that is generated now. You will need to change package name and classname, but other than that no changes needs to be done.
In addition you must add
jna.jar library to the project. (This has also already been done).
Now you are ready to use the C library from your Reactive Blocks application.
You can also read more about more complex datatypes than
const char * here. It is relatively straight forward.
The block is shown in the image above and with exception of the init and stop pin, all functions are the same as in the C library. In addition we have added a state check to query if watchdog is enabled.
We will take a closer look at some functions:
Stringare automatically converted to C
const char *when used as parameter
const char *is returned, there are functions available to easy convert that to Java
enable functions does take one parameter, the watchdog time which is transferred to C library.
Select Test WD JNA application, press Build from rightclick menu and build Java project. Select Java SE as project type.
Export the generated project (located in
com.bitreactive.tutorial.foreigncode.jna.testwdjna_exe) as Runnable JAR and transfer to your Raspberry-Pi to the same directory as the shared library you generated in part 3.