Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | from __future__ import absolute_import, print_function, unicode_literals |
| 4 | |
Petri Gynther | 7adf8d0 | 2014-01-17 17:45:07 -0800 | [diff] [blame] | 5 | from optparse import OptionParser, make_option |
Johan Hedberg | 363c8de | 2012-09-27 22:14:29 +0300 | [diff] [blame] | 6 | import os |
Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 7 | import sys |
Johan Hedberg | fe57c26 | 2012-11-17 06:48:52 +0200 | [diff] [blame] | 8 | import uuid |
Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 9 | import dbus |
| 10 | import dbus.service |
| 11 | import dbus.mainloop.glib |
Petri Gynther | 7adf8d0 | 2014-01-17 17:45:07 -0800 | [diff] [blame] | 12 | try: |
| 13 | from gi.repository import GObject |
| 14 | except ImportError: |
| 15 | import gobject as GObject |
Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 16 | |
| 17 | class Profile(dbus.service.Object): |
Johan Hedberg | 80efa0e | 2012-11-20 14:14:08 +0200 | [diff] [blame] | 18 | fd = -1 |
| 19 | |
Marcel Holtmann | 0e163a6 | 2012-11-09 20:25:22 +0100 | [diff] [blame] | 20 | @dbus.service.method("org.bluez.Profile1", |
Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 21 | in_signature="", out_signature="") |
| 22 | def Release(self): |
| 23 | print("Release") |
| 24 | mainloop.quit() |
| 25 | |
Marcel Holtmann | 0e163a6 | 2012-11-09 20:25:22 +0100 | [diff] [blame] | 26 | @dbus.service.method("org.bluez.Profile1", |
Johan Hedberg | ac06c7f | 2012-09-26 14:46:27 +0300 | [diff] [blame] | 27 | in_signature="", out_signature="") |
| 28 | def Cancel(self): |
| 29 | print("Cancel") |
| 30 | |
Marcel Holtmann | 0e163a6 | 2012-11-09 20:25:22 +0100 | [diff] [blame] | 31 | @dbus.service.method("org.bluez.Profile1", |
Johan Hedberg | c4b6d03 | 2012-11-09 12:29:33 +0200 | [diff] [blame] | 32 | in_signature="oha{sv}", out_signature="") |
| 33 | def NewConnection(self, path, fd, properties): |
Johan Hedberg | 80efa0e | 2012-11-20 14:14:08 +0200 | [diff] [blame] | 34 | self.fd = fd.take() |
| 35 | print("NewConnection(%s, %d)" % (path, self.fd)) |
Johan Hedberg | 5f8e548 | 2012-11-13 10:24:58 +0200 | [diff] [blame] | 36 | for key in properties.keys(): |
Johan Hedberg | 92056d5 | 2012-11-13 10:48:06 +0200 | [diff] [blame] | 37 | if key == "Version" or key == "Features": |
| 38 | print(" %s = 0x%04x" % (key, properties[key])) |
| 39 | else: |
| 40 | print(" %s = %s" % (key, properties[key])) |
Johan Hedberg | 5f8e548 | 2012-11-13 10:24:58 +0200 | [diff] [blame] | 41 | |
Johan Hedberg | 80efa0e | 2012-11-20 14:14:08 +0200 | [diff] [blame] | 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 |
Johan Hedberg | ac06c7f | 2012-09-26 14:46:27 +0300 | [diff] [blame] | 50 | |
Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 51 | if __name__ == '__main__': |
| 52 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 53 | |
| 54 | bus = dbus.SystemBus() |
| 55 | |
Marcel Holtmann | 0e163a6 | 2012-11-09 20:25:22 +0100 | [diff] [blame] | 56 | manager = dbus.Interface(bus.get_object("org.bluez", |
| 57 | "/org/bluez"), "org.bluez.ProfileManager1") |
Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 58 | |
| 59 | option_list = [ |
| 60 | make_option("-u", "--uuid", action="store", |
Johan Hedberg | ac06c7f | 2012-09-26 14:46:27 +0300 | [diff] [blame] | 61 | type="string", dest="uuid", |
Johan Hedberg | fe57c26 | 2012-11-17 06:48:52 +0200 | [diff] [blame] | 62 | default=None), |
Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 63 | make_option("-p", "--path", action="store", |
Johan Hedberg | ac06c7f | 2012-09-26 14:46:27 +0300 | [diff] [blame] | 64 | type="string", dest="path", |
| 65 | default="/foo/bar/profile"), |
| 66 | make_option("-n", "--name", action="store", |
| 67 | type="string", dest="name", |
Johan Hedberg | f0b3a33 | 2012-11-15 13:32:46 +0200 | [diff] [blame] | 68 | default=None), |
Johan Hedberg | ac06c7f | 2012-09-26 14:46:27 +0300 | [diff] [blame] | 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), |
Johan Hedberg | f752bac | 2012-09-26 15:12:45 +0300 | [diff] [blame] | 78 | make_option("-P", "--PSM", action="store", |
Johan Hedberg | 53cbe8d | 2012-11-17 06:50:45 +0200 | [diff] [blame] | 79 | type="int", dest="psm", |
| 80 | default=None), |
Johan Hedberg | f752bac | 2012-09-26 15:12:45 +0300 | [diff] [blame] | 81 | make_option("-C", "--channel", action="store", |
Johan Hedberg | 53cbe8d | 2012-11-17 06:50:45 +0200 | [diff] [blame] | 82 | type="int", dest="channel", |
| 83 | default=None), |
Johan Hedberg | bf1299b | 2012-11-13 10:25:03 +0200 | [diff] [blame] | 84 | make_option("-r", "--record", action="store", |
| 85 | type="string", dest="record", |
| 86 | default=None), |
Johan Hedberg | 9e02ef2 | 2012-11-16 15:47:31 +0200 | [diff] [blame] | 87 | make_option("-S", "--service", action="store", |
| 88 | type="string", dest="service", |
| 89 | default=None), |
Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 90 | ] |
Johan Hedberg | ac06c7f | 2012-09-26 14:46:27 +0300 | [diff] [blame] | 91 | |
Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 92 | parser = OptionParser(option_list=option_list) |
| 93 | |
| 94 | (options, args) = parser.parse_args() |
| 95 | |
Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 96 | profile = Profile(bus, options.path) |
| 97 | |
| 98 | mainloop = GObject.MainLoop() |
| 99 | |
Johan Hedberg | ac06c7f | 2012-09-26 14:46:27 +0300 | [diff] [blame] | 100 | opts = { |
Johan Hedberg | ac06c7f | 2012-09-26 14:46:27 +0300 | [diff] [blame] | 101 | "AutoConnect" : options.auto_connect, |
Johan Hedberg | 3278dc2 | 2012-09-25 19:17:00 +0300 | [diff] [blame] | 102 | } |
Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 103 | |
Johan Hedberg | f0b3a33 | 2012-11-15 13:32:46 +0200 | [diff] [blame] | 104 | if (options.name): |
| 105 | opts["Name"] = options.name |
| 106 | |
Johan Hedberg | ac06c7f | 2012-09-26 14:46:27 +0300 | [diff] [blame] | 107 | if (options.role): |
| 108 | opts["Role"] = options.role |
| 109 | |
Johan Hedberg | 53cbe8d | 2012-11-17 06:50:45 +0200 | [diff] [blame] | 110 | if (options.psm is not None): |
Johan Hedberg | f752bac | 2012-09-26 15:12:45 +0300 | [diff] [blame] | 111 | opts["PSM"] = dbus.UInt16(options.psm) |
| 112 | |
Johan Hedberg | 53cbe8d | 2012-11-17 06:50:45 +0200 | [diff] [blame] | 113 | if (options.channel is not None): |
Johan Hedberg | f752bac | 2012-09-26 15:12:45 +0300 | [diff] [blame] | 114 | opts["Channel"] = dbus.UInt16(options.channel) |
| 115 | |
Johan Hedberg | bf1299b | 2012-11-13 10:25:03 +0200 | [diff] [blame] | 116 | if (options.record): |
| 117 | opts["ServiceRecord"] = options.record |
| 118 | |
Johan Hedberg | 9e02ef2 | 2012-11-16 15:47:31 +0200 | [diff] [blame] | 119 | if (options.service): |
| 120 | opts["Service"] = options.service |
| 121 | |
Johan Hedberg | fe57c26 | 2012-11-17 06:48:52 +0200 | [diff] [blame] | 122 | if not options.uuid: |
| 123 | options.uuid = str(uuid.uuid4()) |
| 124 | |
Johan Hedberg | 384f7e1 | 2012-09-25 15:11:48 +0300 | [diff] [blame] | 125 | manager.RegisterProfile(options.path, options.uuid, opts) |
| 126 | |
| 127 | mainloop.run() |