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)

egoshin 2010-01-31 19:57

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
That is up to you.

There are two basic approach to use N900 - use it as ACCESS device then you don't need an access to you from outside. In this case you can just set it "offline" in "Availability" button (not "Offline mode" - it switches of your cellphone totally ... battery lasts weeks). In this mode your batter would last around 48h, I estimate.

The second approach - stay connected with net/people as possible. In this case you want receive skype/gtalk/email and think about battery.

Matan 2010-01-31 20:01

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
There is another approach - have the device do what the user wants, which in this case is to disconnect from network after a certain period of inactivity. If you do not this behaviour, you don't have to read and comment in this thread, as the title of this thread makes its content very simple. Why come here and try to convince people they don't know what they want?

stayloa 2010-02-02 10:46

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Hey all - I suppose this is a similar question (I don't want to start a new thread and couldn't find it elsewhere), but is it also possible for the internet access to be turned off at certain times? I know there's an app to turn off cell data and leave internet on (which is great), and there's this script here to disable internet after no activity, but I really want to disable internet at night time only. I'd still want to receive calls... Think this is possible? A button would of course be handy as with the cellular modem data button...! Any thoughts?

azstunt 2010-02-06 09:00

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Well... hello everybody.
I don't know if you are still interested in a workaround for this (and a very simple one at that). But reading the codes previously posted here, and via trial and error, I've managed to get a code that works and starts automatically. Sorry 3g guys, my carrier isn't compatible with the n900 frecuency and therefore I can't see how does 3g behaves, maybe someone can adapt my code. Instructions here:

1)
Code:

vim /usr/local/bin/autodisconnect
2) Contents of /usr/local/bin/autodisconnect:
Code:

#!/bin/sh
while true; do
      sleep 60
      a=`tail -1 /proc/net/route | cut -f1`
      while [ "$a" == "wlan0" ]
      do
              b=`grep wlan0 /proc/net/dev | cut -c 8-12`
              c=$b
              sleep 180
              b=`grep wlan0 /proc/net/dev | cut -c 8-12`
              a=`tail -1 /proc/net/route | cut -1`
              if [ "$b" == "$c" -a "$a" == "wlan0" ]; then
                    dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
              fi
              c=$b
      done
done

3)
Code:

chmod a+x /usr/local/bin/autodisconnect
4)
Code:

vim /etc/event.d/autodisconnect
5) Contents of /etc/event.d/autodisconnect
Code:

description "Run a script to disconnect idle wi-fi"

start on started icd2

console none

script
              /usr/local/bin/autodisconnect
end script

6)Reboot device

7)Enjoy!

Credits to matan for the script in event.d. And to farnwomt for some of the lines there.

A few notes here. This script checks every 60 seconds until it finds an established wi-fi connection, it does nothing until then. I'm using grep wlan0 /proc/net/dev | cut -c 8-12 because even when not in use, the wi-fi sends and receives about 1 packet every 30 seconds or so. These are very small packets (most ranging in 30-250 bytes I believe). So what I did was to ignore the last 3-4 digits (depending on how many bytes the device has received since boot-up) in the bytes received and use that as a reference, given that most internet sites are big enough to be about 1-10 kb at least. Not sure how does it behave along with IM (I don't know if IM can receive at least 10kb every 3 mins). For those that decide to test it, it shoud disconnect after 3 mins, but in a bad scenario (unlikely) it should be 6 mins, and in a very bad scenario (very unlikely) it should be 9 mins.

moneytoo 2010-02-07 17:11

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

It is possible to activate it as icd2 idletimer support is still there in
fremantle but you will need kernel support activated.
This is not tested but should probably work:
- compile idletimer kernel module (IDLETIMER config option is in netfilter
configuration), the idletimer files are found in fremantle kernel sources
- install iptables + ipt_IDLETIMER module (take the sources from diablo and
compile)
- load the kernel module before icd2 starts

There is no UI for setting idletimer value. The idletimer settings are per
network type i.e.,
/system/osso/connectivity/network_type/WLAN_INFRA/idle_timeout for WLAN
(cellular network type is called GPRS) and the value is in seconds.
https://bugs.maemo.org/show_bug.cgi?id=5422#c6

