Cheap 867 client¶
One of the beauties of having a shell environment available on your embedded device is the easy and cheap hacks that are available to you. Just this morning I’ve noticed my phone appears to be suffering from minor time drift, and I was thinking about a simple way to sync the time.
The basic idea is simple enough, every time I connect my phone to my desktop to sync data it can avail itself of any network services it wishes. So why not use the remote mount event to trigger a script to set the time, that way it runs every time I connect the phone and doesn’t require any manual intervention.
I’m lucky here, I already have a basic environment available on my phone provided mostly by busybox (although I’m likely to change that soon). So if busybox was built with CONFIG_RDATE then your job is simple, a call to rdate <timeserver> is probably all you need.
If you don’t have rdate available to you or, if like me, you only have access to a daytime server then you quickly set the time with a little script. You will need some version of netcat or netpipes, I personally use nc6. Obviously, you will need to change the snippet below depending on your choice of tool.
#! /bin/sh
set -- `nc6 timeserver 13`
date -s -u -d "$2 $3"
Note
You will probably want to use hwclock to save the time over reboots too.
Of course you could always use the bash network pipes support if the bash is available to you too. If it is available you can just use the output from /dev/tcp/<timeserver>/13 directly, without calling an external tool.
Update on 2005-06-01: If when trying to access /dev/tcp bash reports errors like cat: /dev/tcp/<timeserver>/13: No such file or directory, then you have a crippled bash installation. Some distributions choose to cripple the bash install by not including useful features, you should take it up with them not me ;).
The end result of this is my phone’s clock is now correct again, and it will probably find itself being resynced most work days now too. And thanks to the RFC 1305 support in Linux the amount of drift should hopefully be compensated for in the long term. If you’d like to compensate for drift I would suggest looking in to adjtimex, including the version shipped with busybox