-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
315 lines (288 loc) · 13.8 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/python3
# Copyright (c) 2017 Johannes Leupolz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from pydbus import SessionBus
from pydbus import SystemBus
import asyncio, gbulb
from gi.repository.GLib import GError
#from hbmqtt.client import MQTTClient, ClientException
import paho.mqtt.client as mqtt
import subprocess
import os
import signal
import re
class BluetoothAudioBridge:
def __init__(self, loop):
self.loop = loop
self.DbusPulseAudioPath=""
self.DbusBluezOnSystemBus=True
self.DbusBluezBusName="org.bluez"
self.DbusBluezObjectPath="/org/bluez/hci0"
self.DbusBluezObject=None
self.DbusBluezReceivingFuture=None
self.DbusBluezDiscoveredDevices={}
self.DbusBluezUUIDsOfDevices={}
self.DbusBluezConnectedDevices={}
self.MqttPath="/BluetoothAudioBridge"
self.MqttServer="localhost"
self.MqttUsername="vhost:username"
self.MqttPassword="password"
self.MqttClient=None
self.MqttMessageQueue=asyncio.Queue()
self.MqttReceivingFuture=None
self.Continue=True
self.CancellationToken=self.loop.create_future()
self.TraceLevel=0
self.PollingCycle=3
self.mqttReceivedConnect=self.makeConnect
self.mqttReceivedPairAndTrust=self.makePairAndTrust
self.mqttReceivedScan=self.makeScan
self.dbusBtDeviceDetected=self.btDeviceDetected
self.dbusBtDeviceRemoved=self.btDeviceRemoved
self.dbusBtDeviceConnected=self.btDeviceConnected
self.dbusBtDeviceDisconnected=self.btDeviceDisconnected
self.dbusScanProcesses=0
self.btDeviceConfig = {}
self.btRunningProcesses = {}
def loadConfig(self,appConfig):
self.TraceLevel=appConfig["traceLevel"]
self.PollingCycle=appConfig["pollingCycle"]
self.btDeviceConfig = appConfig["bluetoothDevices"]
def trace(self,level,msg):
if self.TraceLevel >= level:
print(msg)
async def awaitOrStop(self,future):
# currently unused
done,pending = await asyncio.wait([self.CancellationToken, future],return_when=asyncio.FIRST_COMPLETED)
firstFinished=next(iter(done))
if firstFinished==self.CancellationToken:
#Note: pending tasks are still running
return (False,None)
#print(firstFinished)
#print(firstFinished.result())
return (True,firstFinished.result())
def makeConnect(self,message):
self.trace(0,"MQTT: received connect")
def makePairAndTrust(self,message):
self.trace(0,"MQTT: received pair and trust")
def makeScan(self,message):
self.scanProcesses=self.scanProcesses+1
self.trace(0,"MQTT: received scan")
asyncio.ensure_future(self.stopScanningIn30Seconds)
async def stopScanningIn30Seconds(self):
await asyncio.sleep(30)
self.scanProcesses=self.scanProcesses-1
if (self.scanProcesses==0):
self.trace(2,"stop scanning for devices")
async def mqttProcessMessages(self):
while self.Continue:
message=await self.MqttMessageQueue.get()
if message==None:
self.trace(0,"stopping message proccessing")
return
self.trace(1,"MQTT: received message")
if message.startswith("Connect"):
self.mqttReceivedConnect(message)
if message.startswith("Pair and trust"):
self.mqttReceivedConnect(message)
if message.startswith("Scan"):
self.mqttReceivedScan(message)
async def registerMqtt(self):
def on_connect(client, userdata, flags, rc):
self.trace(0,"Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("/BluetoothAudioBridge/commands")
def on_message(client, userdata, msg):
self.trace(1,msg.topic+" "+str(msg.payload))
msgDecoded=msg.payload.decode("utf-8")
asyncio.ensure_future(self.MqttMessageQueue.put(msgDecoded))
async def mqttReceiving():
while self.Continue:
self.trace(3,"MQTT: wait for message")
client.loop_read()
client.loop_write()
client.loop_misc()
await asyncio.sleep(0.1)
client.disconnect()
client.loop_read()
client.loop_write()
client.loop_misc()
self.MqttReceivingFuture.set_result(True)
asyncio.ensure_future(self.MqttMessageQueue.put(None)) # add final (empty) message into queue for a clean shutdown
def on_disconnect(client, userdata, rc):
if rc != 0:
self.trace(0,"Unexpected disconnection.")
client = mqtt.Client(client_id="thing-bluetoothbridge",)
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.username_pw_set(self.MqttUsername, password=self.MqttPassword)
client.connect(self.MqttServer, 1883, 60)
#register receiver
self.MqttReceivingFuture=self.loop.create_future()
asyncio.ensure_future(self.mqttProcessMessages())
asyncio.ensure_future(mqttReceiving())
self.trace(0,"registered on MQTT")
async def btDeviceDetected(self,address):
self.trace(0,"device detected "+address)
async def btDeviceRemoved(self,address):
self.trace(0,"device removed "+address)
def btClassIsAudio(self,btClass):
# https://www.bluetooth.com/specifications/assigned-numbers/baseband
major_service_audio_bit = 1<<21
major_device_audio_bit = 1<<10
is_audio_service = (major_service_audio_bit & btClass)==major_service_audio_bit
is_audio_device = (major_device_audio_bit & btClass)==major_device_audio_bit
return is_audio_service and is_audio_device
def btDeviceHasA2DPSink(self,uuids):
# https://github.com/pauloborges/bluez/blob/master/lib/uuid.h
if "0000110b-0000-1000-8000-00805f9b34fb" in uuids:
return True
return False
def stdoutOfPopen(self):
if self.TraceLevel < 3:
return subprocess.DEVNULL
return None
async def btDeviceConnected(self,address):
self.trace(0,"device connected "+address)
if address in self.btRunningProcesses:
processGroupToKill=self.btRunningProcesses[address].pid
os.killpg(os.getpgid(processGroupToKill), signal.SIGTERM)
await asyncio.sleep(1)
os.killpg(os.getpgid(processGroupToKill), signal.SIGKILL)
self.btRunningProcesses.pop(address,None)
deviceConfig=None
if address in self.btDeviceConfig:
deviceConfig = self.btDeviceConfig[address]
else:
uuids=self.DbusBluezUUIDsOfDevices[address]
if self.btDeviceHasA2DPSink(uuids) and "other_a2dp_sinks" in self.btDeviceConfig:
deviceConfig=self.btDeviceConfig["other_a2dp_sinks"]
if deviceConfig!=None:
if "onConnectCommand" in deviceConfig:
command=deviceConfig["onConnectCommand"]
if command:
commandToExecute=command.replace("$DEVICE",address)
self.btRunningProcesses[address]=subprocess.Popen(commandToExecute,shell=True, start_new_session=True,stdout=self.stdoutOfPopen(),stderr=self.stdoutOfPopen())
async def btDeviceDisconnected(self,address):
self.trace(0,"device disconnected "+address)
if address in self.btRunningProcesses:
processGroupToKill=self.btRunningProcesses[address].pid
os.killpg(os.getpgid(processGroupToKill), signal.SIGTERM)
await asyncio.sleep(1)
os.killpg(os.getpgid(processGroupToKill), signal.SIGKILL)
self.btRunningProcesses.pop(address,None)
deviceConfig=None
if address in self.btDeviceConfig:
deviceConfig = self.btDeviceConfig[address]
else:
uuids=self.DbusBluezUUIDsOfDevices[address]
if self.btDeviceHasA2DPSink(uuids) and "other_a2dp_sinks" in self.btDeviceConfig:
deviceConfig=self.btDeviceConfig["other_a2dp_sinks"]
if deviceConfig!=None:
if "onDisconnectCommand" in deviceConfig:
command=deviceConfig["onDisconnectCommand"]
if command:
commandToExecute=command.replace("$DEVICE",address)
self.btRunningProcesses[address]=subprocess.Popen(commandToExecute,shell=True, start_new_session=True,stdout=self.stdoutOfPopen(),stderr=self.stdoutOfPopen())
async def lookForDbusChanges(self):
deviceFilter = re.compile("^[/]\w+[/]\w+[/]\w+[/]dev_(?P<btmac>\w+)$")
while self.Continue:
self.trace(3,"DBUS: wait for device")
try:
self.trace(1,"DBUS: GetManagedObjects()")
managedObjects = await self.loop.run_in_executor(None, lambda: self.DbusBluezRootNode.GetManagedObjects())
await asyncio.sleep(0.5) # give PulseAudio a chance of connecting (not sure if necessary)
foundDevices={}
for objPath,obj in managedObjects.items():
match = deviceFilter.match(objPath)
if match:
btmac=match.group("btmac")
dev=obj[self.DbusBluezBusName+".Device1"]
foundDevices[btmac]=dev
self.trace(3,"Found "+str(len(foundDevices))+" devices")
removeDevices=[]
for oldDevice in self.DbusBluezDiscoveredDevices:
if oldDevice not in foundDevices:
removeDevices.append(oldDevice)
await self.dbusBtDeviceRemoved(oldDevice)
for removeDevice in removeDevices:
self.DbusBluezDiscoveredDevices.pop(removeDevice,None)
for foundDevice in foundDevices:
if foundDevice not in self.DbusBluezDiscoveredDevices:
self.DbusBluezDiscoveredDevices[foundDevice]=True
await self.dbusBtDeviceDetected(foundDevice)
# now check disconnect <-> connect
connectedDevices = {}
for foundDevice,dev in foundDevices.items():
if foundDevice not in self.DbusBluezUUIDsOfDevices:
self.DbusBluezUUIDsOfDevices[foundDevice] = dev["UUIDs"]
isConnected = dev["Connected"]
if isConnected :
connectedDevices[foundDevice]=True
disconnectedDevices=[]
for alreadyConnectedDevice in self.DbusBluezConnectedDevices:
if alreadyConnectedDevice not in connectedDevices:
disconnectedDevices.append(alreadyConnectedDevice)
await self.dbusBtDeviceDisconnected(alreadyConnectedDevice)
for disconnectedDevice in disconnectedDevices:
self.DbusBluezConnectedDevices.pop(disconnectedDevice,None)
for connectedDevice in connectedDevices:
if connectedDevice not in self.DbusBluezConnectedDevices:
self.DbusBluezConnectedDevices[connectedDevice]=True
await self.dbusBtDeviceConnected(connectedDevice)
except KeyError as err:
self.trace(0,"dbus error (KeyError)")
print(err)
self.trace(0,err)
except GError as err:
self.trace(0,"dbus error (GError)")
self.trace (0,err)
await asyncio.sleep(self.PollingCycle)
print("finished looking for dbus changes")
self.DbusBluezReceivingFuture.set_result(True)
async def registerDbus(self):
try:
if self.DbusBluezOnSystemBus:
self.DbusBluezObject = SystemBus()
else:
self.DbusBluezObject = SessionBus()
self.trace(0,"listening on D-BUS")
self.DbusBluezRootNode = self.DbusBluezObject.get(self.DbusBluezBusName,"/")
self.trace(0,"connected to org.bluez")
except GError as err:
self.trace(0,"dbus error (register)")
self.trace (0,err)
self.DbusBluezRootNode=None
if self.DbusBluezRootNode:
self.DbusBluezReceivingFuture=self.loop.create_future()
asyncio.ensure_future(self.lookForDbusChanges())
async def register(self):
await self.registerMqtt()
await self.registerDbus()
async def unregister(self):
self.Continue=False
if (self.DbusBluezReceivingFuture):
await self.DbusBluezReceivingFuture
self.DbusBluezReceivingFuture = None
if (self.MqttReceivingFuture):
await self.MqttReceivingFuture
self.MqttReceivingFuture=None