9 import dbus.mainloop.glib
15 def extract_list(list):
22 def extract_values(values):
24 for key in values.keys():
25 val += " " + key + "="
26 if key in ["PrefixLength"]:
27 val += "%s" % (int(values[key]))
29 if key in ["Servers", "Excludes"]:
30 val += extract_list(values[key])
32 val += str(values[key])
36 class Notification(dbus.service.Object):
37 def __init__(self, bus, app, notify_path):
38 dbus.service.Object.__init__(self)
41 @dbus.service.method("net.connman.Notification",
42 in_signature='', out_signature='')
44 print "Release %s" % (self._object_path)
45 session_name = self._object_path.split('/')[-1]
46 self.app.release(session_name)
48 @dbus.service.method("net.connman.Notification",
49 in_signature='a{sv}', out_signature='')
50 def Update(self, settings):
51 print "Update called at %s" % (self._object_path)
54 for key in settings.keys():
55 if key in ["IPv4", "IPv6"]:
56 val = extract_values(settings[key])
57 elif key in ["AllowedBearers"]:
58 val = extract_list(settings[key])
61 print " %s = %s" % (key, val)
66 class SessionApplication(dbus.service.Object):
67 def __init__(self, bus, object_path, mainloop):
68 dbus.service.Object.__init__(self, bus, object_path)
71 self.mainloop = mainloop
75 bus = dbus.SystemBus()
76 bus.watch_name_owner('net.connman', self.connman_name_owner_changed)
77 except dbus.DBusException:
81 def connman_name_owner_changed(self, proxy):
84 print "connman appeared on D-Bus ", str(proxy)
86 bus = dbus.SystemBus()
87 self.manager = dbus.Interface(bus.get_object("net.connman", "/"),
88 "net.connman.Manager")
90 print "connman disappeared on D-Bus"
92 for s in self.sessions.keys():
93 self.sessions[s]['notify'].remove_from_connection()
94 self.sessions[s]['notify'] = None
98 except dbus.DBusException:
102 def release(self, session_name):
103 s = self.find_session(session_name)
107 s['session'].Destroy()
110 s['notify'].remove_from_connection()
112 del self.sessions[session_name]
114 def type_convert(self, key, value):
115 if key in [ "AllowedBearers" ]:
117 elif key in [ "RoamingPolicy", "ConnectionType" ]:
120 elif key in [ "Priority", "AvoidHandover",
121 "StayConnected", "EmergencyCall" ]:
122 flag = str(value[0]).strip().lower()
123 val = flag not in ['false', 'f', 'n', '0']
124 return dbus.Boolean(val)
125 elif key in [ "PeriodicConnect", "IdleTimeout" ]:
127 return dbus.UInt32(val)
131 def find_session(self, session_name):
132 if not session_name in self.sessions.keys():
134 return self.sessions[session_name]
136 @dbus.service.method("com.example.TestSession",
137 in_signature='', out_signature='')
138 def CreateSession(self, session_name):
139 print "Create session"
141 s = self.find_session(session_name)
142 if s and s['session'] :
143 print "Session %s already created-> drop reqest" % (session_name)
147 bus = dbus.SystemBus()
151 s['notify_path'] = self._object_path + "/" + session_name
152 s['notify'] = Notification(bus, self, s['notify_path'])
153 s['notify'].add_to_connection(bus, s['notify_path'])
154 if not 'settings' in s.keys():
156 s['session_path'] = self.manager.CreateSession(s['settings'], s['notify_path'])
157 print "notify path %s" % (s['notify_path'])
158 print "session path %s" % (s['session_path'])
159 s['session'] = dbus.Interface(bus.get_object("net.connman", s['session_path']),
160 "net.connman.Session")
161 self.sessions[session_name] = s
163 except dbus.DBusException, e:
164 if e.get_dbus_name() in ['net.connman.Error.Failed']:
165 print e.get_dbus_message()
167 traceback.print_exc()
170 @dbus.service.method("com.example.TestSession",
171 in_signature='', out_signature='')
172 def DestroySession(self, session_name):
173 print "Destroy session"
175 s = self.find_session(session_name)
176 if s == None or s['session'] == None:
177 print "The session is not running -> drop request"
181 self.release(session_name)
182 except dbus.DBusException:
183 traceback.print_exc()
186 @dbus.service.method("com.example.TestSession",
187 in_signature='', out_signature='')
188 def Connect(self, session_name):
189 print "Connect session"
191 s = self.find_session(session_name)
192 if s == None or s['session'] == None:
193 print "The session is not running -> drop request"
197 s['session'].Connect()
198 except dbus.DBusException, e:
199 if e.get_dbus_name() in ['net.connman.Error.Failed']:
200 print e.get_dbus_message()
202 traceback.print_exc()
205 @dbus.service.method("com.example.TestSession",
206 in_signature='', out_signature='')
207 def Disconnect(self, session_name):
208 print "Disconnect session"
210 s = self.find_session(session_name)
211 if s == None or s['session'] == None:
212 print "The session is not running -> drop request"
216 s['session'].Disconnect()
217 except dbus.DBusException, e:
218 if e.get_dbus_name() in ['net.connman.Error.Failed']:
219 print e.get_dbus_message()
221 traceback.print_exc()
224 @dbus.service.method("com.example.TestSession",
225 in_signature='', out_signature='')
226 def Change(self, session_name, key, value):
227 print "Update session settings"
229 s = self.find_session(session_name)
230 if s == None or s['session'] == None:
231 print "The session is not running -> drop request"
235 val = self.type_convert(key, value)
236 s['session'].Change(key, val)
237 except dbus.DBusException, e:
238 if e.get_dbus_name() in ['net.connman.Error.Failed']:
239 print e.get_dbus_message()
241 traceback.print_exc()
244 @dbus.service.method("com.example.TestSession",
245 in_signature='', out_signature='')
246 def Configure(self, session_name, key, value):
247 print "Configure session settings"
248 s = self.find_session(session_name)
251 s['notify_path'] = None
253 if not 'settings' in s.keys():
255 s['session_path'] = None
257 self.sessions[session_name] = s
258 if s and s['session']:
259 print "The session is running, use change -> drop request"
261 val = self.type_convert(key, value)
262 s['settings'][key] = val
265 if len(sys.argv) < 2:
266 print "Usage: %s <command>" % (sys.argv[0])
270 print " create <app_path> <session_name>"
271 print " destroy <app_path> <session_name>"
272 print " connect <app_path> <session_name>"
273 print " disconnect <app_path> <session_name>"
274 print " change <app_path> <session_name> <key> <value>"
275 print " configure <app_path> <session_name> <key> <value>"
277 print " run <app_path>"
280 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
282 if sys.argv[1] == "enable":
283 bus = dbus.SystemBus()
284 manager = dbus.Interface(bus.get_object("net.connman", "/"),
285 "net.connman.Manager")
286 manager.SetProperty("SessionMode", True)
289 elif sys.argv[1] == "disable":
290 bus = dbus.SystemBus()
291 manager = dbus.Interface(bus.get_object("net.connman", "/"),
292 "net.connman.Manager")
293 manager.SetProperty("SessionMode", False)
296 if (len(sys.argv) < 3):
297 print "Need test application path"
300 app_path = sys.argv[2]
301 bus = dbus.SessionBus()
303 app_name = "com.example.SessionApplication.%s" % (string.strip(app_path, "/"))
305 if sys.argv[1] == "run":
306 name = dbus.service.BusName(app_name, bus)
307 mainloop = gobject.MainLoop()
309 app = SessionApplication(bus, app_path, mainloop)
314 app = dbus.Interface(bus.get_object(app_name, app_path),
315 "com.example.TestSession")
317 if sys.argv[1] == "create":
318 app.CreateSession(sys.argv[3])
320 elif sys.argv[1] == "destroy":
321 app.DestroySession(sys.argv[3])
323 elif sys.argv[1] == "connect":
324 app.Connect(sys.argv[3])
326 elif sys.argv[1] == "disconnect":
327 app.Disconnect(sys.argv[3])
329 elif sys.argv[1] == "change":
330 if len(sys.argv) < 5:
331 print "Arguments missing"
334 app.Change(sys.argv[3], sys.argv[4], sys.argv[5:])
336 elif sys.argv[1] == "configure":
337 if len(sys.argv) < 5:
338 print "Arguments missing"
341 app.Configure(sys.argv[3], sys.argv[4], sys.argv[5:])
344 print "Unknown command '%s'" % sys.argv[1]
347 if __name__ == '__main__':