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 logging 19import asyncio 20import sys 21import os 22 23from bumble.gatt import ( 24 GATT_CHARACTERISTIC_USER_DESCRIPTION_DESCRIPTOR, 25 GATT_DEVICE_INFORMATION_SERVICE, 26 GATT_MANUFACTURER_NAME_STRING_CHARACTERISTIC, 27 Characteristic, 28 Descriptor, 29 Service, 30) 31from bumble.device import Device 32from bumble.host import Host 33from bumble.controller import Controller 34from bumble.link import LocalLink 35from bumble.transport import open_transport_or_link 36 37 38# ----------------------------------------------------------------------------- 39async def main(): 40 if len(sys.argv) != 4: 41 print( 42 'Usage: run_controller.py <controller-address> <device-config> ' 43 '<transport-spec>' 44 ) 45 print( 46 'example: run_controller.py F2:F3:F4:F5:F6:F7 device1.json ' 47 'udp:0.0.0.0:22333,172.16.104.161:22333' 48 ) 49 return 50 51 print('>>> connecting to HCI...') 52 async with await open_transport_or_link(sys.argv[3]) as (hci_source, hci_sink): 53 print('>>> connected') 54 55 # Create a local link 56 link = LocalLink() 57 58 # Create a first controller using the packet source/sink as its host interface 59 controller1 = Controller( 60 'C1', host_source=hci_source, host_sink=hci_sink, link=link 61 ) 62 controller1.random_address = sys.argv[1] 63 64 # Create a second controller using the same link 65 controller2 = Controller('C2', link=link) 66 67 # Create a host for the second controller 68 host = Host() 69 host.controller = controller2 70 71 # Create a device to manage the host 72 device = Device.from_config_file(sys.argv[2]) 73 device.host = host 74 75 # Add some basic services to the device's GATT server 76 descriptor = Descriptor( 77 GATT_CHARACTERISTIC_USER_DESCRIPTION_DESCRIPTOR, 78 Descriptor.READABLE, 79 'My Description', 80 ) 81 manufacturer_name_characteristic = Characteristic( 82 GATT_MANUFACTURER_NAME_STRING_CHARACTERISTIC, 83 Characteristic.READ, 84 Characteristic.READABLE, 85 "Fitbit", 86 [descriptor], 87 ) 88 device_info_service = Service( 89 GATT_DEVICE_INFORMATION_SERVICE, [manufacturer_name_characteristic] 90 ) 91 device.add_service(device_info_service) 92 93 # Debug print 94 for attribute in device.gatt_server.attributes: 95 print(attribute) 96 97 await device.power_on() 98 await device.start_advertising() 99 await device.start_scanning() 100 101 await hci_source.wait_for_termination() 102 103 104# ----------------------------------------------------------------------------- 105logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper()) 106asyncio.run(main()) 107