Finished my studies!

August 31, 2009

Finally, after 7 years… I finished my MSc degree (assuming the exam committee accepts my courses/grades). My thesis (and presentation) can be found here. I guess now I have to grow up ;-)


Easy MIDlet Suite construction

June 28, 2009

This post is a follow up on my previous post.

I wrote a (Bash) script that can be used to create a complete project directory including an Ant build file for MIDlet Suite construction and download the required libraries automagically.

It uses only free software like Java (OpenJDK), ProGuard, Ant, the MicroEmulator mobile library files and a custom Ant task (sources) to help with deploying the MIDlet Suite (including signing it). The custom Ant task includes a “jad” Ant task and a “sign” task. The “sign” task is based on the wtksign source code from the Antenna project but is modified to work together with my “jad” task.

The MIDlet Suite Creator can be downloaded here.

It is easy to use:

$ sh create_midlet_suite.sh
Enter project name [MyProject]:
Enter project description [My MIDlet Suite]:
Enter project version [1.0.0]:
Enter target project location [/../../../MyProject]:
Enter MIDlet Suite vendor [MIDlet Suite Vendor]:
Enter MIDlet [1] Name [MyMIDlet]:
Enter MIDlet [1] Package []:
Enter MIDlet Suite Configuration [CLDC-1.1]:
Enter MIDlet Suite Profile [MIDP-2.0]:
Would you like to download the required libraries now [y|n]? y

Downloading required libraries…
[..]
Done.
$

See the included README file for more information.


Creating deployable MIDlets with free software

February 16, 2009

So what we want to do is create a MIDlet for running on a Mobile phone that supports Java ME (Mobile Edition) without using any proprietary software. Assumed is you have a working Java 6 environment, for example OpenJDK. Ingredients:

Traditionally one uses Sun’s Wireless Toolkit (WTK), but this has limited availability on 64 bit platforms, or even OS X. As an extra disadvantage it is non-free software (Update: the preverifier is available as GPL source as part of the PhoneME project. I tried to compile this on a 64 bit machine but it didn’t seem to work, I gave up for now.)
The first step is to configure Eclipse with the Eclipse MTJ plugin and make it use MicroEmulator as it’s SDK and find the relevant devices. This way the Java code can be verified while coding. Eclipse will take care of generating the class files that need to be packaged and preverified. Also the JAD file will be more or less available in Application Descriptor, minus the MIDlet-Jar-Size line. Integration with the preverification of ProGuard seems to not be included (yet) so for now it can be done using some command line tools (yeah, great…)

First we create the JAR file with the classes generated by Eclipse in them (run this from withing the project’s directory in the workspace):

jar -cfm MIDletName_not_preverified.jar Application\ Descriptor -C bin/ .

Now this needs to be preverified with ProGuard. A configuration file for ProGuard would look like this:


-injars MIDletName_not_preverified.jar
-outjars MIDletName.jar
-libraryjars [MicroEmulator Directory]/lib/midpapi20.jar
-libraryjars [MicroEmulator Directory]/lib/cldcapi11.jar
-overloadaggressively
-repackageclasses ''
-allowaccessmodification
-microedition
-printseeds

-keep public class * extends javax.microedition.midlet.MIDlet

You can of course add more libraryjars, for example for Bluetooth. Proguard is started like this:


java -jar [ProGuard Directory]/lib/proguard.jar @proguard.config

where proguard.config is the file described above. Now you can take Application Descriptor and add the JAR file size to it:


cp "Application Descriptor" MIDletName.jad
echo MIDlet-Jar-Size: `ls -l MIDletName.jar | awk {'print $5'}` >> MIDletName.jad

And now you can run it in the emulator:


java -jar [MicroEmulator Directory]/microemulator.jar MIDletName.jad

See this if you want to use this to test Bluetooth MIDlets. This was of great use for creating MIDlets with Bluetooth functionality!

Other interesting links: Antenna, a J2ME build system for Ant. It seems to have a “hard” dependency on SDK’s like the one from Sun looking for a bin/preverify so this was not of much use. Then there is the Maven J2ME Plugin which is much more interesting for serious projects, and it does support ProGuard as preverifier.

Hopefully someday soon support for ProGuard as preverifier can be added to Eclipse MTJ :) Maybe I’ll work on getting ProGuard, MicroEmu and MTJ included in Fedora if BlueCove ever gets reviewed by someone ;)

Update: In case you want to use Bluetooth you can add -libraryjars [MicroEmulator Directory]/lib/microemu-jsr-82.jar. In case you want to use the BouncyCastle crypto library (look for a file named lcrypto-j2me-[version].zip) you can add -injars [BouncyCastle J2ME directory]/zips/cldc_classes.zip. When using injars the code in there will also be obfuscated, and only the classes you need will be included in the output JAR file.


Exams finally over!

January 27, 2009

Friday I had my last exam ever (Cryptography), this was my third try to pass this exam (pfff…) and this time I decided to study really hard for it as it’s actually a really interesting subject and I wanted it to be over finally… Turned out to go very well, grade: 10/10 :)

Only my thesis is left now, hopefully I’ll finish that one day in the not so far future ;)


Using NFC phone as ISO14443 card reader

January 26, 2009

In order to finally get something working for my thesis I decided to move from NFCIP communication to Bluetooth for now. So far that seems to work much better, except the connection time.

