2022年3月20日 星期日

Get IP for MV31-w

知識:
SNR是指信噪比,信号与噪声的比值,值越大就证明信号的功率比噪声功率大,信号质量越好。
C/N, C/No这两个是载噪声比值,一般是指GPS的射频噪声值,
C/N这个值是一般芯片或者是模块不包含发射链路的噪声值,
C/No是从信号到天线发射整条链路的噪声值,一般这两个值在模块中都有提及。
Copy it from SNR,C/N, C/N0分别是什么意思,啥啥分不清楚,请技术大拿帮忙解答下

環境: Xubunt20.04

 Get IP for MV31-W
#!/bin/bash


function cecho
{
    if [ "$1" == "red" ]; then
        echo -e "\033[31m$2\033[0m"
    elif [ "$1" == "blue" ]; then
        echo -e "\033[34m$2\033[0m"
    else
        echo $2
    fi
    return 0
}

function SendCommand
{
    sleep 2 #Avoid running AT command too fast to hung
    USBinterface=$1
    echo -e "\n"
    echo -e "AT cmd:$2"
    echo -n -e "$2" > $USBinterface
    while read line < $USBinterface;
    do
        echo -n -e "$line" | grep 'ERROR'
        if [ $? -eq 0 ]; then
            cecho "red" "Error Code: -1002, Invalid AT command"
            exit 1
        fi
        echo "$line" | grep -v 'OK'
        echo "$line" | grep 'OK'
        if [ $? -eq 0 ]; then
            return 0
        fi
    done
}

function MBIM_PrepareDriver
{
    drv_name=("option" "usb_wwan" "usbserial" "cdc_mbim")

    ret=0
    for drv in ${drv_name[@]}
    do
        #echo $drv 
        ret=$(lsmod | grep $drv | cut -f 1 -d " ") #drv: driver
        if [ -z "$ret" ]; then
            cecho "red" "Please install the driver of \"$drv\" with modprobe"
        fi
    done

    ret=$(cat /sys/bus/usb-serial/drivers/option1/new_id)
    if [ -z "$ret" ]; then
        echo "1e2d 00b3" | sudo tee /sys/bus/usb-serial/drivers/option1/new_id
    fi
}

function QMI_PrepareDriver
{

    #drv_name=("option" "qmi_wwan" "cdc_mbim")
    drv_name=("option" "qmi_wwan")

    ret=0
    for drv in ${drv_name[@]}
    do
        #echo $drv 
        ret=$(lsmod | grep $drv | cut -f 1 -d " ") #drv: driver
        if [ -z "$ret" ]; then
            cecho "red" "Please install the driver of \"$drv\" with modprobe"
        fi
    done

}

function GetInterface
{
    dev=$(ls /dev/ttyUSB* | awk -F "\/" '{print $3}')
    if [ -z "$dev" ];then
        cecho "red" "Error code: -1001, Can't found the /dev/ttyUSB* "
        exit
    else
        echo "$dev"
    fi
}

function MBIM_PrepareGetIP
{
    #install libmbim-utils
    ret=$(whereis mbimcli | cut -f 2 -d " ")
    if [ "$ret" == "mbimcli:" ];then
        sudo apt-get install libmbim-utils
    fi

    #install udhcpc
    ret=$(whereis udhcpc | cut -f 2 -d " ")
    if [ "$ret" == "udhcpc:" ];then
        sudo apt-get install udhcpc
    fi

    #write profile in /etc/mbim-network.conf
    APN=INTERNET #CHT
    APN_USER=
    APN_PASS=
    PROXY=yes
    sudo echo -e "APN=$APN\n" \
    "APN_USER=$APN_USER\n" \
    "APN_PASS=$APN_PASS\n" \
    "PROXY=$PROXY" > /etc/mbim-network.conf

    #chect if the /dev/cdc-wdm exitis
    if [ -z /dev/cdc-wdm[0-9] ];then
        cecho "red" "Error Code: -1004, /dev/cdc-wdm* is null"
        exit 1
    else
        cdc_wdm=$(ls /dev/ | grep cdc)
    fi

    flag=0
    for wwan in $(ls /sys/class/net)
    do
        if [ -d "/sys/class/net/$wwan/device/driver" ]; then
            ret=$(basename $(realpath /sys/class/net/$wwan/device/driver))
            if [ "$ret" == "cdc_mbim" ]; then
                flag=1
                break
            fi
        fi
    done
    wwan0=$wwan
    if [ "$flag" -eq 0 ]; then
        cecho "red" "Error Code: -1005, can't find wwan interface."
        exit 1
    fi

    #Get IP and connect the Internet
    sudo ip link set $wwan0 down
    sudo mbim-network /dev/$cdc_wdm start
    sudo mbimcli -d /dev/$cdc_wdm --device-open-proxy --query-ip-configuration
    sudo udhcpc -i $wwan0
    sudo ip link set $wwan0 up

    ##unsure why the wwan0 value can not return
}

