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