service: Simplify nameserver route adding and removing
[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 LaunchBrowser(dbus.DBusException):
14         _dbus_error_name = "net.connman.Agent.Error.LaunchBrowser"
15
16 class Agent(dbus.service.Object):
17         name = None
18         ssid = None
19         identity = None
20         passphrase = None
21         wpspin = None
22         username = None
23         password = None
24
25         @dbus.service.method("net.connman.Agent",
26                                         in_signature='', out_signature='')
27         def Release(self):
28                 print("Release")
29                 mainloop.quit()
30
31         def input_passphrase(self):
32                 response = {}
33
34                 if not self.identity and not self.passphrase and not self.wpspin:
35                         print "Service credentials requested, type cancel to cancel"
36                         args = raw_input('Answer: ')
37
38                         for arg in args.split():
39                                 if arg.startswith("cancel"):
40                                         response["Error"] = arg
41                                 if arg.startswith("Identity="):
42                                         identity = arg.replace("Identity=", "", 1)
43                                         response["Identity"] = identity
44                                 if arg.startswith("Passphrase="):
45                                         passphrase = arg.replace("Passphrase=", "", 1)
46                                         response["Passphrase"] = passphrase
47                                 if arg.startswith("WPS="):
48                                         wpspin = arg.replace("WPS=", "", 1)
49                                         response["WPS"] = wpspin
50                                         break
51                 else:
52                         if self.identity:
53                                 response["Identity"] = self.identity
54                         if self.passphrase:
55                                 response["Passphrase"] = self.passphrase
56                         if self.wpspin:
57                                 response["WPS"] = self.wpspin
58
59                 return response
60
61         def input_username(self):
62                 response = {}
63
64                 if not self.username and not self.password:
65                         print "User login requested, type cancel to cancel"
66                         print "or browser to login through the browser by yourself."
67                         args = raw_input('Answer: ')
68
69                         for arg in args.split():
70                                 if arg.startswith("cancel") or arg.startswith("browser"):
71                                         response["Error"] = arg
72                                 if arg.startswith("Username="):
73                                         username = arg.replace("Username=", "", 1)
74                                         response["Username"] = username
75                                 if arg.startswith("Password="):
76                                         password = arg.replace("Password=", "", 1)
77                                         response["Password"] = password
78                 else:
79                         if self.username:
80                                 response["Username"] = self.username
81                         if self.password:
82                                 response["Password"] = self.password
83
84                 return response
85
86         def input_hidden(self):
87                 response = {}
88
89                 if not self.name and not self.ssid:
90                         args = raw_input('Answer ')
91
92                         for arg in args.split():
93                                 if arg.startswith("Name="):
94                                         name = arg.replace("Name=", "", 1)
95                                         response["Name"] = name
96                                         break
97                                 if arg.startswith("SSID="):
98                                         ssid = arg.replace("SSID", "", 1)
99                                         response["SSID"] = ssid
100                                         break
101                 else:
102                         if self.name:
103                                 response["Name"] = self.name
104                         if self.ssid:
105                                 response["SSID"] = self.ssid
106
107                 return response
108
109         @dbus.service.method("net.connman.Agent",
110                                         in_signature='oa{sv}',
111                                         out_signature='a{sv}')
112         def RequestInput(self, path, fields):
113                 print "RequestInput (%s,%s)" % (path, fields)
114
115                 response = {}
116
117                 if fields.has_key("Name"):
118                         response.update(self.input_hidden())
119                 if fields.has_key("Passphrase"):
120                         response.update(self.input_passphrase())
121                 if fields.has_key("Username"):
122                         response.update(self.input_username())
123
124                 if response.has_key("Error"):
125                         if response["Error"] == "cancel":
126                                 raise Canceled("canceled")
127                                 return
128                         if response["Error"] == "browser":
129                                 raise LaunchBrowser("launch browser")
130                                 return
131
132                 print "returning (%s)" % (response)
133
134                 return response
135
136         @dbus.service.method("net.connman.Agent",
137                                         in_signature='os',
138                                         out_signature='')
139         def RequestBrowser(self, path, url):
140                 print "RequestBrowser (%s,%s)" % (path, url)
141
142                 print "Please login through the given url in a browser"
143                 print "Then press enter to accept or some text to cancel"
144
145                 args = raw_input('> ')
146
147                 if len(args) > 0:
148                         raise Canceled("canceled")
149
150                 return
151
152         @dbus.service.method("net.connman.Agent",
153                                         in_signature='os',
154                                         out_signature='')
155         def ReportError(self, path, error):
156                 print "ReportError %s, %s" % (path, error)
157                 retry = raw_input("Retry service (yes/no): ")
158                 if (retry == "yes"):
159                         class Retry(dbus.DBusException):
160                                 _dbus_error_name = "net.connman.Agent.Error.Retry"
161
162                         raise Retry("retry service")
163                 else:
164                         return
165
166
167         @dbus.service.method("net.connman.Agent",
168                                         in_signature='', out_signature='')
169         def Cancel(self):
170                 print "Cancel"
171
172 def print_usage():
173         print "Usage:"
174         print "For hidden service:"
175         print "%s Name=<hidden service name> [SSID=<hidden ssid>]" % (sys.argv[0])
176         print "For EAP/WPA input:"
177         print "%s Identity=<identity> Passphrase=<passphrase> WPS=<wpspin>" % (sys.argv[0])
178         print "For WISPr login input:"
179         print "%s Username=<username> Password=<password>" % (sys.argv[0])
180         print "Help: %s help" % (sys.argv[0])
181         sys.exit(1)
182
183 if __name__ == '__main__':
184         if len(sys.argv) == 2 and sys.argv[1] == "help":
185                 print_usage()
186
187         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
188
189         bus = dbus.SystemBus()
190         manager = dbus.Interface(bus.get_object('net.connman', "/"),
191                                         'net.connman.Manager')
192
193         path = "/test/agent"
194         object = Agent(bus, path)
195
196         if len(sys.argv) >= 2:
197                 for arg in sys.argv[1:]:
198                         if arg.startswith("Name="):
199                                 object.name = arg.replace("Name=", "", 1)
200                         elif arg.startswith("SSID="):
201                                 object.ssid = arg.replace("SSID=", "", 1)
202                         elif arg.startswith("Identity="):
203                                 object.identity = arg.replace("Identity=", "", 1)
204                         elif arg.startswith("Passphrase="):
205                                 object.passphrase = arg.replace("Passphrase=", "", 1)
206                         elif arg.startswith("WPS="):
207                                 object.wpspin = arg.replace("WPS=", "", 1)
208                         elif arg.startswith("Username="):
209                                 object.username = arg.replace("Username=", "", 1)
210                         elif arg.startswith("Password="):
211                                 object.password = arg.replace("Password=", "", 1)
212                         else:
213                                 print_usage()
214
215         manager.RegisterAgent(path)
216
217         mainloop = gobject.MainLoop()
218         mainloop.run()
219
220         #manager.UnregisterAgent(path)