DbusScripts

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.

As of 2010-07-16, dbus-scripts-settings is missing a dependency on package 'gnome-python-dev'. If the program crashes and at command line you get the error
Traceback (most recent call last):
  File "/usr/bin/dbus-scripts-settings", line 5, in <module>
    from gnome import gconf
ImportError: No module named gnome
then the fix is to install gnome-python-dev. magick777 14:09, 16 July 2010 (UTC)

Contents

Configuration

Basic 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

Options to dbus-scripts daemon

By default, the dbus-scripts daemon listens on the system message bus, which reports system events but not those from the user session. If you want to access events such as an application being launched, you'll need to configure the daemon to listen to everything.

Ignore /etc/init.d/dbus-scripts and /etc/default/dbus-scripts as these would seem to have been superseded by upstart. Instead, you'll need to edit /etc/event.d/dbus-scripts to launch dbus-scripts without the --system switch and, because it needs access to the user session, launch it via runstandalone.sh instead. So your /etc/event.d/dbus-scripts will look like this:

Nokia-N900:~# cat /etc/event.d/dbus-scripts 
description "dbus-scripts"
author "Graham Cobb <g+770@cobb.uk.net>"
 
start on started dbus
start on started dbus-actdead
stop on stopping dbus
stop on stopping dbus-actdead
 
console none
 
exec run-standalone.sh /usr/sbin/dbus-scripts
 
respawn

and you can then configure dbus-scripts with statements such as

#:Browser launched
/usr/bin/onbrowser * * com.nokia.HildonDesktop.AppMgr LaunchApplication browser

to react to, in this example, the browser being started.

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 =

Enable tethering when connecting to Bluetooth PAN

Tethering is a bit of a hassle in Maemo. Most people refer to JoikuSpot, which is definitely the most comfortable solution, but it's not free. So, why not use builtin (or easily installable) functionality to use the N900 as a modem?

For this script to work, you need to have at least bluetooth-dun and iptables installed. Possibly other packages too, please test and correct.

/etc/dbus-scripts.d/tethering

/usr/bin/tethering.sh * * org.freedesktop.Hal.Manager DeviceAdded
/usr/bin/tethering.sh * * org.freedesktop.Hal.Manager DeviceRemoved

/usr/bin/tethering.sh

#!/bin/sh
#
# /usr/bin/tethering.sh
#
# Enable tethering on USB network and Bluetooth PAN.
#
# Note that the INETDEV must be up and running when this script is
# started, or forwarding will not be enabled.
# Having tethering on multiple devices concurrently is possible,
# but forwarding will be disabled once the first device disconnetcs.
# A better check is necessary.
#
# Put this into /etc/dbus-scripts/tethering :
# /usr/bin/tethering.sh * * org.freedesktop.Hal.Manager DeviceAdded
# /usr/bin/tethering.sh * * org.freedesktop.Hal.Manager DeviceRemoved
#
# dbus-scripts doesn't give us the event path, so we have to use a more
# convoluted method of getting the network interface.
#
 
EVENT="$4"
UDI="$5"
HALDEV="$(echo $UDI | sed 's#.*/\([0-9a-zA-Z_]*\)#\1#')"
LOG="/tmp/tethering.log"
INETDEV="gprs0"
RUNFILE="/var/run/tethering.$HALDEV.pid"
IFACEFILE="/var/run/tethering.$HALDEV.iface"
 
if [ "$EVENT" = "DeviceAdded" ]; then
	IFACE=$(hal-get-property --udi $UDI --key net.interface)
	case $IFACE in
		bnep0 )
			IF_ADDRESS=192.168.254.254
			IF_RANGE=192.168.254.1,192.168.254.254
		;;
	 	usb0 )
			IF_ADDRESS=192.168.253.254
			IF_RANGE=192.168.253.1,192.168.253.254
		;;
		* )
			exit 0
		;;
	esac
 
	echo "$(date) Enabling tethering on interface $IFACE" >> $LOG
	echo "$IFACE" > $IFACEFILE
	ifconfig "$IFACE" "$IF_ADDRESS"
	/sbin/modprobe ipt_MASQUERADE
	/usr/sbin/iptables -t nat -A POSTROUTING -o $INETDEV -j MASQUERADE
	start-stop-daemon -S -p "$RUNFILE" -m -b -x /usr/sbin/dnsmasq -- -k -I lo -i "$IFACE" -a $IF_ADDRESS -z -F $IF_RANGE,3600
	echo 1 > /proc/sys/net/ipv4/conf/$IFACE/forwarding
	echo 1 > /proc/sys/net/ipv4/conf/$INETDEV/forwarding
fi
 
if [ "$EVENT" = "DeviceRemoved" ]; then
	if [ ! -f "$IFACEFILE" ]; then
		exit 0
	fi
	IFACE="$(cat $IFACEFILE)"
	rm "$IFACEFILE"
	echo "$(date) Disabling tethering on device $IFACE" >> $LOG
	echo 0 > /proc/sys/net/ipv4/conf/$INETDEV/forwarding
	start-stop-daemon -K -p "$RUNFILE" -x /usr/sbin/dnsmasq
	/usr/sbin/iptables -t nat -D POSTROUTING -o $INETDEV -j MASQUERADE
fi
 
exit 0

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