sunnuntai 24. elokuuta 2008

Scripting the system behaviour

Here are the scripts that makes the system work as I want it to.

First thing is to set some system settings in rc.local. So /etc/rc.d/rc.local sets power-on-after-power-failure, starts x and sets the ir-receiver to be able to start the system from sleep state:
/sbin/setpci -s 0:1f.0 0xa4.b=0
/usr/bin/startx &
echo "USB3" > /proc/acpi/wakeup

Vdr is started with vdrd.sh script which has while loop to keep vdr running all the time. vdrd.sh script is started from vdr service (listed below)

#! /bin/bash
#
VDRDIR="/usr/vdr"
VDRPRG="vdr"
LIRCD="/usr/local/sbin/lircd -d /dev/hiddev1"
VDRADM="/usr/vdradmin/vdradmind.pl"
VDR_LOGFILE="/var/log/vdr_log.log"
VDR_PIDFILE="/var/run/vdr.pid"

if [ -e "/etc/vdr/vdr_exit_mode_shutdown" ];
then
  mv /etc/vdr/vdr_exit_mode_shutdown /etc/vdr/vdr_exit_mode_sleep
fi

watchdog=30
osdteletext=1
ttxtsubs=1
dvd=0
tvonscreen=0
text2skin=0
mp3=0
wapd=0
skinenigmang=1
streamdev=0
streamdevclient=0
femon=1
burn=1
softdevice=0
epgsearch=1
xineliboutput=1
zpoweroff=0

VDRCMD="$VDRDIR/$VDRPRG --lirc --no-kbd --log=3 --watchdog=$watchdog -L $VDRDIR/PLUGINS/lib"

if [ "$osdteletext" -eq "1" ];
then
  VDRCMD=$VDRCMD" -Posdteletext"
fi
if [ "$ttxtsubs" -eq "1" ];
then
  VDRCMD=$VDRCMD" -Pttxtsubs"
fi
if [ "$dvd" -eq "1" ];
then             
  VDRCMD=$VDRCMD" -Pdvd"
fi
if [ "$tvonscreen" -eq "1" ];      
then
  VDRCMD=$VDRCMD" -Ptvonscreen"
fi
if [ "$text2skin" -eq "1" ];                
then
  VDRCMD=$VDRCMD" -Ptext2skin"
fi
if [ "$mp3" -eq "1" ];
then       
  VDRCMD=$VDRCMD" -P'mp3 -m /video/plugins/mount.sh'"
fi        
if [ "$wapd" -eq "1" ];
then        
  VDRCMD=$VDRCMD" -Pwapd"
fi   
if [ "$zpoweroff" -eq "1" ];
then          
  VDRCMD=$VDRCMD" -Pzpoweroff"
fi               
if [ "$skinenigmang" -eq "1" ];
then  
  VDRCMD=$VDRCMD" -Pskinenigmang"
fi         
if [ "$streamdev" -eq "1" ];
then       
  VDRCMD=$VDRCMD" -Pstreamdev-server"
fi
if [ "$streamdevclient" -eq "1" ];
then
  VDRCMD=$VDRCMD" -Pstreamdev-client"
fi
if [ "$femon" -eq "1" ];
then
  VDRCMD=$VDRCMD" -Pfemon"
fi
if [ "$burn" -eq "1" ];
then
  VDRCMD=$VDRCMD" -Pburn"
fi
if [ "$epgsearch" -eq "1" ];
then
  VDRCMD=$VDRCMD" -Pepgsearch"
fi
if [ "$softdevice" -eq "1" ];
then
  VDRCMD=$VDRCMD" -P'softdevice -vo xv:'"
fi
if [ "$xineliboutput" -eq "1" ];
then                  
  VDRCMD=$VDRCMD" -P'xineliboutput --local=sxfe --video=xv --audio=alsa --remote=none'"
fi


VDRCMD=$VDRCMD" -s /usr/local/bin/vdr_shutdownscript.sh $* --terminal=/dev/tty8"


KILL="/usr/bin/kill -9"     
LANG=en_US

VDRPID=`/sbin/pidof vdr`      
if [ "$VDRPID" ] 
then
  echo "already running"
  exit
fi         

date >> $VDR_LOGFILE
echo "startup" >> $VDR_LOGFILE       

while (true) do
  su -c /usr/local/bin/loaddvb
  sleep 1
  cd $VDRDIR
  su -c /usr/local/bin/vdr_errorlogger.sh &
  touch $VDR_PIDFILE
  su -c "$VDRCMD"         
  date >> $VDR_LOGFILE
  echo "restart" >> $VDR_LOGFILE
  echo "restarting VDR"

  VDRPID=`/sbin/pidof vdr`
  if [ "$VDRPID" ]          
  then
    $KILL $VDRPID             
  fi
  rm -f $VDR_PIDFILE         
  sleep 1
  su -c /usr/local/bin/unloaddvb         
  LIRCDPID=`/sbin/pidof lircd`
  $KILL $LIRCDPID               
  kill `ps ax | grep "vdr_errorlogger.sh" | grep -v grep | awk '{print $1}'`
  su -c "$LIRCD"
done

Helper script for the one above checks if there's some errors in the log that needs reloading dvb drivers. I haven't seen any of them so those lines are from my VDR machine with FF cards.

vdr_errorlogger.sh
#Executable script
#!/bin/sh

#echo "" > /var/log/vdr_errors.log

