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)

msnger 2010-02-11 21:00

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Anyway to check if the script is running and working?

mclarson 2010-02-11 21:33

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

Originally Posted by msnger (Post 521908)
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.

azstunt 2010-02-11 22:19

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
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 2010-02-12 03:33

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

Originally Posted by mclarson (Post 521782)
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".

Quote:

Originally Posted by mclarson (Post 521782)
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!

calvin_42 2010-02-12 03:45

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

Originally Posted by azstunt (Post 522017)
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.

Quote:

Originally Posted by azstunt (Post 522017)
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).

thofstee 2010-02-12 12:39

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
@mclarson: I've copied your code but while executing I get the following error: line 60: syntax error: "then" unexpected (expecting "{")

mclarson 2010-02-12 13:19

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
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.

mclarson 2010-02-12 13:28

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

Originally Posted by thofstee (Post 522863)
@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.

sandybeach 2010-02-12 13:37

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

Originally Posted by mclarson (Post 521034)
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 2010-02-12 17:06

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

Originally Posted by sandybeach (Post 522933)
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!

raily 2010-02-12 18:23

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
today i was able to test icq idling over 3g. seems i have to play around with the bytes-count taken into accout for determining unused connection.

azstunt 2010-02-12 20:17

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

Originally Posted by calvin_42 (Post 522416)
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.



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).

Sincerely, I have no idea where these packets are coming from, I've already checked for anything that might be connecting or updating, do you know any command to check actual connections?

You could always keep your script as it is, I mean, in theory that's the way it should work. But considering that you are making an app for end-users, I believe it would be wise to know why it may not work for everybody and how to fix it. Having said that, it may be just for me, so I won't draw any conclusions until many people have tested if they receive or send packets when not using the connection.


PS: My n900 not only receives 1 packet of 42 bytes every 20 seconds or so, it also sends 1 packet of 60 bytes at the same interval. I had not payed attention to TX since I was not using it in my script.

calvin_42 2010-02-13 18:31

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

Originally Posted by azstunt (Post 523696)
Sincerely, I have no idea where these packets are coming from, I've already checked for anything that might be connecting or updating, do you know any command to check actual connections?

Code:

apt-get install tcpdump
See http://www.tcpdump.org/tcpdump_man.html for documentation.

azstunt 2010-02-13 22:22

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

Originally Posted by calvin_42 (Post 524986)
Code:

apt-get install tcpdump
See http://www.tcpdump.org/tcpdump_man.html for documentation.

Ok, I've installed it... fooled around with it a bit, etc. Anyway, is any of this useful?

Code:

$ tcpdump -i wmaster0 -vv
tcpdump: WARNING: can't create rx ring on packet socket 3: 92-Protocol not avalaible
tcpdump: WARNING: wmaster0: no IPv4 address assigned
tcpdump: listening on wmaster0, link-type IEEE802_11 (802.11), capture size 96 bytes
14:50:44.391267 0us ARP, Ethernet (len 6), IPv4 (len 4), Reply 1 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
14:50:02.515687 0us ARP, Ethernet (len 6), IPv4 (len 4), Reply 1 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
14:50:22.484437 0us ARP, Ethernet (len 6), IPv4 (len 4), Reply 1 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
14:50:43.375062 0us ARP, Ethernet (len 6), IPv4 (len 4), Reply 1 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
14:50:02.523560 0us ARP, Ethernet (len 6), IPv4 (len 4), Reply 1 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
14:50:22.390687 0us ARP, Ethernet (len 6), IPv4 (len 4), Reply 1 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
14:50:42.350495 0us ARP, Ethernet (len 6), IPv4 (len 4), Reply 1 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
14:50:02.531312 0us ARP, Ethernet (len 6), IPv4 (len 4), Reply 1 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
14:50:22.500031 0us ARP, Ethernet (len 6), IPv4 (len 4), Reply 1 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28

and

Code:

$ tcpdump -i wlan0
tcpdump: WARNING: can't create rx ring on packet socket 3: 92-Protocol not avalaible
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
tcpdump: listening on wlan0, link-type EN10MB (Ethernet), capture size 96 bytes
15:43:40.320800 ARP, Request who-has 192.168.1.68 (Broadcast) tell 192.168.1.254, length 28
15.43.40.321075 ARP, Reply 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
15:44:00.404540 ARP, Request who-has 192.168.1.68 (Broadcast) tell 192.168.1.254, length 28
15.44.00.404815 ARP, Reply 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
15:44:21.500182 ARP, Request who-has 192.168.1.68 (Broadcast) tell 192.168.1.254, length 28
15.44.21.500457 ARP, Reply 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
15:44:40.444274 ARP, Request who-has 192.168.1.68 (Broadcast) tell 192.168.1.254, length 28
15.44.40.444549 ARP, Reply 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28


