## Python on Nokia/S60

I finally got a smartphone a week or two ago. I ended up getting a Nokia N86, based on a combination of form factor (I would’ve preferred a flip phone, but apparently not enough people do for there to be lots of different models, and I’m still a bit reluctant to go for touch interfaces), camera spec (optical zoom would’ve been a killer feature, but if you’re not Korean, apparently not until next year), and hackability. There seem to be a variety of smartphone flavours at the moment: there’s Windows CE (which I had on an old phone, and which sucked worse than I expected), there’s Blackberry (which is kinda closed and corporate, and tends to come with relatively crappy cameras for the price), there’s the iPhone (which has a mediocre camera, and no Python support at least as far as the app store is concerned), there’s Symbian (Nokia and Sony-Ericsson and maybe Samsung smartphones), and then there’s the Linux variants: Maemo (Nokia N900), WebOS (Palm Pre), and Android. Android would probably have been my preference there, but any of the Linuxes would work too, and I found out Symbian had decent Python support so that works too. And as it turned out the older Linux phones didn’t really match my feature list, and the newer ones, while pretty, are either as yet unavailable or kinda expensive (or both). So an N86 it was. Which has turned out to do pretty much everything I wanted so far.

Nokia’s phone roadmap is pretty confusing (which seems to be par for the course for the telco industry admittedly) supporting both Maemo as well as different versions of Symbian everywhere (the most recent ones of which are apparently open sourced) as well as lower end “feature” phones. Apparently my Symbian version is “S60 3rd Edition”, and if you want to write apps the difference between that and “2nd edition” is a major one. S60 3rd ed is also, apparently, known as “Symbian OS 9.3”, the followup to which is “Symbian^1” which is also known as “S60 5th Edition” (4th Edition got skipped because 4 is unlucky for some). It’s on the latest Nokia smart phones — N97, 5800 XPressMusic, etc. “Symbian^2” and beyond will apparently be the open source versions, except that maybe it’ll be “Symbian^3” before any phones ship with it. There was also S90 which was more advanced than S60 at the time, but then got merged back in, so now it’s obsolete and S60 is better. Apparently the way phone OSes work is that you only upgrade when you get new hardware, so all the different versions ever are still floating around on old phones.

Anyway, add ons for Symbian come packaged in “sis” files (“Symbian Installation Source” supposedly), which come signed in various different ways (some are signed for only a particular phone, in part to make it harder to write viruses, eg) and with different listed capabilities (so if your app doesn’t need to use gps or wifi, it will be blocked by the OS from doing so). When I first got the phone, the deal was you’d scour the web, and discover that you could download Python for S60 1.9.7 from garage.maemo.org — which is a SourceForge-clone that presumably was originally for Maemo stuff, but is now for anything Nokia-ish. Of course, there was no Python 1.9.7, and this is really Python 2.5.4 with miscellaneous Symbian extensions. In order to get a Python REPL prompt, you need to install both the Python_1.9.7.sis runtime and the PythonScriptShell sis, of which there are a few with different capabilities. Of course, you can only do this after unpacking the PythonForS60_1.9.7.tar.gz, which you can’t do on the phone.

Anyway, get that done and look at the docs and you can actually do something, which is kinda cool. I’ve been just plugging it into my laptop as USB mass storage and copying py scripts across, then running them from the ScriptShell menu so far, which is a bit kludgy but at least usable. So far it seems like lots of little bits of the API aren’t quite implemented for Python — I haven’t found a way to change the top right softkey hint from “Back” or “Exit” to “Save” or “Hide”; but that might just be unfamiliarity. More concerning was that when I tried to use time.mktime to get a Unix timestamp, the interpretor just crashed entirely, so it seems like there are some bugs around too. But the fact that you seem to be able to get at pretty much all the phone features (camera, gps, gsm location, sms, etc, etc) from pretty simple python still makes it a win in my book.

Shortly after I’d gotten that far, I did a random invocation of the “SW Update” app to see if there was any new stuff for me, and got informed “Python for S60 2.0” was available for download. Neato, I thought, and went looking to see what the deal was — but there’s almost nothing out there discussing it. I tried installing it anyway, but apparently the download got cut off, and SW Update isn’t smart enough to continue or start again in that event. But deleting the partial download and trying again worked, and eventually I got me some Python for S60 2.0, which seems to be the same 2.5.4 version 1.9.7 was. The advantage, in theory, should be that programs written in python can just have a sis file that says “I need Python” and the official version will be automatically downloaded. But my first go at making that happen seems to indicate that the dependency used to be on “Python for S60”, but maybe now needs to be on “Python runtime”. Which, of course, is hardcoded into the app (ensymble), and although that’s an open source Python app (and packaged for Debian at that), it’s distributed as a base64 encoded blob so you have to go right back to the source to change the appropriate seven characters. Assuming, of course, that I’m on the right track in my guess as to what the problem is.

