User:Nbc/W32g

w32g is a dbus script to toggle 2g/3g when connected/disconnected to wifi.

The configuration documentation is at the beginning w32g.conf. You can copy it to /home/user/.w32g.conf and modify it :

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

And you must copy the program to /home/user/bin/w32g and follow instruction below :

#!/usr/bin/python # 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

import sys import os import ConfigParser import re import commands import time

debug = False 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', 'wait_on_call': 10, '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']) config['wait_on_call']  = int(config['wait_on_call']) 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): if debug: print "not the good wifi network, exit" 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"; dbus_test_call = "dbus-send --system --dest=com.nokia.csd.Call --print-reply=literal /com/nokia/csd/call/1 com.nokia.csd.Call.Instance.GetStatus" def is_on_call: output = commands.getoutput(dbus_test_call) if (re.match(".*uint32 0", output)): return True return False def wait_or_exit_on_call: if debug: print "in wait or exit on call" choice = config['wait_on_call'] if choice > 0: end = time.time + choice while (end > time.time): if is_on_call: return if (debug): print "wait" time.sleep(1) sys.exit else: sys.exit if state == 'CONNECTED': if (debug): print "CONNECTED STATE" # we verified that the connection works if config['connection_test']: ret = os.system(config['connection_test']); else: ret = 0 # we wait or exit if there's a call wait_or_exit_on_call 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']: if (debug): print "IDLE STATE" dual = "2" if config['change_to_dual']: dual = "0" print dual # we wait or exit if there's a call wait_or_exit_on_call os.system(dbus_wifi + " byte:" + dual); os.system(dbus_notif + " string:'" + config['message_on_idle'] + "'") print state if debug: print "end here"