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