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