mirror of
https://git.kernel.org/pub/scm/bluetooth/bluez.git
synced 2025-01-26 06:13:30 +08:00
test: Update test-discovery to match new API
This commit is contained in:
parent
b4f4f56f5e
commit
3b025cb748
@ -11,7 +11,7 @@ from optparse import OptionParser, make_option
|
||||
compact = False
|
||||
devices = {}
|
||||
|
||||
def print_compact(address, properties):
|
||||
def print_compact(path, address, properties):
|
||||
name = ""
|
||||
address = "<unknown>"
|
||||
|
||||
@ -23,16 +23,14 @@ def print_compact(address, properties):
|
||||
elif (key == "Address"):
|
||||
address = value
|
||||
|
||||
if "Logged" in properties:
|
||||
if path in devices:
|
||||
flag = "*"
|
||||
else:
|
||||
flag = " "
|
||||
|
||||
print("%s%s %s" % (flag, address, name))
|
||||
|
||||
properties["Logged"] = True
|
||||
|
||||
def print_normal(address, properties):
|
||||
def print_normal(path, address, properties):
|
||||
print("[ " + address + " ]")
|
||||
|
||||
for key in properties.keys():
|
||||
@ -46,64 +44,42 @@ def print_normal(address, properties):
|
||||
|
||||
print()
|
||||
|
||||
properties["Logged"] = True
|
||||
def skip_dev(path, new_dev):
|
||||
if not compact:
|
||||
return False;
|
||||
|
||||
def skip_dev(old_dev, new_dev):
|
||||
if not "Logged" in old_dev:
|
||||
if not path in devices:
|
||||
return False
|
||||
|
||||
old_dev = devices[path]
|
||||
|
||||
if "Name" in old_dev:
|
||||
return True
|
||||
|
||||
if not "Name" in new_dev:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def interfaces_added(path, interfaces):
|
||||
properties = interfaces["org.bluez.Device"]
|
||||
if not properties:
|
||||
return
|
||||
|
||||
if path in devices:
|
||||
dev = devices[path]
|
||||
|
||||
if compact and skip_dev(dev, properties):
|
||||
return
|
||||
devices[path] = dict(devices[path].items() + properties.items())
|
||||
def print_dev(path, props):
|
||||
if "Address" in props:
|
||||
address = props["Address"]
|
||||
else:
|
||||
address = "<unknown>"
|
||||
|
||||
if compact:
|
||||
print_compact(path, address, props)
|
||||
else:
|
||||
print_normal(path, address, props)
|
||||
|
||||
def devices_found(found_devs):
|
||||
for path, properties in found_devs.iteritems():
|
||||
if (skip_dev(path, properties)):
|
||||
continue
|
||||
|
||||
print_dev(path, properties)
|
||||
devices[path] = properties
|
||||
|
||||
if "Address" in devices[path]:
|
||||
address = properties["Address"]
|
||||
else:
|
||||
address = "<unknown>"
|
||||
|
||||
if compact:
|
||||
print_compact(address, devices[path])
|
||||
else:
|
||||
print_normal(address, devices[path])
|
||||
|
||||
def properties_changed(interface, changed, invalidated, path):
|
||||
if interface != "org.bluez.Device":
|
||||
return
|
||||
|
||||
if path in devices:
|
||||
dev = devices[path]
|
||||
|
||||
if compact and skip_dev(dev, changed):
|
||||
return
|
||||
devices[path] = dict(devices[path].items() + changed.items())
|
||||
else:
|
||||
devices[path] = changed
|
||||
|
||||
if "Address" in devices[path]:
|
||||
address = devices[path]["Address"]
|
||||
else:
|
||||
address = "<unknown>"
|
||||
|
||||
if compact:
|
||||
print_compact(address, devices[path])
|
||||
else:
|
||||
print_normal(address, devices[path])
|
||||
|
||||
def property_changed(name, value):
|
||||
if (name == "Discovering" and not value):
|
||||
mainloop.quit()
|
||||
@ -136,27 +112,14 @@ if __name__ == '__main__':
|
||||
adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
|
||||
"org.bluez.Adapter")
|
||||
|
||||
bus.add_signal_receiver(interfaces_added,
|
||||
dbus_interface = "org.freedesktop.DBus.ObjectManager",
|
||||
signal_name = "InterfacesAdded")
|
||||
|
||||
bus.add_signal_receiver(properties_changed,
|
||||
dbus_interface = "org.freedesktop.DBus.Properties",
|
||||
signal_name = "PropertiesChanged",
|
||||
arg0 = "org.bluez.Device",
|
||||
path_keyword = "path")
|
||||
bus.add_signal_receiver(devices_found,
|
||||
dbus_interface = "org.bluez.Adapter",
|
||||
signal_name = "DevicesFound")
|
||||
|
||||
bus.add_signal_receiver(property_changed,
|
||||
dbus_interface = "org.bluez.Adapter",
|
||||
signal_name = "PropertyChanged")
|
||||
|
||||
om = dbus.Interface(bus.get_object("org.bluez", "/"),
|
||||
"org.freedesktop.DBus.ObjectManager")
|
||||
objects = om.GetManagedObjects()
|
||||
for path, interfaces in objects.iteritems():
|
||||
if "org.bluez.Device" in interfaces:
|
||||
devices[path] = interfaces["org.bluez.Device"]
|
||||
|
||||
adapter.StartDiscovery()
|
||||
|
||||
mainloop = GObject.MainLoop()
|
||||
|
Loading…
Reference in New Issue
Block a user