#!/system/bin/sh

atdev=/dev/omap_csmi_tty0
pppdev=/dev/omap_csmi_tty1

n1=`cat /data/phoneentry1 2>/dev/null`
n2=`cat /data/phoneentry2 2>/dev/null`
n3=`cat /data/phoneentry3 2>/dev/null`
n1=${n1:-"*#06#"}
n2=${n2:-"*#06#"}
n3=${n3:-"*#06#"}
phoneoutputpid=
eventoutputpid=
notifypid=
notifytoggle=false
pppdpid=
powerdidletime=120

# map phone specific keys
setkey -k 0xe4 -v 0x23 # map #
setkey -k 0xe3 -v 0x2a # map *
setkey -k 231 -v 513 # map send to newline
#setkey -k 0x67 -v 0x20b # map up to scroll back
#setkey -k 0x6c -v 0x20a # map down to scroll forward
setkey -k 0x73 -v 0x20b # map volume up to scroll back
setkey -k 0x72 -v 0x20a # map volume down to scroll forward
setkey -k 0x60 -v 0x211 # map PoC to next console

# tuttle keys
setkey -k 0x38 -v 0x703 # map leftalt to alt
setkey -k 0x9b -v 0x703 # map mail to alt
setkey -t 8 -k 0x9b -v 0x703 # map alt-mail to alt
setkey -t 8 -k 0x10 -v 0x21 # map alt-q to !
setkey -t 8 -k 0x11 -v 0x31 # map alt-w to 1
setkey -t 8 -k 0x12 -v 0x32 # map alt-e to 2
setkey -t 8 -k 0x13 -v 0x33 # map alt-r to 3
setkey -t 8 -k 0x14 -v 0x2b # map alt-t to +
setkey -t 8 -k 0x15 -v 0x28 # map alt-y to (
setkey -t 8 -k 0x16 -v 0x29 # map alt-u to )
setkey -t 8 -k 0x17 -v 0x2d # map alt-i to -
setkey -t 8 -k 0x18 -v 0x5f # map alt-o to _
setkey -t 8 -k 0x19 -v 0x22 # map alt-p to "
setkey -t 8 -k 0x1e -v 0x23 # map alt-a to #
setkey -t 8 -k 0x1f -v 0x34 # map alt-s to 4
setkey -t 8 -k 0x20 -v 0x35 # map alt-d to 5
setkey -t 8 -k 0x21 -v 0x36 # map alt-f to 6
setkey -t 8 -k 0x22 -v 0x2f # map alt-g to /
setkey -t 8 -k 0x23 -v 0x3f # map alt-h to ?
setkey -t 8 -k 0x24 -v 0xa3 # map alt-j to pound
setkey -t 8 -k 0x25 -v 0x24 # map alt-k to $
setkey -t 8 -k 0x2c -v 0x2a # map alt-z to *
setkey -t 8 -k 0x2d -v 0x37 # map alt-x to 7
setkey -t 8 -k 0x2e -v 0x38 # map alt-c to 8
setkey -t 8 -k 0x2f -v 0x39 # map alt-v to 9
setkey -t 8 -k 0x30 -v 0x7c # map alt-b to |
setkey -t 8 -k 0x31 -v 0x40 # map alt-n to @
setkey -t 8 -k 0x32 -v 0x3d # map alt-m to =
setkey -t 8 -k 0x33 -v 0x3b # map alt-, to ;
setkey -t 8 -k 0x34 -v 0x3a # map alt-. to :
setkey -t 8 -k 0x0f -v 0x30 # map alt-tab to 0
setkey -t 8 -k 0x67 -v 0x20b # map alt-up to scroll back
setkey -t 8 -k 0x6c -v 0x20a # map alt-down to scroll forward

