umbim: explicitly check for PIN1 state
[openwrt/staging/hauke.git] / package / network / utils / umbim / files / lib / netifd / proto / mbim.sh
1 #!/bin/sh
2
3 [ -n "$INCLUDE_ONLY" ] || {
4 . /lib/functions.sh
5 . ../netifd-proto.sh
6 init_proto "$@"
7 }
8 #DBG=-v
9
10 proto_mbim_init_config() {
11 available=1
12 no_device=1
13 proto_config_add_string "device:device"
14 proto_config_add_string apn
15 proto_config_add_string pincode
16 proto_config_add_string delay
17 proto_config_add_string auth
18 proto_config_add_string username
19 proto_config_add_string password
20 proto_config_add_defaults
21 }
22
23 _proto_mbim_setup() {
24 local interface="$1"
25 local tid=2
26 local ret
27
28 local device apn pincode delay $PROTO_DEFAULT_OPTIONS
29 json_get_vars device apn pincode delay auth username password $PROTO_DEFAULT_OPTIONS
30
31 [ -n "$ctl_device" ] && device=$ctl_device
32
33 [ -n "$device" ] || {
34 echo "mbim[$$]" "No control device specified"
35 proto_notify_error "$interface" NO_DEVICE
36 proto_set_available "$interface" 0
37 return 1
38 }
39 [ -c "$device" ] || {
40 echo "mbim[$$]" "The specified control device does not exist"
41 proto_notify_error "$interface" NO_DEVICE
42 proto_set_available "$interface" 0
43 return 1
44 }
45
46 devname="$(basename "$device")"
47 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
48 ifname="$( ls "$devpath"/net )"
49
50 [ -n "$ifname" ] || {
51 echo "mbim[$$]" "Failed to find matching interface"
52 proto_notify_error "$interface" NO_IFNAME
53 proto_set_available "$interface" 0
54 return 1
55 }
56
57 [ -n "$apn" ] || {
58 echo "mbim[$$]" "No APN specified"
59 proto_notify_error "$interface" NO_APN
60 return 1
61 }
62
63 [ -n "$delay" ] && sleep "$delay"
64
65 echo "mbim[$$]" "Reading capabilities"
66 umbim $DBG -n -d $device caps || {
67 echo "mbim[$$]" "Failed to read modem caps"
68 tid=$((tid + 1))
69 umbim $DBG -t $tid -d "$device" disconnect
70 proto_notify_error "$interface" PIN_FAILED
71 return 1
72 }
73 tid=$((tid + 1))
74
75 [ "$pincode" ] && {
76 echo "mbim[$$]" "Sending pin"
77 umbim $DBG -n -t $tid -d $device unlock "$pincode" || {
78 echo "mbim[$$]" "Unable to verify PIN"
79 tid=$((tid + 1))
80 umbim $DBG -t $tid -d "$device" disconnect
81 proto_notify_error "$interface" PIN_FAILED
82 proto_block_restart "$interface"
83 return 1
84 }
85 }
86 tid=$((tid + 1))
87
88 echo "mbim[$$]" "Checking pin"
89 umbim $DBG -n -t $tid -d $device pinstate
90 [ $? -eq 2 ] && {
91 echo "mbim[$$]" "PIN required"
92 tid=$((tid + 1))
93 umbim $DBG -t $tid -d "$device" disconnect
94 proto_notify_error "$interface" PIN_FAILED
95 proto_block_restart "$interface"
96 return 1
97 }
98 tid=$((tid + 1))
99
100 echo "mbim[$$]" "Checking subscriber"
101 umbim $DBG -n -t $tid -d $device subscriber || {
102 echo "mbim[$$]" "Subscriber init failed"
103 tid=$((tid + 1))
104 umbim $DBG -t $tid -d "$device" disconnect
105 proto_notify_error "$interface" NO_SUBSCRIBER
106 return 1
107 }
108 tid=$((tid + 1))
109
110 echo "mbim[$$]" "Register with network"
111 umbim $DBG -n -t $tid -d $device registration || {
112 echo "mbim[$$]" "Subscriber registration failed"
113 tid=$((tid + 1))
114 umbim $DBG -t $tid -d "$device" disconnect
115 proto_notify_error "$interface" NO_REGISTRATION
116 return 1
117 }
118 tid=$((tid + 1))
119
120 echo "mbim[$$]" "Attach to network"
121 umbim $DBG -n -t $tid -d $device attach || {
122 echo "mbim[$$]" "Failed to attach to network"
123 tid=$((tid + 1))
124 umbim $DBG -t $tid -d "$device" disconnect
125 proto_notify_error "$interface" ATTACH_FAILED
126 return 1
127 }
128 tid=$((tid + 1))
129
130 echo "mbim[$$]" "Connect to network"
131 while ! umbim $DBG -n -t $tid -d $device connect "$apn" "$auth" "$username" "$password"; do
132 tid=$((tid + 1))
133 sleep 1;
134 done
135 tid=$((tid + 1))
136
137 uci_set_state network $interface tid "$tid"
138
139 echo "mbim[$$]" "Connected, starting DHCP"
140 proto_init_update "$ifname" 1
141 proto_send_update "$interface"
142
143 json_init
144 json_add_string name "${interface}_4"
145 json_add_string ifname "@$interface"
146 json_add_string proto "dhcp"
147 proto_add_dynamic_defaults
148 json_close_object
149 ubus call network add_dynamic "$(json_dump)"
150
151 json_init
152 json_add_string name "${interface}_6"
153 json_add_string ifname "@$interface"
154 json_add_string proto "dhcpv6"
155 json_add_string extendprefix 1
156 proto_add_dynamic_defaults
157 ubus call network add_dynamic "$(json_dump)"
158 }
159
160 proto_mbim_setup() {
161 local ret
162
163 _proto_mbim_setup $@
164 ret=$?
165
166 [ "$ret" = 0 ] || {
167 logger "mbim bringup failed, retry in 15s"
168 sleep 15
169 }
170
171 return $ret
172 }
173
174 proto_mbim_teardown() {
175 local interface="$1"
176
177 local device
178 json_get_vars device
179 local tid=$(uci_get_state network $interface tid)
180
181 [ -n "$ctl_device" ] && device=$ctl_device
182
183 echo "mbim[$$]" "Stopping network"
184 [ -n "$tid" ] && {
185 umbim $DBG -t $tid -d "$device" disconnect
186 uci_revert_state network $interface tid
187 }
188
189 proto_init_update "*" 0
190 proto_send_update "$interface"
191 }
192
193 [ -n "$INCLUDE_ONLY" ] || add_protocol mbim