maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Maemo 5 / Fremantle (https://talk.maemo.org/forumdisplay.php?f=40)
-   -   Connect to Internet on demand & disconnect automatically, particularly for GPRS? (https://talk.maemo.org/showthread.php?t=38832)

azstunt 2010-02-10 23:42

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Hello everybody. Just to let you know that I've updated my script (more like the whole instructions). Now, the script only runs when a connection is active and it closes itself after the device is disconnected (instead of running at boot and staying like that). If you had already used my script, please re-read the whole instructions, a couple of things besides the script are different. Please see back here post #107 or click here http://talk.maemo.org/showpost.php?p...&postcount=107

msnger 2010-02-11 01:12

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Thanks for the script! :D

hey Azstunt can you help me with the install?

I just finish installing VIM and Rootfs

how do I install the script?

azstunt 2010-02-11 01:16

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Quote:

Originally Posted by msnger (Post 520471)
Thanks for the script! :D

hey Azstunt can you help me with the install?

I just finish installing VIM and Rootfs

how do I install the script?

Sorry... it's pretty straight forward as it is... google "vim manual" and give it a read.

msnger 2010-02-11 01:17

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
thanks :D

123456789732

mclarson 2010-02-11 12:04

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
I've merged the scripts from azstunt and calvin_42 since they both have great features - I like the visual notification for starting connection monitoring and ending a connection. if-up was a good choice too.

Seems to work quite well - but not when switching connections. For best results disconnect from a connection before connecting to another one.

Instructions for install pretty much as azstunt says. Read the comments...

/usr/local/bin/autodisconnect:
Code:

#!/bin/sh

# ---------------------------------------------------------------------------
# MODIFY THESE PARAMETERS TO SUIT
# ---------------------------------------------------------------------------

# How often to test for inactivity
g_samplerate=90

# If only a few bytes are received you may still want to disconnect.
# Specify the number of bytes per minute here.
g_dontcountbytespermin=5000

# Don't auto disconnect for the listed interfaces in g_disable
# Effectively disables this script:
#g_disable="wlan gprs"
# Disable for gprs:
#g_disable="gprs"
# Disable for wlan:
#g_disable="wlan"
# Disconnect enabled for wlan and gprs:
g_disable=

# Enable logging
#g_logging="true"
g_logging="false"

# Where to store the logfile. This is cleared when it goes over 100k.
logfile=/root/autodisconnect.log

# ---------------------------------------------------------------------------
# DON'T MODIFY ANYTHING BELOW THIS POINT
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
# GLOBALS
# ---------------------------------------------------------------------------

# Never drop openvpn. Iface is just a heading therefore not real.
g_disable="Iface tun "$g_disable

# Number of bytes to ignore for each time period
g_dontcountbytes=$((g_dontcountbytespermin*g_samplerate/60))

# ---------------------------------------------------------------------------
# FUNCTIONS
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
init()
# ---------------------------------------------------------------------------
{
    # Allow dbus to work
    [[ -e /tmp/dbus-info ]] && eval `cat /tmp/dbus-info`

    export DBUS_SESSION_BUS_ADDRESS \
          DBUS_SESSION_BUS_PID \
          DBUS_SESSION_BUS_WINDOWID

    # Don't let log file get too big
    if [[ -w "$logfile" ]]; then
        size=`stat $logfile | grep Size | awk '{ print $2; }'`
        [[ "$size" -gt 100000 ]] && :>$logfile
    fi
}

# ---------------------------------------------------------------------------
logentry()
# ---------------------------------------------------------------------------
# Send a string to this function to append it to the log file
#
# Arg 1 - The text to log
{
    [[ "$g_logging" == "true" ]] && {
        echo -e "`date` $1" >> $logfile
        echo "`date` $1"
    }
}

# ---------------------------------------------------------------------------
osnotify()
# ---------------------------------------------------------------------------
# Send a string to this function to generate a system popup
#
# Arg 1 - The text to display
{
    /usr/bin/dbus-send --type=method_call \
        --dest=org.freedesktop.Notifications \
        /org/freedesktop/Notifications \
        org.freedesktop.Notifications.SystemNoteInfoprint \
        string:"$1"
}

# ---------------------------------------------------------------------------
disconnect()
# ---------------------------------------------------------------------------
# Disconnect current connection
#
# No args
{
    dbus-send --system --dest=com.nokia.icd \
        /com/nokia/icd_ui \
        com.nokia.icd_ui.disconnect \
        boolean:true
}

# ---------------------------------------------------------------------------
disable_quit()
# ---------------------------------------------------------------------------
{
    # Disable for some interfaces
    [[ -n "$g_disable" ]] && {
        if echo "$g_disable" | grep -qs "$interface" 2>/dev/null; then
            #osnotify "Auto-disconnect disabled for $interface"
            logentry "Auto-disconnect disabled for $interface"
            exit 0
        else
            osnotify "Auto-disconnect enabled for $interface"
            logentry "Auto-disconnect enabled for $interface"
        fi
    }
}

# ---------------------------------------------------------------------------
main()
# ---------------------------------------------------------------------------
# Main entry point
{
    init

    # Log monitor startup to file & notify user via popup
    logentry "Auto-disconnect starting"
    interface=`grep -v tun /proc/net/route | tail -1 | cut -f1 | tr -d 0-9`
    logentry "Current connection : $interface"

    # Quit if this interface is in the g_disable list
    disable_quit

    while true; do
        sleep $g_samplerate
        a=`grep -v tun /proc/net/route | tail -1 |cut -f1`
        while [[ "$a" != "Iface" ]]; do
            b=`ifconfig $a | grep RX.b | cut -d"(" -f1 | cut -d: -f2`
            c=$b
            sleep $g_samplerate
            b=`ifconfig $a | grep RX.b | cut -d"(" -f1 | cut -d: -f2`
            a=`grep -v tun /proc/net/route | tail -1 |cut -f1`
            if [ "$a" == "Iface" ]; then
                osnotify "Auto-disconnect for $interface stopped."
                logentry "$interface lost. Quitting."
                exit 0
            fi
            if [ "$b" -lt "$((c+g_dontcountbytes))" -a "$a" != "Iface" ]; then
                disconnect
                osnotify "Connection $interface closed due to inactivity"
                logentry "$interface closed. Bytes received : $((b-c))"
                exit 0
            else
                logentry "$interface: Bytes received : $((b-c))"
            fi
        done
    done
}

# Start
main

exit 0

/etc/network/if-up.d/99_autodisconnect:
Code:

#!/bin/sh

AUTODISCONNECT=/usr/local/bin/autodisconnect

if [ ! -x $AUTODISCONNECT ]; then
  exit 0
fi

$AUTODISCONNECT

/etc/network/if-down.d/99_autodisconnect:
Code:

#!/bin/sh

AUTODISCONNECT=/usr/local/bin/autodisconnect

if [ ! -x $AUTODISCONNECT ]; then
  exit 0
fi

kill `pidof autodisconnect`

Enjoy!

fred123 2010-02-11 13:17

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
I have got the above script working successfully.

How do I get email to automaticaly connect to network, its set to auto update?

mclarson 2010-02-11 13:44

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Quote:

Originally Posted by fred123 (Post 521123)
I have got the above script working successfully.

How do I get email to automaticaly connect to network, its set to auto update?

Mostly luck I think! My email is set to update every 5 mins and I set my internet connection so that it comes up every 10 mins. My connection idles out after 2 mins with g_samplerate=60 so the email check needs to coincide with this. I don't know what the email retry connection algorithm is but it seems to keep updated pretty well. Maybe there's a better way...

fred123 2010-02-11 14:11

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Thanks I had just set mine up to
connection any
search 5mins

email to update 5mins

as you say seems to work, I will see how it goes.

calvin_42 2010-02-11 18:35

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Hey mclarson!

Thanks for the merge! The script begins to be pretty clean now. I will use it in the application.

I've got a few questions :

1) Why have you chosen to keep the 2 while instructions ? -> See my script with only 1 while
2) Why have you chosen to read the RX instead of the TX value ?
3) Number of bytes to ignore for each time period : where does it come from in fact ? Broadcast ? I ask because in my tests on my device, neither the 3G nor the WLAN connexion have generated transmitted packets when I was connected with no activity. But in my case I read TX value and not the RX value.

