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 to send an AT command and check the response.
function SendCommand {
    local usb_interface="$1"
    local at_command="$2"
    local response
    local line

    sleep 1 # Avoid running AT commands too fast

    echo -e "\nAT cmd: $at_command"
    echo -n -e "$at_command" >"$usb_interface"

    while IFS= read -r line <"$usb_interface"; do
        #while read line <$usb_interface; do

        if [[ "$line" == *COPS* ]]; then
            # Extract the part of the line after the last comma
            last_comma_part="${line##*,}"

            # Check if the extracted part contains 7 or 13
            if ! [[ "$last_comma_part" =~ (7|13) ]]; then
                cecho "red" "Error: AT+COPS response does not contain 7 or 13."
                exit 1
            fi
        fi
        #echo aa$response
        if [[ "$line" == *ERROR* ]]; then
            cecho "red" "Error Code: -1002, Invalid AT command"
            exit 1
        fi

        if [[ "$line" == *OK* ]]; then
            echo -e "$response" | grep -v 'OK' #Print the response without OK
            return 0
        fi

    done

    #If the loop finishes without finding OK, it means there was no OK, thus error.
    cecho "red" "Error: No OK response received for command: $at_command"
    exit 1
}

function PrepareDriver {

    local protocol=$1 # "mbim" or "qmi"
    if [ "$protocol" == "mbim" ]; then
        rv_name=("option" "usb_wwan" "usbserial" "cdc_mbim")
    elif [ "$protocol" == "qmi" ]; then
        drv_name=("option" "qmi_wwan")
    else
        cecho "red" "Error paramaters"
        exit 1
    fi

    ret=0
    for drv in ${drv_name[@]}; do
        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 CheckInterface {
    if ls "$1" >/dev/null 2>&1; then
        echo "$1"
    else
        cecho "red" "Error code: -1001, Can't find the $1" >&2
        echo ""
    fi
}

# Function to write network configuration
function write_network_config {
    local config_file=$1
    local apn=$2
    local apn_user=$3
    local apn_pass=$4
    local proxy=$5

    sudo tee $config_file >/dev/null <<EOF
APN=$apn
APN_USER=$apn_user
APN_PASS=$apn_pass
PROXY=$proxy
EOF
    if [ $? -ne 0 ]; then
        cecho "red" "Error writing to $config_file"
        exit 1
    fi
}

function install_if_missing {
    local package=$1
    local cmd=$2
    if ! type $cmd >/dev/null 2>&1; then
        cecho "blue" "Installing $package..."
        sudo apt-get install $package -y
    else
        cecho "blue" "$package is already installed."
    fi
}

function PrepareGetIP {
    local protocol=$1 # "mbim" or "qmi"
    local cdc_wdm_device
    local config_file
    local wwan0

    # Set config file and install utils based on protocol
    if [ "$protocol" == "mbim" ]; then
        config_file="/etc/mbim-network.conf"
        install_if_missing "libmbim-utils" "mbimcli"
    elif [ "$protocol" == "qmi" ]; then
        config_file="/etc/qmi-network.conf"
        install_if_missing "libqmi-utils" "qmicli"
    else
        cecho "red" "Invalid protocol: $protocol"
        exit 1
    fi

    # Install udhcpc
    install_if_missing "udhcpc" "udhcpc"

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

    # Write network configuration
    APN="INTERNET" #CHT
    APN_USER=""
    APN_PASS=""
    PROXY="yes"
    write_network_config "$config_file" "$APN" "$APN_USER" "$APN_PASS" "$PROXY"

    # Check if /dev/cdc-wdm exists
    if [ ! -c "$cdc_wdm_device" ]; then
        cecho "red" "Error Code: -1004, $cdc_wdm_device does not exist"
        exit 1
    fi

    # Protocol-specific configuration
    if [ "$protocol" == "mbim" ]; then
        # Find wwan interface
        flag=0
        for wwan in $(ls /sys/class/net); do
            if [ -d "/sys/class/net/$wwan/device/driver" ]; then
                driver=$(basename $(realpath /sys/class/net/$wwan/device/driver))
                if [ "$driver" == "cdc_mbim" ]; then
                    flag=1
                    wwan0=$wwan
                    break
                fi
            fi
        done

        if [ "$flag" -eq 0 ]; then
            cecho "red" "Error Code: -1005, can't find wwan interface."
            exit 1
        fi

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

    elif [ "$protocol" == "qmi" ]; then
        # Get wwan interface
        wwan0=$(sudo qmicli -d "$cdc_wdm_device" -p --get-wwan-iface)
        if [ $? -gt 0 ]; then
            cecho "red" "Error Code: -1003, invalid wwan0 interface"
            exit 1
        fi

        # Get IP and connect to the Internet
        sudo qmicli -d "$cdc_wdm_device" --dms-set-operating-mode='online'
        sudo ip link set "$wwan0" down
        sudo tee /sys/class/net/"$wwan0"/qmi/raw_ip <<<'Y'
        sudo ip link set "$wwan0" up
        sudo qmicli -d "$cdc_wdm_device" --wda-get-data-format
        sudo qmi-network "$cdc_wdm_device" start
        sudo udhcpc -i "$wwan0"
    fi
}

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
    -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:
sudo ./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
            ;;
        -t | --test)
            checkArgExit $@
            COUNTER=$2
            TEST=1
            shift 2
            ;;
        -v | --version)
            echo version="1.2"
            exit
            ;;
        *)
            echo "Unknown option $1" >&2
            echo >&2
            usageExit
            ;;
        esac
    done
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=$(CheckInterface $Devinterface)
if [ -z $dev ]; then
    exit 1
fi
echo $dev

SendCommand $dev "AT+COPS?\r"
if [ "$MBIM" -eq 1 ]; then
    cecho "blue" "mbim"
    PrepareGetIP "mbim"
    PrepareDriver "mbim"
elif [ "$QMI" -eq 1 ]; then
    cecho "blue" "qmi"
    PrepareGetIP "qmi"
    PrepareDriver "qmi"
fi
TestIP

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


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

沒有留言:

張貼留言