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

Commit bcf51872 authored by Christine Hallstrom's avatar Christine Hallstrom Committed by Gerrit Code Review
Browse files

Merge "Revert "Add API to set connection interval at GATT connection""

parents 6c4dcebb cb9e6608
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -32,8 +32,6 @@
    <integer name="gatt_balanced_priority_max_interval">40</integer>
    <integer name="gatt_low_power_min_interval">80</integer>
    <integer name="gatt_low_power_max_interval">100</integer>
    <integer name="gatt_ccc_priority_min_interval">24</integer>
    <integer name="gatt_ccc_priority_max_interval">24</integer>

    <!-- min/max connection intervals/latencies for companion devices -->
    <!-- Primary companion -->
@@ -69,7 +67,6 @@
    <integer name="gatt_high_priority_latency">0</integer>
    <integer name="gatt_balanced_priority_latency">0</integer>
    <integer name="gatt_low_power_latency">2</integer>
    <integer name="gatt_ccc_priority_latency">0</integer>

    <!-- Specifies the min/max subrate factor parameters for high priority,
         balanced and low power subrate request configurations. -->
+0 −143
Original line number Diff line number Diff line
@@ -22,10 +22,6 @@ import static android.Manifest.permission.BLUETOOTH_ADVERTISE;
import static android.Manifest.permission.BLUETOOTH_CONNECT;
import static android.Manifest.permission.BLUETOOTH_SCAN;
import static android.Manifest.permission.RENOUNCE_PERMISSIONS;
import static android.bluetooth.BluetoothGatt.CONNECTION_PRIORITY_BALANCED;
import static android.bluetooth.BluetoothGatt.CONNECTION_PRIORITY_CCC;
import static android.bluetooth.BluetoothGatt.CONNECTION_PRIORITY_HIGH;
import static android.bluetooth.BluetoothGatt.CONNECTION_PRIORITY_LOW_POWER;
import static android.bluetooth.BluetoothUtils.USER_HANDLE_NULL;
import static android.content.pm.PackageManager.GET_PERMISSIONS;
import static android.content.pm.PackageManager.MATCH_UNINSTALLED_PACKAGES;
@@ -85,9 +81,7 @@ import java.nio.charset.CharsetDecoder;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@@ -1127,141 +1121,4 @@ public final class Utils {
        }
        return false;
    }

    /**
     * Wrapper class for GATT connection interval and latency values.
     *
     * Prevents calling {@link Context#getResources()} each time a connection interval
     * update is required.
     * Raw values are in multiples of 1.25.
     */
    public static class GattPriority {
        private static final String TAG = "GattPriority";
        private int mMinInterval;
        private int mMaxInterval;
        private int mLatency;

        private static final Map<Integer, GattPriority> sPriorities = new HashMap();

        /**
         * Constructs GattPriority with the config parameters.
         */
        private GattPriority(Context context, int priority) {
            switch (priority) {
                case CONNECTION_PRIORITY_HIGH:
                    mMinInterval = context.getResources().getInteger(
                            R.integer.gatt_high_priority_min_interval);
                    mMaxInterval = context.getResources().getInteger(
                            R.integer.gatt_high_priority_max_interval);
                    mLatency = context.getResources().getInteger(
                            R.integer.gatt_high_priority_latency);
                    break;

                case CONNECTION_PRIORITY_CCC:
                    mMinInterval = context.getResources().getInteger(
                            R.integer.gatt_ccc_priority_min_interval);
                    mMaxInterval = context.getResources().getInteger(
                            R.integer.gatt_ccc_priority_max_interval);
                    mLatency = context.getResources().getInteger(
                            R.integer.gatt_ccc_priority_latency);
                    break;

                case CONNECTION_PRIORITY_LOW_POWER:
                    mMinInterval = context.getResources().getInteger(
                            R.integer.gatt_low_power_min_interval);
                    mMaxInterval = context.getResources().getInteger(
                            R.integer.gatt_low_power_max_interval);
                    mLatency = context.getResources().getInteger(
                            R.integer.gatt_low_power_latency);
                    break;

                default:
                    // Using the values for CONNECTION_PRIORITY_BALANCED.
                    mMinInterval = context.getResources().getInteger(
                            R.integer.gatt_balanced_priority_min_interval);
                    mMaxInterval = context.getResources().getInteger(
                            R.integer.gatt_balanced_priority_max_interval);
                    mLatency = context.getResources().getInteger(
                            R.integer.gatt_balanced_priority_latency);
                    break;
            }
        }

        /**
         * Retrieves the GattPriority corresponding to the priority parameter.
         */
        public static GattPriority getStaticPriority(Context context, int priority) {
            if (priority < CONNECTION_PRIORITY_BALANCED
                    || priority > CONNECTION_PRIORITY_CCC) {
                Log.e(TAG, "getStaticPriority: priority: " + priority + "does not exist");
                return null;
            }
            if (sPriorities.get(priority) == null) {
                sPriorities.put(priority, new GattPriority(context, priority));
            }
            return sPriorities.get(priority);
        }

        /**
         * Retrieves the BluetoothGatt priority with raw values.
         */
        public static int toPriority(Context context, int interval,
                int latency) {
            return toPriority(context, interval, interval, latency);
        }

        /**
         * Retrieves the BluetoothGatt priority with raw values.
         */
        public static int toPriority(Context context, int minInterval,
                int maxInterval, int latency) {

            GattPriority priorityCcc =
                    getStaticPriority(context, CONNECTION_PRIORITY_CCC);
            if (minInterval >= priorityCcc.getMinInterval()
                    && maxInterval <= priorityCcc.getMaxInterval()) {
                return CONNECTION_PRIORITY_CCC;
            }

            GattPriority priorityBalanced =
                    getStaticPriority(context, CONNECTION_PRIORITY_BALANCED);
            if (minInterval >= priorityBalanced.getMinInterval()
                    && maxInterval <= priorityBalanced.getMaxInterval()) {
                return CONNECTION_PRIORITY_BALANCED;
            }

            GattPriority priorityHigh =
                    getStaticPriority(context, CONNECTION_PRIORITY_HIGH);
            if (minInterval >= priorityHigh.getMinInterval()
                    && maxInterval <= priorityHigh.getMaxInterval()) {
                return CONNECTION_PRIORITY_HIGH;
            }

            GattPriority priorityLow =
                    getStaticPriority(context, CONNECTION_PRIORITY_LOW_POWER);
            if (minInterval >= priorityLow.getMinInterval()
                    && maxInterval <= priorityLow.getMaxInterval()) {
                return CONNECTION_PRIORITY_LOW_POWER;
            }

            Log.w(TAG, "toPriority: Did not find any matching priority");
            return CONNECTION_PRIORITY_BALANCED;
        }

        public int getMinInterval() {
            return mMinInterval;
        }

        public int getMaxInterval() {
            return mMaxInterval;
        }

        public int getIntervalWindow() {
            return mMaxInterval - mMinInterval;
        }

        public int getLatency() {
            return mLatency;
        }
    }
}
+16 −32
Original line number Diff line number Diff line
@@ -81,7 +81,6 @@ import com.android.bluetooth.BluetoothMetricsProto;
import com.android.bluetooth.BluetoothStatsLog;
import com.android.bluetooth.R;
import com.android.bluetooth.Utils;
import com.android.bluetooth.Utils.GattPriority;
import com.android.bluetooth.btservice.AbstractionLayer;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.BluetoothAdapterProxy;
@@ -802,25 +801,24 @@ public class GattService extends ProfileService {

        @Override
        public void clientConnect(int clientIf, String address, boolean isDirect, int transport,
                boolean opportunistic, int phy, int connectionPriority,
                AttributionSource attributionSource, SynchronousResultReceiver receiver) {
                boolean opportunistic, int phy, AttributionSource attributionSource,
                SynchronousResultReceiver receiver) {
            try {
                clientConnect(clientIf, address, isDirect, transport, opportunistic, phy,
                        connectionPriority, attributionSource);
                        attributionSource);
                receiver.send(null);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
        }
        private void clientConnect(int clientIf, String address, boolean isDirect, int transport,
                boolean opportunistic, int phy, int connectionPriority,
                AttributionSource attributionSource) {
                boolean opportunistic, int phy, AttributionSource attributionSource) {
            GattService service = getService();
            if (service == null) {
                return;
            }
            service.clientConnect(clientIf, address, isDirect, transport, opportunistic, phy,
                    connectionPriority, attributionSource);
                    attributionSource);
        }

        @Override
