DbusScripts

m (turn to 2g when connected to wifi network)
m (dbus-scripts: couple of comments about what you can watch for)
Line 9: Line 9:
* 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)
 +
* bnep or usb network started or stopped (use to configure or run firewalls etc)
You can find dbus-scripts in [http://wiki.maemo.org/Extras-devel extras-devel]
You can find dbus-scripts in [http://wiki.maemo.org/Extras-devel extras-devel]

Revision as of 22:28, 23 March 2010

Contents

dbus-scripts

dbus-scripts is a daemon that can execute a command when various action occurs on DBus. Some dbus signal 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, you're 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.

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

Limitations

For the moment dbus-scripts will only pass simple dbus 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 ad 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/unreregister SIP and turn on/off the ring.

I use this dbus-scripts config file :

 cat /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.

 cat /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 dbus method :

 cat /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 :


 cat /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'] + "'")


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