core: Change coding style for passing NULL to time
[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         username = None
18         password = None
19
20         @dbus.service.method("net.connman.Agent",
21                                         in_signature='', out_signature='')
22         def Release(self):
23                 print("Release")
24                 mainloop.quit()
25
26         def input_passphrase(self):
27                 response = {}
28
29                 if not self.identity and not self.passphrase and not self.wpspin:
30                         args = raw_input('Answer: ')
31
32                         for arg in args.split():
33                                 if arg.startswith("Identity="):
34                                         identity = arg.replace("Identity=", "", 1)
35                                         response["Identity"] = identity
36                                         break
37                                 if arg.startswith("Passphrase="):
38                                         passphrase = arg.replace("Passphrase=", "", 1)
39                                         response["Passphrase"] = passphrase
40                                         break
41                                 if arg.startswith("WPS="):
42                                         wpspin = arg.replace("WPS=", "", 1)
43                                         response["WPS"] = wpspin
44                                         break
45                 else:
46                         if self.identity:
47                                 response["Identity"] = self.identity
48                         if self.passphrase:
49                                 response["Passphrase"] = self.passphrase
50                         if self.wpspin:
51                                 response["WPS"] = self.wpspin
52
53                 return response
54
55         def input_username(self):
56                 response = {}
57
58                 if not self.username and not self.password:
59                         args = raw_input('Answer: ')
60
61                         for arg in args.split():
62                                 if arg.startswith("Username="):
63                                         username = arg.replace("Username=", "", 1)
64                                         response["Username"] = username
65                                         break
66                                 if arg.startswith("Password="):
67                                         password = arg.replace("Password=", "", 1)
68                                         response["Password"] = password
69                                         break
70                 else:
71                         if self.username:
72                                 response["Username"] = self.username
73                         if self.password:
74                                 response["Password"] = self.password
75
76                 return response
77
78         @dbus.service.method("net.connman.Agent",
79                                         in_signature='oa{sv}',
80                                         out_signature='a{sv}')
81         def RequestInput(self, path, fields):
82                 print "RequestInput (%s,%s)" % (path, fields)
83
84                 response = None
85
86                 if fields.has_key("Passphrase"):
87                         response = self.input_passphrase()
88                 elif fields.has_key("Username"):
89                         response = self.input_username()
90                 else:
91                         print "No method to answer the input request"
92
93                 print "returning (%s)" % (response)
94
95                 return response
96
97         @dbus.service.method("net.connman.Agent",
98                                         in_signature='os',
99                                         out_signature='')
100         def RequestBrowser(self, path, url):
101                 print "RequestBrowser (%s,%s)" % (path, url)
102
103                 print "Please login through the given url in a browser"
104                 print "Then press enter to accept or some text to cancel"
105
106                 args = raw_input('> ')
107
108                 if len(args) > 0:
109                         raise Canceled("canceled")
110
111                 return
112
113         @dbus.service.method("net.connman.Agent",
114                                         in_signature='os',
115                                         out_signature='')
116         def ReportError(self, path, error):
117                 print "ReportError %s, %s" % (path, error)
118                 retry = raw_input("Retry service (yes/no): ")
119                 if (retry == "yes"):
120                         class Retry(dbus.DBusException):
121                                 _dbus_error_name = "net.connman.Agent.Error.Retry"
122
123                         raise Retry("retry service")
124                 else:
125                         return
126
127
128         @dbus.service.method("net.connman.Agent",
129                                         in_signature='', out_signature='')
130         def Cancel(self):
131                 print "Cancel"
132
133 def print_usage():
134         print "Usage:"
135         print "For EAP/WPA input:"
136         print "%s Identity=<identity> Passphrase=<passphrase> WPS=<wpspin>" % (sys.argv[0])
137         print "For WISPr login input:"
138         print "%s Username=<username> Password=<password>" % (sys.argv[0])
139         print "Help: %s help" % (sys.argv[0])
140         sys.exit(1)
141
142 if __name__ == '__main__':
143         if len(sys.argv) == 2 and sys.argv[1] == "help":
144                 print_usage()
145
146         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
147
148         bus = dbus.SystemBus()
149         manager = dbus.Interface(bus.get_object('net.connman', "/"),
150                                         'net.connman.Manager')
151
152         path = "/test/agent"
153         object = Agent(bus, path)
154
155         if len(sys.argv) >= 2:
156                 for arg in sys.argv[1:]:
157                         if arg.startswith("Identity="):
158                                 object.identity = arg.replace("Identity=", "", 1)
159                         elif arg.startswith("Passphrase="):
160                                 object.passphrase = arg.replace("Passphrase=", "", 1)
161                         elif arg.startswith("WPS="):
162                                 object.wpspin = arg.replace("WPS=", "", 1)
163                         elif arg.startswith("Username="):
164                                 object.username = arg.replace("Username=", "", 1)
165                         elif arg.startswith("Password="):
166                                 object.password = arg.replace("Password=", "", 1)
167                         else:
168                                 print_usage()
169
170         manager.RegisterAgent(path)
171
172         mainloop = gobject.MainLoop()
173         mainloop.run()
174
175         #manager.UnregisterAgent(path)