Is anyone willing do thy that?

raily 2010-02-07 18:42

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
@azstunt: i tried your script because thats exactly what I was looking for. Unfortunatly it does not disconnect my wlan.
with "ps" i see 2 instances of the script running and there is "sleep 180" there also. so i think it recognizes the established connection. but it does not disconnect, waited for 30 minutes or so. my messengers are offline and mfe-setting is to check every 60 minutes.

azstunt 2010-02-07 21:11

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

Originally Posted by raily (Post 514657)
@azstunt: i tried your script because thats exactly what I was looking for. Unfortunatly it does not disconnect my wlan.
with "ps" i see 2 instances of the script running and there is "sleep 180" there also. so i think it recognizes the established connection. but it does not disconnect, waited for 30 minutes or so. my messengers are offline and mfe-setting is to check every 60 minutes.

I've changed it a bit... try this one, run it from the terminal first... if it doesn't work, paste here the outcomes in terminal

Code:

#!/bin/sh
while true; do
      sleep 10
      a=`tail -1 /proc/net/route | cut -f1`
      echo $a
      b=`grep wlan0 /proc/net/dev | cut -c 8-19`
      echo $b
      while [ "$a" == "wlan0" ]
      do
            c=`expr $b + 1000`
            echo $c
            sleep 10
            b=`grep wlan0 /proc/net/dev | cut -c 8-19`
            echo $b
            d=`expr $b + 1`
            echo $d
            a=`tail -1 /proc/net/route | cut -f1`
            if [ "$d" -lt "$c" -a "$a" == "wlan0" ]; then
                  dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
                  a=`tail -1 /proc/net/route | cut -f1`
                  sleep 5
            fi
      done
done

This one is just to test it works. If it works, change the script and erase all the "echo"s around. Also, change the sleep times (I like 180... it means that it should disconnect in 180-360 seconds and it checks every 180 seconds for a connection)

raily 2010-02-07 22:07

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

Nokia-N900-42-11:/usr/local/bin# ./autodisconnectd
wlan0
125059300
125060300
125059300
125059301
125060300
125059300
125059301
Iface
125059300
Iface
125059300
Iface
125059300
Iface
125059300
Iface
125059300
Iface
125059300
Iface
125059300
Iface
125059300
^CNokia-N900-42-11:/usr/local/bin#
it disconnected correctly. does it idle again or does it send the disconnect command continously?

I have to admit I dont fully understand your script. so do I change every sleep 10 to sleep 180 for normal use? what about the sleep 5? change to 60? then erase all echos-lines to surpress output? anything else to delete?

azstunt 2010-02-07 22:14

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

Originally Posted by raily (Post 514960)
it disconnected correctly. does it idle again or does it send the disconnect command continously?

I have to admit I dont fully understand your script. so do I change every sleep 10 to sleep 180 for normal use? what about the sleep 5? change to 60? then erase all echos-lines to surpress output? anything else to delete?

I forgot about the "sleep 5" one. Leave sleep 5 alone and change the others two to 180. Like this:

Code:

#!/bin/sh
while true; do
      sleep 180
      a=`tail -1 /proc/net/route | cut -f1`
      b=`grep wlan0 /proc/net/dev | cut -c 8-19`
      while [ "$a" == "wlan0" ]
      do
            c=`expr $b + 1000`
            sleep 180
            b=`grep wlan0 /proc/net/dev | cut -c 8-19`
            d=`expr $b + 1`
            a=`tail -1 /proc/net/route | cut -f1`
            if [ "$d" -lt "$c" -a "$a" == "wlan0" ]; then
                  dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
                  a=`tail -1 /proc/net/route | cut -f1`
                  sleep 5
            fi
      done
done

Sleep 5 is to give a few seconds to the system to disconnect because if you don't, the system goes back into the while cycle again (just once, though).

Now test if it works when being autostarted.

jmk 2010-02-07 22:31

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Could someone do 3G compatible version of this. Change wlan0>gprs0 etc. and edit cuts?

Maybe this script should run only when connected like this /etc/networks/if-pre-up.d/ or /etc/networks/if-up.d/
to start every time a network interface is upped.

