From 3c3e38741a39c8e2498059b10006536cd0df96cc Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Sat, 19 May 2018 22:42:05 +0200 Subject: [PATCH] Pebble: remove the legacy countdownlatch on Pebble GATT server code The GATTServer code is now in line with the new GATTClient code. --- .../devices/pebble/ble/PebbleGATTServer.java | 29 +++++++++++++++++-- .../devices/pebble/ble/PebbleLESupport.java | 25 +++++----------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTServer.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTServer.java index aaa410278..6e3299d17 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTServer.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTServer.java @@ -30,6 +30,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.UUID; +import java.util.concurrent.CountDownLatch; class PebbleGATTServer extends BluetoothGattServerCallback { private static final Logger LOG = LoggerFactory.getLogger(PebbleGATTServer.class); @@ -43,6 +44,7 @@ class PebbleGATTServer extends BluetoothGattServerCallback { private Context mContext; private BluetoothGattServer mBluetoothGattServer; private BluetoothGattCharacteristic writeCharacteristics; + private CountDownLatch mWaitWriteCompleteLatch; PebbleGATTServer(PebbleLESupport pebbleLESupport, Context context, BluetoothDevice btDevice) { mContext = context; @@ -73,9 +75,20 @@ class PebbleGATTServer extends BluetoothGattServerCallback { } synchronized void sendDataToPebble(byte[] data) { + mWaitWriteCompleteLatch = new CountDownLatch(1); writeCharacteristics.setValue(data.clone()); - mBluetoothGattServer.notifyCharacteristicChanged(mBtDevice, writeCharacteristics, false); + boolean success = mBluetoothGattServer.notifyCharacteristicChanged(mBtDevice, writeCharacteristics, false); + if (!success) { + LOG.error("could not send data to pebble (error writing characteristic)"); + } else { + try { + mWaitWriteCompleteLatch.await(); + } catch (InterruptedException e) { + LOG.warn("interrupted while waiting for write complete latch"); + } + } + mWaitWriteCompleteLatch = null; } public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) { @@ -157,7 +170,19 @@ class PebbleGATTServer extends BluetoothGattServerCallback { } public void onNotificationSent(BluetoothDevice bluetoothDevice, int status) { - //LOG.info("onNotificationSent() status = " + status + " to device " + mmBtDevice.getAddress()); + + if (!mPebbleLESupport.isExpectedDevice(bluetoothDevice)) { + return; + } + if (status != BluetoothGatt.GATT_SUCCESS) { + LOG.error("something went wrong when writing to PPoGATT characteristics"); + } + if (mWaitWriteCompleteLatch != null) { + mWaitWriteCompleteLatch.countDown(); + } else { + LOG.warn("mWaitWriteCompleteLatch is null!"); + } +// LOG.info("onNotificationSent() status = " + status + " to device " + bluetoothDevice.getAddress()); } void close() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleLESupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleLESupport.java index e694cf51d..e4804e63d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleLESupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleLESupport.java @@ -27,7 +27,6 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; -import java.util.concurrent.CountDownLatch; import nodomain.freeyourgadget.gadgetbridge.GBApplication; @@ -43,7 +42,6 @@ public class PebbleLESupport { private int mMTULimit = Integer.MAX_VALUE; public boolean clientOnly = false; // currently experimental, and only possible for Pebble 2 private boolean mIsConnected = false; - private CountDownLatch mPPAck; private HandlerThread mWriteHandlerThread; private Handler mWriteHandler; @@ -156,13 +154,6 @@ public class PebbleLESupport { int serial = header >> 3; if (command == 0x01) { LOG.info("got ACK for serial = " + serial); - if (!clientOnly) { - if (mPPAck != null) { - mPPAck.countDown(); - } else { - LOG.warn("mPPAck countdownlatch is not present but it probably should"); - } - } } if (command == 0x02) { // some request? LOG.info("got command 0x02"); @@ -187,7 +178,12 @@ public class PebbleLESupport { private synchronized void sendDataToPebble(final byte[] bytes) { if (mPebbleGATTServer != null) { - mPebbleGATTServer.sendDataToPebble(bytes); + mWriteHandler.post(new Runnable() { + @Override + public void run() { + mPebbleGATTServer.sendDataToPebble(bytes); + } + }); } else { // For now only in experimental client only code mWriteHandler.post(new Runnable() { @@ -226,9 +222,6 @@ public class PebbleLESupport { int payloadToSend = bytesRead + 4; int srcPos = 0; - if (!clientOnly) { - mPPAck = new CountDownLatch(1); - } while (payloadToSend > 0) { int chunkSize = (payloadToSend < (mMTU - 4)) ? payloadToSend : mMTU - 4; byte[] outBuf = new byte[chunkSize + 1]; @@ -238,11 +231,7 @@ public class PebbleLESupport { srcPos += chunkSize; payloadToSend -= chunkSize; } - if (!clientOnly) { - mPPAck.await(); - mPPAck = null; - } - } catch (IOException | InterruptedException e) { + } catch (IOException e) { LOG.info(e.getMessage()); Thread.currentThread().interrupt(); break;