blob: 78fb5fda502b210eb49eb24b57d38ad3068f2021 [file] [log] [blame]
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +03001#!/usr/bin/python
2
Steve Langasekee563372012-05-24 22:44:25 -07003from __future__ import absolute_import, print_function, unicode_literals
4
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +03005import sys
6import dbus
7import dbus.service
8import dbus.mainloop.glib
Petri Gynther7adf8d02014-01-17 17:45:07 -08009try:
10 from gi.repository import GObject
11except ImportError:
12 import gobject as GObject
Mikel Astiz90e78502012-12-05 13:51:30 +010013import bluezutils
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030014
15A2DP_SOURCE_UUID = "0000110A-0000-1000-8000-00805F9B34FB"
16A2DP_SINK_UUID = "0000110B-0000-1000-8000-00805F9B34FB"
17HFP_AG_UUID = "0000111F-0000-1000-8000-00805F9B34FB"
Vinicius Costa Gomes803bff22012-12-03 20:42:28 -030018HFP_HF_UUID = "0000111E-0000-1000-8000-00805F9B34FB"
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030019HSP_AG_UUID = "00001112-0000-1000-8000-00805F9B34FB"
20
21SBC_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
27SBC_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
29SBC_CONFIGURATION = dbus.Array([dbus.Byte(0x21), dbus.Byte(0x15), dbus.Byte(2), dbus.Byte(32)])
30
31MP3_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
39MP3_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
41MP3_CONFIGURATION = dbus.Array([dbus.Byte(0x21), dbus.Byte(0x02), dbus.Byte(0x00), dbus.Byte(0x80)])
42
43PCM_CODEC = dbus.Byte(0x00)
44PCM_CONFIGURATION = dbus.Array([], signature="ay")
45
Vinicius Costa Gomes803bff22012-12-03 20:42:28 -030046CVSD_CODEC = dbus.Byte(0x01)
47
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030048class Rejected(dbus.DBusException):
49 _dbus_error_name = "org.bluez.Error.Rejected"
50
51class 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 Gomes375e7662012-12-05 20:18:09 -030061 @dbus.service.method("org.bluez.MediaEndpoint1",
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030062 in_signature="", out_signature="")
63 def Release(self):
Steve Langasekee563372012-05-24 22:44:25 -070064 print("Release")
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030065 if self.exit_on_release:
66 mainloop.quit()
67
Vinicius Costa Gomes375e7662012-12-05 20:18:09 -030068 @dbus.service.method("org.bluez.MediaEndpoint1",
Luiz Augusto von Dentz55192ae2016-05-30 12:55:55 +030069 in_signature="o", out_signature="")
70 def ClearConfiguration(self, transport):
71 print("ClearConfiguration (%s)" % (transport))
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030072
Vinicius Costa Gomes375e7662012-12-05 20:18:09 -030073 @dbus.service.method("org.bluez.MediaEndpoint1",
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030074 in_signature="oay", out_signature="")
75 def SetConfiguration(self, transport, config):
Steve Langasekee563372012-05-24 22:44:25 -070076 print("SetConfiguration (%s, %s)" % (transport, config))
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030077 return
78
Vinicius Costa Gomes375e7662012-12-05 20:18:09 -030079 @dbus.service.method("org.bluez.MediaEndpoint1",
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030080 in_signature="ay", out_signature="ay")
81 def SelectConfiguration(self, caps):
Steve Langasekee563372012-05-24 22:44:25 -070082 print("SelectConfiguration (%s)" % (caps))
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030083 return self.configuration
84
85if __name__ == '__main__':
86 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
87
88 bus = dbus.SystemBus()
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030089
90 if len(sys.argv) > 1:
Mikel Astiz90e78502012-12-05 13:51:30 +010091 path = bluezutils.find_adapter(sys.argv[1]).object_path
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030092 else:
Mikel Astiz90e78502012-12-05 13:51:30 +010093 path = bluezutils.find_adapter().object_path
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030094
95 media = dbus.Interface(bus.get_object("org.bluez", path),
Vinicius Costa Gomes375e7662012-12-05 20:18:09 -030096 "org.bluez.Media1")
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +030097
98 path = "/test/endpoint"
99 endpoint = Endpoint(bus, path)
Petri Gynther7adf8d02014-01-17 17:45:07 -0800100 mainloop = GObject.MainLoop()
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +0300101
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 Gomes803bff22012-12-03 20:42:28 -0300128 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 Dentz7bc7ee22010-09-10 10:18:18 +0300133
Steve Langasekee563372012-05-24 22:44:25 -0700134 print(properties)
Luiz Augusto von Dentz7bc7ee22010-09-10 10:18:18 +0300135
136 media.RegisterEndpoint(path, properties)
137
138 mainloop.run()