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