Monday, 31 July 2017

Install Intel TinyB Java bluetooth library on Raspberry PI

The TinyB project is a bluetooth LE project for the Intel Edison. It is a project that communicates to bluez through DBUS. This library save you time writing your own JNI classes to interface with DBUS.
On the Intel Edison OS you are by default the root user so there is some change for raspberry pi and additional packages you need.

First upgrade your raspberry PI to the latest raspbian software which is Debian 9 Stretch at the moment. This will give you the latest Bluetooth software.

Install this software before building TinyB
sudo apt-get install openjdk-8-jdk  libglib2.0-dev cmake bluez

Export the variable on the command line or add it to your ~/.bashrc and source it afterwards :  source  ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-armhf/ 

Read the guide from the github page.

You will need to tweak it for the raspbian OS. Clone the project then change in to the project directory.
mkdir build

cd build


You need the -E parameter in there so sudo will use the JAVA_HOME you defined. If you do not use it then it the build will not be able to find the JNI headers because it will not have a JAVA_HOME to check.  JNI is in JAVA_HOME under

This is the error you will receive when not using -E in he sudo command to use your JAVA_HOME.
CMake Error at /usr/share/cmake-3.7/Modules/FindPackageHandleStandardArgs.cmake:138 (message):
Call Stack (most recent call first):
  /usr/share/cmake-3.7/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.7/Modules/FindJNI.cmake:305 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  java/jni/CMakeLists.txt:1 (find_package)

-- Configuring incomplete, errors occurred!
See also "/home/pi/tinyb/build/CMakeFiles/CMakeOutput.log".
See also "/home/pi/tinyb/build/CMakeFiles/CMakeError.log". 

It should complete without errors.

The make command will take about 10 minutes to complete on the raspberry PI.
sudo make

Install the libraries on to the file system
sudo make install
It will install the jar file under
Generating JNI headers..
[  6%] Built target tinybjar
[ 32%] Built target tinyb
[ 54%] Built target javatinyb
[ 60%] Built target HelloTinyB
[ 66%] Built target AsyncTinyB
[ 72%] Built target Notification
[ 76%] Built target esstinyb
[ 80%] Built target asynctinyb
[ 84%] Built target checkinit
[ 88%] Built target hellotinyb
[ 92%] Built target notifications
[ 96%] Built target list_mfg
[100%] Built target uuid
Install the project...
-- Install configuration: ""
-- Installing: /usr/lib/arm-linux-gnueabihf/../lib/java/tinyb.jar
-- Installing: /usr/lib/arm-linux-gnueabihf/
-- Installing: /usr/lib/arm-linux-gnueabihf/
-- Installing: /usr/lib/arm-linux-gnueabihf/
-- Set runtime path of "/usr/lib/arm-linux-gnueabihf/" to ""
-- Installing: /usr/include/tinyb.hpp
-- Installing: /usr/include/tinyb
-- Installing: /usr/include/tinyb/BluetoothEvent.hpp
-- Installing: /usr/include/tinyb/BluetoothUUID.hpp
-- Installing: /usr/include/tinyb/BluetoothException.hpp
-- Installing: /usr/include/tinyb/BluetoothAdapter.hpp
-- Installing: /usr/include/tinyb/BluetoothGattService.hpp
-- Installing: /usr/include/tinyb/BluetoothManager.hpp
-- Installing: /usr/include/tinyb/BluetoothGattCharacteristic.hpp
-- Installing: /usr/include/tinyb/BluetoothGattDescriptor.hpp
-- Installing: /usr/include/tinyb/BluetoothObject.hpp
-- Installing: /usr/include/tinyb/BluetoothDevice.hpp
-- Installing: /usr/lib/pkgconfig/tinyb.pc
-- Installing: /usr/lib/arm-linux-gnueabihf/
-- Installing: /usr/lib/arm-linux-gnueabihf/
-- Installing: /usr/lib/arm-linux-gnueabihf/

Execute the example code to test your installation. It should prompt you include a device address.
sudo java -cp examples/java/HelloTinyB.jar:/usr/lib/lib/java/tinyb.jar HelloTinyB
Run with <device_address> argument

