#!/usr/bin/perl -w

#
# Regression SW Test script for AUV Gulper
# 
# NOTES: 
#        - If this script is run on a Windows machine under Cygwin, remember that
#          serial port 1 (according to windows) will actually be /dev/ttyS0 as far
#          as Perl knows. Subtract 1 for use on windows (e.g. com8 = /dev/ttyS7)  
#  
#        - This script is intedned to be run in such a way that its output is captured
#          in a log (e.g. | tee, > logfile, etc). The script itself does not create
#          its own log file in an effort to provide flexibility to the end user. 
#
# 08/15/07 - Initial creation          B. Kieft
#
#
#
#

# Prompt the user for the serial port number 
$PortNum = &promptUser("Enter the serial port number to which the UUT is attached (0 1 2 3 4 5 6 7 8 9)", 0);

# Serial port being used 
$SP = "/dev/ttyS$PortNum";

printf("Using serial port $SP \n");
printf("9600 Bps, 8-n-1 \n");

# This script requires the use of a serial package from CPAN
# This package is available in the project's test folder. Follow 
# the instructions in the README file in the folder named
# Device-SerialPort-1.002. 
use Device::SerialPort 0.12; 


# Setup serial port properties
# Writes to $ob are followed by a sleep for flushing
#
$ob = Device::SerialPort->new ($SP) || die "Can't Open $SP: $!";
$ob->baudrate(9600)    || die "failed setting baudrate";
$ob->parity("none")    || die "failed setting parity";
$ob->databits(8)       || die "failed setting databits";
$ob->handshake("none") || die "failed setting handshake";
$ob->write_settings    || die "no settings";
##

## Set up variables for test
local $testNum = "";
local $expectedResult = "";
local $chars=0;
local $buffer="";




##########################################################
#### REG0000
#### Version Command
$testNum = "REG0000";
$testDesc = "Version command testing";
printf("\n********************\n");
printf("       $testNum         ");
printf("\n$testDesc\n");
printf("\n********************\n");

my $nodeAddr = 0; # Set node address to start with
my $STALL_DEFAULT=10; # how many seconds to wait for new input
my $timeout=$STALL_DEFAULT;
my $sw_Version = "0.7";

# Power up
&promptUser("\n\n*** Verify all nodes cocked and powered on. Press enter to continue ***\n\n",0);

# Loop through all nodes
while ($nodeAddr <= 9) {
	
  ##	
	$expectedResult = "*" . dec2hex($nodeAddr) . "$sw_Version\r\n" ; # node address and version
 	$input_1 = "\$" . dec2hex($nodeAddr) . "1VFF"; # Version command
  ## 
  
  printf("\n\n*** Testing controller: " . dec2hex($nodeAddr) . " ***\n\n"); 
 
  # Re-initialize bus polling
  $ob->read(5); # Flush input buffer NOTE! This is only done due to the extra characters from power on in this test case only!

  $ob->read_char_time(0);     # don't wait for each character
  $ob->read_const_time(1000); # 1 second per unfulfilled "read" call
  $chars=0;
  $buffer="";
  
  # Send Inputs 
  $ob->write($input_1); # send input_1
# $ob->write($input_1); # send input_2
# $ob->write($input_1); # send input_3    


  # Poll for expected results
  $timeout=$STALL_DEFAULT;
  while ($timeout>0) {
      ($count,$saw)=$ob->read(25); # will read _up to_ 25 chars
      if ($count > 0) {
        $chars+=$count;
        $buffer.=$saw;
        last if($buffer eq $expectedResult)
               
        # Check here to see if what we want is in the $buffer
        # say "last" if we find it
        }
      else {
        $timeout--;
        }
  }
 
  $actualResult = $buffer;
 
### And the results are..... 
  printf("\n***************************************************************************\n");
  printf("                       RESULTS of $testNum for " . dec2hex($nodeAddr) . "      ");
  printf("\n$testDesc\n");
  printf("\n***************************************************************************\n");
  if ($timeout==0) {
    printf("\nTest " . $testNum . " FAIL\nExpected:" . $expectedResult . "\nActual:" . $buffer . "\nWaited $STALL_DEFAULT seconds and never received expected results.\n");
  }
  else {
    printf("\nTest " . $testNum . " PASS\nExpected:" . $expectedResult . "\n" . "Actual:" . $actualResult . "\n");
  }
  printf("**************************************************************************\n");
  printf("**************************************************************************\n\n\n");
  
	
	$nodeAddr += 1;
	sleep 1; # delay between calls
	
} # end of while loop
#### End of REG0000 
###



