Firewire Priming

From MythTV Official Wiki
Revision as of 12:31, 3 April 2007 by Fryfrog (talk | contribs) (Fryfrog's Multi Tuner Version)

Jump to: navigation, search

Introduction

Below are two firewire scripts which change channels and prime the firewire before MythTV starts to record. The first is for a one tuner setup and the second is a more flexible two tuner setup.

Both require the 6200ch helper program:

Required 6200ch change channel script

You also need the 6200ch change channel script which you can compile in the /usr/share/doc/mythtv-0.20/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).


  • Update: the sa3250hd primer doesn't need 6200ch but does look for jwestfalls firewire_tester script which should be located in the contrib directory. That section can be commented out if you don't have it though.


One Tuner Priming Script

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 the 6200ch change channel script.
  • It assumes the firewire connection is on node 1 and p2p routing (not bcast).
  • If your node is not 1, be sure to add the correct node in front of the channel change command. IE: '/home/6200ch -n2 $1' rather than '/home/6200ch $1'
  • 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.


#!/bin/bash
# v1.02, a Firewire change channel and test script by Sean Donovan, techonfoot.com
# Released under GPL, 2006
# Revision history
# v1.02 - Fixed the unnecessary kill_test.sh script
# 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:
       test-mpeg2 -r 1 > testcap.ts &
       sleep 1
       killall test-mpeg2
       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
       test-mpeg2 -r 1 > testcap.ts &
       sleep 1
       killall test-mpeg2
       # 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
       test-mpeg2 -r 1 > testcap.ts &
       sleep 1
       killall test-mpeg2
       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

Fryfrog's Multi Tuner Version

The latest version is a fairly big cleanup and re-tune. It now uses the firewire bus reset available in the "latest" version of firewire_tester. This means you may need to grab the source for it (it is only one file) from SVN head. It is worth it, I think. You can always modify the script a little if you don't want to use it (just deleting two lines should do it). It should now be faster in cases where things are working, and it uses the "single" packet method with 6200ch. I am having "better" luck with it now, but firewire still isn't work 100% for me. I'm almost ready to move to SVN with all the new firewire features.

Download latest version

#!/bin/bash
# Released under the GPL, 2006
# Current Version:  1.0.5
#
# Revision history:
# Version 1.0.5, Lots of minor changes.  The lenght of time spent fixing will
#                now depend on how many times it has looped.  This means that if
#                everything is working, it will be fast.  As it fails, it will spend
#                longer and longer trying to fix.  I also added in a re-tune
#                of the channel after X attemps and a firewire port reset after
#                Y attempts.  I cleaned up the code a bit, and it seems to be
#                working a bit better for me.
#                ** IMPORTANT ** -- The mode options "p" and "b" where changed to
#                "p" and "B" (sorry).
# Version 1.0.4, Added support for the -R reset firewire bus command in svn versions of the
#                firewire_tester program.
# Version 1.0.3, Added support for p2p *and* broadcast, using "firewire_tester"
# Version 1.0.2, Added a "seconds" option controlling how long it gets
#                input from firewire before testing.
# Version 1.0.1, Improved debugging output/formatting!
# Version 1.0.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, fryfrog.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 fw tuner.
#
# 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.
#
# Requirements:
# You will need a channel changer (like 6200ch for the DCT series of STBs),
# the "firewire_tester" program that comes in ./contrib of MythTV, and finally
# the "test-mpeg2" & "plugctl" programs that should be part of libiec61883
#
# 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
MODE="p"
PORT=0
NODE=0
DEBUG=0
VERBOSE=0
TRIES=5
TIME=1

# Values for external binarys / scripts
# You probably want to point these to the correct place
CHANNEL_CHANGER="/usr/local/bin/6200ch"
FIREWIRE_TESTER="/usr/local/bin/firewire_tester"
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 "   -m <mode>    ** [p]2p or [B]roadcast"
   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 "   -s <seconds> ** Number of seconds to record for test, default of 1"
   echo "                   strongly suggest something in the 1-2 range, 5 tries"
   echo "                   with 5 second time out is 25 seconds until fail!"
   echo "   -v           ** Verbose output"
   echo "   -d           ** Debug output"
   echo "   -h           ** Help and usage"
   echo "   <channel>    ** Channel being changed to"
}

