Summary. 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 v2: BridJ (C++)”
test-wd-bridj. Note that you may need to set
The functions we are going to make available from Java are:
void setDeviceName(const char *deviceName);: Set filename of watchdog device.
const char *getErrorMessage();: Gets most recent error message. The message is allocated in C++ heap and a pointer is returned to Java application.
void clearErrorMessage();: Clear last error message and release memory.
int enable(int timeout);: Enable watchdog, parameter is timeout value.
int getRemainingTime();: Returns remaining time until watchdog fires, this is similar to the stupid countdown timer on all atomic bombs in old James Bond movies.
int getTimeout();: Return current timeout value.
int disable();: Disable watchdog
int heartbeat();: Send heartbeat to reset watchdog counter.
void destroy();: Destroy object and release memory.
All int return values < 0 indicates error.
Note that we avoid using
std::string and other STL containers in C++ class we are going to use from Java. JNAerator and/or BridJ does not like STL and vice versa. There is no problem using STL containers in C++ classes not visible to Java.
Download and launch JNAeratorStudio from this page and launch it. It is (obviously) written in Java and some browsers (eg. Chrome) may refuse to download it.
Generate Java class to interface shared library like this:
wd-bridj.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 that the generated file (
src/com/bitreactive/tutorial/foreigncode/bridj/wdbridj/WatchdogInterface.java) is already in place. You can replace it with the one that is generated now. You will need to change package name, but other than that no changes needs to be done (We removed the member variables, since we did not use them, but that is not necessary).
In addition you must add the
bridj.jar library to the project. (It 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, as long as you stay away from STL.
The block is shown in the image above and with exception of the init and stop pin, all functions 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 of the Java functions we made:
The C++ interface is allocated just as any other object, and note that clearing the reference will not call the destructor on the C++ side, so you need to handle that separately.
Pointer class contains helpers to convert between Java String and C++ character pointers.
enable functions does take one parameter, the watchdog time which is transferred to C++ library.
com.bitreactive.tutorial.foreigncode.bridj.testwdbridj_exe) as Runnable JAR and transfer to your Raspberry-Pi to the same directory as the shared library you generated in part 1.