while(true) do
  tail -n24 /var/log/messages > /tmp/tail_messages
  if fgrep -q 'av7110_send_fw_cmd' /tmp/tail_messages
  then
    date >> /var/log/vdr_errors.log
    echo "virhe" >> /var/log/vdr_errors.log
    /bin/sh /usr/local/bin/restarter.sh &
    exit
  fi
  if fgrep -q 'dvb-ttpci: StopHWFilter error' /tmp/tail_messages
  then
    date >> /var/log/vdr_errors.log
    echo "virhe" >> /var/log/vdr_errors.log
    /bin/sh /usr/local/bin/restarter.sh &
    exit
  fi
  if fgrep -q 'dvb-ttpci: StartHWFilter error' /tmp/tail_messages
  then
    date >> /var/log/vdr_errors.log
    echo "virhe" >> /var/log/vdr_errors.log
    /bin/sh /usr/local/bin/restarter.sh &
    exit
  fi
  if fgrep -q 'dvb-ttpci: av7110_fw_cmd error -1' /tmp/tail_messages
  then
    date >> /var/log/vdr_errors.log
    echo "virhe" >> /var/log/vdr_errors.log
    /bin/sh /usr/local/bin/restarter.sh &
    exit
  fi
sleep 2
done


Here's the vdr service script, located in /etc/rc.d/init.d:

vdr
#!/bin/sh
#
# /etc/init.d/vdr
# Subsystem file for vdr server
#
# chkconfig: 2345 98 02
# description: vdr server daemon
#
# processname: vdr
# config: /etc/MySystem/mySystem.conf
# config: /etc/sysconfig/vdr
# pidfile: /var/run/MySystem.pid

# source function library
. /etc/rc.d/init.d/functions

# pull in sysconfig settings
#[ -f /etc/sysconfig/vdr ] && . /etc/sysconfig/vdr

RETVAL=0
DISPLAY=:0
prog="vdr"

start() {
        echo -n $"Starting $prog:"
        sh -c /usr/local/bin/vdrd.sh & &> /dev/null
        if [ -e /usr/vdradmin/vdradmind.pl ]
        then
          sh -c /usr/vdradmin/vdradmind.pl &> /dev/null
        fi
        RETVAL=$?
        echo
[ "$RETVAL" = 0 ] && touch /var/lock/subsys/vdr
}

stop() {
        echo -n $"Stopping $prog:"
        vdrpid=`pidof vdr`
        vdrscriptpid=`ps ax | grep 'bash /usr/local/bin/vdrd.sh' | grep -v grep | awk '{print $1}'`
        vdradminpid=`ps ax | grep 'perl /usr/vdradmin/vdradmind.pl' | grep -v grep | awk '{print $1}'`
        vdrerrorpid=`ps ax | grep 'bash -c /usr/local/bin/vdr_errorlogger.sh' | grep -v grep | awk '{print $1}'`
        kill -9 $vdrscriptpid &> /dev/null
        kill -9 $vdrerrorpid &> /dev/null
        kill -9 $vdradminpid &> /dev/null
        killproc $prog &> /dev/null
  if [ -e "/usr/vdradmin/vdradmind.pid" ];
        then           
          rm -f /usr/vdradmin/vdradmind.pid &> /dev/null
  fi
        if [ -e "/var/run/vdr.pid" ];
        then
          rm -f /var/run/vdr.pid &> /dev/null
        fi
        sleep 2
        /usr/local/bin/unloaddvb &> /dev/null
        RETVAL=$?           
        echo
        [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/vdr
}

case "$1" in
        start)
           start
;;
        stop)
                stop              
                ;;
        restart)
                stop
                start
           ;;
        condrestart)
                if [ -f /var/lock/subsys/$prog ] ; then
               stop
                        # avoid race
                        sleep 3
                        start
                fi                
                ;;        
        status)
                status $prog
                RETVAL=$?
                ;;
        *)
                echo $"Usage: $0         
{start|stop|restart|condrestart|status}"
         RETVAL=1
esac                   
exit $RETVAL

And similar script for lirc:

#!/bin/sh
#
# /etc/init.d/lircd
# Subsystem file for lircd server
#
# chkconfig: 2345 98 02
# description: lircd server daemon
#
# processname: lircd
# config: /etc/MySystem/mySystem.conf
# config: /etc/lircd.conf
# pidfile: /var/run/MySystem.pid

# source function library
. /etc/rc.d/init.d/functions

# pull in sysconfig settings
#[ -f /etc/lircd.conf ] && . /etc/lircd.conf

RETVAL=0
prog="lircd"




start() {
        echo -n $"Starting $prog:"
#        /bin/setserial /dev/ttyS0 uart none
#        modprobe lirc_serial
        /usr/local/sbin/lircd -d /dev/hiddev1 /etc/lircd.conf
        RETVAL=$?
        [ "$RETVAL" = 0 ] && touch /var/lock/subsys/$prog
        echo
}

stop() {
        echo -n $"Stopping $prog:"
        killall -q -TERM lirc
        killproc $prog -TERM
#        rmmod lirc_serial
        RETVAL=$?
        [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/$prog
        echo
}                  

case "$1" in
        start)         
                start             
                ;;
        stop)       
                stop                 
                ;;       
        restart)                
                stop
                start    
                ;;          
        condrestart)
                if [ -f /var/lock/subsys/$prog ] ; then
                        stop                
                        # avoid race
                        sleep 3
                        start
                fi
                ;;
        status)
               status $prog
         RETVAL=$?
                ;;                
        *)
                echo $"Usage: $0
{start|stop|restart||condrestart|status}"
                RETVAL=1
esac
exit $RETVAL

To make vdr to start automatically you need following link (runlevel 3)
/etc/rc.d/rc3.d/S99vdrd -> ../etc/rc.d/init.d/vdrd

Following two scripts are used for loading and unloading dvb-drivers:

loaddvd 
# !/bin/sh
#
/sbin/modprobe dvb_usb_dibusb_common
/sbin/modprobe dvb_usb_dibusb_mc
/sbin/modprobe dvb-usb
/sbin/modprobe dvb_usb_dib0700

unloaddvb
# !/bin/sh
#
rmmod dvb_usb_dib0700
rmmod dib0070
rmmod dvb_usb_dibusb_mc
rmmod dvb_usb_dibusb_common
rmmod dib7000p
rmmod dib7000m
rmmod dib3000mc
rmmod dibx000_common
rmmod dvb-usb

