3 from __future__ import absolute_import, print_function, unicode_literals
5 from optparse import OptionParser, make_option
7 from socket import SOCK_SEQPACKET, socket
11 import dbus.mainloop.glib
14 from gi.repository import GObject
16 import gobject as GObject
19 audio_supported = True
22 from socket import AF_BLUETOOTH, BTPROTO_SCO
24 print("WARNING: python compiled without Bluetooth support"
25 " - audio will not be available")
26 audio_supported = False
30 BDADDR_ANY = '00:00:00:00:00:00'
35 HF_VOICE_RECOGNITION = 0x0008
36 HF_REMOTE_VOL = 0x0010
37 HF_ENHANCED_STATUS = 0x0020
38 HF_ENHANCED_CONTROL = 0x0040
39 HF_CODEC_NEGOTIATION = 0x0080
43 AG_VOICE_RECOGNITION = 0x0004
44 AG_INBAND_RING = 0x0008
46 AG_REJECT_CALL = 0x0020
47 AG_ENHANCED_STATUS = 0x0040
48 AG_ENHANCED_CONTROL = 0x0080
49 AG_EXTENDED_RESULT = 0x0100
50 AG_CODEC_NEGOTIATION = 0x0200
52 HF_FEATURES = (HF_3WAY | HF_CLI | HF_VOICE_RECOGNITION |
53 HF_REMOTE_VOL | HF_ENHANCED_STATUS |
54 HF_ENHANCED_CONTROL | HF_CODEC_NEGOTIATION)
70 glib.source_remove(self.io_id)
73 def slc_completed(self):
74 print("SLC establisment complete")
75 self.slc_complete = True
77 def slc_next_cmd(self, cmd):
79 self.send_cmd("AT+BRSF=%u" % (HF_FEATURES))
80 elif (cmd.startswith("AT+BRSF")):
81 if (self.features & AG_CODEC_NEGOTIATION and
82 HF_FEATURES & HF_CODEC_NEGOTIATION):
83 self.send_cmd("AT+BAC=%s" % (AVAIL_CODECS))
85 self.send_cmd("AT+CIND=?")
86 elif (cmd.startswith("AT+BAC")):
87 self.send_cmd("AT+CIND=?")
88 elif (cmd.startswith("AT+CIND=?")):
89 self.send_cmd("AT+CIND?")
90 elif (cmd.startswith("AT+CIND?")):
91 self.send_cmd("AT+CMER=3,0,0,1")
92 elif (cmd.startswith("AT+CMER=")):
93 if (HF_FEATURES & HF_3WAY and self.features & AG_3WAY):
94 self.send_cmd("AT+CHLD=?")
97 elif (cmd.startswith("AT+CHLD=?")):
100 print("Unknown SLC command completed: %s" % (cmd))
102 def io_cb(self, fd, cond):
103 buf = os.read(fd, BUF_SIZE)
106 print("Received: %s" % (buf))
108 if (buf == "OK" or buf == "ERROR"):
112 if (not self.slc_complete):
113 self.slc_next_cmd(cmd)
117 parts = buf.split(':')
119 if (parts[0] == "+BRSF"):
120 self.features = int(parts[1])
124 def send_cmd(self, cmd):
126 print("ERROR: Another command is pending")
129 print("Sending: %s" % (cmd))
131 os.write(self.fd, cmd + "\r\n")
134 def __init__(self, fd, version, features):
136 self.version = version
137 self.features = features
139 print("Version 0x%04x Features 0x%04x" % (version, features))
141 self.io_id = glib.io_add_watch(fd, glib.IO_IN, self.io_cb)
143 self.slc_next_cmd(None)
145 class HfpProfile(dbus.service.Object):
150 def sco_cb(self, sock, cond):
151 (sco, peer) = sock.accept()
152 print("New SCO connection from %s" % (peer))
154 def init_sco(self, sock):
155 self.sco_socket = sock
156 self.io_id = glib.io_add_watch(sock, glib.IO_IN, self.sco_cb)
158 def __init__(self, bus, path, sco):
159 dbus.service.Object.__init__(self, bus, path)
164 @dbus.service.method("org.bluez.Profile1",
165 in_signature="", out_signature="")
170 @dbus.service.method("org.bluez.Profile1",
171 in_signature="", out_signature="")
175 @dbus.service.method("org.bluez.Profile1",
176 in_signature="o", out_signature="")
177 def RequestDisconnection(self, path):
178 conn = self.conns.pop(path)
181 @dbus.service.method("org.bluez.Profile1",
182 in_signature="oha{sv}", out_signature="")
183 def NewConnection(self, path, fd, properties):
187 print("NewConnection(%s, %d)" % (path, fd))
188 for key in properties.keys():
190 version = properties[key]
191 elif key == "Features":
192 features = properties[key]
194 conn = HfpConnection(fd, version, features)
196 self.conns[path] = conn
198 if __name__ == '__main__':
199 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
201 bus = dbus.SystemBus()
203 manager = dbus.Interface(bus.get_object("org.bluez",
204 "/org/bluez"), "org.bluez.ProfileManager1")
207 make_option("-p", "--path", action="store",
208 type="string", dest="path",
209 default="/bluez/test/hfp"),
210 make_option("-n", "--name", action="store",
211 type="string", dest="name",
213 make_option("-C", "--channel", action="store",
214 type="int", dest="channel",
218 parser = OptionParser(option_list=option_list)
220 (options, args) = parser.parse_args()
222 mainloop = GObject.MainLoop()
225 "Version" : dbus.UInt16(0x0106),
226 "Features" : dbus.UInt16(HF_FEATURES),
230 opts["Name"] = options.name
232 if (options.channel is not None):
233 opts["Channel"] = dbus.UInt16(options.channel)
236 sco = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO)
242 profile = HfpProfile(bus, options.path, sco)
244 manager.RegisterProfile(options.path, "hfp-hf", opts)
246 print("Profile registered - waiting for connections")