File: link.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 (149 lines) | stat: -rw-r--r-- 5,403 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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.
from __future__ import annotations

import asyncio

# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
import logging
from typing import TYPE_CHECKING

from bumble import core, hci, ll, lmp

if TYPE_CHECKING:
    from bumble import controller

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


# -----------------------------------------------------------------------------
# TODO: add more support for various LL exchanges
# (see Vol 6, Part B - 2.4 DATA CHANNEL PDU)
# -----------------------------------------------------------------------------
class LocalLink:
    '''
    Link bus for controllers to communicate with each other
    '''

    controllers: set[controller.Controller]

    def __init__(self):
        self.controllers = set()
        self.pending_classic_connection = None

    ############################################################
    # Common utils
    ############################################################

    def add_controller(self, controller: controller.Controller):
        logger.debug(f'new controller: {controller}')
        self.controllers.add(controller)

    def remove_controller(self, controller: controller.Controller):
        self.controllers.remove(controller)

    def find_le_controller(self, address: hci.Address) -> controller.Controller | None:
        for controller in self.controllers:
            for connection in controller.le_connections.values():
                if connection.self_address == address:
                    return controller
        return None

    def find_classic_controller(
        self, address: hci.Address
    ) -> controller.Controller | None:
        for controller in self.controllers:
            if controller.public_address == address:
                return controller
        return None

    ############################################################
    # LE handlers
    ############################################################

    def on_address_changed(self, controller):
        pass

    def send_acl_data(
        self,
        sender_controller: controller.Controller,
        destination_address: hci.Address,
        transport: core.PhysicalTransport,
        data: bytes,
    ):
        # Send the data to the first controller with a matching address
        if transport == core.PhysicalTransport.LE:
            destination_controller = self.find_le_controller(destination_address)
            source_address = sender_controller.random_address
        elif transport == core.PhysicalTransport.BR_EDR:
            destination_controller = self.find_classic_controller(destination_address)
            source_address = sender_controller.public_address
        else:
            raise ValueError("unsupported transport type")

        if destination_controller is not None:
            asyncio.get_running_loop().call_soon(
                lambda: destination_controller.on_link_acl_data(
                    source_address, transport, data
                )
            )

    def send_advertising_pdu(
        self,
        sender_controller: controller.Controller,
        packet: ll.AdvertisingPdu,
    ):
        loop = asyncio.get_running_loop()
        for c in self.controllers:
            if c != sender_controller:
                loop.call_soon(c.on_ll_advertising_pdu, packet)

    def send_ll_control_pdu(
        self,
        sender_address: hci.Address,
        receiver_address: hci.Address,
        packet: ll.ControlPdu,
    ):
        if not (receiver_controller := self.find_le_controller(receiver_address)):
            raise core.InvalidArgumentError(
                f"Unable to find controller for address {receiver_address}"
            )
        asyncio.get_running_loop().call_soon(
            lambda: receiver_controller.on_ll_control_pdu(sender_address, packet)
        )

    ############################################################
    # Classic handlers
    ############################################################

    def send_lmp_packet(
        self,
        sender_controller: controller.Controller,
        receiver_address: hci.Address,
        packet: lmp.Packet,
    ):
        if not (receiver_controller := self.find_classic_controller(receiver_address)):
            raise core.InvalidArgumentError(
                f"Unable to find controller for address {receiver_address}"
            )
        asyncio.get_running_loop().call_soon(
            lambda: receiver_controller.on_lmp_packet(
                sender_controller.public_address, packet
            )
        )