Following script is called from VDR when it shuts down. It should work on different kind of systems by changing the config variables to match the system settings.

vdr_shutdownscript.sh
#Executable script
#!/bin/sh

##########
# config #
##########

# debug mode, if set to 1 no actions are done
debug=0

# warmup time in seconds = how long it takes vdr
# to be ready from the moment power is turned on
warmup_time=180

# wakeup time when in home mode
wakeup_hour_home=8
wakeup_minutes_home=0
let wakeup_timeinminutes_home=$wakeup_hour_home*60+$wakeup_minutes_home

# wakeup time when in control mode
wakeup_hour_control=17
wakeup_minutes_control=0
let wakeup_timeinminutes_control=$wakeup_hour_control*60+$wakeup_minutes_control

# shutdown time when in home mode
# earliest possible (depends on the vdr settings)
shutdown_hour_home=23
shutdown_minutes_home=0
let shutdown_timeinminutes_home=$shutdown_hour_home*60+$shutdown_minutes_home

# paths to the file and programs
mode_24h="/etc/vdr/vdr_mode_24h"
mode_unctrl="/etc/vdr/vdr_mode_unctrl"
mode_ctrl="/etc/vdr/vdr_mode_ctrl"
mode_home="/etc/vdr/vdr_mode_home"

#if exits system goes to sleep instead of shutdown
exit_mode_sleep="/etc/vdr/vdr_exit_mode_sleep"

#if acpi alarm can be used it will be used instead of rtc wakeup
acpialarm="/proc/acpi/alarm"

#program and file locations
HWCLOCK="/sbin/hwclock"
UNIX2ISO8601="/usr/local/bin/unix2iso8601"
SHUTDOWN="/usr/local/bin/Shutdownforvdr &"
SLEEP="/usr/local/bin/S3Sleepforvdr &"
LOGFILE="/var/log/vdr_log.log"

# utc or localtime
SYSCLOCKMODE="utc"

########################
# initial calculations #
########################

# parameters
# 1 - next timer in UTC
# 2 - next timer in seconds
# 3 - next timer's channel
# 4 - next timer's name
# 5 - shutdown reason, 0 = automatic, 1 = powerkey

# read day and time from vdr's timer using local time to solve the timings
tday=`$UNIX2ISO8601 -l $1 | awk {'print $1'}`
ttime=`$UNIX2ISO8601 -l $1 | awk {'print $2'}`

if [ $debug = 1 ];
then
  echo "wakeup_timeinminutes_home: $wakeup_timeinminutes_home"
fi

# vdr's next timer minus warmup time to avoid distortion
if [ $1 -ne 0 ];
then
  newtime=$(($1 - warmup_time ))   
  let timer_wakeup_time=$newtime
else
  newtime=0
  timer_wakeup_time=0
fi

timezone=`date +%:::z`
let timezonefix=$timezone*60

if [ $debug = 1 ];
then
  echo "newtime: $newtime"
  echo "wakeup_hour_home: $wakeup_hour_home"
  echo "wakeup_hour_control: $wakeup_hour_control"
fi

# parse time
if [ $1 -ne 0 ];
then
  if [ `echo ${tday:8:1}` = "0" ];
  then
    timer_day=`echo ${tday:9:1}`
  else
    timer_day=`echo ${tday:8:2}`
  fi
  if [ `echo ${ttime:0:1}` = "0" ];
  then
    timer_hour=`echo ${ttime:1:1}`
  else
    timer_hour=`echo ${ttime:0:2}`
  fi
  if [ `echo ${ttime:3:1}` = "0" ];
  then
    timer_minute=`echo ${ttime:4:1}`
  else
    timer_minute=`echo ${ttime:3:2}`
  fi
else
  timer_day=0
  timer_hour=0
  timer_minute=0
fi
let timer_inminutes="$timer_hour"*60+$timer_minute

if [ $debug = 1 ]
then
  echo "timer, day: $timer_day hour: $timer_hour min: $timer_minute"
  echo "timer, in minutes: $timer_inminutes"
fi

# read and parse current time
if [ $debug = 1 ];
then
#  current_year=`date +%Y`
#  current_month=`date +%m`
#  current_day=`date +%-d`
#  current_hour=`date +%k`
#  current_minute=`date +%-M`
  current_year=2008
  current_month="08"
  current_day=31
  current_hour=0
  current_minute=11
else
  current_year=`date +%Y`
  current_month=`date +%m`
  current_day=`date +%-d`
  current_hour=`date +%k`
  current_minute=`date +%-M`
fi
let current_timeinminutes="$current_hour"*60+"$current_minute"

if [ $debug = 1 ];
then
  echo "current time: month: $current_month day: $current_day hour: $current_hour min: $current_minute"
  echo "time in minutes: $current_timeinminutes"
fi


#count if this is a leap year
if [ `expr $current_year % 400` -eq 0 ];
then
  leap_year=1
else
  if [ `expr $current_year % 100` -eq 0 ];
  then
    leap_year=0
  else
    if [ `expr $current_year % 4` -eq 0 ];
    then
      leap_year=1
    else
      leap_year=0
    fi
  fi
fi


# count the next day
case "$current_month" in
  "01"|"03"|"05"|"07"|"08"|"10"|"12")
   if [ $debug = 1 ];
   then
     echo "31 days"
   fi
   if [ "$current_day" = 31 ];
   then
     let current_day_plus_1="1"
   else
     let current_day_plus_1="$current_day"+1
   fi
   ;;
  "02")
   if [ "$leap_year" = 1 ];
   then
     if [ "$current_day" = 29 ];
     then
       let current_day_plus_1="1"
     else
       let current_day_plus_1="$current_day"+1
     fi
   else
     if [ "$current_day" = 28 ];
     then
       let current_day_plus_1="1"
     else
       let current_day_plus_1="$current_day"+1
     fi
   fi
   ;;
  "04"|"06"|"09"|"11")
   if [ "$current_day" = 30 ];
   then
     let current_day_plus_1="1"
   else
     let current_day_plus_1="$current_day"+1
   fi
   ;;
