ec33baacb0992fa968920fc8744938e16268a73d
[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_block_restart "$interface"
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_block_restart "$interface"
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_block_restart "$interface"
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                 proto_block_restart "$interface"
83                 return 1
84         }
85
86         qmi_disconnect
87
88         uqmi -s -d "$device" --set-data-format 802.3
89
90         echo "Waiting for network registration"
91         while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
92                 sleep 5;
93         done
94
95         [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes"
96
97         echo "Starting network $apn"
98         cid=`uqmi -s -d "$device" --get-client-id wds`
99         [ $? -ne 0 ] && {
100                 echo "Unable to obtain client ID"
101                 proto_notify_error "$interface" NO_CID
102                 proto_block_restart "$interface"
103                 return 1
104         }
105
106         uqmi -s -d "$device" --set-client-id wds,"$cid" \
107                 --start-network "$apn" \
108                 ${auth:+--auth-type $auth} \
109                 ${username:+--username $username} \
110                 ${password:+--password $password} \
111                 --autoconnect > /dev/null
112
113         echo "Starting DHCP"
114         proto_init_update "$ifname" 1
115         proto_send_update "$interface"
116
117         json_init
118         json_add_string name "${interface}_dhcp"
119         json_add_string ifname "@$interface"
120         json_add_string proto "dhcp"
121         json_close_object
122         ubus call network add_dynamic "$(json_dump)"
123
124         json_init
125         json_add_string name "${interface}_dhcpv6"
126         json_add_string ifname "@$interface"
127         json_add_string proto "dhcpv6"
128         json_close_object
129         ubus call network add_dynamic "$(json_dump)"
130 }
131
132 proto_qmi_teardown() {
133         local interface="$1"
134
135         local device
136         json_get_vars device
137         local cid=$(uci_get_state network $interface cid)
138
139         echo "Stopping network"
140         qmi_disconnect
141         qmi_wds_release
142
143         proto_init_update "*" 0
144         proto_send_update "$interface"
145 }
146
147 add_protocol qmi
148