##########################################################
#### REG0005
#### Query Command
$testNum = "REG0005";
$testDesc = "Query command testing prior to firing";
printf("\n********************\n");
printf("       $testNum         ");
printf("\n$testDesc\n");
printf("\n********************\n");

$nodeAddr = 0; # Set node address to start with


# Loop through all nodes
while ($nodeAddr <= 9) {
	
  ##	
	$expectedResult = "*" . dec2hex($nodeAddr) . "0\r\n" ; # node address and 0 - response to query
 	$input_1 = "\$" . dec2hex($nodeAddr) . "1QFF"; # Query command
  ## 
  
  printf("\n\n*** Testing controller: " . dec2hex($nodeAddr) . " ***\n\n"); 
 
  $ob->read_char_time(0);     # don't wait for each character
  $ob->read_const_time(1000); # 1 second per unfulfilled "read" call
  $chars=0;
  $buffer="";
  
  # Send Inputs 
  $ob->write($input_1); # send input_1
# $ob->write($input_1); # send input_2
# $ob->write($input_1); # send input_3    


  # Poll for expected results
  $timeout=$STALL_DEFAULT;
  while ($timeout>0) {
      ($count,$saw)=$ob->read(25); # will read _up to_ 25 chars
      if ($count > 0) {
        $chars+=$count;
        $buffer.=$saw;
        last if($buffer eq $expectedResult)
               
        # Check here to see if what we want is in the $buffer
        # say "last" if we find it
        }
      else {
        $timeout--;
        }
  }
 
  $actualResult = $buffer;
 
### And the results are..... 
  printf("\n***************************************************************************\n");
  printf("                       RESULTS of $testNum for " . dec2hex($nodeAddr) . "      ");
  printf("\n$testDesc\n");
  printf("\n***************************************************************************\n");
  if ($timeout==0) {
    printf("\nTest " . $testNum . " FAIL\nExpected:" . $expectedResult . "\nActual:" . $buffer . "\nWaited $STALL_DEFAULT seconds and never received expected results.\n");
  }
  else {
    printf("\nTest " . $testNum . " PASS\nExpected:" . $expectedResult . "\n" . "Actual:" . $actualResult . "\n");
  }
  printf("**************************************************************************\n");
  printf("**************************************************************************\n\n\n");
  
	
	$nodeAddr += 1;
	sleep 1; # delay between calls
	
} # end of while loop
#### End of REG0005 
###


##########################################################
#### REG0010
#### Status Command
$testNum = "REG0010";
$testDesc = "Status command testing prior to firing";
printf("\n********************\n");
printf("       $testNum         ");
printf("\n$testDesc\n");
printf("\n********************\n");

$nodeAddr = 0; # Set node address to start with


# Loop through all nodes
while ($nodeAddr <= 9) {
	
  ##	
	$expectedResult = "*" . dec2hex($nodeAddr) . "00\r\n" ;
 	$input_1 = "\$" . dec2hex($nodeAddr) . "1SFF"; 
  ## 
  
  printf("\n\n*** Testing controller: " . dec2hex($nodeAddr) . " ***\n\n"); 
 
  $ob->read_char_time(0);     # don't wait for each character
  $ob->read_const_time(1000); # 1 second per unfulfilled "read" call
  $chars=0;
  $buffer="";
  
  # Send Inputs 
  $ob->write($input_1); # send input_1
# $ob->write($input_1); # send input_2
# $ob->write($input_1); # send input_3    


  # Poll for expected results
  $timeout=$STALL_DEFAULT;
  while ($timeout>0) {
      ($count,$saw)=$ob->read(25); # will read _up to_ 25 chars
      if ($count > 0) {
        $chars+=$count;
        $buffer.=$saw;
        last if($buffer eq $expectedResult)
               
        # Check here to see if what we want is in the $buffer
        # say "last" if we find it
        }
      else {
        $timeout--;
        }
  }
 
  $actualResult = $buffer;
 
### And the results are..... 
  printf("\n***************************************************************************\n");
  printf("                       RESULTS of $testNum for " . dec2hex($nodeAddr) . "      ");
  printf("\n$testDesc\n");
  printf("\n***************************************************************************\n");
  if ($timeout==0) {
    printf("\nTest " . $testNum . " FAIL\nExpected:" . $expectedResult . "\nActual:" . $buffer . "\nWaited $STALL_DEFAULT seconds and never received expected results.\n");
  }
  else {
    printf("\nTest " . $testNum . " PASS\nExpected:" . $expectedResult . "\n" . "Actual:" . $actualResult . "\n");
  }
  printf("**************************************************************************\n");
  printf("**************************************************************************\n\n\n");
  
	
	$nodeAddr += 1;
	sleep 1; # delay between calls
	
} # end of while loop
#### End of REG0010 
###




