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