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)

Matan 2010-01-22 17:25

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
See here: http://wiki.maemo.org/User:Jebba/DBU...d-notification

Replace $1 with your message.

But this is the type that does not go automatically.
Replace SystemNoteDialog with SystemNoteInfoprint and don't include the last uint32:0 string:'NAO OK!'

stopgap 2010-01-23 03:06

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
I've spent some time on this script and refined it a little:

*It now only does anything if there's an active connection.
*It logs startup and disconnections to a file in mydocs/tmp
*It notifies you onscreen when disconnecting
*It distinguishes between wifi/cellular connections

I still cannot get it to autostart though - I don't get what I'm doing wrong. I have an entry in /etc/event.d as suggested and have tried everything I can see - to no avail. Ideally it needs to start once the desktop is up and loaded properly... any ideas anyone?

Here's my script for anyone who wants it :)

Code:

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

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

#PREPARE INITIAL VARIABLES FOR TEMPORAL MONITORING OF NETWORK INTERFACES
getcell=`ifconfig phonet0 | grep RX.p | cut -d: -f2`
getwifi=`ifconfig wlan0 | grep RX.p | cut -d: -f2`
bakcell=$getcell
bakwifi=$getwifi

getcellup=`ifconfig | grep gprs0 | cut -d: -f0`
getwifiup=`ifconfig wlan0 | grep inet.addr | cut -d: -f2`

#LOG MONITOR STARTUP TO FILE & NOTIFY USER VIA POPUP
logentry "\nIdle Monitor Startup $(date)\nEVENTS:\n"
osnotify "Automatic internet disconnection monitor has started"

#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

        getcell=`ifconfig phonet0 | grep RX.p | cut -d: -f2`
        getwifi=`ifconfig wlan0 | grep RX.p | cut -d: -f2`
        getcellup=`ifconfig | grep gprs0 | cut -d: -f0`
        getwifiup=`ifconfig wlan0 | grep inet.addr | cut -d: -f1`

        if [ -n "$getcellup" -o -n "$getwifiup" ]; then
                #IF EITHER NETWORK INTERFACE HAS AN IP PROCEED WITH INACTIVITY TEST

                if [ "$getcell" == "$bakcell" -a "$getwifi" == "$bakwifi" ]; then
                        #IF NO PACKETS WERE RECEIVED SINCE LAST RECORDING THEN SHUTDOWN INTERFACES

                        dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
                       
                        if [ -n "$getcellup" ]; then
                                osnotify "The 2G/3G internet connection has been closed due to inactivity"
                                logentry " - 2G/3G DISCONNECTED @ $(date)"

                        elif [ -n "$getwifiup" ]; then
                                osnotify "The WI-FI internet connection has been closed due to inactivity"
                                logentry " - WI-FI DISCONNECTED @ $(date)"

                        else
                                osnotify "The internet connection has been closed due to inactivity"
                                logentry " - ????? DISCONNECTED @ $(date)"
                       
                        fi


                fi

                bakcell=$getcell
                bakwifi=$getwifi

        fi

done

exit


Matan 2010-01-23 09:08

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Add an echo into some file to the event.d file, so you can know if the commands in this file run at startup.

msnger 2010-01-24 04:35

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
appreciate your effort please let us know when your done :D

farmatito 2010-01-24 10:01

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

Originally Posted by stopgap (Post 489974)
I've spent some time on this script and refined it a little:

*It now only does anything if there's an active connection.
*It logs startup and disconnections to a file in mydocs/tmp
*It notifies you onscreen when disconnecting
*It distinguishes between wifi/cellular connections

I still cannot get it to autostart though - I don't get what I'm doing wrong. I have an entry in /etc/event.d as suggested and have tried everything I can see - to no avail. Ideally it needs to start once the desktop is up and loaded properly... any ideas anyone?

Here's my script for anyone who wants it :)

Code:

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

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

#PREPARE INITIAL VARIABLES FOR TEMPORAL MONITORING OF NETWORK INTERFACES
getcell=`ifconfig phonet0 | grep RX.p | cut -d: -f2`
getwifi=`ifconfig wlan0 | grep RX.p | cut -d: -f2`
bakcell=$getcell
bakwifi=$getwifi

getcellup=`ifconfig | grep gprs0 | cut -d: -f0`
getwifiup=`ifconfig wlan0 | grep inet.addr | cut -d: -f2`

#LOG MONITOR STARTUP TO FILE & NOTIFY USER VIA POPUP
logentry "\nIdle Monitor Startup $(date)\nEVENTS:\n"
osnotify "Automatic internet disconnection monitor has started"

