4 # Copyright (C) 2015 shibby
7 MODE
=`nvram get wan_proto`
9 if [ ! "$MODE" == "lte" ]; then
13 if [ "$1" == "connect" ]; then
14 APN
=`nvram get modem_apn`
15 DEV
=`nvram get modem_dev4g`
16 TYP
=`nvram get modem_type`
17 IFA
=`nvram get wan_4g`
19 logger
4G MODEM
- connecting ...
21 if [ "$TYP" == "nonhilink" ]; then
24 while [ $CONNECTED == "0" ]; do
25 MODE
="AT^NDISDUP=1,1,\"$APN\"" gcom
-d $DEV -s /etc
/gcom
/setverbose.gcom
> /tmp
/4g.mode
27 MODE
="AT+CGCONTRDP" gcom
-d $DEV -e -s /etc
/gcom
/setverbose.gcom
> /tmp
/4g.check
28 CHECK1
=`cat /tmp/4g.check | grep ERROR | wc -l`
29 CHECK2
=`cat /tmp/4g.check | grep open | wc -l`
30 if [ "$CHECK1" == "1" ]; then
31 logger
4G MODEM
- device
$DEV not connected yet ...
33 elif [ "$CHECK2" == "1" ]; then
34 logger
4G MODEM
- device
$DEV not ready ...
37 logger
4G MODEM
- connected ...
44 while [ $GO = "0" ]; do
47 CHECKIFA
=`ifconfig | grep $IFA | wc -l`
48 if [ "$CHECKIFA" == "1" ]; then
50 logger
4G MODEM
- WAN configured ...
54 elif [ "$1" == "disconnect" ]; then
55 DEV
=`nvram get modem_dev4g`
57 logger
4G MODEM
- disconnecting ...
59 MODE
="AT^NDISDUP=1,0" gcom
-d $DEV -s /etc
/gcom
/setmode.gcom
62 LOCK
="/tmp/switch4g.lock"
63 if [ -f $LOCK ]; then #lock exist
64 logger
4G MODEM
- previous proces of switch4g still working
68 PIN
=`nvram get modem_pin`
69 IS_PIN
=`nvram get modem_pin | wc -w`
75 NH
=`cat /proc/bus/usb/devices | grep Driver | grep cdc_ncm | wc -l`
76 H
=`cat /proc/bus/usb/devices | grep Driver | grep cdc_ether | wc -l`
77 US
=`cat /proc/bus/usb/devices | grep Driver | grep usbserial | wc -l`
79 if [ "$H" -gt 0 ]; then
80 logger
4G MODEM already found
- Hilink
- using cdc_ether module
81 nvram
set 4g_module
=cdc_ether
83 elif [ "$NH" -gt 0 ]; then
84 logger
4G MODEM already found
- Non-Hilink
- using cdc_ncm module
85 nvram
set 4g_module
=cdc_ncm
89 if [ "$US" -gt 0 ]; then
90 logger
4G MODEM already found
- Diagnostic interface
- using usbserial module
93 #modem not found, try detect
95 DEVICES
=`lsusb | awk '{print $6}'`
97 for SWITCH
in $DEVICES; do
98 SEARCH
=`ls /etc/usb_modeswitch.d/$SWITCH | wc -l`
102 if [ "$SEARCH" == "1" ]; then
103 logger
4G MODEM FOUND
- $SWITCH - Switching ...
104 DV
=`echo $SWITCH | cut -d ":" -f1`
105 DP
=`echo $SWITCH | cut -d ":" -f2`
106 /usr
/sbin
/usb_modeswitch
-Q -c /etc
/usb_modeswitch.d
/$SWITCH -v $DV -p $DP
108 TEST1
=`cat /etc/usb_modeswitch.d/$SWITCH | grep "TargetVendor" | cut -d "=" -f2 | wc -l`
109 if [ "$TEST1" == "1" ]; then
110 VENDOR
=`cat /etc/usb_modeswitch.d/$SWITCH | grep "TargetVendor" | cut -d "=" -f2 | cut -d "x" -f2`
112 VENDOR
=`echo $SWITCH | cut -d ":" -f1`
115 TEST2
=`lsusb | awk '{print $6}' | grep $VENDOR | wc -l`
116 if [ "$TEST2" == "1" ]; then
117 PRODUCT
=`lsusb | awk '{print $6}' | grep $VENDOR | cut -d ":" -f2`
118 logger
4G MODEM ready
- $VENDOR:$PRODUCT
119 echo "$VENDOR:$PRODUCT" > /tmp
/3g.detect
124 IS_VENDOR
=`echo $VENDOR | wc -w`
125 if [ "$IS_VENDOR" -gt 0 ]; then
126 IS_PRODUCT
=`echo $PRODUCT | wc -w`
127 if [ "$IS_PRODUCT" -gt 0 ]; then
128 logger
4G MODEM
- loading module usbserial
130 insmod usbserial vendor
=0x
$VENDOR product
=0x
$PRODUCT
131 echo "$VENDOR:$PRODUCT" > /tmp
/4g.detect
135 DEV
=`cat /proc/bus/usb/devices | grep Driver | grep usbserial | wc -l`
136 if [ "$DEV" -gt 0 ]; then
137 logger
4G MODEM ready
- using usbserial module
138 nvram
set 4g_module
=usbserial
141 elif [ -f /tmp
/4g.detect
]; then
142 VENDOR
=`cat /tmp/4g.detect | cut -d ":" -f1`
143 PRODUCT
=`cat /tmp/4g.detect | cut -d ":" -f2`
144 TEST3
=`lsusb | grep $VENDOR | grep $PRODUCT | wc -l`
145 if [ "$TEST3" == "1" ]; then
146 logger
4G MODEM FOUND
- already switched
- Last know
$VENDOR:$PRODUCT
148 DEV
=`cat /proc/bus/usb/devices | grep Driver | grep usbserial | wc -l`
149 if [ "$DEV" -gt 0 ]; then
150 logger
4G MODEM ready
- using usbserial module
151 nvram
set 4g_module
=usbserial
155 logger
4G MODEM
- loading module
157 insmod usbserial vendor
=0x
$VENDOR product
=0x
$PRODUCT
158 echo "$VENDOR:$PRODUCT" > /tmp
/4g.detect
159 nvram
set 4g_module
=usbserial
165 #last change. try load usbserial for each usb devices
166 DEVICES
=`lsusb | awk '{print $6}'`
167 for SWITCH
in $DEVICES; do
168 VENDOR
=`echo $SWITCH | cut -d ":" -f1`
169 PRODUCT
=`echo $SWITCH | cut -d ":" -f2`
171 insmod usbserial vendor
=0x
$VENDOR product
=0x
$PRODUCT
173 DEV
=`cat /proc/bus/usb/devices | grep Driver | grep usbserial | wc -l`
174 if [ "$DEV" -gt 0 ]; then
175 logger
4G MODEM ready
- using usbserial module
176 nvram
set 4g_module
=usbserial
177 echo "$VENDOR:$PRODUCT" > /tmp
/4g.detect
185 #search diagnostic device
186 TTY
=`ls /dev/ttyUSB*`
189 CHECKTTY
=`gcom -d $i -e info | grep OK | wc -l`
190 if [ "$CHECKTTY" == "1" ]; then #found working interface
191 nvram
set modem_dev4g
=$i
196 #search WAN interface (usbX or ethX)
197 HWAN
=`dmesg | grep cdc_ether | grep register | grep "'" | cut -d " " -f3 | cut -d ":" -f1`
198 NHWAN
=`dmesg | grep cdc_ncm | grep register | grep "'" | cut -d " " -f3 | cut -d ":" -f1`
200 IS_HWAN
=`echo $HWAN | wc -w`
201 IS_NHWAN
=`echo NHWAN | wc -w`
203 if [ "$IS_HWAN" -gt 0 ]; then
204 logger
4G MODEM WAN found
- Hilink
- using
$HWAN as WAN
205 nvram
set wan_4g
="$HWAN"
206 nvram
set modem_type
=hilink
207 elif [ "$IS_NHWAN" -gt 0 ]; then
208 logger
4G MODEM WAN found
- Non-Hilink
- using
$NHWAN as WAN
209 nvram
set wan_4g
="$NHWAN"
210 nvram
set modem_type
=nonhilink
212 logger
4G MODEM WAN not found
- exit
217 if [ "$IS_PIN" == "1" -a "$NH" -gt 0 ]; then #only for non-hilink
218 PINCODE
="$PIN" gcom
-d $DEVNR -s /etc
/gcom
/setpin.gcom
221 if [ "$NH" -gt 0 ]; then #only for non-hilink
222 #check signal strength
223 CSQ
=`gcom -d $DEVNR -s /etc/gcom/getstrength.gcom | grep "CSQ:" | cut -d " " -f2 | cut -d "," -f1`
225 logger
"4G MODEM Signal Strength: $DBM dBm"
228 #force connection after detect 4G modem