Fresh pull from upstream
[librecmc/librecmc.git] / package / network / utils / comgt / files / 3g.sh
1 #!/bin/sh
2
3 [ -n "$INCLUDE_ONLY" ] || {
4         NOT_INCLUDED=1
5         INCLUDE_ONLY=1
6
7         . ../netifd-proto.sh
8         . ./ppp.sh
9         init_proto "$@"
10 }
11
12 proto_3g_init_config() {
13         no_device=1
14         available=1
15         ppp_generic_init_config
16         proto_config_add_string "device:device"
17         proto_config_add_string "apn"
18         proto_config_add_string "service"
19         proto_config_add_string "pincode"
20         proto_config_add_string "dialnumber"
21 }
22
23 proto_3g_setup() {
24         local interface="$1"
25         local chat
26
27         json_get_var device device
28         json_get_var apn apn
29         json_get_var service service
30         json_get_var pincode pincode
31         json_get_var dialnumber dialnumber
32
33         [ -n "$dat_device" ] && device=$dat_device
34
35         device="$(readlink -f $device)"
36         [ -e "$device" ] || {
37                 proto_set_available "$interface" 0
38                 return 1
39         }
40
41         case "$service" in
42                 cdma|evdo)
43                         chat="/etc/chatscripts/evdo.chat"
44                 ;;
45                 *)
46                         chat="/etc/chatscripts/3g.chat"
47                         cardinfo=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom)
48                         if echo "$cardinfo" | grep -q Novatel; then
49                                 case "$service" in
50                                         umts_only) CODE=2;;
51                                         gprs_only) CODE=1;;
52                                         *) CODE=0;;
53                                 esac
54                                 export MODE="AT\$NWRAT=${CODE},2"
55                         elif echo "$cardinfo" | grep -q Option; then
56                                 case "$service" in
57                                         umts_only) CODE=1;;
58                                         gprs_only) CODE=0;;
59                                         *) CODE=3;;
60                                 esac
61                                 export MODE="AT_OPSYS=${CODE}"
62                         elif echo "$cardinfo" | grep -q "Sierra Wireless"; then
63                                 SIERRA=1
64                         elif echo "$cardinfo" | grep -qi huawei; then
65                                 case "$service" in
66                                         umts_only) CODE="14,2";;
67                                         gprs_only) CODE="13,1";;
68                                         *) CODE="2,2";;
69                                 esac
70                                 export MODE="AT^SYSCFG=${CODE},3FFFFFFF,2,4"
71                         fi
72
73                         if [ -n "$pincode" ]; then
74                                 PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
75                                         proto_notify_error "$interface" PIN_FAILED
76                                         proto_block_restart "$interface"
77                                         return 1
78                                 }
79                         fi
80                         [ -n "$MODE" ] && gcom -d "$device" -s /etc/gcom/setmode.gcom
81
82                         # wait for carrier to avoid firmware stability bugs
83                         [ -n "$SIERRA" ] && {
84                                 gcom -d "$device" -s /etc/gcom/getcarrier.gcom || return 1
85                         }
86
87                         if [ -z "$dialnumber" ]; then
88                                 dialnumber="*99***1#"
89                         fi
90
91                 ;;
92         esac
93
94         connect="${apn:+USE_APN=$apn }DIALNUMBER=$dialnumber /usr/sbin/chat -t5 -v -E -f $chat"
95         ppp_generic_setup "$interface" \
96                 noaccomp \
97                 nopcomp \
98                 novj \
99                 nobsdcomp \
100                 noauth \
101                 set EXTENDPREFIX=1 \
102                 lock \
103                 crtscts \
104                 115200 "$device"
105         return 0
106 }
107
108 proto_3g_teardown() {
109         proto_kill_command "$interface"
110 }
111
112 [ -z "NOT_INCLUDED" ] || add_protocol 3g