Cross-compiling LibGTop2

When you have a Linux embedded system and you’re programming in C (that is quite common), sometimes you need to calculate inside a program the resources usage for a given process ID such as CPU or memory; or in general you could need to know the network bandwith or the list of mounted devices.
The most commmon way for gathering this data is to parse out the files inside /proc/, but this can be tedious. There is a discussion at stackoverflow about it.

An alternative, if you already have or can install glib2, is to use LibGTop that “is a library to get system specific data such as CPU and Memory Usage and information about running Processes”.

Compiling and installing it in a PC is not a problem as the nice Beyond Linux From Scratch guide shows, but for cross-compiling it is not straight forward.

Here’s the simple procedure:

Package
glibtop-2.28.4

Requirements
This library is built on top of other libraries such as glib2 and intltool. I wrote a post some time ago that could be helpul for compiling glib2. If you’re using buildroot, it already builds glib2 for you if selected.

Environment configuration
As normal, set the environment variables that make our life easier. Change them according to your needs. These settings are for an ARM based processor:

$ export HOST=arm-linux
$ export BUILD=i386-linux
$ export PREFIX=/home/projects/rootfs/usr/local

Configuration
Since I have an embedded system without graphical interface and it’s a single-core processor, I don’t need X nor the SMP parameter. Adjust it according to your system:

GLIB_CFLAGS="-I$PREFIX/usr/include/glib-2.0 -I$PREFIX/usr/lib/glib-2.0/include -I$PREFIX/usr/include" 
GLIB_LIBS="-L$PREFIX/lib" 
./configure 
--build=$BUILD 
--host=$HOST 
--prefix=$PREFIX/usr 
--without-x 
--without-libgtop-smp 
--disable-gtk-doc-html 
--disable-nls

The examples/ directory doesn’t build properly unless you modify the Makefile. If you don’t want to compile this directory you can skip it by adding to the above configure command the parameter –with-examples.

Let’s assume that you want the examples/ directory:
Edit the file examples/Makefile:
Set the LIBS variables to the following value:

LIBS = -L/home/projects/rootfs/usr -L/home/projects/rootfs/usr/lib -lglib-2.0 -lintl

Compilation and installation

make
make install

Using LibGTop2
For compiling and linking a program that uses LibGTop2 you need to include the header files and the libraries. Here’s a fragment of the Makefile I use:

CC = arm-linux-gcc
CFLAGS = -Wall -Os 

INCLUDES = -I. -I$(PREFIX)/usr/include -I$(PREFIX)/usr/include/glib-2.0 -I$(PREFIX)/usr/lib/glib-2.0/include -I$(PREFIX)/usr/include/libgtop-2.0
LDFLAGS = -L$(PREFIX)/usr/lib -L$(PREFIX)/usr/local/lib -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl -lgtop-2.0

SRC = my_top.c
OBJS = $(patsubst %.c, %.o, $(SRC))

# Link
my_top: my_top.o
    @echo LD my_top
    @$(CC) $(LDFLAGS) $(OBJS) -o $@

# Pull in dependency info for *existing* .o files
-include $(OBJS:.o=.d)

# Compile
all: $(OBJS)

%.o: %.c 
    @echo CC $*.c
    @$(CC) $(INCLUDES) -c $(CFLAGS) $*.c -o $*.o
    @$(CC) -MM $(CFLAGS) $*.c > $*.d

That’s it. Have fun!

Installing an Epson Perfection V100 Photo scanner in Linux

This post is a bit off-topic since it’s more a reminder for myself and is not related to embedded Linux.
I have an old Epson Perfection V100 Photo and today I needed to use it for the first time in several years. I knew that it was supported in Linux because I have used it in the past, but I didn’t remember what to install for making xsane to recognize it.

I’m running Fedora 16 with kernel 3.4.9, but the links provided have the .deb files needed by Ubuntu or any Debian-based distro.

Here’s the simple procedure I used:
Download the following files:

  • iscan-data-1.16.0-3.noarch.rpm from here
  • iscan-2.29.1-5.usb0.1.ltdl7.i386.rpm from here
  • iscan-plugin-gt-s600-2.1.2-1.i386.rpm from here

Install them as root in the following order:

