~timchen119/+junk/bluez5-trusty

« back to all changes in this revision

Viewing changes to test/test-device

  • Committer: tim
  • Date: 2015-09-04 08:03:03 UTC
  • Revision ID: tim@tim-inspiron-5442-20150904080303-75u7z8bdsl17xz8q
* init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
 
 
3
from optparse import OptionParser, make_option
 
4
import re
 
5
import sys
 
6
import dbus
 
7
import dbus.mainloop.glib
 
8
try:
 
9
  from gi.repository import GObject
 
10
except ImportError:
 
11
  import gobject as GObject
 
12
import bluezutils
 
13
 
 
14
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
 
15
bus = dbus.SystemBus()
 
16
mainloop = GObject.MainLoop()
 
17
 
 
18
option_list = [
 
19
                make_option("-i", "--device", action="store",
 
20
                                type="string", dest="dev_id"),
 
21
                ]
 
22
parser = OptionParser(option_list=option_list)
 
23
 
 
24
(options, args) = parser.parse_args()
 
25
 
 
26
if (len(args) < 1):
 
27
        print("Usage: %s <command>" % (sys.argv[0]))
 
28
        print("")
 
29
        print("  list")
 
30
        print("  create <address>")
 
31
        print("  remove <address|path>")
 
32
        print("  connect <address> [profile]")
 
33
        print("  disconnect <address> [profile]")
 
34
        print("  class <address>")
 
35
        print("  name <address>")
 
36
        print("  alias <address> [alias]")
 
37
        print("  trusted <address> [yes/no]")
 
38
        print("  blocked <address> [yes/no]")
 
39
        sys.exit(1)
 
40
 
 
41
if (args[0] == "list"):
 
42
        adapter = bluezutils.find_adapter(options.dev_id)
 
43
        adapter_path = adapter.object_path
 
44
 
 
45
        om = dbus.Interface(bus.get_object("org.bluez", "/"),
 
46
                                        "org.freedesktop.DBus.ObjectManager")
 
47
        objects = om.GetManagedObjects()
 
48
 
 
49
        for path, interfaces in objects.items():
 
50
                if "org.bluez.Device1" not in interfaces:
 
51
                        continue
 
52
                properties = interfaces["org.bluez.Device1"]
 
53
                if properties["Adapter"] != adapter_path:
 
54
                        continue;
 
55
                print("%s %s" % (properties["Address"], properties["Alias"]))
 
56
 
 
57
        sys.exit(0)
 
58
 
 
59
def create_device_reply(device):
 
60
        print("New device (%s)" % device)
 
61
        mainloop.quit()
 
62
        sys.exit(0)
 
63
 
 
64
def create_device_error(error):
 
65
        print("Creating device failed: %s" % error)
 
66
        mainloop.quit()
 
67
        sys.exit(1)
 
68
 
 
69
if (args[0] == "create"):
 
70
        if (len(args) < 2):
 
71
                print("Need address parameter")
 
72
        else:
 
73
                adapter = bluezutils.find_adapter(options.dev_id)
 
74
                adapter.CreateDevice(args[1],
 
75
                                reply_handler=create_device_reply,
 
76
                                error_handler=create_device_error)
 
77
        mainloop.run()
 
78
 
 
79
if (args[0] == "remove"):
 
80
        if (len(args) < 2):
 
81
                print("Need address or object path parameter")
 
82
        else:
 
83
                managed_objects = bluezutils.get_managed_objects()
 
84
                adapter = bluezutils.find_adapter_in_objects(managed_objects,
 
85
                                                                options.dev_id)
 
86
                try:
 
87
                        dev = bluezutils.find_device_in_objects(managed_objects,
 
88
                                                                args[1],
 
89
                                                                options.dev_id)
 
90
                        path = dev.object_path
 
91
                except:
 
92
                        path = args[1]
 
93
                adapter.RemoveDevice(path)
 
94
        sys.exit(0)
 
95
 
 
96
if (args[0] == "connect"):
 
97
        if (len(args) < 2):
 
98
                print("Need address parameter")
 
99
        else:
 
100
                device = bluezutils.find_device(args[1], options.dev_id)
 
101
                if (len(args) > 2):
 
102
                        device.ConnectProfile(args[2])
 
103
                else:
 