mclarson 2010-02-14 01:27

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

Originally Posted by azstunt (Post 525219)
Code:

$ tcpdump -i wlan0
tcpdump: WARNING: can't create rx ring on packet socket 3: 92-Protocol not avalaible
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
tcpdump: listening on wlan0, link-type EN10MB (Ethernet), capture size 96 bytes
15:43:40.320800 ARP, Request who-has 192.168.1.68 (Broadcast) tell 192.168.1.254, length 28
15.43.40.321075 ARP, Reply 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
15:44:00.404540 ARP, Request who-has 192.168.1.68 (Broadcast) tell 192.168.1.254, length 28
15.44.00.404815 ARP, Reply 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
15:44:21.500182 ARP, Request who-has 192.168.1.68 (Broadcast) tell 192.168.1.254, length 28
15.44.21.500457 ARP, Reply 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28
15:44:40.444274 ARP, Request who-has 192.168.1.68 (Broadcast) tell 192.168.1.254, length 28
15.44.40.444549 ARP, Reply 192.168.1.68 is-at c0:38:f9:b3:07:02 (oui Unknown), length 28


It's your access point at .254 address checking that you're still there every 20s, like you said before. These will be 28 byte rx and tx packets. A very quiet connection for the sample period.

calvin_42 2010-02-17 01:32

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Hi there! The application is almost finished. Need to build the .deb package now.

I made a small script in if.d :

Code:

#!/bin/sh
echo "$IFACE" >> /var/log/ifup.log

-----------------------

When I connect to the Wifi, $IFACE="wlan0", but when i connect to the 3G, $IFACE is empty instead of containing "gprs0".

Any idea why ?
Thanks.

UPDATE : it appears to be a known issue : https://bugs.maemo.org/show_bug.cgi?id=7733. Please vote!

low life 2010-02-18 15:11

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Is there a way to establish the gprs0 connection from the command line/with a simple script?

hollow 2010-02-18 15:52

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
installed this script around a week ago seemed to work fine till phone was re-booted . now doesnt work wifi never disconnects when idle.

calvin_42 2010-02-18 16:10

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

Originally Posted by hollow (Post 534627)
installed this script around a week ago seemed to work fine till phone was re-booted . now doesnt work wifi never disconnects when idle.

I've made an application here with a .deb package : https://garage.maemo.org/frs/?group_id=1353

Hope it will be on Extra-Devel soon.

raily 2010-02-18 16:15

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

Originally Posted by hollow (Post 534627)
installed this script around a week ago seemed to work fine till phone was re-booted . now doesnt work wifi never disconnects when idle.

after the system update some days ago, the script does not properly worl for me either. the connection seems to be taken down, but the connection manager show still connected but no packages can be received by any applkication. i had to manua
lly disconnect and reconnect wifi to make it work again

calvin_42 2010-02-18 23:59

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
The application AutoDisconnect is now available in the extra-devel repository.

The thread for this app is here. Don't hesitate to post feedback there!

mclarson 2010-02-19 19:52

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Note to all people with questions about the script I posted: Please use the autodisconnect calvin_42 uploaded to extras-devel. And don't forget to disable extras-devel afterwards blah blah!

Matan 2010-02-19 20:04

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

Originally Posted by low life (Post 534543)
Is there a way to establish the gprs0 connection from the command line/with a simple script?

Yes. Something like
Code:

dbus-send --type=method_call --system --dest=com.nokia.icd /com/nokia/icd com.nokia.icd.connect string:"Internet Pelephone" uint32:0
Where "Internet Pelephone" should be replaced by the i.d. of your connection. Find it by examining the output of

gconftool-2 -R /system/osso/connectivity/IAP

low life 2010-02-20 13:39

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

Originally Posted by Matan (Post 537199)
Yes. Something like
Code:

dbus-send --type=method_call --system --dest=com.nokia.icd /com/nokia/icd com.nokia.icd.connect string:"Internet Pelephone" uint32:0
Where "Internet Pelephone" should be replaced by the i.d. of your connection. Find it by examining the output of

gconftool-2 -R /system/osso/connectivity/IAP

Ah it works, thanks a lot (again)! I thought I tried that at some point and it didn't work, but I guess I did something a bit different.. That and the wiki saying that it wouldn't work with GPRS connection made me give up trying it that way. Either way, awesome!


All times are GMT. The time now is 03:28.

vBulletin® Version 3.8.8