function QMI_PrepareGetIP
{
    #install libmbim-utils
    ret=$(whereis mbimcli | cut -f 2 -d " ")
    if [ "$ret" == "qmicli:" ];then
        sudo apt-get install libqmi-utils
    fi

    #install udhcpc
    ret=$(whereis udhcpc | cut -f 2 -d " ")
    if [ "$ret" == "udhcpc:" ];then
        sudo apt-get install udhcpc
    fi

    #write profile in /etc/qmi-network.conf
    APN=INTERNET #CHT
    APN_USER=
    APN_PASS=
    PROXY=yes
    sudo echo -e "APN=$APN\n" \
    "APN_USER=$APN_USER\n" \
    "APN_PASS=$APN_PASS\n" \
    "PROXY=$PROXY" > /etc/qmi-network.conf

    #chect if the /dev/cdc-wdm exitis
    if [ -z /dev/cdc-wdm[0-9] ];then
        cecho "red" "Error Code: -1004, /dev/cdc-wdm* is null"
        exit 1
    else
        cdc_wdm=$(ls /dev/ | grep cdc)
    fi

    wwan0=$(sudo qmicli -d /dev/$cdc_wdm -p --get-wwan-iface)
    if [ $? -gt 0 ]; then
        cecho "red" "Error Code: -1003, invalid wwan0 interface"
        exit 1
    fi

    #Get IP and connect the Internet
    sudo qmicli -d /dev/$cdc_wdm --dms-set-operating-mode='online'
    sudo ip link set $wwan0 down
    sudo echo 'Y' | sudo tee /sys/class/net/$wwan0/qmi/raw_ip
    sudo ip link set $wwan0 up
    sudo qmicli -d /dev/$cdc_wdm --wda-get-data-format
    ##sudo qmicli -p -d /dev/cdc-wdm0 -p --wds-start-network="apn='YOUR_APN',username='YOUR_USERNAME',password='YOUR_PASSWORD',ip-type=4" --client-no-release-cid
    sudo qmi-network /dev/$cdc_wdm start
    sudo udhcpc -i $wwan0
}

function TestIP
{

    #test
    sync
    sleep 3
    echo "Ping a Network for Testing Connectivity"
    ping -c 5 1.1.1.1
    if [ $? -eq 0 ];then
        cecho "blue" "ping 1.1.1.1 successed"
    else
        cecho "red" "ping 1.1.1.1 failed"
    fi
    ping -c 5 tw.yahoo.com
    if [ $? -eq 0 ];then
        cecho "blue" "ping tw.yahoo.com successed"
        exit 0
    else
        cecho "red" "ping tw.yahoo.com failed"
        exit 1
    fi
}

#--Help---------------------------------------------------------------------------------


Devinterface=
Open_Qxdm_RecLog=0
TEST=0
COUNTER=0
MBIM=1  #default
QMI=0

function usageExit()
{
  cat >&2 << EOF
    telit.sh - Quick link to Internet for test

    telit.sh [options]

    options:
    -h, --help                    Show this menu
    -p, --path device_path        Specify the port for AT command, such as: /dev/ttyUSB*
    -t, --test counter            Test the speed performance
    -x, --qxdm device_path        Run some AT commands ahead of time to log file using qxdm_logger_x64
    -q, --qmi                     QMI mode
    -m, --mbim                    MBIM mode (default)
EOF
    exit 0
}

function checkArgExit() 
{
    if [[ $# -lt 2 ]]; then
        echo "Missing argument for option $1" >&2
        usageExit
        exit 1
    fi
}

# Parse arguments
if [ $# -lt 1 ]; then
  cat >&2 << EOF
Enter paramaters, such as:
suod ./telit.sh -m -p /dev/ttyUSB0 for MV31,
sudo ./telit.sh -q -p /dev/ttyUSB2 for FN990,
The default mode is MBIM, please use paramater -q or -m to switch QMI/MBIM mode
EOF
    exit 0
else
     while [ -n "$1" ]; do
         case "$1" in
             -h|--help)
                 usageExit
                 ;;
             -q|--qmi)
                 QMI=1
                 MBIM=0
                 shift 1
                 ;;
             -m|--mbim)
                 QMI=0
                 shift 1
                 ;;
             -p|--path)
                 checkArgExit $@
                 Devinterface=$2
                 shift 2
                 ;;
             -x|--qxdm)
                 cecho "blue" "please check "at^diag_enable?" value is 1"
                 checkArgExit $@
                 Devinterface=$2
                 Open_Qxdm_RecLog=1
                 shift 2
                 ;;
             -t|--test)
                 checkArgExit $@
                 COUNTER=$2 
                 TEST=1
                 shift 2
                 ;;
             -v|--version)
                 echo version="1.1"
                 exit
                 ;;
             *)
                 echo "Unknown option $1" >&2
                 echo >&2
                 usageExit
                 ;;
         esac
     done