# Debug & Verbose output
inform() {
   TIMESTAMP=`date +"%F %T"`
   MSG="$1"
   echo "${TIMESTAMP} - ${MSG}"
}

# Mode setting function
set_mode() {
   MODE="$1"
   if [[ "${MODE}" != "p" ]] && [[ "${MODE}" != "B" ]]; then
      echo "Invalid mode specified, use [p]2p or [B]roadcast"
      echo "${MODE}"
      usage
      exit 1
   fi
   (( DEBUG )) && inform "Mode used: ${MODE}"
}

# Port setting function
set_port() {
   PORT=$1
   (( DEBUG )) && inform "Port used: ${PORT}."
}

# Node setting function
set_node() {
   NODE=$1
   (( DEBUG )) && inform "Node used: ${NODE}."
}

set_p2p() {
   ${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 )) && inform "Default firewire settings applied to port ${PORT}, node ${NODE}."
}

set_channel() {
   # set mode, node and other options
   CHAN_OPTS="-s -p ${PORT} -n ${NODE} ${CHANNEL}"
   # set debug option
   (( DEBUG )) && TEST_OPTS="${CHAN_OPTS} -v"

   if [[ ${DEBUG} -eq 1 ]]; then
      ${CHANNEL_CHANGER} ${CHAN_OPTS}
   else
      ${CHANNEL_CHANGER} ${CHAN_OPTS} 2>&1 >/dev/null
   fi
   (( $? )) && inform "External ${CHANNEL_CHANGER} failure!" && exit 1
   (( DEBUG )) || (( VERBOSE )) && inform "Selected channel ${CHANNEL}."
}

reset_p2p() {
   ${PLUGCTL_PROG} -p ${PORT} -n ${NODE} opcr[0].n_p2p_connections=0
   read_p2p
   (( DEBUG )) && inform "Reset firewire settings on port ${PORT}, node ${NODE}."
}

read_p2p() {
   ${TEST_MPEG2} -r ${NODE} 2> /dev/null > ${TEST_FILE} &
   sleep ${TIME}
   killall ${TEST_MPEG2}
   (( DEBUG )) && inform "Attempted read of port ${PORT}, node ${NODE}."
}

# Reset the firewire bus, using new firewire_tester -R option
reset_firewire_bus() {
   # set mode, node and port options
   TEST_OPTS="-R -P ${PORT}"
   # set debug/verbose option
   (( DEBUG )) || (( VERBOSE )) && TEST_OPTS="${TEST_OPTS} -v"

   # run test with above assigned options
   if [[ ${DEBUG} -eq 1 ]]; then
      ${FIREWIRE_TESTER} ${TEST_OPTS}
   else
      ${FIREWIRE_TESTER} ${TEST_OPTS} 2>&1 >/dev/null
   fi

   if [[ $? -eq 0 ]]; then
      (( DEBUG )) || (( VERBOSE )) && inform "Reset of firewire port ${PORT} successful."
   else
      (( DEBUG )) || (( VERBOSE )) && inform "Reset of firewire port ${PORT} failed."
   fi
}

# Test firewire
test_firewire() {
   # set mode, node and port options (using X results in
   # longer attempts later in the loop
   TEST_OPTS="-${MODE} -P ${PORT} -n ${NODE} -r ${X}"
   # set debug/verbose option
   (( DEBUG )) && TEST_OPTS="${TEST_OPTS} -v"
   
   # run test with above assigned options
   if [[ ${DEBUG} -eq 1 ]]; then
      ${FIREWIRE_TESTER} ${TEST_OPTS}
   else
      ${FIREWIRE_TESTER} ${TEST_OPTS} 2>&1 >/dev/null
   fi

   if [[ $? -eq 0 ]]; then
      (( DEBUG )) || (( VERBOSE )) && inform "Firewire working after ${X} attempts!"
      go_byebye
   else
      (( DEBUG )) || (( VERBOSE )) && inform "Firewire NOT working yet after ${X} attempts."
   fi
}

