#!/bin/sh

# Functionality:
# Slaves COM is sending a defined String to the same COM on Master. 
# Master is running a background process that
# only returns what comes in. 

# Hint: "ori" = ORIginal; "rb" = ReadBack; "res" = RESult; "err" = ERRor

#-----------------------------------------------------------------------------
# PREPERATIONS
#-----------------------------------------------------------------------------

# Make shure that result Directory exists
if [ ! -d /test/result ]
then
    mkdir /test/result
fi

# Make shure that result file of Test does NOT exists
if [ -s /test/result/bus2.res ]
then
    rm /test/result/bus2.res
fi

# Make shure that error file of Test does NOT exists
if [ -s /test/result/bus2.err ]
then
    rm /test/result/bus2.err
fi

# Make shure that Original file of Test does NOT exists
if [ -s /test/result/bus2.ori ]
then
    rm /test/result/bus2.ori
fi

# Make shure that readback file of Test does NOT exists
if [ -s /test/result/bus2.rb ]
then
    rm /test/result/bus2.rb
fi

# Set up COM3
stty -F /dev/ttyS4 -echo -echoe -echok -echonl -echoprt -echoctl -echoke -ocrnl -icrnl -onlcr -opost crtscts clocal cread cs8 ignpar
stty -F /dev/ttyS5 -echo -echoe -echok -echonl -echoprt -echoctl -echoke -ocrnl -icrnl -onlcr -opost crtscts clocal cread cs8 ignpar

#-----------------------------------------------------------------------------
# TEST WITH 19200 BAUD
#-----------------------------------------------------------------------------

# Message Baudrate to Log
echo -e "Test bus2 with Baudrate: 19200\n\r"
# Set Baudrate
stty -F /dev/ttyS4 19200
stty -F /dev/ttyS5 19200

# Start a Backgroundprocess to receive something
cat /dev/ttyS4 1>> /test/result/bus2.rb &

sleep 1

# Send the defined String
echo "This is BUS2 Test with 19200 Baud" > /dev/ttyS5 
echo "This is BUS2 Test with 19200 Baud" 1>> /test/result/bus2.ori


# Set Script-Process to Sleep to have time to receive something
sleep 1
# After Receiving (Or Timeout), kill all Processes on CAT
killall cat

# Compare the read String with the original one
diff /test/result/bus2.ori /test/result/bus2.rb 1>> /test/result/bus2.res 
                                                                                                        2>> /test/result/bus2.err

#-----------------------------------------------------------------------------
# TEST WITH 115200 BAUD
#-----------------------------------------------------------------------------

# Message Baudrate to Log
echo -e "Test bus2 with Baudrate: 115200\n\r"
# Set Baudrate
stty -F /dev/ttyS4 115200
stty -F /dev/ttyS5 115200

# Start a Backgroundprocess to receive something
cat /dev/ttyS4 1>> /test/result/bus2.rb &

sleep 1

# Send the defined String
echo "This is BUS2 Test with 115200 Baud" > /dev/ttyS5 
echo "This is BUS2 Test with 115200 Baud" 1>> /test/result/bus2.ori


# Set Script-Process to Sleep to have time to receive something
sleep 1
# After Receiving (Or Timeout), kill all Processes on CAT
killall cat

# Compare the read String with the original one
diff /test/result/bus2.ori /test/result/bus2.rb 1>> /test/result/bus2.res 
                                                                                                        2>> /test/result/bus2.err

  
#-----------------------------------------------------------------------------
# OUTPUT AND RESULT CALCULATION
#-----------------------------------------------------------------------------

# Display content of /test/result/bus2.res
echo -e "\n\rContent of bus2.res"
cat /test/result/bus2.res

# Display content of /test/result/bus2.err
echo -e "\n\rContent of bus2.err"
cat /test/result/bus2.err

# If comparison returned neither results nor errors, the copying and comparision was successful
if [ ! -s /test/result/bus2.res -a ! -s /test/result/bus2.err ]

then
    # Test was successful
    echo -e "\n\rBUS2 TEST-PASSED"
        echo "BUS2 TEST-PASSED" >> /test/result/bus2.res
else
    # Test failed
    echo -e "\n\rBUS2 TEST-FAIL"
    echo "BUS2 TEST-FAILED" >> /test/result/bus2.res
fi 
