Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 590782a8 authored by Kyunglyul Hyun's avatar Kyunglyul Hyun
Browse files

Returns correct gatt interface

Fixed an issue where dynamic alloction for the
GATT interface was not working.

Added a test to make 100 connections and
read characteristics to ensure functionality.

Bug: 348559823
Bug: 273561907
Test: atest GattClientTest
Change-Id: I3181f5969ba0984e20d59cb25cacd7b428cd85de
parent 9bea7d83
Loading
Loading
Loading
Loading
+41 −4
Original line number Diff line number Diff line
@@ -70,7 +70,9 @@ import pandora.HostProto.AdvertiseResponse;
import pandora.HostProto.OwnAddressType;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

@RunWith(TestParameterInjector.class)
@@ -214,7 +216,7 @@ public class GattClientTest {

    @Test
    public void clientGattWriteCharacteristic() throws Exception {
        registerWritableGattService();
        registerGattService();

        BluetoothGattCallback gattCallback = mock(BluetoothGattCallback.class);
        BluetoothGatt gatt = connectGattAndWaitConnection(gattCallback);
@@ -306,7 +308,7 @@ public class GattClientTest {
    public void consecutiveWriteCharacteristicFails_thenSuccess() throws Exception {
        Assume.assumeTrue(Flags.gattFixDeviceBusy());

        registerWritableGattService();
        registerGattService();

        BluetoothGattCallback gattCallback = mock(BluetoothGattCallback.class);
        BluetoothGattCallback gattCallback2 = mock(BluetoothGattCallback.class);
@@ -384,10 +386,12 @@ public class GattClientTest {
        }
    }

    private void registerWritableGattService() {
    private void registerGattService() {
        GattCharacteristicParams characteristicParams =
                GattCharacteristicParams.newBuilder()
                        .setProperties(BluetoothGattCharacteristic.PROPERTY_WRITE)
                        .setProperties(
                                BluetoothGattCharacteristic.PROPERTY_READ
                                        | BluetoothGattCharacteristic.PROPERTY_WRITE)
                        .setUuid(TEST_CHARACTERISTIC_UUID.toString())
                        .build();

@@ -565,4 +569,37 @@ public class GattClientTest {
            disconnectAndWaitDisconnection(gatt, gattCallback);
        }
    }

    // Check if we can have 100 simultaneous clients
    @Test
    @RequiresFlagsEnabled(Flags.FLAG_GATT_CLIENT_DYNAMIC_ALLOCATION)
    public void connectGatt_multipleClients() {
        advertiseWithBumble();
        registerGattService();

        List<BluetoothGatt> gatts = new ArrayList<>();
        final int repeatTimes = 100;

        try {
            for (int i = 0; i < repeatTimes; i++) {
                BluetoothGattCallback gattCallback = mock(BluetoothGattCallback.class);
                BluetoothGatt gatt = connectGattAndWaitConnection(gattCallback);
                gatts.add(gatt);
                gatt.discoverServices();
                verify(gattCallback, timeout(10000)).onServicesDiscovered(any(), eq(GATT_SUCCESS));

                BluetoothGattCharacteristic characteristic =
                        gatt.getService(TEST_SERVICE_UUID)
                                .getCharacteristic(TEST_CHARACTERISTIC_UUID);
                gatt.readCharacteristic(characteristic);
                verify(gattCallback, timeout(5000))
                        .onCharacteristicRead(any(), any(), any(), anyInt());
            }
        } finally {
            for (BluetoothGatt gatt : gatts) {
                gatt.disconnect();
                gatt.close();
            }
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -192,7 +192,7 @@ void bta_gattc_register(const Uuid& app_uuid, tBTA_GATTC_CBACK* p_cback, BtaAppR
  }

  if (com::android::bluetooth::flags::gatt_client_dynamic_allocation()) {
    tGATT_IF client_if = GATT_Register(app_uuid, "GattClient", &bta_gattc_cl_cback, eatt_support);
    client_if = GATT_Register(app_uuid, "GattClient", &bta_gattc_cl_cback, eatt_support);
    if (client_if == 0) {
      log::error("Register with GATT stack failed");
      status = GATT_ERROR;
+1 −0
Original line number Diff line number Diff line
@@ -1302,6 +1302,7 @@ static tGATT_IF GATT_Register_Dynamic(const Uuid& app_uuid128, const std::string
      if (gatt_cb.next_gatt_if == 0) {
        gatt_cb.next_gatt_if = 1;
      }
      return p_reg->gatt_if;
    }
    i_gatt_if++;
    if (i_gatt_if == 0) {