Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed CharacteristicLongWriteOperation defaults #847

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private NamedBooleans() { }
class NamedInts {
static final String GATT_WRITE_MTU_OVERHEAD = "GATT_WRITE_MTU_OVERHEAD";
static final String GATT_MTU_MINIMUM = "GATT_MTU_MINIMUM";
static final String GATT_MAX_ATTR_LENGTH = "GATT_MAX_ATTR_LENGTH";
private NamedInts() { }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
import io.reactivex.Scheduler;

import static com.polidea.rxandroidble2.internal.connection.ConnectionComponent.NamedBooleans.SUPPRESS_OPERATION_CHECKS;
import static com.polidea.rxandroidble2.internal.connection.ConnectionComponent.NamedInts.GATT_MAX_ATTR_LENGTH;
import static com.polidea.rxandroidble2.internal.connection.ConnectionComponent.NamedInts.GATT_MTU_MINIMUM;
import static com.polidea.rxandroidble2.internal.connection.ConnectionComponent.NamedInts.GATT_WRITE_MTU_OVERHEAD;

@Module
public abstract class ConnectionModule {

/** {@see https://issuetracker.google.com/issues/307234027} */
private static final int GATT_MAX_ATTRIBUTE_LENGTH = 600;
/** {@see https://issuetracker.google.com/issues/307234027} */
private static final int GATT_MAX_ATTRIBUTE_LENGTH_API_33 = 512;

public static final String OPERATION_TIMEOUT = "operation-timeout";

@Provides
Expand Down Expand Up @@ -75,6 +81,15 @@ static int minimumMtu() {
return RxBleConnection.GATT_MTU_MINIMUM;
}

@Provides
@Named(GATT_MAX_ATTR_LENGTH)
static int maxAttributeLength(@Named(ClientComponent.PlatformConstants.INT_DEVICE_SDK) int deviceSdk) {
if (deviceSdk >= 33 /* Build.VERSION_CODES.TIRAMISU / Android 13 */) {
return GATT_MAX_ATTRIBUTE_LENGTH_API_33;
}
return GATT_MAX_ATTRIBUTE_LENGTH;
}

@Provides
static BluetoothGatt provideBluetoothGatt(BluetoothGattProvider bluetoothGattProvider) {
return bluetoothGattProvider.getBluetoothGatt();
Expand Down Expand Up @@ -112,4 +127,4 @@ static BluetoothGatt provideBluetoothGatt(BluetoothGattProvider bluetoothGattPro

@Binds
abstract DisconnectionRouterOutput bindDisconnectionRouterOutput(DisconnectionRouter disconnectionRouter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ class MtuBasedPayloadSizeLimit implements PayloadSizeLimitProvider {

private final RxBleConnection rxBleConnection;
private final int gattWriteMtuOverhead;
private final int maxAttributeLength;

@Inject
MtuBasedPayloadSizeLimit(RxBleConnection rxBleConnection,
@Named(ConnectionComponent.NamedInts.GATT_WRITE_MTU_OVERHEAD) int gattWriteMtuOverhead) {
@Named(ConnectionComponent.NamedInts.GATT_WRITE_MTU_OVERHEAD) int gattWriteMtuOverhead,
@Named(ConnectionComponent.NamedInts.GATT_MAX_ATTR_LENGTH) int maxAttributeLength) {
this.rxBleConnection = rxBleConnection;
this.gattWriteMtuOverhead = gattWriteMtuOverhead;
this.maxAttributeLength = maxAttributeLength;
}

@Override
public int getPayloadSizeLimit() {
return rxBleConnection.getMtu() - gattWriteMtuOverhead;
int maxWritePayloadForMtu = rxBleConnection.getMtu() - gattWriteMtuOverhead;
// See https://issuetracker.google.com/issues/307234027}
return Math.min(maxWritePayloadForMtu, maxAttributeLength);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@ class MtuBasedPayloadSizeLimitTest extends Specification {

MtuBasedPayloadSizeLimit objectUnderTest

private void prepareObjectUnderTest(int gattWriteMtuOverhead) {
objectUnderTest = new MtuBasedPayloadSizeLimit(mockBleConnection, gattWriteMtuOverhead)
private void prepareObjectUnderTest(int gattWriteMtuOverhead, int maxAttributeLength) {
objectUnderTest = new MtuBasedPayloadSizeLimit(mockBleConnection, gattWriteMtuOverhead, maxAttributeLength)
}

@Unroll
def "should return current MTU decreased by the write MTU overhead"() {
def "should return current MTU decreased by the write MTU overhead but no more than the maxAttributeLength"() {

given:
prepareObjectUnderTest(gattWriteMtuOverhead)
prepareObjectUnderTest(gattWriteMtuOverhead, maxAttributeLength)
mockBleConnection.getMtu() >> currentMtu

expect:
objectUnderTest.getPayloadSizeLimit() == expectedValue

where:
currentMtu | gattWriteMtuOverhead | expectedValue
10 | 2 | 8
2000 | 32 | 1968
currentMtu | gattWriteMtuOverhead | maxAttributeLength | expectedValue
10 | 2 | 2000 | 8
2000 | 32 | 2000 | 1968
10 | 2 | 512 | 8
2000 | 32 | 512 | 512
}
}
}
Loading