uqmi: use the autoconnect feature
[openwrt/staging/jow.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 proto_qmi_setup() {
29 local interface="$1"
30
31 local device apn auth username password pincode delay modes cid pdh
32 json_get_vars device apn auth username password pincode delay modes
33
34 [ -n "$device" ] || {
35 echo "No control device specified"
36 proto_notify_error "$interface" NO_DEVICE
37 proto_block_restart "$interface"
38 return 1
39 }
40 [ -c "$device" ] || {
41 echo "The specified control device does not exist"
42 proto_notify_error "$interface" NO_DEVICE
43 proto_block_restart "$interface"
44 return 1
45 }
46
47 devname="$(basename "$device")"
48 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
49 ifname="$( ls "$devpath"/net )"
50 [ -n "$ifname" ] || {
51 echo "The interface could not be found."
52 proto_notify_error "$interface" NO_IFACE
53 proto_block_restart "$interface"
54 return 1
55 }
56
57 [ -n "$delay" ] && sleep "$delay"
58
59 while uqmi -s -d "$device" --get-pin-status | grep '"UIM uninitialized"' > /dev/null; do
60 sleep 1;
61 done
62
63 [ -n "$pincode" ] && {
64 uqmi -s -d "$device" --verify-pin1 "$pincode" || {
65 echo "Unable to verify PIN"
66 proto_notify_error "$interface" PIN_FAILED
67 proto_block_restart "$interface"
68 return 1
69 }
70 }
71
72 [ -n "$apn" ] || {
73 echo "No APN specified"
74 proto_notify_error "$interface" NO_APN
75 proto_block_restart "$interface"
76 return 1
77 }
78
79 qmi_disconnect
80
81 echo "Waiting for network registration"
82 while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
83 sleep 5;
84 done
85
86 [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes"
87
88 echo "Starting network $apn"
89 cid=`uqmi -s -d "$device" --get-client-id wds`
90 [ $? -ne 0 ] && {
91 echo "Unable to obtain client ID"
92 proto_notify_error "$interface" NO_CID
93 proto_block_restart "$interface"
94 return 1
95 }
96
97 uqmi -s -d "$device" --set-client-id wds,"$cid" \
98 --start-network "$apn" \
99 ${auth:+--auth-type $auth} \
100 ${username:+--username $username} \
101 ${password:+--password $password} \
102 --autoconnect > /dev/null
103
104 if ! uqmi -s -d "$device" --get-data-status | grep '"connected"' > /dev/null; then
105 echo "Connection lost"
106 proto_notify_error "$interface" NOT_CONNECTED
107 return 1
108 fi
109
110 echo "Connected, starting DHCP"
111 proto_init_update "$ifname" 1
112 proto_send_update "$interface"
113
114 json_init
115 json_add_string name "${interface}_dhcp"
116 json_add_string ifname "@$interface"
117 json_add_string proto "dhcp"
118 json_close_object
119 ubus call network add_dynamic "$(json_dump)"
120
121 json_init
122 json_add_string name "${interface}_dhcpv6"
123 json_add_string ifname "@$interface"
124 json_add_string proto "dhcpv6"
125 json_close_object
126 ubus call network add_dynamic "$(json_dump)"
127 }
128
129 proto_qmi_teardown() {
130 local interface="$1"
131
132 local device
133 json_get_vars device
134 local cid=$(uci_get_state network $interface cid)
135
136 echo "Stopping network"
137 qmi_disconnect
138 [ -n "$cid" ] && {
139 uqmi -s -d "$device" --set-client-id wds,"$cid" --release-client-id wds
140 uci_revert_state network $interface cid
141 }
142
143 proto_init_update "*" 0
144 proto_send_update "$interface"
145 }
146
147 add_protocol qmi
148