2010-01-22

Simplified Option Icon 255 on Gentoo

I have an Option Icon 255 from work to use when I'm out of the office. It is a 3G USB pen. I've used many outside scripts and graphical user interfaces but I never liked them. They crashed a lot and never seemed natural. Also I wanted a script that actually worked all the time instead of failing sometimes because it took the device a couple more seconds to register in the network.

My solution: a mix of udev and shell scripts.

First I made udev rules for the device. I want it to always have the same name on the /dev file system and that as soon as I connect it to the laptop it should validate the PIN and register in the network.I created a "49-hso.rules" (hso is the name of the kernel driver for the device) in the "/etc/udev/rules.d" folder as follows:
ACTION!="add", GOTO="hso_end"

SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTR{hsotype}=="Control", SYMLINK+="wctrl0"
SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTR{hsotype}=="Application", SYMLINK+="wapp0"
SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTR{hsotype}=="Application", SYMLINK+="wappa0"
SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTR{hsotype}=="Application2",SYMLINK+="wappb0"
SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTR{hsotype}=="Diagnostic", SYMLINK+="wdiag0"
SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTR{hsotype}=="Diagnostic", SYMLINK+="wdiaga0"
SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTR{hsotype}=="Diagnostic2", SYMLINK+="wdiagb0"
SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTR{hsotype}=="Modem", SYMLINK+="wmodem0"
SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTR{hsotype}=="GPS", SYMLINK+="wgps0"
SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTR{hsotype}=="GPS_Control", SYMLINK+="wgpsc0"
SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTR{hsotype}=="PCSC", SYMLINK+="wpcsc0"

KERNEL=="ttyHS[0-9]*", NAME="%k", GROUP="plugdev", MODE="0660"

ATTRS{idVendor}=="0af0", ATTRS{idProduct}=="6971", RUN+="/etc/hso/setPin"

LABEL="hso_end"
I copied these rules from here. They should work with other hso devices, but I never tested it.

The magic is all in "/etc/hso" (I created this folder to hold all the scripts). First I created the "setPin" script as follows:
#!/bin/bash

OUTPUTFILE=/tmp/output.hso-chat

( /usr/sbin/chat -E -s -V -f /etc/hso/pin-chat < /dev/wctrl0 > /dev/wctrl0 ) 2> $OUTPUTFILE
I leave the tmp file as I might want to debug it. The "chat-pin" chat script is the following (remember to put your PIN where "PIN-HERE" is written since I removed mine):
ABORT ERROR
TIMEOUT 10
"" ATZ
OK "AT+CPIN=\"PIN-HERE\"^m"
OK "\d\d\d\d\d\d\dAT+COPS=?^m"
OK "AT+CGDCONT=1,,\"internet\"^m"
Yes, the "^m" are on the spot. You may need to adapt the apn name (mine is internet). If you have a user and password will have to add it to the AT+CGDCONT command. Just check the wiki for it. With these steps you should be able to plug the device and notice that it registers with the network. To connect to the network I created a "/etc/hso/connect" script as follows:
#!/bin/bash                 

PIP=""
COUNTER=""
OUTPUTFILE="/tmp/hso.chat"
DEVICE="/dev/wctrl0"
NETDEV=hso0

while [ -z "$PIP" -a "$COUNTER" != "------" ]
do
echo "trying$COUNTER"
sleep 2
rm -f $OUTPUTFILE
( /usr/sbin/chat -E -s -V -f /etc/hso/con-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

if [ -z "$PIP" ]
then
echo "We did not get an IP address from the provider, bailing ..."
cat $OUTPUTFILE
rm -f $OUTPUTFILE
exit
fi
rm -f $OUTPUTFILE

echo "Setting IP address to $PIP"
ifconfig $NETDEV $PIP netmask 255.255.255.255 up

echo "Adding route"
route add default dev $NETDEV

echo "Adding name servers"
( echo nameserver $NS1 ; echo nameserver $NS2 ) | resolvconf -a $NETDEV

echo "Done!"


The "/etc/hso/con-chat" script referenced is as follows:
ABORT ERROR
TIMEOUT 10
"" ATZ
OK "AT_OWANCALL=1,1,0^m"
OK "\d\d\d\d\dAT_OWANDATA=1^m"
OK ""
And with this it should work. Notice that I'm using openresolv to manage my name servers. If you aren't then you probably are better of changing the "connect" script to copy the previous resolv.conf and replace it with another. I just prefer to have openresolv since it takes care of things such as restarting the nscd (Naming Service Cache Daemon, if you are wondering). My end goal is to use dnsmasq and to route only the DNS requests to the company VPN. For that I'm better off using openresolv.

Now that you are connected you need to be able to disconnect :-). The script is very simple:
#!/bin/bash

DEVICE="/dev/wctrl0"
NETDEV=hso0

ifconfig $NETDEV down

/usr/sbin/chat -V -f /etc/hso/dis-chat <$DEVICE >$DEVICE 2> /dev/null

resolvconf -d $NETDEV
And you also need a chat script in "/etc/hso/dis-chat" as follows:
TIMEOUT 10
ABORT ERROR
"" ATZ
OK "AT_OWANCALL=1,0,0^m"
OK ""
And that should do it. At least it works for me :-)

No comments:

Post a Comment