Debian Codegraph @

test/test-hfp

raw symbols SCIP
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 from __future__ import absolute_import, print_function, unicode_literals
5
6 from optparse import OptionParser, make_option
7 import os
8 from socket import SOCK_SEQPACKET, socket
9 import sys
10 import dbus
11 import dbus.service
12 import dbus.mainloop.glib
13 import glib
14 try:
15 from gi.repository import GObject
16 except ImportError:
17 import gobject as GObject
18
19 mainloop = None
20 audio_supported = True
21
22 try:
23 from socket import AF_BLUETOOTH, BTPROTO_SCO
24 except:
25 print("WARNING: python compiled without Bluetooth support"
26 " - audio will not be available")
27 audio_supported = False
28
29 BUF_SIZE = 1024
30
31 BDADDR_ANY = '00:00:00:00:00:00'
32
33 HF_NREC = 0x0001
34 HF_3WAY = 0x0002
35 HF_CLI = 0x0004
36 HF_VOICE_RECOGNITION = 0x0008
37 HF_REMOTE_VOL = 0x0010
38 HF_ENHANCED_STATUS = 0x0020
39 HF_ENHANCED_CONTROL = 0x0040
40 HF_CODEC_NEGOTIATION = 0x0080
41
42 AG_3WAY = 0x0001
43 AG_NREC = 0x0002
44 AG_VOICE_RECOGNITION = 0x0004
45 AG_INBAND_RING = 0x0008
46 AG_VOICE_TAG = 0x0010
47 AG_REJECT_CALL = 0x0020
48 AG_ENHANCED_STATUS = 0x0040
49 AG_ENHANCED_CONTROL = 0x0080
50 AG_EXTENDED_RESULT = 0x0100
51 AG_CODEC_NEGOTIATION = 0x0200
52
53 HF_FEATURES = (HF_3WAY | HF_CLI | HF_VOICE_RECOGNITION |
54 HF_REMOTE_VOL | HF_ENHANCED_STATUS |
55 HF_ENHANCED_CONTROL | HF_CODEC_NEGOTIATION)
56
57 AVAIL_CODECS = "1,2"
58
59 class HfpConnection:
60 slc_complete = False
61 fd = None
62 io_id = 0
63 version = 0
64 features = 0
65 pending = None
66
67 def disconnect(self):
68 if (self.fd >= 0):
69 os.close(self.fd)
70 self.fd = -1
71 glib.source_remove(self.io_id)
72 self.io_id = 0
73
74 def slc_completed(self):
75 print("SLC establisment complete")
76 self.slc_complete = True
77
78 def slc_next_cmd(self, cmd):
79 if not cmd:
80 self.send_cmd("AT+BRSF=%u" % (HF_FEATURES))
81 elif (cmd.startswith("AT+BRSF")):
82 if (self.features & AG_CODEC_NEGOTIATION and
83 HF_FEATURES & HF_CODEC_NEGOTIATION):
84 self.send_cmd("AT+BAC=%s" % (AVAIL_CODECS))
85 else:
86 self.send_cmd("AT+CIND=?")
87 elif (cmd.startswith("AT+BAC")):
88 self.send_cmd("AT+CIND=?")
89 elif (cmd.startswith("AT+CIND=?")):
90 self.send_cmd("AT+CIND?")
91 elif (cmd.startswith("AT+CIND?")):
92 self.send_cmd("AT+CMER=3,0,0,1")
93 elif (cmd.startswith("AT+CMER=")):
94 if (HF_FEATURES & HF_3WAY and self.features & AG_3WAY):
95 self.send_cmd("AT+CHLD=?")
96 else:
97 self.slc_completed()
98 elif (cmd.startswith("AT+CHLD=?")):
99 self.slc_completed()
100 else:
101 print("Unknown SLC command completed: %s" % (cmd))
102
103 def io_cb(self, fd, cond):
104 buf = os.read(fd, BUF_SIZE)
105 buf = buf.strip()
106
107 print("Received: %s" % (buf))
108
109 if (buf == "OK" or buf == "ERROR"):
110 cmd = self.pending
111 self.pending = None
112
113 if (not self.slc_complete):
114 self.slc_next_cmd(cmd)
115
116 return True
117
118 parts = buf.split(':')
119
120 if (parts[0] == "+BRSF"):
121 self.features = int(parts[1])
122
123 return True
124
125 def send_cmd(self, cmd):
126 if (self.pending):
127 print("ERROR: Another command is pending")
128 return
129
130 print("Sending: %s" % (cmd))
131
132 os.write(self.fd, cmd + "\r\n")
133 self.pending = cmd
134
135 def __init__(self, fd, version, features):
136 self.fd = fd
137 self.version = version
138 self.features = features
139
140 print("Version 0x%04x Features 0x%04x" % (version, features))
141
142 self.io_id = glib.io_add_watch(fd, glib.IO_IN, self.io_cb)
143
144 self.slc_next_cmd(None)
145
146 class HfpProfile(dbus.service.Object):
147 sco_socket = None
148 io_id = 0
149 conns = {}
150
151 def sco_cb(self, sock, cond):
152 (sco, peer) = sock.accept()
153 print("New SCO connection from %s" % (peer))
154
155 def init_sco(self, sock):
156 self.sco_socket = sock
157 self.io_id = glib.io_add_watch(sock, glib.IO_IN, self.sco_cb)
158
159 def __init__(self, bus, path, sco):
160 dbus.service.Object.__init__(self, bus, path)
161
162 if sco:
163 self.init_sco(sco)
164
165 @dbus.service.method("org.bluez.Profile1",
166 in_signature="", out_signature="")
167 def Release(self):
168 print("Release")
169 mainloop.quit()
170
171 @dbus.service.method("org.bluez.Profile1",
172 in_signature="", out_signature="")
173 def Cancel(self):
174 print("Cancel")
175
176 @dbus.service.method("org.bluez.Profile1",
177 in_signature="o", out_signature="")
178 def RequestDisconnection(self, path):
179 conn = self.conns.pop(path)
180 conn.disconnect()
181
182 @dbus.service.method("org.bluez.Profile1",
183 in_signature="oha{sv}", out_signature="")
184 def NewConnection(self, path, fd, properties):
185 fd = fd.take()
186 version = 0x0105
187 features = 0
188 print("NewConnection(%s, %d)" % (path, fd))
189 for key in properties.keys():
190 if key == "Version":
191 version = properties[key]
192 elif key == "Features":
193 features = properties[key]
194
195 conn = HfpConnection(fd, version, features)
196
197 self.conns[path] = conn
198
199 if __name__ == '__main__':
200 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
201
202 bus = dbus.SystemBus()
203
204 manager = dbus.Interface(bus.get_object("org.bluez",
205 "/org/bluez"), "org.bluez.ProfileManager1")
206
207 option_list = [
208 make_option("-p", "--path", action="store",
209 type="string", dest="path",
210 default="/bluez/test/hfp"),
211 make_option("-n", "--name", action="store",
212 type="string", dest="name",
213 default=None),
214 make_option("-C", "--channel", action="store",
215 type="int", dest="channel",
216 default=None),
217 ]
218
219 parser = OptionParser(option_list=option_list)
220
221 (options, args) = parser.parse_args()
222
223 mainloop = GObject.MainLoop()
224
225 opts = {
226 "Version" : dbus.UInt16(0x0106),
227 "Features" : dbus.UInt16(HF_FEATURES),
228 }
229
230 if (options.name):
231 opts["Name"] = options.name
232
233 if (options.channel is not None):
234 opts["Channel"] = dbus.UInt16(options.channel)
235
236 if audio_supported:
237 sco = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO)
238 sco.bind(BDADDR_ANY)
239 sco.listen(1)
240 else:
241 sco = None
242
243 profile = HfpProfile(bus, options.path, sco)
244
245 manager.RegisterProfile(options.path, "hfp-hf", opts)
246
247 print("Profile registered - waiting for connections")
248
249 mainloop.run()