#!/bin/sh
source /lib/functions.sh
#运行目录
MODEM_RUNDIR="/var/run/qmodem"
SCRIPT_DIR="/usr/share/qmodem"

modem_config=$1
mkdir -p "${MODEM_RUNDIR}/${modem_config}_dir"
log_file="${MODEM_RUNDIR}/${modem_config}_dir/dial_log"
debug_subject="modem_dial"
source "${SCRIPT_DIR}/generic.sh"
touch $log_file

get_led()
{
    config_foreach get_led_by_slot modem-slot
}

get_led_by_slot()
{
    local cfg="$1"
    config_get slot "$cfg" slot
    if [ "$modem_slot" = "$slot" ];then
        config_get sim_led "$cfg" sim_led
        config_get net_led "$cfg" net_led
    fi
}

get_associate_ethernet_by_path()
{
    local cfg="$1"
    config_get slot "$cfg" slot
    config_get ethernet "$cfg" ethernet
    if [ "$modem_slot" = "$slot" ];then
        config_get ethernet_5g "$cfg" ethernet_5g
    fi
}

set_led()
{
    local type=$1
    local modem_config=$2
    local value=$3
    get_led "$modem_slot"
    case $type in
        sim)
            [ -z "$sim_led" ] && return
            echo $value > /sys/class/leds/$sim_led/brightness
            ;;
        net)
            [ -z "$net_led" ] && return
            cfg_name=$(echo $net_led |tr ":" "_") 
            uci batch << EOF
set system.n${cfg_name}=led
set system.n${cfg_name}.name=${modem_slot}_net_indicator
set system.n${cfg_name}.sysfs=${net_led}
set system.n${cfg_name}.trigger=netdev
set system.n${cfg_name}.dev=${modem_netcard}
set system.n${cfg_name}.mode="tx rx"
commit system
EOF

            /etc/init.d/led restart
            ;;
    esac
}

get_driver()
{
	for i in $(find $modem_path -name driver);do
		lsfile=$(ls -l $i)
		type=${lsfile:0:1}
		if [ "$type" == "l" ];then
			link=$(basename $(ls -l $i | awk '{print $11}'))
			case $link in
				"qmi_wwan"*) 
					mode="qmi"
					break
				;;
				"cdc_mbim")
					mode="mbim"
					break
					;;
				"cdc_ncm")
					mode="ncm"
					break
					;;
				"cdc_ether")
					mode="ecm"
					break
					;;
				"rndis_host")
					mode="rndis"
					break
					;;
                "mhi_netdev")
                    mode="mhi"
                    break
                    ;;
				*)
					if [ -z "$mode" ]; then
						mode="unknown"
					fi
				;;
			esac
		fi
	done
    echo $mode
}

unlock_sim()
{
    pin=$1
    sim_lock_file="/var/run/qmodem/${modem_config}_dir/pincode"
    lock ${sim_lock_file}.lock
    if [ -f $sim_lock_file ] && [ "$pin" == "$(cat $sim_lock_file)"];then
        m_debug "pin code is already try"
    else
        
        res=$(at "$at_port" "AT+CPIN=\"$pin\"")
        case "$?" in
            0)
                m_debug "unlock sim card with pin code $pin success"
                ;;
            *)
                echo $pin > $sim_lock_file
                m_debug "info" "unlock sim card with pin code $pin failed,block try until nextboot"
                ;;
        esac
    fi
    lock -u ${sim_lock_file}.lock

}

