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



All times are GMT. The time now is 17:12.

vBulletin® Version 3.8.8