blob: 9314e4927b332dd0ab470a824a9216220070b2c2 [file] [log] [blame]
#!/bin/sh
#
# wan setup: dial and setup ip/dns/route
#
##usage:
#parameters:
# CID=$1 ,cid for the pdp - optional parameter
# if empty than for LTE: cid=4, 3G: cid=1
# APN=$2 ,APN name - optional parameter
# IP_TYPE=$3 ,IP type (IP, IPV6, IPV4V6)
# AUTO=$4 ,optional parameter, AUTO=0/1
# if auto is set than wsetup will wait upon attach
# max waiting time is 3 sec * 40 MAX_TRY = 120 sec
deactive_if_active(){
status=`serial_atcmd "AT+CGACT?"`
error=`echo $status | grep -i ERROR`
if [ "$error" != "" ]; then
echo "CGDACT? READ ERROR"
exit 3
fi
configured=`echo "$status" | grep "$(($CID+1)),1"`
if [ -z "$configured" ]; then
return
fi
status=`serial_atcmd "AT+CGACT=0,$(($CID+1))"`
error=`echo $status | grep -i ERROR`
if [ "$error" != "" ]; then
echo "CGDACT DEACTIVATE ERROR"
exit 3
fi
}
configure_pdp(){
status=`serial_atcmd "AT+CGDCONT=$(($CID+1)),\"$IP_TYPE\",\"$APN\",\"\",0,0"`
error=`echo $status | grep -i ERROR`
if [ "$error" != "" ]; then
echo "CGDCONT SET ERROR"
exit 3
fi
}
activate_pdp(){
status=`serial_atcmd "AT+CGACT=1,$(($CID+1))"`
error=`echo $status | grep -i ERROR`
if [ "$error" != "" ]; then
echo "CGACT SET ERROR"
exit 3
fi
}
set_and_activate_pdp(){
deactive_if_active
configure_pdp
activate_pdp
}
cleanup_cid()
{
uci delete network.wan$CID.ipaddr > /dev/null 2>&1
uci delete network.wan$CID.netmask > /dev/null 2>&1
uci delete network.wan$CID.gateway > /dev/null 2>&1
uci delete network.wan$CID.ip6addr > /dev/null 2>&1
uci delete network.wan$CID.dns > /dev/null 2>&1
}
set_dns(){
# Configure DNS
DNS_PRIMARY=`echo "$status" | awk -F "," '{print $6}' | tr -d '"'`
DNS_SECOND=`echo "$status" | awk -F "," '{print $7}' | tr -d '"'`
if [ -n "$DNS_PRIMARY" ]; then
uci add_list network.wan$CID.dns=$DNS_PRIMARY
fi
if [ -n "$DNS_SECOND" ]; then
uci add_list network.wan$CID.dns=$DNS_SECOND
fi
}
check_success(){
status=`serial_atcmd "AT+CGACT?"`
error=`echo $status | grep -i ERROR`
if [ "$error" != "" ]; then
echo "CGDACT? READ ERROR"
exit 3
fi
configured=`echo "$status" | grep "$(($CID+1)),1"`
if [ -z "$configured" ]; then
echo "wsetup: connect failed!"
exit 3
else
ERR_STAT=0
echo "wsetup connect success CID=$CID APN=$APN"
fi
}
#################
# main entrance #
#################
CID=$1
APN=$2
IP_TYPE=$3
AUTO=$4
ERR_STAT=1
logfile="./wsetup.log"
TRY=0
MAX_TRY=40
if [ "$AUTO" == "1" ]; then
echo "AUTO MODE LOG" > $logfile
fi
if [ -z "$IP_TYPE" ]; then
IP_TYPE="IP"
fi
while [ $ERR_STAT -eq 1 ]; do
if [ "$AUTO" == "1" ]; then
echo "TRY=$TRY" >> $logfile
let TRY+=1
sleep 3
if [ $TRY -eq $MAX_TRY ]; then
exit 1
fi
fi
## check AT channel
status=`serial_atcmd "AT" | grep OK`
if [ -z "$status" ]; then
if [ "$AUTO" == "1" ]; then
echo "AT CHANNEL ERROR" >> $logfile
continue
else
echo "AT CHANNEL ERROR"
exit 1
fi
fi
## check SIM avilable
status=`serial_atcmd "AT+CPIN?" | grep READY`
if [ -z "$status" ]; then
if [ "$AUTO" == "1" ]; then
echo "SIM CARD NOT AVAILABLE" >> $logfile
continue
else
echo "SIM CARD NOT AVAILABLE"
exit 1
fi
fi
## check registration
rat=`serial_atcmd "AT+COPS?" | grep COPS | awk -F "," '{print $NF}'`
if [ -z "$rat" ]; then
if [ "$AUTO" == "1" ]; then
echo "COPS READ ERROR" >> $logfile
continue
else
echo "COPS READ ERROR"
exit 3
fi
fi
if [ $rat != 7 ]; then
## check registration 2G/3G
status=`serial_atcmd "AT+CGREG?" | grep CGREG | awk -F "," '{print $2}'`
if [ "$status" != "1" ] && [ "$status" != "5" ]; then
if [ "$AUTO" == "1" ]; then
echo "REGISTERATION FAILED" >> $logfile
continue
else
echo "REGISTERATION FAILED"
exit 1
fi
fi
#in case of 3G cid=0 is the default case
if [ -z "$CID" ]; then
CID=0
fi
set_and_activate_pdp
else
## check registration 4G
status=`serial_atcmd "AT+CEREG?" | grep CEREG | awk -F "," '{print $2}'`
if [ "$status" != "1" ] && [ "$status" != "5" ]; then
if [ "$AUTO" == "1" ]; then
echo "REGISTERATION FAILED" >> $logfile
let TRY+=1
continue
else
echo "REGISTERATION FAILED"
exit 1
fi
fi
if [ -z "$CID" ]; then
#in case of LTE cid=4 is the default case
CID=4
fi
if [ $CID -ne 4 ]; then
#Activate PDP only for MO case
set_and_activate_pdp
fi
fi
# Configure WAN
status=`serial_atcmd "AT+CGPIAF=1,0,0,1"`
error=`echo $status | grep -i ERROR`
if [ "$error" != "" ]; then
echo "CGPIAF READ ERROR"
exit 3
fi
status=`serial_atcmd "AT+CGDCONT?"`
error=`echo $status | grep -i ERROR`
if [ "$error" != "" ]; then
echo "CGDCONT? READ ERROR"
exit 3
fi
PROTO=`echo "$status" | grep "CGDCONT: $(($CID+1))" | awk -F "," '{print $2}' | tr -d '"'`
IPV6=`echo "$PROTO" | grep 6`
IPV4=`echo "$PROTO" | grep -v "IPV6"`
WAN_IP=`echo "$status" | grep "CGDCONT: $(($CID+1))" | awk -F "," '{print $4}' | tr -d '"'`
cleanup_cid
uci set network.wan$CID.ifname=ccinet$CID
uci set network.wan$CID.proto=static
if [ -n "$IPV4" ]; then
WAN_IP4_IP=`echo $WAN_IP | cut -d " " -f1`
WAN_IP=`echo $WAN_IP | cut -d " " -f2`
uci set network.wan$CID.ipaddr=$WAN_IP4_IP
uci set network.wan$CID.netmask=255.255.255.255
uci set network.wan$CID.gateway=$WAN_IP4_IP
fi
if [ -n "$IPV6" ]; then
uci set network.wan$CID.ip6addr=$WAN_IP/64
fi
status=`serial_atcmd "AT+CGCONTRDP=$(($CID+1))"`
error=`echo $status | grep -i ERROR`
if [ "$error" != "" ]; then
echo "CGCONTRDP READ ERROR"
exit 3
fi
status=`echo $status | grep CGCONTRDP`
set_dns
status=`echo $status | awk -F "CGCONTRDP:" '{print $3}'`
if [ -n "$status" ]; then
#IPv6 DNS again for Ipv6
set_dns
fi
# Save changes
uci commit network
/etc/init.d/network reload
ifup wan$CID
if [ -n "$IPV6" ]; then
ifup wan6$CID
fi
check_success
done