#!/bin/bash
#-----------------------------------------------------------------------------
#
#   \brief   EnterUBoot()
#
#   Function enters the UBoot of the TC3.
#
#   \param[in]     none         
#   \param[out]    none       
#   \param[in,out] none       
#
#   \return
#   possible return value(s) and description
#   return 0 -> C_NO_ERROR
#   return 1 -> C_UNKOWN_ERROR
# 
#   \created     29.08.2013  STW/A.Appelt
#
#-----------------------------------------------------------------------------

function EnterUBoot() 
{
   # connect to TC3

   # configure stty
   stty -F $serial_port cs8 -cstopb -hupcl -iexten -echo -ixon -crtscts -parenb raw 115200 igncr

   if [ "$1" == "hard_reset" ];
   then
      echo -e "\033[32mPlease restart the TC3...\033[37m"

      # wait for UBoot version
      if (! searchUBootVersion "hard_reset" )
      then
         echo -e "\nWrong Uboot version!"
         return 1
      fi
   elif [ "$1" == "soft_reset" ];
   then
      echo -e "TC3 is restarting...\n"  
   
      # wait for UBoot version
      if (! searchUBootVersion "soft_reset" )
      then
         echo -e "\nWrong Uboot version!"
         return 1
      fi 
   
   else
      return 1
   fi   

   # start buffering RS232 traffic
   OUTPUT=`cat $serial_port > "$script_dir/$VarTmpFile" &`

   # send some ESC siganls to enter UBoot
   echo $'\33'$'\33'$'\33'$'\33'$'\33'$'\33'$'\33'$'\33' > $serial_port

   # wait for UBoot promt
   if (! waitForPromt "=>")
   then
      echo -e "\nError: Timeout!"
      echo -e "Unable to find UBoot promt. Please start repeat update procedure.\n"
      return 1  
   fi  

   echo -e "\nYou have now entered the UBoot.\n"

return 0 
}

# function to search for UBoot version
function searchUBootVersion() 
{
   while read -t $VarTimeoutUBootVersion line; do
      if test "${line#*"2008.10"}" != "$line"
         then
         echo "$line"
         return 1
      elif test "${line#*"2012.10"}" != "$line"
         then
         echo "$line"
         # check UBoot Version
         checkUBootVersion "$line"
         ret_val=$?
         if [ "$ret_val" != 0 ];
         then
            echo -e "\033[33mWarning: UBoot Version is STW-V2.01r0 or older. Update via TFTP could cause errors.\033[37m"
         fi 

         return 0
      else
         if [ "$1" == "hard_reset" ];
         then
            echo "Waiting for restart..."
         fi
      fi
   done < $serial_port
}

# function to search for a Promt
function waitForPromt() 
{  
   param=$1
   length=${#param}
   StartTime=$(date +%s)
   timeout 10 tail -f "$script_dir/$VarTmpFile" | 
   while read -n $length line; do
      CurrTime=$(date +%s)
      TimeDiff=$(( $CurrTime - $StartTime ))
      if [ $TimeDiff -ge 9 ]; 
      then
         killall tail & > /dev/null &
         return 1
      elif test "${line#*"$1"}" != "$line"
         then
         killall tail & > /dev/null &
         return 0
      fi
   done 
   OUT=$? 
   CurrTime=$(date +%s)
   TimeDiff=$(( $CurrTime - $StartTime ))
   if [ "$OUT" == 0 -a $TimeDiff -le 9 ]; then
      killall cat & > /dev/null &
      cat "$script_dir/$VarTmpFile"
      echo -e "\n"
      sleep 1
      return 0
   fi
killall cat & > /dev/null &
return 1
}

# function to check the UBoot Version
function checkUBootVersion() 
{  

   # find index of 'STW-V'
   index=$(grep -o -b "STW-V" <<< "$1" | cut -c1-2 )
   index=$(($index + 6))

   # get UBoot Version Number
   UBootVersion=$(expr substr "$1" "$index" 6)
   UBootVersion=${UBootVersion//[.r]/}

   # check if UBoot Version is compatible with update via TFTP
   if [ "$UBootVersion" -le "$VarUBootVersionForTftp" ];
   then
      return 1
   fi

return 0
}

