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

Commit a6001bc3 authored by Rongxuan Liu's avatar Rongxuan Liu
Browse files

Bass: Refactor bis sync operation usage

Add bis sync constants and keep them separate from bis sync state.
Also use long type for all bis sync use cases to avoid unexpected overflow behavior.

Bug: 384976631
Flag: EXEMPT; refactor only no functional change
Test: atest BassClientServiceTest BassClientStateMachineTest
Change-Id: I99983883bc15288074a421f25742972a213a40bd
parent e65953cc
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -4182,8 +4182,8 @@ public class BassClientService extends ProfileService {
    }

    private boolean isSyncedToBroadcastStream(Long syncState) {
        return syncState != BassConstants.BIS_SYNC_NOT_SYNC_TO_BIS
                && syncState != BassConstants.BIS_SYNC_FAILED_SYNC_TO_BIG;
        return syncState != BassConstants.BCAST_RCVR_STATE_BIS_SYNC_NOT_SYNC_TO_BIS
                && syncState != BassConstants.BCAST_RCVR_STATE_BIS_SYNC_FAILED_SYNC_TO_BIG;
    }

    /** Handle broadcast state changed */
+24 −23
Original line number Diff line number Diff line
@@ -939,15 +939,15 @@ class BassClientStateMachine extends StateMachine {
        // Check Bis state
        for (int i = 0; i < recvState.getNumSubgroups(); i++) {
            Long bisState = recvState.getBisSyncState().get(i);
            if (bisState != BassConstants.BIS_SYNC_FAILED_SYNC_TO_BIG
                    && bisState != BassConstants.BIS_SYNC_NOT_SYNC_TO_BIS) {
            if (bisState != BassConstants.BCAST_RCVR_STATE_BIS_SYNC_FAILED_SYNC_TO_BIG
                    && bisState != BassConstants.BCAST_RCVR_STATE_BIS_SYNC_NOT_SYNC_TO_BIS) {
                // Any bis synced, update status and break
                syncStats.updateBisSyncedTime(SystemClock.elapsedRealtime());
                syncStats.updateSyncStatus(
                        BluetoothStatsLog
                                .BROADCAST_AUDIO_SYNC_REPORTED__SYNC_STATUS__SYNC_STATUS_AUDIO_SYNC_SUCCESS);
                break;
            } else if (bisState == BassConstants.BIS_SYNC_FAILED_SYNC_TO_BIG) {
            } else if (bisState == BassConstants.BCAST_RCVR_STATE_BIS_SYNC_FAILED_SYNC_TO_BIG) {
                logBroadcastSyncStatsWithStatus(
                        broadcastId,
                        BluetoothStatsLog
@@ -2049,15 +2049,16 @@ class BassClientStateMachine extends StateMachine {
        }
    }

    private static int getBisSyncFromChannelPreference(List<BluetoothLeBroadcastChannel> channels) {
        int bisSync = 0;
    private static long getBisSyncFromChannelPreference(
            List<BluetoothLeBroadcastChannel> channels) {
        long bisSync = 0L;
        for (BluetoothLeBroadcastChannel channel : channels) {
            if (channel.isSelected()) {
                if (channel.getChannelIndex() == 0) {
                    Log.e(TAG, "getBisSyncFromChannelPreference: invalid channel index=0");
                    continue;
                }
                bisSync |= 1 << (channel.getChannelIndex() - 1);
                bisSync |= 1L << (channel.getChannelIndex() - 1);
            }
        }

@@ -2106,14 +2107,14 @@ class BassClientStateMachine extends StateMachine {

        for (BluetoothLeBroadcastSubgroup subGroup : subGroups) {
            // BIS_Sync
            int bisSync = getBisSyncFromChannelPreference(subGroup.getChannels());
            if (bisSync == 0) {
                bisSync = 0xFFFFFFFF;
            long bisSync = getBisSyncFromChannelPreference(subGroup.getChannels());
            if (bisSync == BassConstants.BIS_SYNC_DO_NOT_SYNC_TO_BIS) {
                bisSync = BassConstants.BIS_SYNC_NO_PREFERENCE;
            }
            stream.write(bisSync & 0x00000000000000FF);
            stream.write((bisSync & 0x000000000000FF00) >>> 8);
            stream.write((bisSync & 0x0000000000FF0000) >>> 16);
            stream.write((bisSync & 0x00000000FF000000) >>> 24);
            stream.write((byte) (bisSync & 0x00000000000000FFL));
            stream.write((byte) ((bisSync & 0x000000000000FF00L) >>> 8));
            stream.write((byte) ((bisSync & 0x0000000000FF0000L) >>> 16));
            stream.write((byte) ((bisSync & 0x00000000FF000000L) >>> 24));

            // Metadata_Length
            BluetoothLeAudioContentMetadata metadata = subGroup.getContentMetadata();
@@ -2162,14 +2163,14 @@ class BassClientStateMachine extends StateMachine {
        res[offset++] = (byte) numSubGroups;

        for (int i = 0; i < numSubGroups; i++) {
            int bisIndexValue = 0xFFFFFFFF;
            int currentBisIndexValue = 0xFFFFFFFF;
            long bisIndexValue = BassConstants.BIS_SYNC_NO_PREFERENCE;
            long currentBisIndexValue = BassConstants.BIS_SYNC_NO_PREFERENCE;
            if (i < existingState.getBisSyncState().size()) {
                currentBisIndexValue = existingState.getBisSyncState().get(i).intValue();
                currentBisIndexValue = existingState.getBisSyncState().get(i);
            }

            if (paSync == BassConstants.PA_SYNC_DO_NOT_SYNC) {
                bisIndexValue = 0;
                bisIndexValue = BassConstants.BIS_SYNC_DO_NOT_SYNC_TO_BIS;
            } else if (metaData != null) {
                bisIndexValue =
                        getBisSyncFromChannelPreference(
@@ -2179,8 +2180,8 @@ class BassClientStateMachine extends StateMachine {
                if (paSync == BassConstants.PA_SYNC_PAST_AVAILABLE
                        || paSync == BassConstants.PA_SYNC_PAST_NOT_AVAILABLE) {
                    // Let sink decide to which BIS sync if there is no channel preference
                    if (bisIndexValue == 0) {
                        bisIndexValue = 0xFFFFFFFF;
                    if (bisIndexValue == BassConstants.BIS_SYNC_DO_NOT_SYNC_TO_BIS) {
                        bisIndexValue = BassConstants.BIS_SYNC_NO_PREFERENCE;
                    }
                }
            } else {
@@ -2193,10 +2194,10 @@ class BassClientStateMachine extends StateMachine {
                            + " to: "
                            + bisIndexValue);
            // BIS_Sync
            res[offset++] = (byte) (bisIndexValue & 0x00000000000000FF);
            res[offset++] = (byte) ((bisIndexValue & 0x000000000000FF00) >>> 8);
            res[offset++] = (byte) ((bisIndexValue & 0x0000000000FF0000) >>> 16);
            res[offset++] = (byte) ((bisIndexValue & 0x00000000FF000000) >>> 24);
            res[offset++] = (byte) (bisIndexValue & 0x00000000000000FFL);
            res[offset++] = (byte) ((bisIndexValue & 0x000000000000FF00L) >>> 8);
            res[offset++] = (byte) ((bisIndexValue & 0x0000000000FF0000L) >>> 16);
            res[offset++] = (byte) ((bisIndexValue & 0x00000000FF000000L) >>> 24);
            // Metadata_Length; On Modify source, don't update any Metadata
            res[offset++] = 0;
        }
+7 −4
Original line number Diff line number Diff line
@@ -62,6 +62,9 @@ public class BassConstants {
    public static final int BCAST_RCVR_STATE_BADCODE_START_IDX = 14;
    public static final int BCAST_RCVR_STATE_BADCODE_SIZE = 16;
    public static final int BCAST_RCVR_STATE_BIS_SYNC_SIZE = 4;
    // BIS_Sync State value
    public static final long BCAST_RCVR_STATE_BIS_SYNC_NOT_SYNC_TO_BIS = 0x00000000L;
    public static final long BCAST_RCVR_STATE_BIS_SYNC_FAILED_SYNC_TO_BIG = 0xFFFFFFFFL;
    // 30 secs time out for all gatt writes
    public static final int GATT_TXN_TIMEOUT_MS = 30000;
    public static final int SOURCE_OPERATION_TIMEOUT_MS = 3000;
@@ -75,11 +78,11 @@ public class BassConstants {
    public static final int BCAST_NAME_AD_TYPE = 0x30;
    public static final int BCAST_NAME_LEN_MIN = 4;
    public static final int BCAST_NAME_LEN_MAX = 32;
    // PA_Sync parameter value
    // PA_Sync parameter value in BASS operations
    public static final int PA_SYNC_DO_NOT_SYNC = 0x00;
    public static final int PA_SYNC_PAST_AVAILABLE = 0x01;
    public static final int PA_SYNC_PAST_NOT_AVAILABLE = 0x02;
    // BIS_Sync parameter value
    public static final long BIS_SYNC_NOT_SYNC_TO_BIS = 0x00000000L;
    public static final long BIS_SYNC_FAILED_SYNC_TO_BIG = 0xFFFFFFFFL;
    // BIS_Sync parameter value in BASS operations
    public static final long BIS_SYNC_DO_NOT_SYNC_TO_BIS = 0x00000000L;
    public static final long BIS_SYNC_NO_PREFERENCE = 0xFFFFFFFFL;
}
+1 −1
Original line number Diff line number Diff line
@@ -2981,7 +2981,7 @@ public class BassClientStateMachineTest {
                TEST_SOURCE_ID,
                BluetoothLeBroadcastReceiveState.PA_SYNC_STATE_SYNCHRONIZED,
                BluetoothLeBroadcastReceiveState.BIG_ENCRYPTION_STATE_DECRYPTING,
                BassConstants.BIS_SYNC_FAILED_SYNC_TO_BIG);
                BassConstants.BCAST_RCVR_STATE_BIS_SYNC_FAILED_SYNC_TO_BIG);
        // Verify broadcast audio session is logged when bis sync failed
        verify(mMetricsLogger)
                .logLeAudioBroadcastAudioSync(