uqmi: do not block proto handler if modem is unable to registrate
[openwrt/openwrt.git] / package / network / utils / uqmi / files / lib / netifd / proto / qmi.sh
index c60cae653a4296611b853c1d7f705c76895d2fcc..f3c548757419127d1ae8e7dece9faeae26a6148f 100755 (executable)
@@ -22,17 +22,20 @@ proto_qmi_init_config() {
        proto_config_add_boolean dhcpv6
        proto_config_add_boolean autoconnect
        proto_config_add_int plmn
+       proto_config_add_int timeout
        proto_config_add_defaults
 }
 
 proto_qmi_setup() {
        local interface="$1"
        local dataformat connstat
-       local device apn auth username password pincode delay modes pdptype profile dhcpv6 autoconnect plmn $PROTO_DEFAULT_OPTIONS
+       local device apn auth username password pincode delay modes pdptype profile dhcpv6 autoconnect plmn timeout $PROTO_DEFAULT_OPTIONS
        local ip4table ip6table
        local cid_4 pdh_4 cid_6 pdh_6
        local ip_6 ip_prefix_length gateway_6 dns1_6 dns2_6
-       json_get_vars device apn auth username password pincode delay modes pdptype profile dhcpv6 autoconnect plmn ip4table ip6table $PROTO_DEFAULT_OPTIONS
+       json_get_vars device apn auth username password pincode delay modes pdptype profile dhcpv6 autoconnect plmn ip4table ip6table timeout $PROTO_DEFAULT_OPTIONS
+
+       [ "$timeout" = "" ] && timeout="10"
 
        [ "$metric" = "" ] && metric="0"
 
@@ -90,7 +93,7 @@ proto_qmi_setup() {
                        mnc=${plmn:3}
                        echo "Setting PLMN to $plmn"
                fi
-               uqmi -s -d "$device" --set-plmn --mcc "$mcc" --mnc "$mnc" || {
+               uqmi -s -d "$device" --set-plmn --mcc "$mcc" --mnc "$mnc" > /dev/null 2>&1 || {
                        echo "Unable to set PLMN"
                        proto_notify_error "$interface" PLMN_FAILED
                        proto_block_restart "$interface"
@@ -99,11 +102,11 @@ proto_qmi_setup() {
        }
 
        # Cleanup current state if any
-       uqmi -s -d "$device" --stop-network 0xffffffff --autoconnect
+       uqmi -s -d "$device" --stop-network 0xffffffff --autoconnect > /dev/null 2>&1
 
        # Set IP format
-       uqmi -s -d "$device" --set-data-format 802.3
-       uqmi -s -d "$device" --wda-set-data-format 802.3
+       uqmi -s -d "$device" --set-data-format 802.3 > /dev/null 2>&1
+       uqmi -s -d "$device" --wda-set-data-format 802.3 > /dev/null 2>&1
        dataformat="$(uqmi -s -d "$device" --wda-get-data-format)"
 
        if [ "$dataformat" = '"raw-ip"' ]; then
@@ -117,15 +120,24 @@ proto_qmi_setup() {
                echo "Y" > /sys/class/net/$ifname/qmi/raw_ip
        fi
 
-       uqmi -s -d "$device" --sync
+       uqmi -s -d "$device" --sync > /dev/null 2>&1
 
        echo "Waiting for network registration"
+       local registration_timeout=0
        while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
                [ -e "$device" ] || return 1
-               sleep 5;
+               if [ "$registration_timeout" -lt "$timeout" ]; then
+                       let registration_timeout++
+                       sleep 1;
+               else
+                       echo "Network registration failed"
+                       proto_notify_error "$interface" NETWORK_REGISTRATION_FAILED
+                       proto_block_restart "$interface"
+                       return 1
+               fi
        done
 
-       [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes"
+       [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes" > /dev/null 2>&1
 
        echo "Starting network $interface"
 
@@ -147,7 +159,7 @@ proto_qmi_setup() {
                        return 1
                fi
 
-               uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null
+               uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null 2>&1
 
                pdh_4=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
                        --start-network \
@@ -158,22 +170,22 @@ proto_qmi_setup() {
                        ${password:+--password $password} \
                        ${autoconnect:+--autoconnect})
 
-        # pdh_4 is a numeric value on success
+               # pdh_4 is a numeric value on success
                if ! [ "$pdh_4" -eq "$pdh_4" ] 2> /dev/null; then
                        echo "Unable to connect IPv4"
-                       uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds
+                       uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
                        proto_notify_error "$interface" CALL_FAILED
                        return 1
                fi
 
-        # Check data connection state
+               # Check data connection state
                connstat=$(uqmi -s -d "$device" --get-data-status)
-                [ "$connstat" == '"connected"' ] || {
-                        echo "No data link!"
-                        uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds
-                        proto_notify_error "$interface" CALL_FAILED
-                        return 1
-                }
+               [ "$connstat" == '"connected"' ] || {
+                       echo "No data link!"
+                       uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
+                       proto_notify_error "$interface" CALL_FAILED
+                       return 1
+               }
        }
 
        [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
@@ -184,7 +196,7 @@ proto_qmi_setup() {
                        return 1
                fi
 
-               uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null
+               uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null 2>&1
 
                pdh_6=$(uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
                        --start-network \
@@ -195,22 +207,22 @@ proto_qmi_setup() {
                        ${password:+--password $password} \
                        ${autoconnect:+--autoconnect})
 
-        # pdh_6 is a numeric value on success
+               # pdh_6 is a numeric value on success
                if ! [ "$pdh_6" -eq "$pdh_6" ] 2> /dev/null; then
                        echo "Unable to connect IPv6"
-                       uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds
+                       uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
                        proto_notify_error "$interface" CALL_FAILED
                        return 1
                fi
 
-        # Check data connection state
+               # Check data connection state
                connstat=$(uqmi -s -d "$device" --get-data-status)
-                [ "$connstat" == '"connected"' ] || {
-                        echo "No data link!"
-                        uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds
-                        proto_notify_error "$interface" CALL_FAILED
-                        return 1
-                }
+               [ "$connstat" == '"connected"' ] || {
+                       echo "No data link!"
+                       uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
+                       proto_notify_error "$interface" CALL_FAILED
+                       return 1
+               }
        }
 
        echo "Setting up $ifname"