Linux on board

Developing for the Nokia N800

Set up and test your build environment

Content series:

This content is part # of # in the series: Linux on board

Stay tuned for additional content in this series.

This content is part of the series:Linux on board

Stay tuned for additional content in this series.

Linux is enjoying a well-deserved surge in popularity for mobile and embedded applications, owing to its ability to scale down to the small footprint needed to run efficiently in compact devices. The Nokia N800 takes advantage of this efficiency and offers a great deal of general-purpose functionality that developers can take advantage of. But rather than discuss features, my goal in this and the next two installments of Linux on board is to show how to get started building applications by way of a working example, using the camera feature to create a Webcam.

The Nokia N800 is a fairly mature product. Its predecessor, the Nokia 770, has already shown a great deal of evolution from early Linux-based consumer devices, and the N800 has clearly been developed based on responses to customer concerns. The hard-shell case is gone, and now you can put the stylus in or take it out while using the device in its most normal configuration. (The recently-released Nokia N810 will be the subject of a future article.)

Instead of a single "RS-MMC" slot, the N800 provides two slots for regular SD cards. The stand is built in. Most noticeably, available memory, flash, and processor speed have all increased. The company has even included a screen protector, already correctly installed. (My track record with screen protectors is astounding—I think I once managed to get an entire cat caught under the screen protector on a cell phone—so I really appreciate this.) Read about some of the more important technical specs and physical parameters of the N800 below.

A number of features have been expanded, although not everything is complete; for instance, the WiFi code doesn't support trusted root certificate authorities yet, although there's an empty drop-down menu for it. This might get updated in a future release.

One of the things that I find most impressive about the 770 is its build environment, based on scratchbox. Let's look at how this build environment really works.

The N800 has a built-in camera. When you pop it open, the N800 starts up a video-friendly chat program. That's neat, but it's not what I want. I want a plain old Webcam. The N800 seems a perfect platform for a Webcam: it's small, portable, networked, and (don't forget) it has a camera. So let's get down to brass tacks and start messing with code.

Setting up

I set my system up with a terminal program and got root access. You don't really have to do this; it's just that I like to poke around. I downloaded a terminal program from the maemo Wiki's application catalog page (see Related topics for a link). The program is actually designed for the 770, but it's not dependent on the fine details of kernel revisions and whatnot, so it works fine.

Gaining root is only slightly harder: you need to put the device into "R&D" mode, which you do using the flasher utility, downloadable from the maemo site. The flasher utility requires an x86 Linux host, and this is also the preferred environment for running scratchbox. If you don't have an x86 Linux box, you can get an x86 machine powerful enough for this free with your breakfast cereal (and Linux is a free download). My host platform is Kubuntu, but that doesn't seem to matter; I did the same thing with SuSE, and I think the maemo people prefer plain Debian.

The maemo installation instructions work just fine. You have to install scratchbox first. The provided installer works fine, although its default behavior of deleting any files you've already downloaded so it can download them again is inexplicable. (You can install manually, too, but I don't see any reason to.)

The scratchbox installation requires root access, but once you've done it, you don't need root access to use scratchbox. If you use the provided maemo scratchbox installer, all you need to run as root is /scratchbox/sbin/sbox_ctl start and /scratchbox/sbin/sbox_adduser <user> with your user name. Log out and log back in so your new group membership (group sbox) will take effect and you're done.

You shouldn't need root access any more. In fact, the maemo SDK installer (I used; the newer 3.2 update is updated from within the SDK) will not run as root, only as a regular user with scratchbox access. The noise you just heard was a thousand sysadmins sighing in relief.

I actually find it quite comforting to have an installer that doesn't demand root privileges when they're not needed. You do not need to actually set up a target before running the maemo installer; ignore the messages warning you to run sb-menu first (if you look away, you may miss them as the installer gets on with things). Special thanks to the friendly people in Freenode's #maemo channel who reassured me that the error messages were harmless.

If you've never used scratchbox before, a little introduction is in order. Historically, cross-compilation has imposed a number of challenges. Most noticeably, it's hard to run test programs and the like in the target environment, and if it differs from the host environment, this makes configuration a dodgy experience.

