File: hci_bridge.py

package info (click to toggle)
python-bumble 0.0.225-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 9,464 kB
  • sloc: python: 75,258; java: 3,782; javascript: 823; xml: 203; sh: 172; makefile: 8
file content (112 lines) | stat: -rw-r--r-- 4,080 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2021-2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import asyncio

# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
import logging
import sys

import bumble.logging
from bumble import hci, transport
from bumble.bridge import HCI_Bridge

# -----------------------------------------------------------------------------
# Logging
# -----------------------------------------------------------------------------
logger = logging.getLogger(__name__)


# -----------------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------------
async def async_main():
    if len(sys.argv) < 3:
        print(
            'Usage: hci_bridge.py <host-transport-spec> <controller-transport-spec> '
            '[command-short-circuit-list]'
        )
        print(
            'example: python hci_bridge.py udp:0.0.0.0:9000,127.0.0.1:9001 '
            'serial:/dev/tty.usbmodem0006839912171,1000000 '
            '0x3f:0x0070,0x3f:0x0074,0x3f:0x0077,0x3f:0x0078'
        )
        return

    print('>>> connecting to HCI...')
    async with await transport.open_transport(sys.argv[1]) as (
        hci_host_source,
        hci_host_sink,
    ):
        print('>>> connected')

        print('>>> connecting to HCI...')
        async with await transport.open_transport(sys.argv[2]) as (
            hci_controller_source,
            hci_controller_sink,
        ):
            print('>>> connected')

            command_short_circuits = []
            if len(sys.argv) >= 4:
                for op_code_str in sys.argv[3].split(','):
                    if ':' in op_code_str:
                        ogf, ocf = op_code_str.split(':')
                        command_short_circuits.append(
                            hci.hci_command_op_code(int(ogf, 16), int(ocf, 16))
                        )
                    else:
                        command_short_circuits.append(int(op_code_str, 16))

            def host_to_controller_filter(hci_packet):
                if (
                    hci_packet.hci_packet_type == hci.HCI_COMMAND_PACKET
                    and hci_packet.op_code in command_short_circuits
                ):
                    # Respond with a success response
                    logger.debug('short-circuiting packet')
                    response = hci.HCI_Command_Complete_Event(
                        num_hci_command_packets=1,
                        command_opcode=hci_packet.op_code,
                        return_parameters=hci.HCI_StatusReturnParameters(
                            status=hci.HCI_ErrorCode.SUCCESS
                        ),
                    )
                    # Return a packet with 'respond to sender' set to True
                    return (bytes(response), True)

                return None

            _ = HCI_Bridge(
                hci_host_source,
                hci_host_sink,
                hci_controller_source,
                hci_controller_sink,
                host_to_controller_filter,
                None,
            )
            await asyncio.get_running_loop().create_future()


# -----------------------------------------------------------------------------
def main():
    bumble.logging.setup_basic_logging()
    asyncio.run(async_main())


# -----------------------------------------------------------------------------
if __name__ == '__main__':
    main()