summaryrefslogtreecommitdiff
path: root/test/neardutils.py
blob: a7b2946072cb991a92dd18fd82f8ccc162f8f7e7 (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
import dbus

SERVICE_NAME = "org.neard"
ADAPTER_INTERFACE = SERVICE_NAME + ".Adapter"
DEVICE_INTERFACE = SERVICE_NAME + ".Device"
RECORD_INTERFACE = SERVICE_NAME + ".Record"

def get_managed_objects():
	bus = dbus.SystemBus()
	manager = dbus.Interface(bus.get_object("org.neard", "/"),
				"org.freedesktop.DBus.ObjectManager")
	return manager.GetManagedObjects()

def find_adapter(pattern=None):
	return find_adapter_in_objects(get_managed_objects(), pattern)

def find_adapter_in_objects(objects, pattern=None):
	bus = dbus.SystemBus()
	for path, ifaces in objects.iteritems():
		adapter = ifaces.get(ADAPTER_INTERFACE)
		if adapter is None:
			continue
		if not pattern or path.endswith(pattern):
			obj = bus.get_object(SERVICE_NAME, path)
			return dbus.Interface(obj, ADAPTER_INTERFACE)
	raise Exception("NFC adapter not found")

def find_device(pattern=None):
	return find_device_in_objects(get_managed_objects(), pattern)

def find_device_in_objects(objects, pattern=None):
	bus = dbus.SystemBus()
	for path, ifaces in objects.iteritems():
		device = ifaces.get(DEVICE_INTERFACE)
		if device is None:
			continue
		if not pattern or path.endswith(pattern):
			obj = bus.get_object(SERVICE_NAME, path)
			return dbus.Interface(obj, DEVICE_INTERFACE)
	raise Exception("NFC adapter not found")

def find_record(pattern=None):
	return find_record_in_objects(get_managed_objects(), pattern)

def find_record_in_objects(objects, pattern=None):
	bus = dbus.SystemBus()
	for path, ifaces in objects.iteritems():
		record = ifaces.get(RECORD_INTERFACE)
		if record is None:
			continue
		if not pattern or path.endswith(pattern):
			obj = bus.get_object(SERVICE_NAME, path)
			return dbus.Interface(obj, RECORD_INTERFACE)
	raise Exception("NFC adapter not found")