comgt: add metric, defaultroute and peerdns options for ncm protocol
[oweals/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_boolean ipv6
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 ipv6 $PROTO_DEFAULT_OPTIONS
31         json_get_vars device apn auth username password pincode delay mode pdptype ipv6 $PROTO_DEFAULT_OPTIONS
32         
33         if [ "$ipv6" = 0 ]; then
34                 ipv6=""
35         else
36                 ipv6=1
37         fi
38         
39         [ -z "$pdptype" ] && {
40                 if [ -n "$ipv6" ]; then
41                         pdptype="IPV4V6"
42                 else
43                         pdptype="IP"
44                 fi
45         }
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         [ -e "$device" ] || {
56                 echo "Control device not valid"
57                 proto_set_available "$interface" 0
58                 return 1
59         }
60         [ -n "$apn" ] || {
61                 echo "No APN specified"
62                 proto_notify_error "$interface" NO_APN
63                 return 1
64         }
65
66         devname="$(basename "$device")"
67         case "$devname" in
68         'tty'*)
69                 devpath="$(readlink -f /sys/class/tty/$devname/device)"
70                 ifname="$( ls "$devpath"/../../*/net )"
71                 ;;
72         *)
73                 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
74                 ifname="$( ls "$devpath"/net )"
75                 ;;
76         esac
77         [ -n "$ifname" ] || {
78                 echo "The interface could not be found."
79                 proto_notify_error "$interface" NO_IFACE
80                 proto_set_available "$interface" 0
81                 return 1
82         }
83
84         [ -n "$delay" ] && sleep "$delay"
85
86         manufacturer=`gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk '/Manufacturer/ { print tolower($2) }'`
87         [ $? -ne 0 ] && {
88                 echo "Failed to get modem information"
89                 proto_notify_error "$interface" GETINFO_FAILED
90                 return 1
91         }
92
93         json_load "$(cat /etc/gcom/ncm.json)"
94         json_select "$manufacturer"
95         [ $? -ne 0 ] && {
96                 echo "Unsupported modem"
97                 proto_notify_error "$interface" UNSUPPORTED_MODEM
98                 proto_set_available "$interface" 0
99                 return 1
100         }
101         json_get_values initialize initialize
102         for i in $initialize; do
103                 eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
104                         echo "Failed to initialize modem"
105                         proto_notify_error "$interface" INITIALIZE_FAILED
106                         return 1
107                 }
108         done
109
110         [ -n "$pincode" ] && {
111                 PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
112                         echo "Unable to verify PIN"
113                         proto_notify_error "$interface" PIN_FAILED
114                         proto_block_restart "$interface"
115                         return 1
116                 }
117         }
118         [ -n "$mode" ] && {
119                 json_select modes
120                 json_get_var setmode "$mode"
121                 COMMAND="$setmode" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
122                         echo "Failed to set operating mode"
123                         proto_notify_error "$interface" SETMODE_FAILED
124                         return 1
125                 }
126                 json_select ..
127         }
128
129         json_get_vars connect
130         eval COMMAND="$connect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
131                 echo "Failed to connect"
132                 proto_notify_error "$interface" CONNECT_FAILED
133                 return 1
134         }
135
136         echo "Connected, starting DHCP on $ifname"
137         
138         proto_init_update "$ifname" 1
139         proto_send_update "$interface"
140
141         json_init
142         json_add_string name "${interface}_4"
143         json_add_string ifname "@$interface"
144         json_add_string proto "dhcp"
145         proto_add_dynamic_defaults
146         ubus call network add_dynamic "$(json_dump)"
147
148         [ -n "$ipv6" ] && {
149                 json_init
150                 json_add_string name "${interface}_6"
151                 json_add_string ifname "@$interface"
152                 json_add_string proto "dhcpv6"
153                 json_add_string extendprefix 1
154                 proto_add_dynamic_defaults
155                 ubus call network add_dynamic "$(json_dump)"
156         }
157 }
158
159 proto_ncm_teardown() {
160         local interface="$1"
161
162         local manufacturer disconnect
163
164         local device
165         json_get_vars device
166
167         echo "Stopping network"
168
169         manufacturer=`gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk '/Manufacturer/ { print tolower($2) }'`
170         [ $? -ne 0 ] && {
171                 echo "Failed to get modem information"
172                 proto_notify_error "$interface" GETINFO_FAILED
173                 return 1
174         }
175
176         json_load "$(cat /etc/gcom/ncm.json)"
177         json_select "$manufacturer" || {
178                 echo "Unsupported modem"
179                 proto_notify_error "$interface" UNSUPPORTED_MODEM
180                 return 1
181         }
182
183         json_get_vars disconnect
184         COMMAND="$disconnect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
185                 echo "Failed to disconnect"
186                 proto_notify_error "$interface" DISCONNECT_FAILED
187                 return 1
188         }
189
190         proto_init_update "*" 0
191         proto_send_update "$interface"
192 }
193 [ -n "$INCLUDE_ONLY" ] || {
194         add_protocol ncm
195 }