Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Steve Langasek | ee56337 | 2012-05-24 22:44:25 -0700 | [diff] [blame] | 3 | from __future__ import absolute_import, print_function, unicode_literals |
| 4 | |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 5 | import sys |
| 6 | import dbus |
| 7 | import dbus.service |
| 8 | import dbus.mainloop.glib |
Petri Gynther | 7adf8d0 | 2014-01-17 17:45:07 -0800 | [diff] [blame] | 9 | try: |
| 10 | from gi.repository import GObject |
| 11 | except ImportError: |
| 12 | import gobject as GObject |
Mikel Astiz | 90e7850 | 2012-12-05 13:51:30 +0100 | [diff] [blame] | 13 | import bluezutils |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 14 | |
| 15 | A2DP_SOURCE_UUID = "0000110A-0000-1000-8000-00805F9B34FB" |
| 16 | A2DP_SINK_UUID = "0000110B-0000-1000-8000-00805F9B34FB" |
| 17 | HFP_AG_UUID = "0000111F-0000-1000-8000-00805F9B34FB" |
Vinicius Costa Gomes | 803bff2 | 2012-12-03 20:42:28 -0300 | [diff] [blame] | 18 | HFP_HF_UUID = "0000111E-0000-1000-8000-00805F9B34FB" |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 19 | HSP_AG_UUID = "00001112-0000-1000-8000-00805F9B34FB" |
| 20 | |
| 21 | SBC_CODEC = dbus.Byte(0x00) |
| 22 | #Channel Modes: Mono DualChannel Stereo JointStereo |
| 23 | #Frequencies: 16Khz 32Khz 44.1Khz 48Khz |
| 24 | #Subbands: 4 8 |
| 25 | #Blocks: 4 8 12 16 |
| 26 | #Bitpool Range: 2-64 |
| 27 | SBC_CAPABILITIES = dbus.Array([dbus.Byte(0xff), dbus.Byte(0xff), dbus.Byte(2), dbus.Byte(64)]) |
| 28 | # JointStereo 44.1Khz Subbands: Blocks: 16 Bitpool Range: 2-32 |
| 29 | SBC_CONFIGURATION = dbus.Array([dbus.Byte(0x21), dbus.Byte(0x15), dbus.Byte(2), dbus.Byte(32)]) |
| 30 | |
| 31 | MP3_CODEC = dbus.Byte(0x01) |
| 32 | #Channel Modes: Mono DualChannel Stereo JointStereo |
| 33 | #Frequencies: 32Khz 44.1Khz 48Khz |
| 34 | #CRC: YES |
| 35 | #Layer: 3 |
| 36 | #Bit Rate: All except Free format |
| 37 | #VBR: Yes |
| 38 | #Payload Format: RFC-2250 |
| 39 | MP3_CAPABILITIES = dbus.Array([dbus.Byte(0x3f), dbus.Byte(0x07), dbus.Byte(0xff), dbus.Byte(0xfe)]) |
| 40 | # JointStereo 44.1Khz Layer: 3 Bit Rate: VBR Format: RFC-2250 |
| 41 | MP3_CONFIGURATION = dbus.Array([dbus.Byte(0x21), dbus.Byte(0x02), dbus.Byte(0x00), dbus.Byte(0x80)]) |
| 42 | |
| 43 | PCM_CODEC = dbus.Byte(0x00) |
| 44 | PCM_CONFIGURATION = dbus.Array([], signature="ay") |
| 45 | |
Vinicius Costa Gomes | 803bff2 | 2012-12-03 20:42:28 -0300 | [diff] [blame] | 46 | CVSD_CODEC = dbus.Byte(0x01) |
| 47 | |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 48 | class Rejected(dbus.DBusException): |
| 49 | _dbus_error_name = "org.bluez.Error.Rejected" |
| 50 | |
| 51 | class Endpoint(dbus.service.Object): |
| 52 | exit_on_release = True |
| 53 | configuration = SBC_CONFIGURATION |
| 54 | |
| 55 | def set_exit_on_release(self, exit_on_release): |
| 56 | self.exit_on_release = exit_on_release |
| 57 | |
| 58 | def default_configuration(self, configuration): |
| 59 | self.configuration = configuration |
| 60 | |
Vinicius Costa Gomes | 375e766 | 2012-12-05 20:18:09 -0300 | [diff] [blame] | 61 | @dbus.service.method("org.bluez.MediaEndpoint1", |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 62 | in_signature="", out_signature="") |
| 63 | def Release(self): |
Steve Langasek | ee56337 | 2012-05-24 22:44:25 -0700 | [diff] [blame] | 64 | print("Release") |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 65 | if self.exit_on_release: |
| 66 | mainloop.quit() |
| 67 | |
Vinicius Costa Gomes | 375e766 | 2012-12-05 20:18:09 -0300 | [diff] [blame] | 68 | @dbus.service.method("org.bluez.MediaEndpoint1", |
Luiz Augusto von Dentz | 55192ae | 2016-05-30 12:55:55 +0300 | [diff] [blame] | 69 | in_signature="o", out_signature="") |
| 70 | def ClearConfiguration(self, transport): |
| 71 | print("ClearConfiguration (%s)" % (transport)) |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 72 | |
Vinicius Costa Gomes | 375e766 | 2012-12-05 20:18:09 -0300 | [diff] [blame] | 73 | @dbus.service.method("org.bluez.MediaEndpoint1", |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 74 | in_signature="oay", out_signature="") |
| 75 | def SetConfiguration(self, transport, config): |
Steve Langasek | ee56337 | 2012-05-24 22:44:25 -0700 | [diff] [blame] | 76 | print("SetConfiguration (%s, %s)" % (transport, config)) |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 77 | return |
| 78 | |
Vinicius Costa Gomes | 375e766 | 2012-12-05 20:18:09 -0300 | [diff] [blame] | 79 | @dbus.service.method("org.bluez.MediaEndpoint1", |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 80 | in_signature="ay", out_signature="ay") |
| 81 | def SelectConfiguration(self, caps): |
Steve Langasek | ee56337 | 2012-05-24 22:44:25 -0700 | [diff] [blame] | 82 | print("SelectConfiguration (%s)" % (caps)) |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 83 | return self.configuration |
| 84 | |
| 85 | if __name__ == '__main__': |
| 86 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 87 | |
| 88 | bus = dbus.SystemBus() |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 89 | |
| 90 | if len(sys.argv) > 1: |
Mikel Astiz | 90e7850 | 2012-12-05 13:51:30 +0100 | [diff] [blame] | 91 | path = bluezutils.find_adapter(sys.argv[1]).object_path |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 92 | else: |
Mikel Astiz | 90e7850 | 2012-12-05 13:51:30 +0100 | [diff] [blame] | 93 | path = bluezutils.find_adapter().object_path |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 94 | |
| 95 | media = dbus.Interface(bus.get_object("org.bluez", path), |
Vinicius Costa Gomes | 375e766 | 2012-12-05 20:18:09 -0300 | [diff] [blame] | 96 | "org.bluez.Media1") |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 97 | |
| 98 | path = "/test/endpoint" |
| 99 | endpoint = Endpoint(bus, path) |
Petri Gynther | 7adf8d0 | 2014-01-17 17:45:07 -0800 | [diff] [blame] | 100 | mainloop = GObject.MainLoop() |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 101 | |
| 102 | properties = dbus.Dictionary({ "UUID" : A2DP_SOURCE_UUID, |
| 103 | "Codec" : SBC_CODEC, |
| 104 | "DelayReporting" : True, |
| 105 | "Capabilities" : SBC_CAPABILITIES }) |
| 106 | |
| 107 | if len(sys.argv) > 2: |
| 108 | if sys.argv[2] == "sbcsink": |
| 109 | properties = dbus.Dictionary({ "UUID" : A2DP_SINK_UUID, |
| 110 | "Codec" : SBC_CODEC, |
| 111 | "DelayReporting" : True, |
| 112 | "Capabilities" : SBC_CAPABILITIES }) |
| 113 | if sys.argv[2] == "mp3source": |
| 114 | properties = dbus.Dictionary({ "UUID" : A2DP_SOURCE_UUID, |
| 115 | "Codec" : MP3_CODEC, |
| 116 | "Capabilities" : MP3_CAPABILITIES }) |
| 117 | endpoint.default_configuration(MP3_CONFIGURATION) |
| 118 | if sys.argv[2] == "mp3sink": |
| 119 | properties = dbus.Dictionary({ "UUID" : A2DP_SINK_UUID, |
| 120 | "Codec" : MP3_CODEC, |
| 121 | "Capabilities" : MP3_CAPABILITIES }) |
| 122 | endpoint.default_configuration(MP3_CONFIGURATION) |
| 123 | if sys.argv[2] == "hfpag" or sys.argv[2] == "hspag": |
| 124 | properties = dbus.Dictionary({ "UUID" : HFP_AG_UUID, |
| 125 | "Codec" : PCM_CODEC, |
| 126 | "Capabilities" : PCM_CONFIGURATION }) |
| 127 | endpoint.default_configuration(dbus.Array([])) |
Vinicius Costa Gomes | 803bff2 | 2012-12-03 20:42:28 -0300 | [diff] [blame] | 128 | if sys.argv[2] == "hfphf": |
| 129 | properties = dbus.Dictionary({ "UUID" : HFP_HF_UUID, |
| 130 | "Codec" : CVSD_CODEC, |
| 131 | "Capabilities" : PCM_CONFIGURATION }) |
| 132 | endpoint.default_configuration(dbus.Array([])) |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 133 | |
Steve Langasek | ee56337 | 2012-05-24 22:44:25 -0700 | [diff] [blame] | 134 | print(properties) |
Luiz Augusto von Dentz | 7bc7ee2 | 2010-09-10 10:18:18 +0300 | [diff] [blame] | 135 | |
| 136 | media.RegisterEndpoint(path, properties) |
| 137 | |
| 138 | mainloop.run() |