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

Commit 8ad5517e authored by Myles Watson's avatar Myles Watson
Browse files

AdapterService: Support GATT as the only profile

Bug: 72435402
Bug: 73505205
Bug: 26060309
Test: runtest bluetooth -m testEnableDisableOnlyGatt
Change-Id: I3cfda91dba8a914a5419c56cb37ebc4e6a6bf8f8
parent 292bc3c6
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -554,19 +554,27 @@ public class AdapterService extends Service {
    void startProfileServices() {
        debugLog("startCoreServices()");
        Class[] supportedProfileServices = Config.getSupportedProfiles();
        if (supportedProfileServices.length == 1 && GattService.class.getSimpleName()
                .equals(supportedProfileServices[0].getSimpleName())) {
            updateUuids();
            setBluetoothClassFromConfig();
            mAdapterStateMachine.sendMessage(AdapterState.BREDR_STARTED);
        } else {
            setAllProfileServiceStates(supportedProfileServices, BluetoothAdapter.STATE_ON);
        }
    }

    void stopProfileServices() {
        mAdapterProperties.onBluetoothDisable();
        Class[] supportedProfileServices = Config.getSupportedProfiles();
        if (mRunningProfiles.size() < 1 || (mRunningProfiles.size() == 1
        if (supportedProfileServices.length == 1 && (mRunningProfiles.size() == 1
                && GattService.class.getSimpleName().equals(mRunningProfiles.get(0).getName()))) {
            debugLog("stopProfileServices() - No profiles services to stop or already stopped.");
            mAdapterStateMachine.sendMessage(AdapterState.BREDR_STOPPED);
        }
        } else {
            setAllProfileServiceStates(supportedProfileServices, BluetoothAdapter.STATE_OFF);
        }
    }

    private void stopGattProfileService() {
        mAdapterProperties.onBleDisable();
+57 −23
Original line number Diff line number Diff line
@@ -142,17 +142,19 @@ public class AdapterServiceTest {
        }
    }

    private void doEnable(int invocationNumber) {
    private void doEnable(int invocationNumber, boolean onlyGatt) {
        Assert.assertFalse(mAdapterService.isEnabled());

        final int startServiceCalls = 2 * (onlyGatt ? 1 : 3); // Start and stop GATT + 2

        mAdapterService.enable();

        verifyStateChange(BluetoothAdapter.STATE_OFF, BluetoothAdapter.STATE_BLE_TURNING_ON,
                invocationNumber + 1, CONTEXT_SWITCH_MS);

        // Start GATT
        verify(mMockContext,
                timeout(CONTEXT_SWITCH_MS).times(6 * invocationNumber + 1)).startService(any());
        verify(mMockContext, timeout(CONTEXT_SWITCH_MS).times(
                startServiceCalls * invocationNumber + 1)).startService(any());
        mAdapterService.addProfile(mMockGattService);
        mAdapterService.onProfileServiceStateChanged(mMockGattService, BluetoothAdapter.STATE_ON);

@@ -164,13 +166,15 @@ public class AdapterServiceTest {
        verifyStateChange(BluetoothAdapter.STATE_BLE_ON, BluetoothAdapter.STATE_TURNING_ON,
                invocationNumber + 1, CONTEXT_SWITCH_MS);

        if (!onlyGatt) {
            // Start Mock PBAP and PAN services
        verify(mMockContext, timeout(ONE_SECOND_MS).times(6 * invocationNumber + 3)).startService(
                any());
            verify(mMockContext, timeout(ONE_SECOND_MS).times(
                    startServiceCalls * invocationNumber + 3)).startService(any());
            mAdapterService.addProfile(mMockService);
            mAdapterService.addProfile(mMockService2);
            mAdapterService.onProfileServiceStateChanged(mMockService, BluetoothAdapter.STATE_ON);
            mAdapterService.onProfileServiceStateChanged(mMockService2, BluetoothAdapter.STATE_ON);
        }

        verifyStateChange(BluetoothAdapter.STATE_TURNING_ON, BluetoothAdapter.STATE_ON,
                invocationNumber + 1, CONTEXT_SWITCH_MS);
@@ -178,19 +182,23 @@ public class AdapterServiceTest {
        Assert.assertTrue(mAdapterService.isEnabled());
    }

    private void doDisable(int invocationNumber) {
    private void doDisable(int invocationNumber, boolean onlyGatt) {
        Assert.assertTrue(mAdapterService.isEnabled());

        final int startServiceCalls = 2 * (onlyGatt ? 1 : 3); // Start and stop GATT + 2

        mAdapterService.disable();

        verifyStateChange(BluetoothAdapter.STATE_ON, BluetoothAdapter.STATE_TURNING_OFF,
                invocationNumber + 1, CONTEXT_SWITCH_MS);

        if (!onlyGatt) {
            // Stop PBAP and PAN
        verify(mMockContext, timeout(ONE_SECOND_MS).times(6 * invocationNumber + 5)).startService(
                any());
            verify(mMockContext, timeout(ONE_SECOND_MS).times(
                    startServiceCalls * invocationNumber + 5)).startService(any());
            mAdapterService.onProfileServiceStateChanged(mMockService, BluetoothAdapter.STATE_OFF);
            mAdapterService.onProfileServiceStateChanged(mMockService2, BluetoothAdapter.STATE_OFF);
        }

        verifyStateChange(BluetoothAdapter.STATE_TURNING_OFF, BluetoothAdapter.STATE_BLE_ON,
                invocationNumber + 1, CONTEXT_SWITCH_MS);
@@ -201,8 +209,8 @@ public class AdapterServiceTest {
                invocationNumber + 1, CONTEXT_SWITCH_MS);

        // Stop GATT
        verify(mMockContext, timeout(ONE_SECOND_MS).times(6 * invocationNumber + 6)).startService(
                any());
        verify(mMockContext, timeout(ONE_SECOND_MS).times(
                startServiceCalls * invocationNumber + startServiceCalls)).startService(any());
        mAdapterService.onProfileServiceStateChanged(mMockGattService, BluetoothAdapter.STATE_OFF);

        verifyStateChange(BluetoothAdapter.STATE_BLE_TURNING_OFF, BluetoothAdapter.STATE_OFF,
@@ -217,7 +225,7 @@ public class AdapterServiceTest {
     */
    @Test
    public void testEnable() {
        doEnable(0);
        doEnable(0, false);
    }

    /**
@@ -226,8 +234,34 @@ public class AdapterServiceTest {
     */
    @Test
    public void testEnableDisable() {
        doEnable(0);
        doDisable(0);
        doEnable(0, false);
        doDisable(0, false);
    }

    /**
     * Test: Turn Bluetooth on/off with only GATT supported.
     * Check whether the AdapterService gets started and stopped.
     */
    @Test
    public void testEnableDisableOnlyGatt() {
        Context mockContext = mock(Context.class);
        Resources mockResources = mock(Resources.class);

        when(mockContext.getApplicationInfo()).thenReturn(mMockApplicationInfo);
        when(mockContext.getContentResolver()).thenReturn(mMockContentResolver);
        when(mockContext.getApplicationContext()).thenReturn(mockContext);
        when(mockContext.getResources()).thenReturn(mockResources);
        when(mockContext.getUserId()).thenReturn(Process.BLUETOOTH_UID);
        when(mockContext.getPackageManager()).thenReturn(mMockPackageManager);
        when(mockContext.getSystemService(Context.USER_SERVICE)).thenReturn(mMockUserManager);
        when(mockContext.getSystemService(Context.POWER_SERVICE)).thenReturn(mPowerManager);
        when(mockContext.getSystemService(Context.ALARM_SERVICE)).thenReturn(mMockAlarmManager);

        when(mockResources.getBoolean(R.bool.profile_supported_gatt)).thenReturn(true);

        Config.init(mockContext);
        doEnable(0, true);
        doDisable(0, true);
    }

    /**
@@ -267,7 +301,7 @@ public class AdapterServiceTest {
     */
    @Test
    public void testGattStopTimeout() {
        doEnable(0);
        doEnable(0, false);
        Assert.assertTrue(mAdapterService.isEnabled());

        mAdapterService.disable();
@@ -346,7 +380,7 @@ public class AdapterServiceTest {
     */
    @Test
    public void testProfileStopTimeout() {
        doEnable(0);
        doEnable(0, false);

        Assert.assertTrue(mAdapterService.isEnabled());