DbusScripts

m (links)
(formatting changes)
Line 1: Line 1:
-
= dbus-scripts =
+
[http://www.cobb.uk.net/770/#dbus-scripts dbus-scripts] is a daemon that can execute a command when various actions occurs on D-Bus. Some D-Bus signals you can watch for are :
-
 
+
-
[http://www.cobb.uk.net/770/#dbus-scripts dbus-scripts] is a daemon
+
-
that can execute a command when various actions occurs on DBus. Some
+
-
dbus signals you can watch for are :
+
* keyboard slide
* keyboard slide
Line 9: Line 5:
* connection or disconnection from a network
* connection or disconnection from a network
* sms or phone call arriving
* sms or phone call arriving
-
* desktop started (ie after boot is completed)
+
* desktop started (ie. after boot is completed)
* bnep or usb network started or stopped (use to configure or run firewalls etc)
* bnep or usb network started or stopped (use to configure or run firewalls etc)
You can find dbus-scripts in [[extras-devel]] (so be careful, your [[Nokia N900|N900]] could crash).
You can find dbus-scripts in [[extras-devel]] (so be careful, your [[Nokia N900|N900]] could crash).
-
There's some documentation in the
+
There's some documentation in the [http://www.cobb.uk.net/770/dbus-scripts-example dbus-scripts config file]
-
[http://www.cobb.uk.net/770/dbus-scripts-example dbus-scripts config file]
+
-
There's also a configurator program
+
There's also a configurator program [http://maemo.org/packages/view/dbus-scripts-settings/ dbus-scripts-settings] written by Matan Ziv-Av You can use dbus-scripts-settings settings to configure dbus-scripts or just to browse.
-
[http://maemo.org/packages/view/dbus-scripts-settings/ dbus-scripts-settings]
+
-
written by Matan Ziv-Av You can use dbus-scripts-settings settings to configure dbus-scripts or just to browse
+
Thanks to Graham and Matan.
Thanks to Graham and Matan.
Line 25: Line 18:
== Configuration ==
== Configuration ==
-
configuration files for dbus-scripts are in /etc/dbus-scripts.d and
+
Configuration files for dbus-scripts are in <code>/etc/dbus-scripts.d</code> and you can find some documentation in <code>/etc/dbus-scripts.d/dbus-scripts-example</code>:
-
you can find some documentation in
+
-
/etc/dbus-scripts.d/dbus-scripts-example :
+
   # Example of dbus-scripts control file
   # Example of dbus-scripts control file
Line 53: Line 44:
   /some/script * * com.nokia.icd status_changed * WLAN_INFRA
   /some/script * * com.nokia.icd status_changed * WLAN_INFRA
-
* this one will match all events for wlan network id of
+
* this one will match all events for wlan network id of 91f493fb-7c89-4fc6-ac2c-b822923dde45, it's mine
-
91f493fb-7c89-4fc6-ac2c-b822923dde45, it's mine
+
-
(you can find the wlan id with the command gconftool-2 -R
+
You can find the wlan id with the command
-
/system/osso/connectivity/IAP)
+
gconftool-2 -R /system/osso/connectivity/IAP
   /some/script * * com.nokia.icd status_changed 91f493fb-7c89-4fc6-ac2c-b822923dde45 WLAN_INFRA
   /some/script * * com.nokia.icd status_changed 91f493fb-7c89-4fc6-ac2c-b822923dde45 WLAN_INFRA
Line 69: Line 59:
dbus-scripts does not reread edited configuration automatically. After editing (or creating/removing) a configuration file, you need to restart it.
dbus-scripts does not reread edited configuration automatically. After editing (or creating/removing) a configuration file, you need to restart it.
-
On N900 it can easily be done with killall dbus-scripts, since it is then restarted by upstart. On 770/N800/N810, use /etc/init.d/dbus-scripts restart.
+
On N900 it can easily be done with
 +
killall dbus-scripts
 +
since it is then restarted by upstart. On 770/N800/N810, use
 +
/etc/init.d/dbus-scripts restart
-
The package dbus-scripts-settings contains a script logit.sh which simply logs its parameters to the file /media/mmc1/logit.log. If you want to find out if some event generates dbus events, and what configuration line to use for it, create a file in /etc/dbus-scripts.d/ containing:
+
The package dbus-scripts-settings contains a script <code>logit.sh</code> which simply logs its parameters to the file <code>/media/mmc1/logit.log</code>. If you want to find out if some event generates D-Bus events, and what configuration line to use for it, create a file in <code>/etc/dbus-scripts.d/</code> containing:
  /usr/bin/logit.sh *
  /usr/bin/logit.sh *
-
And generate the event. Reading the log file will show you all the dbus activity.  
+
And generate the event. Reading the log file will show you all the D-Bus activity.  
== Limitations ==
== Limitations ==
-
For the moment dbus-scripts will only pass simple dbus type arguments
+
For the moment dbus-scripts will only pass simple D-Bus type arguments like string, boolean and int32 et uint32 to your scripts. You will miss variant, array and all other complex type arguments. So if you want to play, for example, with text of received SMS, you can't yet use dbus-scripts as the text is an array of bytes.
-
like string, boolean and int32 et uint32 to your scripts. You will
+
-
miss variant, array and all other complex type arguments. So if you
+
-
want to play, for example, with text of received sms, you can't yet
+
-
use dbus-scripts as the text is an array of bytes.
+
-
= Some scripts =
+
== Some scripts ==
-
Here are some things you can do, if you have others, add them. Those
+
Here are some things you can do, if you have others, add them. Those examples aren't rocket science and I've stolen some ideas from here and there on web.
-
examples aren't rocket science and I've stolen some ideas from here
+
-
and there on web.
+
-
== when connect on my wifi home network register only SIP and turn on noisy ring ==
+
=== when connect on my wifi home network register only SIP and turn on noisy ring ===
-
When I come back home I want my SIP call redirected to my mobile phone and the phone ring turn on and when I leave I want my ring turn off so I wait for dbus event on my home wifi network and register/unregister SIP and turn on/off the ring.
+
When I come back home I want my SIP call redirected to my mobile phone and the phone ring turn on and when I leave I want my ring turn off so I wait for D-Bus event on my home wifi network and register/unregister SIP and turn on/off the ring.
I use this dbus-scripts config file :
I use this dbus-scripts config file :
-
  cat /etc/dbus-scripts.d/athome  
+
'''/etc/dbus-scripts.d/athome'''
   /home/user/bin/athome * * com.nokia.icd status_changed * WLAN_INFRA
   /home/user/bin/athome * * com.nokia.icd status_changed * WLAN_INFRA
and this script to register/unregister SIP and turn on/off the ring.
and this script to register/unregister SIP and turn on/off the ring.
-
  cat /home/user/bin/athome
+
'''/home/user/bin/athome'''
-
  #!/usr/bin/python
+
<source lang="python">
-
 
+
#!/usr/bin/python
-
  import dbus
+
 
-
  import sys
+
import dbus
-
  import re
+
import sys
-
  import telepathy
+
import re
-
 
+
import telepathy
-
  wifi = '91f493fb-7c89-4fc6-ac2c-b822923dde45'
+
 
-
 
+
wifi = '91f493fb-7c89-4fc6-ac2c-b822923dde45'
-
  # /home/user/bin/w32g * * com.nokia.icd status_changed * WLAN_INFRA *
+
 
-
 
+
# /home/user/bin/w32g * * com.nokia.icd status_changed * WLAN_INFRA *
-
  wifi_id,state = sys.argv[5],sys.argv[7]
+
 
-
 
+
wifi_id,state = sys.argv[5],sys.argv[7]
-
  # if we are not on the good wifi network we do nothing and exit
+
 
-
  if wifi and not re.search(wifi_id, wifi, re.IGNORECASE):
+
# if we are not on the good wifi network we do nothing and exit
-
    sys.exit(0)
+
if wifi and not re.search(wifi_id, wifi, re.IGNORECASE):
-
 
+
  sys.exit(0)
-
  bus = dbus.SessionBus()
+
 
-
  account = bus.get_object('org.freedesktop.Telepathy.AccountManager',
+
bus = dbus.SessionBus()
-
                          '/org/freedesktop/Telepathy/Account/sofiasip/sip/_309517129090')
+
account = bus.get_object('org.freedesktop.Telepathy.AccountManager',
-
  profile = bus.get_object('com.nokia.profiled', '/com/nokia/profiled')
+
                        '/org/freedesktop/Telepathy/Account/sofiasip/sip/_309517129090')
-
 
+
profile = bus.get_object('com.nokia.profiled', '/com/nokia/profiled')
-
  def change_state(prof, presence_const, presence_text):
+
 
-
    profile.set_profile(prof, dbus_interface='com.nokia.profiled')
+
def change_state(prof, presence_const, presence_text):
-
    account.Set('org.freedesktop.Telepathy.Account', 'RequestedPresence', \
+
  profile.set_profile(prof, dbus_interface='com.nokia.profiled')
-
                dbus.Struct(( dbus.UInt32( presence_const) ,  presence_text, ""), signature='uss'),
+
  account.Set('org.freedesktop.Telepathy.Account', 'RequestedPresence', \
-
                dbus_interface='org.freedesktop.DBus.Properties')
+
              dbus.Struct(( dbus.UInt32( presence_const) ,  presence_text, ""), signature='uss'),
-
   
+
              dbus_interface='org.freedesktop.DBus.Properties')
-
 
+
 
-
  if state == "CONNECTED":
+
 
-
    print "available"
+
if state == "CONNECTED":
-
    change_state('general', telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE, 'available')
+
  print "available"
-
  elif state == "IDLE":
+
  change_state('general', telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE, 'available')
-
    print "offline"
+
elif state == "IDLE":
-
    change_state('silent', telepathy.constants.CONNECTION_PRESENCE_TYPE_OFFLINE, 'offline')
+
  print "offline"
-
  else:
+
  change_state('silent', telepathy.constants.CONNECTION_PRESENCE_TYPE_OFFLINE, 'offline')
-
    sys.exit("Usage: %s (start|stop)" %  sys.argv[0])
+
else:
 +
  sys.exit("Usage: %s (start|stop)" %  sys.argv[0])
 +
</source>
== turn to 2g when connected to wifi network ==
== turn to 2g when connected to wifi network ==
-
There's already an [[Fcron#Auto_2G_with_WiFi|alternative method with fcron]]. Here is a dbus method :
+
There's already an [[Fcron#Auto_2G_with_WiFi|alternative method with fcron]]. Here is a D-Bus method :
-
  cat /etc/dbus-scripts.d/w32g
+
'''/etc/dbus-scripts.d/w32g'''
-
  /home/user/bin/w32g.py * * com.nokia.icd status_changed * WLAN_INFRA
+
/home/user/bin/w32g.py * * com.nokia.icd status_changed * WLAN_INFRA
-
Installation documentation is at the beginning of the script and
+
Installation documentation is at the beginning of the script and configuration documentation is at the beginning of the configuration file:
-
configuration documentation is at the beginning of the configuration
+
-
file :
+
 +
'''/home/user/bin/w32g.py'''
 +
<source lang="python">
 +
#!/usr/bin/python
-
  cat /home/user/bin/w32g.py
+
import sys
-
  #!/usr/bin/python
+
import os
-
 
+
import ConfigParser
-
  import sys
+
import re
-
  import os
+
 
-
  import ConfigParser
+
# You need to install dbus-scripts (BE CAREFUL, it's a devel package)
-
  import re
+
#  
-
 
+
# as root you need to create a file /etc/sudoers.d/w32g
-
  # You need to install dbus-scripts (BE CAREFUL, it's a devel package)
+
# with this line :
-
  #  
+
#  user ALL = NOPASSWD: /bin/ping
-
  # as root you need to create a file /etc/sudoers.d/w32g
+
# and run the command update-sudoers because only root can use ping
-
  # with this line :
+
#  
-
  #  user ALL = NOPASSWD: /bin/ping
+
# always as root and you need to create another file
-
  # and run the command update-sudoers because only root can use ping
+
# /etc/dbus-scripts/w32g with the following line :  
-
  #  
+
#
-
  # always as root and you need to create another file
+
#  /home/user/bin/w32g * * com.nokia.icd status_changed * WLAN_INFRA *
-
  # /etc/dbus-scripts/w32g with the following line :  
+
#
-
  #
+
# and copy this script in /home/user/bin/w32g
-
  #  /home/user/bin/w32g * * com.nokia.icd status_changed * WLAN_INFRA *
+
 
-
  #
+
conf_file = '/home/user/.w32g.conf'
-
  # and copy this script in /home/user/bin/w32g
+
 
-
 
+
config = { 'message_on_idle': '3G cellular mode set',
-
  conf_file = '/home/user/.w32g.conf'
+
  'message_on_connected': '2G (GSM) cellular mode set',
-
 
+
  'connection_test': 'sudo /bin/ping -c 1 www.google.com',
-
  config = { 'message_on_idle': '3G cellular mode set',
+
  'change_on_idle': 'true',
-
    'message_on_connected': '2G (GSM) cellular mode set',
+
  'change_to_dual': 'true',
-
    'connection_test': 'sudo /bin/ping -c 1 www.google.com',
+
  'wifi': ''
-
    'change_on_idle': 'true',
+
}
-
    'change_to_dual': 'true',
+
 
-
    'wifi': ''
+
config_parser = ConfigParser.SafeConfigParser()
-
  }
+
 
-
 
+
try:
-
  config_parser = ConfigParser.SafeConfigParser()
+
    config_parser.read(conf_file)
-
 
+
    for item in config_parser.items('w32g'):
-
  try:
+
        config[ item[0] ] = item[1]
-
      config_parser.read(conf_file)
+
except ConfigParser.NoSectionError:
-
      for item in config_parser.items('w32g'):
+
    pass
-
          config[ item[0] ] = item[1]
+
 
-
  except ConfigParser.NoSectionError:
+
def to_bool(string):
-
      pass
+
    if re.search('true', string, re.IGNORECASE):
-
 
+
        return True
-
  def to_bool(string):
+
 
-
      if re.search('true', string, re.IGNORECASE):
+
config['change_on_idle'] = to_bool(config['change_on_idle'])
-
          return True
+
config['change_to_dual'] = to_bool(config['change_to_dual'])
-
 
+
 
-
  config['change_on_idle'] = to_bool(config['change_on_idle'])
+
wifi_id,state = sys.argv[5],sys.argv[7]
-
  config['change_to_dual'] = to_bool(config['change_to_dual'])
+
 
-
 
+
# if we are not on the good wifi network we do nothing and exit
-
  wifi_id,state = sys.argv[5],sys.argv[7]
+
if config['wifi'] and not re.search(wifi_id, config['wifi'], re.IGNORECASE):
-
 
+
    sys.exit(0)
-
  # if we are not on the good wifi network we do nothing and exit
+
 
-
  if config['wifi'] and not re.search(wifi_id, config['wifi'], re.IGNORECASE):
+
dbus_wifi = "dbus-send --system --type=method_call --print-reply --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology";
-
      sys.exit(0)
+
 
-
 
+
dbus_notif = "dbus-send --system --type=method_call --print-reply --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint";
-
  dbus_wifi = "dbus-send --system --type=method_call --print-reply --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology";
+
 
-
 
+
if state == 'CONNECTED':
-
  dbus_notif = "dbus-send --system --type=method_call --print-reply --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint";
+
    # we verified that the connection works
-
 
+
    if config['connection_test']:
-
  if state == 'CONNECTED':
+
        ret = os.system(config['connection_test']);
-
      # we verified that the connection works
+
    else:
-
      if config['connection_test']:
+
        ret = 0
-
          ret = os.system(config['connection_test']);
+
    if ret == 0:
-
      else:
+
        os.system(dbus_wifi + " byte:1");
-
          ret = 0
+
        os.system(dbus_notif + " string:'" + config['message_on_connected'] + "'")
-
      if ret == 0:
+
 
-
          os.system(dbus_wifi + " byte:1");
+
elif state == 'IDLE' and config['change_on_idle']:
-
          os.system(dbus_notif + " string:'" + config['message_on_connected'] + "'")
+
    dual = "2"
-
 
+
    if config['change_to_dual']: dual = "0"
-
  elif state == 'IDLE' and config['change_on_idle']:
+
    print dual
-
      dual = "2"
+
    os.system(dbus_wifi + " byte:" + dual);
-
      if config['change_to_dual']: dual = "0"
+
    os.system(dbus_notif + " string:'" + config['message_on_idle'] + "'")
-
      print dual
+
</source>
-
      os.system(dbus_wifi + " byte:" + dual);
+
 
-
      os.system(dbus_notif + " string:'" + config['message_on_idle'] + "'")
+
'''/home/user/.w32g.conf'''
 +
<pre>
 +
[w32g]
 +
 
 +
### the comment reflect the default configuration
 +
### true is true (case insensitive), everything else is false
 +
 
 +
### the command used to test the wifi connection before downgrading to 2g
 +
### if you don't want to do test just delete after =
 +
# connection_test = sudo /bin/ping -c 1 www.google.com
 +
 
 +
### notification message when going to 3g
 +
# message_on_idle = 3G cellular mode set
 +
### notification message when going to 2g
 +
# message_on_connected = 2G (GSM) cellular mode set
 +
### shall we go back to 3g when wlan disconnect ?
 +
#### if you want to have false just delete after =
 +
# change_on_idle = true
 +
### shall we go back to dual or 3g ?
 +
# change_to_dual = true
-
  cat /home/user/.w32g.conf
+
### if you want go to 2g only when connected to some wlan you can add their wlan id here
-
  [w32g]
+
### you can find the wlan id using the command  
-
 
+
### gconftool-2 -R /system/osso/connectivity/IAP
-
  ### the comment reflect the default configuration
+
### For example :
-
  ### true is true (case insensitive), everything else is false
+
### wifi = 91f493fb-7c89-4fc6-ac2c-b822923dde45 9ee5dd55-9a32-4ee9-9131-c464ad31d907
-
 
+
# wifi =
-
  ### the command used to test the wifi connection before downgrading to 2g
+
</pre>
-
  ### if you don't want to do test just delete after =
+
-
  # connection_test = sudo /bin/ping -c 1 www.google.com
+
-
 
+
-
  ### notification message when going to 3g
+
-
  # message_on_idle = 3G cellular mode set
+
-
  ### notification message when going to 2g
+
-
  # message_on_connected = 2G (GSM) cellular mode set
+
-
 
+
-
  ### shall we go back to 3g when wlan disconnect ?
+
-
  #### if you want to have false just delete after =
+
-
  # change_on_idle = true
+
-
  ### shall we go back to dual or 3g ?
+
-
  # change_to_dual = true
+
-
 
+
-
  ### if you want go to 2g only when connected to some wlan you can add their wlan id here
+
-
  ### you can find the wlan id using the command  
+
-
  ### gconftool-2 -R /system/osso/connectivity/IAP
+
-
  ### For example :
+
-
  ### wifi = 91f493fb-7c89-4fc6-ac2c-b822923dde45 9ee5dd55-9a32-4ee9-9131-c464ad31d907
+
-
  # wifi =
+
-
= Links to other scripts =
+
== Links to other scripts ==
-
== automatically register on public wifi network when connected ==
+
=== automatically register on public wifi network when connected ===
You can find a script [http://talk.maemo.org/showpost.php?p=555757&postcount=6 in this post on tmo]
You can find a script [http://talk.maemo.org/showpost.php?p=555757&postcount=6 in this post on tmo]
-
= Thanks =
+
== Thanks ==
Thanks to Graham Cobb for dbus-scripts and Matan for dbus-scripts-settings
Thanks to Graham Cobb for dbus-scripts and Matan for dbus-scripts-settings

Revision as of 10:53, 23 June 2010

dbus-scripts is a daemon that can execute a command when various actions occurs on D-Bus. Some D-Bus signals you can watch for are :

  • keyboard slide
  • battery full, low or empty
  • connection or disconnection from a network
  • sms or phone call arriving
  • desktop started (ie. after boot is completed)
  • bnep or usb network started or stopped (use to configure or run firewalls etc)

You can find dbus-scripts in extras-devel (so be careful, your N900 could crash).

There's some documentation in the dbus-scripts config file

There's also a configurator program dbus-scripts-settings written by Matan Ziv-Av You can use dbus-scripts-settings settings to configure dbus-scripts or just to browse.

Thanks to Graham and Matan.

Contents

Configuration

Configuration files for dbus-scripts are in /etc/dbus-scripts.d and you can find some documentation in /etc/dbus-scripts.d/dbus-scripts-example:

 # Example of dbus-scripts control file
 #
 # Each line of these files represents a filter to call a script.
 # Tokens are separated by white space.
 # First token on line is the script to execute.
 # Subsequent tokens are filters to match arguments from dbus message.
 # Filters are matched like shell wildcards (using fnmatch(3)).
 # If a filter is specified (even if it is *), the corresponding argument
 # must be present in order to get a match.
 #
 # First argument is sender
 # Second argument is destination
 # For SIGNAL and METHOD_CALL, third argument is interface
 # For SIGNAL and METHOD_CALL, fourth argument is member
 # Other arguments depend on the message
 #
 # Example: to act on WLAN interface state changes use:
 #/some/script * * com.nokia.icd status_changed * WLAN_INFRA
 # In /some/script, $5 will be the interface name and $7 the new state (IDLE, CONNECTED, etc.)
  • this rule will match all wlan network events
 /some/script * * com.nokia.icd status_changed * WLAN_INFRA
  • this one will match all events for wlan network id of 91f493fb-7c89-4fc6-ac2c-b822923dde45, it's mine

You can find the wlan id with the command

gconftool-2 -R /system/osso/connectivity/IAP
 /some/script * * com.nokia.icd status_changed 91f493fb-7c89-4fc6-ac2c-b822923dde45 WLAN_INFRA
  • this one will match only the CONNECTED (wifi works) dbus event for my wlan network :
 /some/script * * com.nokia.icd status_changed 91f493fb-7c89-4fc6-ac2c-b822923dde45 WLAN_INFRA CONNECTED

Usage, tips and tricks

dbus-scripts does not reread edited configuration automatically. After editing (or creating/removing) a configuration file, you need to restart it.

On N900 it can easily be done with

killall dbus-scripts

since it is then restarted by upstart. On 770/N800/N810, use

/etc/init.d/dbus-scripts restart

The package dbus-scripts-settings contains a script logit.sh which simply logs its parameters to the file /media/mmc1/logit.log. If you want to find out if some event generates D-Bus events, and what configuration line to use for it, create a file in /etc/dbus-scripts.d/ containing:

/usr/bin/logit.sh *

And generate the event. Reading the log file will show you all the D-Bus activity.

Limitations

For the moment dbus-scripts will only pass simple D-Bus type arguments like string, boolean and int32 et uint32 to your scripts. You will miss variant, array and all other complex type arguments. So if you want to play, for example, with text of received SMS, you can't yet use dbus-scripts as the text is an array of bytes.

Some scripts

Here are some things you can do, if you have others, add them. Those examples aren't rocket science and I've stolen some ideas from here and there on web.

when connect on my wifi home network register only SIP and turn on noisy ring

When I come back home I want my SIP call redirected to my mobile phone and the phone ring turn on and when I leave I want my ring turn off so I wait for D-Bus event on my home wifi network and register/unregister SIP and turn on/off the ring.

I use this dbus-scripts config file :

/etc/dbus-scripts.d/athome

 /home/user/bin/athome * * com.nokia.icd status_changed * WLAN_INFRA

and this script to register/unregister SIP and turn on/off the ring.

/home/user/bin/athome

#!/usr/bin/python
 
import dbus
import sys
import re
import telepathy
 
wifi = '91f493fb-7c89-4fc6-ac2c-b822923dde45'
 
# /home/user/bin/w32g * * com.nokia.icd status_changed * WLAN_INFRA *
 
wifi_id,state = sys.argv[5],sys.argv[7]
 
# if we are not on the good wifi network we do nothing and exit
if wifi and not re.search(wifi_id, wifi, re.IGNORECASE):
   sys.exit(0)
 
bus = dbus.SessionBus()
account = bus.get_object('org.freedesktop.Telepathy.AccountManager',
                         '/org/freedesktop/Telepathy/Account/sofiasip/sip/_309517129090')
profile = bus.get_object('com.nokia.profiled', '/com/nokia/profiled')
 
def change_state(prof, presence_const, presence_text):
   profile.set_profile(prof, dbus_interface='com.nokia.profiled')
   account.Set('org.freedesktop.Telepathy.Account', 'RequestedPresence', \
               dbus.Struct(( dbus.UInt32( presence_const) ,  presence_text, ""), signature='uss'),
               dbus_interface='org.freedesktop.DBus.Properties')
 
 
if state == "CONNECTED":
   print "available"
   change_state('general', telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE, 'available')
elif state == "IDLE":
   print "offline"
   change_state('silent', telepathy.constants.CONNECTION_PRESENCE_TYPE_OFFLINE, 'offline')
else:
   sys.exit("Usage: %s (start|stop)" %  sys.argv[0])

turn to 2g when connected to wifi network

There's already an alternative method with fcron. Here is a D-Bus method :

/etc/dbus-scripts.d/w32g

/home/user/bin/w32g.py * * com.nokia.icd status_changed * WLAN_INFRA

Installation documentation is at the beginning of the script and configuration documentation is at the beginning of the configuration file:

/home/user/bin/w32g.py

#!/usr/bin/python
 
import sys
import os
import ConfigParser
import re
 
# You need to install dbus-scripts (BE CAREFUL, it's a devel package)
# 
# as root you need to create a file /etc/sudoers.d/w32g
# with this line :
#   user ALL = NOPASSWD: /bin/ping
# and run the command update-sudoers because only root can use ping
# 
# always as root and you need to create another file
# /etc/dbus-scripts/w32g with the following line : 
#
#   /home/user/bin/w32g * * com.nokia.icd status_changed * WLAN_INFRA *
#
# and copy this script in /home/user/bin/w32g
 
conf_file = '/home/user/.w32g.conf'
 
config = { 'message_on_idle': '3G cellular mode set',
  'message_on_connected': '2G (GSM) cellular mode set',
  'connection_test': 'sudo /bin/ping -c 1 www.google.com',
  'change_on_idle': 'true',
  'change_to_dual': 'true',
  'wifi': ''
}
 
config_parser = ConfigParser.SafeConfigParser()
 
try:
    config_parser.read(conf_file)
    for item in config_parser.items('w32g'):
        config[ item[0] ] = item[1]
except ConfigParser.NoSectionError:
    pass
 
def to_bool(string):
    if re.search('true', string, re.IGNORECASE):
        return True
 
config['change_on_idle'] = to_bool(config['change_on_idle'])
config['change_to_dual'] = to_bool(config['change_to_dual'])
 
wifi_id,state = sys.argv[5],sys.argv[7]
 
# if we are not on the good wifi network we do nothing and exit
if config['wifi'] and not re.search(wifi_id, config['wifi'], re.IGNORECASE):
    sys.exit(0)
 
dbus_wifi = "dbus-send --system --type=method_call --print-reply --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology";
 
dbus_notif = "dbus-send --system --type=method_call --print-reply --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint";
 
if state == 'CONNECTED':
    # we verified that the connection works
    if config['connection_test']:
        ret = os.system(config['connection_test']);
    else:
        ret = 0
    if ret == 0:
        os.system(dbus_wifi + " byte:1");
        os.system(dbus_notif + " string:'" + config['message_on_connected'] + "'")
 
elif state == 'IDLE' and config['change_on_idle']:
    dual = "2"
    if config['change_to_dual']: dual = "0"
    print dual
    os.system(dbus_wifi + " byte:" + dual);
    os.system(dbus_notif + " string:'" + config['message_on_idle'] + "'")

/home/user/.w32g.conf

[w32g]

### the comment reflect the default configuration
### true is true (case insensitive), everything else is false

### the command used to test the wifi connection before downgrading to 2g
### if you don't want to do test just delete after =
# connection_test = sudo /bin/ping -c 1 www.google.com

### notification message when going to 3g
# message_on_idle = 3G cellular mode set
### notification message when going to 2g
# message_on_connected = 2G (GSM) cellular mode set

### shall we go back to 3g when wlan disconnect ?
#### if you want to have false just delete after =
# change_on_idle = true
### shall we go back to dual or 3g ?
# change_to_dual = true

### if you want go to 2g only when connected to some wlan you can add their wlan id here
### you can find the wlan id using the command 
### gconftool-2 -R /system/osso/connectivity/IAP
### For example :
### wifi = 91f493fb-7c89-4fc6-ac2c-b822923dde45 9ee5dd55-9a32-4ee9-9131-c464ad31d907
# wifi =

Links to other scripts

automatically register on public wifi network when connected

You can find a script in this post on tmo

Thanks

Thanks to Graham Cobb for dbus-scripts and Matan for dbus-scripts-settings