@@ -2278,7 +2276,6 @@ public class GattService extends ProfileService {
            return;
        }

        app.callback.onPriorityChanged(address, GattPriority.toPriority(this, interval, latency));
        app.callback.onConnectionUpdated(address, interval, latency, timeout, status);
    }

@@ -2373,7 +2370,6 @@ public class GattService extends ProfileService {
            return;
        }

        app.callback.onPriorityChanged(address, GattPriority.toPriority(this, interval, latency));
        app.callback.onConnectionUpdated(address, interval, latency, timeout, status);
    }

@@ -3631,8 +3627,7 @@ public class GattService extends ProfileService {

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    void clientConnect(int clientIf, String address, boolean isDirect, int transport,
            boolean opportunistic, int phy, int connectionPriority,
            AttributionSource attributionSource) {
            boolean opportunistic, int phy, AttributionSource attributionSource) {
        if (!Utils.checkConnectPermissionForDataDelivery(
                this, attributionSource, "GattService clientConnect")) {
            return;
@@ -3648,10 +3643,6 @@ public class GattService extends ProfileService {
                BluetoothProtoEnums.CONNECTION_STATE_CONNECTING, -1);
        mNativeInterface.gattClientConnect(clientIf, address, isDirect, transport, opportunistic,
                phy);
        if (connectionPriority != BluetoothGatt.CONNECTION_PRIORITY_DEFAULT) {
            connectionParameterUpdate(
                    clientIf, address, connectionPriority, attributionSource);
        }
    }

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
@@ -4080,30 +4071,23 @@ public class GattService extends ProfileService {
        // Link supervision timeout is measured in N * 10ms
        int timeout = 500; // 5s

        // Companion manager uses different parameters for primary and secondary companion

        CompanionManager manager =
                AdapterService.getAdapterService().getCompanionManager();
        if (manager.isCompanionDevice(address)) {

        minInterval = manager.getGattConnParameters(
                address, CompanionManager.GATT_CONN_INTERVAL_MIN, connectionPriority);
        maxInterval = manager.getGattConnParameters(
                address, CompanionManager.GATT_CONN_INTERVAL_MAX, connectionPriority);
        latency = manager.getGattConnParameters(
                address, CompanionManager.GATT_CONN_LATENCY, connectionPriority);
        } else {
            GattPriority gattPriority = GattPriority.getStaticPriority(this, connectionPriority);
            minInterval = gattPriority.getMinInterval();
            maxInterval = gattPriority.getMaxInterval();
            latency = gattPriority.getLatency();
        }

        Log.d(TAG, "connectionParameterUpdate() - address=" + address + " params="
                + connectionPriority + " interval=" + minInterval + "/" + maxInterval
                + " timeout=" + timeout);

        mNativeInterface.gattConnectionParameterUpdate(clientIf, address, minInterval,
                    maxInterval, latency, timeout, 0, 0);

        mNativeInterface.gattConnectionParameterUpdate(clientIf, address, minInterval, maxInterval,
                latency, timeout, 0, 0);
    }

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
+2 −4
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import static org.mockito.Mockito.when;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.IBluetoothGattCallback;
@@ -200,11 +199,10 @@ public class GattServiceBinderTest {
        int phy = 3;

        mBinder.clientConnect(clientIf, address, isDirect, transport, opportunistic, phy,
                BluetoothGatt.CONNECTION_PRIORITY_DEFAULT, mAttributionSource,
                SynchronousResultReceiver.get());
                mAttributionSource, SynchronousResultReceiver.get());

        verify(mService).clientConnect(clientIf, address, isDirect, transport, opportunistic, phy,
                BluetoothGatt.CONNECTION_PRIORITY_DEFAULT, mAttributionSource);
                mAttributionSource);
    }

    @Test