##########################################################
#### REG0020
#### Activation Command
$testNum = "REG0020";
$testDesc = "Activation time command testing prior to firing";
printf("\n********************\n");
printf("       $testNum         ");
printf("\n$testDesc\n");
printf("\n********************\n");

$nodeAddr = 0; # Set node address to start with


# Loop through all nodes
while ($nodeAddr <= 9) {
	
  ##	
	$expectedResult = "*" . dec2hex($nodeAddr) . "0\r\n" ;
 	$input_1 = "\$" . dec2hex($nodeAddr) . "1AFF"; 
  ## 
  
  printf("\n\n*** Testing controller: " . dec2hex($nodeAddr) . " ***\n\n"); 
 
  $ob->read_char_time(0);     # don't wait for each character
  $ob->read_const_time(1000); # 1 second per unfulfilled "read" call
  $chars=0;
  $buffer="";
  
  # Send Inputs 
  $ob->write($input_1); # send input_1
# $ob->write($input_1); # send input_2
# $ob->write($input_1); # send input_3    


  # Poll for expected results
  $timeout=$STALL_DEFAULT;
  while ($timeout>0) {
      ($count,$saw)=$ob->read(25); # will read _up to_ 25 chars
      if ($count > 0) {
        $chars+=$count;
        $buffer.=$saw;
        last if($buffer eq $expectedResult)
               
        # Check here to see if what we want is in the $buffer
        # say "last" if we find it
        }
      else {
        $timeout--;
        }
  }
 
  $actualResult = $buffer;
 
### And the results are..... 
  printf("\n***************************************************************************\n");
  printf("                       RESULTS of $testNum for " . dec2hex($nodeAddr) . "      ");
  printf("\n$testDesc\n");
  printf("\n***************************************************************************\n");
  if ($timeout==0) {
    printf("\nTest " . $testNum . " FAIL\nExpected:" . $expectedResult . "\nActual:" . $buffer . "\nWaited $STALL_DEFAULT seconds and never received expected results.\n");
  }
  else {
    printf("\nTest " . $testNum . " PASS\nExpected:" . $expectedResult . "\n" . "Actual:" . $actualResult . "\n");
  }
  printf("**************************************************************************\n");
  printf("**************************************************************************\n\n\n");
  
	
	$nodeAddr += 1;
	sleep 1; # delay between calls
	
} # end of while loop
#### End of REG0020 
###


##########################################################
#### REG0030
#### Bad Command
$testNum = "REG0030";
$testDesc = "Illegal command testing";
printf("\n********************\n");
printf("       $testNum         ");
printf("\n$testDesc\n");
printf("\n********************\n");

$nodeAddr = 0; # Set node address to start with


