1# Copyright 2021-2022 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15# ----------------------------------------------------------------------------- 16# Imports 17# ----------------------------------------------------------------------------- 18import asyncio 19import sys 20import os 21import logging 22import websockets 23 24from typing import Optional 25 26from bumble import decoder 27from bumble import gatt 28from bumble.core import AdvertisingData 29from bumble.device import Device, AdvertisingParameters 30from bumble.transport import open_transport_or_link 31from bumble.profiles import asha 32 33ws_connection: Optional[websockets.WebSocketServerProtocol] = None 34g722_decoder = decoder.G722Decoder() 35 36 37async def ws_server(ws_client: websockets.WebSocketServerProtocol, path: str): 38 del path 39 global ws_connection 40 ws_connection = ws_client 41 42 async for message in ws_client: 43 print(message) 44 45 46# ----------------------------------------------------------------------------- 47async def main() -> None: 48 if len(sys.argv) != 3: 49 print('Usage: python run_asha_sink.py <device-config> <transport-spec>') 50 print('example: python run_asha_sink.py device1.json usb:0') 51 return 52 53 async with await open_transport_or_link(sys.argv[2]) as hci_transport: 54 device = Device.from_config_file_with_hci( 55 sys.argv[1], hci_transport.source, hci_transport.sink 56 ) 57 58 def on_audio_packet(packet: bytes) -> None: 59 global ws_connection 60 if ws_connection: 61 offset = 1 62 while offset < len(packet): 63 pcm_data = g722_decoder.decode_frame(packet[offset : offset + 80]) 64 offset += 80 65 asyncio.get_running_loop().create_task(ws_connection.send(pcm_data)) 66 else: 67 logging.info("No active client") 68 69 asha_service = asha.AshaService( 70 capability=0, 71 hisyncid=b'\x01\x02\x03\x04\x05\x06\x07\x08', 72 device=device, 73 audio_sink=on_audio_packet, 74 ) 75 device.add_service(asha_service) 76 77 # Set the advertising data 78 advertising_data = ( 79 bytes( 80 AdvertisingData( 81 [ 82 ( 83 AdvertisingData.COMPLETE_LOCAL_NAME, 84 bytes(device.name, 'utf-8'), 85 ), 86 (AdvertisingData.FLAGS, bytes([0x06])), 87 ( 88 AdvertisingData.INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 89 bytes(gatt.GATT_ASHA_SERVICE), 90 ), 91 ] 92 ) 93 ) 94 + asha_service.get_advertising_data() 95 ) 96 97 # Go! 98 await device.power_on() 99 await device.create_advertising_set( 100 auto_restart=True, 101 advertising_data=advertising_data, 102 advertising_parameters=AdvertisingParameters( 103 primary_advertising_interval_min=100, 104 primary_advertising_interval_max=100, 105 ), 106 ) 107 108 await websockets.serve(ws_server, port=8888) 109 110 await hci_transport.source.terminated 111 112 113# ----------------------------------------------------------------------------- 114logging.basicConfig( 115 level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper(), 116 format='%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s', 117 datefmt='%Y-%m-%d %H:%M:%S', 118) 119asyncio.run(main()) 120