All pastes #195967 Raw Edit

Unnamed

public text v1 · immutable
·published 2006-10-10 05:48 UTC
rendered paste body
#!/bin/bash
# Version 1.0, initial release!
# Remember, the -h option (and making a mistake) gives a usage summary!
#
# Firewire channel changing and test script, re-written by fryfrog at gmail.com$
# Original idea from Sean Donovan, techonfoot.com
#
# This script has been completely re-written to be more robust and useful than
# the original version.  It can be passed the port, node and number of tries.
# it also has a verbose and debug mode to help track down issues.  The defaults
# should be very similar to the original script.  Since this one can take port
# and node, the same script can be used for those with more than one HD tuner.    
#
# It also requires *no* outside scripts except for the 6200ch channel changer,
# for which I have included a small patch that allows it to change the channel
# on yet another model of the DCT6412 (HD-DVR).
#
# The script also uses variables for a large portion of the script, allowing
# (hopefully) it to be much easier to change and adapt to the future or your
# needs.
#
# Example usage:
# 2x firewire cable boxes.  `plugreport` shows them on adapter 0, node 0 & 1.
# After a bit of manual, 5 tries is found to be "ideal" and you would like
# debug messages.  Script can be found in /usr/local/bin/sure-change.sh
#
# Entry in mythtv-setup, input connections, channel changing script.
# /usr/local/bin/sure-change.sh -p 0 -n 0 -t 5 -d
# Entry for second box
# /usr/local/bin/sure-change.sh -p 0 -n 1 -t 5 -d
#
# Default values for port, node, debug and verbose
# These can be changed if desired, or fed via options
PORT=0
NODE=0
DEBUG=0
VERBOSE=0
TRIES=5

# Values for external binarys / scripts
# You probably want to point these to the correct place
CHANNEL_CHANGER="/usr/local/bin/6200ch"
TEST_MPEG2="/usr/bin/test-mpeg2"
PLUGCTL_PROG="/usr/bin/plugctl"
TEST_FILE="/tmp/testcap.ts"

# Lonely variable used for loop, dont touch!
X=1

# Usage function
usage() {
   echo "Usage:  [-p <port>] [-n <node>] [-t <tries>] [-v] [-d] [-h] <channel>"
   echo "-p <port>    ** The adapter port, default of 0"
   echo "-n <node>    ** The node of device, default of 0"
   echo "-t <tries>   ** Number of reset and test attemps"
   echo "-v           ** Verbose output"
   echo "-d           ** Debug output"
   echo "-h           ** Help and usage"
   echo "<channel>    ** Channel being changed to"
} 

# Port setting function
set_port() {
   PORT=$1
   (( DEBUG )) && echo "port set to: ${PORT}"
}

# Node setting function
set_node() {
   NODE=$1
   (( DEBUG )) && echo "node set to: ${NODE}"
}

set_firewire() {
   ${PLUGCTL_PROG} -p ${PORT} -n ${NODE} ompr.bcast_channel=0
   ${PLUGCTL_PROG} -p ${PORT} -n ${NODE} opcr[0].channel=${NODE}
   ${PLUGCTL_PROG} -p ${PORT} -n ${NODE} opcr[0].bcast_connection=0
   ${PLUGCTL_PROG} -p ${PORT} -n ${NODE} opcr[0].n_p2p_connections=1
   (( DEBUG )) && echo "firewire has been set for port ${PORT}, node ${NODE}"
}

reset_firewire() {
   ${PLUGCTL_PROG} -p ${PORT} -n ${NODE} opcr[0].n_p2p_connections=0
   read_firewire
   (( DEBUG )) && echo "firewire has been reset for port ${PORT}, node ${NODE}"
}

read_firewire() {
   ${TEST_MPEG2} -r ${NODE} 2> /dev/null > ${TEST_FILE} &
   sleep 1
   killall ${TEST_MPEG2}
   (( DEBUG )) && echo "read attempt on ${NODE} completed."
}

# Test firewire
test_firewire() {
   # Check if firewire is actually working
   (( DEBUG )) && echo "testing firewire"

   read_firewire

   if [ -s "${TEST_FILE}" ] ; then
      (( DEBUG )) && echo "firewire is working!"
      go_byebye
   else
      (( DEBUG )) && echo "firewire is not working"
   fi
}

# Exit
go_byebye() {
   clean
   (( $1 )) && echo "OH SHIT!" && exit 1
   exit 0
}

# Cleanup
clean() {
   if [ -f ${TEST_FILE} ]; then
      (( DEBUG )) && echo "test file found, removing"
      rm ${TEST_FILE}
   fi
}

# Begin main!
if [ $# == 0 ]; then
  usage
  go_byebye
fi

while getopts p:n:t:vdh OPTION;
do
   case ${OPTION} in
      p) set_port ${OPTARG}
         ;;
      n) set_node ${OPTARG}
         ;;
      v) VERBOSE=1
         ;;
      d) DEBUG=1
         ;;
      t) TRIES=${OPTARG}
         ;;
      *) usage
         exit 0
         ;;
   esac;
done

shift $(($OPTIND - 1))

if [ "$1" == "" ]; then
   echo "You forgot a <channel>!"
   usage
   go_byebye
else
   CHANNEL=$1
fi

clean

# Attempt to change the channel
(( DEBUG )) || (( VERBOSE )) && echo "${TRIES}x attempts at channel ${CHANNEL}"
${CHANNEL_CHANGER} -p ${PORT} -n ${NODE} ${CHANNEL}
(( $? )) && (( DEBUG )) && echo "couldn't even change channel with external ${C$

# Retrieve current firewire settings
#P2P_SETTING=`${PLUGCTL_PROG} -p ${PORT} -n ${NODE} opcr[0].n_p2p_connections`
#CHAN_SETTING=`${PLUGCTL_PROG} -p ${PORT} -n ${NODE} opcr[0].channel`

(( DEBUG )) && echo "pre-reset test of firewire"
test_firewire

# If fire wire isn't working, try to reset it
while [ ${X} -le ${TRIES} ]
do
   # Reset p2p and do a capture, aparantly this helps with the reset.
   (( DEBUG )) && echo "reset attempt ${X} of ${TRIES}"
   reset_firewire

   # Reset to known-good settings
   (( DEBUG )) && echo "set attempt ${X} of ${TRIES}"
   set_firewire

   # Test again after reset, hope it works!
   (( DEBUG )) && echo "test ${X} of ${TRIES}"
   test_firewire
   X=$((X+=1))
done

(( DEBUG )) || (( VERBOSE )) && echo "${X} attempts failed, could not change to$
go_byebye 1