update_config()
{
    config_load qmodem
    config_get state $modem_config state
    config_get enable_dial $modem_config enable_dial
    config_get modem_path $modem_config path
    config_get dial_tool $modem_config dial_tool
    config_get pdp_type $modem_config pdp_type
    config_get network_bridge $modem_config network_bridge
    config_get metric $modem_config metric
    config_get at_port $modem_config at_port
    config_get manufacturer $modem_config manufacturer
    config_get platform $modem_config platform
    config_get define_connect $modem_config define_connect
    config_get ra_master $modem_config ra_master
    config_get extend_prefix $modem_config extend_prefix
    config_get global_dial main enable_dial
    # config_get ethernet_5g u$modem_config ethernet 转往口获取命令更新，待测试
    config_foreach get_associate_ethernet_by_path modem-slot
    modem_slot=$(basename $modem_path)
    config_get alias $modem_config alias
    driver=$(get_driver)
    update_sim_slot
    case $sim_slot in
        1)
        config_get apn $modem_config apn
        config_get username $modem_config username
        config_get password $modem_config password
        config_get auth $modem_config auth
        config_get pincode $modem_config pincode
        ;;
        2)
        config_get apn $modem_config apn2
        config_get username $modem_config username2
        config_get password $modem_config password2
        config_get auth $modem_config auth2
        config_get pincode $modem_config pincode2
        [ -z "$apn" ] && config_get apn $modem_config apn
        [ -z "$username" ] && config_get username $modem_config username
        [ -z "$password" ] && config_get password $modem_config password
        [ -z "$auth" ] && config_get auth $modem_config auth
        [ -z "$pin" ] && config_get pincode $modem_config pincode
        ;;
        *)
            config_get apn $modem_config apn
            config_get username $modem_config username
            config_get password $modem_config password
            config_get auth $modem_config auth
            config_get pincode $modem_config pincode
            ;;
    esac
    modem_net=$(find $modem_path -name net |tail -1)
    modem_netcard=$(ls $modem_net)
    interface_name=$modem_config
    [ -n "$alias" ] && interface_name=$alias
    interface6_name=${interface_name}v6
}

check_dial_prepare()
{
    cpin=$(at "$at_port" "AT+CPIN?")
    get_sim_status "$cpin"
    case $sim_state_code in
        "0")
            m_debug "info sim card is miss"
            ;;
        "1")
            m_debug "info sim card is ready"
            sim_fullfill=1
            ;;
        "2")
            m_debug "pin code required"
            [ -n "$pincode" ] && unlock_sim $pincode
            ;;
        *)
            m_debug "info sim card state is $sim_state_code"
            ;;
    esac
    
    if [ "$sim_fullfill" = "1" ];then
        set_led "sim" $modem_config 255
    else
        set_led "sim" $modem_config 0
    fi
    if [ -n "$modem_netcard" ] && [ -d "/sys/class/net/$modem_netcard" ];then
        netdev_fullfill=1
    else
        netdev_fullfill=0
    fi

    if [ "$enable_dial" = "1" ] && [ "$sim_fullfill" = "1" ] && [ "$state" != "disabled" ] ;then
        config_fullfill=1
    fi
    if [ "$config_fullfill" = "1" ] && [ "$sim_fullfill" = "1" ] && [ "$netdev_fullfill" = "1" ] ;then
        return 1
    else
        return 0
    fi
}

check_ip()
{
    case $manufacturer in
            "quectel")
                case $platform in
                    "qualcomm")
                        check_ip_command="AT+CGPADDR=1"
                        ;;
                    "unisoc")
                        check_ip_command="AT+CGPADDR=1"
                        ;;
                    "lte")
                        if [ "$define_connect" = "3" ];then
                            check_ip_command="AT+CGPADDR=3"
                        else
                            check_ip_command="AT+CGPADDR=1"
                        fi
                        ;;
                    
                esac
                ;;
            "fibocom")
                case $platform in
                    "qualcomm")
                        check_ip_command="AT+CGPADDR=1"
                        ;;
                    "unisoc")
                        check_ip_command="AT+CGPADDR=1"
                        ;;
                    "lte")
                        check_ip_command="AT+CGPADDR=1"
                        ;;
                    "mediatek")
                        check_ip_command="AT+CGPADDR=3"
                        stric=1
                        ;;
                esac
                ;;
        esac
        ipaddr=$(at "$at_port" "$check_ip_command" |grep +CGPADDR:)
        if [ -n "$ipaddr" ];then
            ipv6=$(echo $ipaddr | grep -oE "\b([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}\b")
            ipv4=$(echo $ipaddr | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
            disallow_ipv4="0.0.0.0"
            #remove the disallow ip
            if [ "$ipv4" == *"$disallow_ipv4"* ];then
                ipv4=""
            fi
            connection_status=0
            if [ -n "$ipv4" ];then
                connection_status=1
            fi
            if [ -n "$ipv6" ];then
                connection_status=2
            fi
            if [ -n "$ipv4" ] && [ -n "$ipv6" ];then
                connection_status=3
            fi
        else
            connection_status="-1"
            m_debug "at port response unexpected $ipaddr"
        fi
}

