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 if self.session == None:
177 print "no session running -> drop request"
181 self.session.Connect()
182 except dbus.DBusException, e:
183 if e.get_dbus_name() in ['net.connman.Error.Failed']:
184 print e.get_dbus_message()
186 traceback.print_exc()
189 @dbus.service.method("com.example.TestSession",
190 in_signature='', out_signature='')
191 def Disconnect(self):
192 print "Disconnect session"
194 if self.session == None:
195 print "no session running -> drop request"
199 self.session.Disconnect()
200 except dbus.DBusException, e:
201 if e.get_dbus_name() in ['net.connman.Error.Failed']:
202 print e.get_dbus_message()
204 traceback.print_exc()
207 @dbus.service.method("com.example.TestSession",
208 in_signature='sv', out_signature='')
209 def Change(self, key, value):
210 print "Update session settings"
212 if self.session == None:
213 print "no session running -> drop request"
217 val = self.type_convert(key, value)
218 self.session.Change(key, val)
219 except dbus.DBusException, e:
220 if e.get_dbus_name() in ['net.connman.Error.Failed']:
221 print e.get_dbus_message()
223 traceback.print_exc()
226 @dbus.service.method("com.example.TestSession",
227 in_signature='sv', out_signature='')
228 def Configure(self, key, value):
229 print "Configure session settings"
230 val = self.type_convert(key, value)
231 self.settings[key] = val
234 if len(sys.argv) < 2:
235 print "Usage: %s <command>" % (sys.argv[0])
239 print " create <app_path>"
240 print " destroy <app_path>"
241 print " connect <app_path>"
242 print " disconnect <app_path>"
243 print " change <app_path> <key> <value>"
244 print " configure <app_path> <key> <value>"
246 print " run <app_path>"
249 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
251 if sys.argv[1] == "enable":
252 bus = dbus.SystemBus()
253 manager = dbus.Interface(bus.get_object("net.connman", "/"),
254 "net.connman.Manager")
255 manager.SetProperty("SessionMode", True)
258 elif sys.argv[1] == "disable":
259 bus = dbus.SystemBus()
260 manager = dbus.Interface(bus.get_object("net.connman", "/"),
261 "net.connman.Manager")
262 manager.SetProperty("SessionMode", False)
265 if (len(sys.argv) < 3):
266 print "Need test application path"
269 app_path = sys.argv[2]
270 bus = dbus.SessionBus()
272 app_name = "com.example.SessionApplication.%s" % (string.strip(app_path, "/"))
274 if sys.argv[1] == "run":
275 name = dbus.service.BusName(app_name, bus)
276 mainloop = gobject.MainLoop()
278 app = SessionApplication(bus, app_path, mainloop)
283 app = dbus.Interface(bus.get_object(app_name, app_path),
284 "com.example.TestSession")
286 if sys.argv[1] == "create":
289 elif sys.argv[1] == "destroy":
292 elif sys.argv[1] == "connect":
295 elif sys.argv[1] == "disconnect":
298 elif sys.argv[1] == "change":
299 if len(sys.argv) < 5:
300 print "Arguments missing"
303 app.Change(sys.argv[3], sys.argv[4:])
305 elif sys.argv[1] == "configure":
306 if len(sys.argv) < 5:
307 print "Arguments missing"
310 app.Configure(sys.argv[3], sys.argv[4:])
313 print "Unknown command '%s'" % sys.argv[1]
316 if __name__ == '__main__':