summaryrefslogtreecommitdiff
path: root/test/test-connman
blob: c180282990ed6a5a67c9b4d3409b0860915069d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/python

import sys
import dbus

bus = dbus.SystemBus()

manager = dbus.Interface(bus.get_object("org.moblin.connman", "/"),
					"org.moblin.connman.Manager")

if len(sys.argv) < 2:
	print "Usage: %s <command>" % (sys.argv[0])
	print ""
	print "  state"
	print "  services"
	print "  passphrase <service> [passphrase]"
	print "  connect <service>"
	print "  disconnect <service>"
	print "  remove <service>"
	print ""
	print "  scan [ <interface> ]"
	print "  dev <interface>"
	print "  dev <interface> scan"
	print "  dev <interface> networks"
	print "  dev <interface> connect <network>"
	print "  dev <interface> remember <network>"
	print "  dev <interface> disconnect [network]"
	print "  dev <interface> policy [ignore|off|auto|manual]"
	print "  dev <interface> powered [on|off]"
	print "  dev <interface> priority [0-255]"
	sys.exit(1)

def print_properties(path, properties):
	print "[ %s ]" % (path)
	for key in properties.keys():
		if key == "Networks":
			continue

		if key in ["Powered", "Scanning", "Connected",
					"Available", "Remember", "Default"]:
			if properties[key] == dbus.Boolean(1):
				val = "true"
			else:
				val = "false"
		elif key in ["Strength", "Priority"]:
			val = int(properties[key])
		else:
			val = str(properties[key])

		print "     %s = %s" % (key, val)

	if "Networks" in properties.keys():
		list = ""
		for path in properties["Networks"]:
			val = str(path)
			list = list + val[val.rfind("/") + 1:] + " "
		print "     Networks = [ %s]" % (list)

def print_networks(networks):
	for path in networks:
		network = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Network")

		properties = network.GetProperties()

		if properties["Connected"] == dbus.Boolean(1):
			connected = "*"
		else:
			connected = " "

		if "Name" in properties.keys():
			name = properties["Name"]
		else:
			name = ""

		strength = int(properties["Strength"])

		details = ""
		try:
			details += "{" + properties["WiFi.Mode"] + "} "
		except:
			pass
		try:
			details += "{" + properties["WiFi.Security"] + "} "
		except:
			pass
		if "WiFi.Passphrase" in properties.keys():
			if properties["WiFi.Passphrase"] != "":
				details += "{passphrase present}"

		print "%s %-26s %3d%%  %s" % (connected,
						name, strength, details)

def select_network(networks, name):
	for path in networks:
		network = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Network")

		properties = network.GetProperties()

		if properties["Name"] != name:
			continue

		if properties["Connected"] == dbus.Boolean(1):
			print "Already connected to network %s" % (name)
			break

		print "Selecting network %s" % (name)

		network.Connect()

def remember_network(networks, name):
	for path in networks:
		network = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Network")

		properties = network.GetProperties()

		if properties["Name"] != name:
			continue

		if properties["Remember"] == dbus.Boolean(1):
			print "Already a known network %s" % (name)
			break

		print "Remembering network %s" % (name)

		network.SetProperty("Remember", dbus.Boolean(1))

def disconnect_network(networks, name):
	for path in networks:
		network = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Network")

		properties = network.GetProperties()

		if name != "" and properties["Name"] != name:
			continue

		if properties["Connected"] == dbus.Boolean(1):
			name = properties["Name"]
			print "Disconnecting from network %s" % (name)
			network.Disconnect()

def print_services(services):
	for path in services:
		service = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Service")

		properties = service.GetProperties()

		identifier = path[path.rfind("/") + 1:]

		if properties["Favorite"] == dbus.Boolean(1):
			favorite = "*"
		else:
			favorite = " "

		if "Name" in properties.keys():
			name = properties["Name"]
		else:
			name = "{" + properties["Type"] + "}"

		print "%s %-26s { %s }" % (favorite, name, identifier)

if sys.argv[1] == "state":
	properties = manager.GetProperties()

	print "System is %s" % (properties["State"])

elif sys.argv[1] in ["services", "list", "show"]:
	properties = manager.GetProperties()

	print_services(properties["Services"])

elif sys.argv[1] in ["passphrase", "pass"]:
	if (len(sys.argv) < 3):
		print "Need at least service parameter"
		sys.exit(1)

	path = "/profile/default/" + sys.argv[2]

	service = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Service")

	if (len(sys.argv) > 3):
		passphrase = sys.argv[3]

		service.SetProperty("Passphrase", passphrase);

		print "Passphrase %s set for %s" % (passphrase, sys.argv[2])
	else:
		properties = service.GetProperties()

		if "Name" in properties.keys():
			name = properties["Name"]
		else:
			name = "{" + properties["Type"] + "}"

		if "Passphrase" in properties.keys():
			passphrase = properties["Passphrase"]
		else:
			passphrase = "not set"

		print "Passphrase for %s is %s" % (name, passphrase)

