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

Commit a420dc6d authored by Jakub Tyszkowski's avatar Jakub Tyszkowski Committed by Łukasz Rymanowski
Browse files

Tbs: Fix exception on device authorization

Bug: 285528071
Test: atest BluetoothInstrumentationTests
Change-Id: Ibedcff1629ad1539436501a0c1979a7a2fd7d53d
parent 55d702bd
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -478,6 +478,8 @@ public class TbsGatt {

        private void notifyCharacteristicChanged(BluetoothDevice device,
                BluetoothGattCharacteristic characteristic, byte[] value) {
            if (getDeviceAuthorization(device) != BluetoothDevice.ACCESS_ALLOWED) return;
            if (value == null) return;
            if (mBluetoothGattServer != null) {
                mBluetoothGattServer.notifyCharacteristicChanged(device, characteristic, false,
                                                                 value);
@@ -487,6 +489,7 @@ public class TbsGatt {
        private void notifyCharacteristicChanged(BluetoothDevice device,
                BluetoothGattCharacteristic characteristic) {
            if (getDeviceAuthorization(device) != BluetoothDevice.ACCESS_ALLOWED) return;

            if (mBluetoothGattServer != null) {
                mBluetoothGattServer.notifyCharacteristicChanged(device, characteristic, false);
            }
+46 −0
Original line number Diff line number Diff line
@@ -133,6 +133,7 @@ public class TbsGattTest {
        Assert.assertNotNull(mMockGattServer);

        verify(mMockGattServer).addService(mGattServiceCaptor.capture());
        doReturn(mGattServiceCaptor.getValue()).when(mMockGattServer).getService(any(UUID.class));
        Assert.assertNotNull(mMockGattServer);
    }

@@ -823,6 +824,51 @@ public class TbsGattTest {
                        eq(BluetoothGatt.GATT_INSUFFICIENT_AUTHORIZATION), eq(0), any());
    }

    @Test
    public void testCharacteristicNotifyOnAuthorization() {
        prepareDefaultService();
        Assert.assertNotNull(mGattServiceCaptor.getValue());

        BluetoothGattCharacteristic characteristic = getCharacteristic(TbsGatt.UUID_STATUS_FLAGS);
        configureNotifications(mFirstDevice, characteristic, true);
        configureNotifications(mSecondDevice, characteristic, true);

        doReturn(mGattServiceCaptor.getValue()).when(mMockGattServer).getService(any(UUID.class));
        Assert.assertNotNull(mGattServiceCaptor.getValue());

        // Leave it as unauthorized yet
        doReturn(BluetoothDevice.ACCESS_REJECTED)
                .when(mMockTbsService)
                .getDeviceAuthorization(any(BluetoothDevice.class));

        int statusFlagValue = TbsGatt.STATUS_FLAG_SILENT_MODE_ENABLED;

        // Assume no update on one of the characteristics
        BluetoothGattCharacteristic characteristic2 = getCharacteristic(TbsGatt.UUID_CALL_STATE);
        characteristic2.setValue((byte[]) null);

        Assert.assertNotNull(mGattServiceCaptor.getValue());

        // Call it once but expect no notification for the unauthorized device
        byte[] valueBytes = new byte[2];
        valueBytes[0] = (byte) (statusFlagValue & 0xFF);
        valueBytes[1] = (byte) ((statusFlagValue >> 8) & 0xFF);
        mTbsGatt.setSilentModeFlag();
        verify(mMockGattServer, times(0))
                .notifyCharacteristicChanged(any(), eq(characteristic), eq(false), eq(valueBytes));

        // Expect a single notification for the just authorized device
        doReturn(BluetoothDevice.ACCESS_ALLOWED)
                .when(mMockTbsService)
                .getDeviceAuthorization(any(BluetoothDevice.class));
        Assert.assertNotNull(mGattServiceCaptor.getValue());
        mTbsGatt.onDeviceAuthorizationSet(mFirstDevice);
        verify(mMockGattServer, times(0))
                .notifyCharacteristicChanged(any(), eq(characteristic2), eq(false));
        verify(mMockGattServer, times(1))
                .notifyCharacteristicChanged(any(), eq(characteristic), eq(false));
    }

    @Test
    public void testCharacteristicReadAccessUnknownUnauthorized() {
        prepareDefaultService();