#!/usr/bin/env bash ############################################################################### # # Copyright (C) 2009-2013 Nintendo. All rights reserved. # # These coded instructions, statements, and computer programs contain # proprietary information of Nintendo of America Inc. and/or Nintendo # Company Ltd., and are protected by Federal copyright law. They may # not be disclosed to third parties or copied or duplicated in any form, # in whole or in part, without the prior written consent of Nintendo. # ############################################################################### ################################################################################ # cafe_utils # Common definitions/functions for cafe scripts. ################################################################################ source monitor_return_codes COMPORTS_SUPPORT="" Monitor_For_Output_calls=0 # count of calls to Monitor_For_Output MONITOR_DEFAULT_TIMEOUT=60 # passed to monitor.exe ################################################################################ # Bash_sourced_check # Checks to see if a script needs to be executed as "source SCRIPT". # Format: Bash_sourced_check bash_is_ok script_name source_name # bash_is_ok: 1=ok to run this script using "source SCRIPT", 0=not ok. # script_name: name of this script to be tested (usually "basename $0"). # source_name: for error message if script needs to be sourced. # Returns: 0 if script did not need to be sourced, non-0 otherwise. ################################################################################ Bash_sourced_check() { local rval=0 local bash_is_ok=$1 local script_name=$2 local source_name=$3 if [ -z "$SCRIPT_IS_SOURCED" ]; then # script is not yet determined to be sourced case "$script_name" in "bash") # script was run as "source script"... if [ $bash_is_ok != 1 ]; then rval=1 # sorry... not allowed fi ;; "cafe_nand2pcfs") rval=1 #cannot be run directly, must be sourced ;; "cafe_pcfs2nand") rval=1 #cannot be run directly, must be sourced ;; "setbootmode") rval=1 #cannot be run directly, must be sourced ;; *) # All other scripts are ok ;; esac if [ $rval == 0 ]; then export SCRIPT_IS_SOURCED=1 fi fi return $rval } ################################################################################ # Cleaning_Child_Processes ################################################################################ Cleaning_Child_Processes() { if [ -f "$CAFE_ROOT/${SESSION_PATH_PREFIX}autotest/monitor_cpid.txt" ] ; then kill -KILL `cat "$CAFE_ROOT/${SESSION_PATH_PREFIX}autotest/monitor_cpid.txt"` >& /dev/null rm -f "$CAFE_ROOT/${SESSION_PATH_PREFIX}autotest/monitor_cpid.txt" >& /dev/null fi } ################################################################################ # Initialize_serial_port_and_directory ################################################################################ Initialize_serial_port_and_directory() { local ret local stop_cmd=$1 if [ $# -lt 1 ]; then echo \"Usage: Initialize_serial_port_and_directory stop_cmd\", where stop_cmd is cafestop or a bash function call. exit 1 fi if [ ! -d "$LOGDIR" ]; then mkdir -p "$LOGDIR" fi $stop_cmd trap "Cleaning_Child_Processes" EXIT if [ $CAFE_CONSOLE = "cattoucan" ] then trap "Cleaning_Child_Processes; CAFE_CONSOLE=cattoucan" EXIT CAFE_CONSOLE=toucan fi if [ $CAFE_CONSOLE = "cmdtoucan" ] then trap "Cleaning_Child_Processes; CAFE_CONSOLE=cattoucan" EXIT CAFE_CONSOLE=toucan fi if [ $CAFE_CONSOLE = "catwaikiki" ] then trap "Cleaning_Child_Processes; CAFE_CONSOLE=catwaikiki" EXIT CAFE_CONSOLE=waikiki fi echo CAFE_CONSOLE=$CAFE_CONSOLE if [ "$COMPORTS_SUPPORT" = "-c" ]; then # initialize the serial port speed before monitor.exe (workaround). echo local init_flag=0 INITIALIZED_COMPORTS= for COMPORT in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 do mode.com com$COMPORT: BAUD=115200 PARITY=N DATA=8 STOP=1 to=off xon=off odsr=off octs=on dtr=off rts=on idsr=off &> /dev/null ret=$? if [ $ret = 0 ]; then echo Initialized COM$COMPORT init_flag=1 INITIALIZED_COMPORTS=$INITIALIZED_COMPORTS"COM$COMPORT " fi done echo if [ $init_flag = 0 -a "$CAFE_CONSOLE" != "toucan" ]; then echo "ERROR: no serial port is available." echo "Please close other softwares using serial port," echo "such as HyperTerminal." echo echo "This automated test script cannot share the serial port" echo "with other softwares." exit 1; fi fi } ################################################################################ # Evaluate_Monitor_Return_Value # Evaluates the return value of monitor.exe. Outputs a pass/fail string, # and sets return code to 0 for pass, 1 for fail. # # Format: Evaluate_Monitor_Return_Value ret_val # ret_val: monitor.exe return value ################################################################################ Evaluate_Monitor_Return_Value() { local mon_rval=$1 local return_val=1 # exit code in case monitor returns error local status_str if [ "$1" == "" ]; then echo Usage: \"Evaluate_Monitor_Return_Value ret_code\", where ret_code is monitor.exe return code >/dev/stderr exit 1 fi case $mon_rval in $MON_RESULT_PASS) return_val=0 ;; $MON_RESULT_TIMEOUT) status_str="TIMEOUT" ;; $MON_RESULT_UNDEF) status_str="UNDEF" ;; $MON_RESULT_MANUAL) status_str="MANUAL" ;; $MON_RESULT_NOLOG) status_str="NOLOG" ;; $MON_RESULT_FAIL) status_str="FAIL" ;; $MON_RESULT_EXPECTED_TERM) status_str="EXPECTED_TERM" ;; $MON_RESULT_EXEC_WIN_APP) status_str="EXEC_WIN_APP" ;; $MON_RESULT_NOPPC) status_str="NOPPC" ;; $MON_RESULT_BOOT1) status_str="BOOT1" ;; $MON_RESULT_RESTART) status_str="RESTART" ;; $MON_RESULT_TEST_AS_RPL) status_str="TEST_AS_RPL" ;; $MON_RESULT_END_OF_RPL_LOADER) status_str="END_OF_RPL_LOADER" ;; $MON_RESULT_BRIDGE_COLLISION) status_str="BRIDGE_COLLISION" ;; $MON_RESULT_TEST_DBG_OUT) status_str="TEST_DBG_OUT" ;; $RESULT_BRIDGE_OFF) status_str="RESULT_BRIDGE_OFF" ;; $MON_RESULT_ERROR) status_str="ERROR" ;; $MON_RESULT_FATAL_ERROR) status_str="FATAL_ERROR" ;; $MON_RESULT_MISSING) status_str="MISSING" ;; $MON_RESULT_ABORTED) status_str="ABORTED" ;; *) status_str="$mon_rval" ;; # All other error codes from monitor. esac if [ $return_val -ne 0 ]; then echo monitor returned $status_str 1>&2 return $return_val fi } ################################################################################ # Monitor_For_Output # After a cafe command is issued, this function is called to invoke monitor.exe # to search for a string indicating that the command succeeded. # # Format: Monitor_For_Output test_str timeout log_dir [file_name] # test_str: string to search for, indicating success # timeout: time, in seconds, to wait between strings sent from the command # log_dir: directory for output log file # file_name: optional output file to write to ################################################################################ Monitor_For_Output() { local str_opt=$1 local test_str=$2 local timeout=$3 local log_dir=$4 local file_name=$5 local mon_rc_file # Store monitor return code to this file, because "tee" overrides it. local call_no local return_val # argument check if [ $# -lt 4 ]; then echo "`basename $0`: Monitor_For_Output args: str_opt test_str timeout log_dir [file_name]" >/dev/stderr exit 1 fi # If log directory does not exist, create it. if [ ! -d "$log_dir" ]; then mkdir -p "$log_dir" fi # Keep a seperate status log for each call to this function. Monitor_For_Output_calls=`expr $Monitor_For_Output_calls + 1` call_no=$Monitor_For_Output_calls mon_rc_file="$log_dir/monitor_return_code_$call_no.txt" # If a file name is not passed in, generate a file name. if [ "$file_name" == "" ]; then file_name="monitor_$call_no.txt" fi (monitor.exe $str_opt "$test_str" -t $timeout -p $SESSION_DEBUG_OUT_PORT; echo -n $? >"$mon_rc_file") | tee -a "$log_dir"/$file_name mon_rval=`cat "$mon_rc_file"` Evaluate_Monitor_Return_Value $mon_rval return_val=$? if [ $return_val -eq 0 ]; then echo monitor returned PASS else return $return_val fi }