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