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

Commit 18984fb4 authored by Jack He's avatar Jack He Committed by Gerrit Code Review
Browse files

Merge changes Ief24ac1d,Ic2909d05,I6e68ddb1,Ia1d38777,I5953c49b

* changes:
  bass: Add BassClientStateMachine unit tests
  BassClient: Verify connection state change intents
  BassClient: State machine cleanup
  BassClient: Add initial support for group operations
  tests: Multiple unit test fixes
parents 9c91318d 9cd1bea1
Loading
Loading
Loading
Loading
+458 −89

File changed.

Preview size limit exceeded, changes collapsed.

+40 −23
Original line number Diff line number Diff line
@@ -138,6 +138,10 @@ public class BassClientStateMachine extends StateMachine {
    static final int PSYNC_ACTIVE_TIMEOUT = 14;
    static final int CONNECT_TIMEOUT = 15;

    // NOTE: the value is not "final" - it is modified in the unit tests
    @VisibleForTesting
    static int sConnectTimeoutMs = BassConstants.CONNECT_TIMEOUT_MS;

    /*key is combination of sourceId, Address and advSid for this hashmap*/
    private final Map<Integer, BluetoothLeBroadcastReceiveState>
            mBluetoothLeBroadcastReceiveStates =
@@ -150,14 +154,15 @@ public class BassClientStateMachine extends StateMachine {
    private final ConnectedProcessing mConnectedProcessing = new ConnectedProcessing();
    private final List<BluetoothGattCharacteristic> mBroadcastCharacteristics =
            new ArrayList<BluetoothGattCharacteristic>();
    private final BluetoothDevice mDevice;
    @VisibleForTesting
    BluetoothDevice mDevice;

    private boolean mIsAllowedList = false;
    private int mLastConnectionState = -1;
    private boolean mMTUChangeRequested = false;
    private boolean mDiscoveryInitiated = false;
    private BassClientService mService;
    private BluetoothGatt mBluetoothGatt = null;
    @VisibleForTesting
    BassClientService mService;

    private BluetoothGattCharacteristic mBroadcastScanControlPoint;
    private boolean mFirstTimeBisDiscovery = false;
@@ -183,6 +188,9 @@ public class BassClientStateMachine extends StateMachine {
    private int mBroadcastSourceIdLength = 3;
    private byte mNextSourceId = 0;

    BluetoothGatt mBluetoothGatt = null;
    BluetoothGattCallback mGattCallback = null;

    BassClientStateMachine(BluetoothDevice device, BassClientService svc, Looper looper) {
        super(TAG + "(" + device.toString() + ")", looper);
        mDevice = device;
@@ -240,6 +248,7 @@ public class BassClientStateMachine extends StateMachine {
            mBluetoothGatt.disconnect();
            mBluetoothGatt.close();
            mBluetoothGatt = null;
            mGattCallback = null;
        }
        mPendingOperation = -1;
        mPendingSourceId = -1;
@@ -785,8 +794,8 @@ public class BassClientStateMachine extends StateMachine {
            if (oldRecvState.getSourceDevice() == null
                    || oldRecvState.getSourceDevice().getAddress().equals(emptyBluetoothDevice)) {
                log("New Source Addition");
                mService.getCallbacks().notifySourceAdded(mDevice,
                        recvState.getSourceId(), BluetoothStatusCodes.REASON_LOCAL_APP_REQUEST);
                mService.getCallbacks().notifySourceAdded(mDevice, recvState,
                        BluetoothStatusCodes.REASON_LOCAL_APP_REQUEST);
                if (mPendingMetadata != null) {
                    setCurrentBroadcastMetadata(recvState.getSourceId(), mPendingMetadata);
                }
@@ -823,8 +832,7 @@ public class BassClientStateMachine extends StateMachine {

    // Implements callback methods for GATT events that the app cares about.
    // For example, connection change and services discovered.
    private final BluetoothGattCallback mGattCallback =
            new BluetoothGattCallback() {
    final class GattCallback extends BluetoothGattCallback {
                @Override
                public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                    boolean isStateChanged = false;
@@ -977,6 +985,25 @@ public class BassClientStateMachine extends StateMachine {
                }
            };

    /**
     * Connects to the GATT server of the device.
     *
     * @return {@code true} if it successfully connects to the GATT server.
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    public boolean connectGatt(Boolean autoConnect) {
        if (mGattCallback == null) {
            mGattCallback = new GattCallback();
        }

        mBluetoothGatt = mDevice.connectGatt(mService, autoConnect,
                mGattCallback, BluetoothDevice.TRANSPORT_LE,
                (BluetoothDevice.PHY_LE_1M_MASK
                        | BluetoothDevice.PHY_LE_2M_MASK
                        | BluetoothDevice.PHY_LE_CODED_MASK), null);
        return mBluetoothGatt != null;
    }

    /**
     * getAllSources
     */
@@ -1050,11 +1077,7 @@ public class BassClientStateMachine extends StateMachine {
                if (mLastConnectionState != BluetoothProfile.STATE_DISCONNECTED) {
                    // Reconnect in background if not disallowed by the service
                    if (mService.okToConnect(mDevice)) {
                        mBluetoothGatt = mDevice.connectGatt(mService, true,
                                mGattCallback, BluetoothDevice.TRANSPORT_LE,
                                (BluetoothDevice.PHY_LE_1M_MASK
                                        | BluetoothDevice.PHY_LE_2M_MASK
                                        | BluetoothDevice.PHY_LE_CODED_MASK), null);
                        connectGatt(false);
                    }
                }
            }
@@ -1080,16 +1103,10 @@ public class BassClientStateMachine extends StateMachine {
                        mBluetoothGatt.close();
                        mBluetoothGatt = null;
                    }
                    mBluetoothGatt = mDevice.connectGatt(mService, mIsAllowedList,
                            mGattCallback, BluetoothDevice.TRANSPORT_LE, false,
                            (BluetoothDevice.PHY_LE_1M_MASK
                                    | BluetoothDevice.PHY_LE_2M_MASK
                                    | BluetoothDevice.PHY_LE_CODED_MASK), null);
                    if (mBluetoothGatt == null) {
                        Log.e(TAG, "Disconnected: error connecting to " + mDevice);
                        break;
                    } else {
                    if (connectGatt(mIsAllowedList)) {
                        transitionTo(mConnecting);
                    } else {
                        Log.e(TAG, "Disconnected: error connecting to " + mDevice);
                    }
                    break;
                case DISCONNECT:
@@ -1130,7 +1147,7 @@ public class BassClientStateMachine extends StateMachine {
        public void enter() {
            log("Enter Connecting(" + mDevice + "): "
                    + messageWhatToString(getCurrentMessage().what));
            sendMessageDelayed(CONNECT_TIMEOUT, mDevice, BassConstants.CONNECT_TIMEOUT_MS);
            sendMessageDelayed(CONNECT_TIMEOUT, mDevice, sConnectTimeoutMs);
            broadcastConnectionState(
                    mDevice, mLastConnectionState, BluetoothProfile.STATE_CONNECTING);
        }
@@ -1786,7 +1803,7 @@ public class BassClientStateMachine extends StateMachine {
        public void enter() {
            log("Enter Disconnecting(" + mDevice + "): "
                    + messageWhatToString(getCurrentMessage().what));
            sendMessageDelayed(CONNECT_TIMEOUT, mDevice, BassConstants.CONNECT_TIMEOUT_MS);
            sendMessageDelayed(CONNECT_TIMEOUT, mDevice, sConnectTimeoutMs);
            broadcastConnectionState(
                    mDevice, mLastConnectionState, BluetoothProfile.STATE_DISCONNECTING);
        }
+19 −0
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.utils.SynchronousResultReceiver;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -633,6 +634,24 @@ public class CsipSetCoordinatorService extends ProfileService {
                .collect(Collectors.toList());
    }

    /**
     * Get grouped devices
     * @param device group member device
     * @param uuid
     * @return related list of devices sorted from the lowest to the highest rank value.
     */
    public @NonNull List<BluetoothDevice> getGroupDevicesOrdered(BluetoothDevice device,
            ParcelUuid uuid) {
        List<Integer> groupIds = getAllGroupIds(uuid);
        for (Integer id : groupIds) {
            List<BluetoothDevice> devices = getGroupDevicesOrdered(id);
            if (devices.contains(device)) {
                return devices;
            }
        }
        return Collections.emptyList();
    }

    /**
     * Get group desired size
     * @param groupId group ID
+2 −1
Original line number Diff line number Diff line
@@ -104,7 +104,8 @@ public class HapClientNativeInterface {
        return Utils.getBytesFromAddress(device.getAddress());
    }

    private void sendMessageToService(HapClientStackEvent event) {
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    void sendMessageToService(HapClientStackEvent event) {
        HapClientService service = HapClientService.getHapClientService();
        if (service != null) {
            service.messageFromNative(event);
+753 −1

File changed.

Preview size limit exceeded, changes collapsed.

Loading