And someone should make simple control panel applet to turn this script on/off. Like wifi-switcher check the source code.

moneytoo 2010-02-07 22:36

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

Originally Posted by Matan (Post 483281)
This script should disconnect from network (wlan or GPRS/UMTS) after 5 to 10 minutes of no received packets:

Code:

#!/bin/sh
a=`ifconfig phonet0 | grep RX.p | cut -d: -f2`
c=`ifconfig wlan0 | grep RX.p | cut -d: -f2`
b=$a
d=$c

while true ; do
        sleep 300
        a=`ifconfig phonet0 | grep RX.p | cut -d: -f2`
        c=`ifconfig wlan0 | grep RX.p | cut -d: -f2`
        if [ "$a" == "$b" -a "$c" == "$d" ] ; then
                dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
        fi
        b=$a
        d=$c
done


I replaced phonet0 with gprs0 and set it the timeout to 180 seconds and it works great!

azstunt 2010-02-07 23:35

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

Originally Posted by moneytoo (Post 515006)
I replaced phonet0 with gprs0 and set it the timeout to 180 seconds and it works great!

Are you sure that it works? Let's asume you are connected to internet via wi-fi and the connection is idle (since I don't use gprs I will ignore it here but my point should still stand... unless for gprs you actually don't receive any packets while being idle). If you input

Code:

ifconfig wlan0 | grep RX.p | cut -d: -f2
as a normal user into the terminal you get the following:

Code:

-sh: ifconfig: not found
If you run that script as a whole as a normal user you get something like (not exactly): error line 2: ifconfig: not found and error line 3: ifconfig not found and it would disconnect you after 5 mins even if you were using the internet.

Get connected again.

Now, gain root and type:

Code:

ifconfig wlan0 | grep RX.p | cut -d: -f2
You'll see that this time it does work.

If you run the script as root you don't get any errors but you also don't get disconnected. To find out why, open another terminal and run (as root).

Code:

ifconfig wlan0 | grep RX.p | cut -d: -f2
Wait about 60-120 seconds and do it again. The values are different, aren't they? Even though you aren't using the connection. Since these values are different. The script doesn't disconnect you.

azstunt 2010-02-08 00:30

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

Originally Posted by jmk (Post 515002)
Could someone do 3G compatible version of this. Change wlan0>gprs0 etc. and edit cuts?

Maybe this script should run only when connected like this /etc/networks/if-pre-up.d/ or /etc/networks/if-up.d/
to start every time a network interface is upped.

And someone should make simple control panel applet to turn this script on/off. Like wifi-switcher check the source code.

Do you want to test my script? I'm not entirely sure about it being 3G compatible but it should work. Run it from terminal first to see if it works (if you can, test it with 2g, 3g and wi-fi). If it works, change the first two sleep to 180 (or any number that you may like... I just like 180) and erase all the "echo"s. If it doesn't work, please paste here the outcomes of the script.

Code:

#!/bin/sh
while true; do
      sleep 180
      a=`tail -1 /proc/net/route | cut -f1`
      while [ "$a" != "Iface" ]
      do
            b=`ifconfig wlan0 | grep RX.p | cut -d"(" -f1 | cut -d: -f2`
            c=`expr $b + 1000`
            sleep 180
            b=`ifconfig wlan0 | grep RX.p | cut -d"(" -f1 | cut -d: -f2`
            a=`tail -1 /proc/net/route | cut -f1`
            if [ "$b" -lt "$c" -a "$a" != "Iface" ]; then
                  dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
                  sleep 10
                  a=`tail -1 /proc/net/route | cut -f1`
            fi
      done
done

I wasn't able to add any file to /etc/networks/if-pre-up.d/ or /etc/networks/if-up.d/ so I could'nt make it start like you suggested. However, I believe that it may be possible to change the contents in /etc/event.d/autodisconnect to start when a connection is established and stop when the connection is lost. Even so, I lack knowledge about that.

raily 2010-02-08 11:56

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

I tested your modified script and at first after bootup it disconnected me correctly. Then I tested if it leaves a used connection unchanged by listening to internet radio streams. The script did not disconnect, fine. Afterwards i closed all programs causing traffic and waited for a disconnect, but nothing happens.

