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