esac

if [ $debug = 1 ];
then
  echo "next day: $current_day_plus_1"
fi


#string creation for acpi alarm controlled mode
if [ -e "$mode_ctrl" ];
then
  if [ "$current_hour" -lt "$wakeup_hour_control" ];
  then
    let wakeup_day="$current_day"
  else
    let wakeup_day="$current_day_plus_1"  
  fi
  if [ $SYSCLOCKMODE = "utc" ]
  then
    if [ -e $acpialarm ];
    then
      if [ "$wakeup_hour_control" -lt "$timezone" ];
      then
        let wakeup_hour_control=24-$wakeup_hour_control-$timezone
      else
        let wakeup_hour_control=$wakeup_hour_control-$timezone
      fi
    fi
    wakeuptime_control=`echo "$current_year-$current_month-$wakeup_day $wakeup_hour_control:$wakeup_minutes_control:00"`
    wakeuptime_control_unix=`date --date="$wakeuptime_control" +%s`
    if [ $debug = 1 ];
    then
      echo "wakeuptime_control: $wakeuptime_control, unix: $wakeuptime_control_unix"
    fi
  fi
fi

#string creation for acpi alarm home mode
if [ -e "$mode_home" ];
then
  if [ "$current_hour" -lt "$wakeup_hour_home" ];
  then
    let wakeup_day="$current_day"
  else
    let wakeup_day="$current_day_plus_1"  
  fi
  if [ $SYSCLOCKMODE = "utc" ]
  then
    if [ -e $acpialarm ];
    then
      if [ "$wakeup_hour_home" -lt "$timezone" ];
      then
        let wakeup_hour_home=24-$wakeup_hour_home-$timezone
      else
        let wakeup_hour_home=$wakeup_hour_home-$timezone
      fi
    fi    
  fi
  wakeuptime_home=`echo "$current_year-$current_month-$wakeup_day $wakeup_hour_home:$wakeup_minutes_home:00"`
  wakeuptime_home_unix=`date --date="$wakeuptime_home" +%s`
  if [ $debug = 1 ];
  then
    echo "wakeuptime_home: $wakeuptime_home, unix: $wakeuptime_home_unix"
  fi
fi

if [ $debug = 1 ];
then
  echo "timer wakeup time: $timer_wakeup_time"
fi

##################################
# recording ongoing, no shutdown #
##################################
if [ "$1" -lt 0 ];
then
  if [ $debug = 1 ];
  then
    echo "record ongoing, no shutdown"
  fi
  exit
fi

#################################################
# 24h mode, no shutdown except if manually done #
#################################################
if [ -e "$mode_24h" ];
then
  if [ $5 -eq 1 ];
  then
    # no timer, shutdown
    if [ $1 -eq 0 ];
    then
      if [ $debug = 1 ];
      then
        if [ -e "$exit_mode_sleep" ];
        then
          echo "24h mode, powerkey pressed, no timer, sleep"
          echo "$SLEEP"
        else
          echo "24h mode, powerkey pressed, no timer, shutdown"
          echo "$SHUTDOWN"
        fi
        exit
      else
        if [ -e "$exit_mode_sleep" ];
        then
          $SLEEP
        else
          $SHUTDOWN
        fi
        exit
      fi
    else
      # timer exists, wakeup for it
      #write time to acpi alarm and shutdown
      if [ $debug = 1 ];
      then
        if [ $SYSCLOCKMODE = "utc" ]
        then
          echo "$HWCLOCK --systohc --utc"
        else
          echo "$HWCLOCK --systohc --localtime"
        fi
        if [ -e "$acpialarm" ];
        then
          echo "acpi alarm in use"
        if [ $SYSCLOCKMODE = "utc" ]
          then
            echo `$UNIX2ISO8601 -u $timer_wakeup_time` "> /proc/acpi/alarm"
          else
            echo `$UNIX2ISO8601 -l $timer_wakeup_time` "> /proc/acpi/alarm"
          fi
          echo `cat /proc/acpi/alarm`
          if [ -e "$exit_mode_sleep" ];
          then
            echo "24h mode: wakeup for timer, sleep"
            echo "$SLEEP"
          else
            echo "24h mode: wakeup for timer, shutdown"
            echo "$SHUTDOWN"
          fi
          exit
        else
          echo "rtc wakealarm in use"
          echo "0 > /sys/class/rtc/rtc0/wakealarm"
          echo "$timer_wakeup_time > /sys/class/rtc/rtc0/wakealarm"
          echo "cat /sys/class/rtc/rtc0/wakealarm"
          if [ -e "$exit_mode_sleep" ];
          then
            echo "24h mode: wakeup for timer, sleep"
            echo "$SLEEP"
          else
            echo "24h mode: wakeup for timer, shutdown"
            echo "$SHUTDOWN"
          fi
          exit
        fi
      else
        if [ $SYSCLOCKMODE = "utc" ]
        then
          $HWCLOCK --systohc --utc
        else
          $HWCLOCK --systohc --localtime
        fi
        if [ -e "$acpialarm" ];
        then
          date >> $LOGFILE
          echo "uncontrolled: wakeup for timer" >> $LOGFILE
          if [ $SYSCLOCKMODE = "utc" ]
          then
            echo `$UNIX2ISO8601 -u $timer_wakeup_time` > /proc/acpi/alarm
          else
            echo `$UNIX2ISO8601 -l $timer_wakeup_time` > /proc/acpi/alarm
          fi
          if [ -e "$exit_mode_sleep" ];
          then
            $SLEEP
          else
            $SHUTDOWN
          fi
          exit
        else
          date >> $LOGFILE
          echo "24h mode: wakeup for timer" >> $LOGFILE
          echo 0 > /sys/class/rtc/rtc0/wakealarm
          echo "$timer_wakeup_time" > /sys/class/rtc/rtc0/wakealarm
          if [ -e "$exit_mode_sleep" ];
          then
            $SLEEP
          else
            $SHUTDOWN
          fi
          exit
        fi
      fi
    fi

  else
    if [ $debug = 1 ];
    then
      echo "24h mode, no automatic shutdown"
    fi
    exit  
  fi
