service: Send D-Bus reply for Manager.ConnectService when service is ready
[framework/connectivity/connman.git] / test / simple-agent
1 #!/usr/bin/python
2
3 import gobject
4
5 import dbus
6 import dbus.service
7 import dbus.mainloop.glib
8 import sys
9
10 class Canceled(dbus.DBusException):
11         _dbus_error_name = "net.connman.Error.Canceled"
12
13 class Agent(dbus.service.Object):
14         passphrase = None
15         wpspin = None
16
17         @dbus.service.method("net.connman.Agent",
18                                         in_signature='', out_signature='')
19         def Release(self):
20                 print("Release")
21                 mainloop.quit()
22
23         @dbus.service.method("net.connman.Agent",
24                                         in_signature='oa{sv}',
25                                         out_signature='a{sv}')
26         def RequestInput(self, path, fields):
27                 print "RequestInput (%s,%s)" % (path, fields)
28
29                 if not self.passphrase and not self.wpspin:
30                         args = raw_input('Answer: ')
31
32                         response = {}
33
34                         for arg in args.split():
35                                 if arg.startswith("Passphrase="):
36                                         passphrase = arg.replace("Passphrase=", "", 1)
37                                         response["Passphrase"] = passphrase
38                                         break
39                                 if arg.startswith("WPS="):
40                                         wpspin = arg.replace("WPS=", "", 1)
41                                         response["WPS"] = wpspin
42                                         break
43                 else:
44                         if self.passphrase:
45                                 response["Passphrase"] = self.passphrase
46                         else:
47                                 response["WPS"] = self.wpspin
48
49                 print "returning (%s)" % (response)
50
51                 return response
52
53         @dbus.service.method("net.connman.Agent",
54                                         in_signature='os',
55                                         out_signature='')
56         def ReportError(self, path, error):
57                 print "ReportError %s, %s" % (path, error)
58                 retry = raw_input("Retry service (yes/no): ")
59                 if (retry == "yes"):
60                         class Retry(dbus.DBusException):
61                                 _dbus_error_name = "net.connman.Agent.Error.Retry"
62
63                         raise Retry("retry service")
64                 else:
65                         return
66
67
68         @dbus.service.method("net.connman.Agent",
69                                         in_signature='', out_signature='')
70         def Cancel(self):
71                 print "Cancel"
72
73 def print_usage():
74         print "Usage: %s Passphrase=<passphrase> WPS=<wpspin>" % (sys.argv[0])
75         print "Help: %s help" % (sys.ar[0])
76         sys.exit(1)
77
78 if __name__ == '__main__':
79         if len(sys.argv) == 2 and sys.argv[1] == "help":
80                 print_usage()
81
82         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
83
84         bus = dbus.SystemBus()
85         manager = dbus.Interface(bus.get_object('net.connman', "/"),
86                                         'net.connman.Manager')
87
88         path = "/test/agent"
89         object = Agent(bus, path)
90
91         if len(sys.argv) >= 2:
92                 for arg in sys.argv[1:]:
93                         if arg.startswith("Passphrase="):
94                                 object.passphrase = arg.replace("Passphrase=", "", 1)
95                         elif arg.startswith("WPS="):
96                                 object.wpspin = arg.replace("WPS=", "", 1)
97                         else:
98                                 print_usage()
99
100         manager.RegisterAgent(path)
101
102         mainloop = gobject.MainLoop()
103         mainloop.run()
104
105         #manager.UnregisterAgent(path)