Can you figure out whats wrong?

bbhl 2010-02-08 12:16

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
I would rather attach it to the cron: http://talk.maemo.org/showthread.php?p=467732 with 3 minutes period instead of global while true...

azstunt 2010-02-08 13:07

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

Originally Posted by raily (Post 516047)
@azstund,

I tested your modified script and at first after bootup it disconnected me correctly. Then I tested if it leaves a used connection unchanged by listening to internet radio streams. The script did not disconnect, fine. Afterwards i closed all programs causing traffic and waited for a disconnect, but nothing happens.

Can you figure out whats wrong?

Were you using 3g, 2g or wi-fi? Would you mind reproducing it in the terminal (with all the echos that I have above) and pasting here the outcomes, please?

EDIT: Another thing, what time did you set for sleep and how much time did you wait for a disconnection after closing all programs?

azstunt 2010-02-09 06:17

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

Originally Posted by raily (Post 516047)
@azstund,

I tested your modified script and at first after bootup it disconnected me correctly. Then I tested if it leaves a used connection unchanged by listening to internet radio streams. The script did not disconnect, fine. Afterwards i closed all programs causing traffic and waited for a disconnect, but nothing happens.

Can you figure out whats wrong?

Disregard my last reply. I have made more changes to the script up here (post # 93). Please check them out and use that script.

raily 2010-02-09 09:20

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
ok, good I was to lazy yesterday to set up the test :)
But is you Script in post #93 affecting packet connection over 3G, GPRS? I just want to disconnect WLAN, hence I don't think I can idle in ICQ when on 3G anymore if I use Script #93.

damion 2010-02-09 09:48

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
I wrote a script for /etc/network/if-up.d/ a number of weeks back to check whether the slide was shut, whether ssh was running (rarely want disconnect in this case) and if so, disconnect. It exited if no route was up. Sorting idled bytes xfered was the missing piece, thanks for this thread! I can post my script if ppl are interested.

mclarson 2010-02-09 11:41

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

Originally Posted by damion (Post 517446)
I wrote a script for /etc/network/if-up.d/ a number of weeks back to check whether the slide was shut, whether ssh was running (rarely want disconnect in this case) and if so, disconnect. It exited if no route was up. Sorting idled bytes xfered was the missing piece, thanks for this thread! I can post my script if ppl are interested.

This sounds really useful, I for one would like to see it.

Also, when I connect to a network the email app automatically checks for new emails. If the script could also automatically bring an interface up intermittently (then normal logic to bring it down) would email be checked? This would be perfect - long battery life plus email notifications.

I would also like to be able to bring down openvpn when on my home wifi connection. It looks like this thread might make this possible too...

azstunt 2010-02-09 14:22

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

Originally Posted by raily (Post 517419)
ok, good I was to lazy yesterday to set up the test :)
But is you Script in post #93 affecting packet connection over 3G, GPRS? I just want to disconnect WLAN, hence I don't think I can idle in ICQ when on 3G anymore if I use Script #93.

It is made to disconnect from any idle connection (3G, GPRS and wi-fi). I'm not sure how many bits do you receive per minute when idle on ICQ via 3G. If you receive less than 1000 bytes each 3 mins it will disconnect you. You can always play with that number. Try setting it to 500 instead of 1000 (line 8 in my script) or lower. If that's not what you are looking for you can always just change "lface" to "wlan0" in lines 6 and 12.

kefren 2010-02-09 17:08

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Just a small battery regarding notice: With a similar script I end the day with about 80% battery. Mails are updated automatically every two hours, I use all kind of widgets like omweather. It's true that I don't speak that much at the telephone.
Before coding that script my battery was almost drained at the end of the day. I didn't expect wlan controller to be _that_ power unfriendly.

woodyear99 2010-02-09 17:09

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Interesting, no wonder so many people are unsatisfied with the battery. It seems that the issue isn't the need for a larger battery but better power management...

Laughingstok 2010-02-09 18:48

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Can we get a "latest and greatest" working version of that script slapped into a fresh thread or somewhere easily accessible in case modifcations/updates are made to it? I am interested in trying this out but would like to know which version is the one people are using with the most success at the moment.

