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