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