while true
do
	echo
	echo "------------------------------"
	echo " 1: init commands"
	echo " 2: call commands"
	echo " 3: misc phone"
	echo " 4: phone debug output"
	echo " 5: test data connection"
	echo " 6: start runtime"
	echo " 7: start runtime w/output"
	echo " 8: stop runtime"
	echo " 9: misc"
	echo -n ": "
	while true
	do
		c=`readtty -t 50 -f -a 1234567890#`
		case "$c" in
			"" ) ;;
			* ) break;
		esac
	done
	echo Got key -$c-
	case $c in
		"1" )
			while true; do
				echo
				echo "------------------------------"
				echo " 1: Print phone output"
				echo " 2: ATQ0V1E1+CMEE=2;+CREG=0"
				echo " 3: AT+CFUN=1"
				echo " 4: AT+COPS=0"
				echo " 5: AT+CREG?"
				echo " 6: Stop phone output"
				echo " 0: back"
				echo -n ": "
				c=`readtty -f -a 1234560#`
				echo Got key -$c-
				case "$c" in
					"1" ) kill $phoneoutputpid; cat $atdev & phoneoutputpid=$! ;;
					"2" ) echo -e "ATQ0V1E1+CMEE=2;+CREG=0\r" >$atdev;;
					"3" ) echo -e "AT+CFUN=1\r" >$atdev;;
					"4" ) echo -e "AT+COPS=0\r" >$atdev;;
					"5" ) echo -e "AT+CREG?\r" >$atdev;;
					"6" ) kill $phoneoutputpid; phoneoutputpid= ;;
					"0" ) break;;
				esac
			done
		;;
		"2" )
			while true; do
				echo
				echo "------------------------------"
				echo " 1: Dial: ATD $n1;"
				echo " 2: Dial: ATD $n2;"
				echo " 3: Dial: ATD $n3;"
				echo " 4: Set number for 1"
				echo " 5: Set number for 2"
				echo " 6: Set number for 3"
				echo " 7: Dial: ATD ...;"
				echo " 8: Hang up: ATH"
				echo " 9: Answer: ATA"
				echo " 0: back"
				echo -n ": "
				c=`readtty -f -a 1234567890#`
				echo Got key -$c-
				case "$c" in
					"1" ) echo "Dialing $n1"; echo -e "ATD $n1;\r" >$atdev;;
					"2" ) echo "Dialing $n2"; echo -e "ATD $n2;\r" >$atdev;;
					"3" ) echo "Dialing $n3"; echo -e "ATD $n3;\r" >$atdev;;
					"4" ) echo -n "Number: "; read n1; echo $n1 >/data/phoneentry1;;
					"5" ) echo -n "Number: "; read n2; echo $n2 >/data/phoneentry2;;
					"6" ) echo -n "Number: "; read n3; echo $n3 >/data/phoneentry3;;
					"7" ) echo -n "Number: "; read n; echo "Dialing $n"; echo -e "ATD $n;\r" >$atdev;;
					"8" ) echo -e "ATH\r" >$atdev;;
					"9" ) echo -e "ATA\r" >$atdev;;
					"0" ) break;;
				esac
			done
		;;
		"3" )
			while true; do
				echo
				echo "------------------------------"
				echo " 1: Save FFS data"
				echo " 2: Load user FFS data"
				echo " 3: Load system FFS data"
				echo " 4: Reset FFS data"
				echo " 5: Set uplink gain"
				echo " 6: Set echo"
				echo " 7: cat /dev/omap_csmi_battery_t"
				echo " 8: cat /dev/omap_csmi_htc"
				echo " 0: back"
				echo -n ": "
				c=`readtty -f -a 123456780#`
				echo Got key -$c-
				case "$c" in
					"1" ) cat /dev/omap_csmi_ffs >/data/ffsdata;;
					"2" ) cat /data/ffsdata >/dev/omap_csmi_ffs;;
					"3" ) cat /system/ffsdata >/dev/omap_csmi_ffs;;
					"4" ) echo - >/dev/omap_csmi_ffs;;
					"5" )
						echo -n "Gain: "; read g;
						echo gu$g >/tmp/gain;
						cat /tmp/gain 2>/dev/null >/dev/omap_csmi_audio_tes
					;;
					"6" )
						echo -n "Echo param (hex): "; read e;
						echo "e0x$e" >/tmp/echo;
						cat /tmp/echo 2>/dev/null >/dev/omap_csmi_audio_tes
					;;
					"7" ) cat /dev/omap_csmi_battery_t;;
					"8" ) cat /dev/omap_csmi_htc;;
					"0" ) break;;
				esac
			done
		;;
		"4" )
			while true; do
				echo
				echo "------------------------------"
				echo " 1: Toggle debug I/O"
				echo " 2: Toggle debug Flow"
				echo " 3: Toggle debug Interrupt"
				echo " 4: Toggle debug Info"
				echo " 5: Toggle GSM run state"
				echo " 6: Clear GSM data area"
				echo " 0: back"
				echo -n ": "
				c=`readtty -f -a 1234560#`
				echo Got key -$c-
				case "$c" in
					"1" ) echo -n "i" >/sys/devices/system/omap_csmi/debug;;
					"2" ) echo -n "f" >/sys/devices/system/omap_csmi/debug;;
					"3" ) echo -n "I" >/sys/devices/system/omap_csmi/debug;;
					"4" ) echo -n "F" >/sys/devices/system/omap_csmi/debug;;
					"5" ) echo -n "s" >/sys/devices/system/omap_csmi/debug;;
					"6" ) echo -n "c" >/sys/devices/system/omap_csmi/debug;;
					"0" ) break;;
				esac
			done
		;;
		"5" )
			while true; do
				echo
				echo "------------------------------"
				echo " 1: Start pppd - userspace"
				echo " 2: Start pppd - kernel"
				echo " 3: Start pppd - kernel <at1"
				echo " 4: Configure ppp data to at2"
				echo " 5: Test with HTTP GET"
				echo " 6: Kill pppd"
				echo " 0: back"
				echo -n ": "
				c=`readtty -f -a 1234560#`
				echo Got key -$c-
				case "$c" in
					"1" ) kill $pppdpid; pppd notty < $pppdev > $pppdev & pppdpid=$!;;
					"2" ) kill $pppdpid; pppd nodetach $pppdev & pppdpid=$!;;
					"3" ) kill &pppdpid; pppd nodetach $pppdev connect "sh -c \"chat -v -f /etc/ppp/connect-data <$atdev >$atdev\"" & pppdpid=$!;;
					"4" ) echo -e 'AT%DATA=2,"UART",1,,"SER","UART",0\r' >$atdev;;
					"5" ) test-data-connection;;
					"6" ) kill $pppdpid; pppdpid=;;
					"0" ) break;;
				esac
			done
		;;
		"6" )
			echo
			echo ------------------------
			echo Starting android runtime
			echo ------------------------
			start
		;;
		"7" )
			echo
			echo ------------------------
			echo Starting android runtime
			echo ------------------------
			if exists /data/singleproc
			then
				single_process="-s"
			else
				single_process=""
			fi
			start runtime $single_process
		;;
		"8" )
			stop
		;;
		"9" )
			while true; do
				echo
				echo "------------------------------"
				echo " 1: Print events"
				echo " 2: Stop event output"
				if $notifytoggle
				then
					echo " 3: stop notify"
				else
					echo " 3: notify /sys/android_power"
				fi
				echo " 4: start powerd"
				echo " 5: start powerd verbose"
				echo " 6: stop powerd"
				echo " 7: set powerd idletime ($powerdidletime)"
				echo " 8: start multitap shell"
				if exists /data/singleproc
				then
					echo " 9: enable multiprocess"
				else
					echo " 9: disable multiprocess"
				fi
				echo " c: start shell"
				echo " 0: back"
				echo -n ": "
				c=`readtty -f -a 1234567890c#`
				echo Got key -$c-
				case "$c" in
					"1" ) kill $eventoutputpid; getevent & eventoutputpid=$! ;;
					"2" ) kill $eventoutputpid; eventoutputpid= ;;
					"3" )
						if $notifytoggle
						then
							kill $notifypid
							notifypid=
							notifytoggle=false
						else
							kill $notifypid
							notify -m 0x00000002 -c 0 -p -v 0 -w 30 /sys/android_power &
							notifypid=$!
							notifytoggle=true
						fi
					;;
					"4" ) start powerd -i $powerdidletime ;;
					"5" ) start powerd -i $powerdidletime -v ;;
					"6" ) stop powerd ;;
					"7" ) echo -n "Idle time (seconds): "; read powerdidletime ;;
					"8" )
						readtty -f -p -t 10 -e "[ ~" | sh -i
					;;
					"9" )
						if exists /data/singleproc
						then
							echo "Enabling multiprocess environment."
							rm /data/singleproc
						else
							echo "Disabling multiprocess environment."
							echo >/data/singleproc "true"
						fi
					;;
					"c" ) sh -i <>/dev/tty0 1>&0 2>&1 ;;
					"0" ) break;;
				esac
			done
		;;
	esac
done

