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

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

Merge "BluetoothMetrics: Upload Bluetooth stack latencies for l2cap coc metrics"

parents 7bce0057 61174be9
Loading
Loading
Loading
Loading
+34 −10
Original line number Diff line number Diff line
@@ -688,7 +688,9 @@ public class AdapterService extends Service {
            int port,
            boolean isSecured,
            int result,
            long connectionLatencyMillis,
            long socketCreationTimeMillis,
            long socketCreationLatencyMillis,
            long socketConnectionTimeMillis,
            long timeoutMillis,
            int appUid) {

@@ -696,13 +698,19 @@ public class AdapterService extends Service {
        if (device != null) {
            metricId = getMetricId(device);
        }
        long currentTime = System.currentTimeMillis();
        long endToEndLatencyMillis = currentTime - socketCreationTimeMillis;
        long socketAcceptanceLatencyMillis = currentTime - socketConnectionTimeMillis;
        Log.i(TAG, "Statslog L2capcoc server connection. metricId "
                + metricId + " port " + port + " isSecured " + isSecured
                + " result " + result + " connectionLatencyMillis " + connectionLatencyMillis
                + " result " + result + " endToEndLatencyMillis " + endToEndLatencyMillis
                + " socketCreationLatencyMillis " + socketCreationLatencyMillis
                + " socketAcceptanceLatencyMillis " + socketAcceptanceLatencyMillis
                + " timeout set by app " + timeoutMillis + " appUid " + appUid);
        BluetoothStatsLog.write(
                BluetoothStatsLog.BLUETOOTH_L2CAP_COC_SERVER_CONNECTION,
                metricId, port, isSecured, result, connectionLatencyMillis, timeoutMillis, appUid);
                metricId, port, isSecured, result, endToEndLatencyMillis, timeoutMillis, appUid,
                socketCreationLatencyMillis, socketAcceptanceLatencyMillis);
    }

    public void setMetricsLogger(MetricsLogger metricsLogger) {
@@ -724,17 +732,25 @@ public class AdapterService extends Service {
            int port,
            boolean isSecured,
            int result,
            long connectionLatencyMillis,
            long socketCreationTimeMillis,
            long socketCreationLatencyMillis,
            long socketConnectionTimeMillis,
            int appUid) {

        int metricId = getMetricId(device);
        long currentTime = System.currentTimeMillis();
        long endToEndLatencyMillis = currentTime - socketCreationTimeMillis;
        long socketConnectionLatencyMillis = currentTime - socketConnectionTimeMillis;
        Log.i(TAG, "Statslog L2capcoc client connection. metricId "
                + metricId + " port " + port + " isSecured " + isSecured
                + " result " + result + " connectionLatencyMillis " + connectionLatencyMillis
                + " result " + result + " endToEndLatencyMillis " + endToEndLatencyMillis
                + " socketCreationLatencyMillis " + socketCreationLatencyMillis
                + " socketConnectionLatencyMillis " + socketConnectionLatencyMillis
                + " appUid " + appUid);
        BluetoothStatsLog.write(
                BluetoothStatsLog.BLUETOOTH_L2CAP_COC_CLIENT_CONNECTION,
                metricId, port, isSecured, result, connectionLatencyMillis, appUid);
                metricId, port, isSecured, result, endToEndLatencyMillis,
                appUid, socketCreationLatencyMillis, socketConnectionLatencyMillis);
    }

    @RequiresPermission(allOf = {
@@ -3476,7 +3492,9 @@ public class AdapterService extends Service {
                int port,
                boolean isSecured,
                int result,
                long connectionLatencyMillis,
                long socketCreationTimeMillis,
                long socketCreationLatencyMillis,
                long socketConnectionTimeMillis,
                long timeoutMillis,
                SynchronousResultReceiver receiver) {
            AdapterService service = getService();
@@ -3489,7 +3507,9 @@ public class AdapterService extends Service {
                        port,
                        isSecured,
                        result,
                        connectionLatencyMillis,
                        socketCreationTimeMillis,
                        socketCreationLatencyMillis,
                        socketConnectionTimeMillis,
                        timeoutMillis,
                        Binder.getCallingUid());
                receiver.send(null);
@@ -3514,7 +3534,9 @@ public class AdapterService extends Service {
                int port,
                boolean isSecured,
                int result,
                long connectionLatencyMillis,
                long socketCreationTimeMillis,
                long socketCreationLatencyMillis,
                long socketConnectionTimeMillis,
                SynchronousResultReceiver receiver) {
            AdapterService service = getService();
            if (service == null) {
@@ -3526,7 +3548,9 @@ public class AdapterService extends Service {
                        port,
                        isSecured,
                        result,
                        connectionLatencyMillis,
                        socketCreationTimeMillis,
                        socketCreationLatencyMillis,
                        socketConnectionTimeMillis,
                        Binder.getCallingUid());
                receiver.send(null);
            } catch (RuntimeException e) {
+24 −8
Original line number Diff line number Diff line
@@ -88,7 +88,8 @@ public final class BluetoothServerSocket implements Closeable {
    private Handler mHandler;
    private int mMessage;
    private int mChannel;
    private long mSocketCreationTime = 0;
    private long mSocketCreationTimeMillis = 0;
    private long mSocketCreationLatencyMillis = 0;

    // BluetoothSocket.getConnectionType() will hide L2CAP_LE.
    // Therefore a new variable need to be maintained here.
@@ -106,13 +107,14 @@ public final class BluetoothServerSocket implements Closeable {
     */
    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
            throws IOException {
        mSocketCreationTimeMillis = System.currentTimeMillis();
        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);
        }
        mSocketCreationLatencyMillis = System.currentTimeMillis() - mSocketCreationTimeMillis;
    }

    /**
@@ -130,14 +132,15 @@ public final class BluetoothServerSocket implements Closeable {
    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port,
            boolean mitm, boolean min16DigitPin)
            throws IOException {
        mSocketCreationTimeMillis = System.currentTimeMillis();
        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) {
            mSocket.setExcludeSdp(true);
        }
        mSocketCreationLatencyMillis = System.currentTimeMillis() - mSocketCreationTimeMillis;
    }

    /**
@@ -152,11 +155,12 @@ public final class BluetoothServerSocket implements Closeable {
     */
    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, ParcelUuid uuid)
            throws IOException {
        mSocketCreationTimeMillis = System.currentTimeMillis();
        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();
        mSocketCreationLatencyMillis = System.currentTimeMillis() - mSocketCreationTimeMillis;
    }


@@ -185,21 +189,31 @@ 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 {
        long socketConnectionTime = System.currentTimeMillis();
        BluetoothSocket acceptedSocket = null;
        try {
            acceptedSocket = mSocket.accept(timeout);
            logL2capcocServerConnection(
                    acceptedSocket, timeout, BluetoothSocket.RESULT_L2CAP_CONN_SUCCESS);
                    acceptedSocket,
                    timeout,
                    BluetoothSocket.RESULT_L2CAP_CONN_SUCCESS,
                    socketConnectionTime);
            return acceptedSocket;
        } catch (IOException e) {
            logL2capcocServerConnection(
                    acceptedSocket, timeout, BluetoothSocket.RESULT_L2CAP_CONN_SERVER_FAILURE);
                    acceptedSocket,
                    timeout,
                    BluetoothSocket.RESULT_L2CAP_CONN_SERVER_FAILURE,
                    socketConnectionTime);
            throw e;
        }
    }

    private void logL2capcocServerConnection(
            BluetoothSocket acceptedSocket, int timeout, int result) {
            BluetoothSocket acceptedSocket,
            int timeout,
            int result,
            long socketConnectionTimeMillis) {
        if (mType != BluetoothSocket.TYPE_L2CAP_LE) {
            return;
        }
@@ -217,7 +231,9 @@ public final class BluetoothServerSocket implements Closeable {
                    getPsm(),
                    mSocket.isAuth(),
                    result,
                    System.currentTimeMillis() - mSocketCreationTime,
                    mSocketCreationTimeMillis, // pass creation time to calculate end to end latency
                    mSocketCreationLatencyMillis, // socket creation latency
                    socketConnectionTimeMillis, // send connection start time for connection latency
                    timeout,
                    recv);
            recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
+46 −17
Original line number Diff line number Diff line
@@ -169,7 +169,8 @@ public final class BluetoothSocket implements Closeable {
    private int mMaxTxPacketSize = 0; // The l2cap maximum packet size supported by the peer.
    private int mMaxRxPacketSize = 0; // The l2cap maximum packet size that can be received.

    private long mSocketCreationTime = 0;
    private long mSocketCreationTimeMillis = 0;
    private long mSocketCreationLatencyMillis = 0;

    private enum SocketState {
        INIT,
@@ -221,7 +222,7 @@ public final class BluetoothSocket implements Closeable {
            BluetoothDevice device, int port, ParcelUuid uuid, boolean mitm, boolean min16DigitPin)
            throws IOException {
        if (VDBG) Log.d(TAG, "Creating new BluetoothSocket of type: " + type);
        mSocketCreationTime = System.currentTimeMillis();
        mSocketCreationTimeMillis = System.currentTimeMillis();
        if (type == BluetoothSocket.TYPE_RFCOMM && uuid == null && fd == -1
                && port != BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
            if (port < 1 || port > MAX_RFCOMM_CHANNEL) {
@@ -253,6 +254,7 @@ public final class BluetoothSocket implements Closeable {
        }
        mInputStream = new BluetoothInputStream(this);
        mOutputStream = new BluetoothOutputStream(this);
        mSocketCreationLatencyMillis = System.currentTimeMillis() - mSocketCreationTimeMillis;
    }

    /**
@@ -298,7 +300,8 @@ public final class BluetoothSocket implements Closeable {
        mExcludeSdp = s.mExcludeSdp;
        mAuthMitm = s.mAuthMitm;
        mMin16DigitPin = s.mMin16DigitPin;
        mSocketCreationTime = s.mSocketCreationTime;
        mSocketCreationTimeMillis = s.mSocketCreationTimeMillis;
        mSocketCreationLatencyMillis = s.mSocketCreationLatencyMillis;
    }

    private BluetoothSocket acceptSocket(String remoteAddr) throws IOException {
@@ -444,25 +447,32 @@ public final class BluetoothSocket implements Closeable {
    public void connect() throws IOException {
        IBluetooth bluetoothProxy =
                BluetoothAdapter.getDefaultAdapter().getBluetoothService();
        long socketConnectionTimeMillis = System.currentTimeMillis();
        if (bluetoothProxy == null) {
            throw new BluetoothSocketException(BluetoothSocketException.BLUETOOTH_OFF_FAILURE);
        }
        if (mDevice == null) {
            logL2capcocClientConnection(
                    bluetoothProxy, RESULT_L2CAP_CONN_BLUETOOTH_NULL_BLUETOOTH_DEVICE);
                    bluetoothProxy,
                    RESULT_L2CAP_CONN_BLUETOOTH_NULL_BLUETOOTH_DEVICE,
                    socketConnectionTimeMillis);
            throw new BluetoothSocketException(BluetoothSocketException.NULL_DEVICE);
        }
        try {
            if (mSocketState == SocketState.CLOSED) {
                logL2capcocClientConnection(
                        bluetoothProxy, RESULT_L2CAP_CONN_BLUETOOTH_SOCKET_CONNECTION_CLOSED);
                        bluetoothProxy,
                        RESULT_L2CAP_CONN_BLUETOOTH_SOCKET_CONNECTION_CLOSED,
                        socketConnectionTimeMillis);
                throw new BluetoothSocketException(BluetoothSocketException.SOCKET_CLOSED);
            }

            IBluetoothSocketManager socketManager = bluetoothProxy.getSocketManager();
            if (socketManager == null) {
                logL2capcocClientConnection(
                        bluetoothProxy, RESULT_L2CAP_CONN_BLUETOOTH_GET_SOCKET_MANAGER_FAILED);
                        bluetoothProxy,
                        RESULT_L2CAP_CONN_BLUETOOTH_GET_SOCKET_MANAGER_FAILED,
                        socketConnectionTimeMillis);
                throw new BluetoothSocketException(BluetoothSocketException.SOCKET_MANAGER_FAILURE);
            }
            mPfd = socketManager.connectSocket(mDevice, mType, mUuid, mPort, getSecurityFlags());
@@ -470,13 +480,17 @@ public final class BluetoothSocket implements Closeable {
                if (DBG) Log.d(TAG, "connect(), SocketState: " + mSocketState + ", mPfd: " + mPfd);
                if (mSocketState == SocketState.CLOSED) {
                    logL2capcocClientConnection(
                            bluetoothProxy, RESULT_L2CAP_CONN_BLUETOOTH_SOCKET_CONNECTION_CLOSED);
                            bluetoothProxy,
                            RESULT_L2CAP_CONN_BLUETOOTH_SOCKET_CONNECTION_CLOSED,
                            socketConnectionTimeMillis);
                    throw new BluetoothSocketException(
                            BluetoothSocketException.SOCKET_CONNECTION_FAILURE);
                }
                if (mPfd == null) {
                    logL2capcocClientConnection(
                            bluetoothProxy, RESULT_L2CAP_CONN_BLUETOOTH_NULL_FILE_DESCRIPTOR);
                            bluetoothProxy,
                            RESULT_L2CAP_CONN_BLUETOOTH_NULL_FILE_DESCRIPTOR,
                            socketConnectionTimeMillis);
                    throw new BluetoothSocketException(
                            BluetoothSocketException.SOCKET_CONNECTION_FAILURE);
                }
@@ -488,12 +502,14 @@ public final class BluetoothSocket implements Closeable {
            int channel = readInt(mSocketIS);
            if (channel == 0) {
                int errCode = (int) mSocketIS.read();
                logL2capcocClientConnection(bluetoothProxy, errCode);
                logL2capcocClientConnection(bluetoothProxy, errCode, socketConnectionTimeMillis);
                throw new BluetoothSocketException(errCode);
            }
            if (channel < 0) {
                logL2capcocClientConnection(
                        bluetoothProxy, RESULT_L2CAP_CONN_BLUETOOTH_SOCKET_CONNECTION_FAILED);
                        bluetoothProxy,
                        RESULT_L2CAP_CONN_BLUETOOTH_SOCKET_CONNECTION_FAILED,
                        socketConnectionTimeMillis);
                throw new BluetoothSocketException(
                        BluetoothSocketException.SOCKET_CONNECTION_FAILURE);
            }
@@ -502,17 +518,23 @@ public final class BluetoothSocket implements Closeable {
            synchronized (this) {
                if (mSocketState == SocketState.CLOSED) {
                    logL2capcocClientConnection(
                            bluetoothProxy, RESULT_L2CAP_CONN_BLUETOOTH_SOCKET_CONNECTION_CLOSED);
                            bluetoothProxy,
                            RESULT_L2CAP_CONN_BLUETOOTH_SOCKET_CONNECTION_CLOSED,
                            socketConnectionTimeMillis);
                    throw new BluetoothSocketException(BluetoothSocketException.SOCKET_CLOSED);
                }
                mSocketState = SocketState.CONNECTED;
                if (DBG) Log.d(TAG, "connect(), socket connected");
            }
            logL2capcocClientConnection(bluetoothProxy, RESULT_L2CAP_CONN_SUCCESS);
            logL2capcocClientConnection(bluetoothProxy,
                    RESULT_L2CAP_CONN_SUCCESS,
                    socketConnectionTimeMillis);
        } catch (RemoteException e) {
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            logL2capcocClientConnection(
                    bluetoothProxy, RESULT_L2CAP_CONN_BLUETOOTH_UNABLE_TO_SEND_RPC);
                    bluetoothProxy,
                    RESULT_L2CAP_CONN_BLUETOOTH_UNABLE_TO_SEND_RPC,
                    socketConnectionTimeMillis);
            throw new BluetoothSocketException(BluetoothSocketException.RPC_FAILURE,
                    "unable to send RPC: " + e.getMessage());
        }
@@ -722,15 +744,22 @@ public final class BluetoothSocket implements Closeable {
        }
    }

    private void logL2capcocClientConnection(IBluetooth bluetoothProxy, int errCode) {
    private void logL2capcocClientConnection(
            IBluetooth bluetoothProxy, int errCode, long socketConnectionTimeMillis) {
        if (mType != TYPE_L2CAP_LE) {
            return;
        }
        try {
            final SynchronousResultReceiver recv = SynchronousResultReceiver.get();
            bluetoothProxy.logL2capcocClientConnection(
                    mDevice, mPort, mAuth, errCode,
                    System.currentTimeMillis() - mSocketCreationTime, recv);
                    mDevice,
                    mPort,
                    mAuth,
                    errCode,
                    mSocketCreationTimeMillis, // to calculate end to end latency
                    mSocketCreationLatencyMillis, // latency of the constructor
                    socketConnectionTimeMillis, // to calculate the latency of connect()
                    recv);
            recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
        } catch (RemoteException | TimeoutException e) {
            Log.w(TAG, "logL2capcocClientConnection failed due to remote exception");
@@ -745,7 +774,7 @@ public final class BluetoothSocket implements Closeable {
    }

    /*package */ long getSocketCreationTime() {
        return mSocketCreationTime;
        return mSocketCreationTimeMillis;
    }

    /**
+2 −2
Original line number Diff line number Diff line
@@ -178,13 +178,13 @@ interface IBluetooth

    // 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);
    oneway void logL2capcocServerConnection(in BluetoothDevice device, int port, boolean isSecured, int result, long socketCreationTimeMillis, long socketCreationLatencyMillis, long socketConnectionTimeMillis, 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, in SynchronousResultReceiver receiver);
    oneway void logL2capcocClientConnection(in BluetoothDevice device, int port, boolean isSecured, int result, long socketCreationTimeMillis, long socketCreationLatencyMillis, long socketConnectionTimeMillis, 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);