#!/bin/bash
#
# Routes the serial console of BP or co-cpu to debug port
#
SOURCE_PORT=1
DEST_PORT=0
PROC_MUX=/proc/driver/uartmux
VERSION=1.0

function set_serial_route() {
	echo "Setting up the console route from port $SOURCE_PORT to $DEST_PORT"
	echo "$SOURCE_PORT $DEST_PORT" > $PROC_MUX
	echo "done."
	if [ "$DEST_PORT" = "1" ]; then
		echo "The dest port selected is Main-cpu"
		echo "use: minicom /dev/ttyS1  to see the console"
	fi
}

# Check if proc entry exists
if [[ -e $PROC_MUX ]]; then
	echo "serial_router: Version $VERSION: using dirver via $PROC_MUX"
else
	echo "Error: Exiting.... Unable to find the uartmux-driver"
	exit 1
fi

echo "Select the source port (from)" 
echo "-----------------------------"
echo "   (1-8) BP slot"
echo "   (9)   Main-CPU /dev/ttyS1"
echo "   (10)  Co-CPU"
echo -n "press number (1-10): "
read ans
if [ "$ans" -gt "0" -a "$ans" -lt "11" ]; then
	SOURCE_PORT=$ans
else
	echo "Error: Invalid source port $ans"
	exit 1
fi

echo ""
echo "Select the dest port (to)" 
echo "-------------------------"
echo "   (0) Debug port"
echo "   (1) Main-CPU /dev/ttyS1"
echo -n "press number (0 or 1): "
read ans

if [ "$ans" = "0" -o "$ans" = "1" ]; then
	DEST_PORT=$ans
else
	echo "Error: Invalid dest port $ans"
	exit 1
fi

if [ "$SOURCE_PORT" = "9" -a "$DEST_PORT" = "1" ]; then
	echo "Error: Invalid routing request Main-CPU to Main-CPU"
	exit 1
fi

set_serial_route		# do it
exit 0