# Loop through all nodes
while ($nodeAddr <= 9) {
	
  ##	
	$expectedResult = "?" . dec2hex($nodeAddr) . " COMMAND ERROR\r\n" ;
 	$input_1 = "\$" . dec2hex($nodeAddr) . "1BFF"; 
  ## 
  
  printf("\n\n*** Testing controller: " . dec2hex($nodeAddr) . " ***\n\n"); 
 
  $ob->read_char_time(0);     # don't wait for each character
  $ob->read_const_time(1000); # 1 second per unfulfilled "read" call
  $chars=0;
  $buffer="";
  
  # Send Inputs 
  $ob->write($input_1); # send input_1
# $ob->write($input_1); # send input_2
# $ob->write($input_1); # send input_3    


  # Poll for expected results
  $timeout=$STALL_DEFAULT;
  while ($timeout>0) {
      ($count,$saw)=$ob->read(25); # will read _up to_ 25 chars
      if ($count > 0) {
        $chars+=$count;
        $buffer.=$saw;
        last if($buffer eq $expectedResult)
               
        # Check here to see if what we want is in the $buffer
        # say "last" if we find it
        }
      else {
        $timeout--;
        }
  }
 
  $actualResult = $buffer;
 
### And the results are..... 
  printf("\n***************************************************************************\n");
  printf("                       RESULTS of $testNum for " . dec2hex($nodeAddr) . "      ");
  printf("\n$testDesc\n");
  printf("\n***************************************************************************\n");
  if ($timeout==0) {
    printf("\nTest " . $testNum . " FAIL\nExpected:" . $expectedResult . "\nActual:" . $buffer . "\nWaited $STALL_DEFAULT seconds and never received expected results.\n");
  }
  else {
    printf("\nTest " . $testNum . " PASS\nExpected:" . $expectedResult . "\n" . "Actual:" . $actualResult . "\n");
  }
  printf("**************************************************************************\n");
  printf("**************************************************************************\n\n\n");
  
	
	$nodeAddr += 1;
	sleep 1; # delay between calls
	
} # end of while loop
#### End of REG0030 
###



##########################################################
#### REG0040
#### Fire Command
$testNum = "REG0040";
$testDesc = "Firing Test";
printf("\n********************\n");
printf("       $testNum         ");
printf("\n$testDesc\n");
printf("\n********************\n");

$nodeAddr = 0; # Set node address to start with


# Loop through all nodes
while ($nodeAddr <= 9) {
	
  ##	
	$expectedResult = ""; # no result from fire command
 	$input_1 = "\$" . dec2hex($nodeAddr) . "1FFF"; 
  ## 
  
  printf("\n\n*** Firing controller: " . dec2hex($nodeAddr) . " ***\n\n"); 
 
  $ob->read_char_time(0);     # don't wait for each character
  $ob->read_const_time(1000); # 1 second per unfulfilled "read" call
  $chars=0;
  $buffer="";
  
  # Send Inputs 
  $ob->write($input_1); # send input_1
# $ob->write($input_1); # send input_2
# $ob->write($input_1); # send input_3    

	
	$nodeAddr += 1;
	sleep 5; # delay between calls
	
} # end of while loop
#### End of REG0040 
###




##########################################################
#### REG0050
#### Status Command after firing
$testNum = "REG0050";
$testDesc = "Status command after firing";
printf("\n********************\n");
printf("       $testNum         ");
printf("\n$testDesc\n");
printf("\n********************\n");

$nodeAddr = 0; # Set node address to start with


# Loop through all nodes
while ($nodeAddr <= 9) {
	
  ##	
	$expectedResult = "*" . dec2hex($nodeAddr) . "0e\r\n" ;
 	$input_1 = "\$" . dec2hex($nodeAddr) . "1SFF"; 
  ## 
  
  printf("\n\n*** Testing controller: " . dec2hex($nodeAddr) . " ***\n\n"); 
 
  $ob->read_char_time(0);     # don't wait for each character
  $ob->read_const_time(1000); # 1 second per unfulfilled "read" call
  $chars=0;
  $buffer="";
  
  # Send Inputs 
  $ob->write($input_1); # send input_1
# $ob->write($input_1); # send input_2
# $ob->write($input_1); # send input_3    


  # Poll for expected results
  $timeout=$STALL_DEFAULT;
  while ($timeout>0) {
      ($count,$saw)=$ob->read(25); # will read _up to_ 25 chars
      if ($count > 0) {
        $chars+=$count;
        $buffer.=$saw;
        last if($buffer eq $expectedResult)
               
        # Check here to see if what we want is in the $buffer
        # say "last" if we find it
        }
      else {
        $timeout--;
        }
  }
 
  $actualResult = $buffer;
 
### And the results are..... 
  printf("\n***************************************************************************\n");
  printf("                       RESULTS of $testNum for " . dec2hex($nodeAddr) . "      ");
  printf("\n$testDesc\n");
  printf("\n***************************************************************************\n");
  if ($timeout==0) {
    printf("\nTest " . $testNum . " FAIL\nExpected:" . $expectedResult . "\nActual:" . $buffer . "\nWaited $STALL_DEFAULT seconds and never received expected results.\n");
  }
  else {
    printf("\nTest " . $testNum . " PASS\nExpected:" . $expectedResult . "\n" . "Actual:" . $actualResult . "\n");
  }
  printf("**************************************************************************\n");
  printf("**************************************************************************\n\n\n");
  
	
	$nodeAddr += 1;
	sleep 1; # delay between calls
	
} # end of while loop
#### End of REG0050 
###




