summaryrefslogtreecommitdiffstats
path: root/hcid/dbus-test
blob: c931fa85bb44f0ef6c7a2880a15fe0fc0d2c0c0a (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
#!/usr/bin/env python

import dbus
import dbus.glib
import gobject
import sys
import getopt
from signal import *

mgr_cmds = [ "DeviceList", "DefaultDevice" ]
dev_cmds = [ "Up", "Down", "Inquiry", "CancelInquiry", "PeriodicInquiry",
             "CancelPeriodic", "RemoteName", "Connections", "Authenticate",
             "RoleSwitch" ]

class Tester:
    exit_events = []
    dev_path = None
    need_dev = False
    listen = False
    at_interrupt = None

    def __init__(self, argv):
        self.name = argv[0]

        self.parse_args(argv[1:])

        try:
            self.dbus_setup()
        except dbus.DBusException, e:
            print 'Failed to do D-BUS setup: %s' % e
            sys.exit(1)

        self.dev_setup()

    def parse_args(self, argv):
        try:
            opts, args = getopt.getopt(argv, "hli:")
        except getopt.GetoptError:
            self.usage()
            sys.exit(1)

        for o, a in opts:
            if o == "-h":
                self.usage()
                sys.exit()
            elif o == "-l":
                self.need_dev = True
                self.listen = True
            elif o == "-i":
                if a[0] == '/':
                    self.dev_path = a
                else:
                    self.dev_path = '/org/bluez/Device/%s' % a

        if not (args or self.listen):
            self.usage()
            sys.exit(1)

        if args:
            self.cmd = args[0]
            self.cmd_args = args[1:]

            if not self.cmd in mgr_cmds:
                self.need_dev = True

    def dev_setup(self):
        if self.need_dev and not self.dev_path:
            try:
                self.dev_path = self.manager.DefaultDevice()
            except dbus.DBusException, e:
                print 'Failed to get default device: %s' % e
                sys.exit(1)

        if self.dev_path:
            try:
                obj = self.bus.get_object('org.bluez', self.dev_path)
                self.dev = dbus.Interface(obj, 'org.bluez.Device')

                self.dev.connect_to_signal('Up', self.dev_up)
                self.dev.connect_to_signal('Down', self.dev_down)

                obj = self.bus.get_object('org.bluez', '%s/Controller' % self.dev_path)
                self.ctl = dbus.Interface(obj, 'org.bluez.Device.Controller')

                self.ctl.connect_to_signal('InquiryStart', self.inquiry_start)
                self.ctl.connect_to_signal('InquiryResult', self.inquiry_result)
                self.ctl.connect_to_signal('InquiryComplete', self.inquiry_complete)
                self.ctl.connect_to_signal('RemoteName', self.remote_name)
                self.ctl.connect_to_signal('RemoteNameFailed', self.remote_name_failed)
                self.ctl.connect_to_signal('AuthenticationComplete', self.authentication_complete)

            except dbus.DBusException, e:
                print 'Failed to setup device path: %s' % e
                sys.exit(1)

    def dbus_setup(self):
        self.bus = dbus.SystemBus()
        manager_obj = self.bus.get_object('org.bluez', '/org/bluez/Manager')
        self.manager = dbus.Interface(manager_obj, 'org.bluez.Manager')
        self.manager.connect_to_signal('DeviceAdded', self.device_added)
        self.manager.connect_to_signal('DeviceRemoved', self.device_removed)

    def usage(self):
        print 'Usage: %s [-i <dev>] [-l] [-h] <cmd> [arg1..]' % self.name
        print '  -i <dev>   Specify device (e.g. "hci0" or "/org/bluez/Device/hci0")'
        print '  -l         Listen for events (no command required)'
        print '  -h         Show this help'
        print 'Manager commands:'
        for cmd in mgr_cmds:
            print '\t%s' % cmd
        print 'Device commands:'
        for cmd in dev_cmds:
            print '\t%s' % cmd

    def device_added(self, path):
        print 'DeviceAdded: %s' % path

    def device_removed(self, path):
        print 'DeviceRemoved: %s' % path

    def remote_name(self, bda, name):
        print 'RemoteName: %s, %s' % (bda, name)
        if 'RemoteName' in self.exit_events:
            self.main_loop.quit()

    def remote_name_failed(self, bda, status):
        print 'RemoteNameFailed: %s, 0x%02X' % (bda, status)
        if 'RemoteNameFailed' in self.exit_events:
            self.main_loop.quit()

    def inquiry_start(self):
        print 'InquiryStart'

    def inquiry_complete(self):
        print 'InquiryComplete'
        if 'InquiryComplete' in self.exit_events:
            self.main_loop.quit()

    def inquiry_result(self, bda, cls, rssi):
        print 'InquiryResult: %s, %06X, %02X' % (bda, cls, rssi)

    def authentication_complete(self, bda, status):
        print 'AuthenticationComplete: %s, 0x%02X' % (bda, status)
        if 'AuthenticationComplete' in self.exit_events:
            self.main_loop.quit()

    def dev_up(self):
        print 'Up'

    def dev_down(self):
        print 'Down'

    def signal_cb(self, sig, frame):
        print 'Caught signal, exiting'
        if self.at_interrupt:
            self.at_interrupt()
        self.main_loop.quit()

    def run(self):
        # Manager methods
        if self.listen:
            print 'Listening for events...'
        elif self.cmd == 'DeviceList':
            for dev in self.manager.DeviceList():
                print dev
        elif self.cmd == 'DefaultDevice':
            print self.manager.DefaultDevice()

        # Device methods
        elif self.cmd == 'Up':
            try:
                self.dev.Up()
            except dbus.DBusException, e:
                print 'Sending %s failed: %s' % (self.cmd, e)
                sys.exit(1)
        elif self.cmd == 'Down':
            try:
                self.dev.Down()
            except dbus.DBusException, e:
                print 'Sending %s failed: %s' % (self.cmd, e)
                sys.exit(1)

        # Device.Controller methods
        elif self.cmd == 'Inquiry':
            if len(self.cmd_args) != 2:
                length, maxrsp = (10, 100)
            else:
                length, maxrsp = self.cmd_args
            try:
                self.ctl.Inquiry(dbus.Byte(length), dbus.Byte(maxrsp))
            except dbus.DBusException, e:
                print 'Sending %s failed: %s' % (self.cmd, e)
                sys.exit(1)
            self.listen = True
            self.exit_events.append('InquiryComplete')
            self.at_interrupt = self.ctl.CancelInquiry

        elif self.cmd == 'CancelInquiry':
            try:
                self.ctl.CancelInquiry()
            except dbus.DBusException, e:
                print 'Sending %s failed: %s' % (self.cmd, e)
                sys.exit(1)

        elif self.cmd == 'RemoteName':
            if len(self.cmd_args) < 1:
                print 'Bluetooth address needed'
                sys.exit(1)
            try:
                self.ctl.RemoteName(self.cmd_args[0])
            except dbus.DBusException, e:
                print 'Sending %s failed: %s' % (self.cmd, e)
                sys.exit(1)
            self.listen = True
            self.exit_events.append('RemoteNameFailed')
            self.exit_events.append('RemoteName')

        elif self.cmd == 'PeriodicInquiry':
            if len(self.cmd_args) != 3:
                length, min, max = (6, 20, 60)
            else:
                length, min, max = self.cmd_args
            self.listen = True
            try:
                self.ctl.PeriodicInquiry(dbus.Byte(length), dbus.Byte(min), dbus.Byte(max))
            except dbus.DBusException, e:
                print 'Sending %s failed: %s' % (self.cmd, e)
                sys.exit(1)

        elif self.cmd == 'CancelPeriodic':
            try:
                self.ctl.CancelPeriodic()
            except dbus.DBusException, e:
                print 'Sending %s failed: %s' % (self.cmd, e)
                sys.exit(1)

        elif self.cmd == 'Authenticate':
            if len(self.cmd_args) < 1:
                print 'Bluetooth address needed'
                sys.exit(1)
            try:
                self.ctl.Authenticate(self.cmd_args[0])
            except dbus.DBusException, e:
                print 'Sending %s failed: %s' % (self.cmd, e)
                sys.exit(1)
            self.listen = True
            self.exit_events.append('AuthenticationComplete')

        elif self.cmd == 'RoleSwitch':
            if len(self.cmd_args) < 2:
                print 'Bluetooth address and role needed'
                exit.exit(1)
            bda, role = self.cmd_args
            if not (role == '0' or role == '1'):
                print 'Role should be 0 (master) or 1 (slave)'
                sys.exit(1)
            try:
                self.ctl.RoleSwitch(bda, dbus.Byte(role))
            except dbus.DBusException, e:
                print 'Sending %s failed: %s' % (self.cmd, e)
                sys.exit(1)

        elif self.cmd == 'Connections':
            connections = self.ctl.Connections()
            for conn in connections:
                print conn

        else:
            print 'Unknown command: %s' % self.cmd
            sys.exit(1)

        if self.listen:
            signal(SIGINT, self.signal_cb)
            signal(SIGTERM, self.signal_cb)
            self.main_loop = gobject.MainLoop()
            self.main_loop.run()

if __name__ == '__main__':
    gobject.threads_init()
    dbus.glib.init_threads()

    tester = Tester(sys.argv)
    tester.run()