kefren 2010-02-09 21:53

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
well, this is what I'm using - based on a similar idea from his thread. Improvments are welcomed, I'm looking especially for "screen on" info in order to let he connection alive when screen is lit.

#!/bin/sh

while true; do
a=`tail -1 /proc/net/route | cut -f1`
b=`grep wlan0 /proc/net/dev | cut -c 9-18`

while [ "$a" = "wlan0" ]
do
c=`expr $b + 6000`
sleep 180
b=`grep wlan0 /proc/net/dev | cut -c 9-18`
d=`expr $b + 1`
a=`tail -1 /proc/net/route | cut -f1`
if [ "$d" -lt "$c" -a "$a" = "wlan0" ]; then
echo Disconnecting
dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
fi
done
sleep 180
done

qwerty12 2010-02-09 22:01

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

Originally Posted by kefren (Post 518400)
well, this is what I'm using - based on a similar idea from his thread. Improvments are welcomed, I'm looking especially for "screen on" info in order to let he connection alive when screen is lit.

That's broadcast over D-Bus as a signal: signal sender=:1.7 -> dest=(null destination) serial=3499 path=/com/nokia/mce/signal; interface=com.nokia.mce.signal; member=display_status_ind
string "on"

"on" can also be "dimmed" and "off". I guess you can use dbus-scripts or something similar to have something happen upon the display state changing.

azstunt 2010-02-09 22:50

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

Originally Posted by Laughingstok (Post 518108)
Can we get a "latest and greatest" working version of that script slapped into a fresh thread or somewhere easily accessible in case modifcations/updates are made to it? I am interested in trying this out but would like to know which version is the one people are using with the most success at the moment.

That's why I've been just updating the one at post #93. But anyway... Let this be the one that I'll be keeping updated.

How to disconnect automatically from idle networks (3g/2g/wi-fi)

UPDATED 09/02/10
SECOND UPDATE 09/02/10 - FIXED A FEW TYPOS
THIRD UPDATE 09/02/10 - MADE THE INSTRUCTIONS MORE USER FRIENDLY
UPDATED 10/02/10 - MADE IT START ONLY WHEN A CONNECTION IS ACTIVE
SECOND UPDATE 10/02/10 - FIXED A TYPO... HAD FORGOTTEN A LINE
THIRD UPDATE 10/02/10 - NO CHANGE TO THE SCRIPT OR MAIN INSTRUCTIONS, JUST TO THE EXTRA FUNCTIONALLITY INSTRUCTIONS


Before you start, you'll need to install vim and rootfsh, both found in the extras repository.

1) In xterminal input
Code:

sudo gainroot
2) In xterminal input
Code:

vim /usr/local/bin/autodisconnect
3) Contents of /usr/local/bin/autodisconnect:
Code:

#!/bin/sh
sleep 180
a=`tail -1 /proc/net/route | cut -f1`
b=`ifconfig $a | grep RX.b | cut -d"(" -f1 | cut -d: -f2`
while [ "$a" != "Iface" ]
do
      c=`expr $b + 1000`
      sleep 180
      b=`ifconfig $a | grep RX.b | cut -d"(" -f1 | cut -d: -f2`
      a=`tail -1 /proc/net/route | cut -f1`
      if [ "$b" -lt "$c" -a "$a" != "Iface" ]; then
              dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
              sleep 10
              a=`tail -1 /proc/net/route | cut -f1`
      fi
done

exit 0

4) In xterminal input
Code:

chmod a+x /usr/local/bin/autodisconnect
5) In xterminal input
Code:

vim /etc/network/if-up.d/00_autodisconnect
6) Contents of /etc/network/if-up.d/00_autodisconnect
Code:

!#/bin/sh
#Run a script to disconnect idle networks

console none

script
              /usr/local/bin/autodisconnect
end script

exit 0

7) In xterminal input
Code:

chmod a+x /etc/network/if-up.d/00_autodisconnect
8) Reboot device

9) Enjoy!

Extra instructions for those looking specific funcionallity:

a) If you want to disconnect only from wi-fi change line 5
Code:

