comgt: add ncm proto support
[openwrt/openwrt.git] / package / network / utils / comgt / files / ncm.sh
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . ../netifd-proto.sh
5 init_proto "$@"
6
7 proto_ncm_init_config() {
8 no_device=1
9 available=1
10 proto_config_add_string "device:device"
11 proto_config_add_string apn
12 proto_config_add_string auth
13 proto_config_add_string username
14 proto_config_add_string password
15 proto_config_add_string pincode
16 proto_config_add_string delay
17 proto_config_add_string mode
18 }
19
20 proto_ncm_setup() {
21 local interface="$1"
22
23 local manufacturer initialize setmode connect
24
25 local device apn auth username password pincode delay mode
26 json_get_vars device apn auth username password pincode delay mode
27
28 [ -n "$device" ] || {
29 echo "ncm[$$]" "No control device specified"
30 proto_notify_error "$interface" NO_DEVICE
31 proto_set_available "$interface" 0
32 return 1
33 }
34 [ -e "$device" ] || {
35 echo "ncm[$$]" "Control device not valid"
36 proto_set_available "$interface" 0
37 return 1
38 }
39 [ -n "$apn" ] || {
40 echo "ncm[$$]" "No APN specified"
41 proto_notify_error "$interface" NO_APN
42 proto_set_available "$interface" 0
43 return 1
44 }
45
46 [ -n "$delay" ] && sleep "$delay"
47
48 manufacturer=`gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk '/Manufacturer/ { print $2 }'`
49 [ $? -ne 0 ] && {
50 echo "ncm[$$]" "Failed to get modem information"
51 proto_notify_error "$interface" GETINFO_FAILED
52 proto_set_available "$interface" 0
53 return 1
54 }
55
56 json_load "$(cat /etc/gcom/ncm.json)"
57 json_select "$manufacturer"
58 [ $? -ne 0 ] && {
59 echo "ncm[$$]" "Unsupported modem"
60 proto_notify_error "$interface" UNSUPPORTED_MODEM
61 proto_set_available "$interface" 0
62 return 1
63 }
64 json_get_values initialize initialize
65 for i in $initialize; do
66 eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
67 echo "ncm[$$]" "Failed to initialize modem"
68 proto_notify_error "$interface" INITIALIZE_FAILED
69 proto_set_available "$interface" 0
70 return 1
71 }
72 done
73
74 [ -n "$pincode" ] && {
75 PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
76 echo "ncm[$$]" "Unable to verify PIN"
77 proto_notify_error "$interface" PIN_FAILED
78 proto_block_restart "$interface"
79 return 1
80 }
81 }
82 [ -n "$mode" ] && {
83 json_select modes
84 json_get_var setmode "$mode"
85 COMMAND="$setmode" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
86 echo "ncm[$$]" "Failed to set operating mode"
87 proto_notify_error "$interface" SETMODE_FAILED
88 proto_set_available "$interface" 0
89 return 1
90 }
91 json_select ..
92 }
93
94 json_get_var connect connect
95 eval COMMAND="$connect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
96 echo "ncm[$$]" "Failed to connect"
97 proto_notify_error "$interface" CONNECT_FAILED
98 proto_set_available "$interface" 0
99 return 1
100 }
101
102 echo "ncm[$$]" "Connected, starting DHCP"
103 proto_init_update "*" 1
104 proto_send_update "$interface"
105
106 json_init
107 json_add_string name "${interface}_dhcp"
108 json_add_string ifname "@$interface"
109 json_add_string proto "dhcp"
110 json_close_object
111 ubus call network add_dynamic "$(json_dump)"
112
113 json_init
114 json_add_string name "${interface}_dhcpv6"
115 json_add_string ifname "@$interface"
116 json_add_string proto "dhcpv6"
117 json_close_object
118 ubus call network add_dynamic "$(json_dump)"
119 }
120
121 proto_ncm_teardown() {
122 local interface="$1"
123
124 proto_init_update "*" 0
125 proto_send_update "$interface"
126
127 local manufacturer disconnect
128
129 local device
130 json_get_vars device
131
132 echo "ncm[$$]" "Stopping network"
133
134 manufacturer=`gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk '/Manufacturer/ { print $2 }'`
135 [ $? -ne 0 ] && {
136 echo "ncm[$$]" "Failed to get modem information"
137 proto_notify_error "$interface" GETINFO_FAILED
138 return 1
139 }
140
141 json_load "$(cat /etc/gcom/ncm.json)"
142 json_select "$manufacturer" || {
143 echo "ncm[$$]" "Unsupported modem"
144 proto_notify_error "$interface" UNSUPPORTED_MODEM
145 return 1
146 }
147
148 json_get_var disconnect disconnect
149 COMMAND="$disconnect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
150 echo "ncm[$$]" "Failed to disconnect"
151 proto_notify_error "$interface" DISCONNECT_FAILED
152 return 1
153 }
154 }
155
156 add_protocol ncm