As far as I can tell, there’s still not much in the way of any sort of official announcement as to what’s going on with PyS60 2.0, but it seems that they’re rolling it out to some handsets, and the N86 is just lucky on that score. It’s bizarre to me that the Nokia devteam aren’t doing any bragging about getting Python for S60 up to 2.0 and into the official distribution, but I get the impression there isn’t much communication going on in general. I haven’t been able to spot the source for 2.0 either, though I haven’t exactly looked very hard.

1. Russell Stuart says:

Mass Storage mode disables to the sdcard, which can be annoying, and possibly worse if the cable falls out and corrupts the FAT file system. This command will transfer a file in “PC Mode” – the same mode the Nokia Tools use, and the mode that also gives you access to the modem, etc:

sudo obexftp -u 1 -c e:/directory/name/ -p filename …

The command above is for cable. A slight variation will go over bluetooth, but don’t I recommend that. The Symbian Bluetooth stack is rock solid, but the kernel’s bluetooth stack will die after a few transfers, and the only fix is a reboot.

The next useful thing is python script shell, which gives you a python prompt. It is included in the pys60 package. It allows you to run the debugger and do just about everything else you can do from within python on a normal linux box. In particular, you can send new versions of your .py files using the command above, and run then without exiting the interpreter, and you can print trace to sys.stdout. Speeds up development no end.

Cool though that may be, it is rather painful if you have to interact with the script shell using the phones keyboard. But you don’t have to. Instead you can redirect the phones python command prompt to go over a Bluetooth tty. Just pair the phone with your debian computer (something you only need do one), run this shell script:

rfcomm=/dev/rfcomm2
temp=”/tmp/${0##*/}-$$.tmp” hciconfig reset sdptool add –channel=”${rfcomm##*m}” SP
sudo rfcomm listen “${rfcomm#/dev/}” “${rfcomm##*m}” &
trap ‘rm -f “${temp}”; [ -z “${rfcomm_pid}” ] || sudo kill ${rfcomm_pid} 2>/dev/null’ 0 1 2 15 rfcomm_pid=$!
while [ ! -c “${rfcomm}” ] do sleep 1 || exit$?
done
cu -l “\${rfcomm}”
rfcomm_pid=

Then ask the python script shell to open a command prompt over bluetooth. You end up with a development environment on the phone that isn’t too different to the one on Linux. The bugs in the Linux bluetooth stack don’t get a chance top bite because you don’t start and stop bluetooth connections that often.

You can do some cool things from Python, as it has total access to the phone. For example, I managed buy a Bluetooth to USB printer adaptor, and then print stuff using a python prog on my phone. You can send and receive SMS’s, make phone calls, find the current location using GPS, make network connections over wireless, take pictures, send them, record sounds. Modern phones actually have more useful IO peripherals in them than a modern PC.

If you want to use time.mktime() avoid the latest version of Ps60 (1.9.7). Calling mktime() in the crashes the interpreter. 1.9.6 works fine though.

Finally, although Nokia’s future road map is a mess, it is a well planned mess. It goes like this. (a) Port QT to Symbian and Maemo. (b) Rewrite every app to use the API provided by QT. (c) Symbian and Maemo then converge because they run the same apps. (d) World domination. (e) Profit. I am hoping they get to (e) before being wiped out by Google and Apple.

2. therohan says:

Forum Nokia announces a technical workshop to learn how to create WRT and know more about QT in Sydney, Australia on 11th May, 2010. Great opportunity for mobile developers.
Checkout: http://www.codeofpartnership.com/home.html
Thanks

3. TheRohan says:

Came across this very interesting training course which can be used for both self-learning and educational purposes. Topics that are covered in this course are:
• Qt for Symbian introduction
• Development environment set up
• Smart installer
• Memory management
• Active objects
• Platform security
• Mobility APIs
This ZIP package includes training documentation in 3 lectures and lab exercises with example code. This is a beta release of this training course.