1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-02-16 12:26:47 +01:00

Pebble: remove the legacy countdownlatch on Pebble GATT server code

The GATTServer code is now in line with the new GATTClient code.
This commit is contained in:
Daniele Gobbetti 2018-05-19 22:42:05 +02:00
parent d817759d1f
commit 3c3e38741a
2 changed files with 34 additions and 20 deletions

View File

@ -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() {

View File

@ -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;