Skip to content

Commit

Permalink
update credits even without a sink
Browse files Browse the repository at this point in the history
  • Loading branch information
barbibulle committed Jan 10, 2024
1 parent d8e6700 commit 481cf40
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
5 changes: 3 additions & 2 deletions bumble/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,14 +69,15 @@
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:
self.analyzer = analyzer
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:
Expand Down
5 changes: 3 additions & 2 deletions bumble/rfcomm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 len(data):
if self.sink:
self.sink(data) # pylint: disable=not-callable

# Update the credits
if self.rx_credits > 0:
Expand Down
2 changes: 1 addition & 1 deletion extras/android/RemoteHCI/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {

defaultConfig {
applicationId = "com.github.google.bumble.remotehci"
minSdk = 26
minSdk = 29
targetSdk = 33
versionCode = 1
versionName = "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -108,6 +111,10 @@ public Status initialize() throws RemoteException, InterruptedException {
public void sendPacket(HciPacket.Type type, byte[] packet) {
ArrayList<Byte> data = HciPacket.byteArrayToList(packet);

if (mTracingEnabled) {
Trace.beginAsyncSection("SEND_PACKET_TO_HAL", 1);
}

try {
switch (type) {
case COMMAND:
Expand All @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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:
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.google.bumble.remotehci;

import android.os.Trace;
import android.util.Log;

import java.io.IOException;
Expand All @@ -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);
Expand All @@ -27,6 +29,8 @@ public interface Listener extends HciParser.Sink {
}

public void run() throws IOException {
Log.i(TAG, "Tracing enabled: " + mTracingEnabled);

for (;;) {
try {
loop();
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
}

0 comments on commit 481cf40

Please sign in to comment.