comgt: port 3g.sh to netifd
[openwrt.git] / package / comgt / files-netifd / 3g.sh
1 #!/bin/sh
2 INCLUDE_ONLY=1
3
4 . ../netifd-proto.sh
5 . ./ppp.sh
6 init_proto "$@"
7
8 proto_3g_init_config() {
9         no_device=1
10         available=1
11         ppp_generic_init_config
12         proto_config_add_string "device"
13         proto_config_add_string "apn"
14         proto_config_add_string "service"
15         proto_config_add_int "pincode"
16 }
17
18 proto_3g_setup() {
19         local interface="$1"
20         local chat
21
22         json_get_var device device
23         json_get_var apn apn
24         json_get_var service service
25         json_get_var pincode pincode
26
27         [ -e "$device" ] || {
28                 proto_set_available "$interface" 0
29                 return 1
30         }
31
32         case "$service" in
33                 cdma|evdo)
34                         chat="/etc/chatscripts/evdo.chat"
35                 ;;
36                 *)
37                         chat="/etc/chatscripts/3g.chat"
38                         cardinfo=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom)
39                         if echo "$cardinfo" | grep -q Novatel; then
40                                 case "$service" in
41                                         umts_only) CODE=2;;
42                                         gprs_only) CODE=1;;
43                                         *) CODE=0;;
44                                 esac
45                                 export MODE="AT\$NWRAT=${CODE},2"
46                         elif echo "$cardinfo" | grep -q Option; then
47                                 case "$service" in
48                                         umts_only) CODE=1;;
49                                         gprs_only) CODE=0;;
50                                         *) CODE=3;;
51                                 esac
52                                 export MODE="AT_OPSYS=${CODE}"
53                         fi
54
55                         if [ -n "$pincode" ]; then
56                                 PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
57                                         proto_notify_error "$interface" PIN_FAILED
58                                         proto_block_restart "$interface"
59                                         return 1
60                                 }
61                         fi
62                         [ -n "$MODE" ] && gcom -d "$device" -s /etc/gcom/setmode.gcom
63                 ;;
64         esac
65
66         connect="${apn:+USE_APN=$apn }/usr/sbin/chat -t5 -v -E -f $chat"
67         ppp_generic_setup "$interface" \
68                 noaccomp \
69                 nopcomp \
70                 novj \
71                 nobsdcomp \
72                 noauth \
73                 lock \
74                 crtscts \
75                 115200 "$device"
76         return 0
77 }
78
79 proto_3g_teardown() {
80         proto_kill_command "$interface"
81 }
82
83 add_protocol 3g