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