As an extra piece. If you want to install them locally instead of globally like it is done aboive and you get either of these errors. DO NOT DO THIS IF YOU DONE THE SETUP ABOVE.
java.lang.UnsatisfiedLinkError: no javatinyb in java.library.path
java.lang.UnsatisfiedLinkError: no tinyb in java.library.path

It is looking for these 2 files
locate what directory they are in then add them in the parameter
In my build directory i ran it like this. The 2 files are located in 2 different directories. This got it working for me.
java -Djava.library.path=/usr/local/lib/:./java/jni/ -cp examples/java/HelloTinyB.jar:./java/tinyb.jar HelloTinyB

Thursday, 22 September 2016

How to approach a memory leak

Steps to take to get a picture of the memory dump:
  • Get a historic look at the logs.
  • Open the heap dump with VisualVM
  • Look at the objects that using the most memory.
  • Query those objects for information using QQL.

If there are lots of objects then you need to distinguish if the object have the same type of information or they are referenced from the same type of object. It will give you a clearer picture of what type of object is causing the problem and where it is coming from.

QQL has the ability to count objects based on a criteria. For example, you query to count the number of hashmaps that have a certain size.

I have added a OQL project to GitHub. It has advanced OQL queries which have been tested with a sample program in the project. I have expanded the queries past what is in the visualvm OQL userguide.

Sunday, 11 September 2016

Making a simple maven plugin

I had a look around for a how to create simple maven 3 plugins. There is a mixture of maven 2 and maven 3 tutorials doing it different ways. Here is one that just does a simple printout. It can be expanded to anything you can dream of.
The project below creates a maven 2 setup but I have added extra steps to make it maven 3 setup

Running this command to create a plugin project
mvn archetype:create -DgroupId=com.naughton \ 
-DartifactId=simple-maven-plugin \ 
-DarchetypeGroupId=org.apache.maven.archetypes \ 

It will create this project structure for you

The pom file should look like this. It will include the maven plugin api as a dependency and packaging will be maven-plugin. That is the only different from a default pom.

 <project xmlns="" xmlns:xsi=""  
  <name>simple-maven-plugin Maven Mojo</name>  

Add in the following to the pom.xml to complete the project

This will let you add annotations in your Mojo files

Change the file to this. I have gotten rid of the Javadoc entries. Previously in maven 2 it got the goal name and phase from the Javadoc. Now you can use annotations in maven 3.
  * Goal which prints a sentence.  
 @Mojo(name = "print",defaultPhase = LifecyclePhase.PACKAGE)  
 public class MyMojo extends AbstractMojo  
   public void execute() throws MojoExecutionException  
     getLog().info("It executed the goal by printing this sentence.");  

You can see in the build that it will find no javadoc descriptors but one annotation descriptor. That is the annotation named print above.
 [INFO] --- maven-plugin-plugin:3.4:descriptor (default-descriptor) @ simple-maven-plugin ---  
 [WARNING] Using platform encoding (UTF-8 actually) to read mojo metadata, i.e. build is platform dependent!  
 [INFO] Mojo extractor with id: java-javadoc found 0 mojo descriptors.  
 [INFO] Mojo extractor with id: java-annotations found 1 mojo descriptors.  

Add this to generate the descriptor plugin.xml file automatically

This is what a plugin.xml file looks like. It includes the goals that the plugins supports. This is the file maven reads to understand your plugin.
  <name>simple-maven-plugin Maven Mojo</name>  
    <description>Goal which touches a timestamp file.</description>  

Structure of the plugin jar file

You can see that the plugin.xml is generated by the pluging plugin.

How to test your new plugin

Quickest way to test is your plugin it though a command line call to one of the goals defined in the new plugin. You must first install the plugin in to your local .m2 repo by running this command in the root plugin project.

mvn install

mvn com.naughton:simple-maven-plugin:1.0-SNAPSHOT:print

 [INFO] Scanning for projects...  
 [INFO] ------------------------------------------------------------------------  
 [INFO] Building simple-maven-plugin Maven Mojo 1.0-SNAPSHOT  
 [INFO] ------------------------------------------------------------------------  
 [INFO] --- simple-maven-plugin:1.0-SNAPSHOT:print (default-cli) @ simple-maven-plugin ---  
 [INFO] It executed the goal by printing this sentence.  
 [INFO] ------------------------------------------------------------------------  
 [INFO] ------------------------------------------------------------------------  
 [INFO] Total time: 0.238 s  
 [INFO] Finished at: 2016-09-11T19:38:39+01:00  
 [INFO] Final Memory: 7M/110M  
 [INFO] ------------------------------------------------------------------------  

