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