fi

############################
# uncontrolled             #
# wakes up only for timers #
############################
if [ -e "$mode_unctrl" ];
then
  # no timer, shutdown
  if [ $1 -eq 0 ];
  then
    if [ $debug = 1 ];
    then
      echo "uncontrolled: no timer, shutdown";
      if [ -e "$exit_mode_sleep" ];
      then
        echo "$SLEEP"
      else
        echo "$SHUTDOWN"
      fi
      exit
    else
      if [ -e "$exit_mode_sleep" ];
      then
        $SLEEP
      else
        $SHUTDOWN
      fi
      exit
    fi
  else
    #write time to acpi alarm and shutdown
    if [ $debug = 1 ];
    then
      if [ $SYSCLOCKMODE = "utc" ]
      then
        echo "$HWCLOCK --systohc --utc"
      else
        echo "$HWCLOCK --systohc --localtime"
      fi
      if [ -e "$acpialarm" ];
      then
        echo "acpi alarm in use"
        if [ $SYSCLOCKMODE = "utc" ]
        then
          echo `$UNIX2ISO8601 -u $timer_wakeup_time` "> /proc/acpi/alarm"
        else
          echo `$UNIX2ISO8601 -l $timer_wakeup_time` "> /proc/acpi/alarm"
        fi
        echo `cat /proc/acpi/alarm`
        if [ -e "$exit_mode_sleep" ];
        then
          echo "uncontrolled: wakeup for timer, sleep"
          echo "$SLEEP"
        else
          echo "uncontrolled: wakeup for timer, shutdown"
          echo "$SHUTDOWN"
        fi
        exit
      else
        echo "rtc wakealarm in use"
        echo "0 > /sys/class/rtc/rtc0/wakealarm"
        echo "$timer_wakeup_time > /sys/class/rtc/rtc0/wakealarm"
        echo "cat /sys/class/rtc/rtc0/wakealarm"
        if [ -e "$exit_mode_sleep" ];
        then
          echo "uncontrolled: wakeup for timer, sleep"
          echo "$SLEEP"
        else
          echo "uncontrolled: wakeup for timer, shutdown"
          echo "$SHUTDOWN"
        fi
        exit
      fi
    else
      if [ $SYSCLOCKMODE = "utc" ]
      then
        $HWCLOCK --systohc --utc
      else
        $HWCLOCK --systohc --localtime
      fi
      if [ -e "$acpialarm" ];
      then
        date >> $LOGFILE
        echo "uncontrolled: wakeup for timer" >> $LOGFILE
        if [ $SYSCLOCKMODE = "utc" ]
        then
          echo `$UNIX2ISO8601 -u $timer_wakeup_time` > /proc/acpi/alarm
        else
          echo `$UNIX2ISO8601 -l $timer_wakeup_time` > /proc/acpi/alarm
        fi
        if [ -e "$exit_mode_sleep" ];
        then
          $SLEEP
        else
          $SHUTDOWN
        fi
        exit
      else
        date >> $LOGFILE
        echo "uncontrolled: wakeup for timer" >> $LOGFILE
        echo 0 > /sys/class/rtc/rtc0/wakealarm
        echo "$timer_wakeup_time" > /sys/class/rtc/rtc0/wakealarm
        if [ -e "$exit_mode_sleep" ];
        then
          $SLEEP
        else
          $SHUTDOWN
        fi
        exit
      fi
    fi
  fi
fi

