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