elif sys.argv[1] in ["connect", "conn"]:
	if (len(sys.argv) < 3):
		print "Need at least service parameter"
		sys.exit(1)

	path = "/profile/default/" + sys.argv[2]

	service = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Service")

	service.Connect()

elif sys.argv[1] in ["disconnect", "disc"]:
	if (len(sys.argv) < 3):
		print "Need at least service parameter"
		sys.exit(1)

	path = "/profile/default/" + sys.argv[2]

	service = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Service")

	service.Disconnect()

elif sys.argv[1] in ["remove"]:
	if (len(sys.argv) < 3):
		print "Need at least service parameter"
		sys.exit(1)

	path = "/profile/default/" + sys.argv[2]

	service = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Service")

	properties = service.GetProperties()

	if properties["Favorite"] == dbus.Boolean(0):
		print "Only favorite services can be removed"
		sys.exit(1)

	service.Remove()

elif sys.argv[1] == "scan":
	properties = manager.GetProperties()

	interface = ""
	found = 0

	if len(sys.argv) > 2:
		interface = sys.argv[2]

	for path in properties["Devices"]:
		device = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Device")

		properties = device.GetProperties()

		if interface != "" and properties["Interface"] != interface:
			continue

		if properties["Type"] in ["wifi", "wimax"]:
			interface = properties["Interface"]
			print "Propose scanning for device %s" % (interface)
			device.ProposeScan()
			found = 1
		elif interface != "":
			print "No scanning support for device %s" % (interface)
			found = 1

	if found == 0:
		print "No such device"

elif sys.argv[1] == "dev":
	properties = manager.GetProperties()

	interface = ""
	command = ""
	value = ""
	found = 0

	if len(sys.argv) > 2:
		interface = sys.argv[2]
		if len(sys.argv) > 3:
			command = sys.argv[3]
			if len(sys.argv) > 4:
				value = sys.argv[4]

	for path in properties["Devices"]:
		device = dbus.Interface(bus.get_object("org.moblin.connman", path),
						"org.moblin.connman.Device")

		properties = device.GetProperties()

		if interface != "" and properties["Interface"] != interface:
			continue

		if command == "scan":
			if properties["Type"] in ["wifi", "wimax"]:
				interface = properties["Interface"]
				print "Scan for device %s" % (interface)
				device.ProposeScan()
			else:
				print "No scanning for device %s" % (interface)
		elif command in ["networks", "net"]:
			if "Networks" in properties.keys():
				print_networks(properties["Networks"])
			else:
				print "Device has no networks"
		elif command in ["connect", "conn"] and value != "":
			if "Networks" in properties.keys():
				select_network(properties["Networks"], value)
			else:
				print "Device can't connect networks"
		elif command in ["connect", "conn"]:
			print "Need to specify network"
		elif command in ["remember", "known"] and value != "":
			if "Networks" in properties.keys():
				remember_network(properties["Networks"], value)
			else:
				print "Device has no networks"
		elif command in ["remember", "known"]:
			print "Need to specify network"
		elif command in ["disconnect", "disc"] and value != "":
			if "Networks" in properties.keys():
				disconnect_network(properties["Networks"], value)
			else:
				print "Device has no networks"
		elif command in ["discconnect", "disc"]:
			if "Networks" in properties.keys():
				disconnect_network(properties["Networks"], "")
			else:
				print "Device has no networks"
		elif command == "policy" and value != "":
			policy = value
			device.SetProperty("Policy", policy)
		elif command == "policy":
			interface = properties["Interface"]
			policy = properties["Policy"]
			print "Policy of device %s is %s" % (interface, policy)
		elif command == "powered" and value != "":
			if value == "on":
				powered = dbus.Boolean(1)
			elif value == "off":
				powered = dbus.Boolean(0)
			else:
				powered = dbus.Boolean(value)
			device.SetProperty("Powered", powered)
		elif command == "powered":
			interface = properties["Interface"]
			if properties["Powered"] == dbus.Boolean(1):
				powered = "on"
			else:
				powered = "off"
			print "Device %s is powered %s" % (interface, powered)
		elif command == "priority" and value != "":
			priority = int(value)
			device.SetProperty("Priority", priority)
		elif command == "priority":
			interface = properties["Interface"]
			priority = properties["Priority"]
			print "Device %s has priority of %d" % (interface, priority)
		elif command == "list" or command == "":
			print_properties(path, properties)
		else:
			print "Unknown command"

else:
	print "Unknown command"