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 logging
20import sys
21import os
22from bumble.device import (
23    AdvertisingParameters,
24    AdvertisingEventProperties,
25    AdvertisingType,
26    Device,
27)
28from bumble.hci import Address
29
30from bumble.transport import open_transport_or_link
31
32
33# -----------------------------------------------------------------------------
34async def main() -> None:
35    if len(sys.argv) < 3:
36        print(
37            'Usage: run_extended_advertiser.py <config-file> <transport-spec> [type] [address]'
38        )
39        print('example: run_extended_advertiser.py device1.json usb:0')
40        return
41
42    if len(sys.argv) >= 4:
43        advertising_properties = AdvertisingEventProperties.from_advertising_type(
44            AdvertisingType(int(sys.argv[3]))
45        )
46    else:
47        advertising_properties = AdvertisingEventProperties()
48
49    if len(sys.argv) >= 5:
50        peer_address = Address(sys.argv[4])
51    else:
52        peer_address = Address.ANY
53
54    print('<<< connecting to HCI...')
55    async with await open_transport_or_link(sys.argv[2]) as hci_transport:
56        print('<<< connected')
57
58        device = Device.from_config_file_with_hci(
59            sys.argv[1], hci_transport.source, hci_transport.sink
60        )
61        await device.power_on()
62        await device.create_advertising_set(
63            advertising_parameters=AdvertisingParameters(
64                advertising_event_properties=advertising_properties,
65                peer_address=peer_address,
66            )
67        )
68        await hci_transport.source.terminated
69
70
71# -----------------------------------------------------------------------------
72logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper())
73asyncio.run(main())
74