Sunday, February 1, 2009

BSNL Dataone Auto Downloader

Bsnl(India) provides an account that has 2am to 8am unlimited download. It was not easy to wake up at 2am and switch on the computer and sleep again. So I tried to find a method to switch on the computer automatically at 2am and download and shutdown at 8am. Although the I thought the idea was foolish, I found out that there actually is a method for doing it. "ACPI Alarm" was introduced in 1999 or so (Even my old PC had it).

So what does this alarm do? The ACPI alarm wakes up the computer from shutdown or hibernate. You need to keep the UPS or Power Plug to the CPU on after scheduling the time in ACPI or it gets reseted.

This function needs Bios Support and I feel most bios supports this feature.
The method to do this depends on your kernel version.
So check the Kernel Version first using the command.

uname -r

If Kernel > 2.6.22 and higher use /sys/class/rtc/rtc0/wakealarm

Kernel < 2.6.21 and lower use /proc/acpi/alarm

Disable hwclock updates

On most machines it's required to make a small change to the Linux shutdown procedure. When your machine goes down, most linux distributions write the system time/data back to the bios. On MANY machines the machines never wakes-up after a time/data update. It's recommended to make this change before you start. See below for more details (distro specific),

The reason for the recommendation above is that most linux distributions write the current system time back to the bios when shutting down the machine, and with some BIOSes, the machine will not wake up if the hardware clock is modified after the alarm timer has been set. To avoid that, it is necessary to disable the writing of the current system time to the hardware clock in the system shutdown scripts. This is distribution specific, so here are some examples:



Debian (and Ubuntu)
modifying
/etc/default/rcS
with the following will fix this problem:
Image:Script.png /etc/default/rcS
#
# /etc/default/rcS
#
# Default settings for the scripts in /etc/rcS.d/
#
# For information about these variables see the rcS(5) manual page.
#
# This file belongs to the "initscripts" package.

TMPTIME=0
SULOGIN=no
DELAYLOGIN=no
UTC=no
VERBOSE=no
FSCKFIX=no
==> HWCLOCKACCESS=no

Gentoo Set clock_systohc to "NO" in
/etc/conf.d/clock
:
Image:Script.png /etc/conf.d/clock
# Set CLOCK to "UTC" if your system clock is set to UTC (also known as
# Greenwich Mean Time).  If your clock is set to the local time, then
# set CLOCK to "local".  Note that if you dual boot with Windows, then
# you should set it to "local".
clock="UTC"

# If you want to set the Hardware Clock to the current System Time
# during shutdown, then say "YES" here.
# You normally don't need to do this if you run a ntp daemon.
clock_systohc="NO"

# If you wish to pass any other arguments to hwclock during bootup,
# you may do so here. Alpha users may wish to use --arc or --srm here.
clock_args=""


openSUSE
modifying
/etc/sysconfig/clock
holds a switch to write back system time to the hardware clock
Image:Script.png /etc/sysconfig/clock
# Set to "-u" if your system clock is set to UTC, and to "--localtime"
# if your clock runs that way.
#
HWCLOCK="-u"

SYSTOHC="no"
# Is set to "yes" write back the system time to the hardware
# clock at reboot or shutdown. Usefull if hardware clock is
# much more inaccurate than system clock.  Set to "no" if
# system time does it wrong due e.g. missed timer interrupts.
# If set to "no" the hardware clock adjust feature is also
# skipped because it is rather useless without writing back
# the system time to the hardware clock.
               

Fedora Core 6
modifying
/etc/init.d/halt
with the following will fix this problem:

Image:Script.png /etc/init.d/halt
==>  ACPITIME=`cat /proc/acpi/alarm`

     [ -x /sbin/hwclock ] && action $"Syncing hardware clock to system time" /sbin/hwclock $CLOCKFLAGS
                
==>  echo "$ACPITIME" > /proc/acpi/alarm  

Using /proc/acpi/alarm for Kernel 2.6.21 and lower


Check that you can write a new time to the RTC Clock Alarm (you will need to be Root) Format is YYYY-MM-DD HH:MM:SS
echo "2009-02-1 02:00:00" > /proc/acpi/alarm

Using /sys/class/rtc/rtc0/wakealarm


Beginning with kernel 2.6.22 /proc/acpi/alarm has been removed and replaced with /sys/class/rtc/rtc0/wakealarm. The key differences with the wakealarm interface are:
  1. Instead of accepting a formatted time, wakealarm accepts the number of seconds since Jan 1, 1970.
  2. If you want to change the wakealarm time, you first need to reset the time.
  3. You have to make sure that your bios clock is set to UTC time not localtime otherwise it will wakeup at the wrong time. None the less it is still possible if the bios clock is set to localtime (likely if you also run windows), see the section below for how to set the alarm correctly when the bios clock is in localtime.

An example of setting alarm is as follows
echo 0 > /sys/class/rtc/rtc0/wakealarm
date -u --date "Jul 1, 2008 10:32:00" +%s > /sys/class/rtc/rtc0/wakealarm

Then we can confirm that the alarm is set with the following.
cat /proc/driver/rtc

