umbim: fix return value of proto_mbim_setup()
[openwrt/openwrt.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 proto_notify_error "$interface" PIN_FAILED
69 return 1
70 }
71 tid=$((tid + 1))
72
73 [ "$pincode" ] && {
74 echo "mbim[$$]" "Sending pin"
75 umbim $DBG -n -t $tid -d $device unlock "$pincode" || {
76 echo "mbim[$$]" "Unable to verify PIN"
77 proto_notify_error "$interface" PIN_FAILED
78 proto_block_restart "$interface"
79 return 1
80 }
81 }
82 tid=$((tid + 1))
83
84 echo "mbim[$$]" "Checking pin"
85 umbim $DBG -n -t $tid -d $device pinstate || {
86 echo "mbim[$$]" "PIN required"
87 proto_notify_error "$interface" PIN_FAILED
88 proto_block_restart "$interface"
89 return 1
90 }
91 tid=$((tid + 1))
92
93 echo "mbim[$$]" "Checking subscriber"
94 umbim $DBG -n -t $tid -d $device subscriber || {
95 echo "mbim[$$]" "Subscriber init failed"
96 proto_notify_error "$interface" NO_SUBSCRIBER
97 return 1
98 }
99 tid=$((tid + 1))
100
101 echo "mbim[$$]" "Register with network"
102 umbim $DBG -n -t $tid -d $device registration || {
103 echo "mbim[$$]" "Subscriber registration failed"
104 proto_notify_error "$interface" NO_REGISTRATION
105 return 1
106 }
107 tid=$((tid + 1))
108
109 echo "mbim[$$]" "Attach to network"
110 umbim $DBG -n -t $tid -d $device attach || {
111 echo "mbim[$$]" "Failed to attach to network"
112 proto_notify_error "$interface" ATTACH_FAILED
113 return 1
114 }
115 tid=$((tid + 1))
116
117 echo "mbim[$$]" "Connect to network"
118 while ! umbim $DBG -n -t $tid -d $device connect "$apn" "$auth" "$username" "$password"; do
119 tid=$((tid + 1))
120 sleep 1;
121 done
122 tid=$((tid + 1))
123
124 uci_set_state network $interface tid "$tid"
125
126 echo "mbim[$$]" "Connected, starting DHCP"
127 proto_init_update "$ifname" 1
128 proto_send_update "$interface"
129
130 json_init
131 json_add_string name "${interface}_4"
132 json_add_string ifname "@$interface"
133 json_add_string proto "dhcp"
134 proto_add_dynamic_defaults
135 json_close_object
136 ubus call network add_dynamic "$(json_dump)"
137
138 json_init
139 json_add_string name "${interface}_6"
140 json_add_string ifname "@$interface"
141 json_add_string proto "dhcpv6"
142 json_add_string extendprefix 1
143 proto_add_dynamic_defaults
144 ubus call network add_dynamic "$(json_dump)"
145 }
146
147 proto_mbim_setup() {
148 local ret
149
150 _proto_mbim_setup $@
151 ret=$?
152
153 [ "$ret" = 0 ] || {
154 logger "mbim bringup failed, retry in 15s"
155 sleep 15
156 }
157
158 return $ret
159 }
160
161 proto_mbim_teardown() {
162 local interface="$1"
163
164 local device
165 json_get_vars device
166 local tid=$(uci_get_state network $interface tid)
167
168 [ -n "$ctl_device" ] && device=$ctl_device
169
170 echo "mbim[$$]" "Stopping network"
171 [ -n "$tid" ] && {
172 umbim $DBG -t$tid -d "$device" disconnect
173 uci_revert_state network $interface tid
174 }
175
176 proto_init_update "*" 0
177 proto_send_update "$interface"
178 }
179
180 [ -n "$INCLUDE_ONLY" ] || add_protocol mbim