########################################
# controlled                           #
# wakes up for timers and control time #
########################################
if [ -e "$mode_ctrl" ]
then
  # no timer
  if [ $1 -eq 0 ];
  then
    if [ $debug = 1 ];
    then
      echo "no timer set, wakeup at control time";
    fi
    wakeup="controltime";
  fi

  #9a
  if [ "$timer_day" -lt "$current_day" ];
  then
    if [ $debug = 1 ];
    then
      echo "9a, wakeup at control time";
    fi
    wakeup="controltime";
  fi

  #9b
  if [ "$timer_day" -eq "$current_day" ];
  then
    if [ "$current_hour" -lt "$wakeup_hour_control" ];
    then
      if [ "$timer_hour" -lt "$current_hour" ];
      then
        if [ $debug = 1 ];
        then
          echo "9b.1.1, wakeup at control time";
        fi
        wakeup="controltime";
      fi
      if [ "$timer_hour" -eq "$current_hour" ];
      then
        if [ "$timer_minute" -le "$current_minute" ];
        then
          if [ $debug = 1 ];
          then
            echo "9b.1.2.1, wakeup at control time";
          fi
          wakeup="controltime";
        else
          if [ $debug = 1 ];
          then
            echo "9b.1.2.2, wakeup for timer";
          fi
          wakeup="timer";
        fi
      fi
      if [ "$timer_hour" -gt "$current_hour" ];
      then
        if [ "$timer_hour" -ge "$wakeup_hour_control" ];
        then
          if [ $debug = 1 ];
          then
            echo "9b.1.3.1, wakeup at control time";
          fi
          wakeup="controltime";
        else
          if [ $debug = 1 ];
          then
            echo "9b.1.3.2, wakeup for timer";
          fi
          wakeup="timer";
        fi
      fi
    fi
    if [ "$current_hour" -eq "$wakeup_hour_control" ];
    then
      if [ "$timer_hour" -le "$current_hour" ];
      then
        if [ $debug = 1 ];
        then
          echo "9b.2.1, wakeup at control time";
        fi
        wakeup="controltime";
      else
        if [ $debug = 1 ];
        then
          echo "9b.2.2, wakeup for timer";
        fi
        wakeup="timer";
      fi
    fi
    if [ "$current_hour" -gt "$wakeup_hour_control" ];
    then
      if [ "$timer_hour" -lt "$current_hour" ];
      then
        if [ $debug = 1 ];
        then
          echo "9b.3.1, wakeup at control time";
        fi
        wakeup="controltime";
      else
        if [ $debug = 1 ];
        then
          echo "9b.3.2, wakeup for timer";
        fi
        wakeup="timer";
      fi
    fi
  fi

  #9c
  if [ "$timer_day" -eq "$current_day_plus_1" ];
  then
    if [ "$current_hour" -lt "$wakeup_hour_control" ];
    then
      if [ $debug = 1 ];
      then
        echo "9c.1, wakeup at control time";
      fi
      wakeup="controltime";
    else
      if [ "$timer_hour" -lt "$wakeup_hour_control" ];
      then
        if [ $debug = 1 ];
        then
          echo "9c.2.1, wakeup for timer";
        fi
        wakeup="timer";
      else
        if [ $debug = 1 ];
        then
          echo "9c.2.2, wakeup at control time";
        fi
        wakeup="controltime";
      fi
    fi
  fi

  #9d
  if [ "$timer_day" -gt "$current_day_plus_1" ];
  then
    if [ $debug = 1 ];
    then
      echo "9d, wakeup at control time";
    fi
    wakeup="controltime";
  fi

  if [ "$wakeup" = "controltime" ];
  then
    if [ $debug = 1 ];
    then
      if [ $SYSCLOCKMODE = "utc" ]
      then
        echo "$HWCLOCK --systohc --utc"
      else
        echo "$HWCLOCK --systohc --localtime"
      fi
      if [ -e "$acpialarm" ];
      then
        echo "acpi alarm in use"
        echo "$wakeuptime_control > /proc/acpi/alarm"
        echo "$wakeuptime_control"
        if [ -e "$exit_mode_sleep" ];
        then
          echo "controlled, wakeup for control time, sleep"
          echo "$SLEEP"
        else
          echo "controlled, wakeup for control time, shutdown"
          echo "$SHUTDOWN"
        fi
        exit
      else
        echo "rtc wakealarm in use"
        echo "0 > /sys/class/rtc/rtc0/wakealarm"
        echo "$wakeuptime_control_unix > /sys/class/rtc/rtc0/wakealarm"
        if [ -e "$exit_mode_sleep" ];
        then
          echo "controlled, wakeup for control time, sleep"
          echo "$SLEEP"
        else
          echo "controlled, wakeup for control time, shutdown"
          echo "$SHUTDOWN"
        fi
      fi

    else
      if [ $SYSCLOCKMODE = "utc" ]
      then
        $HWCLOCK --systohc --utc
      else
        $HWCLOCK --systohc --localtime
      fi
      if [ -e "$acpialarm" ];
      then
        date >> $LOGFILE
        echo "controlled: wakeup at control time" >> $LOGFILE
        echo $wakeuptime_control > /proc/acpi/alarm
        if [ -e "$exit_mode_sleep" ];
        then
          $SLEEP
        else
          $SHUTDOWN
        fi
        exit
      else
        date >> $LOGFILE
        echo "controlled: wakeup at control time" >> $LOGFILE
        echo "0" > /sys/class/rtc/rtc0/wakealarm
        echo $wakeuptime_control_unix > /sys/class/rtc/rtc0/wakealarm
        if [ -e "$exit_mode_sleep" ];
        then
          $SLEEP
        else
          $SHUTDOWN
        fi
      fi
fi
  fi

  if [ "$wakeup" = "timer" ];
  then
if [ $debug = 1 ];
then
      if [ $SYSCLOCKMODE = "utc" ]
      then
        echo "$HWCLOCK --systohc --utc"
      else
        echo "$HWCLOCK --systohc --localtime"
      fi
      if [ -e "$acpialarm" ];
      then
        echo "acpi alarm in use"
        if [ $SYSCLOCKMODE = "utc" ]
        then
          echo `$UNIX2ISO8601 -u $timer_wakeup_time` "> /proc/acpi/alarm"
        else
          echo `$UNIX2ISO8601 -l $timer_wakeup_time` "> /proc/acpi/alarm"
        fi
        if [ -e "$exit_mode_sleep" ];
        then
          echo "controlled: wakeup for timer, sleep"
          echo "$SLEEP"
        else
          echo "controlled: wakeup for timer, shutdown"
          echo "$SHUTDOWN"
        fi
        exit
 else
        echo "rtc wakealarm in use"
        echo "0 > /sys/class/rtc/rtc0/wakealarm"
        echo "$timer_wakeup_time > /sys/class/rtc/rtc0/wakealarm"
        if [ -e "$exit_mode_sleep" ];
        then
          echo "controlled: wakeup for timer, sleep"
          echo "$SLEEP"
        else
          echo "controlled: wakeup for timer, shutdown"
          echo "$SHUTDOWN"
        fi
 fi
else
      if [ $SYSCLOCKMODE = "utc" ]
      then
        $HWCLOCK --systohc --utc
      else
        $HWCLOCK --systohc --localtime
      fi
      if [ -e "$acpialarm" ];
      then
        date >> $LOGFILE
        echo "controlled: wakeup for timer" >> $LOGFILE
        if [ $SYSCLOCKMODE = "utc" ]
        then
          echo `$UNIX2ISO8601 -u $timer_wakeup_time` > /proc/acpi/alarm
        else
          echo `$UNIX2ISO8601 -l $timer_wakeup_time` > /proc/acpi/alarm
        fi
        if [ -e "$exit_mode_sleep" ];
        then
          $SLEEP
        else
          $SHUTDOWN
        fi
        exit
      else
        date >> $LOGFILE
        echo "controlled: wakeup for timer" >> $LOGFILE
        echo "0" > /sys/class/rtc/rtc0/wakealarm
        echo $timer_wakeup_time > /sys/class/rtc/rtc0/wakealarm
        if [ -e "$exit_mode_sleep" ];
        then
          $SLEEP
        else
          $SHUTDOWN
        fi
      fi
