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

Commit 3a2eacfa authored by Chen Chen's avatar Chen Chen Committed by Gerrit Code Review
Browse files

Merge "BluetoothMetrics: Upload L2CAP CoC Server Metrics"

parents ced4fffa 874e7b00
Loading
Loading
Loading
Loading
+73 −8
Original line number Diff line number Diff line
@@ -674,6 +674,37 @@ public class AdapterService extends Service {
        return result;
    }

    /**
     *  Log L2CAP CoC Server Connection Metrics
     *
     *  @param port port of socket
     *  @param isSecured if secured API is called
     *  @param result transaction result of the connection
     *  @param connectionLatencyMillis latency of the connection
     *  @param timeoutMillis timeout set by the app
     */
    public void logL2capcocServerConnection(
            BluetoothDevice device,
            int port,
            boolean isSecured,
            int result,
            long connectionLatencyMillis,
            long timeoutMillis,
            int appUid) {

        int metricId = 0;
        if (device != null) {
            metricId = getMetricId(device);
        }
        Log.i(TAG, "Statslog L2capcoc server connection. metricId "
                + metricId + " port " + port + " isSecured " + isSecured
                + " result " + result + " connectionLatencyMillis " + connectionLatencyMillis
                + " timeout set by app " + timeoutMillis + " appUid " + appUid);
        BluetoothStatsLog.write(
                BluetoothStatsLog.BLUETOOTH_L2CAP_COC_SERVER_CONNECTION,
                metricId, port, isSecured, result, connectionLatencyMillis, timeoutMillis, appUid);
    }

    public void setMetricsLogger(MetricsLogger metricsLogger) {
        mMetricsLogger = metricsLogger;
    }
