1#!/usr/bin/env bash
2###############################################################################
3#
4# Copyright (C) 2009-2013 Nintendo.  All rights reserved.
5#
6# These coded instructions, statements, and computer programs contain
7# proprietary information of Nintendo of America Inc. and/or Nintendo
8# Company Ltd., and are protected by Federal copyright law.  They may
9# not be disclosed to third parties or copied or duplicated in any form,
10# in whole or in part, without the prior written consent of Nintendo.
11#
12###############################################################################
13
14################################################################################
15# cafe_utils
16# Common definitions/functions for cafe scripts.
17################################################################################
18
19source monitor_return_codes
20
21COMPORTS_SUPPORT=""
22Monitor_For_Output_calls=0 # count of calls to Monitor_For_Output
23MONITOR_DEFAULT_TIMEOUT=60 # passed to monitor.exe
24
25################################################################################
26# Bash_sourced_check
27# Checks to see if a script needs to be executed as "source SCRIPT".
28# Format: Bash_sourced_check bash_is_ok script_name source_name
29#   bash_is_ok: 1=ok to run this script using "source SCRIPT", 0=not ok.
30#   script_name: name of this script to be tested (usually "basename $0").
31#   source_name: for error message if script needs to be sourced.
32# Returns: 0 if script did not need to be sourced, non-0 otherwise.
33################################################################################
34Bash_sourced_check()
35{
36    local rval=0
37    local bash_is_ok=$1
38    local script_name=$2
39    local source_name=$3
40
41    if [ -z "$SCRIPT_IS_SOURCED" ]; then
42        # script is not yet determined to be sourced
43        case "$script_name" in
44            "bash") # script was run as "source script"...
45                if [ $bash_is_ok != 1 ]; then
46                    rval=1  # sorry... not allowed
47                fi
48                ;;
49            "cafe_nand2pcfs")
50                rval=1 #cannot be run directly, must be sourced
51                ;;
52            "cafe_pcfs2nand")
53                rval=1 #cannot be run directly, must be sourced
54                ;;
55            "setbootmode")
56                rval=1 #cannot be run directly, must be sourced
57                ;;
58            *)  # All other scripts are ok
59                ;;
60        esac
61        if [ $rval == 0 ]; then
62            export SCRIPT_IS_SOURCED=1
63        fi
64    fi
65
66    return $rval
67}
68
69
70################################################################################
71# Cleaning_Child_Processes
72################################################################################
73Cleaning_Child_Processes()
74{
75    if [ -f "$CAFE_ROOT/${SESSION_PATH_PREFIX}autotest/monitor_cpid.txt" ] ; then
76        kill -KILL `cat "$CAFE_ROOT/${SESSION_PATH_PREFIX}autotest/monitor_cpid.txt"` >& /dev/null
77        rm -f "$CAFE_ROOT/${SESSION_PATH_PREFIX}autotest/monitor_cpid.txt" >& /dev/null
78    fi
79}
80
81################################################################################
82# Initialize_serial_port_and_directory
83################################################################################
84Initialize_serial_port_and_directory()
85{
86    local ret
87    local stop_cmd=$1
88
89    if [ $# -lt 1 ]; then
90        echo \"Usage: Initialize_serial_port_and_directory stop_cmd\", where stop_cmd is cafestop or a bash function call.
91        exit 1
92    fi
93
94    if [ ! -d "$LOGDIR" ]; then
95        mkdir -p "$LOGDIR"
96    fi
97
98    $stop_cmd
99
100    trap "Cleaning_Child_Processes" EXIT
101
102    if [ $CAFE_CONSOLE = "cattoucan" ]
103    then
104        trap "Cleaning_Child_Processes; CAFE_CONSOLE=cattoucan" EXIT
105        CAFE_CONSOLE=toucan
106    fi
107
108    if [ $CAFE_CONSOLE = "cmdtoucan" ]
109    then
110        trap "Cleaning_Child_Processes; CAFE_CONSOLE=cattoucan" EXIT
111        CAFE_CONSOLE=toucan
112    fi
113
114    if [ $CAFE_CONSOLE = "catwaikiki" ]
115    then
116        trap "Cleaning_Child_Processes; CAFE_CONSOLE=catwaikiki" EXIT
117        CAFE_CONSOLE=waikiki
118    fi
119
120    echo CAFE_CONSOLE=$CAFE_CONSOLE
121
122    if [ "$COMPORTS_SUPPORT" = "-c" ]; then
123        # initialize the serial port speed before monitor.exe (workaround).
124        echo
125        local init_flag=0
126        INITIALIZED_COMPORTS=
127        for COMPORT in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
128        do
129            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
130            ret=$?
131            if [ $ret = 0 ]; then
132                echo Initialized COM$COMPORT
133                init_flag=1
134                INITIALIZED_COMPORTS=$INITIALIZED_COMPORTS"COM$COMPORT "
135            fi
136        done
137        echo
138
139        if [ $init_flag = 0 -a "$CAFE_CONSOLE" != "toucan" ]; then
140            echo "ERROR: no serial port is available."
141            echo "Please close other softwares using serial port,"
142            echo "such as HyperTerminal."
143            echo
144            echo "This automated test script cannot share the serial port"
145            echo "with other softwares."
146            exit 1;
147        fi
148    fi
149}
150
151################################################################################
152# Evaluate_Monitor_Return_Value
153# Evaluates the return value of monitor.exe. Outputs a pass/fail string,
154# and sets return code to 0 for pass, 1 for fail.
155#
156# Format: Evaluate_Monitor_Return_Value ret_val
157#   ret_val: monitor.exe return value
158################################################################################
159Evaluate_Monitor_Return_Value()
160{
161    local mon_rval=$1
162    local return_val=1    # exit code in case monitor returns error
163    local status_str
164
165    if [ "$1" == "" ]; then
166        echo Usage: \"Evaluate_Monitor_Return_Value ret_code\", where ret_code is monitor.exe return code >/dev/stderr
167        exit 1
168    fi
169
170    case $mon_rval in
171        $MON_RESULT_PASS)
172            return_val=0
173            ;;
174        $MON_RESULT_TIMEOUT)            status_str="TIMEOUT" ;;
175        $MON_RESULT_UNDEF)              status_str="UNDEF" ;;
176        $MON_RESULT_MANUAL)             status_str="MANUAL" ;;
177        $MON_RESULT_NOLOG)              status_str="NOLOG" ;;
178        $MON_RESULT_FAIL)               status_str="FAIL" ;;
179        $MON_RESULT_EXPECTED_TERM)      status_str="EXPECTED_TERM" ;;
180        $MON_RESULT_EXEC_WIN_APP)       status_str="EXEC_WIN_APP" ;;
181        $MON_RESULT_NOPPC)              status_str="NOPPC" ;;
182        $MON_RESULT_BOOT1)              status_str="BOOT1" ;;
183        $MON_RESULT_RESTART)            status_str="RESTART" ;;
184        $MON_RESULT_TEST_AS_RPL)        status_str="TEST_AS_RPL" ;;
185        $MON_RESULT_END_OF_RPL_LOADER)  status_str="END_OF_RPL_LOADER" ;;
186        $MON_RESULT_BRIDGE_COLLISION)   status_str="BRIDGE_COLLISION" ;;
187        $MON_RESULT_TEST_DBG_OUT)       status_str="TEST_DBG_OUT" ;;
188        $RESULT_BRIDGE_OFF)             status_str="RESULT_BRIDGE_OFF" ;;
189        $MON_RESULT_ERROR)              status_str="ERROR" ;;
190        $MON_RESULT_FATAL_ERROR)        status_str="FATAL_ERROR" ;;
191        $MON_RESULT_MISSING)            status_str="MISSING" ;;
192        $MON_RESULT_ABORTED)            status_str="ABORTED" ;;
193        *)  status_str="$mon_rval" ;; # All other error codes from monitor.
194    esac
195
196    if [ $return_val -ne 0 ]; then
197        echo monitor returned $status_str 1>&2
198        return $return_val
199    fi
200}
201
202################################################################################
203# Monitor_For_Output
204# After a cafe command is issued, this function is called to invoke monitor.exe
205# to search for a string indicating that the command succeeded.
206#
207# Format: Monitor_For_Output test_str timeout log_dir [file_name]
208#   test_str: string to search for, indicating success
209#   timeout: time, in seconds, to wait between strings sent from the command
210#   log_dir: directory for output log file
211#   file_name: optional output file to write to
212################################################################################
213Monitor_For_Output()
214{
215    local str_opt=$1
216    local test_str=$2
217    local timeout=$3
218    local log_dir=$4
219    local file_name=$5
220    local mon_rc_file   # Store monitor return code to this file, because "tee" overrides it.
221    local call_no
222    local return_val
223
224    # argument check
225    if [ $# -lt 4 ]; then
226        echo "`basename $0`: Monitor_For_Output args: str_opt test_str timeout log_dir [file_name]" >/dev/stderr
227        exit 1
228    fi
229
230    # If log directory does not exist, create it.
231    if [ ! -d "$log_dir" ]; then
232        mkdir -p "$log_dir"
233    fi
234
235    # Keep a seperate status log for each call to this function.
236    Monitor_For_Output_calls=`expr $Monitor_For_Output_calls + 1`
237    call_no=$Monitor_For_Output_calls
238    mon_rc_file="$log_dir/monitor_return_code_$call_no.txt"
239
240    # If a file name is not passed in, generate a file name.
241    if [ "$file_name" == "" ]; then
242        file_name="monitor_$call_no.txt"
243    fi
244
245    (monitor.exe $str_opt "$test_str" -t $timeout -p $SESSION_DEBUG_OUT_PORT; echo -n $? >"$mon_rc_file") | tee -a "$log_dir"/$file_name
246    mon_rval=`cat "$mon_rc_file"`
247    Evaluate_Monitor_Return_Value $mon_rval
248    return_val=$?
249    if [ $return_val -eq 0 ]; then
250        echo monitor returned PASS
251    else
252        return $return_val
253    fi
254}
255
256