Scratchbox solves this in the most direct way possible: you run in a native environment using CPU emulation to run native binaries. Given that my host CPU is roughly 8 times the speed of the target CPU, that's a pretty good solution; the payoff in development time saved is unbelievable. This is a great example of progress in technology allowing us to simply ignore a problem that used to be a real killer to overcome.

Scratchbox lets you have multiple build environments where you can mix and match target and host binaries. If you're using an ARM scratchbox on an x86 processor, ARM binaries just magically work and "cc" creates ARM binaries. This is an incredibly cool toy. Nokia funded its development, probably with this device and others like it in mind.

By the way, if you come back to this after a while only to get the cryptic message that "Scratchbox is not properly set up!," you probably need to start it. As root, run /scratchbox/sbin/sbox_ctl start and it should work.

Some systems may not shut down cleanly if you don't run sbox_ctl stop before trying to halt them. If, like me, you rebooted your system for unrelated reasons between installing scratchbox and trying to install the SDK, and you never set your system up to run scratchbox at boot, this one can be a bit of a surprise.

The other common quirk is for apt-get to fail within scratchbox, unable to resolve To fix this, edit /scratchbox/etc/nsswitch.conf and make sure it has "dns" instead of "mdns" on the "hosts:" line. (Note that this is not the same as the nsswitch.conf within your scratchbox environment.)

Been here before: Getting "hello, world!" running

The first step is always just to get "hello, world!" working. It's not hard, but before you start, you must select a target. A target is a set of settings and tools corresponding to a particular device (the N800). With a typical scratchbox environment, you'd have to create one. The maemo setup actually creates a pair of them, creatively named SDK_X86 and SDK_ARMEL. You can switch between them with the sb-conf command; for instance, sb-conf select SDK_ARMEL selects the ARM target.

Now, onto the code:

Listing 1. Um, hello, world!
#include <stdio.h>

/* stop me if you've heard this one */
main(void) {
            printf("hello, world!\n");
            return 0;

Save the sample code listing in a file. Now, and this is the tricky part, cross-compile it:

Listing 2. X-compile it!
[sbox-SDK_ARMEL: ~] > cc -o hello hello.c
[sbox-SDK_ARMEL: ~] > file hello
hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), for GNU/Linux 2.4.17, 
 dynamically linked (uses shared libs), not stripped
[sbox-SDK_ARMEL: ~] > ./hello
hello, world!

It really is that easy. You're probably wondering how does the file get onto the tablet? If you've installed a shell on the tablet and you have an SD card in either of its slots, it's pretty easy: plug the tablet into your Linux box, mount the filesystem on the drive that shows up, and copy the file over. Unmount, unplug, and watch as the drive magically remounts on the tablet. Of course, somewhere in here the file loses execute permissions, but you can copy it off the card, and then chmod +x.

That's okay for a single file, assuming that you've already installed a terminal program and so on. In this case, since the program really does have to run in a shell anyway, it even sort of makes sense to do it this way.

However, it's not really the way you'd want to do a real application, especially a graphical one. For that, you would want to create a proper package file (the N800 uses Debian packages) and let the user install it the proper way. Well, when it comes to doing things the right way instead of adopting a quick hack, there's no time like the present.

Creating maemo packages

developerWorks has the best documentation I found on creating Debian packages (see Related topics). Of course, there's more to it for maemo because it's not enough for your program to be installed in /usr/bin; you also need a way for the UI to discern your package and run it. The link to the sample "hello, world!" application isn't working as of this writing, but that's okay; just apt-get source hello-world-app in your scratchbox environment. That package lays out the whole nine yards, though, including icons and launcher configuration.

Building it is simple enough: run to generate the configure script, then run dpkg-buildpackage -rfakeroot. If this fails because you don't have dpkg-buildpackage in $PATH, run sb-menu and make sure you've installed the debian devkit. Do not install the debian-sarge devkit; it will make your Debian tools think you are building for the arm architecture instead of armel and then you can never have anything nice. Also, the N800 will refuse to install your packages because they are for an incompatible architecture. So, don't do that.

