Adding state to wifi. - conn - A script repository to manage connections in Linux.
       
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit c801a452d5e4e90b1d88115d39284904ba6f604f
 (DIR) parent e4af62c70aa8552ca642cfaf72ce51edcdb75eab
 (HTM) Author: Christoph Lohmann <20h@r-36.net>
       Date:   Tue,  1 Mar 2011 15:22:50 +0100
       
       Adding state to wifi.
       
       Diffstat:
         config.mk                           |       2 +-
         etc/conn/common.sh                  |      48 +++++++++++++++++++------------
         etc/conn/run.sh                     |      12 ++++++------
         examples/wifi/wlan0-action.sh       |      25 ++++++++++++++++++++++++-
       
       4 files changed, 60 insertions(+), 27 deletions(-)
       ---
 (DIR) diff --git a/config.mk b/config.mk
       @@ -1,5 +1,5 @@
        # conn version
       -VERSION = 0.2
       +VERSION = 0.3
        
        # Customize below to fit your system
        
 (DIR) diff --git a/etc/conn/common.sh b/etc/conn/common.sh
       @@ -43,7 +43,7 @@ lock() {
        
        unlock() {
                LOCKF="$1.lock"
       -        [ -e $LOCKF ] && rm $LOCKF
       +        rm $LOCKF
        }
        
        ## Global conn lock, when there is database access.
       @@ -57,40 +57,50 @@ unlockconn() {
        }
        
        ## State handling
       -OPENCONNFILE="$STATEDIR/open"
       -getopenconnections() {
       -        [ ! -e $OPENCONNFILE ] && return 1
       -        [ `stat -c%s $OPENCONNFILE` -eq 0 ] && return 1
       -        cat $OPENCONNFILE
       +getstate() {
       +        [ ! -e $STATEDIR/$1 ] && return 1
       +        [ `stat -c%s $STATEDIR/$1` -eq 0 ] && return 1
       +        cat $STATEDIR/$1
                return 0
        }
        
       -isopen() {
       -        isin=`getopenconnections | grep "$1 $2"`
       +getstates() {
       +        getstate $1 | grep "$2 $3"
       +        return 0
       +}
       +
       +isset() {
       +        isin=`getstate $1 | grep "$2 $3"`
                [ "$isin" == "" ] && return 1 
                return 0
        }
        
       -addconnection() {
       -        isopen $1 $2 && return 1
       -        echo "$1 $2" >> $OPENCONNFILE
       +addstate() {
       +        isset $1 $2 $3 && return 1
       +        echo "$2 $3" >> $STATEDIR/$1
        }
        
       -delconnection() {
       -        [ ! -e $OPENCONNFILE ] && return 1
       +delstate() {
       +        [ ! -e $STATEDIR/$1 ] && return 1
        
       -        if ! isopen $1 $2;
       +        if ! isset $1 $2 $3;
                then
                        return 1 
                else
       -                cat $OPENCONNFILE | grep -v "$1 $2" \
       -                        > $OPENCONNFILE.bak
       -                mv $OPENCONNFILE.bak $OPENCONNFILE
       +                cat $STATEDIR/$1 | grep -v "$2 $3" \
       +                        > $STATEDIR/$1.bak
       +                mv $STATEDIR/$1.bak $STATEDIR/$1
                fi
        }
        
       -getdefaultconnection() {
       -        echo "$DEFAULTCONNECTION $DEFAULTPROFILE"
       +getdefaultstate() {
       +        case "$1" in
       +                open)
       +                        echo "$DEFAULTCONNECTION $DEFAULTPROFILE"
       +                        ;;
       +                *)
       +                        ;;
       +        esac
        }
        
        ## WPA handling 
 (DIR) diff --git a/etc/conn/run.sh b/etc/conn/run.sh
       @@ -73,7 +73,7 @@ fi
        
        if [ $dolistopen -eq 1 ];
        tthen
       -        conns=`getopenconnections`
       +        conns=`getstate "open"`
                if [ "$conns" == "" ];
                then
                        echo "There are no connections open."
       @@ -116,9 +116,9 @@ if [ "$1" == "" ];
        tthen
                if [ $dostart -eq 1 ] && [ ! $dowakeup -eq 1 ];
                then
       -                conns=`getdefaultconnection`
       +                conns=`getdefaultstate "open"`
                else
       -                conns=`getopenconnections`
       +                conns=`getstate "open"`
                fi
        
                [ "$conns" == "" ] && exit 0
       @@ -141,15 +141,15 @@ if [ ! $doforce -eq 1 ];
        tthen
                if [ $dostart -eq 1 ];
                then
       -                if isopen $connection $profile;
       +                if isset "open" $connection $profile;
                        then
                                echo "Connection $connection $profile is already open"
                                echo "Use -k or -f for killing it."
                                exit 1 
                        fi
       -                addconnection $connection $profile
       +                addstate "open" $connection $profile
                fi
       -        [ $dokill -eq 1 ] && delconnection $connection $profile
       +        [ $dokill -eq 1 ] && delstate "open" $connection $profile
        fi
        
        if [ $# -lt 2 ];
 (DIR) diff --git a/examples/wifi/wlan0-action.sh b/examples/wifi/wlan0-action.sh
       @@ -4,7 +4,8 @@
        
        interface="$1"
        action="$2"
       -ssid=`getssid $interface`
       +ssid="$3"
       +t[ "$ssid" == "" ] && ssid=`getssid $interface`
        
        t[ $LOGGING -eq 1 ] && \
                logger -t "$interface-action" "Got request for $interface $ssid $action."
       @@ -13,6 +14,28 @@ getscript() {
                awk -F'\t' "/$1\$/ {print \$1}" networks.tbl
        }
        
       +case "$action" in
       +        CONNECTED)
       +                aps=`getstates "wifi" $interface $ssid`
       +                if [ "$aps" != "" ];
       +                then
       +                        for i in "$aps":
       +                        do
       +                                issid=`echo $i | awk '{print $2}'`
       +                                $0 $interface DISCONNECTED $issid
       +                        done
       +                fi
       +
       +                addstate "wifi" $interface $ssid
       +                ;;
       +        DISCONNECTED)
       +                delstate "wifi" $interface $ssid
       +                ;;
       +        *)
       +                exit 1;
       +                ;;
       +esac
       +
        script=`getscript $ssid`
        if [ "$script" != "" ];
        tthen