#!/bin/bash

#########################################
# Name: snltest
#
# Summary: run sensor node loopback test
#
# Description: cycle sensor node power
#   and run loopback test on snports
#
# Author: k. headley
#
# Copyright 2015 MBARI
#
#########################################

#########################################
# Script configuration defaults
# casual users should not need to change
# anything below this section
#########################################
description="[run sensor node loopback test]"

PERIOD_SEC=30
SNPORT=0
DFL_NCYCLES=-1
DFL_PORTS="-p 0,38400 -p 1,38400 -p 2,38400"
LOOP_CFG="-l 10,64,100,500"
TTY=/dev/ttyO1
GWSN_ON="-1 0,1,2"
GWSN_OFF="-0 0,1,2"
# for a previous version of gwsn
#GWSN_ON="-n 0,1,2"
#GWSN_OFF="-f 0,1,2"

unset NODE
unset PORTS
unset NCYCLES

#################################
# Script variable initialization
#################################
VERBOSE="FALSE"

#################################
# Function Definitions
#################################

#################################
# name: printUsage
# description: print use message
# args: none
#################################
printUsage(){
    echo
    echo "Description: $description"
    echo
    echo "usage: `basename $0` [options]"
    echo " Options:"
    echo "  -n <cycles> : n cycles             [$DFL_NCYCLES]"
    echo "  -c <cycle>  : cycle time (sec)     [$PERIOD_SEC]"
    echo "  -t <device> : snode host port      [$TTY]"
    echo "  -s <snode>  : sensor node port 0:3 [$SNPORT]"
    echo "  -l <n,b,wr,c,aen,ben>: loop config [$LOOP_CFG]"
    echo "  -p <port,baud> : add port(s)       [$DFL_PORTS]"
    echo "  -v             : verbose output    [$VERBOSE]"
    echo "  -h             : print use message"
    echo ""
    echo
}

########################################
# name: vout
# description: print verbose message to stderr
# args:
#     msg: message
########################################
vout(){
    if [ "$VERBOSE" == "TRUE" ]
    then
        echo "$1" >&2
    fi
}

########################################
# name: exitError
# description: print use message to stderr
# args:
#     msg:        error message
#     returnCode: exit status to return
########################################
exitError(){
    echo >&2
    echo "`basename $0`: error - $1" >&2
    echo >&2
    exit $2
}

########################################
# name: processCmdLine
# description: do command line processsing
# args:
#     args:       positional paramters
#     returnCode: none
########################################
processCmdLine(){
    OPTIND=1
    vout "`basename $0` all args[$*]"

    while getopts c:l:n:p:s:t:hv Option
    do
        #vout "processing $Option[$OPTARG]"
        case $Option in
            c ) PERIOD_SEC=$OPTARG
            ;;
            n ) NCYCLES=$OPTARG
            ;;
            t ) TTY=$OPTARG
            ;;
            s ) SNPORT="$OPTARG"
            ;;
            l ) LOOP_CFG="-l $OPTARG"
            ;;
            p ) PORTS="$PORTS -p $OPTARG"
            ;;
            v ) VERBOSE="TRUE"
            ;;
            h) printUsage
            exit 0
            ;;
            *) exit 0 # getopts outputs error message
            ;;
        esac
    done
}

##########################
# Script main entry point
##########################

# Argument processing
# Accepts arguments from command line

processCmdLine $*
let "i=$OPTIND-1"
#while [ "${i}" -gt 0  ]
#do
#	shift
#	let "i-=1"
#done
#fi

# finish variable initialization/validation

NODE="node$SNPORT"

if [ ! "$PORTS" ]
then
    PORTS=$DFL_PORTS
fi

if [ ! "$NCYCLES" ]
then
    NCYCLES=$DFL_NCYCLES
fi

cycle=1
let "loop_end=$NCYCLES+1"

vout "PERIOD_SEC [$PERIOD_SEC]"
vout "NCYCLES    [$NCYCLES]"
vout "NODE       [$NODE]"
vout "TTY        [$TTY]"
vout "PORTS      [$PORTS]"
vout "LOOP_CFG   [$LOOP_CFG]"
vout "VERBOSE    [$VERBOSE]"

while [ "${cycle}" -ne "$loop_end" ]
do

    echo "###### `date` [$cycle/$NCYCLES]######"
    # snode port ON
    gwdio -s $NODE:1 &> /dev/null
    sleep 1

    # snport power ON
    gwsn -s 0,$TTY $GWSN_ON -b 2
    sleep 1

    # run loopback test
    snloop -s $TTY  $PORTS $LOOP_CFG &> /dev/null
    test=$?
    echo "snloop returned [$test]"

    # snport power OFF
    gwsn -s 0,$TTY $GWSN_OFF  -b 0
    sleep 1

    # snode port OFF
    gwdio -s $NODE:0 &> /dev/null
    let "cycle=$cycle+1"

    # delay until next cycle
    sleep $PERIOD_SEC

done
