1# Copyright 2021-2023 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
22from bumble.colors import color
23from bumble.hci import Address
24from bumble.device import Device
25from bumble.transport import open_transport_or_link
26from bumble.snoop import BtSnooper
27
28
29# -----------------------------------------------------------------------------
30async def main() -> None:
31    if len(sys.argv) != 3:
32        print('Usage: run_device_with_snooper.py <transport-spec> <snoop-file>')
33        print('example: run_device_with_snooper.py usb:0 btsnoop.log')
34        return
35
36    print('<<< connecting to HCI...')
37    async with await open_transport_or_link(sys.argv[1]) as hci_transport:
38        print('<<< connected')
39
40        device = Device.with_hci(
41            'Bumble',
42            Address('F0:F1:F2:F3:F4:F5'),
43            hci_transport.source,
44            hci_transport.sink,
45        )
46
47        with open(sys.argv[2], "wb") as snoop_file:
48            device.host.snooper = BtSnooper(snoop_file)
49            await device.power_on()
50            await device.start_scanning()
51
52            await hci_transport.source.wait_for_termination()
53
54
55# -----------------------------------------------------------------------------
56logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper())
57asyncio.run(main())
58