forked from Blendman974/faceclaw
More conservating locking around Android bluetooth APIs, to try to address SIGSEGV seen on Galaxy Tab A9+
This commit is contained in:
1 parent
aa0b1d6874
commit
7cbcbf7c90
5 files changed
+282
-115
No files matched your search
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.SystemClock;
|
||||
@@ -22,6 +23,8 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
@@ -30,7 +33,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private static final int WRITE_TYPE = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE;
|
||||
private static final int IMAGE_WRITE_TYPE = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT;
|
||||
private static final int DESIRED_MTU = 512;
|
||||
private static final int CONNECTION_PRIORITY_HIGH = BluetoothGatt.CONNECTION_PRIORITY_HIGH;
|
||||
|
||||
private static final int CONNECT_TIMEOUT_MS = 5_000;
|
||||
private static final int SERVICES_TIMEOUT_MS = 5_000;
|
||||
@@ -52,6 +54,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private static final int IDLE_SLEEP_MS = 100;
|
||||
private static final int RECONNECT_DELAY_MS = 5_000;
|
||||
|
||||
private interface NativeBridgeOperation<T> {
|
||||
T run();
|
||||
}
|
||||
|
||||
private static final BleProtocol.ImageTileOptions[] DASHBOARD_TILES = new BleProtocol.ImageTileOptions[] {
|
||||
new BleProtocol.ImageTileOptions("img00", 10, 0, 0, 288, 144),
|
||||
new BleProtocol.ImageTileOptions("img10", 11, 288, 0, 288, 144),
|
||||
@@ -63,7 +69,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private final FaceclawBleManager bleManager;
|
||||
private final InterruptibleSleep interruptibleSleep = new InterruptibleSleep();
|
||||
private final Object lock = new Object();
|
||||
private final Object nativeBridgeLock = new Object();
|
||||
private final ReentrantLock nativeBridgeLock = new ReentrantLock();
|
||||
private final AtomicInteger nativeBridgeOperationSeq = new AtomicInteger();
|
||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
private final String rightAddress;
|
||||
private final String leftAddress;
|
||||
@@ -196,7 +203,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (tileBmps == null || tileBmps.size() != DASHBOARD_TILES.length) {
|
||||
throw new IllegalArgumentException("exactly 4 tile bitmaps are required");
|
||||
}
|
||||
synchronized (nativeBridgeLock) {
|
||||
withNativeBridgeLock("submitDashboardImage", () -> {
|
||||
synchronized (desiredTilesLock) {
|
||||
desiredTileBmps = emptyTileSet();
|
||||
for (int i = 0; i < DASHBOARD_TILES.length; i++) {
|
||||
@@ -207,7 +214,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
desiredForceTiledCommit = forceTiledCommit;
|
||||
desiredPaintMs = paintMs;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
interruptibleSleep.interrupt();
|
||||
}
|
||||
|
||||
@@ -241,7 +249,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
boolean forceTiledCommit,
|
||||
int paintMs
|
||||
) {
|
||||
synchronized (nativeBridgeLock) {
|
||||
withNativeBridgeLock("submitDashboardImage4", () -> {
|
||||
synchronized (desiredTilesLock) {
|
||||
desiredTileBmps = emptyTileSet();
|
||||
desiredTileBmps[0] = tile0Bmp == null ? new byte[0] : Arrays.copyOf(tile0Bmp, tile0Bmp.length);
|
||||
@@ -252,10 +260,41 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
desiredForceTiledCommit = forceTiledCommit;
|
||||
desiredPaintMs = paintMs;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
interruptibleSleep.interrupt();
|
||||
}
|
||||
|
||||
public Bitmap createPreviewBitmap(int[] colors, int width, int height) {
|
||||
if (colors == null) {
|
||||
throw new IllegalArgumentException("colors are required");
|
||||
}
|
||||
if (width <= 0 || height <= 0 || colors.length < width * height) {
|
||||
throw new IllegalArgumentException("invalid preview bitmap dimensions");
|
||||
}
|
||||
return withNativeBridgeLock("createPreviewBitmap " + width + "x" + height, () ->
|
||||
Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888));
|
||||
}
|
||||
|
||||
public void acquireNativeBridgeLock(String operation) {
|
||||
String label = operation == null || operation.isEmpty() ? "external" : operation;
|
||||
String threadName = Thread.currentThread().getName();
|
||||
long waitStartedAtMs = SystemClock.elapsedRealtime();
|
||||
Log.i(TAG, "nativeBridge wait external " + label + " thread=" + threadName);
|
||||
nativeBridgeLock.lock();
|
||||
long enteredAtMs = SystemClock.elapsedRealtime();
|
||||
Log.i(TAG, "nativeBridge enter external " + label
|
||||
+ " thread=" + threadName
|
||||
+ " waitedMs=" + (enteredAtMs - waitStartedAtMs));
|
||||
}
|
||||
|
||||
public void releaseNativeBridgeLock(String operation) {
|
||||
String label = operation == null || operation.isEmpty() ? "external" : operation;
|
||||
String threadName = Thread.currentThread().getName();
|
||||
Log.i(TAG, "nativeBridge exit external " + label + " thread=" + threadName);
|
||||
nativeBridgeLock.unlock();
|
||||
}
|
||||
|
||||
public boolean sendShutdown(int exitMode) {
|
||||
int magic;
|
||||
long startedAtMs = SystemClock.elapsedRealtime();
|
||||
@@ -474,7 +513,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (!bleManager.connect(address, CONNECT_TIMEOUT_MS)) {
|
||||
throw new IllegalStateException("connect failed: " + address);
|
||||
}
|
||||
bleManager.requestConnectionPriority(address, CONNECTION_PRIORITY_HIGH);
|
||||
// requestConnectionPriority has no callback in this Android compile target, so there is
|
||||
// no reliable completion point to keep it in the global GATT operation pipeline.
|
||||
bleManager.requestMtu(address, DESIRED_MTU, CONNECT_TIMEOUT_MS);
|
||||
if (!bleManager.discoverServices(address, SERVICES_TIMEOUT_MS)) {
|
||||
throw new IllegalStateException("discoverServices failed: " + address);
|
||||
@@ -636,15 +676,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
|
||||
private boolean writeMessageFrames(OutboundMessage message) {
|
||||
String writeAddress = isLeftArmMessage(message) ? leftAddress : rightAddress;
|
||||
synchronized (nativeBridgeLock) {
|
||||
return bleManager.writeFrames(
|
||||
return withNativeBridgeLock("writeFrames " + message.label + " frames=" + message.frames.size(), () ->
|
||||
bleManager.writeFrames(
|
||||
writeAddress,
|
||||
BleProtocol.WRITE_CHAR_UUID,
|
||||
message.frames,
|
||||
WRITE_TYPE,
|
||||
WRITE_TIMEOUT_MS
|
||||
);
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
private boolean isLeftArmMessage(OutboundMessage message) {
|
||||
@@ -1399,6 +1438,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
|
||||
private void logLine(String line) {
|
||||
Log.i(TAG, line);
|
||||
if (!shouldForwardLogLineToJavascript(line)) {
|
||||
return;
|
||||
}
|
||||
final FaceclawBleCommunicatorListener current = listener;
|
||||
if (current == null) {
|
||||
return;
|
||||
@@ -1412,6 +1454,49 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
});
|
||||
}
|
||||
|
||||
private boolean shouldForwardLogLineToJavascript(String line) {
|
||||
if (line == null || line.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
return line.startsWith("communicator start")
|
||||
|| line.startsWith("communicator stop")
|
||||
|| line.startsWith("connect failed")
|
||||
|| line.startsWith("session ready")
|
||||
|| line.startsWith("message timed out")
|
||||
|| line.startsWith("create layout timed out")
|
||||
|| line.startsWith("unexpected ACK")
|
||||
|| line.startsWith("late ACK")
|
||||
|| line.startsWith("duplicate ACK")
|
||||
|| line.startsWith("skip shutdown")
|
||||
|| line.startsWith("shutdown")
|
||||
|| line.startsWith("Even Realities")
|
||||
|| line.startsWith("communicator loop error");
|
||||
}
|
||||
|
||||
private <T> T withNativeBridgeLock(String operation, NativeBridgeOperation<T> callback) {
|
||||
int operationId = nativeBridgeOperationSeq.incrementAndGet();
|
||||
String threadName = Thread.currentThread().getName();
|
||||
long waitStartedAtMs = SystemClock.elapsedRealtime();
|
||||
Log.i(TAG, "nativeBridge wait op#" + operationId + " " + operation + " thread=" + threadName);
|
||||
nativeBridgeLock.lock();
|
||||
try {
|
||||
long enteredAtMs = SystemClock.elapsedRealtime();
|
||||
Log.i(TAG, "nativeBridge enter op#" + operationId + " " + operation
|
||||
+ " thread=" + threadName
|
||||
+ " waitedMs=" + (enteredAtMs - waitStartedAtMs));
|
||||
try {
|
||||
return callback.run();
|
||||
} finally {
|
||||
long exitedAtMs = SystemClock.elapsedRealtime();
|
||||
Log.i(TAG, "nativeBridge exit op#" + operationId + " " + operation
|
||||
+ " thread=" + threadName
|
||||
+ " heldMs=" + (exitedAtMs - enteredAtMs));
|
||||
}
|
||||
} finally {
|
||||
nativeBridgeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private static String timestamp(long elapsedMs) {
|
||||
long wallMs = System.currentTimeMillis();
|
||||
return String.format(Locale.US, "%tF %tT.%tL elapsed=%dms", wallMs, wallMs, wallMs, elapsedMs);
|
||||
|
||||
@@ -33,7 +33,7 @@ public class FaceclawBleManager {
|
||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
private final ConcurrentHashMap<String, BluetoothGatt> gattClients = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Object> gattLocks = new ConcurrentHashMap<>();
|
||||
private final Object bluetoothApiLock = new Object();
|
||||
|
||||
private final ConcurrentHashMap<String, CountDownLatch> connectLatches = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Boolean> connectResults = new ConcurrentHashMap<>();
|
||||
@@ -103,99 +103,84 @@ public class FaceclawBleManager {
|
||||
if (gatt != null) {
|
||||
gattClients.put(address, gatt);
|
||||
}
|
||||
}
|
||||
if (gatt == null) {
|
||||
connectLatches.remove(address);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!awaitLatch(latch, timeoutMs)) {
|
||||
connectLatches.remove(address);
|
||||
connectResults.remove(address);
|
||||
synchronized (gattLock) {
|
||||
if (gatt == null) {
|
||||
connectLatches.remove(address);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!awaitLatch(latch, timeoutMs)) {
|
||||
connectLatches.remove(address);
|
||||
connectResults.remove(address);
|
||||
gattClients.remove(address, gatt);
|
||||
gatt.disconnect();
|
||||
gatt.close();
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Boolean connected = connectResults.remove(address);
|
||||
connectLatches.remove(address);
|
||||
boolean ok = Boolean.TRUE.equals(connected);
|
||||
if (ok) {
|
||||
synchronized (gattLock) {
|
||||
BluetoothGatt current = gattClients.get(address);
|
||||
if (current == gatt) {
|
||||
gatt.readPhy();
|
||||
}
|
||||
}
|
||||
Boolean connected = connectResults.remove(address);
|
||||
connectLatches.remove(address);
|
||||
return Boolean.TRUE.equals(connected);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
public boolean requestConnectionPriority(String address, int priority) {
|
||||
synchronized (gattLock(address)) {
|
||||
BluetoothGatt gatt = requireGatt(address);
|
||||
return gatt.requestConnectionPriority(priority);
|
||||
}
|
||||
Log.w(TAG, "requestConnectionPriority skipped: no completion callback is available in this build");
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean requestMtu(String address, int mtu, int timeoutMs) {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
mtuLatches.put(address, latch);
|
||||
mtuStatuses.remove(address);
|
||||
|
||||
boolean started;
|
||||
synchronized (gattLock(address)) {
|
||||
BluetoothGatt gatt = requireGatt(address);
|
||||
started = gatt.requestMtu(mtu);
|
||||
}
|
||||
if (!started) {
|
||||
mtuLatches.remove(address);
|
||||
return false;
|
||||
}
|
||||
if (!awaitLatch(latch, timeoutMs)) {
|
||||
mtuLatches.remove(address);
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
mtuLatches.put(address, latch);
|
||||
mtuStatuses.remove(address);
|
||||
return false;
|
||||
|
||||
BluetoothGatt gatt = requireGatt(address);
|
||||
boolean started = gatt.requestMtu(mtu);
|
||||
if (!started) {
|
||||
mtuLatches.remove(address);
|
||||
return false;
|
||||
}
|
||||
if (!awaitLatch(latch, timeoutMs)) {
|
||||
mtuLatches.remove(address);
|
||||
mtuStatuses.remove(address);
|
||||
return false;
|
||||
}
|
||||
Integer status = mtuStatuses.remove(address);
|
||||
mtuLatches.remove(address);
|
||||
return status != null && status == BluetoothGatt.GATT_SUCCESS;
|
||||
}
|
||||
Integer status = mtuStatuses.remove(address);
|
||||
mtuLatches.remove(address);
|
||||
return status != null && status == BluetoothGatt.GATT_SUCCESS;
|
||||
}
|
||||
|
||||
public boolean discoverServices(String address, int timeoutMs) {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
servicesLatches.put(address, latch);
|
||||
servicesStatuses.remove(address);
|
||||
|
||||
boolean started;
|
||||
synchronized (gattLock(address)) {
|
||||
BluetoothGatt gatt = requireGatt(address);
|
||||
started = gatt.discoverServices();
|
||||
}
|
||||
if (!started) {
|
||||
servicesLatches.remove(address);
|
||||
return false;
|
||||
}
|
||||
if (!awaitLatch(latch, timeoutMs)) {
|
||||
servicesLatches.remove(address);
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
servicesLatches.put(address, latch);
|
||||
servicesStatuses.remove(address);
|
||||
return false;
|
||||
|
||||
BluetoothGatt gatt = requireGatt(address);
|
||||
boolean started = gatt.discoverServices();
|
||||
if (!started) {
|
||||
servicesLatches.remove(address);
|
||||
return false;
|
||||
}
|
||||
if (!awaitLatch(latch, timeoutMs)) {
|
||||
servicesLatches.remove(address);
|
||||
servicesStatuses.remove(address);
|
||||
return false;
|
||||
}
|
||||
Integer status = servicesStatuses.remove(address);
|
||||
servicesLatches.remove(address);
|
||||
return status != null && status == BluetoothGatt.GATT_SUCCESS;
|
||||
}
|
||||
Integer status = servicesStatuses.remove(address);
|
||||
servicesLatches.remove(address);
|
||||
return status != null && status == BluetoothGatt.GATT_SUCCESS;
|
||||
}
|
||||
|
||||
public boolean enableNotifications(String address, String characteristicUuid, boolean enable, int timeoutMs) {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
descriptorLatches.put(address, latch);
|
||||
descriptorStatuses.remove(address);
|
||||
|
||||
boolean started;
|
||||
synchronized (gattLock(address)) {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
descriptorLatches.put(address, latch);
|
||||
descriptorStatuses.remove(address);
|
||||
|
||||
BluetoothGatt gatt = requireGatt(address);
|
||||
BluetoothGattCharacteristic characteristic = requireCharacteristic(gatt, characteristicUuid);
|
||||
|
||||
@@ -214,22 +199,21 @@ public class FaceclawBleManager {
|
||||
descriptor.setValue(enable
|
||||
? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
|
||||
: BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
|
||||
started = gatt.writeDescriptor(descriptor);
|
||||
}
|
||||
|
||||
if (!started) {
|
||||
boolean started = gatt.writeDescriptor(descriptor);
|
||||
if (!started) {
|
||||
descriptorLatches.remove(address);
|
||||
descriptorStatuses.remove(address);
|
||||
return false;
|
||||
}
|
||||
if (!awaitLatch(latch, timeoutMs)) {
|
||||
descriptorLatches.remove(address);
|
||||
descriptorStatuses.remove(address);
|
||||
return false;
|
||||
}
|
||||
Integer status = descriptorStatuses.remove(address);
|
||||
descriptorLatches.remove(address);
|
||||
descriptorStatuses.remove(address);
|
||||
return false;
|
||||
return status != null && status == BluetoothGatt.GATT_SUCCESS;
|
||||
}
|
||||
if (!awaitLatch(latch, timeoutMs)) {
|
||||
descriptorLatches.remove(address);
|
||||
descriptorStatuses.remove(address);
|
||||
return false;
|
||||
}
|
||||
Integer status = descriptorStatuses.remove(address);
|
||||
descriptorLatches.remove(address);
|
||||
return status != null && status == BluetoothGatt.GATT_SUCCESS;
|
||||
}
|
||||
|
||||
public boolean writeFrames(
|
||||
@@ -375,7 +359,9 @@ public class FaceclawBleManager {
|
||||
}
|
||||
|
||||
private Object gattLock(String address) {
|
||||
return gattLocks.computeIfAbsent(address, ignored -> new Object());
|
||||
// Android's Bluetooth stack has process-wide command-pipeline constraints on some devices.
|
||||
// Keep every BluetoothGatt API call serialized globally, even for different MAC addresses.
|
||||
return bluetoothApiLock;
|
||||
}
|
||||
|
||||
private void runOnMainAsync(Runnable runnable) {
|
||||
|
||||
Reference in new issue
Block a user