#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

        getcell=`ifconfig phonet0 | grep RX.p | cut -d: -f2`
        getwifi=`ifconfig wlan0 | grep RX.p | cut -d: -f2`
        getcellup=`ifconfig | grep gprs0 | cut -d: -f0`
        getwifiup=`ifconfig wlan0 | grep inet.addr | cut -d: -f1`

        if [ -n "$getcellup" -o -n "$getwifiup" ]; then
                #IF EITHER NETWORK INTERFACE HAS AN IP PROCEED WITH INACTIVITY TEST

                if [ "$getcell" == "$bakcell" -a "$getwifi" == "$bakwifi" ]; then
                        #IF NO PACKETS WERE RECEIVED SINCE LAST RECORDING THEN SHUTDOWN INTERFACES

                        dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
                       
                        if [ -n "$getcellup" ]; then
                                osnotify "The 2G/3G internet connection has been closed due to inactivity"
                                logentry " - 2G/3G DISCONNECTED @ $(date)"

                        elif [ -n "$getwifiup" ]; then
                                osnotify "The WI-FI internet connection has been closed due to inactivity"
                                logentry " - WI-FI DISCONNECTED @ $(date)"

                        else
                                osnotify "The internet connection has been closed due to inactivity"
                                logentry " - ????? DISCONNECTED @ $(date)"
                       
                        fi


                fi

                bakcell=$getcell
                bakwifi=$getwifi

        fi

done

exit


Maybe you need to run the script as a service (daemon) through upstart
or add the script to /etc/networks/if-pre-up.d/ or /etc/networks/if-up.d/
to start every time a network interface is upped.

stopgap 2010-01-24 15:16

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Yeah, that'd be ideal to launch it when the network starts but I'm way out of my limited knowledge of linux here...

I took a look in if-up.d and saw there were some scripts in there. I tried to put my own in but it didn't seem to run. The others were named 00_something - maybe my test had a malformed naming for execution within that folder or perhaps I'm misunderstanding how that works... I can't find anything clear on this area on the web... What is there is linux specific - I'm not even sure if thats completely appropriate for all versions of linux, let alone maemo... I'm a Windows programmer so this is all rather unfamiliar to me to say the least!

Ideally I'd launch the monitor script with some handle, upon network up and then use the handle to terminate on network down (with the handle - if that's even how linux works?)

Any idea how, or does anyone know of any useful guides on this sort of stuff?

I sort of got it to auto-run on startup but something is not right and the script seems to freeze up or something, it stops working anyhow. I can see it running in HTOP still just doesn't seem to do anything.

Incidentally does anyone know how to use HTOP?... its functions use Fx keys and I'm not sure how you do that in a maemo console...

farnwomt 2010-01-28 10:25

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
You would probably do better to use transmitted rather than received packets because most networks will have some general chatter on them even if they are only occasional broadcast messages and those packet could keep your network connection up even if you aren't responding to them.

Perhaps the best solution is actually to check if both RX and TX have changed?

stopgap 2010-01-28 11:08

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

Originally Posted by farnwomt (Post 499114)
You would probably do better to use transmitted rather than received packets because most networks will have some general chatter on them even if they are only occasional broadcast messages and those packet could keep your network connection up even if you aren't responding to them.

Perhaps the best solution is actually to check if both RX and TX have changed?

Yeah, I am doing that now... just refining the script to work on connection up and down, as well as disconnecting when network is idle. A bit of a steep learning curve all this, but I'm quite enjoying it. I think I've worked out how to make it (optionally) disconnect if battery hits a certain level too.

I'll post it when I have a proper working script here for people to test :)

farnwomt 2010-01-28 14:49

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Just a couple of other points

1) phonet is interesting, but I am not entirely sure if it is the best device to check for activity. In my experience it handles packets even when there is no connection in place. It may be that it is better to look at something like gprs0 (or whatever device is created when you are connected) as that will definitely relate to genuine traffic.

2) Using ifconfig is ok, but it is probably slightly more demanding than it needs to be. You can get the same result from the file /proc/net/dev, using something like

c=`grep wlan0 /proc/net/dev | awk '{ print $10 }'`

One of the joys of this option is that if you want to look for something like gprs devices it doesn't matter if they don't exist as you won't get an error.

3) The original script attempted to disconnect even if there was no current connection. I would consequently suggest that you could find out how you are currently connected by doing this ...

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

This would set the variable connection to be 'wlan0' or 'gprs0' or whatever you are using, or 'Iface' if you aren't connected. You could use this information to only look at the live connection for new packets. I am assuming in writing this that only one interface will be active at a time, so if you use something like 'openvpn' you would have an issue. Having said that products like openvpn tend to send regular packets anyway so you wouldn't be able to close your connection very easily.

Hopefully this information will be of help?

h4w4ii4n 2010-01-28 18:58

Why N900 doesn't automatically disconnect when I'm done?
 
Every phone I used somewhat disconnected from the web/wi-fi when I close the browser, email, or whatever app that was using internet connection but the N900 doesn't. For example, if I used 3g to browse then close the browser it takes hours to disconnect from tmobile 3g by itself. If I used wi-fi to browse or check emails, it doesn't disconnect when I'm done with email app. 3G disconnects after a while I guess but wi-fi never disconnects and drains the battery. Anybody else notice this?


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

vBulletin® Version 3.8.8