Firewire Priming

From MythTV Official Wiki
Revision as of 17:32, 8 October 2006 by Fryfrog (talk | contribs) (POSSIBLE NEW REPLACEMENT SCRIPT FOR ALL OF THE ABOVE!)

Jump to: navigation, search

This is a script which is invoked each time a channel change is requested and a firewire connection needs to be working. It first tests the existing p2p and channel settings and if OK, test to see if the result from test-mpeg2 is a non-zero file which means the firewire connection is working. If the result is negative for any of the p2p, channel and non-zero file tests, it then tries to use plugctl to reset the firewire connection, and then runs the test-mpeg2 test again. It repeats ten times and if it gets 10 non-zero results, it fails.

Note:

  • It relies on two other programs, one is a helper script called "kill_test.sh" in the /home directory which is run in the background (&) to stop the test-mpeg2 capture after one second and the source is at the bottom. The other is the 6200ch change channel script, also mentioned at the bottom.
  • It assumes the firewire connection is on node 1 and p2p routing (not bcast).
  • If you run mythbackend with a log file, i.e. "mythbackend >> /home/mythbackend_log.log &", the echo statements below will appear in the log which is useful for debugging.

Primary script

#!/bin/bash
# v1.01, a Firewire change channel and test script by Sean Donovan, techonfoot.com
# Released under GPL, 2006
# Revision history
# v1.01 - Added detection for conditions that work intermittently, i.e. test-mpeg2 will work
#         but the next capture by MythTV won't.  Usually when p2p is 0 or channel <> 63.
#         Fixed a bug whereby the script expects the kill_test.sh script in the /home directory.
# v1.00 - Genesis
#
# ------------------------
# Prime the variables used to exit the while loop and set error messages 
# C2 is used to avoid the while loop or exit it, if the firewire is primed 
# C3 is used to notify either an error condition (=1) or not (=0)
C2=0
C3=1
# Change the channel
/home/6200ch $1
# Then change into the directory with the kill_test.sh script
cd /home
# First check to make sure the firewire port is set up correctly or the
# "Is it primed?" test could report positive but the firewire is in a failing state.
T1=`plugctl -n 1 opcr[0].n_p2p_connections`
T2=`plugctl -n 1 opcr[0].channel`
if [ $T1 != "1" ] ; then
       # Oops, p2p isn't one, let it go into the while loop for reset.
       echo "###### Oops, p2p isn't one!  Going to reset Firewire"  
elif [ $T2 != "63" ] ; then
       # Oops, the channel isn't 63
       echo "###### Oops, the channel isn't 63!  Going to reset Firewire"  
else 
       # Now check if the firewire is primed already, then do nothing:
       ./kill_test.sh &
       test-mpeg2 -r 1 > testcap.ts
       if [ -s testcap.ts ] ; then
               echo "#################"
               echo "# Firewire is primed!!!!!!!!!!!!"
               echo "################"
               # No error and 
               C3=0
               # Don't enter the while loop, the firewire is working already...
               C2=11
       fi
fi	
# While the counter is less then or equal to 10
# Enter the while loop and reset the firewire
while [ $C2 -le 10 ]
do
       C2=$((C2+=1))
       echo "##### Now Changing the Firewire setup for the $C2 time "
       # First set p2p to 0 and do a capture which seems to reset everything
       plugctl -n 1 opcr[0].n_p2p_connections=0
       ./kill_test.sh &
       test-mpeg2 -r 1 > testcap.ts
       # Now set it to what seems to work best, at least for the DCT-6200 series tuner...
       plugctl -n 1 ompr.bcast_channel=0
       plugctl -n 1 opcr[0].channel=63
       plugctl -n 1 opcr[0].bcast_connection=0
       plugctl -n 1 opcr[0].n_p2p_connections=1
       ./kill_test.sh &
       test-mpeg2 -r 1 > testcap.ts
       if [ -s testcap.ts ] ; then
               echo "################"
               echo "# Firewire is primed!!!!!!!!!!!!"
               echo "################"
               C3=0
               # Exit the loop
               C2=11
       fi	
       # Otherwise repeat the loop
done
# Now print the error codes if any.
if [ $C3 -eq 1 ] ; then
       echo "XXXXXXXXXXXXXXXX"
       echo "X Craappp it failed after ten tries - the tuner on?"
       echo "XXXXXXXXXXXXXXX"
fi
# Clean Up
rm testcap.ts

Required Helper Script

And then you need the helper script called "/home/kill_test.sh":

#!/bin/bash
# Sleep for a second
sleep 1
killall test-mpeg2

Required 6200ch change channel script

You also need the 6200ch change channel script which you can compile in the /usr/share/doc/mythtv-0.19/contrib/channel_changers directory (read the README). Don't forget you will need to get the headers for the kernel (yum install kernel-devel). On RedHat Fedora 5, you will also need the libavc1394-devel package, (yum install libavc1394-devel).


POSSIBLE NEW REPLACEMENT SCRIPT FOR ALL OF THE ABOVE!

#!/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