test-connman: Use Powered property to enable/disable Technologies
[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 ReportError(self, path, error):
101                 print "ReportError %s, %s" % (path, error)
102                 retry = raw_input("Retry service (yes/no): ")
103                 if (retry == "yes"):
104                         class Retry(dbus.DBusException):
105                                 _dbus_error_name = "net.connman.Agent.Error.Retry"
106
107                         raise Retry("retry service")
108                 else:
109                         return
110
111
112         @dbus.service.method("net.connman.Agent",
113                                         in_signature='', out_signature='')
114         def Cancel(self):
115                 print "Cancel"
116
117 def print_usage():
118         print "Usage:"
119         print "For EAP/WPA input:"
120         print "%s Identity=<identity> Passphrase=<passphrase> WPS=<wpspin>" % (sys.argv[0])
121         print "For WISPr login input:"
122         print "%s Username=<username> Password=<password>" % (sys.argv[0])
123         print "Help: %s help" % (sys.argv[0])
124         sys.exit(1)
125
126 if __name__ == '__main__':
127         if len(sys.argv) == 2 and sys.argv[1] == "help":
128                 print_usage()
129
130         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
131
132         bus = dbus.SystemBus()
133         manager = dbus.Interface(bus.get_object('net.connman', "/"),
134                                         'net.connman.Manager')
135
136         path = "/test/agent"
137         object = Agent(bus, path)
138
139         if len(sys.argv) >= 2:
140                 for arg in sys.argv[1:]:
141                         if arg.startswith("Identity="):
142                                 object.identity = arg.replace("Identity=", "", 1)
143                         elif arg.startswith("Passphrase="):
144                                 object.passphrase = arg.replace("Passphrase=", "", 1)
145                         elif arg.startswith("WPS="):
146                                 object.wpspin = arg.replace("WPS=", "", 1)
147                         elif arg.startswith("Username="):
148                                 object.username = arg.replace("Username=", "", 1)
149                         elif arg.startswith("Password="):
150                                 object.password = arg.replace("Password=", "", 1)
151                         else:
152                                 print_usage()
153
154         manager.RegisterAgent(path)
155
156         mainloop = gobject.MainLoop()
157         mainloop.run()
158
159         #manager.UnregisterAgent(path)