104
                        device.Connect()
 
105
        sys.exit(0)
 
106
 
 
107
if (args[0] == "disconnect"):
 
108
        if (len(args) < 2):
 
109
                print("Need address parameter")
 
110
        else:
 
111
                device = bluezutils.find_device(args[1], options.dev_id)
 
112
                if (len(args) > 2):
 
113
                        device.DisconnectProfile(args[2])
 
114
                else:
 
115
                        device.Disconnect()
 
116
        sys.exit(0)
 
117
 
 
118
if (args[0] == "class"):
 
119
        if (len(args) < 2):
 
120
                print("Need address parameter")
 
121
        else:
 
122
                device = bluezutils.find_device(args[1], options.dev_id)
 
123
                path = device.object_path
 
124
                props = dbus.Interface(bus.get_object("org.bluez", path),
 
125
                                        "org.freedesktop.DBus.Properties")
 
126
                cls = props.Get("org.bluez.Device1", "Class")
 
127
                print("0x%06x" % cls)
 
128
        sys.exit(0)
 
129
 
 
130
if (args[0] == "name"):
 
131
        if (len(args) < 2):
 
132
                print("Need address parameter")
 
133
        else:
 
134
                device = bluezutils.find_device(args[1], options.dev_id)
 
135
                path = device.object_path
 
136
                props = dbus.Interface(bus.get_object("org.bluez", path),
 
137
                                        "org.freedesktop.DBus.Properties")
 
138
                name = props.Get("org.bluez.Device1", "Name")
 
139
                print(name)
 
140
        sys.exit(0)
 
141
 
 
142
if (args[0] == "alias"):
 
143
        if (len(args) < 2):
 
144
                print("Need address parameter")
 
145
        else:
 
146
                device = bluezutils.find_device(args[1], options.dev_id)
 
147
                path = device.object_path
 
148
                props = dbus.Interface(bus.get_object("org.bluez", path),
 
149
                                        "org.freedesktop.DBus.Properties")
 
150
                if (len(args) < 3):
 
151
                        alias = props.Get("org.bluez.Device1", "Alias")
 
152
                        print(alias)
 
153
                else:
 
154
                        props.Set("org.bluez.Device1", "Alias", args[2])
 
155
        sys.exit(0)
 
156
 
 
157
if (args[0] == "trusted"):
 
158
        if (len(args) < 2):
 
159
                print("Need address parameter")
 
160
        else:
 
161
                device = bluezutils.find_device(args[1], options.dev_id)
 
162
                path = device.object_path
 
163
                props = dbus.Interface(bus.get_object("org.bluez", path),
 
164
                                        "org.freedesktop.DBus.Properties")
 
165
                if (len(args) < 3):
 
166
                        trusted = props.Get("org.bluez.Device1", "Trusted")
 
167
                        print(trusted)
 
168
                else:
 
169
                        if (args[2] == "yes"):
 
170
                                value = dbus.Boolean(1)
 
171
                        elif (args[2] == "no"):
 
172
                                value = dbus.Boolean(0)
 
173
                        else:
 
174
                                value = dbus.Boolean(args[2])
 
175
                        props.Set("org.bluez.Device1", "Trusted", value)
 
176
        sys.exit(0)
 
177
 
 
178
if (args[0] == "blocked"):
 
179
        if (len(args) < 2):
 
180
                print("Need address parameter")
 
181
        else:
 
182
                device = bluezutils.find_device(args[1], options.dev_id)
 
183
                path = device.object_path
 
184
                props = dbus.Interface(bus.get_object("org.bluez", path),
 
185
                                        "org.freedesktop.DBus.Properties")
 
186
                if (len(args) < 3):
 
187
                        blocked = props.Get("org.bluez.Device1", "Blocked")
 
188
                        print(blocked)
 
189
                else:
 
190
                        if (args[2] == "yes"):
 
191
                                value = dbus.Boolean(1)
 
192
                        elif (args[2] == "no"):
 
193
                                value = dbus.Boolean(0)
 
194
                        else:
 
195
                                value = dbus.Boolean(args[2])
 
196
                        props.Set("org.bluez.Device1", "Blocked", value)
 
197
        sys.exit(0)
 
198
 
 
199
print("Unknown command")
 
200
sys.exit(1)