Jan 22 2013

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!

Aug 30 2012

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!

Apr 01 2012

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

-I/home/projects/nethix

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.

Feb 12 2012

Simple C to XML serializer 0.0.2 released in GitHub

Some while ago I wrote a simple C to XML serializer that reads C structs and unions from a header file a writes them to a XML file. You can read that post here.

I’ve improved it a little bit and released it in GitHub. You can check it out here.

This program is in its first stages and currently it only converts the most widely used C-structs constructs to XML. However, it could potentially serialize other C constructs and almost any C code since it uses an almost full-compatible C grammar.

The version I released in GitHub adds the support for structs/unions defined inside a macro.

I’ll try to explain how this feature works:
Suppose you have a file, test3.h, with a macro that defines the following struct:

#define def_struct_data(prefix,number,code_number)\
struct prefix##driver_data\
{\
    task_status_t tsk_sts;\
\
    struct buttons_platform_data pdata[number];\
    struct prefix##an_event event;\
\
    struct prefix##buttons_settings\
    {\
        uint8_t thresholds[3];\
        uint8_t pressed_threshold;\
        struct prefix##buttons_mask hash_table[code_number];\
    }__attribute__((packed)) settings;\
\
    uint8_t raw[((number) / 8) + 1];\
    uint8_t raw_mem[((number) / 8) + 1];\
\
}__attribute__((packed));

As you probably know, the syntax of a C macro is not valid for a C compiler, that’s one of the reasons for using the C pre-compiler: it converts the macro to something that the compiler understands.
In the SC2XML case, applies the same condition. Therefore, you have to create a very simple C file that includes the file that you want to serialize, in this case test3.h, along with the use of the macro and execute sc2xml with that file:

Assume that the name of your simple C file is test3.stub.h. It should looks like this:

#include "test3.h"
def_struct_data(prefix,number,code_number)

def_struct_data is the name of your macro.

Execute sc2xml:

$ sc2xml test3.stub.h
 
sc2xml 0.0.2
 
INFO: *** Parsing file data/test3.gen.h ***

The final generated XML file, test3.h.xml, will look like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<sc2xml>
    <struct>
        <struct_name>prefixdriver_data</struct_name>
        <field type="task_status_t">
            <name>tsk_sts</name>
        </field>
        <field type="struct buttons_platform_data" size="number">
            <name>pdata</name>
        </field>
        <field type="struct prefixan_event">
            <name>event</name>
        </field>
        <struct>
            <struct_name>prefixbuttons_settings</struct_name>
            <field type="uint8_t" size="3">
                <name>thresholds</name>
            </field>
            <field type="uint8_t">
                <name>pressed_threshold</name>
            </field>
            <field type="struct prefixbuttons_mask" size="code_number">
                <name>hash_table</name>
            </field>
            <struct_attributes>__attribute__ ( ( packed ) ) </struct_attributes>
            <struct_nested_name>settings </struct_nested_name>
        </struct>
        <field type="uint8_t" size="( ( number ) / 8 ) + 1">
            <name>raw</name>
        </field>
        <field type="uint8_t" size="( ( number ) / 8 ) + 1">
            <name>raw_mem</name>
        </field>
        <struct_attributes>__attribute__ ( ( packed ) )</struct_attributes>
    </struct>
</sc2xml>

Now you can see that the XML file shows exactly the struct defined in the macro def_struct_data but with nice XML tags!

I hope this project could be useful, specially if you want a non-trivial example that defines a parser and grammar with lex and yacc.

Apr 15 2011

Updating the Samsung Galaxy i7500 using Linux

The Galaxy i7500 was the first android phone built by Samsung. I got this phone some while ago and I wanted to update to a more recent android version (the original firmware ships version 1.6), but unfortunately, Samsung doesn’t give any support nor updates. Thus, if you want to update the firmware, you can use a non-official firmware.

In this post I try to explain how to do this when your host/PC is a Linux machine.
The procedure described here is partially based in the quite uselful Easy guide to update to Galaxo or GAOSP! written by DaSchmarotzer that explains how to update this phone but using a Windows based machine.

