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.service
      8 import dbus.mainloop.glib
      9 
     10 class Rejected(dbus.DBusException):
     11 	_dbus_error_name = "org.bluez.Error.Rejected"
     12 
     13 class Agent(dbus.service.Object):
     14 	exit_on_release = True
     15 
     16 	def set_exit_on_release(self, exit_on_release):
     17 		self.exit_on_release = exit_on_release
     18 
     19 	@dbus.service.method("org.bluez.Agent",
     20 					in_signature="", out_signature="")
     21 	def Release(self):
     22 		print "Release"
     23 		if self.exit_on_release:
     24 			mainloop.quit()
     25 
     26 	@dbus.service.method("org.bluez.Agent",
     27 					in_signature="os", out_signature="")
     28 	def Authorize(self, device, uuid):
     29 		print "Authorize (%s, %s)" % (device, uuid)
     30 		authorize = raw_input("Authorize connection (yes/no): ")
     31 		if (authorize == "yes"):
     32 			return
     33 		raise Rejected("Connection rejected by user")
     34 
     35 	@dbus.service.method("org.bluez.Agent",
     36 					in_signature="o", out_signature="s")
     37 	def RequestPinCode(self, device):
     38 		print "RequestPinCode (%s)" % (device)
     39 		return raw_input("Enter PIN Code: ")
     40 
     41 	@dbus.service.method("org.bluez.Agent",
     42 					in_signature="o", out_signature="u")
     43 	def RequestPasskey(self, device):
     44 		print "RequestPasskey (%s)" % (device)
     45 		passkey = raw_input("Enter passkey: ")
     46 		return dbus.UInt32(passkey)
     47 
     48 	@dbus.service.method("org.bluez.Agent",
     49 					in_signature="ou", out_signature="")
     50 	def DisplayPasskey(self, device, passkey):
     51 		print "DisplayPasskey (%s, %d)" % (device, passkey)
     52 
     53 	@dbus.service.method("org.bluez.Agent",
     54 					in_signature="ou", out_signature="")
     55 	def RequestConfirmation(self, device, passkey):
     56 		print "RequestConfirmation (%s, %d)" % (device, passkey)
     57 		confirm = raw_input("Confirm passkey (yes/no): ")
     58 		if (confirm == "yes"):
     59 			return
     60 		raise Rejected("Passkey doesn't match")
     61 
     62 	@dbus.service.method("org.bluez.Agent",
     63 					in_signature="s", out_signature="")
     64 	def ConfirmModeChange(self, mode):
     65 		print "ConfirmModeChange (%s)" % (mode)
     66 		authorize = raw_input("Authorize mode change (yes/no): ")
     67 		if (authorize == "yes"):
     68 			return
     69 		raise Rejected("Mode change by user")
     70 
     71 	@dbus.service.method("org.bluez.Agent",
     72 					in_signature="", out_signature="")
     73 	def Cancel(self):
     74 		print "Cancel"
     75 
     76 def create_device_reply(device):
     77 	print "New device (%s)" % (device)
     78 	mainloop.quit()
     79 
     80 def create_device_error(error):
     81 	print "Creating device failed: %s" % (error)
     82 	mainloop.quit()
     83 
     84 if __name__ == '__main__':
     85 	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
     86 
     87 	bus = dbus.SystemBus()
     88 	manager = dbus.Interface(bus.get_object("org.bluez", "/"),
     89 							"org.bluez.Manager")
     90 
     91 	if len(sys.argv) > 1:
     92 		path = manager.FindAdapter(sys.argv[1])
     93 	else:
     94 		path = manager.DefaultAdapter()
     95 
     96 	adapter = dbus.Interface(bus.get_object("org.bluez", path),
     97 							"org.bluez.Adapter")
     98 
     99 	path = "/test/agent"
    100 	agent = Agent(bus, path)
    101 
    102 	mainloop = gobject.MainLoop()
    103 
    104 	if len(sys.argv) > 2:
    105 		if len(sys.argv) > 3:
    106 			device = adapter.FindDevice(sys.argv[2])
    107 			adapter.RemoveDevice(device)
    108 
    109 		agent.set_exit_on_release(False)
    110 		adapter.CreatePairedDevice(sys.argv[2], path, "DisplayYesNo",
    111 					reply_handler=create_device_reply,
    112 					error_handler=create_device_error)
    113 	else:
    114 		adapter.RegisterAgent(path, "DisplayYesNo")
    115 		print "Agent registered"
    116 
    117 	mainloop.run()
    118 
    119 	#adapter.UnregisterAgent(path)
    120 	#print "Agent unregistered"
    121