summaryrefslogtreecommitdiffstats
path: root/test/apitest
blob: b1c3f103e9892dfcd6147c36b21ff3846e473c2a (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python

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

mgr_cmds = [ "InterfaceVersion", "ListAdapters", "DefaultAdapter" ]
mgr_signals = [ "AdapterAdded", "AdapterRemoved" ]

dev_cmds = [ "GetAddress",
             "GetVersion",
             "GetRevision",
             "GetManufacturer",
             "GetCompany",
             "GetMode",
             "SetMode",
             "GetDiscoverableTimeout",
             "SetDiscoverableTimeout",
             "IsConnectable",
             "IsDiscoverable",
             "IsConnected",
             "ListConnections",
             "GetMajorClass",
             "ListAvailableMinorClasses",
             "GetMinorClass",
             "SetMinorClass",
             "GetServiceClasses",
             "GetName",
             "SetName",
             "GetRemoteVersion",
             "GetRemoteRevision",
             "GetRemoteManufacturer",
             "GetRemoteCompany",
             "GetRemoteMajorClass",
             "GetRemoteMinorClass",
             "GetRemoteServiceClasses",
             "GetRemoteClass",
             "GetRemoteName",
             "GetRemoteAlias",
             "SetRemoteAlias",
             "ClearRemoteAlias",
             "LastSeen",
             "LastUsed",
             "DisconnectRemoteDevice",
             "CreateBonding",
             "CancelBondingProcess",
             "RemoveBonding",
             "HasBonding",
             "ListBondings",
             "GetPinCodeLength",
             "GetEncryptionKeySize",
             "DiscoverDevices",
             "DiscoverDevicesWithoutNameResolving",
             "CancelDiscovery",
             "ListRemoteDevices",
             "ListRecentRemoteDevices" ]
dev_signals = [ "ModeChanged",
                "NameChanged",
                "MinorClassChanged",
                "DiscoveryStarted",
                "DiscoveryCompleted",
                "RemoteDeviceFound",
                "RemoteNameUpdated",
                "RemoteNameFailed",
                "RemoteAliasChanged"
                "RemoteAliasCleared",
                "RemoteDeviceConnected",
                "RemoteDeviceDisconnectRequested",
                "RemoteDeviceDisconnected",
                "BondingCreated",
                "BondingRemoved" ]

dev_signals_filter = [ "/org/bluez/hci0", "/org/bluez/hci1",
                       "/org/bluez/hci2", "/org/bluez/hci3",
                       "/org/bluez/hci4", "/org/bluez/hci5",
                       "/org/bluez/hci6", "/org/bluez/hci7" ]

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)

    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.listen = True
            elif o == "-i":
                if a[0] == '/':
                    self.dev_path = a
                else:
                    self.dev_path = '/org/bluez/%s' % a

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

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

    def dbus_dev_setup(self):
        if not self.dev_path:
            try:
                self.dbus_mgr_setup()
                self.dev_path = self.manager.DefaultAdapter()
            except dbus.DBusException, e:
                print 'Failed to get default device: %s' % e
                sys.exit(1)
        try:
            obj = self.bus.get_object('org.bluez', self.dev_path)
            self.device = dbus.Interface(obj, 'org.bluez.Adapter')
        except dbus.DBusException, e:
            print 'Failed to setup device path: %s' % e
            sys.exit(1)

    def dbus_dev_sig_setup(self):
        try:
           for signal in dev_signals:
                for path in dev_signals_filter:
                    self.bus.add_signal_receiver(self.dev_signal_handler,
                                             signal, 'org.bluez.Adapter',
                                             'org.bluez', path,
                                             message_keyword='dbus_message')
        except dbus.DBusException, e:
            print 'Failed to setup signal handler for device path: %s' % e
            sys.exit(1)

    def dbus_mgr_sig_setup(self):
        try:
            for signal in mgr_signals:
                self.bus.add_signal_receiver(self.mgr_signal_handler,
                                         signal,'org.bluez.Manager',
                                         'org.bluez', '/org/bluez')
        except dbus.DBusException, e:
            print 'Failed to setup signal handler for manager path: %s' % e
            sys.exit(1)

    def dbus_mgr_setup(self):
        self.manager_obj = self.bus.get_object('org.bluez', '/org/bluez')
        self.manager = dbus.Interface(self.manager_obj, 'org.bluez.Manager')

    def dbus_setup(self):
        self.bus = dbus.SystemBus()

    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/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 'Adapter commands:'
        for cmd in dev_cmds:
            print '\t%s' % cmd

    #@dbus.decorators.explicitly_pass_message
    def dev_signal_handler(*args, **keywords):
        dbus_message = keywords["dbus_message"]
        print '%s - %s: ' % (dbus_message.get_member(), dbus_message.get_path()),
        for arg in args[1:]:
            print '%s   ' % arg,
        print

    #@dbus.decorators.explicitly_pass_message
    def mgr_signal_handler(*args, **keywords):
        dbus_message = keywords["dbus_message"]
        print '%s: ' % dbus_message.get_member()
        for arg in args[1:]:
            print '%s   ' % arg,
        print

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

    def call_mgr_dbus_func(self):
        if self.cmd == 'InterfaceVersion':
            try:
                print self.manager.InterfaceVersion()
            except dbus.DBusException, e:
                print 'Sending %s failed: %s' % (self.cmd, e)
        if self.cmd == 'ListAdapters':
            try:
                devices = self.manager.ListAdapters()
            except dbus.DBusException, e:
                print 'Sending %s failed: %s' % (self.cmd, e)
                sys.exit(1)
            for device in devices:
                print device
        elif self.cmd == 'DefaultAdapter':
            try:
                print self.manager.DefaultAdapter()
            except dbus.DBusException, e:
                print 'Sending %s failed: %s' % (self.cmd, e)
                sys.exit(1)

    def call_dev_dbus_func(self):
       try:
           if self.cmd == 'GetAddress':
               print self.device.GetAddress()
           elif self.cmd == 'GetManufacturer':
               print self.device.GetManufacturer()
           elif self.cmd == 'GetVersion':
               print self.device.GetVersion()
           elif self.cmd == 'GetRevision':
               print self.device.GetRevision()
           elif self.cmd == 'GetCompany':
               print self.device.GetCompany()
           elif self.cmd == 'GetMode':
               print self.device.GetMode()
           elif self.cmd == 'SetMode':
               if len(self.cmd_args) == 1:
                   self.device.SetMode(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> SetMode scan_mode' % self.name
           elif self.cmd == 'GetDiscoverableTimeout':
               print '%u' % (self.device.GetDiscoverableTimeout())
           elif self.cmd == 'SetDiscoverableTimeout':
               if len(self.cmd_args) == 1:
                   self.device.SetDiscoverableTimeout(dbus.UInt32(self.cmd_args[0]))
               else:
                   print 'Usage: %s -i <dev> SetDiscoverableTimeout timeout' % self.name
           elif self.cmd == 'IsConnectable':
               print self.device.IsConnectable()
           elif self.cmd == 'IsDiscoverable':
               print self.device.IsDiscoverable()
           elif self.cmd == 'IsConnected':
               if len(self.cmd_args) == 1:
                   print self.device.IsConnected(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> IsConnected address' % self.name
           elif self.cmd == 'ListConnections':
               print self.device.ListConnections()
           elif self.cmd == 'GetMajorClass':
               print self.device.GetMajorClass()
           elif self.cmd == 'ListAvailableMinorClasses':
               print self.device.ListAvailableMinorClasses()
           elif self.cmd == 'GetMinorClass':
               print self.device.GetMinorClass()
           elif self.cmd == 'SetMinorClass':
               if len(self.cmd_args) == 1:
                   self.device.SetMinorClass(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> SetMinorClass minor' % self.name
           elif self.cmd == 'GetServiceClasses':
               classes = self.device.GetServiceClasses()
               for clas in classes: 
                   print clas,
           elif self.cmd == 'GetName':
               print self.device.GetName()
           elif self.cmd == 'SetName':
               if len(self.cmd_args) == 1:
                   self.device.SetName(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> SetName newname' % self.name
           elif self.cmd == 'GetRemoteName':
               if len(self.cmd_args) == 1:
                   print self.device.GetRemoteName(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> GetRemoteName address' % self.name
           elif self.cmd == 'GetRemoteVersion':
               if len(self.cmd_args) == 1:
                   print self.device.GetRemoteVersion(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> GetRemoteVersion address' % self.name
           elif self.cmd == 'GetRemoteRevision':
               if len(self.cmd_args) == 1:
                   print self.device.GetRemoteRevision(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> GetRemoteRevision address' % self.name
           elif self.cmd == 'GetRemoteManufacturer':
               if len(self.cmd_args) == 1:
                   print self.device.GetRemoteManufacturer(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> GetRemoteManufacturer address' % self.name
           elif self.cmd == 'GetRemoteCompany':
               if len(self.cmd_args) == 1:
                   print self.device.GetRemoteCompany(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> GetRemoteCompany address' % self.name
           elif self.cmd == 'GetRemoteAlias':
               if len(self.cmd_args) == 1:
                   print self.device.GetRemoteAlias(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> GetRemoteAlias address' % self.name
           elif self.cmd == 'GetRemoteMajorClass':
               if len(self.cmd_args) == 1:
                   print self.device.GetRemoteMajorClass(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> GetRemoteMajorClass address' % self.name
           elif self.cmd == 'GetRemoteMinorClass':
               if len(self.cmd_args) == 1:
                   print self.device.GetRemoteMinorClass(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> GetRemoteMinorClass address' % self.name
           elif self.cmd == 'GetRemoteServiceClasses':
               if len(self.cmd_args) == 1:
                   print self.device.GetRemoteServiceClasses(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> GetRemoteServiceClasses address' % self.name
           elif self.cmd == 'SetRemoteAlias':
               if len(self.cmd_args) == 2:
                   self.device.SetRemoteAlias(self.cmd_args[0], self.cmd_args[1])
               else:
                   print 'Usage: %s -i <dev> SetRemoteAlias address alias' % self.name
           elif self.cmd == 'ClearRemoteAlias':
               if len(self.cmd_args) == 1:
                   print self.device.ClearRemoteAlias(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> ClearRemoteAlias address' % self.name
           elif self.cmd == 'LastSeen':
               if len(self.cmd_args) == 1:
                   print self.device.LastSeen(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> LastSeen address' % self.name
           elif self.cmd == 'LastUsed':
               if len(self.cmd_args) == 1:
                   print self.device.LastUsed(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> LastUsed address' % self.name
           elif self.cmd == 'DisconnectRemoteDevice':
               if len(self.cmd_args) == 1:
                   print self.device.LastUsed(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> DisconnectRemoteDevice address' % self.name
           elif self.cmd == 'CreateBonding':
               if len(self.cmd_args) == 1:
                   print self.device.CreateBonding(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> CreateBonding address' % self.name
           elif self.cmd == 'RemoveBonding':
               if len(self.cmd_args) == 1:
                   print self.device.RemoveBonding(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> RemoveBonding address' % self.name
           elif self.cmd == 'CancelBondingProcess':
               if len(self.cmd_args) == 1:
                   print self.device.CancelBondingProcess(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> CancelBondingProcess address' % self.name
           elif self.cmd == 'HasBonding':
               if len(self.cmd_args) == 1:
                   print self.device.HasBonding(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> HasBonding address' % self.name
           elif self.cmd == 'ListBondings':
               bondings = self.device.ListBondings()
               for bond in bondings: 
                   print bond,
           elif self.cmd == 'GetPinCodeLength':
               if len(self.cmd_args) == 1:
                   print self.device.GetPinCodeLength(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> GetPinCodeLength address' % self.name
           elif self.cmd == 'GetEncryptionKeySize':
               if len(self.cmd_args) == 1:
                   print self.device.GetEncryptionKeySize(self.cmd_args[0])
               else:
                   print 'Usage: %s -i <dev> GetEncryptionKeySize address' % self.name
           elif self.cmd == 'DiscoverDevices':
               print self.device.DiscoverDevices()
           elif self.cmd == 'DiscoverDevicesWithoutNameResolving':
               print self.device.DiscoverDevicesWithoutNameResolving()
           elif self.cmd == 'ListRemoteDevices':
               devices = self.device.ListRemoteDevices()
               for device in devices: 
                   print device,
           elif self.cmd == 'ListRecentRemoteDevices':
               if len(self.cmd_args) == 1:
                   devices = self.device.ListRecentRemoteDevices(self.cmd_args[0])
                   for device in devices: 
                       print device,
               else:
                   print 'Usage: %s -i <dev> ListRecentRemoteDevices date' % self.name
           else:
                # FIXME: remove at future version
                print 'Script Error: Method %s not found. Maybe a mispelled word.' % (self.cmd_args)
       except dbus.DBusException, e:
           print '%s failed: %s' % (self.cmd, e)
           sys.exit(1)

    def run(self):
        # Manager methods
        if self.listen:
            self.dbus_mgr_sig_setup()
            self.dbus_dev_sig_setup()
            print 'Listening for events...'

        if self.cmd in mgr_cmds:
            try:
                self.dbus_mgr_setup()
            except dbus.DBusException, e:
                print 'Failed to setup manager interface: %s' % e
                sys.exit(1)
            self.call_mgr_dbus_func()
        elif self.cmd in dev_cmds:
            try:
                self.dbus_dev_setup()
            except dbus.DBusException, e:
                print 'Failed to setup device interface: %s' % e
                sys.exit(1)
            self.call_dev_dbus_func()
        elif not self.listen:
            print 'Unknown command: %s' % self.cmd
            self.usage()
            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()