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

Commit b7d96bb1 authored by Prerepa Viswanadham's avatar Prerepa Viswanadham
Browse files

Refactor ScanClient scan settings to one place

Scan_mode_low_power    500ms/5000ms  (10%)
scan_mode_balanced     2000ms/5000ms (40%)
scan_mode_low_latency  5000ms/5000ms (100%)

Change-Id: I5f24dc24fc86eba35c645a968e1c80934e3dd2d5
parent ae3e916d
Loading
Loading
Loading
Loading
+44 −45
Original line number Diff line number Diff line
@@ -90,6 +90,16 @@ public class GattService extends ProfileService {
    static final int SCAN_FILTER_ENABLED = 1;
    static final int SCAN_FILTER_MODIFIED = 2;

    /**
     * Scan params corresponding to scan setting
     */
    private static final int SCAN_MODE_LOW_POWER_WINDOW_MS = 500;
    private static final int SCAN_MODE_LOW_POWER_INTERVAL_MS = 5000;
    private static final int SCAN_MODE_BALANCED_WINDOW_MS = 2000;
    private static final int SCAN_MODE_BALANCED_INTERVAL_MS = 5000;
    private static final int SCAN_MODE_LOW_LATENCY_WINDOW_MS = 5000;
    private static final int SCAN_MODE_LOW_LATENCY_INTERVAL_MS = 5000;

    /**
     * Search queue to serialize remote onbject inspection.
     */
@@ -121,7 +131,8 @@ public class GattService extends ProfileService {
    private byte[] mManufacturerData = new byte[0];
    private Integer mAdvertisingState = BluetoothAdapter.STATE_ADVERTISE_STOPPED;
    private final Object mLock = new Object();
    private static int lastConfiguredDutyCycle = 0;
    private static int lastConfiguredScanSetting = Integer.MIN_VALUE;
    private int mMaxScanFilters;

    /**
     * Pending service declaration queue
@@ -331,18 +342,6 @@ public class GattService extends ProfileService {
            service.startScanWithUuids(appIf, isServer, uuids);
        }

        public void startScanWithUuidsScanParam(int appIf, boolean isServer,
                ParcelUuid[] ids, int scanWindow, int scanInterval) {
            GattService service = getService();
            if (service == null) return;
            UUID[] uuids = new UUID[ids.length];
            for(int i = 0; i < ids.length; ++i) {
                uuids[i] = ids[i].getUuid();
            }
            service.startScanWithUuidsScanParam(appIf, isServer, uuids,
                    scanWindow, scanInterval);
        }

        @Override
        public void startScanWithFilters(int appIf, boolean isServer, ScanSettings settings,
                List<ScanFilter> filters) {
@@ -1225,21 +1224,6 @@ public class GattService extends ProfileService {
        configureScanParams();
    }

    void startScanWithUuidsScanParam(int appIf, boolean isServer, UUID[] uuids,
                int scanWindow, int scanInterval) {
        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission");

        if (DBG) Log.d(TAG, "startScanWithUuidsScanParam() - queue=" + mScanQueue.size());

        if (getScanClient(appIf, isServer) == null) {
            if (DBG) Log.d(TAG, "startScanWithUuidsScanParam() - adding client=" + appIf
                            + " scanWindow=" + scanWindow + " scanInterval=" + scanInterval);
            mScanQueue.add(new ScanClient(appIf, isServer, uuids, scanWindow, scanInterval));
        }

        configureScanParams();
    }

    void startScanWithFilters(int appIf, boolean isServer, ScanSettings settings,
            List<ScanFilter> filters) {
        if (DBG) Log.d(TAG, "start scan with filters " + filters.size());
@@ -1271,33 +1255,48 @@ public class GattService extends ProfileService {

    void configureScanParams() {
        if (DBG) Log.d(TAG, "configureScanParams() - queue=" + mScanQueue.size());

        int scanWindow = 0, scanInterval = 0;
        int curDutyCycle = 0;
        int curScanSetting = Integer.MIN_VALUE;

        for(ScanClient client : mScanQueue) {
            // Pick the highest duty cycle - most stressful on battery
            int newDutyCycle = (client.scanWindow * 100)/client.scanInterval;
            if (newDutyCycle > curDutyCycle && newDutyCycle <= 100) {
                curDutyCycle = newDutyCycle;
                scanWindow = client.scanWindow;
                scanInterval = client.scanInterval;
            // ScanClient scan settings are assumed to be monotonically increasing in value for more
            // power hungry(higher duty cycle) operation
            if (client.settings.getScanMode() > curScanSetting) {
                curScanSetting = client.settings.getScanMode();
            }
        }

        if (DBG) Log.d(TAG, "configureScanParams() - dutyCyle=" + curDutyCycle +
                    " scanWindow=" + scanWindow + " scanInterval=" + scanInterval +
                    " lastConfiguredDutyCycle=" + lastConfiguredDutyCycle);
        if (DBG) Log.d(TAG, "configureScanParams() - ScanSetting Scan mode=" + curScanSetting +
                    " lastConfiguredScanSetting=" + lastConfiguredScanSetting);

        if (curDutyCycle != 0) {
            if (curDutyCycle != lastConfiguredDutyCycle) {
        if (curScanSetting != Integer.MIN_VALUE) {
            if (curScanSetting != lastConfiguredScanSetting) {
                int scanWindow, scanInterval;
                switch (curScanSetting){
                    case ScanSettings.SCAN_MODE_LOW_POWER:
                        scanWindow = SCAN_MODE_LOW_POWER_WINDOW_MS;
                        scanInterval = SCAN_MODE_LOW_POWER_INTERVAL_MS;
                        break;
                    case ScanSettings.SCAN_MODE_BALANCED:
                        scanWindow = SCAN_MODE_BALANCED_WINDOW_MS;
                        scanInterval = SCAN_MODE_BALANCED_INTERVAL_MS;
                        break;
                    case ScanSettings.SCAN_MODE_LOW_LATENCY:
                        scanWindow = SCAN_MODE_LOW_LATENCY_WINDOW_MS;
                        scanInterval = SCAN_MODE_LOW_LATENCY_INTERVAL_MS;
                        break;
                    default:
                        Log.e(TAG, "Invalid value for curScanSetting " + curScanSetting);
                        scanWindow = SCAN_MODE_LOW_POWER_WINDOW_MS;
                        scanInterval = SCAN_MODE_LOW_POWER_INTERVAL_MS;
                        break;
                }
                // convert scanWindow and scanInterval from ms to LE scan units(0.625ms)
                scanWindow = (scanWindow * 1000)/625;
                scanInterval = (scanInterval * 1000)/625;
                // Presence of scan clients means scan is active.
                sendStopScanMessage();
                gattSetScanParametersNative(scanInterval, scanWindow);
                lastConfiguredDutyCycle = curDutyCycle;
                lastConfiguredScanSetting = curScanSetting;
                mScanFilters.clear();
                sendStartScanMessage(mScanFilters);
            } else {
@@ -1309,7 +1308,7 @@ public class GattService extends ProfileService {
                }
            }
        } else {
            lastConfiguredDutyCycle = curDutyCycle;
            lastConfiguredScanSetting = curScanSetting;
            mScanFilters.clear();
            sendStopScanMessage();
            if (DBG) Log.d(TAG, "configureScanParams() - queue emtpy, scan stopped");
+8 −54
Original line number Diff line number Diff line
@@ -28,80 +28,34 @@ import java.util.UUID;
 * @hide
 */
/* package */class ScanClient {

    /**
     * Default scan window value
     */
    private static final int LE_SCAN_WINDOW_MS = 100;

    /**
     * Default scan interval value
     */
    private static final int LE_SCAN_INTERVAL_MS = 100;

    /**
     * Scan params corresponding to scan setting
     */
    private static final int SCAN_MODE_LOW_POWER_WINDOW_MS = 500;
    private static final int SCAN_MODE_LOW_POWER_INTERVAL_MS = 5000;
    private static final int SCAN_MODE_BALANCED_WINDOW_MS = 1000;
    private static final int SCAN_MODE_BALANCED_INTERVAL_MS = 5000;
    private static final int SCAN_MODE_LOW_LATENCY_WINDOW_MS = 2500;
    private static final int SCAN_MODE_LOW_LATENCY_INTERVAL_MS = 5000;

    int appIf;
    boolean isServer;
    UUID[] uuids;
    int scanWindow, scanInterval;
    ScanSettings settings;
    List<ScanFilter> filters;
    private static final ScanSettings DEFAULT_SCAN_SETTINGS = new ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();

    ScanClient(int appIf, boolean isServer) {
        this(appIf, isServer, new UUID[0], LE_SCAN_WINDOW_MS, LE_SCAN_INTERVAL_MS);
        this(appIf, isServer, new UUID[0], DEFAULT_SCAN_SETTINGS, null);
    }

    ScanClient(int appIf, boolean isServer, UUID[] uuids) {
        this(appIf, isServer, uuids, LE_SCAN_WINDOW_MS, LE_SCAN_INTERVAL_MS);
    }

    ScanClient(int appIf, boolean isServer, UUID[] uuids, int scanWindow, int scanInterval) {
        this(appIf, isServer, uuids, scanWindow, scanInterval, null, null);
        this(appIf, isServer, uuids, DEFAULT_SCAN_SETTINGS, null);
    }

    ScanClient(int appIf, boolean isServer, ScanSettings settings,
            List<ScanFilter> filters) {
        this(appIf, isServer, new UUID[0], LE_SCAN_WINDOW_MS, LE_SCAN_INTERVAL_MS,
                settings, filters);
        this(appIf, isServer, new UUID[0], settings, filters);
    }

    private ScanClient(int appIf, boolean isServer, UUID[] uuids, int scanWindow, int scanInterval,
            ScanSettings settings, List<ScanFilter> filters) {
    private ScanClient(int appIf, boolean isServer, UUID[] uuids, ScanSettings settings,
            List<ScanFilter> filters) {
        this.appIf = appIf;
        this.isServer = isServer;
        this.uuids = uuids;
        this.scanWindow = scanWindow;
        this.scanInterval = scanInterval;
        this.settings = settings;
        this.filters = filters;
        if (settings != null) {
            switch (settings.getScanMode()) {
                case ScanSettings.SCAN_MODE_LOW_POWER:
                    this.scanWindow = SCAN_MODE_LOW_POWER_WINDOW_MS;
                    this.scanInterval = SCAN_MODE_LOW_POWER_INTERVAL_MS;
                    break;
                case ScanSettings.SCAN_MODE_BALANCED:
                    this.scanWindow = SCAN_MODE_BALANCED_WINDOW_MS;
                    this.scanInterval = SCAN_MODE_BALANCED_INTERVAL_MS;
                    break;
                case ScanSettings.SCAN_MODE_LOW_LATENCY:
                    this.scanWindow = SCAN_MODE_LOW_LATENCY_WINDOW_MS;
                    this.scanInterval = SCAN_MODE_LOW_LATENCY_INTERVAL_MS;
                    break;
                default:
                    this.scanWindow = SCAN_MODE_BALANCED_WINDOW_MS;
                    this.scanInterval = SCAN_MODE_BALANCED_INTERVAL_MS;
                    break;
            }
        }

    }
}