fi
  fi
fi 




########
# home #
########
if [ -e "$mode_home" ];
then
  shutdown=0
  # power off by user
  if [ "$5" -eq 1 ];
  then
    let shutdown=1
    if [ $debug = 1 ];
    then
      echo "home: power key pressed"
    fi
  fi
  #check if it is time to shutdown
  if [ "$current_timeinminutes" -gt "$shutdown_timeinminutes_home" ];
  then
    if [ "$current_timeinminutes" -lt "$wakeup_timeinminutes_home" ];
    then
      if [ "$shutdown_timeinminutes_home" -lt "$wakeup_timeinminutes_home" ];
      then
        let shutdown=1
      fi
    fi
  fi

  if [ "$current_timeinminutes" -lt "$shutdown_timeinminutes_home" ];
  then
    if [ "$current_timeinminutes" -lt "$wakeup_timeinminutes_home" ];
    then
      if [ "$shutdown_timeinminutes_home" -gt "$wakeup_timeinminutes_home" ];
      then
        let shutdown=1
      fi
    fi
  fi

  if [ "$current_timeinminutes" -gt "$shutdown_timeinminutes_home" ];
  then
    if [ "$current_timeinminutes" -gt "$wakeup_timeinminutes_home" ];
    then
      if [ "$shutdown_timeinminutes_home" -gt "$wakeup_timeinminutes_home" ];
      then
        let shutdown=1
      fi
    fi
  fi

  if [ "$shutdown" -eq 1 ];
  then
    #check first that is there timers still today
    timer_today=0;
    if [ "$current_hour" -ge "$wakeup_hour_home" ] && [ "$current_hour" -le 23 ];
    then
      if [ "$timer_day" -eq "$current_day" ];
      then
        if [ "$timer_inminutes" -gt "$current_timeinminutes" ];
        then
          if [ $debug = 1 ];
          then
            echo "home: timer still today, wakeup for timer"
          fi
          wakeup="timer";
          let timer_today=1
        fi
      else
        let current_day="$current_day_plus_1"
        if [ $debug = 1 ];
        then
          echo "home: day updated for tomorrow"
          echo "$current_day"
        fi
      fi
    fi
    # no timers today, update day for tomorrow
    if [ "$current_hour" -ge "$wakeup_hour_home" ] && [ "$current_hour" -le 23 ] && [ "$timer_today" -ne 1 ];
    then 
      let current_day="$current_day_plus_1"
      if [ $debug = 1 ];
      then
        echo "home: day updated for tomorrow"
        echo "$current_day"
      fi
    fi
    if [ "$timer_today" -ne 1 ];
    then
      if [ "$timer_day" -eq "$current_day" ];
      then
        # timer before wakeup time
        if [ "$timer_inminutes" -lt "$wakeup_timeinminutes_home" ];
        then
          if [ $debug = 1 ];
          then
            echo "home: timer before wakeup time, wakeup for timer"
          fi
          wakeup="timer";
        else
          if [ $debug = 1 ];
          then
            echo "home: timer at or later than wakeup time, wakeup at wakeup time"
          fi
          wakeup="wakeuptime";
        fi
      else
        if [ $debug = 1 ];
        then
          echo "home: no timer for that day, wakeup at wakeup time"
        fi
        wakeup="wakeuptime";
      fi
    fi
  else
    if [ $debug = 1 ];
    then
      echo "home: no shutdown"
    fi
    exit
  fi

  if [ "$wakeup" = "wakeuptime" ];
  then
    if [ $debug = 1 ];
    then
      if [ $SYSCLOCKMODE = "utc" ]
      then
        echo "$HWCLOCK --systohc --utc"
      else
        echo "$HWCLOCK --systohc --localtime"
      fi
      if [ -e "$acpialarm" ];
      then
        echo "acpialarm in use"
        echo "$wakeuptime_home > /proc/acpi/alarm"
        if [ -e "$exit_mode_sleep" ];
        then
          echo "home: wakeup at wakeup time, sleep"
          echo "$SLEEP"
        else
          echo "home: wakeup at wakeup time, shutdown"
          echo "$SHUTDOWN"
        fi
        exit
      else
        echo "rtc wakealarm in use"
        echo "0 > /sys/class/rtc/rtc0/wakealarm"
        echo "$wakeuptime_home_unix > /sys/class/rtc/rtc0/wakealarm"
        if [ -e "$exit_mode_sleep" ];
        then
          echo "home: wakeup at wakeup time, sleep"
          echo "$SLEEP"
        else
          echo "home: wakeup at wakeup time, shutdown"
          echo "$SHUTDOWN"
        fi
      fi
    else
      if [ $SYSCLOCKMODE = "utc" ]
      then
        $HWCLOCK --systohc --utc
      else
        $HWCLOCK --systohc --localtime
      fi
      if [ -e "$acpialarm" ];
      then
        date >> $LOGFILE
        echo "home: shutdown for wakeup time" >> $LOGFILE
        echo $wakeuptime_home > /proc/acpi/alarm
        if [ -e "$exit_mode_sleep" ];
        then
          $SLEEP
        else
          $SHUTDOWN
        fi
        exit
      else
        date >> $LOGFILE
        echo "home: shutdown for wakeup time" >> $LOGFILE
        echo "0" > /sys/class/rtc/rtc0/wakealarm
        echo "$wakeuptime_home_unix" > /sys/class/rtc/rtc0/wakealarm
        if [ -e "$exit_mode_sleep" ];
        then
          $SLEEP
        else
          $SHUTDOWN
        fi
      fi
