#!/bin/sh

#----------------------------------------------------------------------------
#   script file to maintain a 3G/4G connection
#
#   (C) Cypress Solutions Inc.
#
#   Unauthorized copying or distribution in parts or as a whole prohibited
#----------------------------------------------------------------------------

. /etc/config/config.running
. /etc/functions.sh

logger -p user.info -t dialer dialer started

# wait until first-boot stuff is done
while [ -f /etc/default/firstboot ]; do
	sleep 1
done

# make sure everything is clean before we begin
kill_proc_wait ql-dial 30

WWAN_RETRIES=0

# enter main loop to
while [ 1 ]; do
    card_detect_block

	# update WWAN band preferences (if required)
	wwan_bandselect

    VZW_SPR_MODE=""
    # detect whether Oxygen contains a Verizon-provisioned radio
    if [ "`cat /var/run/modeminfo | grep Provisioning | grep 'VZW\|VERIZON'`" != "" ]; then
    	logger -p user.info -t dialer "radio module is provisioned for Verizon"
        VZW_SPR_MODE=1
    fi
    if [ "`cat /var/run/modeminfo | grep Provisioning | grep 'SPRINT'`" != "" ]; then
        logger -p user.info -t dialer "radio module is provisioned for Sprint"
        VZW_SPR_MODE=1
    fi

    # read current config - things might have changed while waiting for modem to be ready
    . /etc/config/config.running

	# do some clean-up...
    rm -f /etc/config/resolv.conf

    # establish connection now
    echo calling > /var/run/wwan_addr

    # setup WWAN connection data
    apn_param=""
    user_param=""
    pass_param=""
    auth_param=""
    profile_param=""
    data_format_param=""

    [ "$WWAN_APN1" != "" ] && apn_param="--apn=$WWAN_APN1"
    [ "$WWAN_AUTH_USER1" != "" ] && user_param="--user=\"$WWAN_AUTH_USER1\""
    [ "$WWAN_AUTH_PASS1" != "" ] && pass_param="--password=\"$WWAN_AUTH_PASS1\""
    if [ "$VZW_SPR_MODE" = "1" ] && [ "$WWAN_APN1" = "" ]; then
        logger -p user.info -t dialer "using cell module 'profile 3' parameters"
        profile_param="--profile=3"
    fi
    if [ "$WWAN_AUTH_USER1" != "" ] || [ "$WWAN_AUTH_PASS1" != "" ]; then
        [ "$WWAN_AUTH_TYPE" = "0" ] && auth_param="--auth-method=pap"
        [ "$WWAN_AUTH_TYPE" = "1" ] && auth_param="--auth-method=chap"
    fi
    [ "$devicearg" != "" ] && data_format_param="--data-format=$devicearg"
    [ "$DIALER_DEBUG_LEVEL" != "" ] && [ "$DIALER_DEBUG_LEVEL" != "0" ] && verbose_param="--verbose=$DIALER_DEBUG_LEVEL"


    if [ "$deviceclass" == "ql-dial" ]; then
    	#---------------------------------------------------------------------
    	# establish connection using ql-dial
    	#---------------------------------------------------------------------

        logger -p user.info -t dialer "calling /bin/ql-dial $apn_param $user_param $pass_param $profile_param $auth_param $data_format_param $verbose_param"

        echo 1 > /sys/class/net/wwan0/qmi/raw_ip

   		/bin/ql-dial $apn_param $user_param $pass_param $profile_param $auth_param $data_format_param  $verbose_param
   		x=$?
    	logger -p user.info -t dialer "ql-dial terminated, return code "$x
    else
        logger -p user.warn -t dialer "unknown dial command: $deviceclass"
        x=100
	fi
   	echo offline > /var/run/status
   	rm -f /var/run/wwan_addr

    #---------------------------------------------------------------------
    # determine wait time until next connect....
    #---------------------------------------------------------------------

    if [ "$(( ($x == 0) || (x == 9) || ($x == 10) || ($x == 11) || ($x == 12) ))" = "1" ]; then
        # return codes 0, 9, 10, 11, 12 indicate that a connection had been established
        # or that an external event requested shutdown
  		WWAN_RETRIES=0
  	else
  	    # all other return codes indicate that no connection had been established in
   	    # first place
     	WWAN_RETRIES=$(($WWAN_RETRIES + 1))
  	fi

    if [ "$SLOW_RETRY" = "1" ]; then
   		if [ $WWAN_RETRIES -le 4 ]; then
       		WAIT_TIME=65
        fi
  		if [ $WWAN_RETRIES -eq 5 ]; then
           	WAIT_TIME=615
   		fi
   		if [ $WWAN_RETRIES -eq 6 ]; then
       		WAIT_TIME=920
   		fi
   		if [ $WWAN_RETRIES -eq 7 ]; then
       		WAIT_TIME=1830
   		fi
   		if [ $WWAN_RETRIES -ge 8 ]; then
       		WAIT_TIME=3650
   		fi
    else
		if [ $WWAN_RETRIES -le 4 ]; then
			WAIT_TIME=15
		fi
		if [ $WWAN_RETRIES -eq 5 ]; then
           	WAIT_TIME=60
   		fi
   		if [ $WWAN_RETRIES -eq 6 ]; then
       		WAIT_TIME=180
   		fi
   		if [ $WWAN_RETRIES -eq 7 ]; then
       		WAIT_TIME=300
   		fi
   		if [ $WWAN_RETRIES -ge 8 ]; then
       		WAIT_TIME=600
   		fi
    fi

    # restart radio if dialer was interrupted by user
    #if [ "$x" == "9" ]; then
    #    restart_radio
    #fi

    # restart radio after too many consecutive failed connection attempts
    if [ "$WWAN_RETRIES" = "12" ] || [ "$WWAN_RETRIES" = "20" ]; then
  	    restart_radio
    fi

    # and reboot the whole unit after 30 failed connection attempts
    if [ "$WWAN_RETRIES" = "30" ]; then
        logger -p user.info -t dialer "15 consecutive failed connection attempts - rebooting using watchdog!"
        echo 1 > /var/run/wd_reset.request
        sleep 120
    fi

    logger -p user.notice -t dialer "restarting connection in "$WAIT_TIME" seconds"
    sleep $WAIT_TIME
    . /etc/config/config.running
done

logger -p user.notice -t dialer "dialer finished - should never happen!"
