#!/bin/sh
scan_hso() {
local device ttys app_port i
# try to figure out the device
ttys=`find /sys/class/tty -name "ttyHS*"`
device=""
for i in $ttys; do
if [ `grep Application $i/hsotype` ]
then
device=/dev/`echo $i | cut -d/ -f5`
break
fi
done
config_set "$1" device "$device"
}
stop_interface_hso() {
local config="$1"
local chat="/etc/chatscripts/hsohup.chat"
config_get device "$config" device
/usr/sbin/chat -f $chat <$device > $device
killall gcom >/dev/null 2>/dev/null
}
setup_interface_hso() {
local iface="$1"
local config="$2"
local chat="/etc/chatscripts/hso.chat"
config_get device "$config" device
config_get maxwait "$config" maxwait
maxwait=${maxwait:-20}
while [ ! -e "$device" -a $maxwait -gt 0 ];do # wait for driver loading to catch up
maxwait=$(($maxwait - 1))
sleep 1
done
config_get apn "$config" apn
config_get pincode "$config" pincode
config_get mtu "$config" mtu
config_get username "$config" username
config_get password "$config" password
config_get ifname "$config" ifname
# set pin if configured
if [ ! -z "$pincode" ]
then
PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
echo "$config(hso): Failed to set the PIN code."
return 1
}
fi
# set username and password if configured
if [ -n "$username" -a -n "$password" ]
then
USER="$username" PASS="$password" gcom -d "$device" -s /etc/gcom/setuser.gcom || {
echo "$config(hso): Failed to set username and password."
return 1
}
fi
local pip counter
local outputfile="/tmp/hsoout.$$"
while [ -z "$pip" -a "$counter" != "---------------" ]
do
sleep 2
rm -f $outputfile
( USE_APN=$apn /usr/sbin/chat -E -s -V -f $chat <$device > $device ) 2> $outputfile
iserror=`grep '^ERROR' $outputfile`
if [ -z "$iserror" ]
then
pip="`grep '^_OWANDATA' $outputfile | cut -d, -f2`"
ns1="`grep '^_OWANDATA' $outputfile | cut -d, -f4`"
ns2="`grep '^_OWANDATA' $outputfile | cut -d, -f5`"
fi
counter="${counter}-"
done
rm -f $outputfile
if [ -z $pip ]
then
echo "$config(hso): Failed to connect and obtain IP address."
return 1
fi
uci_set network $config ipaddr $pip
uci_set network $config dns "$ns1 $ns2"
uci_set network $config netmask 255.255.255.255
uci_set network $config proto static
ifup $config
route add default dev $ifname
uci revert network
}
local ifname device
. /lib/functions.sh
include /lib/network
scan_interfaces
config_load network
config_get ifname wan ifname
scan_hso wan
if [ "x$1" = "xstop" ]; then
stop_interface_hso wan
ifdown wan
exit 0
fi
setup_interface_hso $ifname wan
exit 0