uqmi: fix network registration loop
[openwrt/staging/zorun.git] / package / network / utils / uqmi / files / lib / netifd / proto / qmi.sh
index a6c785eb563d680b0ce672cb82477b7dc4fdf69c..c0134f44dde3e5755456744c7f083013f8580ac9 100755 (executable)
@@ -209,19 +209,36 @@ proto_qmi_setup() {
 
        uqmi -s -d "$device" --sync > /dev/null 2>&1
 
+       uqmi -s -d "$device" --network-register > /dev/null 2>&1
+
        echo "Waiting for network registration"
+       sleep 1
        local registration_timeout=0
-       while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
-               [ -e "$device" ] || return 1
-               if [ "$registration_timeout" -lt "$timeout" -o "$timeout" = "0" ]; then
-                       let registration_timeout++
-                       sleep 1;
+       local registration_state=""
+       while true; do
+               registration_state=$(uqmi -s -d "$device" --get-serving-system 2>/dev/null | jsonfilter -e "@.registration" 2>/dev/null)
+
+               [ "$registration_state" = "registered" ] && break
+
+               if [ "$registration_state" = "searching" ] || [ "$registration_state" = "not_registered" ]; then
+                       if [ "$registration_timeout" -lt "$timeout" ] || [ "$timeout" = "0" ]; then
+                               [ "$registration_state" = "searching" ] || {
+                                       echo "Device stopped network registration. Restart network registration"
+                                       uqmi -s -d "$device" --network-register > /dev/null 2>&1
+                               }
+                               let registration_timeout++
+                               sleep 1
+                               continue
+                       fi
+                       echo "Network registration failed, registration timeout reached"
                else
-                       echo "Network registration failed"
-                       proto_notify_error "$interface" NETWORK_REGISTRATION_FAILED
-                       proto_block_restart "$interface"
-                       return 1
+                       # registration_state is 'registration_denied' or 'unknown' or ''
+                       echo "Network registration failed (reason: '$registration_state')"
                fi
+
+               proto_notify_error "$interface" NETWORK_REGISTRATION_FAILED
+               proto_block_restart "$interface"
+               return 1
        done
 
        [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes" > /dev/null 2>&1