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