More on SMS Chat
After I posted about my proof of concept SMS chat idea, I started thinking about integrating with the phone’s address book to automatically alias phone numbers to names, and then I remembered that BitPim was able to pull out the complete phonebook from my K1M.
I started poking around, and BitPim is an open-source app written in Python, plus is supports a protocol log which dumps all commands send to and received from a phone, which makes it really handy for reverse-engineering phone protocols.
What’s more, since there’s a Python impl for OS2008, if I write the SMS chat app in Python, I can use the BitPim code as my comms layer (albeit with the damnable GPL license terms). Sweet.
Proof of Concept: Bluetooth SMS Chat
As soon as I got my Nokia N810 Internet Tablet, I set up the WLAN and configured the built-in chat application to use my Google Apps for Domains GTalk accounts. Chatting on the slide-out keyboard from the comfort of a TV chair or couch was immediately sweet, and the automatic conversation archiving still allows me to go back and look at transcripts.
The problem is this doesn’t work when I’m out of range of an access point. Sure, I spend the bulk of my life within range of my home and office access points, but sometimes I’m traveling or out and about. I could of course use my Motorola K1M as a teathered EVDO device, but I’m not willing to pay VZW $60/mo for the privilege, nor am I willing to fraudulently use the EVDO without paying for it by manually modifying the Mobile IP profile. So, where does that leave me?
There’s only one phone service I can use without limit, and that’s SMS messaging. As it happens, the main app I want to use on the move is…chatting! So why can’t I use the N810 chat functionality with my K1M phone to chat via SMS? Indeed.
With the idea in mind, I set about looking for software to do what I want. I found gnokii, which supposedly supported all the necessary SMS functions (list, send, receive, register for receive notifications), but I couldn’t get it to run on the N810′s OS2008 to save my life. A few other SMS toolkits like SMSlib and Gammu weren’t any better.
Next I figured I could write something myself, at least as a proof of concept. So I started inquiring as to how one does SMS over Bluetooth. After much googling and gnashing of teeth, I determined that, in most cases, one connects to the phone over a serial link (USB, Bluetooth, or RS-232; the physical details don’t matter), and sends–get this–AT commands.
Unless you’re at least my age, you don’t know what AT commands are. Back in the dark ages, before broadband, we used devices called ‘modems’ to connect to other computers over regular phone lines. Modems were God-forsaken devices trying in vain to link the 1950s phone technology to the coming 21st century. One controlled a modem by sending ‘AT’ commands, so-called because each command began with ‘AT’. For example, ‘ATDT’ means ‘dial with DTMF’, ‘ATH’ means ‘hangup’, etc. Apparently, some European cell phone back in the 80′s used AT commands for its automation interface, and everyone ever sense has gone the same way.
Once I realized I was dealing with the AT commandset, I still needed to figure out what AT commands I needed. To start with, I took a guess that the COM port that appears on my laptop when I plug my K1M into the USB port (only because I installed the Motorola USB drivers; don’t expect shit like this to be plug-and-play) is the serial link suitable for using AT commands. To test the theory I plugged in my phone, fired up HyperTerminal, configured it to connect to the raw serial port used by my phone, and typed ‘ATZ’, which resets the state of the AT command processor. Lo! and behold, I got the ‘OK’ response back, confirming I was talking to an AT-aware device.
Once I had that working, it was time to figure out what exact AT commands to use. By chance I ran across gammu, a bit of Czech open source that supposedly exposes most of the data functions of different types of phones, in the form of a command line. After some fucking about I came up with a gammurc file that worked with my phone, gave it the COM port I found earlier, and had it do some commands like listing folders and such. The cool think about Gammu is it can generate a logfile of all the stuff it sends to and receives from the COM port, which is a great way for me to figure out what commands do what.
I was able to do a gammu text getsmsfolders successfully, but then I did gammu text getsms 1 1 and was nailed with Function not implemented. Help required.. Doh!
Getting desperate, I went to the Motorola developer’s site. I poked around their tech docs, and stumbled across a PDF document called “G24 AT Commands Developer Guide”. The G24 is a cellular card for use embedding cellular technology into electronic devices, but my hope was Motorola developers don’t reinvent the wheel (especially the AT commandset wheel) with every cellular device, and thus that the AT commands for the G24 resemble those for the K1M.
I skimmed the document, testing out various commands in HyperTerm. Some of them worked on my K1M, and others didn’t, then I got to the ‘SMS’ section of the document and hit paydirt. The document walked through the whole process of listing messages, choosing folders, writing messages, sending them, and deleting them. Yes!
You can get the details for yourself by downloading the G24 AT guide (motodev accounts are free), but here’s a sample (note this is a transcript from my test session; not all the commands are relevant to sending SMS):
AT+MODE=2
OK
+MBAN: Copyright 2000-2004 Motorola, Inc.
AT+CGMI
+CGMI: “Motorola CE, Copyright 2000″
OK
AT+CGMM
+CGMM: “CDMA800″,”CDMA1900″,”MODEL=K1mm”
OK
AT+CGMR
+CGMR: “24.0_00.26.0F”
AT+CPBS=?
+CPBS: (“ME”,”MT”,”ON”,”DC”,”MC”,”RC”,”AD”,”QD”)
OK
AT+CPMS=?
+CPMS: (“MT”,”IM”,”OM”,”DM”),(“OM”,”DM”),(“IM”)
OK
AT+CPMS=”IM”
+CPMS: 0,179
OK
AT+CMGF=1
OK
AT+CMGL=”ALL”
OK
AT+CPMS=”MT”
+CPMS: 4,179
AT+CMGW=”xxxxxxxxxx” <—– This is the destination number for the SMS
Fear is the mind killer <—– This is the text of the message, terminated by a Control-Z character
+CMGW: 121
OK
Pretty sweet! There’s also docs for call control, changing the character set, and GPRS stuff.
So, I had the basic AT commands needed to list, read, and send SMS messages. That’s great, but I was only able to do it with a USB connection to the phone, on a Windows machine with special Motorola drivers. I want it over Bluetooth, on my N810.
As a first step, I thought I’d try Bluetooth under Linux, which is basically what the N810 environment is. So I fired up thorby, my Ubuntu Feisty VM, and plugged in my Linksys USBBT100 USB Bluetooth adapter. Linux detected it right away, and I paired it with my phone in the usual way. Using these instructions, I setup a /etc/bluetooth/rmcomm.conf file mapping the DUN profile of my phone to /dev/rfcomm0. Since my phone doesn’t support the Bluetooth Serial Port Profile (SPP), I figured the only other profile that might allow me to issue AT commands is Dial-up networking, since after all it is semantically a dial-up modem device.
Once I had /dev/rfcomm0 mapped, I installed gtkterm, which is a GTK+ terminal program along the lines of HyperTerm. I pointed it at /dev/rfcomm0, 115200 bps, 8-N-1, and issued ATZ. Once again, I got back OK, and I was in!
I then issued more or less the same commands as above, and was able to send another SMS message, this time over Bluetooth!
So, at this point I now know that I can, in theory, use my N810 as a sort of SMS client via a Bluetooth connection to my K1M. I only today got the Maemo 4.0 development environment working, so I’m a bit behind the curve on building apps for the N810, but my first instinct is to try to get gnokii working, since it already has a bunch of shit in it to support a wide range of phones, and there’s really no reason to reinvent the wheel. However, if gnokii gives me any pushback, I’ll just roll my own and worry about supporting other phones if my prototype works to my satisfaction.
I must say, this little N810 has been just the thing to get me back to hacking on embedded devices; I forgot how rewarding it can be to slog through a jungle of underdocumented proprietary voodoo, and come out the other side with a neat solution.