Tuesday, July 10, 2012


While playing with Android libsensors virtual HW access I have decided not to torture my PandaBoard with soldering additional serial port (note it has only one with console connected to it by default) and use some sort of a virtual serial port instead connected to real HW over the network:

After googling a while I have found a nice tool 'socat' that allows to... well, it can do almost everything, checkout on the project website: http://www.dest-unreach.org/socat/

It only turned out that Android build script has a minor bug in it I had to fix (see below 'android_termios_shift_fix.patch') and also there is a problem with PTYs support. By default openpty() and other related functions are absent in Bionic library, while Linux kernel configuration used in Android implements UNIX98 PTYs. In order to get my small system working I have ported openpty() function from uClibc which seemed to be quite enough for socat to get it working. The 'enable_android_pty.patch' adds an 'openpty.c' file to the build and modifies Android build script to perform following changes when it is invoked:
 - enable HAVE_OPENPTY and HAVE_GRANTPT features in config.h
 - add openpty.c to the Makefile
Actually this is a quick-n-dirty solution: it produces a warning for openpty() since no pty.h header exist, the port itself is a license violation, etc., but I don't really care at the moment - the whole 'socat_buildscript_for_android.sh' distributed with socat is a dirty hack.

So I've got everything working and I can also capture and analyze packets going trough serial port with Wireshark by just writing a simple dissector! sweeeeet...

Patches are available here.

Socat is used on host (sandbox) with following command:
socat tcp-l:54321,reuseaddr,fork /dev/ttyS0,raw,b115200,echo=0
and on panda with:
socat pty,link=/dev/ttyS0,raw,echo=0 tcp:sandbox:54321