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

Commit c11d8485 authored by Wei Wang's avatar Wei Wang Committed by Android (Google) Code Review
Browse files

Merge "Clear pending data and allow all filter when batch stopped." into lmp-dev

parents 998d0eef 71c8c4ec
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -34,7 +34,6 @@ import com.android.bluetooth.btservice.AdapterService;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
@@ -207,7 +206,6 @@ class AdvertiseManager {
            if (client == null) {
                return;
            }
            int clientIf = client.clientIf;
            logd("advertise clients size " + mAdvertiseClients.size());
            if (mAdvertiseClients.contains(client)) {
                mAdvertiseNative.stopAdvertising(client);
+49 −9
Original line number Diff line number Diff line
@@ -220,7 +220,7 @@ public class GattService extends ProfileService {

        public void binderDied() {
            if (DBG) Log.d(TAG, "Binder is dead - unregistering client (" + mAppIf + ")!");
            if (mScanManager.scanQueue().contains(new ScanClient(mAppIf, false))) {
            if (isScanClient(mAppIf)) {
                stopScan(mAppIf, false);
            } else {
                stopMultiAdvertising(mAppIf);
@@ -229,6 +229,20 @@ public class GattService extends ProfileService {
            // condition.
            unregisterClient(mAppIf);
        }

        private boolean isScanClient(int clientIf) {
            for (ScanClient client : mScanManager.getRegularScanQueue()) {
                if (client.clientIf == clientIf) {
                    return true;
                }
            }
            for (ScanClient client : mScanManager.getBatchScanQueue()) {
                if (client.clientIf == clientIf) {
                    return true;
                }
            }
            return false;
        }
    }

    class ServerDeathRecipient implements IBinder.DeathRecipient {
@@ -539,7 +553,7 @@ public class GattService extends ProfileService {
                    + ", rssi=" + rssi);
        ScanRecord record = ScanRecord.parseFromBytes(adv_data);
        List<UUID> remoteUuids = parseUuids(adv_data);
        for (ScanClient client : mScanManager.scanQueue()) {
        for (ScanClient client : mScanManager.getRegularScanQueue()) {
            if (client.uuids.length > 0) {
                int matches = 0;
                for (UUID search : client.uuids) {
@@ -950,13 +964,37 @@ public class GattService extends ProfileService {
            Log.d(TAG, "onBatchScanReports() - clientIf=" + clientIf + ", status=" + status
                    + ", reportType=" + reportType + ", numRecords=" + numRecords);
        }
        mScanManager.callbackDone(clientIf, status);
        Set<ScanResult> results = parseBatchScanResults(numRecords, reportType, recordData);
        if (reportType == ScanManager.SCAN_RESULT_TYPE_TRUNCATED) {
            // We only support single client for truncated mode.
            ClientMap.App app = mClientMap.getById(clientIf);
            if (app == null) return;
        Set<ScanResult> results = parseBatchScanResults(numRecords, reportType, recordData);
        for (ScanResult result : new ArrayList<ScanResult>(results)) {
            Log.d(TAG, result.getScanRecord().toString());
        }
            app.callback.onBatchScanResults(new ArrayList<ScanResult>(results));
        } else {
            for (ScanClient client : mScanManager.getBatchScanQueue()) {
                // Deliver results for each client.
                deliverBatchScan(client, results);
            }
        }
    }

    // Check and deliver scan results for different scan clients.
    private void deliverBatchScan(ScanClient client, Set<ScanResult> allResults) throws
            RemoteException {
        ClientMap.App app = mClientMap.getById(client.clientIf);
        if (app == null) return;
        if (client.filters == null || client.filters.isEmpty()) {
            app.callback.onBatchScanResults(new ArrayList<ScanResult>(allResults));
        }
        // Reconstruct the scan results.
        List<ScanResult> results = new ArrayList<ScanResult>();
        for (ScanResult scanResult : allResults) {
            if (matchesFilters(client, scanResult)) {
                results.add(scanResult);
            }
        }
        app.callback.onBatchScanResults(results);
    }

    private Set<ScanResult> parseBatchScanResults(int numRecords, int reportType,
@@ -1228,7 +1266,9 @@ public class GattService extends ProfileService {

    void stopScan(int appIf, boolean isServer) {
        enforceAdminPermission();
        if (DBG) Log.d(TAG, "stopScan() - queue=" + mScanManager.scanQueue().size());
        int scanQueueSize = mScanManager.getBatchScanQueue().size() +
                mScanManager.getRegularScanQueue().size();
        if (DBG) Log.d(TAG, "stopScan() - queue size =" + scanQueueSize);
        mScanManager.stopScan(new ScanClient(appIf, isServer));
    }

+18 −10
Original line number Diff line number Diff line
@@ -101,13 +101,17 @@ public class ScanManager {
    }

    /**
     * Returns the combined scan queue of regular scans and batch scans.
     * Returns the regular scan queue.
     */
    List<ScanClient> scanQueue() {
        List<ScanClient> clients = new ArrayList<>();
        clients.addAll(mRegularScanClients);
        clients.addAll(mBatchClients);
        return clients;
    Set<ScanClient> getRegularScanQueue() {
        return mRegularScanClients;
    }

    /**
     * Returns batch scan queue.
     */
    Set<ScanClient> getBatchScanQueue() {
        return mBatchClients;
    }

    void startScan(ScanClient client) {
@@ -195,11 +199,9 @@ public class ScanManager {
        void handleStopScan(ScanClient client) {
            Utils.enforceAdminPermission(mService);
            if (mRegularScanClients.contains(client)) {
                mRegularScanClients.remove(client);
                mScanNative.configureRegularScanParams();
                mScanNative.stopRegularScan(client);
            } else {
                mBatchClients.remove(client);
                mScanNative.stopBatchScan(client);
            }
        }
@@ -286,7 +288,7 @@ public class ScanManager {
                                if (mBatchClients.isEmpty()) {
                                    return;
                                }
                                // TODO: find out if we need to flush all clients at once.
                                // Note this actually flushes all pending batch data.
                                flushBatchScanResults(mBatchClients.iterator().next());
                            }
                        }
@@ -418,9 +420,10 @@ public class ScanManager {
        }

        void stopBatchScan(ScanClient client) {
            flushBatchResults(client.clientIf);
            removeScanFilters(client.clientIf);
            mBatchClients.remove(client);
            gattClientStopBatchScanNative(client.clientIf);
            mBatchClients.remove(client);
            setBatchAlarm();
        }

@@ -432,7 +435,9 @@ public class ScanManager {
                return;
            }
            int resultType = getResultType(client.settings);
            resetCountDownLatch();
            gattClientReadScanReportsNative(client.clientIf, resultType);
            waitForCallback();
        }

        void cleanup() {
@@ -463,6 +468,9 @@ public class ScanManager {
                resetCountDownLatch();
                configureFilterParamter(clientIf, client, ALLOW_ALL_FILTER_SELECTION,
                        ALLOW_ALL_FILTER_INDEX);
                Deque<Integer> clientFilterIndices = new ArrayDeque<Integer>();
                clientFilterIndices.add(ALLOW_ALL_FILTER_INDEX);
                mClientFilterIndexMap.put(clientIf, clientFilterIndices);
                waitForCallback();
            } else {
                Deque<Integer> clientFilterIndices = new ArrayDeque<Integer>();