46802f2d3c5dd40ca4d93b4905ae1e197e5ec42d
[openwrt/openwrt.git] / package / network / utils / umbim / files / lib / netifd / proto / mbim.sh
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . ../netifd-proto.sh
5 init_proto "$@"
6
7 #DBG=-v
8
9 proto_mbim_init_config() {
10 available=1
11 no_device=1
12 proto_config_add_string "device:device"
13 proto_config_add_string apn
14 proto_config_add_string pincode
15 proto_config_add_string delay
16 proto_config_add_string auth
17 proto_config_add_string username
18 proto_config_add_string password
19 }
20
21 proto_mbim_setup() {
22 local interface="$1"
23 local tid=2
24 local ret
25
26 local device apn pincode delay
27 json_get_vars device apn pincode delay auth username password
28
29 [ -n "$device" ] || {
30 logger -p daemon.err -t "mbim[$$]" "No control device specified"
31 proto_notify_error "$interface" NO_DEVICE
32 proto_block_restart "$interface"
33 return 1
34 }
35 [ -c "$device" ] || {
36 logger -p daemon.err -t "mbim[$$]" "The specified control device does not exist"
37 proto_notify_error "$interface" NO_DEVICE
38 proto_block_restart "$interface"
39 return 1
40 }
41
42 devname="$(basename "$device")"
43 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
44 ifname="$( ls "$devpath"/net )"
45
46 [ -n "$ifname" ] || {
47 logger -p daemon.err -t "mbim[$$]" "Failed to find matching interface"
48 proto_notify_error "$interface" NO_IFNAME
49 proto_set_available "$interface" 0
50 return 1
51 }
52
53 [ -n "$apn" ] || {
54 logger -p daemon.err -t "mbim[$$]" "No APN specified"
55 proto_notify_error "$interface" NO_APN
56 proto_block_restart "$interface"
57 return 1
58 }
59
60 [ -n "$delay" ] && sleep "$delay"
61
62 logger -p daemon.info -t "mbim[$$]" "Reading capabilities"
63 umbim $DBG -n -d $device caps || {
64 logger -p daemon.err -t "mbim[$$]" "Failed to read modem caps"
65 proto_notify_error "$interface" PIN_FAILED
66 proto_block_restart "$interface"
67 return 1
68 }
69 tid=$((tid + 1))
70
71 [ "$pincode" ] && {
72 logger -p daemon.info -t "mbim[$$]" "Sending pin"
73 umbim $DBG -n -t $tid -d $device unlock "$pincode" || {
74 logger -p daemon.err -t "mbim[$$]" "Unable to verify PIN"
75 proto_notify_error "$interface" PIN_FAILED
76 proto_block_restart "$interface"
77 return 1
78 }
79 }
80 tid=$((tid + 1))
81
82 logger -p daemon.info -t "mbim[$$]" "Checking pin"
83 umbim $DBG -n -t $tid -d $device pinstate || {
84 logger -p daemon.err -t "mbim[$$]" "PIN required"
85 proto_notify_error "$interface" PIN_FAILED
86 proto_block_restart "$interface"
87 return 1
88 }
89 tid=$((tid + 1))
90
91 logger -p daemon.info -t "mbim[$$]" "Checking subscriber"
92 umbim $DBG -n -t $tid -d $device subscriber || {
93 logger -p daemon.err -t "mbim[$$]" "Subscriber init failed"
94 proto_notify_error "$interface" NO_SUBSCRIBER
95 proto_block_restart "$interface"
96 return 1
97 }
98 tid=$((tid + 1))
99
100 logger -p daemon.info -t "mbim[$$]" "Register with network"
101 umbim $DBG -n -t $tid -d $device registration || {
102 logger -p daemon.err -t "mbim[$$]" "Subscriber registration failed"
103 proto_notify_error "$interface" NO_REGISTRATION
104 proto_block_restart "$interface"
105 return 1
106 }
107 tid=$((tid + 1))
108
109 logger -p daemon.info -t "mbim[$$]" "Attach to network"
110 umbim $DBG -n -t $tid -d $device attach || {
111 logger -p daemon.err -t "mbim[$$]" "Failed to attach to network"
112 proto_notify_error "$interface" ATTACH_FAILED
113 proto_block_restart "$interface"
114 return 1
115 }
116 tid=$((tid + 1))
117
118 logger -p daemon.info -t "mbim[$$]" "Connect to network"
119 while ! umbim $DBG -n -t $tid -d $device connect "$apn" "$auth" "$username" "$password"; do
120 tid=$((tid + 1))
121 sleep 1;
122 done
123 tid=$((tid + 1))
124
125 uci_set_state network $interface tid "$tid"
126
127 logger -p daemon.info -t "mbim[$$]" "Connected, starting DHCP"
128 proto_init_update "*" 1
129 proto_send_update "$interface"
130
131 json_init
132 json_add_string name "${interface}_dhcp"
133 json_add_string ifname "$ifname"
134 json_add_string proto "dhcp"
135 json_close_object
136 ubus call network add_dynamic "$(json_dump)"
137
138 json_init
139 json_add_string name "${interface}_dhcpv6"
140 json_add_string ifname "$ifname"
141 json_add_string proto "dhcpv6"
142 ubus call network add_dynamic "$(json_dump)"
143 }
144
145 proto_mbim_teardown() {
146 local interface="$1"
147
148 local device
149 json_get_vars device
150 local tid=$(uci_get_state network $interface tid)
151
152 logger -p daemon.info -t "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 add_protocol mbim