blob: 27915806f3560f924a3f7aa9b905072450f2e126 [file] [log] [blame]
Johan Hedberg384f7e12012-09-25 15:11:48 +03001#!/usr/bin/python
2
3from __future__ import absolute_import, print_function, unicode_literals
4
Petri Gynther7adf8d02014-01-17 17:45:07 -08005from optparse import OptionParser, make_option
Johan Hedberg363c8de2012-09-27 22:14:29 +03006import os
Johan Hedberg384f7e12012-09-25 15:11:48 +03007import sys
Johan Hedbergfe57c262012-11-17 06:48:52 +02008import uuid
Johan Hedberg384f7e12012-09-25 15:11:48 +03009import dbus
10import dbus.service
11import dbus.mainloop.glib
Petri Gynther7adf8d02014-01-17 17:45:07 -080012try:
13 from gi.repository import GObject
14except ImportError:
15 import gobject as GObject
Johan Hedberg384f7e12012-09-25 15:11:48 +030016
17class Profile(dbus.service.Object):
Johan Hedberg80efa0e2012-11-20 14:14:08 +020018 fd = -1
19
Marcel Holtmann0e163a62012-11-09 20:25:22 +010020 @dbus.service.method("org.bluez.Profile1",
Johan Hedberg384f7e12012-09-25 15:11:48 +030021 in_signature="", out_signature="")
22 def Release(self):
23 print("Release")
24 mainloop.quit()
25
Marcel Holtmann0e163a62012-11-09 20:25:22 +010026 @dbus.service.method("org.bluez.Profile1",
Johan Hedbergac06c7f2012-09-26 14:46:27 +030027 in_signature="", out_signature="")
28 def Cancel(self):
29 print("Cancel")
30
Marcel Holtmann0e163a62012-11-09 20:25:22 +010031 @dbus.service.method("org.bluez.Profile1",
Johan Hedbergc4b6d032012-11-09 12:29:33 +020032 in_signature="oha{sv}", out_signature="")
33 def NewConnection(self, path, fd, properties):
Johan Hedberg80efa0e2012-11-20 14:14:08 +020034 self.fd = fd.take()
35 print("NewConnection(%s, %d)" % (path, self.fd))
Johan Hedberg5f8e5482012-11-13 10:24:58 +020036 for key in properties.keys():
Johan Hedberg92056d52012-11-13 10:48:06 +020037 if key == "Version" or key == "Features":
38 print(" %s = 0x%04x" % (key, properties[key]))
39 else:
40 print(" %s = %s" % (key, properties[key]))
Johan Hedberg5f8e5482012-11-13 10:24:58 +020041
Johan Hedberg80efa0e2012-11-20 14:14:08 +020042 @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 Hedbergac06c7f2012-09-26 14:46:27 +030050
Johan Hedberg384f7e12012-09-25 15:11:48 +030051if __name__ == '__main__':
52 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
53
54 bus = dbus.SystemBus()
55
Marcel Holtmann0e163a62012-11-09 20:25:22 +010056 manager = dbus.Interface(bus.get_object("org.bluez",
57 "/org/bluez"), "org.bluez.ProfileManager1")
Johan Hedberg384f7e12012-09-25 15:11:48 +030058
59 option_list = [
60 make_option("-u", "--uuid", action="store",
Johan Hedbergac06c7f2012-09-26 14:46:27 +030061 type="string", dest="uuid",
Johan Hedbergfe57c262012-11-17 06:48:52 +020062 default=None),
Johan Hedberg384f7e12012-09-25 15:11:48 +030063 make_option("-p", "--path", action="store",
Johan Hedbergac06c7f2012-09-26 14:46:27 +030064 type="string", dest="path",
65 default="/foo/bar/profile"),
66 make_option("-n", "--name", action="store",
67 type="string", dest="name",
Johan Hedbergf0b3a332012-11-15 13:32:46 +020068 default=None),
Johan Hedbergac06c7f2012-09-26 14:46:27 +030069 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 Hedbergf752bac2012-09-26 15:12:45 +030078 make_option("-P", "--PSM", action="store",
Johan Hedberg53cbe8d2012-11-17 06:50:45 +020079 type="int", dest="psm",
80 default=None),
Johan Hedbergf752bac2012-09-26 15:12:45 +030081 make_option("-C", "--channel", action="store",
Johan Hedberg53cbe8d2012-11-17 06:50:45 +020082 type="int", dest="channel",
83 default=None),
Johan Hedbergbf1299b2012-11-13 10:25:03 +020084 make_option("-r", "--record", action="store",
85 type="string", dest="record",
86 default=None),
Johan Hedberg9e02ef22012-11-16 15:47:31 +020087 make_option("-S", "--service", action="store",
88 type="string", dest="service",
89 default=None),
Johan Hedberg384f7e12012-09-25 15:11:48 +030090 ]
Johan Hedbergac06c7f2012-09-26 14:46:27 +030091
Johan Hedberg384f7e12012-09-25 15:11:48 +030092 parser = OptionParser(option_list=option_list)
93
94 (options, args) = parser.parse_args()
95
Johan Hedberg384f7e12012-09-25 15:11:48 +030096 profile = Profile(bus, options.path)
97
98 mainloop = GObject.MainLoop()
99
Johan Hedbergac06c7f2012-09-26 14:46:27 +0300100 opts = {
Johan Hedbergac06c7f2012-09-26 14:46:27 +0300101 "AutoConnect" : options.auto_connect,
Johan Hedberg3278dc22012-09-25 19:17:00 +0300102 }
Johan Hedberg384f7e12012-09-25 15:11:48 +0300103
Johan Hedbergf0b3a332012-11-15 13:32:46 +0200104 if (options.name):
105 opts["Name"] = options.name
106
Johan Hedbergac06c7f2012-09-26 14:46:27 +0300107 if (options.role):
108 opts["Role"] = options.role
109
Johan Hedberg53cbe8d2012-11-17 06:50:45 +0200110 if (options.psm is not None):
Johan Hedbergf752bac2012-09-26 15:12:45 +0300111 opts["PSM"] = dbus.UInt16(options.psm)
112
Johan Hedberg53cbe8d2012-11-17 06:50:45 +0200113 if (options.channel is not None):
Johan Hedbergf752bac2012-09-26 15:12:45 +0300114 opts["Channel"] = dbus.UInt16(options.channel)
115
Johan Hedbergbf1299b2012-11-13 10:25:03 +0200116 if (options.record):
117 opts["ServiceRecord"] = options.record
118
Johan Hedberg9e02ef22012-11-16 15:47:31 +0200119 if (options.service):
120 opts["Service"] = options.service
121
Johan Hedbergfe57c262012-11-17 06:48:52 +0200122 if not options.uuid:
123 options.uuid = str(uuid.uuid4())
124
Johan Hedberg384f7e12012-09-25 15:11:48 +0300125 manager.RegisterProfile(options.path, options.uuid, opts)
126
127 mainloop.run()