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

Commit 91713457 authored by Rahul Sabnis's avatar Rahul Sabnis
Browse files

Tie TBS and MCS authorization to the LEA connection policy

Bug: 279990836
Test: atest LeAudioServiceTest
Change-Id: Ic30955beec537b0d25aefc1f6c8f185d751511c6
parent 3693e36e
Loading
Loading
Loading
Loading
+11 −1
Original line number Original line Diff line number Diff line
@@ -2413,9 +2413,17 @@ public class LeAudioService extends ProfileService {
                connectionPolicy)) {
                connectionPolicy)) {
            return false;
            return false;
        }
        }

        if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
        if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            // Authorizes LEA GATT server services if already assigned to a group
            int groupId = getGroupId(device);
            if (groupId != LE_AUDIO_GROUP_ID_INVALID) {
                setAuthorizationForRelatedProfiles(device, true);
            }
            connect(device);
            connect(device);
        } else if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
        } else if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
            // Remove authorization for LEA GATT server services
            setAuthorizationForRelatedProfiles(device, false);
            disconnect(device);
            disconnect(device);
        }
        }
        return true;
        return true;
@@ -2555,9 +2563,11 @@ public class LeAudioService extends ProfileService {


        synchronized (mGroupLock) {
        synchronized (mGroupLock) {
            for (BluetoothDevice device : mDeviceDescriptors.keySet()) {
            for (BluetoothDevice device : mDeviceDescriptors.keySet()) {
                if (getConnectionPolicy(device) != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
                    setAuthorizationForRelatedProfiles(device, true);
                    setAuthorizationForRelatedProfiles(device, true);
                }
                }
            }
            }
        }


        startAudioServersBackgroundScan(/* retry = */ false);
        startAudioServersBackgroundScan(/* retry = */ false);
    }
    }
+42 −0
Original line number Original line Diff line number Diff line
@@ -1747,6 +1747,48 @@ public class LeAudioServiceTest {
        verify(mMcpService, times(1)).setDeviceAuthorized(mRightDevice, false);
        verify(mMcpService, times(1)).setDeviceAuthorized(mRightDevice, false);
    }
    }


    /**
     * Test verifying that when the LE Audio connection policy of a device is set to
     * {@link BluetoothProfile#CONNECTION_POLICY_FORBIDDEN}, we unauthorize McpService and
     * TbsService. When the LE Audio connection policy is set to
     * {@link BluetoothProfile#CONNECTION_POLICY_ALLOWED}, we will authorize these services.
     */
    @Test
    public void testMcsAndTbsAuthorizationWithConnectionPolicy() {
        int groupId = 1;

        mService.handleBluetoothEnabled();
        doReturn(true).when(mNativeInterface).connectLeAudio(any(BluetoothDevice.class));
        doReturn(true).when(mNativeInterface).disconnectLeAudio(any(BluetoothDevice.class));
        doReturn(true).when(mDatabaseManager).setProfileConnectionPolicy(any(BluetoothDevice.class),
                anyInt(), anyInt());
        when(mDatabaseManager.getProfileConnectionPolicy(mSingleDevice, BluetoothProfile.LE_AUDIO))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_UNKNOWN);

        // Ensures GATT server services are not authorized when the device does not have a group
        assertThat(mService.setConnectionPolicy(mSingleDevice,
                BluetoothProfile.CONNECTION_POLICY_ALLOWED)).isTrue();
        verify(mMcpService, never()).setDeviceAuthorized(mSingleDevice, false);
        verify(mTbsService, never()).setDeviceAuthorized(mSingleDevice, false);

        // Connects the test device and verifies GATT server services are authorized
        connectTestDevice(mSingleDevice, groupId);
        verify(mMcpService, times(1)).setDeviceAuthorized(mSingleDevice, true);
        verify(mTbsService, times(1)).setDeviceAuthorized(mSingleDevice, true);

        // Ensure that disconnecting unauthorizes GATT server services
        assertThat(mService.setConnectionPolicy(mSingleDevice,
                BluetoothProfile.CONNECTION_POLICY_FORBIDDEN)).isTrue();
        verify(mMcpService, times(1)).setDeviceAuthorized(mSingleDevice, false);
        verify(mTbsService, times(1)).setDeviceAuthorized(mSingleDevice, false);

        // Connecting a device that has a group re-authorizes the GATT server services
        assertThat(mService.setConnectionPolicy(mSingleDevice,
                BluetoothProfile.CONNECTION_POLICY_ALLOWED)).isTrue();
        verify(mMcpService, times(2)).setDeviceAuthorized(mSingleDevice, true);
        verify(mTbsService, times(2)).setDeviceAuthorized(mSingleDevice, true);
    }

    @Test
    @Test
    public void testGetGroupDevices() {
    public void testGetGroupDevices() {
        int firstGroupId = 1;
        int firstGroupId = 1;