Parse boolean flag from test-connman argv
[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 "  apn <service> [apn]"
17         print "  passphrase <service> [passphrase]"
18         print "  autoconnect <service> [autoconnect]"
19         print "  connect <service>"
20         print "  disconnect <service>"
21         print "  remove <service>"
22         print ""
23         print "  scan [type]"
24         print "  enable <type>"
25         print "  disable <type>"
26         print "  offlinemode [on|off]"
27         print ""
28         print "  dev <interface>"
29         print "  dev <interface> scan"
30         print "  dev <interface> scan_interval <period>"
31         print "  dev <interface> networks"
32         print "  dev <interface> connect <network>"
33         print "  dev <interface> disconnect [network]"
34         print "  dev <interface> powered [on|off]"
35         sys.exit(1)
36
37 def print_properties(path, properties):
38         print "[ %s ]" % (path)
39         for key in properties.keys():
40                 if key == "Networks":
41                         continue
42
43                 if key in ["Powered", "Scanning", "Connected",
44                                         "Available", "Remember", "Default"]:
45                         if properties[key] == dbus.Boolean(1):
46                                 val = "true"
47                         else:
48                                 val = "false"
49                 elif key in ["Strength", "Priority"]:
50                         val = int(properties[key])
51                 else:
52                         val = str(properties[key])
53
54                 print "     %s = %s" % (key, val)
55
56         if "Networks" in properties.keys():
57                 list = ""
58                 for path in properties["Networks"]:
59                         val = str(path)
60                         list = list + val[val.rfind("/") + 1:] + " "
61                 print "     Networks = [ %s]" % (list)
62
63 def print_networks(networks):
64         for path in networks:
65                 network = dbus.Interface(bus.get_object("org.moblin.connman", path),
66                                                 "org.moblin.connman.Network")
67
68                 properties = network.GetProperties()
69
70                 if properties["Connected"] == dbus.Boolean(1):
71                         connected = "*"
72                 else:
73                         connected = " "
74
75                 if "Name" in properties.keys():
76                         name = properties["Name"]
77                 else:
78                         name = ""
79
80                 strength = int(properties["Strength"])
81
82                 details = ""
83                 try:
84                         details += "{" + properties["WiFi.Mode"] + "} "
85                 except:
86                         pass
87                 try:
88                         details += "{" + properties["WiFi.Security"] + "} "
89                 except:
90                         pass
91                 if "WiFi.Passphrase" in properties.keys():
92                         if properties["WiFi.Passphrase"] != "":
93                                 details += "{passphrase present}"
94
95                 print "%s %-26s %3d%%  %s" % (connected,
96                                                 name, strength, details)
97
98 def select_network(networks, name):
99         for path in networks:
100                 network = dbus.Interface(bus.get_object("org.moblin.connman", path),
101                                                 "org.moblin.connman.Network")
102
103                 properties = network.GetProperties()
104
105                 if properties["Name"] != name:
106                         continue
107
108                 if properties["Connected"] == dbus.Boolean(1):
109                         print "Already connected to network %s" % (name)
110                         break
111
112                 print "Selecting network %s" % (name)
113
114                 network.Connect()
115
116 def disconnect_network(networks, name):
117         for path in networks:
118                 network = dbus.Interface(bus.get_object("org.moblin.connman", path),
119                                                 "org.moblin.connman.Network")
120
121                 properties = network.GetProperties()
122
123                 if name != "" and properties["Name"] != name:
124                         continue
125
126                 if properties["Connected"] == dbus.Boolean(1):
127                         name = properties["Name"]
128                         print "Disconnecting from network %s" % (name)
129                         network.Disconnect()
130
131 def print_services(services):
132         for path in services:
133                 service = dbus.Interface(bus.get_object("org.moblin.connman", path),
134                                                 "org.moblin.connman.Service")
135
136                 properties = service.GetProperties()
137
138                 identifier = path[path.rfind("/") + 1:]
139
140                 if properties["Favorite"] == dbus.Boolean(1):
141                         favorite = "*"
142                 else:
143                         favorite = " "
144
145                 if "Name" in properties.keys():
146                         name = properties["Name"]
147                 else:
148                         name = "{" + properties["Type"] + "}"
149
150                 print "%s %-26s { %s }" % (favorite, name, identifier)
151
152 def device_handle(devices, interface):
153         for path in devices:
154                 device = dbus.Interface(bus.get_object("org.moblin.connman", path),
155                                                 "org.moblin.connman.Device")
156
157                 properties = device.GetProperties()
158
159                 if "Interface" not in properties.keys():
160                         continue
161
162                 if interface != "" and properties["Interface"] != interface:
163                         continue
164
165                 if command == "scan":
166                         if properties["Type"] in ["wifi", "wimax"]:
167                                 interface = properties["Interface"]
168                                 print "Scan for device %s" % (interface)
169                                 device.ProposeScan()
170                         else:
171                                 print "No scanning for device %s" % (interface)
172                 elif command == "scan_interval" and value != "":
173                         device.SetProperty("ScanInterval", dbus.UInt16(value))
174                 elif command in ["networks", "net"]:
175                         if "Networks" in properties.keys():
176                                 print_networks(properties["Networks"])
177                         else:
178                                 print "Device has no networks"
179                 elif command in ["connect", "conn"] and value != "":
180                         if "Networks" in properties.keys():
181                                 select_network(properties["Networks"], value)
182                         else:
183                                 print "Device can't connect networks"
184                 elif command in ["connect", "conn"]:
185                         print "Need to specify network"
186                 elif command in ["disconnect", "disc"] and value != "":
187                         if "Networks" in properties.keys():
188                                 disconnect_network(properties["Networks"], value)
189                         else:
190                                 print "Device has no networks"
191                 elif command in ["discconnect", "disc"]:
192                         if "Networks" in properties.keys():
193                                 disconnect_network(properties["Networks"], "")
194                         else:
195                                 print "Device has no networks"
196                 elif command == "powered" and value != "":
197                         if value == "on":
198                                 powered = dbus.Boolean(1)
199                         elif value == "off":
200                                 powered = dbus.Boolean(0)
201                         else:
202                                 powered = dbus.Boolean(value)
203                         device.SetProperty("Powered", powered)
204                 elif command == "powered":
205                         interface = properties["Interface"]
206                         if properties["Powered"] == dbus.Boolean(1):
207                                 powered = "on"
208                         else:
209                                 powered = "off"
210                         print "Device %s is powered %s" % (interface, powered)
211                 elif command == "list" or command == "":
212                         print_properties(path, properties)
213                 else:
214                         print "Unknown command"
215
216
217
218 if sys.argv[1] == "state":
219         properties = manager.GetProperties()
220
221         print "System is %s" % (properties["State"])
222
223 elif sys.argv[1] in ["services", "list", "show"]:
224         properties = manager.GetProperties()
225
226         print_services(properties["Services"])
227
228 elif sys.argv[1] in ["apn"]:
229         if (len(sys.argv) < 3):
230                 print "Need at least service parameter"
231                 sys.exit(1)
232
233         path = "/profile/default/" + sys.argv[2]
234
235         service = dbus.Interface(bus.get_object("org.moblin.connman", path),
236                                                 "org.moblin.connman.Service")
237
238         if (len(sys.argv) > 3):
239                 apn = sys.argv[3]
240
241                 service.SetProperty("APN", apn);
242
243                 print "APN %s set for %s" % (apn, sys.argv[2])
244         else:
245                 properties = service.GetProperties()
246
247                 if "Name" in properties.keys():
248                         name = properties["Name"]
249                 else:
250                         name = "{" + properties["Type"] + "}"
251
252                 if "APN" in properties.keys():
253                         apn = properties["APN"]
254                 else:
255                         apn = "not set"
256
257                 print "APN for %s is %s" % (name, apn)
258
259 elif sys.argv[1] in ["passphrase", "pass"]:
260         if (len(sys.argv) < 3):
261                 print "Need at least service parameter"
262                 sys.exit(1)
263
264         path = "/profile/default/" + sys.argv[2]
265
266         service = dbus.Interface(bus.get_object("org.moblin.connman", path),
267                                                 "org.moblin.connman.Service")
268
269         if (len(sys.argv) > 3):
270                 passphrase = sys.argv[3]
271
272                 service.SetProperty("Passphrase", passphrase);
273
274                 print "Passphrase %s set for %s" % (passphrase, sys.argv[2])
275         else:
276                 properties = service.GetProperties()
277
278                 if "Name" in properties.keys():
279                         name = properties["Name"]
280                 else:
281                         name = "{" + properties["Type"] + "}"
282
283                 if "Passphrase" in properties.keys():
284                         passphrase = properties["Passphrase"]
285                 else:
286                         passphrase = "not set"
287
288                 print "Passphrase for %s is %s" % (name, passphrase)
289
290 elif sys.argv[1] in ["autoconnect", "autoconn"]:
291         if (len(sys.argv) < 3):
292                 print "Need at least service parameter"
293                 sys.exit(1)
294
295         path = "/profile/default/" + sys.argv[2]
296
297         service = dbus.Interface(bus.get_object("org.moblin.connman", path),
298                                                 "org.moblin.connman.Service")
299
300         if (len(sys.argv) > 3):
301                 flag = sys.argv[3].strip().lower()
302                 autoconnect = dbus.Boolean(flag not in ['false', 'f', 'n', '0'])
303
304                 service.SetProperty("AutoConnect", autoconnect);
305
306                 print "Auto connect %s for %s" % (autoconnect, sys.argv[2])
307         else:
308                 properties = service.GetProperties()
309
310                 if "Name" in properties.keys():
311                         name = properties["Name"]
312                 else:
313                         name = "{" + properties["Type"] + "}"
314
315                 if "AutoConnect" in properties.keys():
316                         autoconnect = properties["AutoConnect"]
317                 else:
318                         autoconnect = dbus.Boolean(0)
319
320                 print "Auto connect %s for %s" % (autoconnect, name)
321
322 elif sys.argv[1] in ["connect", "conn"]:
323         if (len(sys.argv) < 3):
324                 print "Need at least service parameter"
325                 sys.exit(1)
326
327         path = "/profile/default/" + sys.argv[2]
328
329         service = dbus.Interface(bus.get_object("org.moblin.connman", path),
330                                                 "org.moblin.connman.Service")
331
332         try:
333                 service.Connect(timeout=60000)
334         except dbus.DBusException, error:
335                 print "%s: %s" % (error._dbus_error_name, error.message)
336
337 elif sys.argv[1] in ["disconnect", "disc"]:
338         if (len(sys.argv) < 3):
339                 print "Need at least service parameter"
340                 sys.exit(1)
341
342         path = "/profile/default/" + sys.argv[2]
343
344         service = dbus.Interface(bus.get_object("org.moblin.connman", path),
345                                                 "org.moblin.connman.Service")
346
347         try:
348                 service.Disconnect()
349         except dbus.DBusException, error:
350                 print "%s: %s" % (error._dbus_error_name, error.message)
351
352 elif sys.argv[1] in ["remove"]:
353         if (len(sys.argv) < 3):
354                 print "Need at least service parameter"
355                 sys.exit(1)
356
357         path = "/profile/default/" + sys.argv[2]
358
359         service = dbus.Interface(bus.get_object("org.moblin.connman", path),
360                                                 "org.moblin.connman.Service")
361
362         properties = service.GetProperties()
363
364         if properties["Favorite"] == dbus.Boolean(0):
365                 print "Only favorite services can be removed"
366                 sys.exit(1)
367
368         try:
369                 service.Remove()
370         except dbus.DBusException, error:
371                 print "%s: %s" % (error._dbus_error_name, error.message)
372
373 elif sys.argv[1] == "scan":
374         if len(sys.argv) > 2:
375                 manager.RequestScan(sys.argv[2])
376         else:
377                 manager.RequestScan("")
378
379 elif sys.argv[1] == "enable":
380         if len(sys.argv) > 2:
381                 manager.EnableTechnology(sys.argv[2])
382         else:
383                 manager.EnableTechnology("")
384
385 elif sys.argv[1] == "disable":
386         if len(sys.argv) > 2:
387                 manager.DisableTechnology(sys.argv[2])
388         else:
389                 manager.DisableTechnology("")
390
391 elif sys.argv[1] in ["offlinemode", "flightmode"]:
392         if len(sys.argv) > 2:
393                 if sys.argv[2] == "on":
394                         active = dbus.Boolean(1)
395                 elif sys.argv[2] == "off":
396                         active = dbus.Boolean(0)
397                 else:
398                         active = dbus.Boolean(sys.argv[2])
399                 manager.SetProperty("OfflineMode", active)
400         else:
401                 properties = manager.GetProperties()
402                 print "Offline mode is %s" % (properties["OfflineMode"])
403
404 elif sys.argv[1] == "dev":
405         properties = manager.GetProperties()
406
407         interface = ""
408         command = ""
409         value = ""
410         found = 0
411
412         if len(sys.argv) > 2:
413                 interface = sys.argv[2]
414                 if len(sys.argv) > 3:
415                         command = sys.argv[3]
416                         if len(sys.argv) > 4:
417                                 value = sys.argv[4]
418
419         for path in properties["Technologies"]:
420                 technology = dbus.Interface(bus.get_object("org.moblin.connman", path),
421                                                         "org.moblin.connman.Technology")
422
423                 properties = technology.GetProperties()
424
425                 device_handle(properties["Devices"], interface)
426
427 else:
428         print "Unknown command"