append_to_fw_zone()
{
    local fw_zone=$1
    local if_name=$2
    source /etc/os-release
    local os_version=${VERSION_ID:0:2}
    if [ "$os_version" -le 21 ];then
        has_ifname=0
        origin_line=$(uci -q get firewall.@zone[${fw_zone}].network)
        for i in $origin_line
        do
            if [ "$i" = "$if_name" ];then
                has_ifname=1
            fi
        done
        if [ -n "$origin_line" ] && [ "$has_ifname" -eq 0 ];then
            uci set firewall.@zone[${fw_zone}].network="${origin_line} ${if_name}"
        elif [ -z "$origin_line" ];then
            uci set firewall.@zone[${fw_zone}].network="${if_name}"
        fi
    else
        uci add_list firewall.@zone[${fw_zone}].network=${if_name}
    fi
}

set_if()
{
    #check if exist
    interface=$(uci -q get network.$interface_name)
    if [ -z "$interface" ];then
        uci set network.${interface_name}=interface
        uci set network.${interface_name}.modem_config="${modem_config}"
        uci set network.${interface_name}.proto='dhcp'
        uci set network.${interface_name}.defaultroute='1'
        uci set network.${interface_name}.peerdns='0'
        uci set network.${interface_name}.metric="${metric}"
        uci add_list network.${interface_name}.dns='114.114.114.114'
        uci add_list network.${interface_name}.dns='119.29.29.29'
        local num=$(uci show firewall | grep "name='wan'" | wc -l)
        local wwan_num=$(uci -q get firewall.@zone[$num].network | grep -w "${interface_name}" | wc -l)
        if [ "$wwan_num" = "0" ]; then
            append_to_fw_zone $num ${interface_name}
        fi
        #set ipv6
        #if pdptype contain 6
        if [ -n "$(echo $pdp_type | grep "6")" ];then
            uci set network.lan.ipv6='1'
            uci set network.lan.ip6assign='64'
            uci set network.lan.ip6class="${interface6_name}"
            uci set network.${interface6_name}.modem_config="${modem_config}"
            uci set network.${interface6_name}='interface'
            uci set network.${interface6_name}.proto='dhcpv6'
            uci set network.${interface6_name}.ifname="@${interface_name}"
            uci set network.${interface6_name}.device="@${interface_name}"
            uci set network.${interface6_name}.metric="${metric}"
            if [ "$ra_master" = "1" ];then
                uci set dhcp.${interface6_name}='dhcp'
                uci set dhcp.${interface6_name}.interface="${interface6_name}"
                uci set dhcp.${interface6_name}.ra='relay'
                uci set dhcp.${interface6_name}.ndp='relay'
                uci set dhcp.${interface6_name}.master='1'
                uci set dhcp.${interface6_name}.ignore='1'
                uci set dhcp.lan.ra='relay'
                uci set dhcp.lan.ndp='relay'
                uci set dhcp.lan.dhcpv6='relay'
                uci commit dhcp
            elif [ "$extend_prefix" = "1" ];then
                uci set network.${interface6_name}.extendprefix=1
            fi
            local wwan6_num=$(uci -q get firewall.@zone[$num].network | grep -w "${interface6_name}" | wc -l)
            if [ "$wwan6_num" = "0" ]; then
                append_to_fw_zone $num ${interface6_name}
            fi
        fi
        uci commit network
        uci commit firewall
        ifup ${interface_name}
        m_debug "create interface $interface_name"

    fi
    
    set_modem_netcard=$modem_netcard
    if [ -z "$set_modem_netcard" ];then
        m_debug "no netcard found"
    fi
    ethernet_check=$(handle_5gethernet)
    if [ -n "$ethernet_check" ] && [ -n "/sys/class/net/$ethernet_5g" ] && [ -n "$ethernet_5g" ];then
        set_modem_netcard=$ethernet_5g
    fi
    #set led
    set_led "net" $modem_config $set_modem_netcard
    origin_netcard=$(uci -q get network.$interface_name.ifname)
    origin_device=$(uci -q get network.$interface_name.device)
    if [ "$origin_netcard" == "$set_modem_netcard" ] && [ "$origin_device" == "$set_modem_netcard" ];then
        m_debug "interface $interface_name already set to $set_modem_netcard"
    else
        uci set network.${interface_name}.ifname="${set_modem_netcard}"
        uci set network.${interface_name}.device="${set_modem_netcard}"
        uci commit network
        ifup ${interface_name}
        m_debug "set interface $interface_name to $set_modem_netcard"
    fi
}