It’s rather easy to write a Java app running on a normal PC (the host) using the BlueCove stack to communicate with the phone and write a MIDlet that acts as a middle man between a contactless ISO14443 card and the host. I did this to test the Bluetooth communication and it seems to work :)

For now I created a BTChannel class that implements the javax.smartcardio.CardChannel API so it’s easy to modify existing applications that use the Java Smart Card API to use the Bluetooth link. On the phone I created a small MIDlet that acts as a Bluetooth server and waits for a connection from a client. As soon as you keep a card close to the phone it sends the APDU received over Bluetooth from the host to the smart card and relays the results back to the host.

bluetooth-rfid

Maybe I can find some time to finish this and turn it into a TerminalFactory provider where all bluetooth devices in the vicinity with the “relay” MIDlet running are seen as card readers.


Creative Commons licensed books

January 17, 2009

Recently some interesting developments took place in eBook world. I found some interesting CC-licensed books. These are my favorites so far:

Very nice :)


Bluetooth, Java and Linux

January 8, 2009

For my thesis I’m having serious problems with the Nokia NFCIP functionality so I was looking to get the communication working over Bluetooth. The basic requirements are simple, make it possible to send and receive byte arrays of (in principle) unlimited size.

I’m programming the phone and the host application (running on a normal PC with Linux) in Java (see JSR-82), so I was looking for a Bluetooth Java stack that works with Linux’ Bluetooth stack (BlueZ).

I found the Bluecove project which supports a number of Bluetooth stacks as backend. The project is free software and cross platform so perfect for this project.

It’s however a bit tricky to get this to work as it needs to build some “native” code for the system you want to use it on. After some playing around I managed to get it to compile from source as I didn’t want to use the pre compiled binaries.

It got a bit out of hand and decided to create an RPM package for it as it will be more useful for other people as well. Preliminary discussion about it here and an early version of the SPEC file and the source RPM package.

The Fedora package review request can be found here.


Fedora package contribution with GPshell and GlobalPlatform

December 15, 2008

Recently I submitted the packages globalplatform and gpshell to Fedora for inclusion. The package make it possible to manage OpenPlatform and GlobalPlatform smart cards like JavaCard. The packages were reviewed and accepted and I got sponsored to be able to include them myself. The packages are available for Fedora 9 (globalplatform, gpshell), 10 (globalplatform, gpshell) and also rawhide.


Convert short to byte array (and byte array to short)

November 25, 2008

It was very hard to find something that worked, so finally I found something that worked and passed my tests by combining some sources online and modifying it a bit. This conversion is aims to be compatible with JavaCard’s Util.setShort, Util.getShort and Util.makeShort.

public static short readShort(byte[] data, int offset) {
	return (short) (((data[offset] << 8)) | ((data[offset + 1] & 0xff)));
}

public static byte[] shortToByteArray(short s) {
	return new byte[] { (byte) ((s & 0xFF00) >> 8), (byte) (s & 0x00FF) };
}

The tests I performed (convert short to byte array and then back and compare):

0 -> 0x00 0x00  -> 0 ---> OK
1 -> 0x00 0x01  -> 1 ---> OK
-1 -> 0xFF 0xFF  -> -1 ---> OK
100 -> 0x00 0x64  -> 100 ---> OK
255 -> 0x00 0xFF  -> 255 ---> OK
256 -> 0x01 0x00  -> 256 ---> OK
-256 -> 0xFF 0x00  -> -256 ---> OK
32767 -> 0x7F 0xFF  -> 32767 ---> OK
-32768 -> 0x80 0x00  -> -32768 ---> OK
-255 -> 0xFF 0x01  -> -255 ---> OK

Hope it helps at least someone else :)


Intel TV-out and Fedora 10

October 30, 2008

It works, sort of. At best it’s unstable. With the use of xrandr you can get it to work, but not with System->Preferences->Hardware->Screen Resolution which is a weird name by the way and doesn’t cover the whole function of the tool.

This is the output of xrandr on my laptop (with Intel 965 video):

Screen 0: minimum 320 x 200, current 1280 x 800, maximum 1280 x 1280
VGA disconnected (normal left inverted right x axis y axis)
LVDS connected 1280x800+0+0 (normal left inverted right x axis y axis) 331mm x 207mm
1280x800       60.0*+
1024x768       60.0
800x600        60.3
640x480        59.9
TV connected (normal left inverted right x axis y axis)
1024x768       25.0
800x600        25.0
848x480        25.0
640x480        25.0

So, now to activate TV-out and set the mode to PAL you use this command:

xrandr --output TV --mode 800x600 --set TV_FORMAT PAL

My laptop also has a key combination (FN+F4) to toggle TV-out so pressing this combination in combination with some xrandr makes it sometimes work, switching to another VT (CTRL+ALT+F2 for example) helps too sometimes, pressing FN+F4 there and returning to the X VT (CTRL+ALT+F1) and sometimes use FN+F4 again to get it work… Sometimes it works just right from the beginning! You plug in the cable, run the xrandr command and it works!

You can turn of TV out using:

xrandr --output TV --off

For the record, I’m using the Intel driver from xorg-x11-drv-i810-2.5.0-1.fc10.x86_64.

Of course it could be a lot better, like auto detection of TV out connected devices, make it actually work from the Screen Resolution configuration tool. In Windows (Vista) it sucks as well, because it will resize your laptop’s desktop to 800×600 as well which fucks up the location of any icons you had there when taking the cable out.

Well anyway, glad I don’t have to boot Windows to watch something on the TV screen :)