Hi everyone,
I have written a small and simple script which I use here to assign my three modems that are connected using USB to the different kannel instances.

The problem in short:

- I need 3 gsm modems (because there is no routing between the provider's networks in the destination country :-(.) - Therefore I use USB to serial converters to connect my modems to the computer running kannel (on Ubuntu 5.10 Breezy). - The USB devices are managed by the hotplug daemon assigning a "/dev/ttyUSBxxx" to every found device, BUT: - You cannot be sure about the order in which these devices are assigned (especially when you plugout and plugin again.) So /dev/ttyUSB*** changes...
This led me to a few headaches.
- Solution: Fight the multiheaded USB monster! (http://www.linuxjournal.com/article/6518)

NOTE: All modems are connected to ONE USB hub using a PL-2303-USB-Serial converter. (By understanding the syntax of /proc/tty/driver/usbserial you can add more switches of course!)

Step by step through my tiny script:

- The hotplug daemon needs to be restarted by root. Therefor simple check for root first. - Then: Kill all kannel stuff the hard way (easy, just to make sure, normally the script is run at boot time) - Now restart the hotplug daemon so that it reads the devices again and from scratch and automatically assigns them to /dev/ttyUSB0 -> /dev/ttyUSBn
- These information are now stored in /proc/tty/driver/usbserial, hooray!:

<snip>
usbserinfo:1.0 driver:v2.0
0: module:pl2303 name:"PL-2303" vendor:067b product:2303 num_ports:1 port:1 path:usb-0000:00:1d.7-4.1 1: module:pl2303 name:"PL-2303" vendor:067b product:2303 num_ports:1 port:1 path:usb-0000:00:1d.7-4.2 2: module:pl2303 name:"PL-2303" vendor:067b product:2303 num_ports:1 port:1 path:usb-0000:00:1d.7-4.3
</snip>

This reads like:
"0:" -> this is the device /dev/ttyUSB0 (the 0 at the end) that hotplug found. Wonderful, but normally you can't rely on that this is always the same modem. Argl.

"module:pl2303 name:"PL-2303" vendor:067b product:2303" -> some information about the converter, because I use three the same it was not very helpful to identify every modem by this (but can be if you have more than 3 modems)...

"path:usb-0000:00:1d.7-4.1" -> here comes the important stuff. Just read it from the back, so the "1" stands for the physical port 1 of the USB hub, the "7-4" has something to do with the hub itself, then follows the physical address of the USB port to which the hub is connected to. Read the website above to find more about it.

Well the idea is now: I always connect the modem that uses e.g. kannel1.conf to the the physical(!) port 1 of the hub, but cannot be sure that the hotplug will assign it to /dev/ttyUSB0 always (as it mixes it up). Thats why, have look afterwards and make a symlink to the final destination and change kannel1.conf e.g. to the symlink /dev/gsm1


Example: So if it is like

3: module:pl2303 name:"PL-2303" vendor:067b product:2303 num_ports:1 port:1 path:usb-0000:00:1d.7-4.1

The system knows my modem on port 1 (physical port of the hub) as /dev/ttyUSB3 - normally a problem, but with a ln -s /dev/ttyUSB3 /dev/gsm1 I can add /dev/gsm1 as fixed device in my kannel.conf, no matter which device hotplug finds first.

/dev/gsm1 -> /dev/ttyUSB3

I just need 3 modems, and therefore I made only 3 simple if/elif to find whether my modems are found or not.

As it works for me (as simple as this) - perhaps it puts someone into the right direction when fighting the multiheaded USB hydra...


Here the script (no, I'm not a bash god but I wanted to give my 2cents back to the community - perhaps it helps):

greetz from Bonn,
Lars-Hendrik


#!/bin/bash

# setttyUSB.sh - sets /dev/gsm* by hardware address for the GSM modems
#
# written by:   Lars-Hendrik Schneider <[EMAIL PROTECTED]>
# date:         2005-01-23
# last change:  2006-02-13
#
# NOTES:        - needs to be run as root
#

# // CHECK FOR ROOT FIRST

ID=`/usr/bin/id -u`
if [ "$ID" != "0" ]; then
        echo "ERROR: This script needs to be run as root!"
        echo "EXIT."
        exit 1
fi

# // SET PATHS & VARIABLES
USBSERIAL="/proc/tty/driver/usbserial" # (file where usbserial settings are given) DEVICENAME="PL-2303" # (device 'name:' of the serial converter from usbserial file)

UNIQUE=`date | md5sum | sed -ne "1s/^0* //" -ne "s/ //gp"` # make a file/variable/anything unique
TMPFILE=/tmp/$UNIQUE.txt

# // KILLALL BEARERBOX & SMSBOX TO MAKE SURE THAT THEY WILL BE MAPPED CORRECTLY
echo "NOTE: First try to kill all kannel services ..."
echo " * Try to kill smsbox(es) ..."
killall smsbox
echo " * Try to kill bearerbox(es) ..."
killall bearerbox
echo " * Try to kill wapbox(es) ..."
killall wapbox
# // RESTART HOTPLUG TO ENSURE THE DEVICES ARE READ FROM SCRATCH
echo "NOTE: Restarting hotplugd to read USB devices from scratch ..."
/etc/init.d/hotplug restart

# // DUMP USB DEVICE LIST TO TMPFILE
cat $USBSERIAL | grep $DEVICENAME  > $TMPFILE

# // REMOVE ALL OLD /DEV/GSM* - SYMBOLIC LINKS BEFORE SETTING THE NEW ONES
echo "NOTE: Remove old symlinks in /dev/ ..."
rm -f /dev/gsm*

# // READ TMPFILE TO GET DEVICE <-> TTYUSB*
echo "NOTE: Read $USBSERIAL for device linking ..."
exec 3<&0
exec 0<$TMPFILE
while read LINE
do
        TTYUSB=`echo $LINE | cut -d" " -f1`
        USBPATH=`echo $LINE | cut -d" " -f8`
        TTYUSB=`echo $TTYUSB | awk -F':' '{ print $1 }'`
        USBPATH=`echo $USBPATH | awk -F'.' '{ print $3 }'`
        
        if [ "$USBPATH" == "1" ]
        then
echo " * NOTE: GSM modem @ port $USBPATH of the hub is known as /dev/ttyUSB$TTYUSB"
        ln -s /dev/ttyUSB$TTYUSB /dev/gsm$USBPATH
        echo " * NOTE: /dev/ttyUSB$TTYUSB  linked to /dev/gsm$USBPATH"
        # TODO -> Start bearerbox/smsbox for /dev/gsm$USBPATH NOW!

        elif [ "$USBPATH" == "2" ]
        then
echo " * NOTE: GSM modem @ port $USBPATH of the hub is known as /dev/ttyUSB$TTYUSB"
        ln -s /dev/ttyUSB$TTYUSB /dev/gsm$USBPATH
        echo " * NOTE: /dev/ttyUSB$TTYUSB  linked to /dev/gsm$USBPATH"
        # TODO -> Start bearerbox/smsbox for /dev/gsm$USBPATH NOW!

        elif [ "$USBPATH" == "3" ]
        then
echo " * NOTE: GSM modem @ port $USBPATH of the hub is known as /dev/ttyUSB$TTYUSB"
        ln -s /dev/ttyUSB$TTYUSB /dev/gsm$USBPATH
        # TODO -> Start bearerbox/smsbox for /dev/gsm$USBPATH NOW!

        echo " * NOTE: /dev/ttyUSB$TTYUSB  linked to /dev/gsm$USBPATH"
        else
echo " * ERROR: GSM modem @ port $USBPATH of the hub is known as /dev/ttyUSB$TTYUSB - but will not be linked!"
        fi
done

# CLEANUP
echo "NOTE: Remove tmpfiles ..."
rm -f $TMPFILE
echo "NOTE: Done."


Reply via email to