flush_if()
{
    # uci delete network.${interface_name}
    # uci delete network.${interface6_name}
    # uci delete dhcp.${interface6_name}
    # uci commit network
    # uci commit dhcp
    # set_led "net" $modem_config
    # set_led "sim" $modem_config 0
    # m_debug "delete interface $interface_name"
    config_load network
    remove_target="$modem_config"
    config_foreach flush_ip_cb "interface"
    set_led "net" $modem_config
    set_led "sim" $modem_config 0
    m_debug "delete interface $interface_name"
}

flush_ip_cb()
{
    local network_cfg=$1
    
    config_get bind_modem_config "$network_cfg" modem_config
    if [ "$remove_target" = "$bind_modem_config" ];then
        uci delete network.$network_cfg
        uci commit network
        uci commit dhcp
    fi
    
}


dial(){
    update_config
    m_debug "modem_path=$modem_path,driver=$driver,interface=$interface_name,at_port=$at_port,using_sim_slot:$sim_slot"
    while [ "$dial_prepare" != 1 ] ; do
        sleep 5
        update_config
        check_dial_prepare
        dial_prepare=$?
    done
    set_if
    m_debug "dialing $modem_path driver $driver"
    case $driver in
        "qmi")
            qmi_dial
            ;;
        "mbim")
            mbim_dial
            ;;
        "mhi")
            mhi_dial
            ;;
        "ncm")
            at_dial_monitor
            ;;
        "ecm")
            at_dial_monitor
            ;;
        "rndis")
            at_dial_monitor
            ;;
        *)
            mbim_dial
            ;;
    esac
}

wwan_hang()
{
    #kill quectel-CM
    killall quectel-CM
}


ecm_hang()
{
    if [ "$manufacturer" = "quectel" ]; then
		at_command="AT+QNETDEVCTL=1,2,1"
	elif [ "$manufacturer" = "fibocom" ]; then
		#联发科平台（广和通FM350-GL）
		if [ "$platform" = "mediatek" ]; then
			at_command="AT+CGACT=0,3"
		else
			at_command="AT+GTRNDIS=0,1"
		fi
	elif [ "$manufacturer" = "meig" ]; then
		at_command="AT$QCRMCALL=0,1,1,2,1"
	else
		at_command='ATI'
	fi

	tmp=$(at "${at_port}" "${at_command}")
}


hang()
{
    m_debug "hang up $modem_path driver $driver"
    case $driver in
        "ncm")
            ecm_hang
            ;;
        "ecm")
            ecm_hang
            ;;
        "rndis")
            ecm_hang
            ;;
        "qmi")
            wwan_hang
            ;;
        "mbim")
            wwan_hang
            ;;
        "mhi")
            wwan_hang
            ;;
    esac
    flush_if
}

mbim_dial(){
    if [ -z "$apn" ];then
        apn="auto"
    fi
    qmi_dial
}

mhi_dial()
{
    qmi_dial
}
qmi_dial()
{
    cmd_line="quectel-CM"
    [ -e "/usr/bin/quectel-CM-M" ] cmd_line="quectel-CM-M"
	case $pdp_type in
		"ip") cmd_line="$cmd_line -4" ;;
		"ipv6") cmd_line="$cmd_line -6" ;;
		"ipv4v6") cmd_line="$cmd_line -4 -6" ;;
		*) cmd_line="$cmd_line -4 -6" ;;
	esac

	if [ "$network_bridge" = "1" ]; then
		cmd_line="$cmd_line -b"
	fi
	if [ -n "$apn" ]; then
		cmd_line="$cmd_line -s $apn"
	fi
	if [ -n "$username" ]; then
		cmd_line="$cmd_line $username"
	fi
	if [ -n "$password" ]; then
		cmd_line="$cmd_line $password"
	fi
	if [ "$auth" != "none" ]; then
		cmd_line="$cmd_line $auth"
	fi
	if [ -n "$modem_netcard" ]; then
        qmi_if=$(echo "$modem_netcard" | cut -d_ -f1)
        qmi_if=$(echo "$modem_netcard" | cut -d. -f1)
		cmd_line="${cmd_line} -i ${qmi_if}"
	fi
    [ -e "/usr/bin/quectel-CM-M" ] && [ -n "$metric" ] && cmd_line="$cmd_line -M $metric"
    [ -e "/usr/bin/quectel-CM-M" ] && cmd_line="$cmd_line -f $log_file" || cmd_line="$cmd_line -d -f $log_file"
    $cmd_line
    
    
}

