OSDN Git Service

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