uqmi: dont use proto_block_restart
[openwrt.git] / package / network / utils / uqmi / files / lib / netifd / proto / qmi.sh
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . ../netifd-proto.sh
5 init_proto "$@"
6
7 proto_qmi_init_config() {
8         available=1
9         no_device=1
10         proto_config_add_string "device:device"
11         proto_config_add_string apn
12         proto_config_add_string auth
13         proto_config_add_string username
14         proto_config_add_string password
15         proto_config_add_string pincode
16         proto_config_add_string delay
17         proto_config_add_string modes
18 }
19
20 qmi_disconnect() {
21         # disable previous autoconnect state using the global handle
22         # do not reuse previous wds client id to prevent hangs caused by stale data
23         uqmi -s -d "$device" \
24                 --stop-network 0xffffffff \
25                 --autoconnect > /dev/null
26 }
27
28 qmi_wds_release() {
29         [ -n "$cid" ] || return 0
30
31         uqmi -s -d "$device" --set-client-id wds,"$cid" --release-client-id wds
32         uci_revert_state network $interface cid
33 }
34
35 proto_qmi_setup() {
36         local interface="$1"
37
38         local device apn auth username password pincode delay modes cid pdh
39         json_get_vars device apn auth username password pincode delay modes
40
41         [ -n "$device" ] || {
42                 echo "No control device specified"
43                 proto_notify_error "$interface" NO_DEVICE
44                 proto_set_available "$interface" 0
45                 return 1
46         }
47         [ -c "$device" ] || {
48                 echo "The specified control device does not exist"
49                 proto_notify_error "$interface" NO_DEVICE
50                 proto_set_available "$interface" 0
51                 return 1
52         }
53
54         devname="$(basename "$device")"
55         devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
56         ifname="$( ls "$devpath"/net )"
57         [ -n "$ifname" ] || {
58                 echo "The interface could not be found."
59                 proto_notify_error "$interface" NO_IFACE
60                 proto_set_available "$interface" 0
61                 return 1
62         }
63
64         [ -n "$delay" ] && sleep "$delay"
65
66         while uqmi -s -d "$device" --get-pin-status | grep '"UIM uninitialized"' > /dev/null; do
67                 sleep 1;
68         done
69
70         [ -n "$pincode" ] && {
71                 uqmi -s -d "$device" --verify-pin1 "$pincode" || {
72                         echo "Unable to verify PIN"
73                         proto_notify_error "$interface" PIN_FAILED
74                         proto_block_restart "$interface"
75                         return 1
76                 }
77         }
78
79         [ -n "$apn" ] || {
80                 echo "No APN specified"
81                 proto_notify_error "$interface" NO_APN
82                 return 1
83         }
84
85         qmi_disconnect
86
87         uqmi -s -d "$device" --set-data-format 802.3
88
89         echo "Waiting for network registration"
90         while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
91                 sleep 5;
92         done
93
94         [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes"
95
96         echo "Starting network $apn"
97         cid=`uqmi -s -d "$device" --get-client-id wds`
98         [ $? -ne 0 ] && {
99                 echo "Unable to obtain client ID"
100                 proto_notify_error "$interface" NO_CID
101                 return 1
102         }
103
104         uqmi -s -d "$device" --set-client-id wds,"$cid" \
105                 --start-network "$apn" \
106                 ${auth:+--auth-type $auth} \
107                 ${username:+--username $username} \
108                 ${password:+--password $password} \
109                 --autoconnect > /dev/null
110
111         echo "Starting DHCP"
112         proto_init_update "$ifname" 1
113         proto_send_update "$interface"
114
115         json_init
116         json_add_string name "${interface}_dhcp"
117         json_add_string ifname "@$interface"
118         json_add_string proto "dhcp"
119         json_close_object
120         ubus call network add_dynamic "$(json_dump)"
121
122         json_init
123         json_add_string name "${interface}_dhcpv6"
124         json_add_string ifname "@$interface"
125         json_add_string proto "dhcpv6"
126         json_close_object
127         ubus call network add_dynamic "$(json_dump)"
128 }
129
130 proto_qmi_teardown() {
131         local interface="$1"
132
133         local device
134         json_get_vars device
135         local cid=$(uci_get_state network $interface cid)
136
137         echo "Stopping network"
138         qmi_disconnect
139         qmi_wds_release
140
141         proto_init_update "*" 0
142         proto_send_update "$interface"
143 }
144
145 add_protocol qmi
146