In order to occupy my mind whilst I look for a contract role (if you have one, please let me know!), I decided to start digging into ActiveSync and see what would be involved in building a simple server.
As I’m looking for a new role, I get calls from recruitment agents here in the UK and it’s a little hard to keep track of, especially if I’m out and about. Wouldn’t it be nice if I could just add a predefined list of contacts to my phone? That way I’ll always know who’s calling and what agency they are from [Most of the time – some agents block their numbers]. Another benefit is that when I’m done job hunting, I can remove all the contacts by simply removing the settings from my iPhone, instead of clogging up my personal contacts and having to delete them one by one. It also lets me add agencies that haven’t yet called me.
So enough about my “motivations”. I want to document this journey with a little detail, mainly so I have something to refer back to in the coming months. ActiveSync, to give you a little background, is a Microsoft Protocol designed for mobile devices to enable them to sync data with Exchange server. It’s standard across all major mobile platforms and is used by Outlook and other Exchange clients. I always though it was a closed protocol, but I discovered that it’s fully documented and doesn’t need to be licensed anymore! A quick Google and I found a ZIP containing 132 PDFs, covering the entire spec. I don’t know about you, but this is heaven for me. I love turning specifications into working software!
The starting point was [MS-ASHTTP].pdf , which covers the basics of the HTTP protocol. After implementing the OPTIONS verb in my API, I was able to get the iPhone to add my server. The OPTIONS verb returns basic information about the server, such as its version and the commands it supports. Nothing crazy!
The first issue was the SSL. I had to add the server, let validation fail, save the settings and then disable SSL under Advanced Settings before trying again. Once I had successfully added the server, I turned off all data with the exception of Contacts.
You can see that the “Automatic Reply” setting is “Loading”. I’ve no idea what to return to make that go away, so it’s something I will need to come back to at a future date.
With my iPod successfully connected to my ActiveSync server, I now needed to understand the Commands and the Sync process. This involved reading [MS-ASCMD].pdf. Not all of it, but just the sections on FolderSync and Sync. Having grasped the basics of the flow, I ran into a wall named WBXML!
WBXML is a standard for encoding XML to reduce payload size. Created back in the WAP days, it’s still in use. As luck would have it, I wrote a WBXML parser back in 2001 when I worked for CMG, so I knew the principal. Rather than do it again, I copied Microsoft’s sample code 🙂
With a working WXML encoding/decoder, I was in business! Or so I thought!
I’ll save you my tale of woe and angst, sufficeit to say the WebAPI MediaFormatters caught me out. When ever I returned data to the iPod, I would get a cryptic message in the logs about a code page not being selected. After lots of fiddler and Wiresharking, I realised by perfect WBXML was being converted to JSON by the WebAPI ! I added a new MediaFormatter and, bingo, my contacts folder appeared on my iPod.
After some more reading, I figured out how to add a Contact!
I celebrated with a macaroon. There is, of course, lots more to do and tracking sync changes is not an easy thing to do, but my aim is simple. I will add a simple web interface and database. It is then a matter of adding the details of the various recruitment agenicies .
I’ll be blogging again soon I hope!