yum install iscan-data-1.16.0-3.noarch.rpm
yum install iscan-2.29.1-5.usb0.1.ltdl7.i386.rpm
yum install iscan-plugin-gt-s600-2.1.2-1.i386.rpm

Then, from a software manager utility, PackageKit in the case of Fedora or Synaptic in Ubuntu, install xsane that is a widely, although quite old, piece of software use for scanning under Linux.

epson v100

That’s it!

Cross-compiling gnokki

[Updated: 15/03/2013]

Recently, I needed a software that sends and recevies Short Message Services (SMS) from a Linux ARM-based embedded device. Obviously, I needed something with a small foot-print and not resource-hungry. I found three FOSS options that could fit my needs:

gsm-utils
Package: gsmlib_1.10.orig.tar.gz
This project is quite old and unmaintained. If you compile it with a recent gcc (I was using 4.3.x), you’ll need to apply this patch provided by Debian. The building procedure is straight-forward, but the results, at least for me, were quite disapointing because the SMS daemon crashed very often.

gammu
Package: gammu-1.31.0.tar.bz2
Gammu is a fork of gnokii (the third option), it’s well documented, it’s robust and more complex, and it has a lot of features making it a bit heavier than gnokii. If your hardware is a PC or a high-end ARM processor I would recommend gammu. It also depends on libdbi that implements a database-independent abstraction layer in C, similar to the DBI/DBD layer in Perl.
Since I can only use SQLite3, I don’t need the portability to other databases making the use of libdbi redundant.

gnokii
Package: gnokii-0.6.31.tar.bz2
Gnokii is a set of tools, similar to gammu that allows to send/receive SMS, read/write to a phonebook and other services. I was only interested in the SMS functionality that seemed to cover quite well.
gnokii’s SMS daemon, smsd, is quite light, making it suitable for small ARM processors like the one I have.
Latest versions support SQLite3 so I could even put the two tables that smsd uses for sending and receiving SMS inside my database.

The documentation is not great, but good enough to get started. The code is quite clear and easy to understand. In fact, I found a memory leak in smsd that I fixed and sent to the mailing list. The patch was applied quite fast showing the active development around this project.

I decided to use gnokii and document its building procedure since the cross-compilation is not straight forward.

This is the procedure I followed:

Set the following environment variables that will make our life easier:

$ export BUILD=i386-linux
$ export HOST=arm-linux
$ export PROJECT_PATH=/home/projects/rootfs

Configure:

GLIB_CFLAGS="-I$PROJECT_PATH/usr/include/glib-2.0 -I$PROJECT_PATH/usr/lib/glib-2.0/include" 
GLIB_LIBS="-L$PROJECT_PATH/usr/lib" 
SQLITE3_CFLAGS="-I$PROJECT_PATH/usr/include" 
SQLITE3_LIBS="-L$PROJECT_PATH/usr/lib" 
./configure 
--build=$BUILD 
--host=$HOST 
--without-x 
--without-bluetooth 
--without-libical 
--disable-libusb 
--disable-phonet 
--enable-sqlite 
--disable-xdebug

Before building, edit the following Makefiles:

  1. common/phones/Makefile
  2. common/links/Makefile
  3. common/devices/Makefile
  4. common/data/Makefile
  5. gnokii/Makefile
  6. common/Makefile
  7. utils/Makefile
  8. gnokiid/Makefile

Unset the variable LIBS:

LIBS =

Edit the files gnokii/Makefile and gnokiid/Makefile:
Set the variable LIBS to the following value:

LIBS =  -L/home/projects/rootfs/usr/lib -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0

Edit the file smsd/Makefile:
Set the variable GLIB_LIBS to the following value:

GLIB_LIBS = -L/home/projects/rootfs/usr/lib -lsqlite3

Set the variable CFLAGS to the following value:

CFLAGS = -g -O2 -Wall -Wno-pointer-sign -fvisibility=hidden -fno-strict-aliasing -I/home/projects/rootfs/usr/include

Now you can build and install in /home/projects/rootfs/usr/local:

make
make DESTDIR=$PROJECT_PATH/usr/local install

Gnokii still has some rough edges that can be fixed/improved, but after a couple of months of intensive use its behaviour has been quite stable.