##########################################################
#### REG0060
#### Activation Command after firing
$testNum = "REG0060";
$testDesc = "Activation time after firing";
printf("\n********************\n");
printf("       $testNum         ");
printf("\n$testDesc\n");
printf("\n********************\n");

$nodeAddr = 0; # Set node address to start with


# Loop through all nodes
while ($nodeAddr <= 9) {
	
  ##	
	$expectedResult = "\r\n" ;
 	$input_1 = "\$" . dec2hex($nodeAddr) . "1AFF"; 
  ## 
  
  printf("\n\n*** Testing controller: " . dec2hex($nodeAddr) . " ***\n\n"); 
 
  $ob->read_char_time(0);     # don't wait for each character
  $ob->read_const_time(1000); # 1 second per unfulfilled "read" call
  $chars=0;
  $buffer="";
  
  # Send Inputs 
  $ob->write($input_1); # send input_1
# $ob->write($input_1); # send input_2
# $ob->write($input_1); # send input_3    


  # Poll for expected results
  $timeout=$STALL_DEFAULT;
  while ($timeout>0) {
      ($count,$saw)=$ob->read(25); # will read _up to_ 25 chars
      if ($count > 0) {
        $chars+=$count;
        $buffer.=$saw;
        last if($buffer =~/\*\d+\d+\d+\d+\d?.*/)
               
        # Check here to see if what we want is in the $buffer
        # say "last" if we find it
        }
      else {
        $timeout--;
        }
  }
 
  $actualResult = $buffer;
 
### And the results are..... 
  printf("\n***************************************************************************\n");
  printf("                       RESULTS of $testNum for " . dec2hex($nodeAddr) . "      ");
  printf("\n$testDesc\n");
  printf("\n***************************************************************************\n");
  if ($timeout==0) {
    printf("\nTest " . $testNum . " FAIL\nActual:" . $buffer . "\nWaited $STALL_DEFAULT seconds and never received expected results.\n");
  }
  else {
    printf("\nTest " . $testNum . " PASS\nActual:" . $actualResult . "\n");
  }
  printf("**************************************************************************\n");
  printf("**************************************************************************\n\n\n");
  
	
	$nodeAddr += 1;
	sleep 1; # delay between calls
	
} # end of while loop
#### End of REG0060 
###



##########################################################
#### REG0070
#### Query Command after firing
$testNum = "REG0070";
$testDesc = "Fill time after firing";
printf("\n********************\n");
printf("       $testNum         ");
printf("\n$testDesc\n");
printf("\n********************\n");

$nodeAddr = 0; # Set node address to start with


# Loop through all nodes
while ($nodeAddr <= 9) {
	
  ##	
	$expectedResult = "\r\n" ;
 	$input_1 = "\$" . dec2hex($nodeAddr) . "1QFF"; 
  ## 
  
  printf("\n\n*** Testing controller: " . dec2hex($nodeAddr) . " ***\n\n"); 
 
  $ob->read_char_time(0);     # don't wait for each character
  $ob->read_const_time(1000); # 1 second per unfulfilled "read" call
  $chars=0;
  $buffer="";
  
  # Send Inputs 
  $ob->write($input_1); # send input_1
# $ob->write($input_1); # send input_2
# $ob->write($input_1); # send input_3    


  # Poll for expected results
  $timeout=$STALL_DEFAULT;
  while ($timeout>0) {
      ($count,$saw)=$ob->read(25); # will read _up to_ 25 chars
      if ($count > 0) {
        $chars+=$count;
        $buffer.=$saw;
        last if($buffer =~/\*\d+\d+\d+\d+\d+\d?.*/)
               
        # Check here to see if what we want is in the $buffer
        # say "last" if we find it
        }
      else {
        $timeout--;
        }
  }
 
  $actualResult = $buffer;
 
### And the results are..... 
  printf("\n***************************************************************************\n");
  printf("                       RESULTS of $testNum for " . dec2hex($nodeAddr) . "      ");
  printf("\n$testDesc\n");
  printf("\n***************************************************************************\n");
  if ($timeout==0) {
    printf("\nTest " . $testNum . " FAIL\nActual:" . $buffer . "\nWaited $STALL_DEFAULT seconds and never received expected results.\n");
  }
  else {
    printf("\nTest " . $testNum . " PASS\nActual:" . $actualResult . "\n");
  }
  printf("**************************************************************************\n");
  printf("**************************************************************************\n\n\n");
  
	
	$nodeAddr += 1;
	sleep 1; # delay between calls
	
} # end of while loop
#### End of REG0070 
###