fi
  fi

  if [ "$wakeup" = "timer" ];
  then
    if [ $debug = 1 ];
    then
      if [ $SYSCLOCKMODE = "utc" ]
      then
        echo "$HWCLOCK --systohc --utc"
      else
        echo "$HWCLOCK --systohc --localtime"
      fi
      if [ -e "$acpialarm" ];
      then
        if [ $SYSCLOCKMODE = "utc" ]
        then
          echo `$UNIX2ISO8601 -u $timer_wakeup_time` "> /proc/acpi/alarm"
        else
          echo `$UNIX2ISO8601 -l $timer_wakeup_time` "> /proc/acpi/alarm"
        fi
        if [ -e "$exit_mode_sleep" ];
        then
          echo "home: wakeup for timer, sleep"
          echo "$SLEEP"
        else
          echo "home: wakeup for timer, shutdown"
          echo "$SHUTDOWN"
        fi
        exit
      else
        echo "rtc wakealarm in use"
        echo "0 > /sys/class/rtc/rtc0/wakealarm"
        echo "$timer_wakeup_time > /sys/class/rtc/rtc0/wakealarm"
        if [ -e "$exit_mode_sleep" ];
        then
          echo "home: wakeup for timer, sleep"
          echo "$SLEEP"
        else
          echo "home: wakeup for timer, shutdown"
          echo "$SHUTDOWN"
        fi
      fi
    else
      if [ $SYSCLOCKMODE = "utc" ]
      then
        $HWCLOCK --systohc --utc
      else
        $HWCLOCK --systohc --localtime
      fi
      if [ -e "$acpialarm" ];
      then
        date >> $LOGFILE
        echo "home: shutdown for timer" >> $LOGFILE
        if [ $SYSCLOCKMODE = "utc" ]
        then
          echo `$UNIX2ISO8601 -u $timer_wakeup_time` > /proc/acpi/alarm
        else
          echo `$UNIX2ISO8601 -l $timer_wakeup_time` > /proc/acpi/alarm
        fi
        if [ -e "$exit_mode_sleep" ];
        then
          $SLEEP
        else
          $SHUTDOWN
        fi
        exit
      else
        date >> $LOGFILE
        echo "home: shutdown for timer" >> $LOGFILE
        echo "0" > /sys/class/rtc/rtc0/wakealarm
        echo $timer_wakeup_time > /sys/class/rtc/rtc0/wakealarm
        if [ -e "$exit_mode_sleep" ];
        then
          $SLEEP
        else
          $SHUTDOWN
        fi
      fi
    fi
  fi
fi


Helper scripts for shutdown, all located in /usr/local/bin:

S3Sleep:
# !/bin/sh
#
echo "3" > /proc/acpi/sleep

S3Sleepforvdr
#! /bin/sh
#
/sbin/service vdr stop
sleep 3
/usr/local/bin/forcekillvdr.sh
/usr/local/bin/unloaddvb
PIDOFX=`/sbin/pidof X`
kill -9 $PIDOFX
sleep 2
/usr/local/bin/S3Sleep
sleep 1
/usr/local/bin/loaddvb
/usr/bin/startx &
sleep 3
/sbin/service vdr start

Shutdownforvdr
#! /bin/sh
#
/sbin/service vdr stop
/usr/local/bin/unloaddvb
/sbin/shutdown -h now

forcekillvdr.sh
#!/bin/sh
#
echo -n $"Stopping vdr:"
vdrpid=`pidof vdr`
vdrscriptpid=`ps ax | grep 'bash /usr/local/bin/vdrd.sh' | grep -v grep | awk '{print $1}'`
vdradminpid=`ps ax | grep 'perl /usr/vdradmin/vdradmind.pl' | grep -v grep | awk '{print $1}'`
vdrerrorpid=`ps ax | grep 'bash -c /usr/local/bin/vdr_errorlogger.sh' | grep -v grep | awk '{print $1}'`
kill -9 $vdrscriptpid &> /dev/null
kill -9 $vdrerrorpid &> /dev/null
kill -9 $vdrpid &> /dev/null
kill -9 $vdradminpid &> /dev/null

Following two scripts are used to offer a re-scan function from VDR's commands menu.

channelscan.sh
#!/bin/sh
#
sh -c /usr/local/bin/vdrscan.sh &
sh -c /usr/local/bin/forcekillvdr.sh &

vdrscan.sh
#!/bin/sh
#
VDRLOG="/var/log/vdr_log.log"
sleep 2
/usr/local/bin/scan -o vdr -e 6 /etc/vdr/fi-Kruunupyy > /video/tmp/channels.tmp
CHANNELCOUNT=`cat /video/tmp/channels.tmp | grep -c ";"`
if [ $CHANNELCOUNT -lt 1 ];
then
  echo "No channels found!" >> $VDRLOG
else
  if [ -e /video/tmp/channels.tmp ];
  then
    mv /video/channels.conf /video/channels.backup
    mv /video/tmp/channels.tmp /video/channels.conf
    echo "Channels copied" >> $VDRLOG
  fi
fi
/usr/local/bin/vdrd.sh &

If I make changes to those scripts I will update this post regardingly.

New firmware for T14BR

I just noticed that there's a new firmware for T14BR. It can be downloaded from here. You may need to rename the file to 1.10 version as older drivers won't search for that.

I still have problems with DVD playback. The language selections are not working correctly with xineliboutput. When I get fix for that I only need to see if I can do something for the refresh rate problems and write a manual for the system. I think that my scripts are now finished and I will post them soon.

torstai 7. elokuuta 2008

New DVB receiver

I went today to the store to ask about the service of the faulty receiver. Found out that Artec T14 was unavailable, so I got T14BRLite instead. T14BR is also supported by the kernel I'm using, but it needs another firmware, which can be found from here.

After putting the firmware to /lib/firmware receiver started to work, but I noticed a problem with the T14BR's built-in IR-receiver. I got error messages to the log saying "unknown remote command". I googled around and found out that the IR-receiver can be disabled by putting following line into /etc/modprobe.d/options 

options dvb_usb disable_rc_polling=1

After that the receiver started to work fine. Actually the reception seems to be better than with T14.

I also continued to work with the scripts, which should be in quite good shape now.