# Exit
go_byebye() {
   clean
   (( $1 )) && exit 1
   exit 0
}

# Cleanup
clean() {
   if [ -f ${TEST_FILE} ]; then
      (( DEBUG )) && inform "Test file found, deleting."
      rm ${TEST_FILE}
   fi
}

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

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

shift $(($OPTIND - 1))

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

clean

# Attempt to change the channel
(( DEBUG )) || (( VERBOSE )) && inform "Trying ${TRIES} times to get channel ${CHANNEL}"
set_channel

# If fire wire isn't working, try to reset it
while [ ${X} -le ${TRIES} ]
do
   (( DEBUG )) && inform "Attempt ${X} of ${TRIES}..."

   if [[ "${MODE}" == "p" ]]; then
      # Reset p2p and do a capture, aparantly this helps with the reset.
      reset_p2p

      # Reset to known-good settings
      set_p2p
   fi

   # If it fails twice, try re-tuning the channel
   if [ $(( X % 3 )) -eq 0 ]; then
      set_channel
   fi

   # If it fails 3 times, reset the firewire bus
   if [ $(( X % 5 )) -eq 0 ]; then
      reset_firewire_bus
   fi

   # Test again after reset, hope it works!
   test_firewire

   X=$((X+=1))
done

(( DEBUG )) || (( VERBOSE )) && inform "${TRIES} attempts failed, could not change to ${CHANNEL} and recieve signal via firewire"
go_byebye 1

SA3250HD Primer

Primer Script for SA3250HD cable boxes written by jwestfall. Written for two boxes but works equally well on one box. Still can't reliably record from two of these boxes but this will get them going. I put this in my /usr/bin directory and call it from the startup programs in the sessions application in Ubuntu and it works every time for me. Post something to the mailing list if you have problems getting it going.

#!/bin/bash
# sa3520_fixer.sh [-v]
#
# This should only be used prior to starting the backend.  Using it as 
# part of an external channel changer would be a bad idea since its doing 
# a bus reset.
#
# jwestfall@surrealistic.net 

# List of nodes to fix
NODES="0 1"

# The number of times to try and fix a node
ATTEMPTS=5

# The number of consecutive tests that must be successful
# for a node to be considered working
TESTS=5

# verify firewire_tester and plugctl are installed
if [ "`which firewire_tester`" = "" ]; then
  echo "firewire_tester is required, but not found"
  exit
fi

if [ "`which plugctl`" = "" ]; then
  echo "plugctl is required, but not found"
  exit
fi

# deal with -v commandline
VERBOSE="/dev/null"
if [ "$1" = "-v" ]; then
  echo "verbose output enabled"
  VERBOSE="/dev/stdout"
fi

# global failed variable for when the script exits
FAILED=0

# reset the firewire bus
# this should normalize all the plugs and reset n_p2p to 0
echo Resetting the Firewire bus
firewire_tester -R > $VERBOSE 2>&1

for NODE in $NODES;
do
  TRY=0
  SUCCESS=0  
  
  echo -n "Setting up node $NODE, ";
  while [ "$TRY" -lt "$ATTEMPTS" -a "$SUCCESS" -ne "1" ];do

    # tell stb to send data
    plugctl -n $NODE oPCR[0].n_p2p_connections=1

    # see if the stb is sending data
    firewire_tester -p -n $NODE -r $TESTS > $VERBOSE 2>&1
    if [ "$?" -eq "0" ];then
      SUCCESS=1
    else
      # stop sending data
      plugctl -n $NODE oPCR[0].n_p2p_connections=0
    fi
    let TRY+=1
  done

  if [ "$SUCCESS" -eq "1" ];then
     echo "SUCCESS (after $TRY attempts)"
  else
     echo "FAILED"
     FAILED=1
  fi

done

exit $FAILED