network: also shorten virtual interface names of ppp and 3g/4g connections
[openwrt/staging/chunkeey.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 }
21
22 proto_mbim_setup() {
23 local interface="$1"
24 local tid=2
25 local ret
26
27 local device apn pincode delay
28 json_get_vars device apn pincode delay auth username password
29
30 [ -n "$ctl_device" ] && device=$ctl_device
31
32 [ -n "$device" ] || {
33 echo "mbim[$$]" "No control device specified"
34 proto_notify_error "$interface" NO_DEVICE
35 proto_set_available "$interface" 0
36 return 1
37 }
38 [ -c "$device" ] || {
39 echo "mbim[$$]" "The specified control device does not exist"
40 proto_notify_error "$interface" NO_DEVICE
41 proto_set_available "$interface" 0
42 return 1
43 }
44
45 devname="$(basename "$device")"
46 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
47 ifname="$( ls "$devpath"/net )"
48
49 [ -n "$ifname" ] || {
50 echo "mbim[$$]" "Failed to find matching interface"
51 proto_notify_error "$interface" NO_IFNAME
52 proto_set_available "$interface" 0
53 return 1
54 }
55
56 [ -n "$apn" ] || {
57 echo "mbim[$$]" "No APN specified"
58 proto_notify_error "$interface" NO_APN
59 return 1
60 }
61
62 [ -n "$delay" ] && sleep "$delay"
63
64 echo "mbim[$$]" "Reading capabilities"
65 umbim $DBG -n -d $device caps || {
66 echo "mbim[$$]" "Failed to read modem caps"
67 proto_notify_error "$interface" PIN_FAILED
68 return 1
69 }
70 tid=$((tid + 1))
71
72 [ "$pincode" ] && {
73 echo "mbim[$$]" "Sending pin"
74 umbim $DBG -n -t $tid -d $device unlock "$pincode" || {
75 echo "mbim[$$]" "Unable to verify PIN"
76 proto_notify_error "$interface" PIN_FAILED
77 proto_block_restart "$interface"
78 return 1
79 }
80 }
81 tid=$((tid + 1))
82
83 echo "mbim[$$]" "Checking pin"
84 umbim $DBG -n -t $tid -d $device pinstate || {
85 echo "mbim[$$]" "PIN required"
86 proto_notify_error "$interface" PIN_FAILED
87 proto_block_restart "$interface"
88 return 1
89 }
90 tid=$((tid + 1))
91
92 echo "mbim[$$]" "Checking subscriber"
93 umbim $DBG -n -t $tid -d $device subscriber || {
94 echo "mbim[$$]" "Subscriber init failed"
95 proto_notify_error "$interface" NO_SUBSCRIBER
96 return 1
97 }
98 tid=$((tid + 1))
99
100 echo "mbim[$$]" "Register with network"
101 umbim $DBG -n -t $tid -d $device registration || {
102 echo "mbim[$$]" "Subscriber registration failed"
103 proto_notify_error "$interface" NO_REGISTRATION
104 return 1
105 }
106 tid=$((tid + 1))
107
108 echo "mbim[$$]" "Attach to network"
109 umbim $DBG -n -t $tid -d $device attach || {
110 echo "mbim[$$]" "Failed to attach to network"
111 proto_notify_error "$interface" ATTACH_FAILED
112 return 1
113 }
114 tid=$((tid + 1))
115
116 echo "mbim[$$]" "Connect to network"
117 while ! umbim $DBG -n -t $tid -d $device connect "$apn" "$auth" "$username" "$password"; do
118 tid=$((tid + 1))
119 sleep 1;
120 done
121 tid=$((tid + 1))
122
123 uci_set_state network $interface tid "$tid"
124
125 echo "mbim[$$]" "Connected, starting DHCP"
126 proto_init_update "$ifname" 1
127 proto_send_update "$interface"
128
129 json_init
130 json_add_string name "${interface}_4"
131 json_add_string ifname "@$interface"
132 json_add_string proto "dhcp"
133 json_close_object
134 ubus call network add_dynamic "$(json_dump)"
135
136 json_init
137 json_add_string name "${interface}_6"
138 json_add_string ifname "@$interface"
139 json_add_string proto "dhcpv6"
140 ubus call network add_dynamic "$(json_dump)"
141 }
142
143 proto_mbim_teardown() {
144 local interface="$1"
145
146 local device
147 json_get_vars device
148 local tid=$(uci_get_state network $interface tid)
149
150 [ -n "$ctl_device" ] && device=$ctl_device
151
152 echo "mbim[$$]" "Stopping network"
153 [ -n "$tid" ] && {
154 umbim $DBG -t$tid -d "$device" disconnect
155 uci_revert_state network $interface tid
156 }
157
158 proto_init_update "*" 0
159 proto_send_update "$interface"
160 }
161
162 [ -n "$INCLUDE_ONLY" ] || add_protocol mbim