9784122ff690ed94a51f704427d8e9e5fbe0d7b3
[platform/upstream/connman.git] / test / test-session
1 #!/usr/bin/python
2
3 import sys
4 import gobject
5 import string
6
7 import dbus
8 import dbus.service
9 import dbus.mainloop.glib
10
11 import glib
12
13 import traceback
14
15 def extract_list(list):
16         val = "["
17         for i in list:
18                 val += " " + str(i)
19         val += " ]"
20         return val
21
22 def extract_values(values):
23         val = "{"
24         for key in values.keys():
25                 val += " " + key + "="
26                 if key in ["PrefixLength"]:
27                         val += "%s" % (int(values[key]))
28                 else:
29                         if key in ["Servers", "Excludes"]:
30                                 val += extract_list(values[key])
31                         else:
32                                 val += str(values[key])
33         val += " }"
34         return val
35
36 class Notification(dbus.service.Object):
37         def __init__(self, bus, app, notify_path):
38                 dbus.service.Object.__init__(self)
39                 self.app = app
40
41         @dbus.service.method("net.connman.Notification",
42                                 in_signature='', out_signature='')
43         def Release(self):
44                 print("Release")
45                 self.app.release()
46
47         @dbus.service.method("net.connman.Notification",
48                                 in_signature='a{sv}', out_signature='')
49         def Update(self, settings):
50                 print "Update called"
51
52                 try:
53                         for key in settings.keys():
54                                 if key in ["IPv4", "IPv6"]:
55                                         val = extract_values(settings[key])
56                                 elif key in  ["AllowedBearers"]:
57                                         val = extract_list(settings[key])
58                                 else:
59                                         val = settings[key]
60                                 print "    %s = %s" % (key, val)
61                 except:
62                         print "Exception:"
63                         traceback.print_exc()
64
65 class SessionApplication(dbus.service.Object):
66         def __init__(self, bus, object_path, mainloop):
67                 dbus.service.Object.__init__(self, bus, object_path)
68
69                 self.manager = None
70                 self.notify_path = object_path + "/notify"
71                 self.notify = None
72                 self.session_path = None
73                 self.mainloop = mainloop
74                 self.session = None
75                 self.settings = { }
76
77                 try:
78                         bus = dbus.SystemBus()
79                         bus.watch_name_owner('net.connman', self.connman_name_owner_changed)
80                 except dbus.DBusException:
81                         traceback.print_exc()
82                         exit(1)
83
84         def connman_name_owner_changed(self, proxy):
85                 try:
86                         if proxy:
87                                 print "connman appeared on D-Bus ", str(proxy)
88
89                                 bus = dbus.SystemBus()
90                                 self.manager = dbus.Interface(bus.get_object("net.connman", "/"),
91                                                               "net.connman.Manager")
92                         else:
93                                 print "connman disappeared on D-Bus"
94                                 self.manager = None
95                                 self.session = None
96                                 if self.notify:
97                                         self.notify.remove_from_connection()
98                                         self.notify = None
99
100                 except dbus.DBusException:
101                         traceback.print_exc()
102                         exit(1)
103
104         def release(self):
105                 if self.session:
106                         self.manager.DestroySession(self.session_path)
107                         self.session = None
108                 if self.notify:
109                         self.notify.remove_from_connection()
110                         self.notify = None
111
112         def type_convert(self, key, value):
113                 if key in [ "AllowedBearers", "RoamingPolicy" ]:
114                         return value
115                 elif key in [ "Priority", "AvoidHandover",
116                               "StayConnected", "EmergencyCall" ]:
117                         flag = str(value[0]).strip().lower()
118                         val = flag not in ['false', 'f', 'n', '0']
119                         return dbus.Boolean(val)
120                 elif key in [ "PeriodicConnect", "IdleTimeout" ]:
121                         val = value[0]
122                         return dbus.UInt32(val)
123
124                 return value
125
126         @dbus.service.method("com.example.TestSession",
127                                 in_signature='', out_signature='')
128         def CreateSession(self):
129                 print "Create session"
130
131                 if self.session:
132                         print "already running a session -> drop reqest"
133                         return
134
135                 try:
136                         bus = dbus.SystemBus()
137
138                         self.notify = Notification(bus, self, self.notify_path)
139                         self.notify.add_to_connection(bus, self.notify_path)
140
141                         self.session_path = self.manager.CreateSession(self.settings, self.notify_path)
142
143                         print "notify path %s" % (self.notify_path)
144                         print "session path %s" % (self.session_path)
145
146                         self.session = dbus.Interface(bus.get_object("net.connman", self.session_path),
147                                                       "net.connman.Session")
148
149                 except dbus.DBusException, e:
150                         if e.get_dbus_name() in ['net.connman.Error.Failed']:
151                                 print e.get_dbus_message()
152                                 return
153                         traceback.print_exc()
154                         exit(1)
155
156         @dbus.service.method("com.example.TestSession",
157                                 in_signature='', out_signature='')
158         def DestroySession(self):
159                 print "Destroy session"
160
161                 if self.session == None:
162                         print "no session running -> drop request"
163                         return
164
165                 try:
166                         self.release()
167                 except dbus.DBusException:
168                         traceback.print_exc()
169                         exit(1)
170
171         @dbus.service.method("com.example.TestSession",
172                                 in_signature='', out_signature='')
173         def Connect(self):
174                 print "Connect session"
175                 try:
176                         self.session.Connect()
177                 except dbus.DBusException, e:
178                         if e.get_dbus_name() in ['net.connman.Error.Failed']:
179                                 print e.get_dbus_message()
180                                 return
181                         traceback.print_exc()
182                         exit(1)
183
184         @dbus.service.method("com.example.TestSession",
185                                 in_signature='', out_signature='')
186         def Disconnect(self):
187                 print "Disconnect session"
188                 try:
189                         self.session.Disconnect()
190                 except dbus.DBusException, e:
191                         if e.get_dbus_name() in ['net.connman.Error.Failed']:
192                                 print e.get_dbus_message()
193                                 return
194                         traceback.print_exc()
195                         exit(1)
196
197         @dbus.service.method("com.example.TestSession",
198                                 in_signature='sv', out_signature='')
199         def Change(self, key, value):
200                 print "Update session settings"
201                 try:
202                         val = self.type_convert(key, value)
203                         self.session.Change(key, val)
204                 except dbus.DBusException, e:
205                         if e.get_dbus_name() in ['net.connman.Error.Failed']:
206                                 print e.get_dbus_message()
207                                 return
208                         traceback.print_exc()
209                         exit(1)
210
211         @dbus.service.method("com.example.TestSession",
212                                 in_signature='sv', out_signature='')
213         def Configure(self, key, value):
214                 print "Configure session settings"
215                 val = self.type_convert(key, value)
216                 self.settings[key] = val
217
218 def main():
219         if len(sys.argv) < 2:
220                 print "Usage: %s <command>" % (sys.argv[0])
221                 print ""
222                 print "  enable"
223                 print "  disable"
224                 print "  create <app_path>"
225                 print "  destroy <app_path>"
226                 print "  connect <app_path>"
227                 print "  disconnect <app_path>"
228                 print "  change <app_path> <key> <value>"
229                 print "  configure <app_path> <key> <value>"
230                 print ""
231                 print "  run <app_path>"
232                 sys.exit(1)
233
234         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
235
236         if sys.argv[1] == "enable":
237                 bus = dbus.SystemBus()
238                 manager = dbus.Interface(bus.get_object("net.connman", "/"),
239                                          "net.connman.Manager")
240                 manager.SetProperty("SessionMode", True)
241                 return
242
243         elif sys.argv[1] == "disable":
244                 bus = dbus.SystemBus()
245                 manager = dbus.Interface(bus.get_object("net.connman", "/"),
246                                          "net.connman.Manager")
247                 manager.SetProperty("SessionMode", False)
248                 return
249
250         if (len(sys.argv) < 3):
251                 print "Need test application path"
252                 sys.exit(1)
253
254         app_path = sys.argv[2]
255         bus = dbus.SessionBus()
256
257         app_name = "com.example.SessionApplication.%s" % (string.strip(app_path, "/"))
258
259         if sys.argv[1] == "run":
260                 name = dbus.service.BusName(app_name, bus)
261                 mainloop = gobject.MainLoop()
262
263                 app = SessionApplication(bus, app_path, mainloop)
264
265                 mainloop.run()
266                 return
267
268         app = dbus.Interface(bus.get_object(app_name, app_path),
269                              "com.example.TestSession")
270
271         if sys.argv[1] == "create":
272                 app.CreateSession()
273
274         elif sys.argv[1] == "destroy":
275                 app.DestroySession()
276
277         elif sys.argv[1] == "connect":
278                 app.Connect()
279
280         elif sys.argv[1] == "disconnect":
281                 app.Disconnect()
282
283         elif sys.argv[1] == "change":
284                 if len(sys.argv) < 4:
285                         print "Arguments missing"
286                         sys.exit(1)
287
288                 app.Change(sys.argv[3], sys.argv[4:])
289
290         elif sys.argv[1] == "configure":
291                 if len(sys.argv) < 4:
292                         print "Arguments missing"
293                         sys.exit(1)
294
295                 app.Configure(sys.argv[3], sys.argv[4:])
296
297         else:
298                 print "Unknown command '%s'" % sys.argv[1]
299                 sys.exit(1)
300
301 if __name__ == '__main__':
302         main()