~timchen119/+junk/bluez5-trusty

« back to all changes in this revision

Viewing changes to test/test-profile

  • 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 os
 
5
import sys
 
6
import uuid
 
7
import dbus
 
8
import dbus.service
 
9
import dbus.mainloop.glib
 
10
try:
 
11
  from gi.repository import GObject
 
12
except ImportError:
 
13
  import gobject as GObject
 
14
 
 
15
class Profile(dbus.service.Object):
 
16
        fd = -1
 
17
 
 
18
        @dbus.service.method("org.bluez.Profile1",
 
19
                                        in_signature="", out_signature="")
 
20
        def Release(self):
 
21
                print("Release")
 
22
                mainloop.quit()
 
23
 
 
24
        @dbus.service.method("org.bluez.Profile1",
 
25
                                        in_signature="", out_signature="")
 
26
        def Cancel(self):
 
27
                print("Cancel")
 
28
 
 
29
        @dbus.service.method("org.bluez.Profile1",
 
30
                                in_signature="oha{sv}", out_signature="")
 
31
        def NewConnection(self, path, fd, properties):
 
32
                self.fd = fd.take()
 
33
                print("NewConnection(%s, %d)" % (path, self.fd))
 
34
                for key in properties.keys():
 
35
                        if key == "Version" or key == "Features":
 
36
                                print("  %s = 0x%04x" % (key, properties[key]))
 
37
                        else:
 
38
                                print("  %s = %s" % (key, properties[key]))
 
39
 
 
40
        @dbus.service.method("org.bluez.Profile1",
 
41
                                in_signature="o", out_signature="")
 
42
        def RequestDisconnection(self, path):
 
43
                print("RequestDisconnection(%s)" % (path))
 
44
 
 
45
                if (self.fd > 0):
 
46
                        os.close(self.fd)
 
47
                        self.fd = -1
 
48
 
 
49
if __name__ == '__main__':
 
50
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
 
51
 
 
52
        bus = dbus.SystemBus()
 
53
 
 
54
        manager = dbus.Interface(bus.get_object("org.bluez",
 
55
                                "/org/bluez"), "org.bluez.ProfileManager1")
 
56
 
 
57
        option_list = [
 
58
                        make_option("-u", "--uuid", action="store",
 
59
                                        type="string", dest="uuid",
 
60
                                        default=None),
 
61
                        make_option("-p", "--path", action="store",
 
62
                                        type="string", dest="path",
 
63
                                        default="/foo/bar/profile"),
 
64
                        make_option("-n", "--name", action="store",
 
65
                                        type="string", dest="name",
 
66
                                        default=None),
 
67
                        make_option("-s", "--server",
 
68
                                        action="store_const",
 
69
                                        const="server", dest="role"),
 
70
                        make_option("-c", "--client",
 
71
                                        action="store_const",
 
72
                                        const="client", dest="role"),
 
73
                        make_option("-a", "--auto-connect",
 
74
                                        action="store_true",
 
75
                                        dest="auto_connect", default=False),
 
76
                        make_option("-P", "--PSM", action="store",
 
77
                                        type="int", dest="psm",
 
78
                                        default=None),
 
79
                        make_option("-C", "--channel", action="store",
 
80
                                        type="int", dest="channel",
 
81
                                        default=None),
 
82
                        make_option("-r", "--record", action="store",
 
83
                                        type="string", dest="record",
 
84
                                        default=None),
 
85
                        make_option("-S", "--service", action="store",
 
86
                                        type="string", dest="service",
 
87
                                        default=None),
 
88
                        ]
 
89
 
 
90
        parser = OptionParser(option_list=option_list)
 
91
 
 
92
        (options, args) = parser.parse_args()
 
93
 
 
94
        profile = Profile(bus, options.path)
 
95
 
 
96
        mainloop = GObject.MainLoop()
 
97
 
 
98
        opts = {
 
99
                        "AutoConnect" : options.auto_connect,
 
100
                }
 
101
 
 
102
        if (options.name):
 
103
                opts["Name"] = options.name
 
104
 
 
105
        if (options.role):
 
106
                opts["Role"] = options.role
 
107
 
 
108
        if (options.psm is not None):
 
109
                opts["PSM"] = dbus.UInt16(options.psm)
 
110
 
 
111
        if (options.channel is not None):
 
112
                opts["Channel"] = dbus.UInt16(options.channel)
 
113
 
 
114
        if (options.record):
 
115
                opts["ServiceRecord"] = options.record
 
116
 
 
117
        if (options.service):
 
118
                opts["Service"] = options.service
 
119
 
 
120
        if not options.uuid:
 
121
                options.uuid = str(uuid.uuid4())
 
122
 
 
123
        manager.RegisterProfile(options.path, options.uuid, opts)
 
124
 
 
125
        mainloop.run()