at_dial()
{
    if [ -z "$apn" ];then
        apn="auto"
    fi
    if [ -z "$pdp_type" ];then
        pdp_type="IP"
    fi
    local at_command='AT+COPS=0,0'
	tmp=$(at "${at_port}" "${at_command}")
    pdp_type=$(echo $pdp_type | tr 'a-z' 'A-Z')
    case $manufacturer in
        "quectel")
            case $platform in
                "qualcomm")
                    at_command="AT+QNETDEVCTL=1,3,1"
                    cgdcont_command="AT+CGDCONT=1,\"$pdp_type\",\"$apn\""
                    ;;
                "unisoc")
                    at_command="AT+QNETDEVCTL=1,3,1"
                    cgdcont_command="AT+CGDCONT=1,\"$pdp_type\",\"$apn\""
                    ;;
                "lte")
                    if [ "$define_connect" = "3" ];then
                        at_command="AT+QNETDEVCTL=3,3,1"
                        cgdcont_command="AT+CGDCONT=3,\"$pdp_type\",\"$apn\""
                    else
                        at_command="AT+QNETDEVCTL=1,3,1"
                        cgdcont_command="AT+CGDCONT=1,\"$pdp_type\",\"$apn\""
                    fi
                    ;;
                *)
                    at_command="AT+QNETDEVCTL=1,3,1"
                    cgdcont_command="AT+CGDCONT=1,\"$pdp_type\",\"$apn\""
                    ;;
            esac
            ;;
        "fibocom")
            case $platform in
                "qualcomm")
                    at_command="AT+GTRNDIS=1,1"
                    cgdcont_command="AT+CGDCONT=1,\"$pdp_type\",\"$apn\""
                    ;;
                "unisoc")
                    at_command="AT+GTRNDIS=1,1"
                    cgdcont_command="AT+CGDCONT=1,\"$pdp_type\",\"$apn\""
                    ;;
                "lte")
                    at_command="AT+GTRNDIS=1,1"
                    cgdcont_command="AT+CGDCONT=1,\"$pdp_type\",\"$apn\""
                    ;;
                "mediatek")
                    at_command="AT+CGACT=1,3"
                    cgdcont_command="AT+CGDCONT=3,\"$pdp_type\",\"$apn\""
                    ;;
            esac
            ;;
            
    esac
    m_debug "dialing vendor:$manufacturer;platform:$platform; $cgdcont_command ; $at_command"
    at "${at_port}" "${cgdcont_command}"
    at "$at_port" "$at_command"
}

ip_change_fm350()
{
    m_debug "ip_change_fm350"
    at_command="AT+CGPADDR=3"
    local ipv4_config=$(at ${at_port} ${at_command} | cut -d, -f2 | grep -oE '[0-9]+.[0-9]+.[0-9]+.[0-9]+')
    local public_dns1_ipv4="223.5.5.5"
    local public_dns2_ipv4="119.29.29.29"
    local public_dns1_ipv6="2400:3200::1"
    local public_dns2_ipv6="2402:4e00::"
    at_command="AT+GTDNS=3" | grep "+GTDNS: "| grep -E '[0-9]+.[0-9]+.[0-9]+.[0-9]+' | sed -n '1p'
    local ipv4_dns1=$(echo "${response}" | awk -F'"' '{print $2}' | awk -F',' '{print $1}')
    [ -z "$ipv4_dns1" ] && {
        ipv4_dns1="${public_dns1_ipv4}"
    }

    local ipv4_dns2=$(echo "${response}" | awk -F'"' '{print $4}' | awk -F',' '{print $1}')
    [ -z "$ipv4_dns2" ] && {
        ipv4_dns2="${public_dns2_ipv4}"
    }

    local ipv6_dns1=$(echo "${response}" | awk -F'"' '{print $2}' | awk -F',' '{print $2}')
    [ -z "$ipv6_dns1" ] && {
        ipv6_dns1="${public_dns1_ipv6}"
    }

    local ipv6_dns2=$(echo "${response}" | awk -F'"' '{print $4}' | awk -F',' '{print $2}')
    [ -z "$ipv6_dns2" ] && {
        ipv6_dns2="${public_dns2_ipv6}"
    }
    uci_ipv4=$(uci -q get network.$interface_name.ipaddr)
    
    uci set network.${interface_name}.proto='static'
    uci set network.${interface_name}.ipaddr="${ipv4_config}"
    uci set network.${interface_name}.netmask='255.255.255.0'
    uci set network.${interface_name}.gateway="${ipv4_config%.*}.1"
    uci set network.${interface_name}.peerdns='0'
    uci -q del network.${interface_name}.dns
    uci add_list network.${interface_name}.dns="${ipv4_dns1}"
    uci add_list network.${interface_name}.dns="${ipv4_dns2}"
    uci commit network
    ifdown ${interface_name}
    ifup ${interface_name}
    m_debug "set interface $interface_name to $ipv4_config"

}