Download

Get all the needed files:

Put everything in place

First of all, create a backup of your data. The SD card (pictures, music, etc) won’t be touched, but other important info like your contacts will be erased. If you have a Gmail account you just can synchronize your contacts for uploading them to your account and once you finish the update you can synchronize it again for downloading them to the phone.

Unpack the android SDK wherever you want. I put all the downloaded files in /home/android/.

Decompress the android SDK. The directory /home/android/android-sdk-linux_x86 will be created.

Change to the directory /home/android/android-sdk-linux_x86/tools

Move the file fastboot.zip inside the /home/android/android-sdk-linux_x86/tools directory and decompress it. A new file called fastboot will be created.
Give it execution permissions:

chmod +x fastboot

Connect your phone via USB and mount its SD card.
Copy the chosen ROM and Gapps files to the phone’s SD card.
Umount the SD card.
Turn off the phone, but leave it connected to the PC using the USB cable.

Now we have all the downloaded files in the required locations.

Manipulate the i7500 flash with fastboot

We have to tell the phone’s bootloader how to load our files. fastboot can write a new bootloader image (recovery-6.5.1.img) that allow us to do exactly that plus several other operations.

Before executing fastboot, we need to tell udev through its rules that we’re the owner of the usb device with vendors ID 18d1 (Google Inc.) and 04e8 (Samsung Electronics Co., Ltd).
You have to do these steps as root.
Change username to the username you use for logging in your system.

Create the file /etc/udev/rules.d/50.android.rules with the following content

SUBSYSTEM=="usb", SYSFS{idVendor}=="18d1", MODE="0666", OWNER="username"

Create the file /etc/udev/rules.d/90.android.rules with the following content

SUBSYSTEM=="usb", SYSFS{idVendor}=="04e8", MODE="0666", OWNER="username"

Reload udev’s rules

$ reload udev

As a normal user, add the /tools directory where fastboot is located to your PATH environment variable.

$ export PATH=$PATH:/home/android/android-sdk-linux_x86/tools

Change to the /home/android/android-sdk-linux_x86/tools directory and write the following command without pressing enter.

$ fastboot flash recovery recovery-6.5.1.img

You’ll press it after booting the phone in fastboot mode. Do so by pressing and holding both the Call button (bottom left) and the Power button (bottom right) at the same time until you see on the phone that you are in fastboot mode. You’ll have about 15 seconds to press enter on the shell. If it works, you’ll see in the shell this output:

sending 'recovery' (3080 KB)... OKAY
writing 'recovery'... OKAY

In the phone’s display you’ll see something like this:

Android fastboot

Android fastboot

At this point we have a new bootloader image in the phone’s flash that allow us to do custom operations.

Turn off your phone. I had problems for turning it off, so I removed the battery and put it again.

Updating to the new firmware

Boot the phone, but this time in recovery mode. Do so by holding the Lower volume button, the Call button and the Power button. If it works, you’ll see a green menu with lots of choices:

Android recovery mode

Android recovery mode

Select the Nandroid backup option and press the Home button to confirm. Then just follow the instructions on-screen. I chose slot 1 when asked which slot to use.

Select Wipe data/Factory reset. It’s going to erase all your applications and settings, but it shouldn’t touch anything saved on your SD card. It’s important to do so, else the phone will not work properly.

Select Apply any zip. Select either the GAOSP or the Galaxo zip files, depending on your choice. This will take a bit since it was to copy 60 or 49MB, respectively.

Select Fix package permissions.

Select Apply any zip again but this time pick the Gapps, which will restore the Google Applications on your phone.

Now you can reboot by selecting the first option Reboot system.

Notice: It could happen that the phone freezes during the booting procedure in the android or gaosp boot screen. Wait for ~10 minutes, and then if it’s still stuck, pull out the battery and boot again.
If it still doesn’t work, repeat the procedure described in this section Updating to the new firmware.

Btw, sorry for the images quality. I’ll change them with better ones.

That’s it!

References

WordPress Themes