How to test your plugin in another project.

Your plugin should now be in your local .m2 repository after mvn install.  To use the simple plugin use this in plugin entry in the other project pom.xml


I have added the simple maven plugin to github to hack around with: 


Thursday, 8 September 2016

A look at haskell

Recently I got some time on my hands so I started looking at programming in Haskell. I have heard of haskell a few times from academic people but never truly understood the syntax. Last week I was at a programming language debate. Haskell did have some good points so I thought I would at least understand the basics of the syntax. Just in case it was useful in the future and improve my pure functional programming skills

How I approached learning it
First I always try the hello world example. I learn two things this way. How to compile and execute  and how to print out to standard out. Printing to standard out helps with debugging. When learning a new language putting a few debugging message helps you see the flow of the program. It also shows where the program was before the failure and the values of the variables.

Next I try a few simple maths equations. Then I jump in to calling functions or methods.
  • First with no return type or arguments
  • Then with a return type. Usually returning an integer for simplicity.
  • Then with a return type and arguments. The are not linked. Just to avoid any computation problem. More concerned with getting the in and out correct first.
  • The with a return and arguments. The return is based on the arguments supplied.
Once I mastered the above then I have a basic know the of the syntax and how everything runs. I am no master at this point but a good foundation.

Thoughts on haskell while learning from scratch:
  • Has 2 ways of defining and calling functions. Makes it confusing when looking at different tutorials
  • For a beginner haskell has a lot of builtin functions. There is no way to tell the difference between defined function and a builtin which knowing the builtin functions. In java you can check were the import came to see if it came from the core library.
  • The compiler errors are hard to decipher for a beginners. It is better to start with a correct program with customizing and constant compiling. 
  • I  am not a big fan white spaces to separate arguments. I prefer to have parentheses for better readability. Also I prefer curly braces to group information instead of indentation. You can do these in haskell but white spaces and indentation seem to be the normal for less verbose code.

Here are 2 resources that I found useful.

 I uploaded my small haskell programs. It might help a beginner.


Saturday, 16 July 2016

Findbug : Unusual equals method (EQ_UNUSUAL)

I came across this find bug issue in my code recently. It appeared on the all the equals methods that had overidden the objects equals. The equals method that was there look fine to me.

Definition of the issue.
This class doesn't do any of the patterns we recognize for checking that the type of the argument is compatible with the type of the this object. There might not be anything wrong with this code, but it is worth reviewing.

What patterns does find bug look for? 

Here is the source code for the equals pattern detection.

