Release tizen_2.0_beta
[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                                 if arg.startswith("Passphrase="):
37                                         passphrase = arg.replace("Passphrase=", "", 1)
38                                         response["Passphrase"] = passphrase
39                                 if arg.startswith("WPS="):
40                                         wpspin = arg.replace("WPS=", "", 1)
41                                         response["WPS"] = wpspin
42                                         break
43                 else:
44                         if self.identity:
45                                 response["Identity"] = self.identity
46                         if self.passphrase:
47                                 response["Passphrase"] = self.passphrase
48                         if self.wpspin:
49                                 response["WPS"] = self.wpspin
50
51                 return response
52
53         def input_username(self):
54                 response = {}
55
56                 if not self.username and not self.password:
57                         args = raw_input('Answer: ')
58
59                         for arg in args.split():
60                                 if arg.startswith("Username="):
61                                         username = arg.replace("Username=", "", 1)
62                                         response["Username"] = username
63                                 if arg.startswith("Password="):
64                                         password = arg.replace("Password=", "", 1)
65                                         response["Password"] = password
66                 else:
67                         if self.username:
68                                 response["Username"] = self.username
69                         if self.password:
70                                 response["Password"] = self.password
71
72                 return response
73
74         @dbus.service.method("net.connman.Agent",
75                                         in_signature='oa{sv}',
76                                         out_signature='a{sv}')
77         def RequestInput(self, path, fields):
78                 print "RequestInput (%s,%s)" % (path, fields)
79
80                 response = None
81
82                 if fields.has_key("Passphrase"):
83                         response = self.input_passphrase()
84                 elif fields.has_key("Username"):
85                         response = self.input_username()
86                 else:
87                         print "No method to answer the input request"
88
89                 print "returning (%s)" % (response)
90
91                 return response
92
93         @dbus.service.method("net.connman.Agent",
94                                         in_signature='os',
95                                         out_signature='')
96         def ReportError(self, path, error):
97                 print "ReportError %s, %s" % (path, error)
98                 retry = raw_input("Retry service (yes/no): ")
99                 if (retry == "yes"):
100                         class Retry(dbus.DBusException):
101                                 _dbus_error_name = "net.connman.Agent.Error.Retry"
102
103                         raise Retry("retry service")
104                 else:
105                         return
106
107
108         @dbus.service.method("net.connman.Agent",
109                                         in_signature='', out_signature='')
110         def Cancel(self):
111                 print "Cancel"
112
113 def print_usage():
114         print "Usage:"
115         print "For EAP/WPA input:"
116         print "%s Identity=<identity> Passphrase=<passphrase> WPS=<wpspin>" % (sys.argv[0])
117         print "For WISPr login input:"
118         print "%s Username=<username> Password=<password>" % (sys.argv[0])
119         print "Help: %s help" % (sys.argv[0])
120         sys.exit(1)
121
122 if __name__ == '__main__':
123         if len(sys.argv) == 2 and sys.argv[1] == "help":
124                 print_usage()
125
126         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
127
128         bus = dbus.SystemBus()
129         manager = dbus.Interface(bus.get_object('net.connman', "/"),
130                                         'net.connman.Manager')
131
132         path = "/test/agent"
133         object = Agent(bus, path)
134
135         if len(sys.argv) >= 2:
136                 for arg in sys.argv[1:]:
137                         if arg.startswith("Identity="):
138                                 object.identity = arg.replace("Identity=", "", 1)
139                         elif arg.startswith("Passphrase="):
140                                 object.passphrase = arg.replace("Passphrase=", "", 1)
141                         elif arg.startswith("WPS="):
142                                 object.wpspin = arg.replace("WPS=", "", 1)
143                         elif arg.startswith("Username="):
144                                 object.username = arg.replace("Username=", "", 1)
145                         elif arg.startswith("Password="):
146                                 object.password = arg.replace("Password=", "", 1)
147                         else:
148                                 print_usage()
149
150         manager.RegisterAgent(path)
151
152         mainloop = gobject.MainLoop()
153         mainloop.run()
154
155         #manager.UnregisterAgent(path)