Home | History | Annotate | Download | only in test
      1 #!/usr/bin/python
      2 
      3 import sys
      4 import dbus
      5 import re
      6 from optparse import OptionParser, make_option
      7 
      8 bus = dbus.SystemBus()
      9 
     10 manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager")
     11 
     12 option_list = [
     13 		make_option("-i", "--device", action="store",
     14 				type="string", dest="dev_id"),
     15 		]
     16 parser = OptionParser(option_list=option_list)
     17 
     18 (options, args) = parser.parse_args()
     19 
     20 if options.dev_id:
     21 	adapter_path = manager.FindAdapter(options.dev_id)
     22 else:
     23 	adapter_path = manager.DefaultAdapter()
     24 
     25 adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
     26 							"org.bluez.Adapter")
     27 
     28 if (len(args) < 1):
     29 	print "Usage: %s <command>" % (sys.argv[0])
     30 	print ""
     31 	print "  list"
     32 	print "  create <address>"
     33 	print "  remove <address|path>"
     34 	print "  discover <address> [pattern]"
     35 	print "  class <address>"
     36 	print "  name <address>"
     37 	print "  alias <address> [alias]"
     38 	print "  trusted <address> [yes/no]"
     39 	print "  blocked <address> [yes/no]"
     40 	sys.exit(1)
     41 
     42 if (args[0] == "list"):
     43 	for path in adapter.ListDevices():
     44 		device = dbus.Interface(bus.get_object("org.bluez", path),
     45 							"org.bluez.Device")
     46 		properties = device.GetProperties()
     47 		print "%s %s" % (properties["Address"], properties["Alias"])
     48 
     49 	sys.exit(0)
     50 
     51 if (args[0] == "create"):
     52 	if (len(args) < 2):
     53 		print "Need address parameter"
     54 	else:
     55 		device = adapter.CreateDevice(args[1])
     56 		print device
     57 	sys.exit(0)
     58 
     59 if (args[0] == "remove"):
     60 	if (len(args) < 2):
     61 		print "Need address or object path parameter"
     62 	else:
     63 		try:
     64 			path = adapter.FindDevice(args[1])
     65 		except:
     66 			path = args[1]
     67 		adapter.RemoveDevice(path)
     68 	sys.exit(0)
     69 
     70 if (args[0] == "discover"):
     71 	if (len(args) < 2):
     72 		print "Need address parameter"
     73 	else:
     74 		path = adapter.FindDevice(args[1])
     75 		device = dbus.Interface(bus.get_object("org.bluez", path),
     76 							"org.bluez.Device")
     77 		if (len(args) < 3):
     78 			pattern = ""
     79 		else:
     80 			pattern = args[2]
     81 		services = device.DiscoverServices(pattern);
     82 		for key in services.keys():
     83 			p = re.compile(">.*?<")
     84 			xml = p.sub("><", services[key].replace("\n", ""))
     85 			print "[ 0x%5x ]" % (key)
     86 			print xml
     87 			print
     88 	sys.exit(0)
     89 
     90 if (args[0] == "class"):
     91 	if (len(args) < 2):
     92 		print "Need address parameter"
     93 	else:
     94 		path = adapter.FindDevice(args[1])
     95 		device = dbus.Interface(bus.get_object("org.bluez", path),
     96 							"org.bluez.Device")
     97 		properties = device.GetProperties()
     98 		print "0x%06x" % (properties["Class"])
     99 	sys.exit(0)
    100 
    101 if (args[0] == "name"):
    102 	if (len(args) < 2):
    103 		print "Need address parameter"
    104 	else:
    105 		path = adapter.FindDevice(args[1])
    106 		device = dbus.Interface(bus.get_object("org.bluez", path),
    107 							"org.bluez.Device")
    108 		properties = device.GetProperties()
    109 		print properties["Name"]
    110 	sys.exit(0)
    111 
    112 if (args[0] == "alias"):
    113 	if (len(args) < 2):
    114 		print "Need address parameter"
    115 	else:
    116 		path = adapter.FindDevice(args[1])
    117 		device = dbus.Interface(bus.get_object("org.bluez", path),
    118 							"org.bluez.Device")
    119 		if (len(args) < 3):
    120 			properties = device.GetProperties()
    121 			print properties["Alias"]
    122 		else:
    123 			device.SetProperty("Alias", args[2])
    124 	sys.exit(0)
    125 
    126 if (args[0] == "trusted"):
    127 	if (len(args) < 2):
    128 		print "Need address parameter"
    129 	else:
    130 		path = adapter.FindDevice(args[1])
    131 		device = dbus.Interface(bus.get_object("org.bluez", path),
    132 							"org.bluez.Device")
    133 		if (len(args) < 3):
    134 			properties = device.GetProperties()
    135 			print properties["Trusted"]
    136 		else:
    137 			if (args[2] == "yes"):
    138 				value = dbus.Boolean(1)
    139 			elif (args[2] == "no"):
    140 				value = dbus.Boolean(0)
    141 			else:
    142 				value = dbus.Boolean(args[2])
    143 			device.SetProperty("Trusted", value)
    144 	sys.exit(0)
    145 
    146 if (args[0] == "blocked"):
    147 	if (len(args) < 2):
    148 		print "Need address parameter"
    149 	else:
    150 		path = adapter.FindDevice(args[1])
    151 		device = dbus.Interface(bus.get_object("org.bluez", path),
    152 							"org.bluez.Device")
    153 		if (len(args) < 3):
    154 			properties = device.GetProperties()
    155 			print properties["Blocked"]
    156 		else:
    157 			if (args[2] == "yes"):
    158 				value = dbus.Boolean(1)
    159 			elif (args[2] == "no"):
    160 				value = dbus.Boolean(0)
    161 			else:
    162 				value = dbus.Boolean(args[2])
    163 			device.SetProperty("Blocked", value)
    164 	sys.exit(0)
    165 
    166 print "Unknown command"
    167 sys.exit(1)
    168