1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-08-24 16:21:16 +02:00

Zepp OS: Do not change GATT Callback unless explicitely set

This commit is contained in:
José Rebelo 2022-10-22 22:00:01 +01:00
parent addf7ff6a6
commit 9c82180930
7 changed files with 51 additions and 2 deletions

View File

@ -141,6 +141,11 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
return autoReconnect; return autoReconnect;
} }
@Override
public boolean getImplicitCallbackModify() {
return true;
}
@Override @Override
public GBDevice getDevice() { public GBDevice getDevice() {
return gbDevice; return gbDevice;

View File

@ -114,6 +114,16 @@ public interface DeviceSupport extends EventHandler {
*/ */
boolean getAutoReconnect(); boolean getAutoReconnect();
/**
* Returns whether the gatt callback should be implicitly set to the one on the transaction,
* even if it was not set directly on the transaction. If true, the gatt callback will always
* be set to the one in the transaction, even if null and not explicitly set to null.
* See https://codeberg.org/Freeyourgadget/Gadgetbridge/pulls/2912 for more information.
* This should be false by default, but we are making it configurable to avoid breaking
* older devices that rely on this behavior.
*/
boolean getImplicitCallbackModify();
/** /**
* Returns the associated device this instance communicates with. * Returns the associated device this instance communicates with.
*/ */

View File

@ -102,6 +102,11 @@ public class ServiceDeviceSupport implements DeviceSupport {
return delegate.getAutoReconnect(); return delegate.getAutoReconnect();
} }
@Override
public boolean getImplicitCallbackModify() {
return delegate.getImplicitCallbackModify();
}
@Override @Override
public void dispose() { public void dispose() {
delegate.dispose(); delegate.dispose();

View File

@ -22,7 +22,6 @@ import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor; import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService; import android.bluetooth.BluetoothGattService;
import android.content.Intent;
import android.location.Location; import android.location.Location;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -78,6 +77,7 @@ public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport im
if (mQueue == null) { if (mQueue == null) {
mQueue = new BtLEQueue(getBluetoothAdapter(), getDevice(), this, this, getContext(), mSupportedServerServices); mQueue = new BtLEQueue(getBluetoothAdapter(), getDevice(), this, this, getContext(), mSupportedServerServices);
mQueue.setAutoReconnect(getAutoReconnect()); mQueue.setAutoReconnect(getAutoReconnect());
mQueue.setImplicitGattCallbackModify(getImplicitCallbackModify());
} }
return mQueue.connect(); return mQueue.connect();
} }

View File

@ -79,6 +79,7 @@ public final class BtLEQueue {
private final InternalGattCallback internalGattCallback; private final InternalGattCallback internalGattCallback;
private final InternalGattServerCallback internalGattServerCallback; private final InternalGattServerCallback internalGattServerCallback;
private boolean mAutoReconnect; private boolean mAutoReconnect;
private boolean mImplicitGattCallbackModify = true;
private Thread dispatchThread = new Thread("Gadgetbridge GATT Dispatcher") { private Thread dispatchThread = new Thread("Gadgetbridge GATT Dispatcher") {
@ -137,7 +138,10 @@ public final class BtLEQueue {
if(qTransaction instanceof Transaction) { if(qTransaction instanceof Transaction) {
Transaction transaction = (Transaction)qTransaction; Transaction transaction = (Transaction)qTransaction;
internalGattCallback.setTransactionGattCallback(transaction.getGattCallback()); LOG.trace("Changing gatt callback for {}? {}", transaction.getTaskName(), transaction.isModifyGattCallback());
if (mImplicitGattCallbackModify || transaction.isModifyGattCallback()) {
internalGattCallback.setTransactionGattCallback(transaction.getGattCallback());
}
mAbortTransaction = false; mAbortTransaction = false;
// Run all actions of the transaction until one doesn't succeed // Run all actions of the transaction until one doesn't succeed
for (BtLEAction action : transaction.getActions()) { for (BtLEAction action : transaction.getActions()) {
@ -202,6 +206,10 @@ public final class BtLEQueue {
mAutoReconnect = enable; mAutoReconnect = enable;
} }
public void setImplicitGattCallbackModify(final boolean enable) {
mImplicitGattCallbackModify = enable;
}
protected boolean isConnected() { protected boolean isConnected() {
return mGbDevice.isConnected(); return mGbDevice.isConnected();
} }

View File

@ -31,10 +31,13 @@ import androidx.annotation.Nullable;
*/ */
public class Transaction extends AbstractTransaction { public class Transaction extends AbstractTransaction {
private final List<BtLEAction> mActions = new ArrayList<>(4); private final List<BtLEAction> mActions = new ArrayList<>(4);
private private
@Nullable @Nullable
GattCallback gattCallback; GattCallback gattCallback;
private boolean modifyGattCallback;
public Transaction(String taskName) { public Transaction(String taskName) {
super(taskName); super(taskName);
} }
@ -53,6 +56,7 @@ public class Transaction extends AbstractTransaction {
public void setGattCallback(@Nullable GattCallback callback) { public void setGattCallback(@Nullable GattCallback callback) {
gattCallback = callback; gattCallback = callback;
modifyGattCallback = true;
} }
/** /**
@ -64,6 +68,14 @@ public class Transaction extends AbstractTransaction {
return gattCallback; return gattCallback;
} }
/**
* Returns whether the gatt callback should be modified for this transaction (either set, or
* unset if {@code getGattCallback} is null.
*/
public boolean isModifyGattCallback() {
return modifyGattCallback;
}
@Override @Override
public int getActionCount() { public int getActionCount() {
return mActions.size(); return mActions.size();

View File

@ -161,6 +161,15 @@ public abstract class Huami2021Support extends HuamiSupport {
return (byte) 0x80; return (byte) 0x80;
} }
/**
* Do not reset the gatt callback implicitly, as that would interrupt operations.
* See https://codeberg.org/Freeyourgadget/Gadgetbridge/pulls/2912 for more information.
*/
@Override
public boolean getImplicitCallbackModify() {
return false;
}
@Override @Override
public void onSendConfiguration(final String config) { public void onSendConfiguration(final String config) {
final ConfigSetter configSetter = new ConfigSetter(); final ConfigSetter configSetter = new ConfigSetter();