diff --git a/bumble/helpers.py b/bumble/helpers.py index 762601c5..93bbc24e 100644 --- a/bumble/helpers.py +++ b/bumble/helpers.py @@ -18,7 +18,7 @@ from __future__ import annotations from collections.abc import Callable, MutableMapping -from typing import cast, Any +from typing import cast, Any, Optional import logging from bumble import avdtp @@ -69,7 +69,7 @@ class PacketTracer: class AclStream: psms: MutableMapping[int, int] - peer: PacketTracer.AclStream + peer: Optional[PacketTracer.AclStream] avdtp_assemblers: MutableMapping[int, avdtp.MessageAssembler] def __init__(self, analyzer: PacketTracer.Analyzer) -> None: @@ -77,6 +77,7 @@ def __init__(self, analyzer: PacketTracer.Analyzer) -> None: self.packet_assembler = HCI_AclDataPacketAssembler(self.on_acl_pdu) self.avdtp_assemblers = {} # AVDTP assemblers, by source_cid self.psms = {} # PSM, by source_cid + self.peer = None # pylint: disable=too-many-nested-blocks def on_acl_pdu(self, pdu: bytes) -> None: diff --git a/bumble/rfcomm.py b/bumble/rfcomm.py index 53bbe53e..d72b260d 100644 --- a/bumble/rfcomm.py +++ b/bumble/rfcomm.py @@ -538,8 +538,9 @@ def on_uih_frame(self, frame: RFCOMM_Frame) -> None: f'[{self.dlci}] {len(data)} bytes, ' f'rx_credits={self.rx_credits}: {data.hex()}' ) - if len(data) and self.sink: - self.sink(data) # pylint: disable=not-callable + if data: + if self.sink: + self.sink(data) # pylint: disable=not-callable # Update the credits if self.rx_credits > 0: diff --git a/extras/android/RemoteHCI/app/build.gradle.kts b/extras/android/RemoteHCI/app/build.gradle.kts index 2e2df389..0e68a2fd 100644 --- a/extras/android/RemoteHCI/app/build.gradle.kts +++ b/extras/android/RemoteHCI/app/build.gradle.kts @@ -10,7 +10,7 @@ android { defaultConfig { applicationId = "com.github.google.bumble.remotehci" - minSdk = 26 + minSdk = 29 targetSdk = 33 versionCode = 1 versionName = "1.0" diff --git a/extras/android/RemoteHCI/app/src/main/java/com/github/google/bumble/remotehci/HciHal.java b/extras/android/RemoteHCI/app/src/main/java/com/github/google/bumble/remotehci/HciHal.java index fd819218..a1bd8eb3 100644 --- a/extras/android/RemoteHCI/app/src/main/java/com/github/google/bumble/remotehci/HciHal.java +++ b/extras/android/RemoteHCI/app/src/main/java/com/github/google/bumble/remotehci/HciHal.java @@ -4,6 +4,7 @@ import android.os.IBinder; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.Trace; import android.util.Log; import java.util.ArrayList; @@ -53,6 +54,7 @@ class HciHidlHal extends android.hardware.bluetooth.V1_0.IBluetoothHciCallbacks. private final android.hardware.bluetooth.V1_0.IBluetoothHci mHciService; private final HciHalCallback mHciCallbacks; private int mInitializationStatus = -1; + private final boolean mTracingEnabled = Trace.isEnabled(); public static HciHidlHal create(HciHalCallback hciCallbacks) { @@ -89,6 +91,7 @@ public Status initialize() throws RemoteException, InterruptedException { } // Map the status code. + Log.d(TAG, "Initialization status = " + mInitializationStatus); switch (mInitializationStatus) { case android.hardware.bluetooth.V1_0.Status.SUCCESS: return Status.SUCCESS; @@ -108,6 +111,10 @@ public Status initialize() throws RemoteException, InterruptedException { public void sendPacket(HciPacket.Type type, byte[] packet) { ArrayList data = HciPacket.byteArrayToList(packet); + if (mTracingEnabled) { + Trace.beginAsyncSection("SEND_PACKET_TO_HAL", 1); + } + try { switch (type) { case COMMAND: @@ -125,6 +132,10 @@ public void sendPacket(HciPacket.Type type, byte[] packet) { } catch (RemoteException error) { Log.w(TAG, "failed to forward packet: " + error); } + + if (mTracingEnabled) { + Trace.endAsyncSection("SEND_PACKET_TO_HAL", 1); + } } @Override @@ -157,6 +168,7 @@ class HciAidlHal extends android.hardware.bluetooth.IBluetoothHciCallbacks.Stub private final android.hardware.bluetooth.IBluetoothHci mHciService; private final HciHalCallback mHciCallbacks; private int mInitializationStatus = android.hardware.bluetooth.Status.SUCCESS; + private final boolean mTracingEnabled = Trace.isEnabled(); public static HciAidlHal create(HciHalCallback hciCallbacks) { IBinder binder = ServiceManager.getService("android.hardware.bluetooth.IBluetoothHci/default"); @@ -187,6 +199,7 @@ public Status initialize() throws RemoteException, InterruptedException { } // Map the status code. + Log.d(TAG, "Initialization status = " + mInitializationStatus); switch (mInitializationStatus) { case android.hardware.bluetooth.Status.SUCCESS: return Status.SUCCESS; @@ -208,6 +221,10 @@ public Status initialize() throws RemoteException, InterruptedException { // HciHal methods. @Override public void sendPacket(HciPacket.Type type, byte[] packet) { + if (mTracingEnabled) { + Trace.beginAsyncSection("SEND_PACKET_TO_HAL", 1); + } + try { switch (type) { case COMMAND: @@ -229,6 +246,10 @@ public void sendPacket(HciPacket.Type type, byte[] packet) { } catch (RemoteException error) { Log.w(TAG, "failed to forward packet: " + error); } + + if (mTracingEnabled) { + Trace.endAsyncSection("SEND_PACKET_TO_HAL", 1); + } } // IBluetoothHciCallbacks methods. diff --git a/extras/android/RemoteHCI/app/src/main/java/com/github/google/bumble/remotehci/HciServer.java b/extras/android/RemoteHCI/app/src/main/java/com/github/google/bumble/remotehci/HciServer.java index df1d0d87..93323054 100644 --- a/extras/android/RemoteHCI/app/src/main/java/com/github/google/bumble/remotehci/HciServer.java +++ b/extras/android/RemoteHCI/app/src/main/java/com/github/google/bumble/remotehci/HciServer.java @@ -1,5 +1,6 @@ package com.github.google.bumble.remotehci; +import android.os.Trace; import android.util.Log; import java.io.IOException; @@ -15,6 +16,7 @@ public class HciServer { private final int mPort; private final Listener mListener; private OutputStream mOutputStream; + private final boolean mTracingEnabled = Trace.isEnabled(); public interface Listener extends HciParser.Sink { void onHostConnectionState(boolean connected); @@ -27,6 +29,8 @@ public interface Listener extends HciParser.Sink { } public void run() throws IOException { + Log.i(TAG, "Tracing enabled: " + mTracingEnabled); + for (;;) { try { loop(); @@ -73,6 +77,10 @@ private void loop() throws IOException { } public void sendPacket(HciPacket.Type type, byte[] packet) { + if (mTracingEnabled) { + Trace.beginAsyncSection("SEND_PACKET_FROM_HAL", 2); + } + // Create a combined data buffer so we can write it out in a single call. byte[] data = new byte[packet.length + 1]; data[0] = type.value; @@ -89,5 +97,9 @@ public void sendPacket(HciPacket.Type type, byte[] packet) { Log.d(TAG, "no client, dropping packet"); } } + + if (mTracingEnabled) { + Trace.endAsyncSection("SEND_PACKET_FROM_HAL", 2); + } } }