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

Commit db0e3a4a authored by Chen Chen's avatar Chen Chen
Browse files

BluetoothMetrics: Upload GATT server and client connection state change events

* clientConnect(): GATT client starts connecting to a GATT server.

* onConnected(): GATT client completes connection successfully or fails.

* clientDisconnect(): GATT client starts disconnecting from a GATT server.

* onDisconnected(): GATT client completes disconnection from a GATT server. (Always successfully)

* onClientConnected(): GATT server successfully receives a connection or disconnection from a GATT client (depending on the value of boolean “connected”)

Bug: 233400948
Test: BluetoothInstrumentationTests
Tag: #feature
Change-Id: If0fc3f489c0abffb1bf7d2ef87a6c554d45ebbbf
parent 6c19b7b4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -692,7 +692,7 @@ class AdapterProperties {
                        + prevState + " -> " + state);
        BluetoothStatsLog.write(BluetoothStatsLog.BLUETOOTH_CONNECTION_STATE_CHANGED, state,
                0 /* deprecated */, profile, mService.obfuscateAddress(device),
                mService.getMetricId(device));
                mService.getMetricId(device), 0);

        if (!isNormalStateTransition(prevState, state)) {
            Log.w(TAG,
+35 −3
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothProtoEnums;
import android.bluetooth.BluetoothStatusCodes;
import android.bluetooth.IBluetoothGatt;
import android.bluetooth.IBluetoothGattCallback;
@@ -70,6 +71,7 @@ import android.text.format.DateUtils;
import android.util.Log;

import com.android.bluetooth.BluetoothMetricsProto;
import com.android.bluetooth.BluetoothStatsLog;
import com.android.bluetooth.R;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AbstractionLayer;
@@ -2005,7 +2007,7 @@ public class GattService extends ProfileService {
            Log.d(TAG, "onConnected() - clientIf=" + clientIf + ", connId=" + connId + ", address="
                    + address);
        }

        int connectionState = BluetoothProtoEnums.CONNECTION_STATE_DISCONNECTED;
        if (status == 0) {
            mClientMap.addConnection(clientIf, connId, address);

@@ -2015,12 +2017,16 @@ public class GattService extends ProfileService {
                    + address);
                mPermits.putIfAbsent(address, new AtomicBoolean(true));
            }
            connectionState = BluetoothProtoEnums.CONNECTION_STATE_CONNECTED;

        }
        ClientMap.App app = mClientMap.getById(clientIf);
        if (app != null) {
            app.callback.onClientConnectionState(status, clientIf,
                    (status == BluetoothGatt.GATT_SUCCESS), address);
        }
        statsLogGattConnectionStateChange(
                BluetoothProfile.GATT, address, clientIf, connectionState);
    }

    void onDisconnected(int clientIf, int connId, int status, String address)
@@ -2047,6 +2053,9 @@ public class GattService extends ProfileService {
        if (app != null) {
            app.callback.onClientConnectionState(status, clientIf, false, address);
        }
        statsLogGattConnectionStateChange(
                BluetoothProfile.GATT, address, clientIf,
                BluetoothProtoEnums.CONNECTION_STATE_DISCONNECTED);
    }

    void onClientPhyUpdate(int connId, int txPhy, int rxPhy, int status) throws RemoteException {
@@ -3386,6 +3395,9 @@ public class GattService extends ProfileService {
            Log.d(TAG, "clientConnect() - address=" + address + ", isDirect=" + isDirect
                    + ", opportunistic=" + opportunistic + ", phy=" + phy);
        }
        statsLogGattConnectionStateChange(
                BluetoothProfile.GATT, address, clientIf,
                BluetoothProtoEnums.CONNECTION_STATE_CONNECTING);
        gattClientConnectNative(clientIf, address, isDirect, transport, opportunistic, phy);
    }

@@ -3400,7 +3412,9 @@ public class GattService extends ProfileService {
        if (DBG) {
            Log.d(TAG, "clientDisconnect() - address=" + address + ", connId=" + connId);
        }

        statsLogGattConnectionStateChange(
                BluetoothProfile.GATT, address, clientIf,
                BluetoothProtoEnums.CONNECTION_STATE_DISCONNECTING);
        gattClientDisconnectNative(clientIf, address, connId != null ? connId : 0);
    }

@@ -3962,14 +3976,18 @@ public class GattService extends ProfileService {
        if (app == null) {
            return;
        }

        int connectionState;
        if (connected) {
            mServerMap.addConnection(serverIf, connId, address);
            connectionState = BluetoothProtoEnums.CONNECTION_STATE_CONNECTED;
        } else {
            mServerMap.removeConnection(serverIf, connId);
            connectionState = BluetoothProtoEnums.CONNECTION_STATE_DISCONNECTED;
        }

        app.callback.onServerConnectionState((byte) 0, serverIf, connected, address);
        statsLogGattConnectionStateChange(
                BluetoothProfile.GATT_SERVER, address, serverIf, connectionState);
    }

    void onServerReadCharacteristic(String address, int connId, int transId, int handle, int offset,
@@ -4643,6 +4661,20 @@ public class GattService extends ProfileService {
        }
    }

    private void statsLogGattConnectionStateChange(
            int profile, String address, int sessionIndex, int connectionState) {
        BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address);
        BluetoothStatsLog.write(
                BluetoothStatsLog.BLUETOOTH_CONNECTION_STATE_CHANGED, connectionState,
                0 /* deprecated */, profile, new byte[0],
                mAdapterService.getMetricId(device), sessionIndex);
        if (DBG) {
            Log.d(TAG, "Gatt Logging: metric_id=" + mAdapterService.getMetricId(device)
                    + ", session_index=" + sessionIndex
                    + ", connection state=" + connectionState);
        }
    }

    @Override
    public void dumpProto(BluetoothMetricsProto.BluetoothLog.Builder builder) {
        synchronized (mScanEvents) {