@@ -3439,6 +3470,34 @@ public class AdapterService extends Service {
            return true;
        }

        @Override
        public void logL2capcocServerConnection(
                BluetoothDevice device,
                int port,
                boolean isSecured,
                int result,
                long connectionLatencyMillis,
                long timeoutMillis,
                SynchronousResultReceiver receiver) {
            AdapterService service = getService();
            if (service == null) {
                return;
            }
            try {
                service.logL2capcocServerConnection(
                        device,
                        port,
                        isSecured,
                        result,
                        connectionLatencyMillis,
                        timeoutMillis,
                        Binder.getCallingUid());
                receiver.send(null);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
        }

        @Override
        public IBluetoothSocketManager getSocketManager() {
            AdapterService service = getService();
@@ -3455,11 +3514,13 @@ public class AdapterService extends Service {
                int port,
                boolean isSecured,
                int result,
                long connectionLatencyMillis) {
                long connectionLatencyMillis,
                SynchronousResultReceiver receiver) {
            AdapterService service = getService();
            if (service == null) {
                return;
            }
            try {
                service.logL2capcocClientConnection(
                        device,
                        port,
@@ -3467,6 +3528,10 @@ public class AdapterService extends Service {
                        result,
                        connectionLatencyMillis,
                        Binder.getCallingUid());
                receiver.send(null);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
        }

        @Override
+56 −1
Original line number Diff line number Diff line
@@ -16,14 +16,20 @@

package android.bluetooth;

import static android.bluetooth.BluetoothUtils.getSyncTimeout;

import android.annotation.SuppressLint;
import android.compat.annotation.UnsupportedAppUsage;
import android.os.Handler;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.util.Log;

import com.android.modules.utils.SynchronousResultReceiver;

import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * A listening Bluetooth socket.
@@ -82,6 +88,11 @@ public final class BluetoothServerSocket implements Closeable {
    private Handler mHandler;
    private int mMessage;
    private int mChannel;
    private long mSocketCreationTime = 0;

    // BluetoothSocket.getConnectionType() will hide L2CAP_LE.
    // Therefore a new variable need to be maintained here.
    private int mType;

    /**
     * Construct a socket for incoming connections.
@@ -95,7 +106,9 @@ public final class BluetoothServerSocket implements Closeable {
     */
    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
            throws IOException {
        mType = type;
        mChannel = port;
        mSocketCreationTime = System.currentTimeMillis();
        mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null);
        if (port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
            mSocket.setExcludeSdp(true);
@@ -117,7 +130,9 @@ public final class BluetoothServerSocket implements Closeable {
    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port,
            boolean mitm, boolean min16DigitPin)
            throws IOException {
        mType = type;
        mChannel = port;
        mSocketCreationTime = System.currentTimeMillis();
        mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null, mitm,
                min16DigitPin);
        if (port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
@@ -137,6 +152,8 @@ public final class BluetoothServerSocket implements Closeable {
     */
    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, ParcelUuid uuid)
            throws IOException {
        mType = type;
        mSocketCreationTime = System.currentTimeMillis();
        mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, -1, uuid);
        // TODO: This is the same as mChannel = -1 - is this intentional?
        mChannel = mSocket.getPort();
@@ -168,9 +185,47 @@ public final class BluetoothServerSocket implements Closeable {
     * @throws IOException on error, for example this call was aborted, or timeout
     */
    public BluetoothSocket accept(int timeout) throws IOException {
        return mSocket.accept(timeout);
        BluetoothSocket acceptedSocket = null;
        try {
            acceptedSocket = mSocket.accept(timeout);
            logL2capcocServerConnection(
                    acceptedSocket, timeout, BluetoothSocket.RESULT_L2CAP_CONN_SUCCESS);
            return acceptedSocket;
        } catch (IOException e) {
            logL2capcocServerConnection(
                    acceptedSocket, timeout, BluetoothSocket.RESULT_L2CAP_CONN_SERVER_FAILURE);
            throw e;
        }
    }

    private void logL2capcocServerConnection(
            BluetoothSocket acceptedSocket, int timeout, int result) {
        if (mType != BluetoothSocket.TYPE_L2CAP_LE) {
            return;
        }
        IBluetooth bluetoothProxy =
                BluetoothAdapter.getDefaultAdapter().getBluetoothService();
        if (bluetoothProxy == null) {
            Log.w(TAG,
                    "bluetoothProxy is null while trying to log l2cap soc server connection");
            return;
        }
        try {
            final SynchronousResultReceiver recv = SynchronousResultReceiver.get();
            bluetoothProxy.logL2capcocServerConnection(
                    acceptedSocket == null ? null : acceptedSocket.getRemoteDevice(),
                    getPsm(),
                    mSocket.isAuth(),
                    result,
                    System.currentTimeMillis() - mSocketCreationTime,
                    timeout,
                    recv);
            recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);

        } catch (RemoteException | TimeoutException e) {
            Log.w(TAG, "logL2capcocServerConnection failed due to remote exception");
        }
    }
    /**
     * Immediately close this socket, and release all associated resources.
     * <p>Causes blocked calls on this socket in other threads to immediately
+14 −3
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.bluetooth;

import static android.bluetooth.BluetoothUtils.getSyncTimeout;

import android.annotation.RequiresNoPermission;
import android.annotation.RequiresPermission;
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
@@ -27,6 +29,8 @@ import android.os.ParcelUuid;
import android.os.RemoteException;
import android.util.Log;

import com.android.modules.utils.SynchronousResultReceiver;

import java.io.Closeable;
import java.io.FileDescriptor;
import java.io.IOException;
@@ -37,6 +41,7 @@ import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

/**
 * A connected or connecting Bluetooth socket.
@@ -123,13 +128,14 @@ public final class BluetoothSocket implements Closeable {

    // Defined in BluetoothProtoEnums.L2capCocConnectionResult of proto logging
    private static final int RESULT_L2CAP_CONN_UNKNOWN = 0;
    private static final int RESULT_L2CAP_CONN_SUCCESS = 1;
    /*package*/ static final int RESULT_L2CAP_CONN_SUCCESS = 1;
    private static final int RESULT_L2CAP_CONN_BLUETOOTH_SOCKET_CONNECTION_FAILED = 1000;
    private static final int RESULT_L2CAP_CONN_BLUETOOTH_SOCKET_CONNECTION_CLOSED = 1001;
    private static final int RESULT_L2CAP_CONN_BLUETOOTH_UNABLE_TO_SEND_RPC = 1002;
    private static final int RESULT_L2CAP_CONN_BLUETOOTH_NULL_BLUETOOTH_DEVICE = 1003;
    private static final int RESULT_L2CAP_CONN_BLUETOOTH_GET_SOCKET_MANAGER_FAILED = 1004;
    private static final int RESULT_L2CAP_CONN_BLUETOOTH_NULL_FILE_DESCRIPTOR = 1005;
    /*package*/ static final int RESULT_L2CAP_CONN_SERVER_FAILURE = 2000;

    private final int mType;  /* one of TYPE_RFCOMM etc */
    private BluetoothDevice mDevice;    /* remote device */
@@ -537,6 +543,9 @@ public final class BluetoothSocket implements Closeable {
        mServiceName = name;
    }

    /*package*/ boolean isAuth() {
        return mAuth;
    }
    /**
     * Attempt to connect to a remote device.
     * <p>This method will block until a connection is made or the connection
@@ -908,10 +917,12 @@ public final class BluetoothSocket implements Closeable {
            return;
        }
        try {
            final SynchronousResultReceiver recv = SynchronousResultReceiver.get();
            bluetoothProxy.logL2capcocClientConnection(
                    mDevice, mPort, mAuth, errCode,
                    System.currentTimeMillis() - mSocketCreationTime);
        } catch (RemoteException e) {
                    System.currentTimeMillis() - mSocketCreationTime, recv);
            recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
        } catch (RemoteException | TimeoutException e) {
            Log.w(TAG, "logL2capcocClientConnection failed due to remote exception");
        }
    }
+4 −1
Original line number Diff line number Diff line
@@ -177,11 +177,14 @@ interface IBluetooth
    oneway void unregisterCallback(in IBluetoothCallback callback, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);

    // For Socket
    @JavaPassthrough(annotation="@android.annotation.RequiresNoPermission")
    oneway void logL2capcocServerConnection(in BluetoothDevice device, int port, boolean isSecured, int result, long connectionLatencyMillis, long timeoutMillis, in SynchronousResultReceiver receiver);

    @JavaPassthrough(annotation="@android.annotation.RequiresNoPermission")
    IBluetoothSocketManager getSocketManager();

    @JavaPassthrough(annotation="@android.annotation.RequiresNoPermission")
    oneway void logL2capcocClientConnection(in BluetoothDevice device, int port, boolean isSecured, int result, long connectionLatencyMillis);
    oneway void logL2capcocClientConnection(in BluetoothDevice device, int port, boolean isSecured, int result, long connectionLatencyMillis, in SynchronousResultReceiver receiver);

    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT,android.Manifest.permission.BLUETOOTH_PRIVILEGED})")
    oneway void factoryReset(in AttributionSource attributionSource, in SynchronousResultReceiver receiver);