+0 −4
Original line number Diff line number Diff line
@@ -514,7 +514,6 @@ package android.bluetooth {
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int);
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int, int);
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int, int, android.os.Handler);
    method @NonNull @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public android.bluetooth.BluetoothGatt connectGatt(@NonNull android.content.Context, boolean, int, int, int, @Nullable android.os.Handler, @NonNull android.bluetooth.BluetoothGattCallback);
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public boolean createBond();
    method @NonNull @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public android.bluetooth.BluetoothSocket createInsecureL2capChannel(int) throws java.io.IOException;
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public android.bluetooth.BluetoothSocket createInsecureRfcommSocketToServiceRecord(java.util.UUID) throws java.io.IOException;
@@ -610,7 +609,6 @@ package android.bluetooth {
    method @Deprecated @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public boolean writeDescriptor(android.bluetooth.BluetoothGattDescriptor);
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public int writeDescriptor(@NonNull android.bluetooth.BluetoothGattDescriptor, @NonNull byte[]);
    field public static final int CONNECTION_PRIORITY_BALANCED = 0; // 0x0
    field public static final int CONNECTION_PRIORITY_CCC = 3; // 0x3
    field public static final int CONNECTION_PRIORITY_HIGH = 1; // 0x1
    field public static final int CONNECTION_PRIORITY_LOW_POWER = 2; // 0x2
    field public static final int GATT_CONNECTION_CONGESTED = 143; // 0x8f
@@ -640,7 +638,6 @@ package android.bluetooth {
    method public void onMtuChanged(android.bluetooth.BluetoothGatt, int, int);
    method public void onPhyRead(android.bluetooth.BluetoothGatt, int, int, int);
    method public void onPhyUpdate(android.bluetooth.BluetoothGatt, int, int, int);
    method public void onPriorityChanged(@NonNull android.bluetooth.BluetoothGatt, int);
    method public void onReadRemoteRssi(android.bluetooth.BluetoothGatt, int, int);
    method public void onReliableWriteCompleted(android.bluetooth.BluetoothGatt, int);
    method public void onServiceChanged(@NonNull android.bluetooth.BluetoothGatt);
@@ -754,7 +751,6 @@ package android.bluetooth {
    method public void onNotificationSent(android.bluetooth.BluetoothDevice, int);
    method public void onPhyRead(android.bluetooth.BluetoothDevice, int, int, int);
    method public void onPhyUpdate(android.bluetooth.BluetoothDevice, int, int, int);
    method public void onPriorityChanged(@NonNull android.bluetooth.BluetoothDevice, int);
    method public void onServiceAdded(int, android.bluetooth.BluetoothGattService);
  }

Loading