Created
September 29, 2021 12:43
-
-
Save bjoern-r/400f8aade73f682c0b99566a117a3bc9 to your computer and use it in GitHub Desktop.
Script to connect a Quectel 5G modem with qmicli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set traps to catch exite signals | |
trap stopservice SIGINT | |
trap stopservice SIGKILL | |
trap stopservice SIGTERM | |
function stopservice { | |
if [[ ! -z ${DATA_HANDLE} && ! -z ${CLIENT_CID} ]] | |
then | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Terminating 5G-Network: PACKET DATA HANDLE - > ${DATA_HANDLE} and CLIENT CID -> ${CLIENT_CID}" | |
ip addr flush dev wwan0 | |
qmicli --device /dev/cdc-wdm0 --device-open-proxy --wds-stop-network=${DATA_HANDLE} --client-cid=${CLIENT_CID} | |
else | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Terminating 5G-Network: no PACKET DATA HANDLE and no CLIENT CID" | |
fi | |
exit 0 | |
} | |
# Check if interface exists | |
if [ ! -e /dev/cdc-wdm0 ] | |
then | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Error - interface cdc-wdm0 not found!..." | |
exit 1 | |
fi | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Found interface cdc-wdm0." | |
# Check if configuration file exists | |
if [ ! -f /etc/default/5g-network ] | |
then | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Error - Configuration file /etc/default/5g-network not found!..." | |
exit 1 | |
fi | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Found configuration file(/etc/default/5g-network)." | |
# Import variables from configuration file | |
source /etc/default/5g-network | |
# Start service loop | |
while true | |
do | |
# Get SIM status | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Querying SIM card 1." | |
CARD_STATUS=( $(qmicli -d /dev/cdc-wdm0 --device-open-proxy --uim-get-slot-status | grep -A 1 "Physical slot 1" | grep "Card status" | awk '{ print $3 }') ) | |
while [[ $CARD_STATUS != "present" ]] | |
do | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - SIM card not detected!" | |
echo "${LOG_DATE[@]} - SIM card not detected - powering off SIM 1." | |
qmicli -d /dev/cdc-wdm0 --device-open-proxy --uim-sim-power-off=1 | |
sleep 1 | |
echo "${LOG_DATE[@]} - SIM card not detected - powering on SIM 1." | |
qmicli -d /dev/cdc-wdm0 --device-open-proxy --uim-sim-power-on=1 | |
sleep 1 | |
CARD_STATUS=( $(qmicli -d /dev/cdc-wdm0 --device-open-proxy --uim-get-slot-status | grep -A 1 "Physical slot 1" | grep "Card status" | awk '{ print $3 }') ) | |
sleep 5 | |
done | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - SIM card detected - SIM 1" | |
# Get IP configuration form interface wwan0 | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Querying raw_ip on interface wwan0." | |
RAW_IP=( $(cat /sys/class/net/wwan0/qmi/raw_ip) ) | |
if [[ $RAW_IP != 'Y' ]] | |
then | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - RAW_IP not set on interface wwan0!" | |
echo "${LOG_DATE[@]} - Setting raw_ip on interface wwan0." | |
ip link set dev wwan0 down | |
echo Y > /sys/class/net/wwan0/qmi/raw_ip | |
ip link set dev wwan0 up | |
else | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - RAW_IP set on interface wwan0." | |
fi | |
ip link set wwan0 mtu 1400 | |
if [ ! -z $PIN ] | |
then | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - PIN configuration detected, setting PIN" | |
qmicli --device=/dev/cdc-wdm0 --device-open-proxy --uim-verify-pin=PIN1,$PIN | |
fi | |
qmicli --device /dev/cdc-wdm0 --device-open-proxy --wds-set-ip-family=4 | |
if [ ! -z $APN ] | |
then | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - APN configuration found, APN = ${APN}" | |
QMI_RESULT=( $(qmicli --device /dev/cdc-wdm0 --device-open-proxy --wds-start-network="${APN}" --client-no-release-cid) ) | |
else | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - APN configuration not found, trying APN = internet" | |
QMI_RESULT=( $(qmicli --device /dev/cdc-wdm0 --device-open-proxy --wds-start-network="internet" --client-no-release-cid) ) | |
fi | |
if [ ! -z ${QMI_RESULT} ] | |
then | |
DATA_HANDLE=( $(echo ${QMI_RESULT[6]} | cut -d "'" -f 2) ) | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Got PACKET DATA HANDLE - > ${DATA_HANDLE}" | |
CLIENT_CID=( $(echo ${QMI_RESULT[15]} | cut -d "'" -f 2) ) | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Got CLIENT CID -> ${CLIENT_CID}" | |
fi | |
udhcpc -q -f -i wwan0 | |
UNREACHEABLE=0 | |
if [ ! -z $NET_TEST ] | |
then | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Network test configuration found destination ${NET_TEST}." | |
while [[ "$UNREACHEABLE" -eq "0" ]] | |
do | |
ping -q -c 1 $NET_TEST &> /dev/null | |
UNREACHEABLE=$? | |
sleep 5 | |
done | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Network error - ${NET_TEST} unreachable." | |
if [[ ! -z ${DATA_HANDLE} && ! -z ${CLIENT_CID} ]] | |
then | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Terminating 5G-Network: PACKET DATA HANDLE - > ${DATA_HANDLE} and CLIENT CID -> ${CLIENT_CID}" | |
qmicli --device /dev/cdc-wdm0 --device-open-proxy --wds-stop-network=${DATA_HANDLE} --client-cid=${CLIENT_CID} | |
ip addr flush dev wwan0 | |
else | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - Terminating 5G-Network: no PACKET DATA HANDLE and no CLIENT CID" | |
fi | |
else | |
while [ $CARD_STATUS == "present" ] | |
do | |
CARD_STATUS=( $(qmicli -d /dev/cdc-wdm0 --device-open-proxy --uim-get-slot-status --client-cid=${CLIENT_CID} | grep -A 1 "Physical slot 1" | grep "Card status" | awk '{ print $3 }') ) | |
sleep 5 | |
done | |
LOG_DATE=( $(date +"%Y %b %H:%M") ) | |
echo "${LOG_DATE[@]} - SIM card not detected - SIM 1 has gone away." | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment