v1.5 branch refresh based upon upstream master @ c8677ca89e53e3be7988d54280fce166cc894a7e
[librecmc/librecmc.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 ] && {
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         json_get_values initialize initialize
92         for i in $initialize; do
93                 eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
94                         echo "Failed to initialize modem"
95                         proto_notify_error "$interface" INITIALIZE_FAILED
96                         return 1
97                 }
98         done
99
100         [ -n "$pincode" ] && {
101                 PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
102                         echo "Unable to verify PIN"
103                         proto_notify_error "$interface" PIN_FAILED
104                         proto_block_restart "$interface"
105                         return 1
106                 }
107         }
108
109         json_get_values configure configure
110         echo "Configuring modem"
111         for i in $configure; do
112                 eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
113                         echo "Failed to configure modem"
114                         proto_notify_error "$interface" CONFIGURE_FAILED
115                         return 1
116                 }
117         done
118
119         [ -n "$mode" ] && {
120                 json_select modes
121                 json_get_var setmode "$mode"
122                 echo "Setting mode"
123                 eval COMMAND="$setmode" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
124                         echo "Failed to set operating mode"
125                         proto_notify_error "$interface" SETMODE_FAILED
126                         return 1
127                 }
128                 json_select ..
129         }
130
131         echo "Starting network $interface"
132         json_get_vars connect
133         echo "Connecting modem"
134         eval COMMAND="$connect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
135                 echo "Failed to connect"
136                 proto_notify_error "$interface" CONNECT_FAILED
137                 return 1
138         }
139
140         json_get_vars finalize
141
142         echo "Setting up $ifname"
143         proto_init_update "$ifname" 1
144         proto_add_data
145         json_add_string "manufacturer" "$manufacturer"
146         proto_close_data
147         proto_send_update "$interface"
148
149         [ "$pdptype" = "IP" -o "$pdptype" = "IPV4V6" ] && {
150                 json_init
151                 json_add_string name "${interface}_4"
152                 json_add_string ifname "@$interface"
153                 json_add_string proto "dhcp"
154                 proto_add_dynamic_defaults
155                 ubus call network add_dynamic "$(json_dump)"
156         }
157
158         [ "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] && {
159                 json_init
160                 json_add_string name "${interface}_6"
161                 json_add_string ifname "@$interface"
162                 json_add_string proto "dhcpv6"
163                 json_add_string extendprefix 1
164                 proto_add_dynamic_defaults
165                 ubus call network add_dynamic "$(json_dump)"
166         }
167
168         [ -n "$finalize" ] && {
169                 eval COMMAND="$finalize" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
170                         echo "Failed to configure modem"
171                         proto_notify_error "$interface" FINALIZE_FAILED
172                         return 1
173                 }
174         }
175
176 }
177
178 proto_ncm_teardown() {
179         local interface="$1"
180
181         local manufacturer disconnect
182
183         local device profile
184         json_get_vars device profile
185
186         [ -n "$ctl_device" ] && device=$ctl_device
187
188         [ -n "$profile" ] || profile=1
189
190         echo "Stopping network $interface"
191
192         json_load "$(ubus call network.interface.$interface status)"
193         json_select data
194         json_get_vars manufacturer
195
196         json_load "$(cat /etc/gcom/ncm.json)"
197         json_select "$manufacturer" || {
198                 echo "Unsupported modem"
199                 proto_notify_error "$interface" UNSUPPORTED_MODEM
200                 return 1
201         }
202
203         json_get_vars disconnect
204         eval COMMAND="$disconnect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
205                 echo "Failed to disconnect"
206                 proto_notify_error "$interface" DISCONNECT_FAILED
207                 return 1
208         }
209
210         proto_init_update "*" 0
211         proto_send_update "$interface"
212 }
213 [ -n "$INCLUDE_ONLY" ] || {
214         add_protocol ncm
215 }