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
76 bus = dbus.SystemBus()
77 bus.watch_name_owner('net.connman', self.connman_name_owner_changed)
78 except dbus.DBusException:
82 def connman_name_owner_changed(self, proxy):
85 print "connman appeared on D-Bus ", str(proxy)
87 bus = dbus.SystemBus()
88 self.manager = dbus.Interface(bus.get_object("net.connman", "/"),
89 "net.connman.Manager")
91 print "connman disappeared on D-Bus"
95 self.notify.remove_from_connection()
98 except dbus.DBusException:
104 self.manager.DestroySession(self.session_path)
107 self.notify.remove_from_connection()
110 @dbus.service.method("com.example.TestSession",
111 in_signature='', out_signature='')
112 def CreateSession(self):
113 print "Create session"
116 print "already running a session -> drop reqest"
120 bus = dbus.SystemBus()
122 self.notify = Notification(bus, self, self.notify_path)
123 self.notify.add_to_connection(bus, self.notify_path)
125 #settings = { "AllowedBearers" : [ "ethernet", "*" ] }
126 settings = { "AllowedBearers" : [ "ethernet" ] }
127 self.session_path = self.manager.CreateSession(settings, self.notify_path)
129 print "notify path %s" % (self.notify_path)
130 print "session path %s" % (self.session_path)
132 self.session = dbus.Interface(bus.get_object("net.connman", self.session_path),
133 "net.connman.Session")
135 except dbus.DBusException:
136 traceback.print_exc()
139 @dbus.service.method("com.example.TestSession",
140 in_signature='', out_signature='')
141 def DestroySession(self):
142 print "Destroy session"
144 if self.session == None:
145 print "no session running -> drop request"
150 except dbus.DBusException:
151 traceback.print_exc()
154 @dbus.service.method("com.example.TestSession",
155 in_signature='', out_signature='')
157 print "Connect session"
159 self.session.Connect()
160 except dbus.DBusException, e:
161 if e.get_dbus_name() in ['net.connman.Error.InProgress',
162 'net.connman.Error.AlreadyConnected']:
163 print e.get_dbus_message()
165 traceback.print_exc()
168 @dbus.service.method("com.example.TestSession",
169 in_signature='', out_signature='')
170 def Disconnect(self):
171 print "Disconnect session"
173 self.session.Disconnect()
174 except dbus.DBusException, e:
175 if e.get_dbus_name() in ['net.connman.Error.AlreadyDisabled',
176 'net.connman.Error.NotConnected']:
177 print e.get_dbus_message()
179 traceback.print_exc()
183 if len(sys.argv) < 2:
184 print "Usage: %s <command>" % (sys.argv[0])
188 print " create <app_path>"
189 print " destroy <app_path>"
190 print " connect <app_path>"
191 print " disconnect <app_path>"
193 print " run <app_path>"
196 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
198 if sys.argv[1] == "enable":
199 bus = dbus.SystemBus()
200 manager = dbus.Interface(bus.get_object("net.connman", "/"),
201 "net.connman.Manager")
202 manager.SetProperty("SessionMode", True)
205 elif sys.argv[1] == "disable":
206 bus = dbus.SystemBus()
207 manager = dbus.Interface(bus.get_object("net.connman", "/"),
208 "net.connman.Manager")
209 manager.SetProperty("SessionMode", False)
212 if (len(sys.argv) < 3):
213 print "Need test application path"
216 app_path = sys.argv[2]
217 bus = dbus.SessionBus()
219 if sys.argv[1] == "run":
220 name = dbus.service.BusName("com.example.SessionApplication", bus)
221 mainloop = gobject.MainLoop()
223 app = SessionApplication(bus, app_path, mainloop)
228 app = dbus.Interface(bus.get_object("com.example.SessionApplication", app_path),
229 "com.example.TestSession")
231 if sys.argv[1] == "create":
234 elif sys.argv[1] == "destroy":
237 elif sys.argv[1] == "connect":
240 elif sys.argv[1] == "disconnect":
243 if __name__ == '__main__':