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

Commit 8fb52922 authored by Prerepa Viswanadham's avatar Prerepa Viswanadham
Browse files

set scan parameters in BT stack. Apply the most demanding request

in terms of duty cycle for all outstanding requests.

Change-Id: Iee460003d210455ab180367f3518bfb10a6c2539
parent 23ceee85
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -778,10 +778,10 @@ static void gattClientUnregisterAppNative(JNIEnv* env, jobject object, jint clie
    sGattIf->client->unregister_client(clientIf);
}

static void gattClientScanNative(JNIEnv* env, jobject object, jint clientIf, jboolean start)
static void gattClientScanNative(JNIEnv* env, jobject object, jboolean start)
{
    if (!sGattIf) return;
    sGattIf->client->scan(clientIf, start);
    sGattIf->client->scan(start);
}

static void gattClientConnectNative(JNIEnv* env, jobject object, jint clientif,
@@ -1311,7 +1311,7 @@ static JNINativeMethod sMethods[] = {
    {"gattClientGetDeviceTypeNative", "(Ljava/lang/String;)I", (void *) gattClientGetDeviceTypeNative},
    {"gattClientRegisterAppNative", "(JJ)V", (void *) gattClientRegisterAppNative},
    {"gattClientUnregisterAppNative", "(I)V", (void *) gattClientUnregisterAppNative},
    {"gattClientScanNative", "(IZ)V", (void *) gattClientScanNative},
    {"gattClientScanNative", "(Z)V", (void *) gattClientScanNative},
    {"gattClientConnectNative", "(ILjava/lang/String;ZI)V", (void *) gattClientConnectNative},
    {"gattClientDisconnectNative", "(ILjava/lang/String;I)V", (void *) gattClientDisconnectNative},
    {"gattClientRefreshNative", "(ILjava/lang/String;)V", (void *) gattClientRefreshNative},
+72 −20
Original line number Diff line number Diff line
@@ -106,6 +106,7 @@ 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;

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

        public void startScanWithUuidsAndScanWindowInterval(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.startScanWithUuidsAndScanWindowInterval(appIf, isServer, uuids,
                    scanWindow, scanInterval);
        }

        public void stopScan(int appIf, boolean isServer) {
            GattService service = getService();
            if (service == null) return;
@@ -421,12 +434,6 @@ public class GattService extends ProfileService {
            service.configureMTU(clientIf, address, mtu);
        }

        public void setScanParameters(int clientIf, int scan_interval, int scan_window) {
            GattService service = getService();
            if (service == null) return;
            service.setScanParameters(clientIf, scan_interval, scan_window);
        }

        public void registerServer(ParcelUuid uuid, IBluetoothGattServerCallback callback) {
            GattService service = getService();
            if (service == null) return;
@@ -1049,7 +1056,7 @@ public class GattService extends ProfileService {
            mScanQueue.add(new ScanClient(appIf, isServer));
        }

        gattClientScanNative(appIf, true);
        configureScanParams();
    }

    void startScanWithUuids(int appIf, boolean isServer, UUID[] uuids) {
@@ -1062,7 +1069,60 @@ public class GattService extends ProfileService {
            mScanQueue.add(new ScanClient(appIf, isServer, uuids));
        }

        gattClientScanNative(appIf, true);
        configureScanParams();
    }

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

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

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

        configureScanParams();
    }

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

        int scanWindow = 0, scanInterval = 0;
        int curDutyCycle = 0;

        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;
            }
        }

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

        if (curDutyCycle != 0) {
            if (curDutyCycle != lastConfiguredDutyCycle) {
                // 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.
                gattClientScanNative(false);
                gattSetScanParametersNative(scanInterval, scanWindow);
                lastConfiguredDutyCycle = curDutyCycle;
                gattClientScanNative(true);
            }
        } else {
            lastConfiguredDutyCycle = curDutyCycle;
            gattClientScanNative(false);
            if (DBG) Log.d(TAG, "configureScanParams() - queue emtpy, scan stopped");
        }
    }

    void stopScan(int appIf, boolean isServer) {
@@ -1070,10 +1130,10 @@ public class GattService extends ProfileService {

        if (DBG) Log.d(TAG, "stopScan() - queue=" + mScanQueue.size());
        removeScanClient(appIf, isServer);
        configureScanParams();

        if (mScanQueue.isEmpty()) {
            if (DBG) Log.d(TAG, "stopScan() - queue empty; stopping scan");
            gattClientScanNative(appIf, false);
            if (DBG) Log.d(TAG, "stopScan() - queue empty; scan stopped");
        }
    }

@@ -1418,13 +1478,6 @@ public class GattService extends ProfileService {
        }
    }

    void setScanParameters(int clientIf, int scan_interval, int scan_window) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        if (DBG) Log.d(TAG, "setScanParameters() - interval=" + scan_interval
                            + " window=" + scan_window);
        gattSetScanParametersNative(scan_interval, scan_window);
    }

    /**************************************************************************
     * Callback functions - SERVER
     *************************************************************************/
@@ -2030,7 +2083,8 @@ public class GattService extends ProfileService {

    private native void gattClientUnregisterAppNative(int clientIf);

    private native void gattClientScanNative(int clientIf, boolean start);
    private native void gattClientScanNative(boolean start);
    private native void gattSetScanParametersNative(int scan_interval, int scan_window);

    private native void gattClientConnectNative(int clientIf, String address,
            boolean isDirect, int transport);
@@ -2100,8 +2154,6 @@ public class GattService extends ProfileService {
            boolean inclTxPower, int minInterval, int maxInterval,
            int appearance, byte[] manufacturerData, byte[] serviceData, byte[] serviceUuid);

    private native void gattSetScanParametersNative(int scan_interval, int scan_window);

    private native void gattServerRegisterAppNative(long app_uuid_lsb,
                                                    long app_uuid_msb);

+19 −3
Original line number Diff line number Diff line
@@ -23,19 +23,35 @@ 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;

    int appIf;
    boolean isServer;
    UUID[] uuids;
    int scanWindow, scanInterval;

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

    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 = appIf;
        this.isServer = isServer;
        this.uuids = uuids;
        this.scanWindow = scanWindow;
        this.scanInterval = scanInterval;
    }
}