handle_5gethernet()
{
    case $manufacturer in
        "quectel")
            case $platform in
                "qualcomm")
                    quectel_qualcomm_ethernet
                    ;;
                "unisoc")
                    quectel_unisoc_ethernet
                    ;;
            esac
            ;;
    esac
}

quectel_unisoc_ethernet()
{
    case "$driver" in
        "ncm"|\
        "ecm"|\
        "rndis")
            check_ethernet_cmd="AT+QCFG=\"ethernet\""
            time=0
            while [ $time -lt 5 ]; do
                result=$(at $at_port $check_ethernet_cmd | grep "+QCFG:")
                if [ -n "$result" ]; then
                    if [ -n "$(echo $result | grep "ethernet\",1")" ]; then
                        echo "1"
                        m_debug "5G Ethernet mode is enabled"
                        break
                    fi
                fi
                sleep 5
                time=$((time+1))
            done
        ;;
    esac
}

quectel_qualcomm_ethernet()
{
     case "$driver" in
        "mbim")
            eth_driver_at="AT+QETH=\"eth_driver\""
            data_interface_at="AT+QCFG=\"data_interface\""
            ehter_driver_expect="\"r8125\",1"
            data_interface_expect="\"data_interface\",1"

            time=0
            while [ $time -lt 5 ]; do
                eth_driver_result=$(at $at_port $eth_driver_at | grep "+QETH:")
                time=$(($time+1))
                sleep 1
                if [ -n "$eth_driver_result" ];then
                    break
                fi
            done
            time=0
            while [ $time -lt 5 ]; do
                data_interface_result=$(at $at_port $data_interface_at | grep "+QCFG:")
                time=$(($time+1))
                sleep 1
                if [ -n "$data_interface_result" ];then
                    break
                fi
            done
            eth_driver_pass=$(echo $eth_driver_result | grep "$ehter_driver_expect")
            data_interface_pass=$(echo $data_interface_result | grep "$data_interface_expect")
            if  [ -n "$eth_driver_pass" ] && [ -n "$data_interface_pass" ];then
                echo "1"
                m_debug "5G Ethernet mode is enabled"
            fi
            ;;
    esac
}

handle_ip_change()
{
    export ipv4
    export ipv6
    export connection_status
    m_debug  "ip changed from $ipv6_cache,$ipv4_cache to $ipv6,$ipv4"
    case $manufacturer in
        "fibocom")
            case $platform in
                "mediatek")
                    ip_change_fm350
                    ;;
            esac
            ;;
    esac
}

check_logfile_line()
{
    local line=$(wc -l $log_file | awk '{print $1}')
    if [ $line -gt 300 ];then
        echo "" > $log_file
        m_debug  "log file line is over 300,clear it"
    fi
}

unexpected_response_count=0
at_dial_monitor()
{
    check_ip
    at_dial
    ipv4_cache=$ipv4
    while true; do
        check_ip
        if [ $connection_status -eq 0 ];then
            at_dial
            sleep 5
        elif [ $connection_status -eq -1 ];then
            unexpected_response_count=$((unexpected_response_count+1))
            if [ $unexpected_response_count -gt 3 ];then
                at_dial
                unexpected_response_count=0
            fi
            sleep 10
        else
        #检测ipv4是否变化
            sleep 15
            if [ "$ipv4" != "$ipv4_cache" ];then
                handle_ip_change
                ipv6_cache=$ipv6
                ipv4_cache=$ipv4
            fi
        fi
        check_logfile_line
    done
}

case "$2" in
    "hang")
        debug_subject="modem_hang"
        update_config
        hang;;
    "dial")
        case "$state" in
            "disabled")
                debug_subject="modem_hang"
                hang;;
            *)
                dial;;
        esac
esac
