All entries for Thursday 29 April 2010

April 29, 2010

O2 Joggler shell script strangeness

Last night I took delivery of an O2 Joggler which I ordered whilst they were recently on offer for £50. The offer exhausted O2's stock so I had to wait a couple weeks for mine to arrive. I didn't buy it for what O2 advertise it as, I bought it for what is is as piece of hardware. Inside is an Intel Atom Z520, 512MB ram, 1GB of storage and a wireless adaptor. Visible on the outside are an Ethernet port, 3.5mm headphone jack, USB port and a 7" 800x480 touch screen. Oh and a small sticker indicating it's 'powered by openpeak'. It runs Linux. I've seen people say that the Linux is a cut down Ubuntu 8.04 though I've not been able to find any proof of this. It very definitely has BusyBox on there though. Put some special files on to a USB flash drive, plug it in, boot the device and you have telnet access and can mess around with it's OS. You can also make it run a different OS off a flash drive. So, a neat little box that should be fun to play with. The Joggler has been around for nearly a year so there's already several forums out there already on the subject and the recent £50 offer has inevitably fuelled interest.

So far my biggest hack is to enable ssh access to it. Next will be installing scp so I can copy files/to from it easily. Earlier on I was having a bit of a general poke around in it, looking around the filesystem, seeing what processes were running and such when I discovered an interesting script. What makes it interesting are these three functions:

numDaysInMonth()
{
case $1 in
1|3|5|7|8|10|12)
echo 31
;;
2) echo $((28 + leap))
;;
4|6|9|11)
echo 30
;;
*) echo 0
;;
esac
}

parseTime()
{
set -- $(date "+%Y %m %d %H %M %S %Z")
Year=$1
CurMonth=`echo $2 | sed 's/^0//'`
Today=`echo $3 | sed 's/^0//'`
Hour=`echo $4 | sed 's/^0//'`
Min=`echo $5 | sed 's/^0//'`
Secs=`echo $6 | sed 's/^0//'`
Timezone=$7
}

# function to get a very close approximate to the number
# of seconds since epoch for use to mark app start time
# and, upon app exit, current time. The delta of current
# to start time is used to determine if the restart should
# be tallied or not by comparing to MinAppUpTime
##
calculateEpoch()
{
parseTime

# leap days in past years
##
leapdays=$(( (Year - 1969)/4 ))
leap=$(( Year % 4 == 0 ))

# days since the epoch, not counting earlier months this year
##
numDays=$(( (Year - 1970) * 365 + leapdays + Today - 1))

# step through earlier months this year and add the days
##
month=$((CurMonth - 1))
while [ ${month} -ge 1 ]; do
numDays=$(( numDays + `numDaysInMonth $month` ))
month=$(expr $month - 1)
done

# now the seconds...
##
epoch=$(( ( (numDays * 24 + Hour) * 60 + Min) * 60 + Secs ))

echo ${epoch}
}

The calculateEpoch function is later called like so:

startTime=`calculateEpoch`

I can't figure out why someone wrote all that code to get a 'close approximate', or to put it another way, incorrect, value for the number of seconds since epoch instead of using this to get the correct value:

startTime=`date +%s`

And yes, the BusyBox date function does allow that. If you happen to have a Joggler on which you've enabled shell access, the code in question appears in /openpeak/tango/xinit.sh


Search this blog

Tags

Not signed in
Sign in

Powered by BlogBuilder
© MMXXIV