Thanks!

mclarson 2010-02-11 19:45

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Quote:

Originally Posted by calvin_42 (Post 521622)
Hey mclarson!

Thanks for the merge! The script begins to be pretty clean now. I will use it in the application.

I've got a few questions :

1) Why have you chosen to keep the 2 while instructions ? -> See my script with only 1 while

I didn't think too much about it. I just used the known working script. But looking at both, yours is more efficient code but run time should be the same. I just noticed I put a useless 'break' after an 'exit'.

Quote:

2) Why have you chosen to read the RX instead of the TX value ?
As the original but made sense to me. TX won't usually make much traffic - I would guess it would be harder to detect an idle connection using TX.

Quote:

3) Number of bytes to ignore for each time period : where does it come from in fact ? Broadcast ? I ask because in my tests on my device, neither the 3G nor the WLAN connexion have generated transmitted packets when I was connected with no activity. But in my case I read TX value and not the RX value.
Interesting. I agreed with azstunt's detection method but did not think too hard about it as it felt right. Conversely I find it hard to believe there are no TX packets - what about email checks, weather updates or whatever.

I would expect there to be a much bigger difference between idle and busy RX than idle and busy TX. More samples should be more accurate. It would take fewer TX packets to affect the measurement considerably.

For a bulk download relativey tiny ACKS would be sent, ie tiny sample, but would have lots of rx, ie big sample. My *idle* connection does about 2k per minute - there /must/ be tx in there but I've not checked what that traffic is.

I'd be intersted to see how well your tx method works.


All times are GMT. The time now is 22:23.

vBulletin® Version 3.8.8