#!/bin/sh
#
#
#    Copyright (c) 1996-2001 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   srcTest
#    Module name: diag/scripts
#
#    This file performs a verification of the System Reference Clock
#    on Ulysses platforms
#
# NAME
#      srctest - system reference clock test
# 
# SYNOPSIS
#      srctest
# 
# AVAILABILITY
#      factory
# 
# DESCRIPTION
#      This  command will read the current state of the system ref-
#      erence clock, test the ability to reset,  set,  and  restore
#      the clock. The state of the CP board, and the blades in this
#      switch are monitored to ensure they correctly respond to the
#      state of the reference clock.
# 
#      The messages will go only to the console during normal oper-
#      ation. The error messages will go to  SEEPROM  under  burnin
#      mode.
# 
#      (WARNING)  Your  use  of  the  functionality  made available
#      through the root or factory account is at your sole risk and
#      you assume all liability resulting from such use.
# 

####################################
#
# Function to perform result comparison on a slot
#
# $1 = slot the check
# $2 = Condition to expect
# $3 = State of the system reference clock
#
####################################
do_a_slot()
{
	state=`getPLLStatus -slot $1 | cut -d " " -f4`
	match=FALSE
	if [ "$2" = "TRUE" ]
	then
		if [ "$state" = "SYNC" ]
		then
			match=TRUE
		fi
	else
		if [ "$state" != "SYNC" ]
		then
			match=TRUE
		fi
	fi
	if [ "$match" = "TRUE" ]
	then
		echo "srcTest: Blade $1 correctly went to $2 state when reference clock was $3."
	else
		forceerror --slot $1 -severity 2 -message "srcTest: Blade $1 failed. The PLL was $state when reference clock was $3."
		overall_status=1
	fi
}
####################################
#
# Function to perform result comparison on a slot
#
# $1 = Condition to expect
# $2 = State of the system reference clock
#
####################################
do_all_slots()
{
	export FABOS_SWITCHNO=0
	slots=`/fabos/sbin/exec_diag diagshow | \
			     /bin/grep Slot | \
			     /usr/bin/cut -d " " -f2 | \
			     /usr/bin/xargs /bin/echo`
	if [ ! -z "$slots" ] ;
	then
		for slot in $slots
		do
			do_a_slot $slot $1 $2
		done
	fi
	export FABOS_SWITCHNO=1
	slots=`/fabos/sbin/exec_diag diagshow | \
			     /bin/grep Slot | \
			     /usr/bin/cut -d " " -f2 | \
			     /usr/bin/xargs /bin/echo`
	if [ ! -z "$slots" ] ;
	then
		for slot in $slots
		do
			do_a_slot $slot $1 $2
		done
	fi

}
usage()
{
    echo "srcTest <-SKIP_ON_TEST> <-SKIP_OFF_TEST>"
    echo " Tests the slot 5 CP reference clock control on CP and blades."
    echo "   -SKIP_ON_TEST  Omits reference clock ON validation at blade."
    echo "   -SKIP_OFF_TEST Omits reference clock OFF validation at blade."
    exit 1
}
####################################
#
# MAIN section of script
#
####################################
if [ "`/sbin/sin | \
		  /bin/grep Platform | \
		  /usr/bin/cut -d' ' -f2 | \
		  /usr/bin/cut -d',' -f1`" != "SWBD10" ]
then
    echo "Not supported on this platform."
    exit -3
fi

do_clockon=TRUE
do_clockoff=TRUE

if [ $# -ge 2 ]
then
    case "$2" in
	"-SKIP_ON_TEST" ) do_clockon=FALSE;;
	"-SKIP_OFF_TEST" ) do_clockoff=FALSE;;
	* ) echo "Invalid parameter: $2";
	    usage
    esac
fi
if [ $# -ge 1 ]
then
    case "$1" in
	"-SKIP_ON_TEST" ) do_clockon=FALSE;;
	"-SKIP_OFF_TEST" ) do_clockoff=FALSE;;
	* ) echo "Invalid parameter: $1";
	    usage
    esac
fi

####################################
#
# Get the cp slot, and post to CP log
#
####################################
overall_status=0
cp_slot=`getCPSlot | /usr/bin/cut -d" " -f3`

if [ "$cp_slot" = 6 ]
then
	/bin/echo "srcTest: This command must be run from the CP in slot 5."
	/bin/echo "scrTest: testing SKIPPED"
	exit 16
fi

if [ "$cp_slot" = 0 ]
then
	/bin/echo "srcTest: This command can not be run on this system type."
	/bin/echo "scrTest: testing SKIPPED"
	exit 16
fi
original_switchno=$FABOS_SWITCHNO
####################################
#
# Save the original state of the clock
#
####################################
original_state=`referenceClock | cut -d " " -f4`
if [ ON = $original_state ]
then
	# expect SYNC
	original_condition=TRUE
else
	# expect not SYNC
	original_condition=FALSE
fi

####################################
#
# Test the ability to reset the clock
#
####################################
referenceClock OFF
/bin/sleep 1
state=`referenceClock  | cut -d " " -f4`
if [ "$state" = "OFF" ]
then
	/bin/echo "srcTest: CP reference clock correctly went to $state."
else
	forceerror --slot $cp_slot -severity 2 -message "srcTest: CP failed to turn OFF the reference clock."
	overall_status=1
fi

if [ "$do_clockoff" = "TRUE" ]
then
    # expect not SYNC
    condition=FALSE

    # Test the blade reaction
    do_all_slots $condition OFF
else
    echo "Skipping the reference clock OFF validation at the blades"
fi

####################################
#
# Test the ability to set the clock
#
####################################
referenceClock ON
/bin/sleep 1
state=`referenceClock  | cut -d " " -f4`
if [ "$state" = "ON" ]
then
	/bin/echo "srcTest: CP reference clock correctly went to $state."
else
	forceerror --slot $cp_slot -severity 2 -message "srcTest: CP failed to turn ON the reference clock."
	overall_status=1
fi

if [ "$do_clockon" = "TRUE" ]
then
    # expect SYNC
    condition=TRUE

    # Test the blade reaction
    do_all_slots $condition ON
else
    echo "Skipping the reference clock ON validation at the blades"
fi

####################################
#
# Restore the original state of the clock if it is not ON
#
####################################
if [ $original_state != ON ]
then
	referenceClock $original_state
	/bin/sleep 1
	state=`referenceClock  | cut -d " " -f4`
	if [ "$state" = $original_state ]
	then
		/bin/echo CP correctly restored to original state
	else
		forceerror --slot $cp_slot -severity 2 -message "srcTest: CP failed to return to $origianl_state of the reference clock."
		overall_status=1
	fi
	case $original_state in
	"ON" ) if [ "$do_clockon" = "TRUE" ]
	       then
		    do_all_slots $original_condition $original_state
	       fi;;
	"OFF" ) if [ "$do_clockoff" = "TRUE" ]
	       then
		    do_all_slots $original_condition $original_state
	       fi;;
	esac
fi
export FABOS_SWITCHNO=$original_switchno
if [ $overall_status -eq 0 ]
then
    /bin/echo "srcTest: PASSED without errors"
else
    /bin/echo "srcTest: FAILED !!!."
fi
exit $overall_status
