8 import dbus.mainloop.glib
14 def extract_list(list):
21 def extract_values(values):
23 for key in values.keys():
24 val += " " + key + "="
25 if key in ["PrefixLength"]:
26 val += "%s" % (int(values[key]))
28 if key in ["Servers", "Excludes"]:
29 val += extract_list(values[key])
31 val += str(values[key])
35 class Notification(dbus.service.Object):
36 def __init__(self, bus, app, notify_path):
37 dbus.service.Object.__init__(self)
40 @dbus.service.method("net.connman.Notification",
41 in_signature='', out_signature='')
46 @dbus.service.method("net.connman.Notification",
47 in_signature='a{sv}', out_signature='')
48 def Update(self, settings):
52 for key in settings.keys():
53 if key in ["IPv4", "IPv6"]:
54 val = extract_values(settings[key])
55 elif key in ["AllowedBearers"]:
56 val = extract_list(settings[key])
59 print " %s = %s" % (key, val)
64 class SessionApplication(dbus.service.Object):
65 def __init__(self, bus, object_path, mainloop):
66 dbus.service.Object.__init__(self, bus, object_path)
69 self.notify_path = object_path + "/notify"
71 self.session_path = None
72 self.mainloop = mainloop
77 bus = dbus.SystemBus()
78 bus.watch_name_owner('net.connman', self.connman_name_owner_changed)
79 except dbus.DBusException:
83 def connman_name_owner_changed(self, proxy):
86 print "connman appeared on D-Bus ", str(proxy)
88 bus = dbus.SystemBus()
89 self.manager = dbus.Interface(bus.get_object("net.connman", "/"),
90 "net.connman.Manager")
92 print "connman disappeared on D-Bus"
96 self.notify.remove_from_connection()
99 except dbus.DBusException:
100 traceback.print_exc()
105 self.manager.DestroySession(self.session_path)
108 self.notify.remove_from_connection()
111 def type_convert(self, key, value):
112 if key in [ "AllowedBearers", "RoamingPolicy" ]:
114 elif key in [ "Priority", "AvoidHandover",
115 "StayConnected", "EmergencyCall" ]:
116 flag = str(value[0]).strip().lower()
117 val = flag not in ['false', 'f', 'n', '0']
118 return dbus.Boolean(val)
119 elif key in [ "PeriodicConnect", "IdleTimeout" ]:
121 return dbus.UInt32(val)
125 @dbus.service.method("com.example.TestSession",
126 in_signature='', out_signature='')
127 def CreateSession(self):
128 print "Create session"
131 print "already running a session -> drop reqest"
135 bus = dbus.SystemBus()
137 self.notify = Notification(bus, self, self.notify_path)
138 self.notify.add_to_connection(bus, self.notify_path)
140 self.session_path = self.manager.CreateSession(self.settings, self.notify_path)
142 print "notify path %s" % (self.notify_path)
143 print "session path %s" % (self.session_path)
145 self.session = dbus.Interface(bus.get_object("net.connman", self.session_path),
146 "net.connman.Session")
148 except dbus.DBusException, e:
149 if e.get_dbus_name() in ['net.connman.Error.InvalidArguments']:
150 print e.get_dbus_message()
152 traceback.print_exc()
155 @dbus.service.method("com.example.TestSession",
156 in_signature='', out_signature='')
157 def DestroySession(self):
158 print "Destroy session"
160 if self.session == None:
161 print "no session running -> drop request"
166 except dbus.DBusException:
167 traceback.print_exc()
170 @dbus.service.method("com.example.TestSession",
171 in_signature='', out_signature='')
173 print "Connect session"
175 self.session.Connect()
176 except dbus.DBusException, e:
177 if e.get_dbus_name() in ['net.connman.Error.InProgress',
178 'net.connman.Error.AlreadyConnected']:
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.AlreadyDisabled',
192 'net.connman.Error.NotConnected']:
193 print e.get_dbus_message()
195 traceback.print_exc()
198 @dbus.service.method("com.example.TestSession",
199 in_signature='sv', out_signature='')
200 def Change(self, key, value):
201 print "Update session settings"
203 val = self.type_convert(key, value)
204 self.session.Change(key, val)
205 except dbus.DBusException, e:
206 if e.get_dbus_name() in ['net.connman.Error.InvalidArguments']:
207 print e.get_dbus_message()
209 traceback.print_exc()
212 @dbus.service.method("com.example.TestSession",
213 in_signature='sv', out_signature='')
214 def Configure(self, key, value):
215 print "Configure session settings"
216 val = self.type_convert(key, value)
217 self.settings[key] = val
220 if len(sys.argv) < 2:
221 print "Usage: %s <command>" % (sys.argv[0])
225 print " create <app_path>"
226 print " destroy <app_path>"
227 print " connect <app_path>"
228 print " disconnect <app_path>"
229 print " change <app_path> <key> <value>"
230 print " configure <app_path> <key> <value>"
232 print " run <app_path>"
235 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
237 if sys.argv[1] == "enable":
238 bus = dbus.SystemBus()
239 manager = dbus.Interface(bus.get_object("net.connman", "/"),
240 "net.connman.Manager")
241 manager.SetProperty("SessionMode", True)
244 elif sys.argv[1] == "disable":
245 bus = dbus.SystemBus()
246 manager = dbus.Interface(bus.get_object("net.connman", "/"),
247 "net.connman.Manager")
248 manager.SetProperty("SessionMode", False)
251 if (len(sys.argv) < 3):
252 print "Need test application path"
255 app_path = sys.argv[2]
256 bus = dbus.SessionBus()
258 if sys.argv[1] == "run":
259 name = dbus.service.BusName("com.example.SessionApplication", bus)
260 mainloop = gobject.MainLoop()
262 app = SessionApplication(bus, app_path, mainloop)
267 app = dbus.Interface(bus.get_object("com.example.SessionApplication", app_path),
268 "com.example.TestSession")
270 if sys.argv[1] == "create":
273 elif sys.argv[1] == "destroy":
276 elif sys.argv[1] == "connect":
279 elif sys.argv[1] == "disconnect":
282 elif sys.argv[1] == "change":
283 if len(sys.argv) < 4:
284 print "Arguments missing"
287 app.Change(sys.argv[3], sys.argv[4:])
289 elif sys.argv[1] == "configure":
290 if len(sys.argv) < 4:
291 print "Arguments missing"
294 app.Configure(sys.argv[3], sys.argv[4:])
297 print "Unknown command '%s'" % sys.argv[1]
300 if __name__ == '__main__':