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