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() -> None:
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_transport:
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',
61            host_source=hci_transport.source,
62            host_sink=hci_transport.sink,
63            link=link,
64        )
65        controller1.random_address = sys.argv[1]
66
67        # Create a second controller using the same link
68        controller2 = Controller('C2', link=link)
69
70        # Create a host for the second controller
71        host = Host()
72        host.controller = controller2
73
74        # Create a device to manage the host
75        device = Device.from_config_file(sys.argv[2])
76        device.host = host
77
78        # Add some basic services to the device's GATT server
79        descriptor = Descriptor(
80            GATT_CHARACTERISTIC_USER_DESCRIPTION_DESCRIPTOR,
81            Descriptor.READABLE,
82            'My Description',
83        )
84        manufacturer_name_characteristic = Characteristic(
85            GATT_MANUFACTURER_NAME_STRING_CHARACTERISTIC,
86            Characteristic.Properties.READ,
87            Characteristic.READABLE,
88            "Fitbit",
89            [descriptor],
90        )
91        device_info_service = Service(
92            GATT_DEVICE_INFORMATION_SERVICE, [manufacturer_name_characteristic]
93        )
94        device.add_service(device_info_service)
95
96        # Debug print
97        for attribute in device.gatt_server.attributes:
98            print(attribute)
99
100        await device.power_on()
101        await device.start_advertising()
102        await device.start_scanning()
103
104        await hci_transport.source.wait_for_termination()
105
106
107# -----------------------------------------------------------------------------
108logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper())
109asyncio.run(main())
110