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 19 20from .hci import HCI_Packet 21from .helpers import PacketTracer 22 23# ----------------------------------------------------------------------------- 24# Logging 25# ----------------------------------------------------------------------------- 26logger = logging.getLogger(__name__) 27 28 29# ----------------------------------------------------------------------------- 30class HCI_Bridge: 31 class Forwarder: 32 def __init__(self, hci_sink, sender_hci_sink, packet_filter, trace): 33 self.hci_sink = hci_sink 34 self.sender_hci_sink = sender_hci_sink 35 self.packet_filter = packet_filter 36 self.trace = trace 37 38 def on_packet(self, packet): 39 # Convert the packet bytes to an object 40 hci_packet = HCI_Packet.from_bytes(packet) 41 42 # Filter the packet 43 if self.packet_filter is not None: 44 filtered = self.packet_filter(hci_packet) 45 if filtered is not None: 46 packet, respond_to_sender = filtered 47 hci_packet = HCI_Packet.from_bytes(packet) 48 if respond_to_sender: 49 self.sender_hci_sink.on_packet(packet) 50 return 51 52 # Analyze the packet 53 self.trace(hci_packet) 54 55 # Bridge the packet 56 self.hci_sink.on_packet(packet) 57 58 def __init__( 59 self, 60 hci_host_source, 61 hci_host_sink, 62 hci_controller_source, 63 hci_controller_sink, 64 host_to_controller_filter=None, 65 controller_to_host_filter=None, 66 ): 67 tracer = PacketTracer(emit_message=logger.info) 68 host_to_controller_forwarder = HCI_Bridge.Forwarder( 69 hci_controller_sink, 70 hci_host_sink, 71 host_to_controller_filter, 72 lambda packet: tracer.trace(packet, 0), 73 ) 74 hci_host_source.set_packet_sink(host_to_controller_forwarder) 75 76 controller_to_host_forwarder = HCI_Bridge.Forwarder( 77 hci_host_sink, 78 hci_controller_sink, 79 controller_to_host_filter, 80 lambda packet: tracer.trace(packet, 1), 81 ) 82 hci_controller_source.set_packet_sink(controller_to_host_forwarder) 83