Add option for offline mode to test script
[framework/connectivity/connman.git] / test / test-connman
1 #!/usr/bin/python
2
3 import sys
4 import dbus
5
6 bus = dbus.SystemBus()
7
8 manager = dbus.Interface(bus.get_object("org.moblin.connman", "/"),
9                                         "org.moblin.connman.Manager")
10
11 if len(sys.argv) < 2:
12         print "Usage: %s <command>" % (sys.argv[0])
13         print ""
14         print "  state"
15         print "  services"
16         print "  passphrase <service> [passphrase]"
17         print "  connect <service>"
18         print "  disconnect <service>"
19         print "  remove <service>"
20         print ""
21         print "  scan [type]"
22         print "  enable <type>"
23         print "  disable <type>"
24         print "  offlinemode [on|off]"
25         print ""
26         print "  dev <interface>"
27         print "  dev <interface> scan"
28         print "  dev <interface> networks"
29         print "  dev <interface> connect <network>"
30         print "  dev <interface> disconnect [network]"
31         print "  dev <interface> powered [on|off]"
32         sys.exit(1)
33
34 def print_properties(path, properties):
35         print "[ %s ]" % (path)
36         for key in properties.keys():
37                 if key == "Networks":
38                         continue
39
40                 if key in ["Powered", "Scanning", "Connected",
41                                         "Available", "Remember", "Default"]:
42                         if properties[key] == dbus.Boolean(1):
43                                 val = "true"
44                         else:
45                                 val = "false"
46                 elif key in ["Strength", "Priority"]:
47                         val = int(properties[key])
48                 else:
49                         val = str(properties[key])
50
51                 print "     %s = %s" % (key, val)
52
53         if "Networks" in properties.keys():
54                 list = ""
55                 for path in properties["Networks"]:
56                         val = str(path)
57                         list = list + val[val.rfind("/") + 1:] + " "
58                 print "     Networks = [ %s]" % (list)
59
60 def print_networks(networks):
61         for path in networks:
62                 network = dbus.Interface(bus.get_object("org.moblin.connman", path),
63                                                 "org.moblin.connman.Network")
64
65                 properties = network.GetProperties()
66
67                 if properties["Connected"] == dbus.Boolean(1):
68                         connected = "*"
69                 else:
70                         connected = " "
71
72                 if "Name" in properties.keys():
73                         name = properties["Name"]
74                 else:
75                         name = ""
76
77                 strength = int(properties["Strength"])
78
79                 details = ""
80                 try:
81                         details += "{" + properties["WiFi.Mode"] + "} "
82                 except:
83                         pass
84                 try:
85                         details += "{" + properties["WiFi.Security"] + "} "
86                 except:
87                         pass
88                 if "WiFi.Passphrase" in properties.keys():
89                         if properties["WiFi.Passphrase"] != "":
90                                 details += "{passphrase present}"
91
92                 print "%s %-26s %3d%%  %s" % (connected,
93                                                 name, strength, details)
94
95 def select_network(networks, name):
96         for path in networks:
97                 network = dbus.Interface(bus.get_object("org.moblin.connman", path),
98                                                 "org.moblin.connman.Network")
99
100                 properties = network.GetProperties()
101
102                 if properties["Name"] != name:
103                         continue
104
105                 if properties["Connected"] == dbus.Boolean(1):
106                         print "Already connected to network %s" % (name)
107                         break
108
109                 print "Selecting network %s" % (name)
110
111                 network.Connect()
112
113 def disconnect_network(networks, name):
114         for path in networks:
115                 network = dbus.Interface(bus.get_object("org.moblin.connman", path),
116                                                 "org.moblin.connman.Network")
117
118                 properties = network.GetProperties()
119
120                 if name != "" and properties["Name"] != name:
121                         continue
122
123                 if properties["Connected"] == dbus.Boolean(1):
124                         name = properties["Name"]
125                         print "Disconnecting from network %s" % (name)
126                         network.Disconnect()
127
128 def print_services(services):
129         for path in services:
130                 service = dbus.Interface(bus.get_object("org.moblin.connman", path),
131                                                 "org.moblin.connman.Service")
132
133                 properties = service.GetProperties()
134
135                 identifier = path[path.rfind("/") + 1:]
136
137                 if properties["Favorite"] == dbus.Boolean(1):
138                         favorite = "*"
139                 else:
140                         favorite = " "
141
142                 if "Name" in properties.keys():
143                         name = properties["Name"]
144                 else:
145                         name = "{" + properties["Type"] + "}"
146
147                 print "%s %-26s { %s }" % (favorite, name, identifier)
148
149 if sys.argv[1] == "state":
150         properties = manager.GetProperties()
151
152         print "System is %s" % (properties["State"])
153
154 elif sys.argv[1] in ["services", "list", "show"]:
155         properties = manager.GetProperties()
156
157         print_services(properties["Services"])
158
159 elif sys.argv[1] in ["passphrase", "pass"]:
160         if (len(sys.argv) < 3):
161                 print "Need at least service parameter"
162                 sys.exit(1)
163
164         path = "/profile/default/" + sys.argv[2]
165
166         service = dbus.Interface(bus.get_object("org.moblin.connman", path),
167                                                 "org.moblin.connman.Service")
168
169         if (len(sys.argv) > 3):
170                 passphrase = sys.argv[3]
171
172                 service.SetProperty("Passphrase", passphrase);
173
174                 print "Passphrase %s set for %s" % (passphrase, sys.argv[2])
175         else:
176                 properties = service.GetProperties()
177
178                 if "Name" in properties.keys():
179                         name = properties["Name"]
180                 else:
181                         name = "{" + properties["Type"] + "}"
182
183                 if "Passphrase" in properties.keys():
184                         passphrase = properties["Passphrase"]
185                 else:
186                         passphrase = "not set"
187
188                 print "Passphrase for %s is %s" % (name, passphrase)
189
190 elif sys.argv[1] in ["connect", "conn"]:
191         if (len(sys.argv) < 3):
192                 print "Need at least service parameter"
193                 sys.exit(1)
194
195         path = "/profile/default/" + sys.argv[2]
196
197         service = dbus.Interface(bus.get_object("org.moblin.connman", path),
198                                                 "org.moblin.connman.Service")
199
200         try:
201                 service.Connect(timeout=60000)
202         except dbus.DBusException, error:
203                 print "%s: %s" % (error._dbus_error_name, error.message)
204
205 elif sys.argv[1] in ["disconnect", "disc"]:
206         if (len(sys.argv) < 3):
207                 print "Need at least service parameter"
208                 sys.exit(1)
209
210         path = "/profile/default/" + sys.argv[2]
211
212         service = dbus.Interface(bus.get_object("org.moblin.connman", path),
213                                                 "org.moblin.connman.Service")
214
215         try:
216                 service.Disconnect()
217         except dbus.DBusException, error:
218                 print "%s: %s" % (error._dbus_error_name, error.message)
219
220 elif sys.argv[1] in ["remove"]:
221         if (len(sys.argv) < 3):
222                 print "Need at least service parameter"
223                 sys.exit(1)
224
225         path = "/profile/default/" + sys.argv[2]
226
227         service = dbus.Interface(bus.get_object("org.moblin.connman", path),
228                                                 "org.moblin.connman.Service")
229
230         properties = service.GetProperties()
231
232         if properties["Favorite"] == dbus.Boolean(0):
233                 print "Only favorite services can be removed"
234                 sys.exit(1)
235
236         try:
237                 service.Remove()
238         except dbus.DBusException, error:
239                 print "%s: %s" % (error._dbus_error_name, error.message)
240
241 elif sys.argv[1] == "scan":
242         if len(sys.argv) > 2:
243                 manager.RequestScan(sys.argv[2])
244         else:
245                 manager.RequestScan("")
246
247 elif sys.argv[1] == "enable":
248         if len(sys.argv) > 2:
249                 manager.EnableTechnology(sys.argv[2])
250         else:
251                 manager.EnableTechnology("")
252
253 elif sys.argv[1] == "disable":
254         if len(sys.argv) > 2:
255                 manager.DisableTechnology(sys.argv[2])
256         else:
257                 manager.DisableTechnology("")
258
259 elif sys.argv[1] in ["offlinemode", "flightmode"]:
260         if len(sys.argv) > 2:
261                 if sys.argv[2] == "on":
262                         active = dbus.Boolean(1)
263                 elif sys.argv[2] == "off":
264                         active = dbus.Boolean(0)
265                 else:
266                         active = dbus.Boolean(sys.argv[2])
267                 manager.SetProperty("OfflineMode", active)
268         else:
269                 properties = manager.GetProperties()
270                 print "Offline mode is %s" % (properties["OfflineMode"])
271
272 elif sys.argv[1] == "dev":
273         properties = manager.GetProperties()
274
275         interface = ""
276         command = ""
277         value = ""
278         found = 0
279
280         if len(sys.argv) > 2:
281                 interface = sys.argv[2]
282                 if len(sys.argv) > 3:
283                         command = sys.argv[3]
284                         if len(sys.argv) > 4:
285                                 value = sys.argv[4]
286
287         for path in properties["Devices"]:
288                 device = dbus.Interface(bus.get_object("org.moblin.connman", path),
289                                                 "org.moblin.connman.Device")
290
291                 properties = device.GetProperties()
292
293                 if interface != "" and properties["Interface"] != interface:
294                         continue
295
296                 if command == "scan":
297                         if properties["Type"] in ["wifi", "wimax"]:
298                                 interface = properties["Interface"]
299                                 print "Scan for device %s" % (interface)
300                                 device.ProposeScan()
301                         else:
302                                 print "No scanning for device %s" % (interface)
303                 elif command in ["networks", "net"]:
304                         if "Networks" in properties.keys():
305                                 print_networks(properties["Networks"])
306                         else:
307                                 print "Device has no networks"
308                 elif command in ["connect", "conn"] and value != "":
309                         if "Networks" in properties.keys():
310                                 select_network(properties["Networks"], value)
311                         else:
312                                 print "Device can't connect networks"
313                 elif command in ["connect", "conn"]:
314                         print "Need to specify network"
315                 elif command in ["disconnect", "disc"] and value != "":
316                         if "Networks" in properties.keys():
317                                 disconnect_network(properties["Networks"], value)
318                         else:
319                                 print "Device has no networks"
320                 elif command in ["discconnect", "disc"]:
321                         if "Networks" in properties.keys():
322                                 disconnect_network(properties["Networks"], "")
323                         else:
324                                 print "Device has no networks"
325                 elif command == "powered" and value != "":
326                         if value == "on":
327                                 powered = dbus.Boolean(1)
328                         elif value == "off":
329                                 powered = dbus.Boolean(0)
330                         else:
331                                 powered = dbus.Boolean(value)
332                         device.SetProperty("Powered", powered)
333                 elif command == "powered":
334                         interface = properties["Interface"]
335                         if properties["Powered"] == dbus.Boolean(1):
336                                 powered = "on"
337                         else:
338                                 powered = "off"
339                         print "Device %s is powered %s" % (interface, powered)
340                 elif command == "list" or command == "":
341                         print_properties(path, properties)
342                 else:
343                         print "Unknown command"
344
345 else:
346         print "Unknown command"