~bluetooth/bluez/vivid-phone-overlay

« back to all changes in this revision

Viewing changes to test/test-attrib

  • Committer: Simon Fels
  • Date: 2015-09-11 09:01:46 UTC
  • Revision ID: morphis@gravedo.de-20150911090146-4c0ln9s7ec3xf0nx
Import package bluez_4.101-0ubuntu25.1~overlay4 from stable phone overlay PPA

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
 
 
3
# Script for testing the Attribute D-Bus API
 
4
 
 
5
import sys
 
6
from optparse import OptionParser, OptionValueError
 
7
from binascii import hexlify, unhexlify
 
8
 
 
9
import gobject
 
10
 
 
11
import sys
 
12
import dbus
 
13
import dbus.mainloop.glib
 
14
from optparse import OptionParser, make_option
 
15
 
 
16
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
 
17
bus = dbus.SystemBus()
 
18
mainloop = gobject.MainLoop()
 
19
 
 
20
manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager")
 
21
 
 
22
option_list = [
 
23
                make_option("-i", "--device", action="store",
 
24
                                type="string", dest="dev_id"),
 
25
                ]
 
26
parser = OptionParser(option_list=option_list)
 
27
 
 
28
(options, args) = parser.parse_args()
 
29
 
 
30
if options.dev_id:
 
31
        adapter_path = manager.FindAdapter(options.dev_id)
 
32
else:
 
33
        adapter_path = manager.DefaultAdapter()
 
34
 
 
35
adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
 
36
                                                        "org.bluez.Adapter")
 
37
 
 
38
if (len(args) < 1):
 
39
        print("Usage: %s <command>" % (sys.argv[0]))
 
40
        print("")
 
41
        print("  list")
 
42
        print("  services <address>")
 
43
        print("  discover <service path>")
 
44
        print("  chars <service path>")
 
45
        sys.exit(1)
 
46
 
 
47
if (args[0] == "list"):
 
48
        for path in adapter.ListDevices():
 
49
                device = dbus.Interface(bus.get_object("org.bluez", path),
 
50
                                                        "org.bluez.Device")
 
51
                devprop = device.GetProperties()
 
52
                print("[ %s ]" % devprop["Address"])
 
53
                for path in devprop["Services"]:
 
54
 
 
55
                        service = dbus.Interface(bus.get_object("org.bluez", path),
 
56
                                                                         "org.bluez.Characteristic")
 
57
                        srvprop = service.GetProperties()
 
58
                        print(" * %s" % (path))
 
59
                        print(" UUID: %s" % srvprop["UUID"])
 
60
                        print(" Chars: ",)
 
61
                        for char in srvprop["Characteristics"]:
 
62
                                print("%s " % char,)
 
63
                        print()
 
64
                        print()
 
65
                print()
 
66
        sys.exit(0)
 
67
 
 
68
if (args[0] == "services"):
 
69
        if (len(args) < 2):
 
70
                print("Need address parameter")
 
71
        else:
 
72
                path = adapter.FindDevice(args[1])
 
73
                device = dbus.Interface(bus.get_object("org.bluez", path),
 
74
                                                        "org.bluez.Device")
 
75
                properties = device.GetProperties()
 
76
                for path in properties["Services"]:
 
77
                        print(path)
 
78
        sys.exit(0)
 
79
 
 
80
if (args[0] == "discover"):
 
81
        if (len(args) < 2):
 
82
                print("Need service path parameter")
 
83
        else:
 
84
                service = dbus.Interface(bus.get_object("org.bluez", args[1]),
 
85
                                                        "org.bluez.Characteristic")
 
86
                for path in service.DiscoverCharacteristics():
 
87
                        print(path)
 
88
        sys.exit(0)
 
89
 
 
90
if (args[0] == "chars"):
 
91
        if (len(args) < 2):
 
92
                print("Need service path parameter")
 
93
        else:
 
94
                service = dbus.Interface(bus.get_object("org.bluez", args[1]),
 
95
                                                                 "org.bluez.Characteristic")
 
96
                srvprop = service.GetProperties()
 
97
                for path in srvprop["Characteristics"]:
 
98
                        print("[ %s ]" % (path))
 
99
                        char = dbus.Interface(bus.get_object("org.bluez", path),
 
100
                                                                 "org.bluez.Characteristic")
 
101
                        charprop = char.GetProperties()
 
102
                        print(" Name: %s" % charprop["Name"])
 
103
                        print(" UUID: %s" % charprop["UUID"])
 
104
                        print()
 
105
                print()
 
106
        sys.exit(0)
 
107
 
 
108
print("Unknown command")
 
109
sys.exit(1)