RFC: Script for Quectel EC20 lte reconnection on Omnia

Hi all, just a regular user not an expert here.
Due to zero support from CZ.nic regarding LTE issues I have been struggling for long to make omnia with EC20 stable or at least able to reconnect if Lte connection fails.
My experience with the buggy quectel EC20 modem is that disconnections (without reconnections) on omnia may be usually traced at:

  1. Provider issues that just need to reset connection
  2. QMI protocol freezes
  3. Sim card gets powered off
  4. Modem disconnects from usb (and reconnects after a while)

For me, during the previous 15 days sim card getting powered off and usb disconnections were the most usual issues.
So I wrote a script to bypass the buggy quectel EC20 modem issues and @Nowaker suggested some improvements.

Using the script (without the try_many function) seems to work decently in most cases. I experienced a long disconnection once, caused by QMI protocol freeze, that needed to reset the modem a few times before reconnection.

I use rc.local to start the script at boot using a second script that kills other instances (no experience how to use traps) if running (though if started manually two instances may be running at the same time).

Any suggestion for improvement is welcome.

rc.local:

Summary

(sleep 60 && /usr/bin/nohup /bin/sh /root/start_modem_checks.sh </dev/null >/dev/null 2>&1) &

start script:

Summary
#!/bin/sh
    while true; do
    	/bin/pgrep -f lte_checks | xargs kill >/dev/null 2>&1
    	/bin/pgrep -f qmicli | xargs kill >/dev/null 2>&1
    	/bin/pgrep -f uqmi | xargs kill >/dev/null 2>&1
    	sleep 2
    	/bin/sh /root/lte_check.sh
    	sleep 300
    done

script:

Summary
#!/bin/sh

#References:
#https://www.grymoire.com/Unix/Sh.html
#https://forum.test.turris.cz/t/several-questions-about-omnia-mostly-about-lte-modules-and-omnia-ui-in-comparison-to-mofi-4500/13477/42
#https://forum.openwrt.org/t/if-wan-connection-lost-restart-usb-wan-device/7819/16
#https://forum.openwrt.org/t/4g-lte-does-not-retry-to-connect-after-a-loss-of-connection/34720/6

try_many() {
  for i in 1 2 3; do
    $@ && return 1
  done
  return 0
}

#Query DNS server and exit on success
[ "$(dig +short myDDNSname.duckdns.org)" ] && { logger "Querying lte DNS"; exit 1; }

#Failed DNS query trigers ping to DNS server twice through Lte, should ping also fail, restart the interface, test ip, protocol and card state
/usr/bin/ping -c2 -w 5 -I wwan0 8.8.8.8 >/dev/null 2>&1 && { logger "Lte DNS ping success"; exit 2; } || { ifup Lte; sleep 10; logger "Ping failed"; }

#Check IPv4 and exit on success
/usr/bin/qmicli -d /dev/cdc-wdm0 --wds-get-current-settings >/dev/null 2>&1 && { logger "IPv4 present"; exit 3; }

#Qmi protocol test, if it fails use AT command to reset the modem and bring up the interface
try_many /usr/bin/qmicli -d /dev/cdc-wdm0 --dms-uim-get-state >/dev/null 2>&1 && { logger "Qmi proto test success"; } || { ifdown Lte; sleep 2; echo -e "AT+CFUN=1,1" > /dev/ttyUSB2; sleep 22; ifup Lte; sleep 10; }

# Sim card power state test, if it fails power on sim card and bring up the interface
/usr/bin/qmicli -d /dev/cdc-wdm0 --uim-get-card-status | grep "Card state: 'present'" >/dev/null 2>&1 && { logger "Sim test success"; exit 5; } || { ifdown Lte; sleep 2; /usr/bin/qmicli -d /dev/cdc-wdm0 --uim-sim-power-on=1 >/dev/null 2>&1; sleep 23; ifup Lte; logger "Powering on Sim"; }

exit 0
1 Like