fi

if [ ! "$TEST" -eq 1 ]; then
    #turn off ModemManager.service
    #cecho "red" "Ready to turn off ModemManager.service, it will take a while..."
    #cecho "red" "Press 'q' to exit"
    sudo systemctl stop ModemManager.service
    #sudo systemctl status ModemManager.service
    sleep 2

    #echo tty=${Devinterface##*/}
    #echo tty1=$tty
    tty=$(basename $Devinterface)
    ret=$(ps -ef | grep -i $tty | grep -iv $0 | grep -v grep)

    if [ ! -z "$ret" ]; then # $ret is not empty
        echo "There are other programs in the background to occupy $Devinterface"
        exit 1
    fi

    exec <> $Devinterface
fi

if [ "$Open_Qxdm_RecLog" -eq 1 ]; then
    SendCommand $Devinterface "AT+CFUN=0\r"
    sleep 1
    SendCommand $Devinterface "AT+CFUN=1\r"
    SendCommand $Devinterface "at^diag_enable?\r"
    
    #The following information must be sent to Thalse
    cecho  "red" "The following information must be sent to Thalse - start"
    SendCommand $Devinterface "at^debug?\r"
    SendCommand $Devinterface "ati1\r"
    SendCommand $Devinterface "at^version?\r"
    SendCommand $Devinterface "at+cops?\r"
    SendCommand $Devinterface "at+cimi\r"
    SendCommand $Devinterface "at+csq\r"
    SendCommand $Devinterface "at^sbfs?\r"
    SendCommand $Devinterface "at^mcfg?\r"
    SendCommand $Devinterface "at^band_pref_ext?\r"
    cecho  "red" "The following information must be sent to Thalse - end"
    cecho  "red" "The following information must be sent to Thalse - end"
    cecho  "red" "The following information and *.qmdl must be sent to Thalse - end"
    exit 0
fi

if [ "$TEST" -eq 1 ]; then
ii=0
log=speedtest_log.txt
    if [ ! -f "./speedtest-cli" ];then
        wget -O speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
        chmod +x speedtest-cli
    fi
    ret=$(whereis netperf | cut -f 2 -d " ")
    if [ "$ret" == "netperf:" ];then
        sudo apt install netperf
    fi
    while [ $ii -lt $COUNTER ]
    do    
        TIMESTAMP=`date +%Y-%m-%d_%H:%M:%S`
        ping -c 5 1.1.1.1
        echo -e "Timestamp: $TIMESTAMP, OK\n" >> $log
        if [ $? -eq 0 ];then
            cecho "blue" "ping 1.1.1.1 successed"
            ./speedtest-cli > /tmp/speedtest_result
            cat /tmp/speedtest_result | grep "Download\|Upload\|Packet" >> $log 
	        echo -n -e "\r" >> $log

            echo -e "---> netperf -4 -H \"netperf-eu.bufferbloat.net\" -t TCP_MAERTS -l 10 -v 1 -P 1 "
            netperf -4 -H "netperf-eu.bufferbloat.net" -t TCP_MAERTS -l 10 -v 1 -P 1
            sleep 1

            echo -e "\n---> netperf UL test "
            echo -e "---> netperf -4 -H \"netperf-eu.bufferbloat.net\" -t TCP_STREAM -l 10 -v 1s -P 1 "
            netperf -4 -H "netperf-eu.bufferbloat.net" -t TCP_STREAM -l 10 -v 1 -P 1
            sleep 1

            sleep 60
        else
            cecho "red" "ping 1.1.1.1 failed"
            echo -e "Timestamp: $TIMESTAMP, Fail\n" >> $log
        fi
	ii=$((ii+1))
    done
    exit 1
fi

#dev=$(GetInterface)
#dev=($dev)

SendCommand $Devinterface "AT+COPS?\r"
if [ "$MBIM" -eq 1 ]; then
    cecho "blue" "mbim"
    MBIM_PrepareDriver
    MBIM_PrepareGetIP
elif [ "$QMI" -eq 1 ]; then
    cecho "blue" "qmi"
    QMI_PrepareDriver
    QMI_PrepareGetIP
fi
TestIP


exit
如果 /dev/cdc-wdm* ( libmbim ) 一直沒有產生
systemctl disable ModemManager.service後,電腦重新開機
或是確認 cdc_mbim.ko 是否有掛載


Linux Driver:
4.9.309 support (keyword: 1e2d and 00b3)

沒有留言:

張貼留言