while [ "$a" != "Iface" ]
to
Code:

while [ "$a" == "wlan0" ]
b) You can also play with two values:

i) The constant (1000 in this case) in line 7 is about how many bytes must be receiven in a given period of time (the sleep time in line 9) to consider the connection in use. Anything less than that is considered idle. I also recomend a low number because I still don't know exactly how does it get along with idling in IM (but you still want to stay "Avalaible"). If you get randomly disconnected when idling in IM lower the value.

ii) The sleep time in line 8 determines when you'll disconnect automatically. It will almost never be the exact time indicated. If you stop using your connection it should disconnect in a range of x to 2x seconds of the time set (x being the sleep time).

Any questions feel free to post here. Hope it works for you.

raily 2010-02-10 10:48

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Is it normal that two instances of the Script are running? I don't know how things in event.d work, so I am just curious.
Code:

Nokia-N900-42-11:~# ps |grep auto
 1288 root      2084 S    /bin/sh -e -c /usr/local/bin/autodisconnect  /bin/sh
 1289 root      2084 S    /bin/sh /usr/local/bin/autodisconnect

I did the Internetradio test again it it works great! Couldn't test ICQ idling though because it doesn't connect me since yesterday on my N900. But thats a different story. I'll update you when my ICQ is working again.

azstunt 2010-02-10 14:38

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

Originally Posted by raily (Post 519156)
Is it normal that two instances of the Script are running? I don't know how things in event.d work, so I am just curious.
Code:

Nokia-N900-42-11:~# ps |grep auto
 1288 root      2084 S    /bin/sh -e -c /usr/local/bin/autodisconnect  /bin/sh
 1289 root      2084 S    /bin/sh /usr/local/bin/autodisconnect

I did the Internetradio test again it it works great! Couldn't test ICQ idling though because it doesn't connect me since yesterday on my N900. But thats a different story. I'll update you when my ICQ is working again.

I also don't know how things in event.d work, in every instance I used PS I found the same as you did, but I believe it to be normal. And I think that it should work with most (if not all IM), if it doesn't, try reading above and lower the value in line 8.

calvin_42 2010-02-10 16:13

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Hi there. I've been running the following script for now a week and it works pretty good on my device both with 3G and Wifi. Since it's the script I want to use in an application I am currently developping, if I could have some feedbacks, it would be great.

Thx!

Code:

#!/bin/sh
logentry()
{
        #SEND A STRING TO THIS FUNCTION TO APPEND IT TO THE LOG FILE
        echo -e "$1" >> ~/autoDCLog.txt
        echo "$1"
}

osnotify()
{
        #SEND A STRING TO THIS FUNCTION TO GENERATE A SYSTEM POPUP
        dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$1"
}

disconnect()
{
        #DISCONNECT CURRENT CONNECTION
        dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
}

#PREPARE INITIAL VARIABLES FOR TEMPORAL MONITORING OF NETWORK INTERFACES
interface=`tail -1 /proc/net/route | cut -f1`
packets_before=`grep $interface /proc/net/dev | awk '{ print $10 }'`

#LOG MONITOR STARTUP TO FILE & NOTIFY USER VIA POPUP
logentry "\nIdle Monitor Startup $(date)\nEVENTS:\n"

#PERFORM MAIN MONITORING LOOPS
while true;
do
        #CHANGE THE NUMBER AFTER SLEEP TO SET THE NUMBER OF SECONDS BEFORE
        #THE INTERNET IS DISCONNECTED IF NO INTERNET ACTIVITY IS DETECTED
        sleep 300

        interface=`tail -1 /proc/net/route | cut -f1`

        logentry "Current connexion : $interface"

        if [ "$interface" != "Iface" ]; then

                packets_after=`grep $interface /proc/net/dev | awk '{ print $10 }'`
                logentry "Packets : $packets_before -> $packets_after"

                if [ "$packets_before" = "$packets_after" ]; then
                        disconnect
                        osnotify "Connection closed due to inactivity"
                        logentry "[Connection closed @ $(date)]"
                        packets_before=0
                else
                        packets_before=$packets_after             
                fi
        fi

done

exit


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 11:35.

vBulletin® Version 3.8.8