undef $ob;

exit 0;






#----------------------------(  promptUser  )-----------------------------#
#                                                                         #
#  FUNCTION:	promptUser                                                #
#                                                                         #
#  PURPOSE:	Prompt the user for some type of input, and return the    #
#		input back to the calling program.                        #
#                                                                         #
#  ARGS:	$promptString - what you want to prompt the user with     #
#		$defaultValue - (optional) a default value for the prompt #
#                                                                         #
#-------------------------------------------------------------------------#

sub promptUser {

   #-------------------------------------------------------------------#
   #  two possible input arguments - $promptString, and $defaultValue  #
   #  make the input arguments local variables.                        #
   #-------------------------------------------------------------------#

   local($promptString,$defaultValue) = @_;

   #-------------------------------------------------------------------#
   #  if there is a default value, use the first print statement; if   #
   #  no default is provided, print the second string.                 #
   #-------------------------------------------------------------------#

   if ($defaultValue) {
      print $promptString, "[", $defaultValue, "]: ";
   } else {
      print $promptString, ": ";
   }

   $| = 1;               # force a flush after our print
   $_ = <STDIN>;         # get the input from STDIN (presumably the keyboard)


   #------------------------------------------------------------------#
   # remove the newline character from the end of the input the user  #
   # gave us.                                                         #
   #------------------------------------------------------------------#

   chomp;

   #-----------------------------------------------------------------#
   #  if we had a $default value, and the user gave us input, then   #
   #  return the input; if we had a default, and they gave us no     #
   #  no input, return the $defaultValue.                            #
   #                                                                 # 
   #  if we did not have a default value, then just return whatever  #
   #  the user gave us.  if they just hit the <enter> key,           #
   #  the calling routine will have to deal with that.               #
   #-----------------------------------------------------------------#

   if ("$defaultValue") {
      return $_ ? $_ : $defaultValue;    # return $_ if it has a value
   } else {
      return $_;
   }
}


sub dec2hex {
    # parameter passed to
    # the subfunction
    my $decnum = $_[0];
    # the final hex number
    my $hexnum="";
    my $tempval="";
    
    if ($decnum == 0) {
    	return "00";
    }
    
    while ($decnum != 0) {
    # get the remainder (modulus function)
    # by dividing by 16
    $tempval = $decnum % 16;
    # convert to the appropriate letter
    # if the value is greater than 9
    if ($tempval > 9) {
    $tempval = chr($tempval + 55);
    }
    # 'concatenate' the number to 
    # what we have so far in what will
    # be the final variable
    $hexnum = $tempval . $hexnum ;
    # new actually divide by 16, and 
    # keep the integer value of the 
    # answer
    $decnum = int($decnum / 16); 
    # if we cant divide by 16, this is the
    # last step
    if ($decnum < 16) {
    # convert to letters again..
    if ($decnum > 9) {
    $decnum = chr($decnum + 55);
    }
    
    # add this onto the final answer.. 
    # reset decnum variable to zero so loop
    # will exit
    $hexnum = $decnum . $hexnum; 
    $decnum = 0 
    }
    }
    $hexnum =~ s/([^\W0-9_])/\l$1/g; # convert to lowercase
    return $hexnum;
    } # end sub

#sub obFlush {
#	
#	
#	
#}