Maemo applications are built using Gtk widgets. There's a bit of extra magic: a family of "osso_..." functions, which are wrappers around the normal Gnome dbus functions. If you don't use these, your application may not work correctly in the maemo environment. The "hello world" sample is a good starting point for understanding the environment. Take a look at it, build packages, and make sure you're comfortable with that.

With that in mind, let's build some applications. While the Hello World application is a very complete example, it's also a fairly excessive one. The average application does not need to show up on the status bar, in the control panel, as a DBUS service and a regular application, and also as an applet for the home page. In fact, typically one or two of these is sufficient. Furthermore, the need to do all of these things is why the application is implemented in effect as a shared library used by all the different components. That's the easiest way to share code and keep each of the examples focused on the tasks unique to its implementation. Excellent for teaching and as an example; not necessarily the best template for your own application. But what application to target?

The N800 has a really neat feature, a little pop-out camera. By default, if you pop this open, it immediately launches a little interactive chat program. That's neat, but it's not all that flexible. What might be nice is to have an application that can snap individual pictures and automatically upload them to a remote site. That takes advantage of the N800's convenient ability to hook up through a variety of networks and gives us a bit more control over which frames get sent.

This turns out to be a fairly involved process, so I'm not going to develop a fully polished app. Rather, I'm going to focus on the basics of getting it running. Conveniently, most of the work is already done for us in the form of gstreamer, a library for handling media streams. The goal is to extract frames from the camera at particular intervals and save them somewhere, preferably on another machine, and possibly also allow the user to push a button to save frames.

Preparing for the next installment

In Part 2 of this series, we'll look at a program that actually does the core work of building a gstreamer pipeline to get images from the Web camera to the screen or to another system. The goal is to get images from the camera, convert them to JPEG, and upload them somewhere.

In the meantime, go ahead and read up on gstreamer if you aren't familiar with it. The gstreamer environment has a fairly large set of concepts to pick up. Take the time to get a terminal program installed, too; I recommend osso-xterm.

Techspecs for the N800

In general, the Nokia N800 is a portable Internet tablet that lets you access the Web with the following technologies:

  • High-resolution wide screen display
  • Opera 8 browser
  • Adobe Flash 9 browser plug-in
  • Internet communications
  • Skype support
  • Internet calling with an integrated Webcam
  • Instant Messaging
  • Multi-protocol email client.
  • Full-screen finger keyboard
  • Stereo speakers
  • Media player
  • UPnP architecture
  • Real Rhapsody
  • Expandable mass memory
  • 800 x 480-pixel, 65,536-color touchscreen

Its standard silver/black unit weighs in at:

  • Volume: 137cc
  • Weight: 206g
  • Length: 75mm
  • Width: 144mm

Its memory is a DDR RAM 128MB; there's also Flash 256MB and a 128 MiniSD with extender. The two internal memory card slots are compatible with SD, MicroSD, MiniSD, MMC, and RS-MMC. It supports memory cards up to 8GB. SD cards over 2GB must be SDHC compatible.

The BP-5L battery should produce a browsing time of up to 3.5 hours (standby 13 days).

The on-boards support such file formats as:

  • Audio: AAC, AMR, AWB, M4A, MP2, MP3, RA (RealAudio), WAV, WMA
  • Image: BMP, GIF, ICO, JPEG, PNG, TIFF, SVG-tiny
  • Video: 3GP, AVI, H.263, MPEG-1, MPEG-4, RV (Real Video)
  • Internet radio playlists: M3U, PLS

It handles connectivity via:

  • WLAN: 802.11b/g
  • Bluetooth specification: 2.0 (for Internet connection and file transfer via phone)
  • Nokia AV connector 3.5mm. Profiles supported: Dial-up Networking, File Transfer, Generic Access, SIM Access, Object Push Profile, Human Interface Profile and Serial Port profiles
  • USB 2.0 high speed device mode for PC connectivity

Downloadable resources

Related topics

ArticleTitle=Linux on board: Developing for the Nokia N800