If the alarm is set then you should see something like this. If so then shutdown and see if it wakes up at the alarm date/time.
rtc_time : 13:40:26
rtc_date : 2008-12-21
alrm_time : 10:45:00
alrm_date : 2008-12-22
alarm_IRQ : yes
alrm_pending : no
24hr : yes
periodic_IRQ : no
update_IRQ : no
HPET_emulated : no
DST_enable : no
periodic_freq : 1024
batt_status : okay
If you see the alarm date similar to ****-12-21 then the alarm is set to a time in the past and it won't wake up.
rtc_time : 13:42:01
rtc_date : 2008-12-21
alrm_time : 13:46:59
alrm_date : ****-12-21
alarm_IRQ : no
alrm_pending : no
24hr : yes
periodic_IRQ : no
update_IRQ : no
HPET_emulated : no
DST_enable : no
periodic_freq : 1024
batt_status : okay
In most systems this method should work. But in some Bios called as Fussy Bios using this function is tough. Lets see the different problems it might have and it's solution

  1. Disable/Enable RTC in BIOS

    In some cases you need to disable the RTC alarm function is the bios to make things work.

    * Another possible glitch is, that the option 'Resume By Alarm' (or whatever it is called) is set to Enabled but wake up using /proc/acpi/alarm only works if the option is set to Disabled. Sounds weird but works with some boards.
    * On many boards, when the RTC setting is enabled in the BIOS, it will wake only from a time set and saved from BIOS setup, and not from a time set outside of the BIOS setup environment - as we want. All of the boards the original author of this document needed this setting disabled to correctly wake with ACPI. This is the recommended starting point.
  2. Time/Date not visible in BIOS

    After you have set the RTC alarm from Linux, it could be that you not see changes in the BIOS but it still works.
  3. Required to write time/date two times

    Some users reported that their BIOS is only updated after writing 2 times to the alarm file. (reported with proc/acpi/alarm)

Ok, so the Basic of ACPI is done.Now lets see how we switch on the computer at 2am.
We use the command as,
  • If it's to be set on the next day 2pm :
    date +'%a %b %d 02:00:00 GMT %Y' -d '+1 day'|xargs -i date '+%s' -d''{}'' > /sys/class/rtc/rtc0/wakealarm
  • If it's to be set on the same day 2pm :
    date +'%a %b %d 02:00:00 GMT %Y' |xargs -i date '+%s' -d''{}'' > /sys/class/rtc/rtc0/wakealarm
So, the computer boots up at 2am. Now we need to reboot the modem.
Rebooting modem is required as Dataone calculates session from On time to Off time. If the modem is up from 1am to 5am and we download 4gb, what the dataone does is, it takes the total time modem was on ie 4 hours. 2am to 5am is free so the non free time is 1hr so we will be charged for 1gb (4gb/4 hours). So it's essential to reboot the modem at 2am.
The code to reboot is ,
#!/bin/bash
now=$(date +"%H")
now=${now#0};
if (( $now < 8 )) && (( $now > 1 ))
then
(echo "admin";
sleep 1;
echo "admin";
sleep 1;
echo "13";
sleep 1;
echo "1";
sleep 40;
)|telnet 192.168.1.1
deluge &
fi
deluge is torrent client I use.
We should run this command in sessions so that it runs at each boot.
We do this by Adding in
System -> Preferences -> Sessions -> Add
Give the path of the File that contains the above script. That's it!! Done!!. The script starts at 2am. Now we need to stop the script at 8am.
We use following code for that,
#!/bin/bash
now=$(date +"%H")
date +'%a %b %d 02:00:00 GMT %Y' -d '+1 day'|xargs -i date '+%s' -d''{}'' > /sys/class/rtc/rtc0/wakealarm
sudo killall deluge
sleep 5;
(echo "admin";
sleep 1;
echo "admin";
sleep 1;
echo "13";
sleep 1;
echo "1";
sleep 40;
)|telnet 192.168.1.1
sudo shutdown -h now
We use the crontab to schedule and run it at 7 59am. If you are not sure how to write a crontab entry see the posts before.
Well Happy Downloading!! Keep me Posted on how it went.

6 comments:

  1. now tat is a way to hav good sleep :)

    ReplyDelete
  2. is this method applicable, i mean the ACPI alarm, in case someoe is using Windows XP or VISTA? please help,the problem of waking up at 2 am is what i'm suffering from since 6 months,will be glad if u reply me at the earliest....

    ReplyDelete
  3. ya there is some software 4 it. I am not sure the name of it. It is called something like "Auto Poweron/Poweroff".
    Or you can see if you can set it straight from bios

    ReplyDelete
  4. that's wat i want to know how to make the changes in the bios????

    ReplyDelete
  5. if your bios supports acpi alarm you can go the bios and search 4 something called RTC alarm. And setting it is like setting date and time. The exact place you will find is bios dependent so search 4 your configurations or check the manual

    ReplyDelete
  6. I use windows 7 and follow a different method to auto download in BSNL 500 plan...i just use the task scheduler to launch IE at 2:10 am and 7:50 am..the trick is making http://192.168.1.1/rebootinfo.cgi as the home page so that as IE runs,the router is made to reboot and u can sleep peacefully

    ReplyDelete