In the method visit() there is a nested if else statement. This method is checking what equals pattern was found by method sawOpcode(). At the end of the nested if statement is this code:

 else {  
         if (AnalysisContext.currentAnalysisContext().isApplicationClass(getThisClass())) {  
           .reportBug(new BugInstance(this, "EQ_UNUSUAL", Priorities.NORMAL_PRIORITY).addClassAndMethod(this));  

This code will flag the "Unusual equals method" issue since it has checked your code against every pattern it knows.

Does your code have an issue?

Probably not since it has checked all other possible issues you could of had with your code. Like the 2 issues below. It will make you think about your equals method thought. Wondering for ages why FindBug does not like your equals method.

Some patterns it is looking for

Patterns it does not like
EQ_ALWAYS_FALSE and EQ_ALWAYS_TRUE pattern is when it is returning a boolean and the previous opcode was ICONST_0 or ICONST_1. That means the equals method always returns the same boolean regardless of input so this is flagged as EQ_ALWAYS_FALSE or EQ_ALWAYS_TRUE in findbug.

EQ_GETCLASS_AND_CLASS_CONSTANT is when you are comparing the class to another class but you are using the class name instead of "this" variable. Subclasses will be broken if they are inherit this equals.

Patterns findbug likes in an equals method
A method call to compare
An Instanceof check
A method call to getClass

Saturday, 9 July 2016

Hand carving your own wireless configurations

After Debian 6 "squeeze" network manager does not handle interfaces that  are in /etc/network/interfaces by default. You update the network manager configuration to inform it to handle those interfaces. Network Manager is included in many Linux distros. Its reason for living is to make Linux networking just work.

Network Manager will infinitely display a loading logo sign in Gnome if it does not handle the wireless interface.

There is no fun network manager handling all the juicy configuration.

What is involved when making your own wireless configurations:
  • Scanning all the wireless networks on the command line using iw
  • Looking at the details of wireless access point I want to connect to. I need to know if the authentication process it uses such as if its PSK (Pre shared key) or EAP (Extensible Authentician Protocol). I also need to know if there is a cipher required on top of the protocol.
  •  Once you have the information you can then add it to the wpa_supplicant.
 wpa_supplicant is just for the WPA protocol. For the WEP (Wired Equivalant Protocol) you use another application.  Reason is that nobody wants to touch the WEP application again. No one is using it because of the way you can sniff the packets and figure out the pre shared key being used which makes WEP unsecured.

Network Manager code

You can browse the code to see how it decides what information it needs for every network configuration.

What are the cipher used for?

TKIP and CCMP are used to encrypt your data from the computer to access point. They make sure no see what you are browsing. They use the pre shared key to encrypt the data to the router.

Here is an example of wireless printout from iwlist. It is a WPA2 router.
      Cell 06 - Address: 5C:DC:96:56:4F:F3  
           Frequency:2.462 GHz (Channel 11)  
           Quality=54/70 Signal level=-56 dBm   
           Encryption key:on  
           Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s  
                24 Mb/s; 36 Mb/s; 54 Mb/s  
           Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s  
           Extra: Last beacon: 28ms ago  
           IE: Unknown: 000B4254487562352D4A354A37  
           IE: Unknown: 010882848B962430486C  
           IE: Unknown: 03010B  
           IE: Unknown: 0706474220010D14  
           IE: Unknown: 2A0106  
           IE: Unknown: 2F0106  
           IE: IEEE 802.11i/WPA2 Version 1  
             Group Cipher : CCMP  
             Pairwise Ciphers (1) : CCMP  
             Authentication Suites (1) : PSK  

 Information you need from this print out
  • Group Cipher
  • Pairwise Cipher
  • Authentication Suites
You can then add this to /etc/wpa_supplicant/wpa_supplicant.conf
     psk=<your psk passphrase produced by wpa_passphrase>  

That is a general over view to make your own wireless configurations.

I GIT you now

My relationship with GIT is a love/hate relationship. Comparing it with SVN I have all love for GIT.

I remember the first time setting up SVN that I wanted to use with my project. I did not want the project online and I did not have a running server so I had to host the repository on my computer.  On my windows machine I thought this would be no problem but from memory you could not have the client and repository on the same machine with out a hack. SVN did not like the way I was going to set it up. I just remember giving up on SVN then.

Then came GIT with its local check in which I thought was a god send. Now development was easier to keep track of.

GIT is at version 2.9.0 now and it has a lot of features. If you look through the man pages for GIT there is a lot of options. This makes it very powerful but also colossal. Like my oven that has 20 different features, I only use 2 of them from day to day. I do not use all the  fancy features of GIT.

I like the positive flow of GIT. When all you checkins are correct. I just hate when I have a negative flow. A negative flow is when you have messed up a commit message, accidentally checked in, merged from the work branch etc. They are simple negative flows. There are harder negative flows to come back from which you need google and some one that deeply knows git.

I was at a conference recently and there was a GIT presentation. The presenter focused how to fix negative flows to return to a positive flow. Most presentations about GIT are what it is and what it does. This was a refreshing presentation. What was thought provoking was even though GIT was improving all the time the foundation GIT is built on has not changed. Blob objects, tree objects and commits are the foundation. Understand those deeply and you can figure out how to come back to a positive flow. You just need to find the command that allows to do it. Instead of not know how to come back and finding a post to explain it to you along with the command to do it.

In the future I will post my GIT negative flow conquests.