Reply
Thread Tools
Posts: 5 | Thanked: 0 times | Joined on Jan 2010
#121
Anyway to check if the script is running and working?
 
Posts: 17 | Thanked: 14 times | Joined on Jan 2010 @ London, UK
#122
Originally Posted by msnger View Post
Anyway to check if the script is running and working?
For the merged script? You should see one of the yellow notification popups a few seconds after connecting to gprs or wlan and one when it disconnects.

From the command line
ps | grep auto
will show if any autodisconnect is running.
 
Posts: 43 | Thanked: 32 times | Joined on Jan 2010
#123
Originally Posted by calvin_42 View Post
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!
It may be wrong for me to answer but since most of the operational code of mclarson seems to be derivated from mine, I'll answer point 3. Put bluntly, your script doesn't fully work for me. When using my wi-fi at home the n900 receives 1 packet of 42 bytes or so every 20 (aprox) seconds. I've tested other 2 wi-fi spots and got another similar situation and another one where I don't receive any packets (and your script would work). Same configuration in the device. That's why I decided to use a small amount of bytes to asure the connection is idle and those are random packets. I don't use TX due to the low traffic and the possible erroneous disconnections that it may cause.
 
calvin_42's Avatar
Posts: 286 | Thanked: 219 times | Joined on Feb 2010 @ France
#124
Originally Posted by mclarson View Post
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..
In this case, for me, that's not an idle connexion. I consider an idle connexion as a connexion which is not used by any programs running on the device. If your device checks emails, you do not have to close the connexion.

Maybe in the end we're just using a different definition for the term "idle".

Originally Posted by mclarson View Post
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 totally agree with you. Of course there will be a lot more RX than TX packets. But you will have several TX packets and then, enough to detect that the connexion is active and used.

What are your 2k per minute used for ? Emails ? Weather update ? Then again we are not using the different definition of the term 'idle'.

Anyway that's interesting because we have both different needs and it has to be taken in account to make a generic app for that.

Thanks again for your feedback!

Last edited by calvin_42; 2010-02-12 at 03:47. Reason: Spelling
 
calvin_42's Avatar
Posts: 286 | Thanked: 219 times | Joined on Feb 2010 @ France
#125
Originally Posted by azstunt View Post
It may be wrong for me to answer but since most of the operational code of mclarson seems to be derivated from mine, I'll answer point 3. Put bluntly, your script doesn't fully work for me. When using my wi-fi at home the n900 receives 1 packet of 42 bytes or so every 20 (aprox) seconds. I've tested other 2 wi-fi spots and got another similar situation and another one where I don't receive any packets (and your script would work). Same configuration in the device. That's why I decided to use a small amount of bytes to asure the connection is idle and those are random packets.
Hey azstunt!

Thanks for the feedback. I didn't asked to you directly because I knew what your answers would be (I have read all your interesting previous posts).

Do you think you could you try to identificate the origin of the 42 bytes ? I'm curious and it would help me to understand the process. I just ran a few tests with my home wifi connexion but unfortunately I didn't log any received or sent packets on my device. That's why I assumed my script worked.

Originally Posted by azstunt View Post
I don't use TX due to the low traffic and the possible erroneous disconnections that it may cause.
As I said before, even if they are a lot more RX packets than TX, I am pretty sure the proportion is the same (TCP needs feedback from the recipient).
 
Posts: 8 | Thanked: 0 times | Joined on Feb 2010 @ Netherlands
#126
@mclarson: I've copied your code but while executing I get the following error: line 60: syntax error: "then" unexpected (expecting "{")
 
Posts: 17 | Thanked: 14 times | Joined on Jan 2010 @ London, UK
#127
I think the script should be given a simple GUI, packaged and then see what feedback it gets. I've been wanting something like this since I got my n900 back in November. For me 3g eats up way too much battery life and if I forget to turn it off I find myself without a phone when I most need it.

Many thanks to azstunt for getting the bulk of the work done and to calvin_42 for the extra touches. So far it works ok for me but I'm still trying to find optimal settings.
 

The Following 2 Users Say Thank You to mclarson For This Useful Post:
Posts: 17 | Thanked: 14 times | Joined on Jan 2010 @ London, UK
#128
Originally Posted by thofstee View Post
@mclarson: I've copied your code but while executing I get the following error: line 60: syntax error: "then" unexpected (expecting "{")
The 'if' at the beginning of the line has gone missing. Re-copy and try again.
 

The Following User Says Thank You to mclarson For This Useful Post:
Posts: 52 | Thanked: 10 times | Joined on Jan 2010 @ London, UK
#129
Originally Posted by mclarson View Post
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. 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!
This is a brilliant idea, could we get this created as an actual Maemo application?

I can see this benefitting alot of people
 
calvin_42's Avatar
Posts: 286 | Thanked: 219 times | Joined on Feb 2010 @ France
#130
Originally Posted by sandybeach View Post
This is a brilliant idea, could we get this created as an actual Maemo application?

I can see this benefitting alot of people
Working on it!
 

The Following User Says Thank You to calvin_42 For This Useful Post:
Reply

Tags
automatic, connect, connect automatically, connect on activity, connect on demand, connect on use, data, data connection, disconnect, disconnect automatically, disconnect when idle, fremantle, gprs, internet, maemo, maemo 5, n900, on demand, on use


 
Forum Jump


All times are GMT. The time now is 11:36.