comgt-ncm: Add support for specifying profile index
[openwrt/staging/blogic.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 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 [ -e "$device" ] || {
49 echo "Control device not valid"
50 proto_set_available "$interface" 0
51 return 1
52 }
53
54 devname="$(basename "$device")"
55 case "$devname" in
56 'tty'*)
57 devpath="$(readlink -f /sys/class/tty/$devname/device)"
58 ifname="$( ls "$devpath"/../../*/net )"
59 ;;
60 *)
61 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
62 ifname="$( ls "$devpath"/net )"
63 ;;
64 esac
65 [ -n "$ifname" ] || {
66 echo "The interface could not be found."
67 proto_notify_error "$interface" NO_IFACE
68 proto_set_available "$interface" 0
69 return 1
70 }
71
72 [ -n "$delay" ] && sleep "$delay"
73
74 manufacturer=`gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk 'NF && $0 !~ /AT\+CGMI/ { sub(/\+CGMI: /,""); print tolower($1); exit; }'`
75 [ $? -ne 0 ] && {
76 echo "Failed to get modem information"
77 proto_notify_error "$interface" GETINFO_FAILED
78 return 1
79 }
80
81 json_load "$(cat /etc/gcom/ncm.json)"
82 json_select "$manufacturer"
83 [ $? -ne 0 ] && {
84 echo "Unsupported modem"
85 proto_notify_error "$interface" UNSUPPORTED_MODEM
86 proto_set_available "$interface" 0
87 return 1
88 }
89 json_get_values initialize initialize
90 for i in $initialize; do
91 eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
92 echo "Failed to initialize modem"
93 proto_notify_error "$interface" INITIALIZE_FAILED
94 return 1
95 }
96 done
97
98 [ -n "$pincode" ] && {
99 PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
100 echo "Unable to verify PIN"
101 proto_notify_error "$interface" PIN_FAILED
102 proto_block_restart "$interface"
103 return 1
104 }
105 }
106 [ -n "$mode" ] && {
107 json_select modes
108 json_get_var setmode "$mode"
109 eval COMMAND="$setmode" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
110 echo "Failed to set operating mode"
111 proto_notify_error "$interface" SETMODE_FAILED
112 return 1
113 }
114 json_select ..
115 }
116
117 echo "Starting network $interface"
118 json_get_vars connect
119 eval COMMAND="$connect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
120 echo "Failed to connect"
121 proto_notify_error "$interface" CONNECT_FAILED
122 return 1
123 }
124
125 echo "Setting up $ifname"
126
127 proto_init_update "$ifname" 1
128 proto_add_data
129 json_add_string "manufacturer" "$manufacturer"
130 proto_close_data
131 proto_send_update "$interface"
132
133 [ "$pdptype" = "IP" -o "$pdptype" = "IPV4V6" ] && {
134 json_init
135 json_add_string name "${interface}_4"
136 json_add_string ifname "@$interface"
137 json_add_string proto "dhcp"
138 proto_add_dynamic_defaults
139 ubus call network add_dynamic "$(json_dump)"
140 }
141
142 [ "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] && {
143 json_init
144 json_add_string name "${interface}_6"
145 json_add_string ifname "@$interface"
146 json_add_string proto "dhcpv6"
147 json_add_string extendprefix 1
148 proto_add_dynamic_defaults
149 ubus call network add_dynamic "$(json_dump)"
150 }
151 }
152
153 proto_ncm_teardown() {
154 local interface="$1"
155
156 local manufacturer disconnect
157
158 local device profile
159 json_get_vars device profile
160
161 [ -n "$ctl_device" ] && device=$ctl_device
162
163 [ -n "$profile" ] || profile=1
164
165 echo "Stopping network $interface"
166
167 json_load "$(ubus call network.interface.$interface status)"
168 json_select data
169 json_get_vars manufacturer
170
171 json_load "$(cat /etc/gcom/ncm.json)"
172 json_select "$manufacturer" || {
173 echo "Unsupported modem"
174 proto_notify_error "$interface" UNSUPPORTED_MODEM
175 return 1
176 }
177
178 json_get_vars disconnect
179 eval COMMAND="$disconnect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
180 echo "Failed to disconnect"
181 proto_notify_error "$interface" DISCONNECT_FAILED
182 return 1
183 }
184
185 proto_init_update "*" 0
186 proto_send_update "$interface"
187 }
188 [ -n "$INCLUDE_ONLY" ] || {
189 add_protocol ncm
190 }