| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, print_function, unicode_literals |
| 5 |
|
| 6 |
import sys |
| 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 |
import bluezutils |
| 15 |
|
| 16 |
A2DP_SOURCE_UUID = "0000110A-0000-1000-8000-00805F9B34FB" |
| 17 |
A2DP_SINK_UUID = "0000110B-0000-1000-8000-00805F9B34FB" |
| 18 |
HFP_AG_UUID = "0000111F-0000-1000-8000-00805F9B34FB" |
| 19 |
HFP_HF_UUID = "0000111E-0000-1000-8000-00805F9B34FB" |
| 20 |
HSP_AG_UUID = "00001112-0000-1000-8000-00805F9B34FB" |
| 21 |
PAC_SINK_UUID = "00008f96-0000-1000-8000-00805F9B34FB" |
| 22 |
PAC_SOURCE_UUID = "00008f98-0000-1000-8000-00805F9B34FB" |
| 23 |
|
| 24 |
SBC_CODEC = dbus.Byte(0x00) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
SBC_CAPABILITIES = dbus.Array([dbus.Byte(0xff), dbus.Byte(0xff), dbus.Byte(2), dbus.Byte(64)]) |
| 31 |
|
| 32 |
SBC_CONFIGURATION = dbus.Array([dbus.Byte(0x21), dbus.Byte(0x15), dbus.Byte(2), dbus.Byte(32)]) |
| 33 |
|
| 34 |
MP3_CODEC = dbus.Byte(0x01) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
MP3_CAPABILITIES = dbus.Array([dbus.Byte(0x3f), dbus.Byte(0x07), dbus.Byte(0xff), dbus.Byte(0xfe)]) |
| 43 |
|
| 44 |
MP3_CONFIGURATION = dbus.Array([dbus.Byte(0x21), dbus.Byte(0x02), dbus.Byte(0x00), dbus.Byte(0x80)]) |
| 45 |
|
| 46 |
LC3_CODEC = dbus.Byte(0x06) |
| 47 |
|
| 48 |
|
| 49 |
LC3_CAPABILITIES = dbus.Array([dbus.Byte(16), dbus.Byte(96)]) |
| 50 |
|
| 51 |
PCM_CODEC = dbus.Byte(0x00) |
| 52 |
PCM_CONFIGURATION = dbus.Array([], signature="ay") |
| 53 |
|
| 54 |
CVSD_CODEC = dbus.Byte(0x01) |
| 55 |
|
| 56 |
class Rejected(dbus.DBusException): |
| 57 |
_dbus_error_name = "org.bluez.Error.Rejected" |
| 58 |
|
| 59 |
class Endpoint(dbus.service.Object): |
| 60 |
exit_on_release = True |
| 61 |
configuration = SBC_CONFIGURATION |
| 62 |
|
| 63 |
def set_exit_on_release(self, exit_on_release): |
| 64 |
self.exit_on_release = exit_on_release |
| 65 |
|
| 66 |
def default_configuration(self, configuration): |
| 67 |
self.configuration = configuration |
| 68 |
|
| 69 |
@dbus.service.method("org.bluez.MediaEndpoint1", |
| 70 |
in_signature="", out_signature="") |
| 71 |
def Release(self): |
| 72 |
print("Release") |
| 73 |
if self.exit_on_release: |
| 74 |
mainloop.quit() |
| 75 |
|
| 76 |
@dbus.service.method("org.bluez.MediaEndpoint1", |
| 77 |
in_signature="o", out_signature="") |
| 78 |
def ClearConfiguration(self, transport): |
| 79 |
print("ClearConfiguration (%s)" % (transport)) |
| 80 |
|
| 81 |
@dbus.service.method("org.bluez.MediaEndpoint1", |
| 82 |
in_signature="oay", out_signature="") |
| 83 |
def SetConfiguration(self, transport, config): |
| 84 |
print("SetConfiguration (%s, %s)" % (transport, config)) |
| 85 |
return |
| 86 |
|
| 87 |
@dbus.service.method("org.bluez.MediaEndpoint1", |
| 88 |
in_signature="ay", out_signature="ay") |
| 89 |
def SelectConfiguration(self, caps): |
| 90 |
print("SelectConfiguration (%s)" % (caps)) |
| 91 |
return self.configuration |
| 92 |
|
| 93 |
if __name__ == '__main__': |
| 94 |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 95 |
|
| 96 |
bus = dbus.SystemBus() |
| 97 |
|
| 98 |
if len(sys.argv) > 1: |
| 99 |
path = bluezutils.find_adapter(sys.argv[1]).object_path |
| 100 |
else: |
| 101 |
path = bluezutils.find_adapter().object_path |
| 102 |
|
| 103 |
media = dbus.Interface(bus.get_object("org.bluez", path), |
| 104 |
"org.bluez.Media1") |
| 105 |
|
| 106 |
path = "/test/endpoint" |
| 107 |
endpoint = Endpoint(bus, path) |
| 108 |
mainloop = GObject.MainLoop() |
| 109 |
|
| 110 |
properties = dbus.Dictionary({ "UUID" : A2DP_SOURCE_UUID, |
| 111 |
"Codec" : SBC_CODEC, |
| 112 |
"DelayReporting" : True, |
| 113 |
"Capabilities" : SBC_CAPABILITIES }) |
| 114 |
|
| 115 |
if len(sys.argv) > 2: |
| 116 |
if sys.argv[2] == "sbcsink": |
| 117 |
properties = dbus.Dictionary({ "UUID" : A2DP_SINK_UUID, |
| 118 |
"Codec" : SBC_CODEC, |
| 119 |
"DelayReporting" : True, |
| 120 |
"Capabilities" : SBC_CAPABILITIES }) |
| 121 |
if sys.argv[2] == "mp3source": |
| 122 |
properties = dbus.Dictionary({ "UUID" : A2DP_SOURCE_UUID, |
| 123 |
"Codec" : MP3_CODEC, |
| 124 |
"Capabilities" : MP3_CAPABILITIES }) |
| 125 |
endpoint.default_configuration(MP3_CONFIGURATION) |
| 126 |
if sys.argv[2] == "mp3sink": |
| 127 |
properties = dbus.Dictionary({ "UUID" : A2DP_SINK_UUID, |
| 128 |
"Codec" : MP3_CODEC, |
| 129 |
"Capabilities" : MP3_CAPABILITIES }) |
| 130 |
endpoint.default_configuration(MP3_CONFIGURATION) |
| 131 |
if sys.argv[2] == "hfpag" or sys.argv[2] == "hspag": |
| 132 |
properties = dbus.Dictionary({ "UUID" : HFP_AG_UUID, |
| 133 |
"Codec" : PCM_CODEC, |
| 134 |
"Capabilities" : PCM_CONFIGURATION }) |
| 135 |
endpoint.default_configuration(dbus.Array([])) |
| 136 |
if sys.argv[2] == "hfphf": |
| 137 |
properties = dbus.Dictionary({ "UUID" : HFP_HF_UUID, |
| 138 |
"Codec" : CVSD_CODEC, |
| 139 |
"Capabilities" : PCM_CONFIGURATION }) |
| 140 |
endpoint.default_configuration(dbus.Array([])) |
| 141 |
if sys.argv[2] == "lc3sink": |
| 142 |
properties = dbus.Dictionary({ "UUID" : PAC_SINK_UUID, |
| 143 |
"Codec" : LC3_CODEC, |
| 144 |
"Capabilities" : |
| 145 |
LC3_CAPABILITIES }) |
| 146 |
if sys.argv[2] == "lc3source": |
| 147 |
properties = dbus.Dictionary({ "UUID" : PAC_SOURCE_UUID, |
| 148 |
"Codec" : LC3_CODEC, |
| 149 |
"Capabilities" : |
| 150 |
LC3_CAPABILITIES }) |
| 151 |
|
| 152 |
print(properties) |
| 153 |
|
| 154 |
media.RegisterEndpoint(path, properties) |
| 155 |
|
| 156 |
mainloop.run() |