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='')
47 @dbus.service.method("net.connman.Notification",
48 in_signature='a{sv}', out_signature='')
49 def Update(self, settings):
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])
60 print " %s = %s" % (key, val)
65 class SessionApplication(dbus.service.Object):
66 def __init__(self, bus, object_path, mainloop):
67 dbus.service.Object.__init__(self, bus, object_path)
70 self.notify_path = object_path + "/notify"
72 self.session_path = None
73 self.mainloop = mainloop
78 bus = dbus.SystemBus()
79 bus.watch_name_owner('net.connman', self.connman_name_owner_changed)
80 except dbus.DBusException:
84 def connman_name_owner_changed(self, proxy):
87 print "connman appeared on D-Bus ", str(proxy)
89 bus = dbus.SystemBus()
90 self.manager = dbus.Interface(bus.get_object("net.connman", "/"),
91 "net.connman.Manager")
93 print "connman disappeared on D-Bus"
97 self.notify.remove_from_connection()
100 except dbus.DBusException:
101 traceback.print_exc()
106 self.manager.DestroySession(self.session_path)
109 self.notify.remove_from_connection()
112 def type_convert(self, key, value):
113 if key in [ "AllowedBearers", "RoamingPolicy" ]:
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" ]:
122 return dbus.UInt32(val)
126 @dbus.service.method("com.example.TestSession",
127 in_signature='', out_signature='')
128 def CreateSession(self):
129 print "Create session"
132 print "already running a session -> drop reqest"
136 bus = dbus.SystemBus()
138 self.notify = Notification(bus, self, self.notify_path)
139 self.notify.add_to_connection(bus, self.notify_path)
141 self.session_path = self.manager.CreateSession(self.settings, self.notify_path)
143 print "notify path %s" % (self.notify_path)
144 print "session path %s" % (self.session_path)
146 self.session = dbus.Interface(bus.get_object("net.connman", self.session_path),
147 "net.connman.Session")
149 except dbus.DBusException, e:
150 if e.get_dbus_name() in ['net.connman.Error.Failed']:
151 print e.get_dbus_message()
153 traceback.print_exc()
156 @dbus.service.method("com.example.TestSession",
157 in_signature='', out_signature='')
158 def DestroySession(self):
159 print "Destroy session"
161 if self.session == None:
162 print "no session running -> drop request"
167 except dbus.DBusException:
168 traceback.print_exc()
171 @dbus.service.method("com.example.TestSession",
172 in_signature='', out_signature='')
174 print "Connect session"
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()
181 traceback.print_exc()
184 @dbus.service.method("com.example.TestSession",
185 in_signature='', out_signature='')
186 def Disconnect(self):
187 print "Disconnect session"
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()
194 traceback.print_exc()
197 @dbus.service.method("com.example.TestSession",
198 in_signature='sv', out_signature='')
199 def Change(self, key, value):
200 print "Update session settings"
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()
208 traceback.print_exc()
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
219 if len(sys.argv) < 2:
220 print "Usage: %s <command>" % (sys.argv[0])
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>"
231 print " run <app_path>"
234 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
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)
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)
250 if (len(sys.argv) < 3):
251 print "Need test application path"
254 app_path = sys.argv[2]
255 bus = dbus.SessionBus()
257 app_name = "com.example.SessionApplication.%s" % (string.strip(app_path, "/"))
259 if sys.argv[1] == "run":
260 name = dbus.service.BusName(app_name, bus)
261 mainloop = gobject.MainLoop()
263 app = SessionApplication(bus, app_path, mainloop)
268 app = dbus.Interface(bus.get_object(app_name, app_path),
269 "com.example.TestSession")
271 if sys.argv[1] == "create":
274 elif sys.argv[1] == "destroy":
277 elif sys.argv[1] == "connect":
280 elif sys.argv[1] == "disconnect":
283 elif sys.argv[1] == "change":
284 if len(sys.argv) < 4:
285 print "Arguments missing"
288 app.Change(sys.argv[3], sys.argv[4:])
290 elif sys.argv[1] == "configure":
291 if len(sys.argv) < 4:
292 print "Arguments missing"
295 app.Configure(sys.argv[3], sys.argv[4:])
298 print "Unknown command '%s'" % sys.argv[1]
301 if __name__ == '__main__':