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