ncm: add sourcefilter option support
[openwrt/staging/linusw.git] / package / network / utils / comgt / files / ncm.sh
1 #!/bin/sh
2
3 [ -n "$INCLUDE_ONLY" ] || {
4 . /lib/functions.sh
5 . ../netifd-proto.sh
6 init_proto "$@"
7 }
8
9 proto_ncm_init_config() {
10 no_device=1
11 available=1
12 proto_config_add_string "device:device"
13 proto_config_add_string ifname
14 proto_config_add_string apn
15 proto_config_add_string auth
16 proto_config_add_string username
17 proto_config_add_string password
18 proto_config_add_string pincode
19 proto_config_add_string delay
20 proto_config_add_string mode
21 proto_config_add_string pdptype
22 proto_config_add_boolean sourcefilter
23 proto_config_add_int profile
24 proto_config_add_defaults
25 }
26
27 proto_ncm_setup() {
28 local interface="$1"
29
30 local manufacturer initialize setmode connect finalize devname devpath ifpath
31
32 local device ifname apn auth username password pincode delay mode pdptype profile $PROTO_DEFAULT_OPTIONS
33 json_get_vars device ifname apn auth username password pincode delay mode pdptype sourcefilter profile $PROTO_DEFAULT_OPTIONS
34
35 local context_type
36
37 [ "$metric" = "" ] && metric="0"
38
39 [ -n "$profile" ] || profile=1
40
41 pdptype=$(echo "$pdptype" | awk '{print toupper($0)}')
42 [ "$pdptype" = "IP" -o "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] || pdptype="IP"
43
44 [ "$pdptype" = "IPV4V6" ] && context_type=3
45 [ -z "$context_type" -a "$pdptype" = "IPV6" ] && context_type=2
46 [ -n "$context_type" ] || context_type=1
47
48 [ -n "$ctl_device" ] && device=$ctl_device
49
50 [ -n "$device" ] || {
51 echo "No control device specified"
52 proto_notify_error "$interface" NO_DEVICE
53 proto_set_available "$interface" 0
54 return 1
55 }
56
57 device="$(readlink -f $device)"
58 [ -e "$device" ] || {
59 echo "Control device not valid"
60 proto_set_available "$interface" 0
61 return 1
62 }
63
64 [ -z "$ifname" ] && {
65 devname="$(basename "$device")"
66 case "$devname" in
67 'ttyACM'*)
68 devpath="$(readlink -f /sys/class/tty/$devname/device)"
69 ifpath="$devpath/../*/net"
70 ;;
71 'tty'*)
72 devpath="$(readlink -f /sys/class/tty/$devname/device)"
73 ifpath="$devpath/../../*/net"
74 ;;
75 *)
76 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
77 ifpath="$devpath/net"
78 ;;
79 esac
80 ifname="$(ls $(ls -1 -d $ifpath | head -n 1))"
81 }
82
83 [ -n "$ifname" ] || {
84 echo "The interface could not be found."
85 proto_notify_error "$interface" NO_IFACE
86 proto_set_available "$interface" 0
87 return 1
88 }
89
90 start=$(date +%s)
91 while true; do
92 manufacturer=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk 'NF && $0 !~ /AT\+CGMI/ { sub(/\+CGMI: /,""); print tolower($1); exit; }')
93 [ "$manufacturer" = "error" ] && {
94 manufacturer=""
95 }
96 [ -n "$manufacturer" ] && {
97 break
98 }
99 [ -z "$delay" ] && {
100 break
101 }
102 sleep 1
103 elapsed=$(($(date +%s) - start))
104 [ "$elapsed" -gt "$delay" ] && {
105 break
106 }
107 done
108 [ -z "$manufacturer" ] && {
109 echo "Failed to get modem information"
110 proto_notify_error "$interface" GETINFO_FAILED
111 return 1
112 }
113
114 json_load "$(cat /etc/gcom/ncm.json)"
115 json_select "$manufacturer"
116 [ $? -ne 0 ] && {
117 echo "Unsupported modem"
118 proto_notify_error "$interface" UNSUPPORTED_MODEM
119 proto_set_available "$interface" 0
120 return 1
121 }
122
123 json_get_values initialize initialize
124 for i in $initialize; do
125 eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
126 echo "Failed to initialize modem"
127 proto_notify_error "$interface" INITIALIZE_FAILED
128 return 1
129 }
130 done
131
132 [ -n "$pincode" ] && {
133 PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
134 echo "Unable to verify PIN"
135 proto_notify_error "$interface" PIN_FAILED
136 proto_block_restart "$interface"
137 return 1
138 }
139 }
140
141 json_get_values configure configure
142 echo "Configuring modem"
143 for i in $configure; do
144 eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
145 echo "Failed to configure modem"
146 proto_notify_error "$interface" CONFIGURE_FAILED
147 return 1
148 }
149 done
150
151 [ -n "$mode" ] && {
152 json_select modes
153 json_get_var setmode "$mode"
154 [ -n "$setmode" ] && {
155 echo "Setting mode"
156 eval COMMAND="$setmode" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
157 echo "Failed to set operating mode"
158 proto_notify_error "$interface" SETMODE_FAILED
159 return 1
160 }
161 }
162 json_select ..
163 }
164
165 echo "Starting network $interface"
166 json_get_vars connect
167 [ -n "$connect" ] && {
168 echo "Connecting modem"
169 eval COMMAND="$connect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
170 echo "Failed to connect"
171 proto_notify_error "$interface" CONNECT_FAILED
172 return 1
173 }
174 }
175
176 json_get_vars finalize
177
178 echo "Setting up $ifname"
179 proto_init_update "$ifname" 1
180 proto_add_data
181 json_add_string "manufacturer" "$manufacturer"
182 proto_close_data
183 proto_send_update "$interface"
184
185 local zone="$(fw3 -q network "$interface" 2>/dev/null)"
186
187 [ "$pdptype" = "IP" -o "$pdptype" = "IPV4V6" ] && {
188 json_init
189 json_add_string name "${interface}_4"
190 json_add_string ifname "@$interface"
191 json_add_string proto "dhcp"
192 proto_add_dynamic_defaults
193 [ -n "$zone" ] && {
194 json_add_string zone "$zone"
195 }
196 json_close_object
197 ubus call network add_dynamic "$(json_dump)"
198 }
199
200 [ "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] && {
201 json_init
202 json_add_string name "${interface}_6"
203 json_add_string ifname "@$interface"
204 json_add_string proto "dhcpv6"
205 json_add_string extendprefix 1
206 [ "$sourcefilter" = "0" ] && json_add_boolean sourcefilter "0"
207 proto_add_dynamic_defaults
208 [ -n "$zone" ] && {
209 json_add_string zone "$zone"
210 }
211 json_close_object
212 ubus call network add_dynamic "$(json_dump)"
213 }
214
215 [ -n "$finalize" ] && {
216 eval COMMAND="$finalize" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
217 echo "Failed to configure modem"
218 proto_notify_error "$interface" FINALIZE_FAILED
219 return 1
220 }
221 }
222 }
223
224 proto_ncm_teardown() {
225 local interface="$1"
226
227 local manufacturer disconnect
228
229 local device profile
230 json_get_vars device profile
231
232 [ -n "$ctl_device" ] && device=$ctl_device
233
234 [ -n "$device" ] || {
235 echo "No control device specified"
236 proto_notify_error "$interface" NO_DEVICE
237 proto_set_available "$interface" 0
238 return 1
239 }
240
241 device="$(readlink -f $device)"
242 [ -e "$device" ] || {
243 echo "Control device not valid"
244 proto_set_available "$interface" 0
245 return 1
246 }
247
248 [ -n "$profile" ] || profile=1
249
250 echo "Stopping network $interface"
251
252 json_load "$(ubus call network.interface.$interface status)"
253 json_select data
254 json_get_vars manufacturer
255 [ $? -ne 0 -o -z "$manufacturer" ] && {
256 # Fallback to direct detect, for proper handle device replug.
257 manufacturer=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk 'NF && $0 !~ /AT\+CGMI/ { sub(/\+CGMI: /,""); print tolower($1); exit; }')
258 [ $? -ne 0 -o -z "$manufacturer" ] && {
259 echo "Failed to get modem information"
260 proto_notify_error "$interface" GETINFO_FAILED
261 return 1
262 }
263 json_add_string "manufacturer" "$manufacturer"
264 }
265
266 json_load "$(cat /etc/gcom/ncm.json)"
267 json_select "$manufacturer" || {
268 echo "Unsupported modem"
269 proto_notify_error "$interface" UNSUPPORTED_MODEM
270 return 1
271 }
272
273 json_get_vars disconnect
274 [ -n "$disconnect" ] && {
275 eval COMMAND="$disconnect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
276 echo "Failed to disconnect"
277 proto_notify_error "$interface" DISCONNECT_FAILED
278 return 1
279 }
280 }
281
282 proto_init_update "*" 0
283 proto_send_update "$interface"
284 }
285 [ -n "$INCLUDE_ONLY" ] || {
286 add_protocol ncm
287 }